[libdazzle] shortcut manager: allow entries from stack or heap



commit 6034d2536055bcda405f0a0cd05596c8c6864f4d
Author: Sebastien Lafargue <slafargue gnome org>
Date:   Sun Aug 13 11:10:46 2017 +0200

    shortcut manager: allow entries from stack or heap
    
    We avoid double entries from instances by comparing entries pointer
    but we need more than that if the array is not static.
    
    Here we introduce a hash function on the strings.

 src/shortcuts/dzl-shortcut-manager.c |   30 +++++++++++++++++++++++++++++-
 1 files changed, 29 insertions(+), 1 deletions(-)
---
diff --git a/src/shortcuts/dzl-shortcut-manager.c b/src/shortcuts/dzl-shortcut-manager.c
index 4bd10e5..b8cb407 100644
--- a/src/shortcuts/dzl-shortcut-manager.c
+++ b/src/shortcuts/dzl-shortcut-manager.c
@@ -408,12 +408,40 @@ dzl_shortcut_manager_class_init (DzlShortcutManagerClass *klass)
                   0, NULL, NULL, NULL, G_TYPE_NONE, 0);
 }
 
+static guint
+shortcut_entry_hash (gconstpointer key)
+{
+  DzlShortcutEntry *entry = (DzlShortcutEntry *)key;
+  guint command_hash = 0;
+  guint section_hash = 0;
+  guint group_hash = 0;
+  guint title_hash = 0;
+  guint subtitle_hash = 0;
+
+  if (entry->command != NULL)
+    command_hash = g_str_hash (entry->command);
+
+  if (entry->section != NULL)
+    section_hash = g_str_hash (entry->section);
+
+  if (entry->group != NULL)
+    group_hash = g_str_hash (entry->group);
+
+  if (entry->title != NULL)
+    title_hash = g_str_hash (entry->title);
+
+  if (entry->subtitle != NULL)
+    subtitle_hash = g_str_hash (entry->subtitle);
+
+  return (command_hash ^ section_hash ^ group_hash ^ title_hash ^ subtitle_hash);
+}
+
 static void
 dzl_shortcut_manager_init (DzlShortcutManager *self)
 {
   DzlShortcutManagerPrivate *priv = dzl_shortcut_manager_get_instance_private (self);
 
-  priv->seen_entries = g_hash_table_new (NULL, NULL);
+  priv->seen_entries = g_hash_table_new (shortcut_entry_hash, NULL);
   priv->themes = g_ptr_array_new_with_free_func (destroy_theme);
   priv->root = g_node_new (NULL);
   priv->internal_theme = g_object_new (DZL_TYPE_SHORTCUT_THEME,


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