From 30449ce7afd4c9d33c17bc22eebae2bb01cff3f5 Mon Sep 17 00:00:00 2001 From: Patrick McDermott Date: Fri, 10 Nov 2017 10:50:24 -0500 Subject: mq_profiles_new(): Monitor profiles file for changes --- (limited to 'src') diff --git a/src/config/profiles.c b/src/config/profiles.c index bf08af0..3c8abad 100644 --- a/src/config/profiles.c +++ b/src/config/profiles.c @@ -111,6 +111,55 @@ insert(MqProfiles *profiles, const gchar *id, const gchar *name, return TRUE; } +static void +changed(GFileMonitor G_GNUC_UNUSED *monitor, GFile G_GNUC_UNUSED *file, + GFile G_GNUC_UNUSED *other_file, + GFileMonitorEvent G_GNUC_UNUSED event_type, MqProfiles *profiles) +{ + gchar *cur_name; + gchar *cur_color; + + /* Save the current profile. */ + if (profiles->current) { + cur_name = mq_profiles_get_name( profiles, profiles->current); + cur_color = mq_profiles_get_color(profiles, profiles->current); + } + + /* Get changes. */ + load(profiles); + + /* If another process removed the current profile, restore it. All + * other processes will reread the file and see the profile re-inserted. + * Multiple processes of the same profile may re-insert their profile, + * but they should all write the same contents to the file. But this + * can cause a thundering hurd as all running processes read and/or + * write the file in response to each other's changes. */ + if (profiles->current) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" /* False positive */ + if (!g_key_file_has_group(profiles->key_file, + profiles->current)) { + insert(profiles, profiles->current, + cur_name, cur_color); + mq_profiles_save(profiles); + } + g_free(cur_name); + g_free(cur_color); +#pragma GCC diagnostic pop + } +} + +static void +monitor(MqProfiles *profiles) +{ + GFile *file; + GFileMonitor *monitor; + + file = g_file_new_for_path(profiles->file_name); + monitor = g_file_monitor_file(file, G_FILE_MONITOR_NONE, NULL, NULL); + g_signal_connect(monitor, "changed", G_CALLBACK(changed), profiles); +} + MqProfiles * mq_profiles_new(void) { @@ -130,7 +179,7 @@ mq_profiles_new(void) load(profiles); } - /* TODO: Set up GFileMonitor. */ + monitor(profiles); return profiles; } -- cgit v0.9.1