diff options
author | Patrick McDermott <pj@pehjota.net> | 2017-11-10 10:50:24 (EST) |
---|---|---|
committer | Patrick McDermott <pj@pehjota.net> | 2017-11-10 11:03:43 (EST) |
commit | 30449ce7afd4c9d33c17bc22eebae2bb01cff3f5 (patch) | |
tree | 39323a664018083a4669d6592b95b66940430d0e /src | |
parent | e4a23a27484124dc0c1c23a06177f81767ed5fe7 (diff) | |
download | marquee-30449ce7afd4c9d33c17bc22eebae2bb01cff3f5.zip marquee-30449ce7afd4c9d33c17bc22eebae2bb01cff3f5.tar.gz marquee-30449ce7afd4c9d33c17bc22eebae2bb01cff3f5.tar.bz2 |
mq_profiles_new(): Monitor profiles file for changes
Diffstat (limited to 'src')
-rw-r--r-- | src/config/profiles.c | 51 |
1 files changed, 50 insertions, 1 deletions
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; } |