[gthumb] shortcuts: allow extensions to register shortcut categories



commit 70ec2d721ea0296a54d3a7eb624e7b72b4199f45
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Tue Nov 12 16:55:16 2019 +0100

    shortcuts: allow extensions to register shortcut categories

 gthumb/dlg-preferences-shortcuts.c | 74 ++++++++++----------------------------
 gthumb/gth-main-default-types.c    | 12 +++++++
 gthumb/gth-main.c                  | 31 ++++++++++++++++
 gthumb/gth-main.h                  |  3 ++
 gthumb/gth-shortcut.c              |  2 +-
 gthumb/gth-shortcut.h              | 17 +++++++--
 gthumb/gth-window.c                | 22 ++++++++++++
 gthumb/gth-window.h                |  2 ++
 gthumb/typedefs.h                  | 10 ------
 9 files changed, 105 insertions(+), 68 deletions(-)
---
diff --git a/gthumb/dlg-preferences-shortcuts.c b/gthumb/dlg-preferences-shortcuts.c
index e495896c..682336ee 100644
--- a/gthumb/dlg-preferences-shortcuts.c
+++ b/gthumb/dlg-preferences-shortcuts.c
@@ -339,15 +339,16 @@ _new_shortcut_row (GthShortcut *shortcut,
 
 
 static GtkWidget *
-_new_shortcut_category_row (GthShortcutCategory category,
-                           int                 n_category)
+_new_shortcut_category_row (const char *category_id,
+                           int         n_category)
 {
-       GtkWidget  *row;
-       GtkWidget  *box;
-       const char *text;
-       char       *esc_text;
-       char       *markup_text;
-       GtkWidget  *label;
+       GtkWidget           *row;
+       GtkWidget           *box;
+       GthShortcutCategory *category;
+       const char          *text;
+       char                *esc_text;
+       char                *markup_text;
+       GtkWidget           *label;
 
        row = gtk_list_box_row_new ();
        gtk_list_box_row_set_activatable (GTK_LIST_BOX_ROW (row), FALSE);
@@ -358,28 +359,8 @@ _new_shortcut_category_row (GthShortcutCategory category,
                gtk_widget_set_margin_top (box, 15);
        gtk_container_add (GTK_CONTAINER (row), box);
 
-       text = _("Others");
-       switch (category) {
-       case GTH_SHORTCUT_CATEGORY_UI:
-               text = _("Show/Hide");
-               break;
-       case GTH_SHORTCUT_CATEGORY_FILE_NAVIGATION:
-               text = _("File Navigation");
-               break;
-       case GTH_SHORTCUT_CATEGORY_FILE_EDIT:
-               text = _("File Edit");
-               break;
-       case GTH_SHORTCUT_CATEGORY_IMAGE_VIEW:
-               text = _("Viewer");
-               break;
-       case GTH_SHORTCUT_CATEGORY_IMAGE_EDIT:
-               text = _("Image Edit");
-               break;
-       case GTH_SHORTCUT_CATEGORY_SLIDESHOW:
-               text = _("Slideshow");
-               break;
-       }
-
+       category = gth_main_get_shortcut_category (category_id);
+       text = (category != NULL) ? category->display_name : _("Other");
        esc_text = g_markup_escape_text (text, -1);
        markup_text = g_strdup_printf ("<b>%s</b>", esc_text);
 
@@ -403,22 +384,6 @@ _new_shortcut_category_row (GthShortcutCategory category,
 }
 
 
-static int
-sort_shortcuts_by_category (gconstpointer a,
-                           gconstpointer b)
-{
-       const GthShortcut *sa = * (GthShortcut **) a;
-       const GthShortcut *sb = * (GthShortcut **) b;
-
-       if (sa->category < sb->category)
-               return -1;
-       if (sa->category > sb->category)
-               return 1;
-
-       return g_strcmp0 (sa->description, sb->description);
-}
-
-
 static void
 restore_all_button_clicked_cb (GtkButton *button,
                               gpointer   user_data)
@@ -459,8 +424,8 @@ shortcuts__dlg_preferences_construct_cb (GtkWidget  *dialog,
 {
        BrowserData *data;
        GtkWidget   *shortcuts_list;
-       GPtrArray   *accel_v;
-       int          last_category;
+       GPtrArray   *shortcuts_v;
+       const char  *last_category;
        int          n_category;
        int          i;
        GtkWidget   *label;
@@ -475,17 +440,16 @@ shortcuts__dlg_preferences_construct_cb (GtkWidget  *dialog,
        g_object_set_data_full (G_OBJECT (dialog), BROWSER_DATA_KEY, data, (GDestroyNotify) 
browser_data_free);
 
        shortcuts_list = _gtk_builder_get_widget (data->builder, "shortcuts_list");
-       accel_v = gth_window_get_shortcuts (GTH_WINDOW (browser));
-       g_ptr_array_sort (accel_v, sort_shortcuts_by_category);
-       last_category = -1;
+       shortcuts_v = gth_window_get_shortcuts_by_category (GTH_WINDOW (browser));
+       last_category = NULL;
        n_category = 0;
-       for (i = 0; i < accel_v->len; i++) {
-               GthShortcut *shortcut = g_ptr_array_index (accel_v, i);
+       for (i = 0; i < shortcuts_v->len; i++) {
+               GthShortcut *shortcut = g_ptr_array_index (shortcuts_v, i);
 
-               if (shortcut->category == GTH_SHORTCUT_CATEGORY_HIDDEN)
+               if (g_strcmp0 (shortcut->category, GTH_SHORTCUT_CATEGORY_HIDDEN) == 0)
                        continue;
 
-               if (shortcut->category != last_category) {
+               if (g_strcmp0 (shortcut->category,last_category) != 0) {
                        last_category = shortcut->category;
                        n_category++;
                        gtk_list_box_insert (GTK_LIST_BOX (shortcuts_list),
diff --git a/gthumb/gth-main-default-types.c b/gthumb/gth-main-default-types.c
index 8cde66b2..a51122e4 100644
--- a/gthumb/gth-main-default-types.c
+++ b/gthumb/gth-main-default-types.c
@@ -33,6 +33,17 @@
 #include "pixbuf-io.h"
 
 
+GthShortcutCategory shortcut_categories[] = {
+       { GTH_SHORTCUT_CATEGORY_HIDDEN, NULL },
+       { GTH_SHORTCUT_CATEGORY_UI, N_("Show/Hide") },
+       { GTH_SHORTCUT_CATEGORY_FILE_NAVIGATION, N_("File Navigation") },
+       { GTH_SHORTCUT_CATEGORY_FILE_EDIT, N_("File Edit") },
+       { GTH_SHORTCUT_CATEGORY_IMAGE_VIEW, N_("Viewer") },
+       { GTH_SHORTCUT_CATEGORY_IMAGE_EDIT, N_("Image Edit") },
+       { GTH_SHORTCUT_CATEGORY_SLIDESHOW, N_("Slideshow") },
+};
+
+
 static void
 gth_main_register_default_file_loader (void)
 {
@@ -69,6 +80,7 @@ gth_main_register_default_types (void)
        gth_main_register_type ("file-properties", GTH_TYPE_FILE_COMMENT);
        gth_main_register_type ("file-properties", GTH_TYPE_FILE_DETAILS);
        gth_main_register_default_file_loader ();
+       gth_main_register_shortcut_category (shortcut_categories);
        gth_hook_add_callback ("dlg-preferences-construct", 1, G_CALLBACK 
(general__dlg_preferences_construct_cb), NULL);
        gth_hook_add_callback ("dlg-preferences-apply", 1, G_CALLBACK (general__dlg_preferences_apply), NULL);
        gth_hook_add_callback ("dlg-preferences-construct", 2, G_CALLBACK 
(browser__dlg_preferences_construct_cb), NULL);
diff --git a/gthumb/gth-main.c b/gthumb/gth-main.c
index d06dae0a..29f4c707 100644
--- a/gthumb/gth-main.c
+++ b/gthumb/gth-main.c
@@ -106,6 +106,8 @@ struct _GthMainPrivate {
        GPtrArray           *metadata_info;
        GHashTable          *metadata_info_hash;
        gboolean             metadata_info_sorted;
+       GPtrArray           *shortcut_category_v;
+       GHashTable          *shortcut_category_h;
        GHashTable          *sort_types;
        GHashTable          *image_loaders;
        GHashTable          *types;
@@ -139,6 +141,9 @@ gth_main_finalize (GObject *object)
        g_list_foreach (gth_main->priv->metadata_provider, (GFunc) g_object_unref, NULL);
        g_list_free (gth_main->priv->metadata_provider);
 
+       g_ptr_array_unref (gth_main->priv->shortcut_category_v);
+       g_hash_table_unref (gth_main->priv->shortcut_category_h);
+
        if (gth_main->priv->sort_types != NULL)
                g_hash_table_unref (gth_main->priv->sort_types);
        if (gth_main->priv->image_loaders != NULL)
@@ -183,6 +188,8 @@ gth_main_init (GthMain *main)
        main->priv->metadata_info = g_ptr_array_new ();
        main->priv->metadata_info_hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
        main->priv->metadata_info_sorted = FALSE;
+       main->priv->shortcut_category_v = g_ptr_array_new ();
+       main->priv->shortcut_category_h = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
        main->priv->sort_types = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
        main->priv->image_loaders = g_hash_table_new_full (g_str_hash,
                                                           (GEqualFunc) g_content_type_equals,
@@ -635,6 +642,30 @@ gth_main_get_all_metadata_info (void)
 }
 
 
+GthShortcutCategory *
+gth_main_get_shortcut_category (const char *id)
+{
+       return g_hash_table_lookup (Main->priv->shortcut_category_h, id);
+}
+
+
+void
+gth_main_register_shortcut_category (GthShortcutCategory *shortcut_category)
+{
+       int i;
+
+       g_mutex_lock (&register_mutex);
+
+       for (i = 0; shortcut_category[i].id != NULL; i++) {
+               if (gth_main_get_shortcut_category (shortcut_category[i].id) == NULL) {
+                       g_ptr_array_add (Main->priv->shortcut_category_v, &shortcut_category[i]);
+                       g_hash_table_insert (Main->priv->shortcut_category_h, shortcut_category[i].id, 
&shortcut_category[i]);
+               }
+       }
+       g_mutex_unlock (&register_mutex);
+}
+
+
 void
 gth_main_register_sort_type (GthFileDataSort *sort_type)
 {
diff --git a/gthumb/gth-main.h b/gthumb/gth-main.h
index bd0b8153..91bd7e7d 100644
--- a/gthumb/gth-main.h
+++ b/gthumb/gth-main.h
@@ -35,6 +35,7 @@
 #include "gth-image-saver.h"
 #include "gth-metadata-provider.h"
 #include "gth-monitor.h"
+#include "gth-shortcut.h"
 #include "gth-tags-file.h"
 #include "gth-test.h"
 #include "pixbuf-io.h"
@@ -85,6 +86,8 @@ GthMetadataProvider *  gth_main_get_metadata_writer           (const char
 GthMetadataCategory *  gth_main_get_metadata_category         (const char           *id);
 GthMetadataInfo *      gth_main_get_metadata_info             (const char           *id);
 GList *                gth_main_get_all_metadata_info         (void);
+void                   gth_main_register_shortcut_category    (GthShortcutCategory  *shortcut_category);
+GthShortcutCategory *  gth_main_get_shortcut_category         (const char           *id);
 void                   gth_main_register_sort_type            (GthFileDataSort      *sort_type);
 GthFileDataSort *      gth_main_get_sort_type                 (const char           *name);
 GList *                gth_main_get_all_sort_types            (void);
diff --git a/gthumb/gth-shortcut.c b/gthumb/gth-shortcut.c
index 3d69670b..2e807046 100644
--- a/gthumb/gth-shortcut.c
+++ b/gthumb/gth-shortcut.c
@@ -36,7 +36,7 @@ gth_shortcut_new (void)
        shortcut->action_name = NULL;
        shortcut->description = NULL;
        shortcut->context = 0;
-       shortcut->category = 0;
+       shortcut->category = NULL;
        shortcut->default_accelerator = NULL;
        shortcut->accelerator = NULL;
        shortcut->label = NULL;
diff --git a/gthumb/gth-shortcut.h b/gthumb/gth-shortcut.h
index 4ffc7ee7..a7d9d827 100644
--- a/gthumb/gth-shortcut.h
+++ b/gthumb/gth-shortcut.h
@@ -27,14 +27,27 @@
 G_BEGIN_DECLS
 
 #define GTH_SHORTCUT_CONTEXT_INTERNAL -1
-#define GTH_SHORTCUT_CATEGORY_HIDDEN -1
+
+#define GTH_SHORTCUT_CATEGORY_HIDDEN "hidden"
+#define GTH_SHORTCUT_CATEGORY_UI "ui"
+#define GTH_SHORTCUT_CATEGORY_FILE_NAVIGATION "file-navigation"
+#define GTH_SHORTCUT_CATEGORY_FILE_EDIT "file-edit"
+#define GTH_SHORTCUT_CATEGORY_IMAGE_VIEW "image-view"
+#define GTH_SHORTCUT_CATEGORY_IMAGE_EDIT "image-edit"
+#define GTH_SHORTCUT_CATEGORY_SLIDESHOW "slideshow"
+
+
+typedef struct {
+       char *id;
+       char *display_name;
+} GthShortcutCategory;
 
 
 typedef struct {
        char            *action_name;
        char            *description;
        int              context;
-       int              category;
+       char            *category;
        char            *default_accelerator;
        char            *accelerator;
        char            *label;
diff --git a/gthumb/gth-window.c b/gthumb/gth-window.c
index c5ecf242..fb202b1e 100644
--- a/gthumb/gth-window.c
+++ b/gthumb/gth-window.c
@@ -22,6 +22,7 @@
 #include <config.h>
 #include <gtk/gtk.h>
 #include "glib-utils.h"
+#include "gth-main.h"
 #include "gth-window.h"
 #include "gth-window-title.h"
 #include "gtk-utils.h"
@@ -822,6 +823,27 @@ gth_window_get_shortcuts (GthWindow *window)
 }
 
 
+static int
+sort_shortcuts_by_category (gconstpointer a,
+                           gconstpointer b)
+{
+       const GthShortcut *sa = * (GthShortcut **) a;
+       const GthShortcut *sb = * (GthShortcut **) b;
+
+       return g_strcmp0 (sa->category, sb->category);
+}
+
+
+GPtrArray *
+gth_window_get_shortcuts_by_category (GthWindow *window)
+{
+       g_return_val_if_fail (GTH_IS_WINDOW (window), NULL);
+
+       g_ptr_array_sort (window->priv->shortcuts_v, sort_shortcuts_by_category);
+       return window->priv->shortcuts_v;
+}
+
+
 gboolean
 gth_window_activate_shortcut (GthWindow       *window,
                              int              context,
diff --git a/gthumb/gth-window.h b/gthumb/gth-window.h
index 0d14e7b7..9f459a05 100644
--- a/gthumb/gth-window.h
+++ b/gthumb/gth-window.h
@@ -125,6 +125,8 @@ void                gth_window_add_shortcuts        (GthWindow              *window,
                                                 const GthShortcut      *shortcuts,
                                                 int                     n_shortcuts);
 GPtrArray *    gth_window_get_shortcuts        (GthWindow              *window);
+GPtrArray *    gth_window_get_shortcuts_by_category
+                                               (GthWindow              *window);
 gboolean       gth_window_activate_shortcut    (GthWindow              *window,
                                                 int                     context,
                                                 guint                   keycode,
diff --git a/gthumb/typedefs.h b/gthumb/typedefs.h
index 39ed1133..29eb6994 100644
--- a/gthumb/typedefs.h
+++ b/gthumb/typedefs.h
@@ -143,16 +143,6 @@ typedef enum /*< skip >*/ {
 } GthShortcutContext;
 
 
-typedef enum {
-       GTH_SHORTCUT_CATEGORY_UI,
-       GTH_SHORTCUT_CATEGORY_FILE_NAVIGATION,
-       GTH_SHORTCUT_CATEGORY_FILE_EDIT,
-       GTH_SHORTCUT_CATEGORY_IMAGE_VIEW,
-       GTH_SHORTCUT_CATEGORY_IMAGE_EDIT,
-       GTH_SHORTCUT_CATEGORY_SLIDESHOW
-} GthShortcutCategory;
-
-
 typedef void (*DataFunc)         (gpointer    user_data);
 typedef void (*ReadyFunc)        (GError     *error,
                                  gpointer    user_data);


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