summaryrefslogtreecommitdiff
path: root/font-viewer/font-model.c
diff options
context:
space:
mode:
authorraveit65 <[email protected]>2017-01-08 18:38:01 +0100
committerraveit65 <[email protected]>2017-02-17 12:28:22 +0100
commit9ee57014854f8290888f2c479d02cdcd67a124a6 (patch)
tree48b455403136575d7642cf101aea8e48dfd2f9e5 /font-viewer/font-model.c
parent80c5618cfdc28be14bbfea6d69a24061acfc1180 (diff)
downloadmate-control-center-9ee57014854f8290888f2c479d02cdcd67a124a6.tar.bz2
mate-control-center-9ee57014854f8290888f2c479d02cdcd67a124a6.tar.xz
Fontviewer: font-model, install file monitors on fontconfig font directories
So we can update our model when a font file is added there. taken from: https://git.gnome.org/browse/gnome-font-viewer/commit/?id=b3c81f6
Diffstat (limited to 'font-viewer/font-model.c')
-rw-r--r--font-viewer/font-model.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/font-viewer/font-model.c b/font-viewer/font-model.c
index 9a365527..887de59f 100644
--- a/font-viewer/font-model.c
+++ b/font-viewer/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,10 @@ 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 +540,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 +551,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));
}