From 7f06c25230db49e6a7dcf00692cc2d1bae9ec6ea Mon Sep 17 00:00:00 2001
From: Lukas Fleischer <calcurse@cryptocrack.de>
Date: Fri, 10 Oct 2014 01:46:24 +0200
Subject: Factor out item reload code

Adds a new function io_reload_data() which can be used to reload all
appointments and TODO items.

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
---
 src/calcurse.c | 93 +-------------------------------------------------------
 src/calcurse.h |  1 +
 src/io.c       | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 97 insertions(+), 92 deletions(-)

(limited to 'src')

diff --git a/src/calcurse.c b/src/calcurse.c
index 364d0fa..dd5abf5 100644
--- a/src/calcurse.c
+++ b/src/calcurse.c
@@ -256,102 +256,11 @@ static inline void key_generic_save(void)
 
 static inline void key_generic_reload(void)
 {
-	char *msg_um_asktype = NULL;
-	const char *reload_success =
-		_("The data files were reloaded successfully");
-	const char *enter = _("Press [ENTER] to continue");
-
-	if (io_get_modified()) {
-		const char *msg_um_prefix =
-				_("There are unsaved modifications:");
-		const char *msg_um_discard = _("(d)iscard");
-		const char *msg_um_merge = _("(m)erge");
-		const char *msg_um_keep = _("(k)eep and cancel");
-		const char *msg_um_choice = _("[dmk]");
-
-		asprintf(&msg_um_asktype, "%s %s, %s, %s", msg_um_prefix,
-			 msg_um_discard, msg_um_merge, msg_um_keep);
-
-		char *path_apts_backup, *path_todo_backup;
-		const char *backup_ext = ".sav";
-
-		switch (status_ask_choice(msg_um_asktype, msg_um_choice, 3)) {
-		case 1:
-			break;
-		case 2:
-			asprintf(&path_apts_backup, "%s%s", path_apts,
-				 backup_ext);
-			asprintf(&path_todo_backup, "%s%s", path_todo,
-				 backup_ext);
-
-			io_save_mutex_lock();
-			io_save_apts(path_apts_backup);
-			io_save_todo(path_todo_backup);
-			io_save_mutex_unlock();
-
-			if (!io_files_equal(path_apts, path_apts_backup)) {
-				const char *arg_apts[] = { conf.mergetool,
-							   path_apts,
-							   path_apts_backup,
-							   NULL };
-				wins_launch_external(arg_apts);
-			}
-
-			if (!io_files_equal(path_todo, path_todo_backup)) {
-				const char *arg_todo[] = { conf.mergetool,
-							   path_todo,
-							   path_todo_backup,
-							   NULL };
-				wins_launch_external(arg_todo);
-			}
-
-			xfree(path_apts_backup);
-			xfree(path_todo_backup);
-			break;
-		case 3:
-			/* FALLTHROUGH */
-		default:
-			wins_update(FLAG_STA);
-			goto cleanup;
-		}
-	}
-
-	if (notify_bar())
-		notify_stop_main_thread();
-
-	/* Reinitialize data structures. */
-	apoint_llist_free();
-	event_llist_free();
-	recur_apoint_llist_free();
-	recur_event_llist_free();
-	todo_free_list();
-
-	apoint_llist_init();
-	event_llist_init();
-	recur_apoint_llist_init();
-	recur_event_llist_init();
-	todo_init_list();
-
-	io_load_data(NULL);
-	io_unset_modified();
-	ui_todo_load_items();
-	ui_todo_sel_reset();
-
-	if (conf.system_dialogs) {
-		status_mesg(reload_success, enter);
-		wgetch(win[KEY].p);
-	}
-
-	if (notify_bar())
-		notify_start_main_thread();
-
+	io_reload_data();
 	do_storage(0);
 	notify_check_next_app(1);
 	ui_calendar_monthly_view_cache_set_invalid();
 	wins_update(FLAG_ALL);
-
-cleanup:
-	mem_free(msg_um_asktype);
 }
 
 static inline void key_generic_import(void)
diff --git a/src/calcurse.h b/src/calcurse.h
index dc5b13e..4a6ea24 100644
--- a/src/calcurse.h
+++ b/src/calcurse.h
@@ -797,6 +797,7 @@ void io_save_cal(enum save_display);
 void io_load_app(struct item_filter *);
 void io_load_todo(struct item_filter *);
 void io_load_data(struct item_filter *);
+void io_reload_data(void);
 void io_load_keys(const char *);
 int io_check_dir(const char *);
 unsigned io_dir_exists(const char *);
diff --git a/src/io.c b/src/io.c
index d515249..0f3d908 100644
--- a/src/io.c
+++ b/src/io.c
@@ -693,6 +693,101 @@ void io_load_data(struct item_filter *filter)
 	io_load_todo(filter);
 }
 
+void io_reload_data(void)
+{
+	char *msg_um_asktype = NULL;
+	const char *reload_success =
+		_("The data files were reloaded successfully");
+	const char *enter = _("Press [ENTER] to continue");
+
+	if (io_get_modified()) {
+		const char *msg_um_prefix =
+				_("There are unsaved modifications:");
+		const char *msg_um_discard = _("(d)iscard");
+		const char *msg_um_merge = _("(m)erge");
+		const char *msg_um_keep = _("(k)eep and cancel");
+		const char *msg_um_choice = _("[dmk]");
+
+		asprintf(&msg_um_asktype, "%s %s, %s, %s", msg_um_prefix,
+			 msg_um_discard, msg_um_merge, msg_um_keep);
+
+		char *path_apts_backup, *path_todo_backup;
+		const char *backup_ext = ".sav";
+
+		switch (status_ask_choice(msg_um_asktype, msg_um_choice, 3)) {
+		case 1:
+			break;
+		case 2:
+			asprintf(&path_apts_backup, "%s%s", path_apts,
+				 backup_ext);
+			asprintf(&path_todo_backup, "%s%s", path_todo,
+				 backup_ext);
+
+			io_save_mutex_lock();
+			io_save_apts(path_apts_backup);
+			io_save_todo(path_todo_backup);
+			io_save_mutex_unlock();
+
+			if (!io_files_equal(path_apts, path_apts_backup)) {
+				const char *arg_apts[] = { conf.mergetool,
+							   path_apts,
+							   path_apts_backup,
+							   NULL };
+				wins_launch_external(arg_apts);
+			}
+
+			if (!io_files_equal(path_todo, path_todo_backup)) {
+				const char *arg_todo[] = { conf.mergetool,
+							   path_todo,
+							   path_todo_backup,
+							   NULL };
+				wins_launch_external(arg_todo);
+			}
+
+			xfree(path_apts_backup);
+			xfree(path_todo_backup);
+			break;
+		case 3:
+			/* FALLTHROUGH */
+		default:
+			wins_update(FLAG_STA);
+			goto cleanup;
+		}
+	}
+
+	if (notify_bar())
+		notify_stop_main_thread();
+
+	/* Reinitialize data structures. */
+	apoint_llist_free();
+	event_llist_free();
+	recur_apoint_llist_free();
+	recur_event_llist_free();
+	todo_free_list();
+
+	apoint_llist_init();
+	event_llist_init();
+	recur_apoint_llist_init();
+	recur_event_llist_init();
+	todo_init_list();
+
+	io_load_data(NULL);
+	io_unset_modified();
+	ui_todo_load_items();
+	ui_todo_sel_reset();
+
+	if (conf.system_dialogs) {
+		status_mesg(reload_success, enter);
+		wgetch(win[KEY].p);
+	}
+
+	if (notify_bar())
+		notify_start_main_thread();
+
+cleanup:
+	mem_free(msg_um_asktype);
+}
+
 static void
 load_keys_ht_getkey(struct ht_keybindings_s *data, const char **key,
 		    int *len)
-- 
cgit v1.2.3-70-g09d2