diff options
author | Jan-Michael Brummer <jan.brummer@tabos.org> | 2023-03-17 08:57:25 -0400 |
---|---|---|
committer | Jan-Michael Brummer <jan.brummer@tabos.org> | 2023-03-27 16:31:58 +0200 |
commit | f20a56d73694ffcf37eccb6c8c31070def74aaba (patch) | |
tree | 66bcae2ba0b93b91e319f6d6b32f6e637f6e0c73 | |
parent | 98187d7567e333b4b0f8fbb199b39bb5eb096dda (diff) | |
download | libproxy-git-f20a56d73694ffcf37eccb6c8c31070def74aaba.tar.gz |
Add file monitor support (#81)
-rw-r--r-- | src/backend/plugins/config-kde/config-kde.c | 64 | ||||
-rw-r--r-- | src/backend/plugins/config-sysconfig/config-sysconfig.c | 53 | ||||
-rw-r--r-- | src/backend/plugins/config-sysconfig/meson.build | 2 |
3 files changed, 84 insertions, 35 deletions
diff --git a/src/backend/plugins/config-kde/config-kde.c b/src/backend/plugins/config-kde/config-kde.c index 24fb9e1..1079c19 100644 --- a/src/backend/plugins/config-kde/config-kde.c +++ b/src/backend/plugins/config-kde/config-kde.c @@ -40,8 +40,9 @@ typedef enum { struct _PxConfigKde { GObject parent_instance; - char *config_option; + char *config_file; gboolean available; + GFileMonitor *monitor; char *no_proxy; char *http_proxy; @@ -62,9 +63,25 @@ enum { PROP_CONFIG_OPTION }; +static void px_config_kde_set_config_file (PxConfigKde *self, + char *proxy_file); + static void -px_config_kde_read_config (PxConfigKde *self, - char *proxy_file) +on_file_changed (GFileMonitor *monitor, + GFile *file, + GFile *other_file, + GFileMonitorEvent event_type, + gpointer user_data) +{ + PxConfigKde *self = PX_CONFIG_KDE (user_data); + + g_debug ("%s: Reloading configuration\n", __FUNCTION__); + px_config_kde_set_config_file (self, g_file_get_path (file)); +} + +static void +px_config_kde_set_config_file (PxConfigKde *self, + char *proxy_file) { g_autoptr (GError) error = NULL; g_autofree char *line = NULL; @@ -72,15 +89,19 @@ px_config_kde_read_config (PxConfigKde *self, g_autoptr (GFileInputStream) istr = NULL; g_autoptr (GDataInputStream) dstr = NULL; - file = g_file_new_for_path (proxy_file); + g_clear_pointer (&self->config_file, g_free); + self->config_file = proxy_file ? g_strdup (proxy_file) : g_build_filename (g_get_user_config_dir (), "kioslaverc", NULL); + self->available = FALSE; + + file = g_file_new_for_path (self->config_file); if (!file) { - g_debug ("%s: Could not create file", __FUNCTION__); + g_debug ("%s: Could not create file for %s", __FUNCTION__, self->config_file); return; } istr = g_file_read (file, NULL, NULL); if (!istr) { - g_debug ("%s: Could not read file", __FUNCTION__); + g_debug ("%s: Could not read file %s", __FUNCTION__, self->config_file); return; } @@ -88,6 +109,13 @@ px_config_kde_read_config (PxConfigKde *self, if (!dstr) return; + g_clear_object (&self->monitor); + self->monitor = g_file_monitor (file, G_FILE_MONITOR_NONE, NULL, &error); + if (!self->monitor) + g_warning ("Could not add a file monitor for %s, error: %s", g_file_get_uri (file), error->message); + else + g_signal_connect_object (G_OBJECT (self->monitor), "changed", G_CALLBACK (on_file_changed), self, 0); + do { g_clear_pointer (&line, g_free); @@ -122,25 +150,11 @@ px_config_kde_read_config (PxConfigKde *self, } } } while (line); -} - -static -void -px_config_kde_set_config_file (PxConfigKde *self, - const char *file) -{ - g_autofree char *config = NULL; - - g_clear_pointer (&self->config_option, g_free); - self->config_option = file ? g_strdup (file) : NULL; - config = self->config_option ? g_strdup (self->config_option) : g_build_filename (g_get_user_config_dir (), "kioslaverc", NULL); - - self->available = g_file_test (config, G_FILE_TEST_EXISTS); - if (self->available) - px_config_kde_read_config (self, config); + self->available = TRUE; } + static void px_config_kde_init (PxConfigKde *self) { @@ -150,6 +164,10 @@ px_config_kde_init (PxConfigKde *self) static void px_config_kde_dispose (GObject *object) { + PxConfigKde *self = PX_CONFIG_KDE (object); + + g_clear_object (&self->monitor); + G_OBJECT_CLASS (px_config_kde_parent_class)->dispose (object); } @@ -182,7 +200,7 @@ px_config_kde_get_property (GObject *object, switch (prop_id) { case PROP_CONFIG_OPTION: - g_value_set_string (value, config->config_option); + g_value_set_string (value, config->config_file); break; default: diff --git a/src/backend/plugins/config-sysconfig/config-sysconfig.c b/src/backend/plugins/config-sysconfig/config-sysconfig.c index d7a1fa1..a7e7d47 100644 --- a/src/backend/plugins/config-sysconfig/config-sysconfig.c +++ b/src/backend/plugins/config-sysconfig/config-sysconfig.c @@ -28,9 +28,9 @@ struct _PxConfigSysConfig { GObject parent_instance; + GFileMonitor *monitor; - char *proxy_file; - char *config_option; + char *config_file; gboolean available; gboolean proxy_enabled; @@ -53,6 +53,22 @@ enum { PROP_CONFIG_OPTION }; +static void px_config_sysconfig_set_config_file (PxConfigSysConfig *self, + const char *config_file); + +static void +on_file_changed (GFileMonitor *monitor, + GFile *file, + GFile *other_file, + GFileMonitorEvent event_type, + gpointer user_data) +{ + PxConfigSysConfig *self = PX_CONFIG_SYSCONFIG (user_data); + + g_debug ("%s: Reloading configuration", __FUNCTION__); + px_config_sysconfig_set_config_file (self, g_file_get_path (file)); +} + static void px_config_sysconfig_set_config_file (PxConfigSysConfig *self, @@ -65,21 +81,19 @@ px_config_sysconfig_set_config_file (PxConfigSysConfig *self, g_autoptr (GDataInputStream) dstr = NULL; char *line = NULL; - g_clear_pointer (&self->config_option, g_free); - self->config_option = config_file ? g_strdup (config_file) : NULL; - - self->proxy_file = g_strdup (self->config_option ? self->config_option : "/etc/sysconfig/proxy"); + g_clear_pointer (&self->config_file, g_free); + self->config_file = g_strdup (config_file ? config_file : "/etc/sysconfig/proxy"); self->available = FALSE; - file = g_file_new_for_path (self->proxy_file); + file = g_file_new_for_path (self->config_file); if (!file) { - g_debug ("%s: Could not create file", __FUNCTION__); + g_debug ("%s: Could not create file for %s", __FUNCTION__, self->config_file); return; } istr = g_file_read (file, NULL, NULL); if (!istr) { - g_debug ("%s: Could not read file", __FUNCTION__); + g_debug ("%s: Could not read file %s", __FUNCTION__, self->config_file); return; } @@ -87,6 +101,13 @@ px_config_sysconfig_set_config_file (PxConfigSysConfig *self, if (!dstr) return; + g_clear_object (&self->monitor); + self->monitor = g_file_monitor (file, G_FILE_MONITOR_NONE, NULL, &error); + if (!self->monitor) + g_warning ("Could not add a file monitor for %s, error: %s", g_file_get_uri (file), error->message); + else + g_signal_connect_object (G_OBJECT (self->monitor), "changed", G_CALLBACK (on_file_changed), self, 0); + do { g_clear_pointer (&line, g_free); @@ -123,7 +144,6 @@ px_config_sysconfig_set_config_file (PxConfigSysConfig *self, static void px_config_sysconfig_init (PxConfigSysConfig *self) { - px_config_sysconfig_set_config_file (self, NULL); } static void @@ -155,7 +175,7 @@ px_config_sysconfig_get_property (GObject *object, switch (prop_id) { case PROP_CONFIG_OPTION: - g_value_set_string (value, config->config_option); + g_value_set_string (value, config->config_file); break; default: @@ -165,10 +185,21 @@ px_config_sysconfig_get_property (GObject *object, } static void +px_config_sysconfig_dispose (GObject *object) +{ + PxConfigSysConfig *self = PX_CONFIG_SYSCONFIG (object); + + g_clear_object (&self->monitor); + + G_OBJECT_CLASS (px_config_sysconfig_parent_class)->dispose (object); +} + +static void px_config_sysconfig_class_init (PxConfigSysConfigClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); + object_class->dispose = px_config_sysconfig_dispose; object_class->set_property = px_config_sysconfig_set_property; object_class->get_property = px_config_sysconfig_get_property; diff --git a/src/backend/plugins/config-sysconfig/meson.build b/src/backend/plugins/config-sysconfig/meson.build index 2ac17fb..1aef00b 100644 --- a/src/backend/plugins/config-sysconfig/meson.build +++ b/src/backend/plugins/config-sysconfig/meson.build @@ -32,4 +32,4 @@ custom_target( install: true, ) -endif
\ No newline at end of file +endif |