[gnome-shell] app-system: Consider usage frequency in search results



commit da83ad561ba6ff56827b4a310778260827b13da2
Author: Florian MÃllner <fmuellner gnome org>
Date:   Wed Oct 5 16:44:50 2011 +0200

    app-system: Consider usage frequency in search results
    
    Application search results are internally categorized in four sets,
    multiple and single prefix matches and multiple and single substring
    matches. Each set is currently sorted alphabetically by application
    name when concatenating the sets to the final result.
    Change the last step to sort each set by usage frequency instead,
    which is more likely to favor the most relevant match than
    "arbitrary" alphabetic order.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=623372

 src/shell-app-system.c |   19 +++++++++++--------
 src/shell-app-usage.c  |   39 +++++++++++++++++++++++++++++++++++++++
 src/shell-app-usage.h  |    4 ++++
 3 files changed, 54 insertions(+), 8 deletions(-)
---
diff --git a/src/shell-app-system.c b/src/shell-app-system.c
index 51821d8..e9de0db 100644
--- a/src/shell-app-system.c
+++ b/src/shell-app-system.c
@@ -3,6 +3,7 @@
 #include "config.h"
 
 #include "shell-app-system.h"
+#include "shell-app-usage.h"
 #include <string.h>
 
 #include <gio/gio.h>
@@ -670,14 +671,16 @@ shell_app_system_get_running (ShellAppSystem *self)
 
 
 static gint
-compare_apps_by_name (gconstpointer a,
-                      gconstpointer b,
-                      gpointer      data)
+compare_apps_by_usage (gconstpointer a,
+                       gconstpointer b,
+                       gpointer      data)
 {
+  ShellAppUsage *usage = shell_app_usage_get_default ();
+
   ShellApp *app_a = (ShellApp*)a;
   ShellApp *app_b = (ShellApp*)b;
 
-  return shell_app_compare_by_name (app_a, app_b);
+  return shell_app_usage_compare (usage, "", app_a, app_b);
 }
 
 static GSList *
@@ -688,16 +691,16 @@ sort_and_concat_results (ShellAppSystem *system,
                          GSList         *substring_matches)
 {
   multiple_prefix_matches = g_slist_sort_with_data (multiple_prefix_matches,
-                                                    compare_apps_by_name,
+                                                    compare_apps_by_usage,
                                                     system);
   prefix_matches = g_slist_sort_with_data (prefix_matches,
-                                           compare_apps_by_name,
+                                           compare_apps_by_usage,
                                            system);
   multiple_substring_matches = g_slist_sort_with_data (multiple_substring_matches,
-                                                       compare_apps_by_name,
+                                                       compare_apps_by_usage,
                                                        system);
   substring_matches = g_slist_sort_with_data (substring_matches,
-                                              compare_apps_by_name,
+                                              compare_apps_by_usage,
                                               system);
   return g_slist_concat (multiple_prefix_matches, g_slist_concat (prefix_matches, g_slist_concat (multiple_substring_matches, substring_matches)));
 }
diff --git a/src/shell-app-usage.c b/src/shell-app-usage.c
index 7b7fe96..bbc4fac 100644
--- a/src/shell-app-usage.c
+++ b/src/shell-app-usage.c
@@ -528,6 +528,45 @@ shell_app_usage_get_most_used (ShellAppUsage   *self,
   return apps;
 }
 
+
+/**
+ * shell_app_usage_compare:
+ * @self: the usage instance to request
+ * @context: Activity identifier
+ * @app_a: First app
+ * @app_b: Second app
+ *
+ * Compare @app_a and @app_b based on frequency of use.
+ *
+ * Returns: -1 if @app_a ranks higher than @app_b, 1 if @app_b ranks higher
+ *          than @app_a, and 0 if both rank equally.
+ */
+int
+shell_app_usage_compare (ShellAppUsage *self,
+                         const char    *context,
+                         ShellApp      *app_a,
+                         ShellApp      *app_b)
+{
+  GHashTable *usages;
+  UsageData *usage_a, *usage_b;
+
+  usages = g_hash_table_lookup (self->app_usages_for_context, context);
+  if (usages == NULL)
+    return 0;
+
+  usage_a = g_hash_table_lookup (usages, shell_app_get_id (app_a));
+  usage_b = g_hash_table_lookup (usages, shell_app_get_id (app_b));
+
+  if (usage_a == NULL && usage_b == NULL)
+    return 0;
+  else if (usage_a == NULL)
+    return 1;
+  else if (usage_b == NULL)
+    return -1;
+
+  return usage_b->score - usage_a->score;
+}
+
 static void
 ensure_queued_save (ShellAppUsage *self)
 {
diff --git a/src/shell-app-usage.h b/src/shell-app-usage.h
index 1070077..aacd364 100644
--- a/src/shell-app-usage.h
+++ b/src/shell-app-usage.h
@@ -30,6 +30,10 @@ ShellAppUsage* shell_app_usage_get_default(void);
 GSList *shell_app_usage_get_most_used (ShellAppUsage *usage,
                                        const char    *context,
                                        gint           max_count);
+int shell_app_usage_compare (ShellAppUsage *self,
+                             const char    *context,
+                             ShellApp      *app_a,
+                             ShellApp      *app_b);
 
 G_END_DECLS
 



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