26 #include "gnc-autosave.h"
28 #include <glib/gi18n.h>
30 #include "gnc-session.h"
36 #include "gnc-gui-query.h"
38 #define GNC_PREF_AUTOSAVE_SHOW_EXPLANATION "autosave-show-explanation"
39 #define GNC_PREF_AUTOSAVE_INTERVAL "autosave-interval-minutes"
40 #define AUTOSAVE_SOURCE_ID "autosave_source_id"
45 #define G_LOG_DOMAIN "gnc.gui.autosave"
48 autosave_remove_timer_cb(
QofBook *book, gpointer key, gpointer user_data);
78 static gboolean autosave_confirm(GtkWidget *toplevel)
83 gboolean switch_off_autosave, show_expl_again, save_now;
86 #define YES_THIS_TIME 1
89 #define NO_NOT_THIS_TIME 4
93 gtk_message_dialog_new(GTK_WINDOW(toplevel),
94 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
98 _(
"Save file automatically?"));
99 gtk_message_dialog_format_secondary_text
100 (GTK_MESSAGE_DIALOG(dialog),
101 ngettext(
"Your data file needs to be saved to your hard disk to save your changes. "
102 "GnuCash has a feature to save the file automatically every %d minute, "
103 "just as if you had pressed the \"Save\" button each time. \n\n"
104 "You can change the time interval or turn off this feature under "
105 "Edit -> Preferences -> General -> Auto-save time interval. \n\n"
106 "Should your file be saved automatically?",
107 "Your data file needs to be saved to your hard disk to save your changes. "
108 "GnuCash has a feature to save the file automatically every %d minutes, "
109 "just as if you had pressed the \"Save\" button each time. \n\n"
110 "You can change the time interval or turn off this feature under "
111 "Edit -> Preferences -> General -> Auto-save time interval. \n\n"
112 "Should your file be saved automatically?",
115 gtk_dialog_add_buttons(GTK_DIALOG(dialog),
116 _(
"_Yes, this time"), YES_THIS_TIME,
117 _(
"Yes, _always"), YES_ALWAYS,
118 _(
"No, n_ever"), NO_NEVER,
119 _(
"_No, not this time"), NO_NOT_THIS_TIME,
121 gtk_dialog_set_default_response( GTK_DIALOG(dialog), NO_NOT_THIS_TIME);
124 response = gtk_dialog_run( GTK_DIALOG( dialog ) );
125 gtk_widget_destroy( dialog );
131 switch_off_autosave = FALSE;
132 show_expl_again = TRUE;
136 switch_off_autosave = FALSE;
137 show_expl_again = FALSE;
141 switch_off_autosave = TRUE;
142 show_expl_again = FALSE;
146 case NO_NOT_THIS_TIME:
147 switch_off_autosave = FALSE;
148 show_expl_again = TRUE;
153 gnc_prefs_set_bool(GNC_PREFS_GROUP_GENERAL, GNC_PREF_AUTOSAVE_SHOW_EXPLANATION, show_expl_again);
154 g_debug(
"autosave_timeout_cb: Show explanation again=%s\n",
155 (show_expl_again ?
"TRUE" :
"FALSE"));
158 if (switch_off_autosave)
161 g_debug(
"autosave_timeout_cb: User chose to disable auto-save.\n");
168 static gboolean autosave_timeout_cb(gpointer user_data)
171 gboolean show_explanation;
172 gboolean save_now = TRUE;
175 g_debug(
"autosave_timeout_cb called\n");
180 if (gnc_file_save_in_progress() || !gnc_current_session_exist()
190 if (show_explanation)
192 save_now = autosave_confirm(toplevel);
197 g_debug(
"autosave_timeout_cb: Really trigger auto-save now.\n");
200 if (GNC_IS_MAIN_WINDOW(toplevel))
203 g_debug(
"autosave_timeout_cb: toplevel is not a GNC_MAIN_WINDOW\n");
204 if (GNC_IS_WINDOW(toplevel))
205 gnc_window_set_progressbar_window( GNC_WINDOW( toplevel ) );
207 g_debug(
"autosave_timeout_cb: toplevel is not a GNC_WINDOW\n");
221 g_debug(
"autosave_timeout_cb: No auto-save this time, let the timeout run again.\n");
229 autosave_remove_timer_cb(
QofBook *book, gpointer key, gpointer user_data)
231 guint autosave_source_id = GPOINTER_TO_UINT(user_data);
234 if (autosave_source_id > 0)
236 res = g_source_remove (autosave_source_id);
237 g_debug(
"Removing auto save timer with id %d, result=%s\n",
238 autosave_source_id, (res ?
"TRUE" :
"FALSE"));
242 GUINT_TO_POINTER(0), autosave_remove_timer_cb);
246 void gnc_autosave_remove_timer(
QofBook *book)
248 autosave_remove_timer_cb(book, AUTOSAVE_SOURCE_ID,
252 static void gnc_autosave_add_timer(
QofBook *book)
254 guint interval_mins =
258 if ( interval_mins > 0
259 && ( ! gnc_file_save_in_progress() )
260 && gnc_current_session_exist() )
264 guint autosave_source_id =
265 g_timeout_add_seconds(interval_mins * 60,
266 autosave_timeout_cb, book);
267 g_debug(
"Adding new auto-save timer with id %d\n", autosave_source_id);
272 GUINT_TO_POINTER(autosave_source_id),
273 autosave_remove_timer_cb);
277 void gnc_autosave_dirty_handler (
QofBook *book, gboolean dirty)
279 g_debug(
"gnc_main_window_autosave_dirty(dirty = %s)\n",
280 (dirty ?
"TRUE" :
"FALSE"));
294 gnc_autosave_remove_timer(book);
297 gnc_autosave_add_timer(book);
301 g_debug(
"Shutting down book, ignoring dirty book");
308 gnc_autosave_remove_timer(book);
Functions that are supported by all types of windows.
Functions for adding content to a window.
gboolean gnc_prefs_set_bool(const gchar *group, const gchar *pref_name, gboolean value)
void qof_book_set_data_fin(QofBook *book, const gchar *key, gpointer data, QofBookFinalCB)
gboolean gnc_prefs_set_float(const gchar *group, const gchar *pref_name, gdouble value)
All type declarations for the whole Gnucash engine.
Generic api to store and retrieve preferences.
gboolean qof_book_is_readonly(const QofBook *book)
gboolean gnc_prefs_get_bool(const gchar *group, const gchar *pref_name)
GtkWidget * gnc_ui_get_toplevel(void)
gboolean qof_book_shutting_down(const QofBook *book)
gpointer qof_book_get_data(const QofBook *book, const gchar *key)
void gnc_main_window_set_progressbar_window(GncMainWindow *window)
gdouble gnc_prefs_get_float(const gchar *group, const gchar *pref_name)