diff options
author | Gaël Bonithon <gael@xfce.org> | 2021-12-29 10:48:14 +0100 |
---|---|---|
committer | Gaël Bonithon <gael@xfce.org> | 2021-12-29 13:09:43 +0100 |
commit | 7fdab65f7a2144a7cae4d49bea13cea3644dfc3c (patch) | |
tree | 8bf0045d59c05be0f26d78ffb4756e4e9ddfa997 | |
parent | 270289b3a3f2f9ac7891f26135bae6d5e574c868 (diff) | |
download | tumbler-7fdab65f7a2144a7cae4d49bea13cea3644dfc3c.tar.gz |
desktop-thumbnailer: Allow to override configuration in desktop files
The "X-Tumbler Settings" group is searched in each desktop file with the
same keys as in `tumbler.rc`, where the default configuration is now.
Closes #52.
-rw-r--r-- | docs/reference/tumbler/tumbler-sections.txt | 2 | ||||
-rw-r--r-- | plugins/desktop-thumbnailer/desktop-thumbnailer-provider.c | 136 | ||||
-rw-r--r-- | tumbler/Makefile.am | 4 | ||||
-rw-r--r-- | tumbler/tumbler-thumbnailer.c | 14 | ||||
-rw-r--r-- | tumbler/tumbler-util.c | 33 | ||||
-rw-r--r-- | tumbler/tumbler-util.h | 5 | ||||
-rw-r--r-- | tumblerd/main.c | 71 | ||||
-rw-r--r-- | tumblerd/tumbler.rc | 16 |
8 files changed, 182 insertions, 99 deletions
diff --git a/docs/reference/tumbler/tumbler-sections.txt b/docs/reference/tumbler/tumbler-sections.txt index 8378231..ac7ba1c 100644 --- a/docs/reference/tumbler/tumbler-sections.txt +++ b/docs/reference/tumbler/tumbler-sections.txt @@ -262,9 +262,11 @@ tumbler_util_dump_strv tumbler_util_toggle_stderr tumbler_util_get_supported_uri_schemes tumbler_util_get_settings +tumbler_util_locations_from_strv tumbler_util_get_thumbnailer_dirs tumbler_util_guess_is_sparse tumbler_util_size_prepared tumbler_util_scale_pixbuf +tumbler_util_object_ref </SECTION> diff --git a/plugins/desktop-thumbnailer/desktop-thumbnailer-provider.c b/plugins/desktop-thumbnailer/desktop-thumbnailer-provider.c index 69297a0..10d74f0 100644 --- a/plugins/desktop-thumbnailer/desktop-thumbnailer-provider.c +++ b/plugins/desktop-thumbnailer/desktop-thumbnailer-provider.c @@ -48,6 +48,14 @@ struct _DesktopThumbnailerProvider GObject __parent__; }; +typedef struct +{ + gint priority; + GSList *locations; + GSList *excludes; + gint64 max_file_size; +} TumblerThumbnailerSettings; + G_DEFINE_DYNAMIC_TYPE_EXTENDED (DesktopThumbnailerProvider, @@ -96,14 +104,19 @@ desktop_thumbnailer_provider_init (DesktopThumbnailerProvider *provider) static DesktopThumbnailer * desktop_thumbnailer_get_from_desktop_file (GFile *file, - GStrv uri_schemes) + GStrv uri_schemes, + TumblerThumbnailerSettings *settings) { DesktopThumbnailer *thumbnailer; - GKeyFile *key_file; - GError *error = NULL; - const gchar *filename; - gchar *exec; - gchar **mime_types; + GKeyFile *rc; + GSList *locations; + GSList *excludes; + GError *error = NULL; + gchar **mime_types, **paths; + const gchar *filename; + gchar *exec = NULL; + gint64 max_file_size; + gint priority; g_return_val_if_fail (G_IS_FILE (file), NULL); @@ -111,56 +124,81 @@ desktop_thumbnailer_get_from_desktop_file (GFile *file, filename = g_file_peek_path (file); /* allocate a new key file object */ - key_file = g_key_file_new (); + rc = g_key_file_new (); - /* try to load the key file data from the input file */ - if (!g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, &error)) + /* try to load the mandatory key file data from the input file */ + if (! g_key_file_load_from_file (rc, filename, G_KEY_FILE_NONE, &error) + || (exec = g_key_file_get_string (rc, "Thumbnailer Entry", "Exec", &error)) == NULL + || (mime_types = g_key_file_get_string_list (rc, "Thumbnailer Entry", + "MimeType", NULL, &error)) == NULL) { g_warning (TUMBLER_WARNING_LOAD_FILE_FAILED, filename, error->message); - g_clear_error (&error); - g_key_file_free (key_file); + g_error_free (error); + g_key_file_free (rc); + g_free (exec); return NULL; } - /* determine the Exec of the desktop thumbnailer */ - exec = g_key_file_get_string (key_file, "Thumbnailer Entry", - "Exec", &error); - if (exec == NULL) + /* return if disabled */ + if (g_key_file_get_boolean (rc, "X-Tumbler Settings", "Disabled", &error) && error == NULL) + return NULL; + else if (error != NULL) + g_clear_error (&error); + + /* try to load the settings */ + priority = g_key_file_get_integer (rc, "X-Tumbler Settings", "Priority", &error); + if (error != NULL) { - g_warning (TUMBLER_WARNING_MALFORMED_FILE, filename, error->message); + priority = settings->priority; g_clear_error (&error); - g_key_file_free (key_file); + } - return NULL; + max_file_size = g_key_file_get_int64 (rc, "X-Tumbler Settings", "MaxFileSize", &error); + if (error != NULL) + { + max_file_size = settings->max_file_size; + g_clear_error (&error); } - /* determine the MIME types supported by this thumbnailer */ - mime_types = g_key_file_get_string_list (key_file, "Thumbnailer Entry", - "MimeType", NULL, &error); - if (mime_types == NULL) + paths = g_key_file_get_string_list (rc, "X-Tumbler Settings", "Locations", NULL, &error); + if (error != NULL) { - g_warning (TUMBLER_WARNING_MALFORMED_FILE, filename, error->message); + locations = g_slist_copy_deep (settings->locations, tumbler_util_object_ref, NULL); g_clear_error (&error); - g_free (exec); - g_key_file_free (key_file); + } + else + { + locations = tumbler_util_locations_from_strv (paths); + g_strfreev (paths); + } - return NULL; + paths = g_key_file_get_string_list (rc, "X-Tumbler Settings", "Excludes", NULL, &error); + if (error != NULL) + { + excludes = g_slist_copy_deep (settings->excludes, tumbler_util_object_ref, NULL); + g_clear_error (&error); + } + else + { + excludes = tumbler_util_locations_from_strv (paths); + g_strfreev (paths); } - thumbnailer = g_object_new (TYPE_DESKTOP_THUMBNAILER, - "uri-schemes", uri_schemes, - "mime-types", mime_types, - "exec", exec, - NULL); + thumbnailer = g_object_new (TYPE_DESKTOP_THUMBNAILER, "uri-schemes", uri_schemes, + "mime-types", mime_types, "priority", priority, + "max-file-size", max_file_size, "locations", locations, + "excludes", excludes, "exec", exec, NULL); g_debug ("Registered thumbnailer '%s'", filename); tumbler_util_dump_strv (G_LOG_DOMAIN, "Supported mime types", (const gchar *const *) mime_types); - g_key_file_free (key_file); - g_strfreev(mime_types); - g_free(exec); + g_key_file_free (rc); + g_strfreev (mime_types); + g_free (exec); + g_slist_free_full (locations, g_object_unref); + g_slist_free_full (excludes, g_object_unref); return thumbnailer; } @@ -169,6 +207,7 @@ static GList * desktop_thumbnailer_get_thumbnailers_from_dir (GList *thumbnailers, GFile *directory, GStrv uri_schemes, + TumblerThumbnailerSettings *settings, GHashTable **single_name) { const gchar *base_name; @@ -199,7 +238,7 @@ desktop_thumbnailer_get_thumbnailers_from_dir (GList *thumbnailers, /* try to load the file if it is regular */ if (type == G_FILE_TYPE_REGULAR) - thumbnailer = desktop_thumbnailer_get_from_desktop_file (file, uri_schemes); + thumbnailer = desktop_thumbnailer_get_from_desktop_file (file, uri_schemes, settings); g_object_unref (file); @@ -219,9 +258,12 @@ desktop_thumbnailer_get_thumbnailers_from_dir (GList *thumbnailers, static GList * desktop_thumbnailer_provider_get_thumbnailers (TumblerThumbnailerProvider *provider) { + TumblerThumbnailerSettings *settings; GHashTable *single_name; + GKeyFile *rc; GList *directories, *iter, *thumbnailers = NULL; - GStrv uri_schemes; + GStrv uri_schemes, paths; + const gchar *type = "DesktopThumbnailer"; uri_schemes = tumbler_util_get_supported_uri_schemes (); directories = tumbler_util_get_thumbnailer_dirs (); @@ -229,6 +271,21 @@ desktop_thumbnailer_provider_get_thumbnailers (TumblerThumbnailerProvider *provi tumbler_util_dump_strv (G_LOG_DOMAIN, "Supported URI schemes", (const gchar *const *) uri_schemes); + /* get settings from rc file */ + rc = tumbler_util_get_settings (); + settings = g_new (TumblerThumbnailerSettings, 1); + + settings->priority = g_key_file_get_integer (rc, type, "Priority", NULL); + settings->max_file_size = g_key_file_get_int64 (rc, type, "MaxFileSize", NULL); + + paths = g_key_file_get_string_list (rc, type, "Locations", NULL, NULL); + settings->locations = tumbler_util_locations_from_strv (paths); + g_strfreev (paths); + + paths = g_key_file_get_string_list (rc, type, "Excludes", NULL, NULL); + settings->excludes = tumbler_util_locations_from_strv (paths); + g_strfreev (paths); + /* use a ghash table to avoid duplication and allow for thumbnailer override */ single_name = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); @@ -236,11 +293,16 @@ desktop_thumbnailer_provider_get_thumbnailers (TumblerThumbnailerProvider *provi * not be reversed: this will happen during sorted insertion in tumbler_registry_add() */ for (iter = directories; iter != NULL; iter = iter->next) thumbnailers = desktop_thumbnailer_get_thumbnailers_from_dir (thumbnailers, iter->data, - uri_schemes, &single_name); + uri_schemes, settings, + &single_name); g_strfreev (uri_schemes); g_list_free_full (directories, g_object_unref); g_hash_table_destroy (single_name); + g_key_file_free (rc); + g_slist_free_full (settings->locations, g_object_unref); + g_slist_free_full (settings->excludes, g_object_unref); + g_free (settings); return thumbnailers; } diff --git a/tumbler/Makefile.am b/tumbler/Makefile.am index 8554e1b..af32d7e 100644 --- a/tumbler/Makefile.am +++ b/tumbler/Makefile.am @@ -83,6 +83,7 @@ libtumbler_1_la_CFLAGS = \ $(GLIB_CFLAGS) \ $(GMODULE_CFLAGS) \ $(GTHREAD_CFLAGS) \ + $(LIBXFCE4UTIL_CFLAGS) \ $(PLATFORM_CFLAGS) \ $(PLATFORM_CPPFLAGS) @@ -97,7 +98,8 @@ libtumbler_1_la_LIBADD = \ $(GIO_LIBS) \ $(GLIB_LIBS) \ $(GMODULE_LIBS) \ - $(GTHREAD_LIBS) + $(GTHREAD_LIBS) \ + $(LIBXFCE4UTIL_LIBS) EXTRA_DIST = \ tumbler-config.h.in \ diff --git a/tumbler/tumbler-thumbnailer.c b/tumbler/tumbler-thumbnailer.c index 5074d35..f027946 100644 --- a/tumbler/tumbler-thumbnailer.c +++ b/tumbler/tumbler-thumbnailer.c @@ -22,9 +22,8 @@ #include <config.h> #endif -#include <tumbler/tumbler-marshal.h> -#include <tumbler/tumbler-file-info.h> #include <tumbler/tumbler-thumbnailer.h> +#include <tumbler/tumbler.h> @@ -282,15 +281,6 @@ tumbler_thumbnailer_supports_hash_key (TumblerThumbnailer *thumbnailer, -static gpointer -tumbler_thumbnailer_object_ref (gconstpointer src, - gpointer data) -{ - return g_object_ref ((gpointer) src); -} - - - GList ** tumbler_thumbnailer_array_copy (GList **thumbnailers, guint length) @@ -303,7 +293,7 @@ tumbler_thumbnailer_array_copy (GList **thumbnailers, copy = g_new0 (GList *, length + 1); for (n = 0; n < length; ++n) - copy[n] = g_list_copy_deep (thumbnailers[n], tumbler_thumbnailer_object_ref, NULL); + copy[n] = g_list_copy_deep (thumbnailers[n], tumbler_util_object_ref, NULL); copy[n] = NULL; diff --git a/tumbler/tumbler-util.c b/tumbler/tumbler-util.c index 4e8f718..453e131 100644 --- a/tumbler/tumbler-util.c +++ b/tumbler/tumbler-util.c @@ -35,6 +35,8 @@ #include <tumbler/tumbler-util.h> +#include <libxfce4util/libxfce4util.h> + /* Float block size used in the stat struct */ #define TUMBLER_STAT_BLKSIZE 512. @@ -222,6 +224,28 @@ tumbler_util_get_settings (void) +GSList * +tumbler_util_locations_from_strv (gchar **array) +{ + GSList *locations = NULL; + guint n; + gchar *path; + + if (array == NULL) + return NULL; + + for (n = 0; array[n] != NULL; n++) + { + path = xfce_expand_variables (array[n], NULL); + locations = g_slist_prepend (locations, g_file_new_for_commandline_arg (path)); + g_free (path); + } + + return locations; +} + + + GList * tumbler_util_get_thumbnailer_dirs (void) { @@ -374,3 +398,12 @@ tumbler_util_scale_pixbuf (GdkPixbuf *source, return gdk_pixbuf_scale_simple (source, MAX (dest_width, 1), MAX (dest_height, 1), GDK_INTERP_BILINEAR); } + + + +gpointer +tumbler_util_object_ref (gconstpointer src, + gpointer data) +{ + return g_object_ref ((gpointer) src); +} diff --git a/tumbler/tumbler-util.h b/tumbler/tumbler-util.h index 5f278db..e95d59a 100644 --- a/tumbler/tumbler-util.h +++ b/tumbler/tumbler-util.h @@ -40,6 +40,8 @@ gchar **tumbler_util_get_supported_uri_schemes (void) G_GNUC_MALLOC; GKeyFile *tumbler_util_get_settings (void) G_GNUC_MALLOC; +GSList *tumbler_util_locations_from_strv (gchar **array); + GList *tumbler_util_get_thumbnailer_dirs (void); gboolean tumbler_util_guess_is_sparse (TumblerFileInfo *info); @@ -53,6 +55,9 @@ GdkPixbuf *tumbler_util_scale_pixbuf (GdkPixbuf *source, gint dest_width, gint dest_height); +gpointer tumbler_util_object_ref (gconstpointer src, + gpointer data); + G_END_DECLS #endif /* !__TUMBLER_UTIL_H__ */ diff --git a/tumblerd/main.c b/tumblerd/main.c index 58efe81..9289a97 100644 --- a/tumblerd/main.c +++ b/tumblerd/main.c @@ -46,8 +46,6 @@ #include <tumblerd/tumbler-registry.h> #include <tumblerd/tumbler-service.h> -#include <libxfce4util/libxfce4util.h> - static void @@ -63,26 +61,6 @@ shutdown_tumbler (TumblerLifecycleManager *lifecycle_manager, -static GSList * -locations_from_strv (gchar **array) -{ - GSList *locations = NULL; - guint n; - gchar *path; - - if (array == NULL) - return NULL; - - for (n = 0; array[n] != NULL; n++) - { - path = xfce_expand_variables (array[n], NULL); - locations = g_slist_prepend (locations, g_file_new_for_commandline_arg (path)); - g_free (path); - } - - return locations; -} - static void on_dbus_name_lost (GDBusConnection *connection, const gchar *name, @@ -172,37 +150,36 @@ main (int argc, /* add all thumbnailers to the registry */ for (tp = thumbnailers; tp != NULL; tp = tp->next) { - /* set settings from rc file */ - type_name = G_OBJECT_TYPE_NAME (tp->data); - priority = g_key_file_get_integer (rc, type_name, "Priority", NULL); - file_size = g_key_file_get_int64 (rc, type_name, "MaxFileSize", NULL); - - paths = g_key_file_get_string_list (rc, type_name, "Locations", NULL, NULL); - locations = locations_from_strv (paths); - g_strfreev (paths); - - paths = g_key_file_get_string_list (rc, type_name, "Excludes", NULL, NULL); - excludes = locations_from_strv (paths); - g_strfreev (paths); - - g_object_set (G_OBJECT (tp->data), - "priority", priority, - "max-file-size", file_size, - "locations", locations, - "excludes", excludes, - NULL); + /* desktop thumbnailers are set up per desktop file */ + if (g_object_class_find_property (G_OBJECT_GET_CLASS (tp->data), "exec") == NULL) + { + /* set settings from rc file */ + type_name = G_OBJECT_TYPE_NAME (tp->data); + priority = g_key_file_get_integer (rc, type_name, "Priority", NULL); + file_size = g_key_file_get_int64 (rc, type_name, "MaxFileSize", NULL); + + paths = g_key_file_get_string_list (rc, type_name, "Locations", NULL, NULL); + locations = tumbler_util_locations_from_strv (paths); + g_strfreev (paths); + + paths = g_key_file_get_string_list (rc, type_name, "Excludes", NULL, NULL); + excludes = tumbler_util_locations_from_strv (paths); + g_strfreev (paths); + + g_object_set (tp->data, "priority", priority, "max-file-size", file_size, + "locations", locations, "excludes", excludes, NULL); + + /* cleanup */ + g_slist_free_full (locations, g_object_unref); + g_slist_free_full (excludes, g_object_unref); + } /* ready for usage */ tumbler_registry_add (registry, tp->data); - - /* cleanup */ - g_object_unref (tp->data); - g_slist_free_full (locations, g_object_unref); - g_slist_free_full (excludes, g_object_unref); } /* free the thumbnailer list */ - g_list_free (thumbnailers); + g_list_free_full (thumbnailers, g_object_unref); } g_key_file_free (rc); diff --git a/tumblerd/tumbler.rc b/tumblerd/tumbler.rc index bda93b1..0fdef47 100644 --- a/tumblerd/tumbler.rc +++ b/tumblerd/tumbler.rc @@ -122,12 +122,24 @@ MaxFileSize=0 # External Thumbnailers ### -# Thumbnailers provided by .thumbnailer desktop files. +# Thumbnailers provided by .thumbnailer desktop files located in $XDG_HOME_DIR/thumbnailers/ and +# $XDG_DATA_DIRS/thumbnailers/ +# # They can handle any mime type, depending on the contents of each particular desktop file. -# Their priority is defined globally and by default lower than that of all internal thumbnailers. +# Their priority is defined here globally and by default lower than that of all internal +# thumbnailers. # These priorities should in any case remain different, otherwise there is no way to know in # advance which of the internal or external thumbnailer will be prioritized in case of a mime type # match. +# +# In case of several desktop files with the same name in the thumbnailer directories, the one +# placed in the highest priority directory override all the others. +# +# All the properties below can be overridden per desktop file, in the "X-Tumbler Settings" group, +# for example: +# [X-Tumbler Settings] +# Priority=4 +# MaxFileSize=104857600 [DesktopThumbnailer] Disabled=false Priority=0 |