Save main and project configuration whenever documents are opened/closed

The main idea is to save the session file list more often to prevent
accidental lost but saving the rest of the configuration might help
as well.
To prevent too many save attempts, an idle function is used and it's
only added once until it was executed.
master
Enrico Tröger 2019-03-28 20:35:05 +01:00
parent 82590af4cc
commit 26996aa1aa
2 changed files with 39 additions and 1 deletions

View File

@ -141,7 +141,8 @@ signal void (*document_close)(GObject *obj, GeanyDocument *doc, gpointer user_da
signal void (*project_open)(GObject *obj, GKeyFile *config, gpointer user_data);
/** Sent when a project is saved (happens when the project is created, the properties
* dialog is closed, before the project is closed, or when Geany is exited).
* dialog is closed, before the project is closed, when Geany automatically
* saves its configuration by opening/closing documents or when Geany is exited).
* This signal is emitted shortly before Geany will write the contents of the
* GKeyFile to the disc.
*

View File

@ -106,6 +106,7 @@ static GPtrArray *session_files = NULL;
static gint session_notebook_page;
static gint hpan_position;
static gint vpan_position;
static guint document_list_update_idle_func_id = 0;
static const gchar atomic_file_saving_key[] = "use_atomic_file_saving";
static GPtrArray *keyfile_groups = NULL;
@ -1327,11 +1328,45 @@ void configuration_apply_settings(void)
}
static gboolean save_configuration_cb(gpointer data)
{
configuration_save();
if (app->project != NULL)
{
project_write_config();
}
document_list_update_idle_func_id = 0;
return G_SOURCE_REMOVE;
}
static void document_list_changed_cb(GObject *obj, GeanyDocument *doc, gpointer data)
{
g_return_if_fail(doc != NULL && doc->is_valid);
/* save configuration, especially session file list, but only if we are not just starting
* and not about to quit */
if (main_status.main_window_realized &&
!main_status.opening_session_files &&
!main_status.quitting)
{
if (document_list_update_idle_func_id == 0)
{
document_list_update_idle_func_id = g_idle_add(save_configuration_cb, NULL);
}
}
}
void configuration_init(void)
{
keyfile_groups = g_ptr_array_new();
pref_groups = g_ptr_array_new();
init_pref_groups();
g_signal_connect(geany_object, "document-open", G_CALLBACK(document_list_changed_cb), NULL);
g_signal_connect(geany_object, "document-save", G_CALLBACK(document_list_changed_cb), NULL);
g_signal_connect(geany_object, "document-close", G_CALLBACK(document_list_changed_cb), NULL);
}
@ -1340,6 +1375,8 @@ void configuration_finalize(void)
guint i;
StashGroup *group;
g_signal_handlers_disconnect_by_func(geany_object, G_CALLBACK(document_list_changed_cb), NULL);
foreach_ptr_array(group, i, keyfile_groups)
stash_group_free(group);