[epiphany] Keep track of changes to users css file



commit 0fc899967707025ec8984b83e69b4fdfaa8917a4
Author: Jan-Michael Brummer <jan brummer tabos org>
Date:   Mon Dec 24 23:19:27 2018 +0100

    Keep track of changes to users css file
    
    Fixes: https://gitlab.gnome.org/GNOME/epiphany/issues/370

 embed/ephy-embed-prefs.c | 41 +++++++++++++++++++++++++++++++++++------
 1 file changed, 35 insertions(+), 6 deletions(-)
---
diff --git a/embed/ephy-embed-prefs.c b/embed/ephy-embed-prefs.c
index fda4f3f6a..e7884c334 100644
--- a/embed/ephy-embed-prefs.c
+++ b/embed/ephy-embed-prefs.c
@@ -37,7 +37,9 @@ typedef struct {
 } PrefData;
 
 #define DEFAULT_ENCODING_SETTING "default-charset"
+// FIXME: Refactor this code to remove the need of those globals
 static WebKitSettings *webkit_settings = NULL;
+static GFileMonitor *user_style_sheet_monitor = NULL;
 
 static void
 user_style_sheet_output_stream_splice_cb (GOutputStream *output_stream,
@@ -60,9 +62,9 @@ user_style_sheet_output_stream_splice_cb (GOutputStream *output_stream,
 }
 
 static void
-user_style_seet_read_cb (GFile        *file,
-                         GAsyncResult *result,
-                         gpointer      user_data)
+user_style_sheet_read_cb (GFile        *file,
+                          GAsyncResult *result,
+                          gpointer      user_data)
 {
   GFileInputStream *input_stream;
   GOutputStream *output_stream;
@@ -83,6 +85,21 @@ user_style_seet_read_cb (GFile        *file,
   g_object_unref (output_stream);
 }
 
+static void
+user_style_sheet_file_changed (GFileMonitor      *monitor,
+                               GFile             *file,
+                               GFile             *other_file,
+                               GFileMonitorEvent  event_type,
+                               gpointer           user_data)
+{
+  if (event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT) {
+    webkit_user_content_manager_remove_all_style_sheets (WEBKIT_USER_CONTENT_MANAGER 
(ephy_embed_shell_get_user_content_manager (ephy_embed_shell_get_default ())));
+
+    g_file_read_async (file, G_PRIORITY_DEFAULT, NULL,
+                       (GAsyncReadyCallback)user_style_sheet_read_cb, NULL);
+  }
+}
+
 static void
 webkit_pref_callback_user_stylesheet (GSettings  *settings,
                                       const char *key,
@@ -92,10 +109,12 @@ webkit_pref_callback_user_stylesheet (GSettings  *settings,
 
   value = g_settings_get_boolean (settings, key);
 
-  if (!value)
+  if (!value) {
+    g_clear_object (&user_style_sheet_monitor);
     webkit_user_content_manager_remove_all_style_sheets (WEBKIT_USER_CONTENT_MANAGER 
(ephy_embed_shell_get_user_content_manager (ephy_embed_shell_get_default ())));
-  else {
+  } else {
     GFile *file;
+    GError *error = NULL;
     char *filename;
 
     filename = g_build_filename (ephy_dot_dir (), USER_STYLESHEET_FILENAME, NULL);
@@ -103,7 +122,17 @@ webkit_pref_callback_user_stylesheet (GSettings  *settings,
     g_free (filename);
 
     g_file_read_async (file, G_PRIORITY_DEFAULT, NULL,
-                       (GAsyncReadyCallback)user_style_seet_read_cb, NULL);
+                       (GAsyncReadyCallback)user_style_sheet_read_cb, NULL);
+
+    g_assert (user_style_sheet_monitor == NULL);
+    user_style_sheet_monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error);
+    if (user_style_sheet_monitor == NULL) {
+      g_warning ("Could not create a file monitor for %s: %s\n", g_file_get_uri (file), error->message);
+      g_error_free (error);
+    } else {
+      g_signal_connect (user_style_sheet_monitor, "changed", G_CALLBACK (user_style_sheet_file_changed), 
NULL);
+    }
+
     g_object_unref (file);
   }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]