[libdazzle] shortcuts: allow context lookup without creating new context



commit f6329ef7e540bd38059d2fb1bd843009aff189c2
Author: Christian Hergert <chergert redhat com>
Date:   Mon Jul 17 21:00:39 2017 -0700

    shortcuts: allow context lookup without creating new context
    
    This allows locating a context without creating it upon cache miss.
    It depends on the implementation of g_intern_string() and quarks in GLib
    in that intern'ed strings are just quarks that have been inserted/fetched.

 src/shortcuts/dzl-shortcut-private.h |    2 ++
 src/shortcuts/dzl-shortcut-theme.c   |   26 ++++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 0 deletions(-)
---
diff --git a/src/shortcuts/dzl-shortcut-private.h b/src/shortcuts/dzl-shortcut-private.h
index f7e3690..f4cac1c 100644
--- a/src/shortcuts/dzl-shortcut-private.h
+++ b/src/shortcuts/dzl-shortcut-private.h
@@ -128,6 +128,8 @@ gboolean               _dzl_shortcut_chord_table_iter_next      (DzlShortcutChor
                                                                  const DzlShortcutChord    **chord,
                                                                  gpointer                   *value);
 void                   _dzl_shortcut_chord_table_iter_steal     (DzlShortcutChordTableIter  *iter);
+DzlShortcutContext    *_dzl_shortcut_theme_try_find_context_by_name (DzlShortcutTheme           *self,
+                                                                     const gchar                *name);
 
 G_END_DECLS
 
diff --git a/src/shortcuts/dzl-shortcut-theme.c b/src/shortcuts/dzl-shortcut-theme.c
index c5b0272..975868e 100644
--- a/src/shortcuts/dzl-shortcut-theme.c
+++ b/src/shortcuts/dzl-shortcut-theme.c
@@ -264,6 +264,32 @@ dzl_shortcut_theme_find_context_by_name (DzlShortcutTheme *self,
   return ret;
 }
 
+/**
+ * _dzl_shortcut_theme_try_find_context_by_name:
+ * @self: a #DzlShortcutTheme
+ *
+ * This function is like dzl_shortcut_theme_find_context_by_name() but will
+ * not create the context if it does not exist.
+ *
+ * Returns: (transfer none) (nullable): A #DzlShortcutContext or %NULL.
+ */
+DzlShortcutContext *
+_dzl_shortcut_theme_try_find_context_by_name (DzlShortcutTheme *self,
+                                              const gchar      *name)
+{
+  DzlShortcutThemePrivate *priv = dzl_shortcut_theme_get_instance_private (self);
+  GQuark qname;
+
+  g_return_val_if_fail (DZL_IS_SHORTCUT_THEME (self), NULL);
+  g_return_val_if_fail (name != NULL, NULL);
+
+  /* Names are interned (which are quarks) */
+  if (0 != (qname = g_quark_try_string (name)))
+    return g_hash_table_lookup (priv->contexts, g_quark_to_string (qname));
+
+  return NULL;
+}
+
 static DzlShortcutContext *
 dzl_shortcut_theme_find_default_context_by_type (DzlShortcutTheme *self,
                                                  GType             type)


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