[gnome-font-viewer] model: listen to fontconfig timestamp notifications



commit 15c98a962ae1fe5bea03c71943313c3abffbe6e6
Author: Cosimo Cecchi <cosimo endlessm com>
Date:   Wed Oct 26 12:15:22 2016 -0700

    model: listen to fontconfig timestamp notifications
    
    Instead of watching all the font directories manually, connect to the
    fontconfig timestamp notification.
    This also fixes a race condition where we were trying to recreate the
    configuration of fontconfig before font files were completely written,
    which caused installed fonts not to get picked up by fontconfig under
    certain circumstances.
    
    Note that this relies on an agent being present in the session updating
    the Fontconfig/Timestamp XSETTING. In GNOME, gnome-settings-daemon takes
    care of that.

 src/font-model.c |   57 ++++++++++++++---------------------------------------
 1 files changed, 15 insertions(+), 42 deletions(-)
---
diff --git a/src/font-model.c b/src/font-model.c
index f49ff8f..e40a2ec 100644
--- a/src/font-model.c
+++ b/src/font-model.c
@@ -50,12 +50,12 @@ struct _FontViewModelPrivate {
 
     FT_Library library;
 
-    GList *monitors;
     cairo_surface_t *fallback_icon;
     GCancellable *cancellable;
 
     gint scale_factor;
     guint font_list_idle_id;
+    guint fontconfig_update_id;
 };
 
 enum {
@@ -556,47 +556,14 @@ font_view_model_sort_func (GtkTreeModel *model,
 }
 
 static void
-file_monitor_changed_cb (GFileMonitor *monitor,
-                         GFile *file,
-                         GFile *other_file,
-                         GFileMonitorEvent event,
-                         gpointer user_data)
+connect_to_fontconfig_updates (FontViewModel *self)
 {
-    FontViewModel *self = user_data;
+    GtkSettings *settings;
 
-    if (event == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT ||
-        event == G_FILE_MONITOR_EVENT_DELETED ||
-        event == G_FILE_MONITOR_EVENT_CREATED)
-        ensure_font_list (self);
-}
-
-static void
-create_file_monitors (FontViewModel *self)
-{
-    FcConfig *config;
-    FcStrList *str_list;
-    FcChar8 *path;
-    GFile *file;
-    GFileMonitor *monitor;
-
-    config = FcConfigGetCurrent ();
-    str_list = FcConfigGetFontDirs (config);
-
-    while ((path = FcStrListNext (str_list)) != NULL) {
-        file = g_file_new_for_path ((const gchar *) path);
-        monitor = g_file_monitor (file, G_FILE_MONITOR_NONE,
-                                  NULL, NULL);
-
-        if (monitor != NULL) {
-            self->priv->monitors = g_list_prepend (self->priv->monitors, monitor);
-            g_signal_connect (monitor, "changed",
-                              G_CALLBACK (file_monitor_changed_cb), self);
-        }
-
-        g_object_unref (file);
-    }
-
-    FcStrListDone (str_list);
+    settings = gtk_settings_get_default ();
+    self->priv->fontconfig_update_id =
+        g_signal_connect_swapped (settings, "notify::gtk-fontconfig-timestamp",
+                                  G_CALLBACK (ensure_font_list), self);
 }
 
 static void
@@ -624,13 +591,14 @@ font_view_model_init (FontViewModel *self)
                                      NULL, NULL);
 
     schedule_update_font_list (self);
-    create_file_monitors (self);
+    connect_to_fontconfig_updates (self);
 }
 
 static void
 font_view_model_finalize (GObject *obj)
 {
     FontViewModel *self = FONT_VIEW_MODEL (obj);
+    GtkSettings *settings;
 
     if (self->priv->cancellable) {
         g_cancellable_cancel (self->priv->cancellable);
@@ -652,9 +620,14 @@ font_view_model_finalize (GObject *obj)
         self->priv->font_list_idle_id = 0;
     }
 
+    if (self->priv->fontconfig_update_id != 0) {
+        settings = gtk_settings_get_default ();
+        g_signal_handler_disconnect (settings, self->priv->fontconfig_update_id);
+        self->priv->fontconfig_update_id = 0;
+    }
+
     g_mutex_clear (&self->priv->font_list_mutex);
     g_clear_pointer (&self->priv->fallback_icon, cairo_surface_destroy);
-    g_list_free_full (self->priv->monitors, (GDestroyNotify) g_object_unref);
 
     G_OBJECT_CLASS (font_view_model_parent_class)->finalize (obj);
 }


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