[Xfce4-commits] <design:master> Add history loading.

Nick Schermer noreply at xfce.org
Sat Jun 4 18:22:02 CEST 2011


Updating branch refs/heads/master
         to f022175679338adec51c4ad4bbeedc303cae5c6a (commit)
       from e767224f8c47a03a3aba8b5a937c8f24876b2a6d (commit)

commit f022175679338adec51c4ad4bbeedc303cae5c6a
Author: Nick Schermer <nick at xfce.org>
Date:   Sat Jun 4 18:19:51 2011 +0200

    Add history loading.

 .../merge-with-xfrun/demo-code/c/appfinder-model.c |  175 ++++++++++++++++----
 .../demo-code/c/appfinder-window.c                 |    2 +
 2 files changed, 146 insertions(+), 31 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 a8c61d5..7883df7 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,8 @@ struct _XfceAppfinderModel
   GSList            *items;
   GarconMenu        *menu;
 
+  GdkPixbuf         *command_icon;
+
   gchar             *filter_category;
   gchar             *filter_string;
   guint              filter_idle_id;
@@ -79,6 +81,7 @@ typedef struct
   gchar          *key;
   gchar          *abstract;
   gchar          *category;
+  gchar          *command;
   guint           visible : 1;
 }
 ModelItem;
@@ -125,6 +128,7 @@ xfce_appfinder_model_init (XfceAppfinderModel *model)
 {
   /* generate a unique stamp */
   model->stamp = g_random_int ();
+  model->command_icon = xfce_appfinder_model_load_pixbuf (GTK_STOCK_EXECUTE, 32);
 
   model->menu = garcon_menu_new_applications ();
   model->collect_thread = g_thread_create (xfce_appfinder_model_collect_thread, model, TRUE, NULL);
@@ -177,6 +181,9 @@ xfce_appfinder_model_finalize (GObject *object)
   g_free (model->filter_category);
   g_free (model->filter_string);
 
+  if (model->command_icon != NULL)
+    g_object_unref (G_OBJECT (model->command_icon));
+
   g_message ("model cleared");
 
   (*G_OBJECT_CLASS (xfce_appfinder_model_parent_class)->finalize) (object);
@@ -277,7 +284,8 @@ xfce_appfinder_model_get_value (GtkTreeModel *tree_model,
   g_return_if_fail (iter->stamp == model->stamp);
 
   item = ITER_GET_DATA (iter);
-  g_return_if_fail (GARCON_IS_MENU_ITEM (item->item));
+  g_return_if_fail ((item->item == NULL && item->command != NULL)
+                    || (item->item != NULL && GARCON_IS_MENU_ITEM (item->item)));
 
   switch (column)
     {
@@ -287,16 +295,22 @@ xfce_appfinder_model_get_value (GtkTreeModel *tree_model,
       break;
 
     case XFCE_APPFINDER_MODEL_COLUMN_ABSTRACT:
-      if (item->abstract == NULL
-          && item->item != NULL)
+      if (item->abstract == NULL)
         {
-          name = garcon_menu_item_get_name (item->item);
-          comment = garcon_menu_item_get_comment (item->item);
+          if (item->item != NULL)
+            {
+              name = garcon_menu_item_get_name (item->item);
+              comment = garcon_menu_item_get_comment (item->item);
 
-          if (comment != NULL)
-            item->abstract = g_markup_printf_escaped ("<b>%s</b>\n%s", name, comment);
-          else
-            item->abstract = g_markup_printf_escaped ("<b>%s</b>", name);
+              if (comment != NULL)
+                item->abstract = g_markup_printf_escaped ("<b>%s</b>\n%s", name, comment);
+              else
+                item->abstract = g_markup_printf_escaped ("<b>%s</b>", name);
+            }
+          else if (item->command != NULL)
+            {
+              item->abstract = g_markup_escape_text (item->command, -1);
+            }
         }
 
       g_value_init (value, G_TYPE_STRING);
@@ -304,22 +318,37 @@ xfce_appfinder_model_get_value (GtkTreeModel *tree_model,
       break;
 
     case XFCE_APPFINDER_MODEL_COLUMN_ICON:
-      if (item->pixbuf == NULL
-          && item->item != NULL)
-        item->pixbuf = xfce_appfinder_model_load_pixbuf (garcon_menu_item_get_icon_name (item->item), 32);
+      if (item->pixbuf == NULL)
+        {
+          if (item->item != NULL)
+            {
+              name = garcon_menu_item_get_icon_name (item->item);
+              item->pixbuf = xfce_appfinder_model_load_pixbuf (name, 32);
+            }
+          else if (item->command != NULL)
+            {
+              item->pixbuf = g_object_ref (G_OBJECT (model->command_icon));
+            }
+        }
 
       g_value_init (value, GDK_TYPE_PIXBUF);
       g_value_set_object (value, item->pixbuf);
       break;
 
     case XFCE_APPFINDER_MODEL_COLUMN_ICON_LARGE:
+      if (item->item != NULL)
+        name = garcon_menu_item_get_icon_name (item->item);
+      else
+        name = GTK_STOCK_EXECUTE;
+
       g_value_init (value, GDK_TYPE_PIXBUF);
-      g_value_take_object (value, xfce_appfinder_model_load_pixbuf (garcon_menu_item_get_icon_name (item->item), 48));
+      g_value_take_object (value, xfce_appfinder_model_load_pixbuf (name, 48));
       break;
 
     case XFCE_APPFINDER_MODEL_COLUMN_URI:
       g_value_init (value, G_TYPE_STRING);
-      g_value_take_string (value, garcon_menu_item_get_uri (item->item));
+      if (item->item != NULL)
+        g_value_take_string (value, garcon_menu_item_get_uri (item->item));
       break;
 
     default:
@@ -489,14 +518,20 @@ static gint
 xfce_appfinder_model_item_compare (gconstpointer a,
                                    gconstpointer b)
 {
-  const ModelItem *item_a = a;
-  const ModelItem *item_b = b;
+  const ModelItem *item_a = a, *item_b = b;
+  const gchar     *name_a, *name_b;
 
-  g_return_val_if_fail (GARCON_IS_MENU_ITEM (item_a->item), 0);
-  g_return_val_if_fail (GARCON_IS_MENU_ITEM (item_b->item), 0);
+  if (item_a->item != NULL)
+    name_a = garcon_menu_item_get_name (item_a->item);
+  else
+    name_a = item_a->command;
+
+  if (item_b->item != NULL)
+    name_b = garcon_menu_item_get_name (item_b->item);
+  else
+    name_b = item_b->command;
 
-  return g_utf8_collate (garcon_menu_item_get_name (item_a->item),
-                         garcon_menu_item_get_name (item_b->item));
+  return g_utf8_collate (name_a, name_b);
 }
 
 
@@ -524,6 +559,7 @@ xfce_appfinder_model_item_free (gpointer data)
   g_free (item->abstract);
   g_free (item->key);
   g_free (item->category);
+  g_free (item->command);
   g_slice_free (ModelItem, item);
 }
 
@@ -608,6 +644,7 @@ xfce_appfinder_model_collect_items (XfceAppfinderModel *model,
           item = g_slice_new0 (ModelItem);
           item->visible = TRUE;
           item->item = li->data;
+          item->visible = FALSE;
           item->category = g_strdup (category);
 
           model->collect_items = g_slist_prepend (model->collect_items, item);
@@ -633,17 +670,52 @@ xfce_appfinder_model_collect_items (XfceAppfinderModel *model,
 
 
 
+static void
+xfce_appfinder_model_collect_history (XfceAppfinderModel *model,
+                                      GMappedFile        *history)
+{
+  gchar     *contents, *end;
+  gsize      len;
+  ModelItem *item;
+
+  contents = g_mapped_file_get_contents (history);
+  if (contents == NULL)
+    return;
+
+  for (;;)
+    {
+      end = strchr (contents, '\n');
+      if (G_UNLIKELY (end == NULL))
+        break;
+
+      len = end - contents;
+      if (len > 0)
+        {
+          item = g_slice_new0 (ModelItem);
+          item->command = g_strndup (contents, len);
+          model->collect_items = g_slist_prepend (model->collect_items, item);
+        }
+
+      contents += len + 1;
+    }
+}
+
+
+
 static gpointer
 xfce_appfinder_model_collect_thread (gpointer user_data)
 {
   XfceAppfinderModel *model = XFCE_APPFINDER_MODEL (user_data);
   GError             *error = NULL;
+  gchar              *filename;
+  GMappedFile        *history;
 
   g_return_val_if_fail (GARCON_IS_MENU (model->menu), NULL);
   g_return_val_if_fail (model->collect_items == NULL, NULL);
 
   g_message ("collect thread start");
 
+  /* load menu data */
   if (G_LIKELY (model->menu != NULL))
     {
       if (garcon_menu_load (model->menu, NULL, &error))
@@ -653,6 +725,23 @@ xfce_appfinder_model_collect_thread (gpointer user_data)
       else
         {
           g_warning ("Failed to load the root menu: %s", error->message);
+          g_clear_error (&error);
+        }
+    }
+
+  /* load command history */
+  filename = xfce_resource_lookup (XFCE_RESOURCE_CACHE, "xfce4/xfce4-appfinder/history");
+  if (G_LIKELY (filename != NULL))
+    {
+      history = g_mapped_file_new (filename, FALSE, &error);
+      if (G_LIKELY (history != NULL))
+        {
+          xfce_appfinder_model_collect_history (model, history);
+          g_mapped_file_free (history);
+        }
+      else
+        {
+          g_warning ("Failed to open history file: %s", error->message);
           g_error_free (error);
         }
     }
@@ -692,18 +781,36 @@ xfce_appfinder_model_filter_idle (gpointer data)
       item = li->data;
       visible = TRUE;
 
-      if (model->filter_category != NULL
-          && (item->category == NULL
-              || strcmp (model->filter_category, item->category) != 0))
+      g_return_val_if_fail ((item->item == NULL && item->command != NULL)
+                            || (item->item != NULL && GARCON_IS_MENU_ITEM (item->item)), FALSE);
+
+      if (item->item != NULL)
         {
-          visible = FALSE;
+          if (model->filter_category != NULL
+              && (item->category == NULL
+                  || strcmp (model->filter_category, item->category) != 0))
+            {
+              visible = FALSE;
+            }
+          else if (model->filter_string != NULL)
+            {
+              if (item->key == NULL)
+                item->key = xfce_appfinder_model_item_key (item->item);
+
+              visible = strstr (item->key, model->filter_string) != NULL;
+            }
         }
-      else if (model->filter_string != NULL)
+      else if (item->command != NULL)
         {
-          if (item->key == NULL)
-            item->key = xfce_appfinder_model_item_key (item->item);
-
-          visible = strstr (item->key, model->filter_string) != NULL;
+          if (model->filter_category == NULL
+              || *model->filter_category != '\0')
+            {
+              visible = FALSE;
+            }
+          else if (model->filter_string != NULL)
+            {
+              visible = strstr (item->command, model->filter_string) != NULL;
+            }
         }
 
       if (item->visible != visible)
@@ -739,7 +846,7 @@ xfce_appfinder_model_filter (XfceAppfinderModel *model)
 {
   if (model->filter_idle_id == 0)
     {
-      model->filter_idle_id = g_idle_add_full (G_PRIORITY_LOW, xfce_appfinder_model_filter_idle,
+      model->filter_idle_id = g_idle_add_full (G_PRIORITY_HIGH_IDLE, xfce_appfinder_model_filter_idle,
                                                model, xfce_appfinder_model_filter_idle_destroyed);
     }
 }
@@ -760,6 +867,9 @@ xfce_appfinder_model_filter_category (XfceAppfinderModel *model,
 {
   g_return_if_fail (XFCE_IS_APPFINDER_MODEL (model));
 
+  if (model->filter_category == category)
+    return;
+
   g_free (model->filter_category);
 
   if (category != NULL)
@@ -780,6 +890,9 @@ xfce_appfinder_model_filter_string (XfceAppfinderModel *model,
 
   g_return_if_fail (XFCE_IS_APPFINDER_MODEL (model));
 
+  if (model->filter_string == seach_string)
+    return;
+
   g_free (model->filter_string);
 
   if (seach_string != NULL && *seach_string != '\0')
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 ad6ef43..5a6dcf2 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
@@ -469,6 +469,8 @@ xfce_appfinder_window_category_changed (GtkTreeSelection    *selection,
 
       if (g_utf8_collate (category, _("All Applications")) == 0)
         xfce_appfinder_model_filter_category (window->model, NULL);
+      else if (g_utf8_collate (category, _("Commands History")) == 0)
+        xfce_appfinder_model_filter_category (window->model, "\0");
       else
         xfce_appfinder_model_filter_category (window->model, category);
 



More information about the Xfce4-commits mailing list