[gnome-font-viewer] font-model: install file monitors on fontconfig font directories



commit b3c81f6375b3731dc05fb6567b7f0ad70ebb6b00
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Thu May 3 13:04:25 2012 -0400

    font-model: install file monitors on fontconfig font directories
    
    So we can update our model when a font file is added there.

 src/font-model.c |   77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 77 insertions(+), 0 deletions(-)
---
diff --git a/src/font-model.c b/src/font-model.c
index e4fc1ab..c7a3308 100644
--- a/src/font-model.c
+++ b/src/font-model.c
@@ -46,8 +46,17 @@ struct _FontViewModelPrivate {
     FcFontSet *font_list;
 
     FT_Library library;
+
+    GList *monitors;
+};
+
+enum {
+    CONFIG_CHANGED,
+    NUM_SIGNALS
 };
 
+static guint signals[NUM_SIGNALS] = { 0, };
+
 static void ensure_thumbnail (FontViewModel *self, const gchar *path);
 
 G_DEFINE_TYPE (FontViewModel, font_view_model, GTK_TYPE_LIST_STORE);
@@ -381,6 +390,17 @@ ensure_font_list (FontViewModel *self)
     FcChar8 *file;
     gchar *font_name;
 
+    if (self->priv->font_list) {
+            FcFontSetDestroy (self->priv->font_list);
+            self->priv->font_list = NULL;
+    }
+
+    gtk_list_store_clear (GTK_LIST_STORE (self));
+
+    /* always reinitialize the font database */
+    if (!FcInitReinitialize())
+        return;
+
     pat = FcPatternCreate ();
     os = FcObjectSetBuild (FC_FILE, FC_FAMILY, FC_WEIGHT, FC_SLANT, NULL);
 
@@ -433,6 +453,52 @@ 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)
+{
+    FontViewModel *self = user_data;
+
+    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);
+        g_signal_emit (self, signals[CONFIG_CHANGED], 0);
+    }
+}
+
+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);
+}
+
+static void
 font_view_model_init (FontViewModel *self)
 {
     GType types[NUM_COLUMNS] =
@@ -453,7 +519,9 @@ font_view_model_init (FontViewModel *self)
                                      COLUMN_NAME,
                                      font_view_model_sort_func,
                                      NULL, NULL);
+
     ensure_font_list (self);
+    create_file_monitors (self);
 }
 
 static void
@@ -471,6 +539,8 @@ font_view_model_finalize (GObject *obj)
         self->priv->library = NULL;
     }
 
+    g_list_free_full (self->priv->monitors, (GDestroyNotify) g_object_unref);
+
     G_OBJECT_CLASS (font_view_model_parent_class)->finalize (obj);
 }
 
@@ -480,6 +550,13 @@ font_view_model_class_init (FontViewModelClass *klass)
     GObjectClass *oclass = G_OBJECT_CLASS (klass);
     oclass->finalize = font_view_model_finalize;
 
+    signals[CONFIG_CHANGED] = 
+        g_signal_new ("config-changed",
+                      FONT_VIEW_TYPE_MODEL,
+                      G_SIGNAL_RUN_FIRST,
+                      0, NULL, NULL, NULL,
+                      G_TYPE_NONE, 0);
+
     g_type_class_add_private (klass, sizeof (FontViewModelPrivate));
 }
 



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