[Xfce4-commits] <design:master> Use hashtable for command -> icon lookup.

Nick Schermer noreply at xfce.org
Mon Jun 6 19:54:01 CEST 2011


Updating branch refs/heads/master
         to a23f44210c81d1550cac59c0543a590a9db585ea (commit)
       from 00a85721931aab4896e66e35d2123e02359035d5 (commit)

commit a23f44210c81d1550cac59c0543a590a9db585ea
Author: Nick Schermer <nick at xfce.org>
Date:   Sun Jun 5 19:59:04 2011 +0200

    Use hashtable for command -> icon lookup.

 .../merge-with-xfrun/demo-code/c/appfinder-model.c |   57 ++++++++++----------
 .../merge-with-xfrun/demo-code/c/appfinder-model.h |    4 +-
 .../demo-code/c/appfinder-window.c                 |    2 +-
 3 files changed, 31 insertions(+), 32 deletions(-)

diff --git a/xfce4-appfinder/merge-with-xfrun/demo-code/c/appfinder-model.c b/xfce4-appfinder/merge-with-xfrun/demo-code/c/appfinder-model.c
index 479c329..d0fe836 100644
--- a/xfce4-appfinder/merge-with-xfrun/demo-code/c/appfinder-model.c
+++ b/xfce4-appfinder/merge-with-xfrun/demo-code/c/appfinder-model.c
@@ -61,6 +61,7 @@ struct _XfceAppfinderModel
   gint               stamp;
 
   GSList            *items;
+  GHashTable        *items_hash;
   GarconMenu        *menu;
 
   GdkPixbuf         *command_icon_small;
@@ -135,6 +136,7 @@ xfce_appfinder_model_init (XfceAppfinderModel *model)
 {
   /* generate a unique stamp */
   model->stamp = g_random_int ();
+  model->items_hash = g_hash_table_new (g_str_hash, g_str_equal);
   model->command_icon_small = xfce_appfinder_model_load_pixbuf (GTK_STOCK_EXECUTE, ICON_SMALL);
   model->command_icon_large = xfce_appfinder_model_load_pixbuf (GTK_STOCK_EXECUTE, ICON_LARGE);
 
@@ -188,6 +190,7 @@ xfce_appfinder_model_finalize (GObject *object)
 
   if (model->collect_desktop_ids != NULL)
     g_hash_table_destroy (model->collect_desktop_ids);
+  g_hash_table_destroy (model->items_hash);
 
   g_free (model->filter_category);
   g_free (model->filter_string);
@@ -293,7 +296,6 @@ xfce_appfinder_model_get_value (GtkTreeModel *tree_model,
   ModelItem           *item;
   const gchar         *name;
   const gchar         *comment;
-  const gchar         *command, *p;
   GFile               *file;
   gchar               *parse_name;
   GList               *categories, *li;
@@ -339,21 +341,6 @@ xfce_appfinder_model_get_value (GtkTreeModel *tree_model,
       break;
 
     case XFCE_APPFINDER_MODEL_COLUMN_COMMAND:
-      if (item->command == NULL
-          && item->item != NULL)
-        {
-          command = garcon_menu_item_get_command (item->item);
-          if (command != NULL)
-            {
-              /* only add first part of the command */
-              p = strchr (command, ' ');
-              if (p != NULL)
-                item->command = g_strndup (command, p - command);
-              else
-                item->command = g_strdup (command);
-            }
-        }
-
       g_value_init (value, G_TYPE_STRING);
       g_value_set_static_string (value, item->command);
       break;
@@ -712,6 +699,7 @@ xfce_appfinder_model_collect_items (XfceAppfinderModel *model,
   ModelItem           *item;
   gboolean             has_items = FALSE;
   const gchar         *desktop_id;
+  const gchar         *command, *p;
 
   g_return_val_if_fail (GARCON_IS_MENU (menu), FALSE);
 
@@ -743,11 +731,22 @@ xfce_appfinder_model_collect_items (XfceAppfinderModel *model,
               item->item = li->data;
               item->visible = FALSE;
 
+              command = garcon_menu_item_get_command (li->data);
+              if (G_LIKELY (command != NULL))
+                {
+                  p = strchr (command, ' ');
+                  if (p != NULL)
+                    item->command = g_strndup (command, p - command);
+                  else
+                    item->command = g_strdup (command);
+                }
+
               item->categories = g_ptr_array_new_with_free_func (g_free);
               g_ptr_array_add (item->categories, g_strdup (category));
 
               model->collect_items = g_slist_prepend (model->collect_items, item);
               g_hash_table_insert (model->collect_desktop_ids, (gchar *) desktop_id, item);
+              g_hash_table_insert (model->items_hash, item->command, item);
             }
           else if (!xfce_appfinder_model_ptr_array_strcmp (item->categories, category))
             {
@@ -801,6 +800,8 @@ xfce_appfinder_model_collect_history (XfceAppfinderModel *model,
           item->icon_small = g_object_ref (G_OBJECT (model->command_icon_small));
           item->icon_large = g_object_ref (G_OBJECT (model->command_icon_large));
           model->collect_items = g_slist_prepend (model->collect_items, item);
+
+          g_hash_table_insert (model->items_hash, item->command, item);
         }
 
       contents += len + 1;
@@ -1189,25 +1190,23 @@ xfce_appfinder_model_save_commands (XfceAppfinderModel  *model,
 
 
 GdkPixbuf *
-xfce_appfinder_model_get_icon_for_text (XfceAppfinderModel *model,
-                                        const gchar        *text)
+xfce_appfinder_model_get_icon_for_command (XfceAppfinderModel *model,
+                                           const gchar        *command)
 {
-  ModelItem *item;
-  GSList    *li;
-
-  if (!IS_STRING (text))
-    return NULL;
+  ModelItem   *item;
+  const gchar *icon_name;
 
-  for (li = model->items; li != NULL; li = li->next)
+  if (IS_STRING (command))
     {
-      item = li->data;
-
-      if (item->command != NULL
-          && strcmp (item->command, text) == 0)
+      item = g_hash_table_lookup (model->items_hash, command);
+      if (G_LIKELY (item != NULL))
         {
           if (item->icon_large == NULL
               && item->item != NULL)
-            item->icon_large = xfce_appfinder_model_load_pixbuf (garcon_menu_item_get_icon_name (item->item), ICON_LARGE);
+            {
+              icon_name = garcon_menu_item_get_icon_name (item->item);
+              item->icon_large = xfce_appfinder_model_load_pixbuf (icon_name, ICON_LARGE);
+            }
 
           return g_object_ref (G_OBJECT (item->icon_large));
         }
diff --git a/xfce4-appfinder/merge-with-xfrun/demo-code/c/appfinder-model.h b/xfce4-appfinder/merge-with-xfrun/demo-code/c/appfinder-model.h
index 22c87dd..abf3003 100644
--- a/xfce4-appfinder/merge-with-xfrun/demo-code/c/appfinder-model.h
+++ b/xfce4-appfinder/merge-with-xfrun/demo-code/c/appfinder-model.h
@@ -62,8 +62,8 @@ GdkPixbuf          *xfce_appfinder_model_load_pixbuf          (const gchar
 gboolean            xfce_appfinder_model_save_commands        (XfceAppfinderModel  *model,
                                                                GError             **error);
 
-GdkPixbuf          *xfce_appfinder_model_get_icon_for_text    (XfceAppfinderModel  *model,
-                                                               const gchar         *text);
+GdkPixbuf          *xfce_appfinder_model_get_icon_for_command (XfceAppfinderModel  *model,
+                                                               const gchar         *command);
 
 G_END_DECLS
 
diff --git a/xfce4-appfinder/merge-with-xfrun/demo-code/c/appfinder-window.c b/xfce4-appfinder/merge-with-xfrun/demo-code/c/appfinder-window.c
index c2cc726..46c1285 100644
--- a/xfce4-appfinder/merge-with-xfrun/demo-code/c/appfinder-window.c
+++ b/xfce4-appfinder/merge-with-xfrun/demo-code/c/appfinder-window.c
@@ -384,7 +384,7 @@ xfce_appfinder_window_entry_changed (XfceAppfinderWindow *window)
     {
       gtk_widget_set_sensitive (window->button_launch, IS_STRING (text));
 
-      pixbuf = xfce_appfinder_model_get_icon_for_text (window->model, text);
+      pixbuf = xfce_appfinder_model_get_icon_for_command (window->model, text);
       xfce_appfinder_window_update_image (window, pixbuf);
       if (pixbuf != NULL)
         g_object_unref (G_OBJECT (pixbuf));



More information about the Xfce4-commits mailing list