app-usage: Remove crufty old "context"s idea

Back in the day, there was a proposed system of tracking apps in a
specific context.

The inspiration was that you may have used apps in multiple modes:
Firefox may have been used in both "Programmer Reference" and
"Kitten Videos" contexts. Early user response to the feedback wasn't
too positive - context switching is something that humans have trouble
doing implicitly, let alone explicitly. The old codebase still has a
few remnants of this around; let's finally put them to rest.

Note that we still write out a dummy context tag to the XML file - old
versions of the shell will flat out crash if you don't have one of those
in there, so just leave it in for compatibility sake.

https://bugzilla.gnome.org/show_bug.cgi?id=673767
This commit is contained in:
Jasper St. Pierre 2012-04-09 03:27:47 -03:00 committed by Jonas Ådahl
parent 52c59ac0dd
commit 04d7069d83
6 changed files with 55 additions and 273 deletions

View File

@ -809,11 +809,11 @@ var FrequentView = new Lang.Class({
}, },
hasUsefulData() { hasUsefulData() {
return this._usage.get_most_used("").length >= MIN_FREQUENT_APPS_COUNT; return this._usage.get_most_used().length >= MIN_FREQUENT_APPS_COUNT;
}, },
_loadApps() { _loadApps() {
let mostUsed = this._usage.get_most_used (""); let mostUsed = this._usage.get_most_used();
let hasUsefulData = this.hasUsefulData(); let hasUsefulData = this.hasUsefulData();
this._noFrequentAppsLabel.visible = !hasUsefulData; this._noFrequentAppsLabel.visible = !hasUsefulData;
if(!hasUsefulData) if(!hasUsefulData)
@ -1112,7 +1112,7 @@ var AppSearchProvider = new Lang.Class({
return app && app.should_show(); return app && app.should_show();
}); });
results = results.concat(group.sort( results = results.concat(group.sort(
(a, b) => usage.compare('', a, b) (a, b) => usage.compare(a, b)
)); ));
}); });

View File

@ -386,8 +386,7 @@ _shell_app_system_notify_app_state_changed (ShellAppSystem *self,
* @self: A #ShellAppSystem * @self: A #ShellAppSystem
* *
* Returns the set of applications which currently have at least one * Returns the set of applications which currently have at least one
* open window in the given context. The returned list will be sorted * open window. The returned list will be sorted by shell_app_compare().
* by shell_app_compare().
* *
* Returns: (element-type ShellApp) (transfer container): Active applications * Returns: (element-type ShellApp) (transfer container): Active applications
*/ */

View File

@ -95,8 +95,8 @@ struct _ShellAppUsage
long watch_start_time; long watch_start_time;
ShellApp *watched_app; ShellApp *watched_app;
/* <char *context, GHashTable<char *appid, UsageData *usage>> */ /* <char *appid, UsageData *usage> */
GHashTable *app_usages_for_context; GHashTable *app_usages;
}; };
G_DEFINE_TYPE (ShellAppUsage, shell_app_usage, G_TYPE_OBJECT); G_DEFINE_TYPE (ShellAppUsage, shell_app_usage, G_TYPE_OBJECT);
@ -118,9 +118,6 @@ static void shell_app_usage_finalize (GObject *object);
static void on_session_status_changed (GDBusProxy *proxy, guint status, ShellAppUsage *self); static void on_session_status_changed (GDBusProxy *proxy, guint status, ShellAppUsage *self);
static void on_focus_app_changed (ShellWindowTracker *tracker, GParamSpec *spec, ShellAppUsage *self); static void on_focus_app_changed (ShellWindowTracker *tracker, GParamSpec *spec, ShellAppUsage *self);
static void ensure_queued_save (ShellAppUsage *self); static void ensure_queued_save (ShellAppUsage *self);
static UsageData * get_app_usage_for_context_and_id (ShellAppUsage *self,
const char *context,
const char *appid);
static gboolean idle_save_application_usage (gpointer data); static gboolean idle_save_application_usage (gpointer data);
@ -148,131 +145,35 @@ shell_app_usage_class_init (ShellAppUsageClass *klass)
gobject_class->finalize = shell_app_usage_finalize; gobject_class->finalize = shell_app_usage_finalize;
} }
static GHashTable *
get_usages_for_context (ShellAppUsage *self,
const char *context)
{
GHashTable *context_usages;
context_usages = g_hash_table_lookup (self->app_usages_for_context, context);
if (context_usages == NULL)
{
context_usages = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
g_hash_table_insert (self->app_usages_for_context, g_strdup (context),
context_usages);
}
return context_usages;
}
static UsageData *
get_app_usage_for_context_and_id (ShellAppUsage *self,
const char *context,
const char *appid)
{
UsageData *usage;
GHashTable *context_usages;
context_usages = get_usages_for_context (self, context);
usage = g_hash_table_lookup (context_usages, appid);
if (usage)
return usage;
usage = g_new0 (UsageData, 1);
g_hash_table_insert (context_usages, g_strdup (appid), usage);
return usage;
}
static UsageData * static UsageData *
get_usage_for_app (ShellAppUsage *self, get_usage_for_app (ShellAppUsage *self,
ShellApp *app) ShellApp *app)
{ {
const char *context; UsageData *usage;
const char *appid = shell_app_get_id (app);
context = _shell_window_tracker_get_app_context (shell_window_tracker_get_default (), app); usage = g_hash_table_lookup (self->app_usages, appid);
if (usage)
return usage;
return get_app_usage_for_context_and_id (self, context, shell_app_get_id (app)); usage = g_new0 (UsageData, 1);
} g_hash_table_insert (self->app_usages, g_strdup (appid), usage);
typedef struct { return usage;
gboolean in_context;
GHashTableIter context_iter;
const char *context_id;
GHashTableIter usage_iter;
} UsageIterator;
static void
usage_iterator_init (ShellAppUsage *self,
UsageIterator *iter)
{
iter->in_context = FALSE;
g_hash_table_iter_init (&(iter->context_iter), self->app_usages_for_context);
}
static gboolean
usage_iterator_next (ShellAppUsage *self,
UsageIterator *iter,
const char **context,
const char **id,
UsageData **usage)
{
gpointer key, value;
gboolean next_context;
if (!iter->in_context)
next_context = TRUE;
else if (!g_hash_table_iter_next (&(iter->usage_iter), &key, &value))
next_context = TRUE;
else
next_context = FALSE;
while (next_context)
{
GHashTable *app_usages;
if (!g_hash_table_iter_next (&(iter->context_iter), &key, &value))
return FALSE;
iter->in_context = TRUE;
iter->context_id = key;
app_usages = value;
g_hash_table_iter_init (&(iter->usage_iter), app_usages);
next_context = !g_hash_table_iter_next (&(iter->usage_iter), &key, &value);
}
*context = iter->context_id;
*id = key;
*usage = value;
return TRUE;
}
static void
usage_iterator_remove (ShellAppUsage *self,
UsageIterator *iter)
{
g_assert (iter->in_context);
g_hash_table_iter_remove (&(iter->usage_iter));
} }
/* Limit the score to a certain level so that most used apps can change */ /* Limit the score to a certain level so that most used apps can change */
static void static void
normalize_usage (ShellAppUsage *self) normalize_usage (ShellAppUsage *self)
{ {
UsageIterator iter; GHashTableIter iter;
const char *context;
const char *id;
UsageData *usage; UsageData *usage;
usage_iterator_init (self, &iter); g_hash_table_iter_init (&iter, self->app_usages);
while (usage_iterator_next (self, &iter, &context, &id, &usage)) while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &usage))
{
usage->score /= 2; usage->score /= 2;
} }
}
static void static void
increment_usage_for_app_at_time (ShellAppUsage *self, increment_usage_for_app_at_time (ShellAppUsage *self,
@ -395,7 +296,7 @@ shell_app_usage_init (ShellAppUsage *self)
global = shell_global_get (); global = shell_global_get ();
self->app_usages_for_context = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_destroy); self->app_usages = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
tracker = shell_window_tracker_get_default (); tracker = shell_window_tracker_get_default ();
g_signal_connect (tracker, "notify::focus-app", G_CALLBACK (on_focus_app_changed), self); g_signal_connect (tracker, "notify::focus-app", G_CALLBACK (on_focus_app_changed), self);
@ -451,25 +352,20 @@ shell_app_usage_finalize (GObject *object)
G_OBJECT_CLASS (shell_app_usage_parent_class)->finalize(object); G_OBJECT_CLASS (shell_app_usage_parent_class)->finalize(object);
} }
typedef struct {
ShellAppUsage *usage;
GHashTable *context_usages;
} SortAppsByUsageData;
static int static int
sort_apps_by_usage (gconstpointer a, sort_apps_by_usage (gconstpointer a,
gconstpointer b, gconstpointer b,
gpointer datap) gpointer datap)
{ {
SortAppsByUsageData *data = datap; ShellAppUsage *self = datap;
ShellApp *app_a, *app_b; ShellApp *app_a, *app_b;
UsageData *usage_a, *usage_b; UsageData *usage_a, *usage_b;
app_a = (ShellApp*)a; app_a = (ShellApp*)a;
app_b = (ShellApp*)b; app_b = (ShellApp*)b;
usage_a = g_hash_table_lookup (data->context_usages, shell_app_get_id (app_a)); usage_a = g_hash_table_lookup (self->app_usages, shell_app_get_id (app_a));
usage_b = g_hash_table_lookup (data->context_usages, shell_app_get_id (app_b)); usage_b = g_hash_table_lookup (self->app_usages, shell_app_get_id (app_b));
return usage_b->score - usage_a->score; return usage_b->score - usage_a->score;
} }
@ -477,33 +373,23 @@ sort_apps_by_usage (gconstpointer a,
/** /**
* shell_app_usage_get_most_used: * shell_app_usage_get_most_used:
* @usage: the usage instance to request * @usage: the usage instance to request
* @context: Activity identifier
*
* Get a list of most popular applications for a given context.
* *
* Returns: (element-type ShellApp) (transfer full): List of applications * Returns: (element-type ShellApp) (transfer full): List of applications
*/ */
GSList * GSList *
shell_app_usage_get_most_used (ShellAppUsage *self, shell_app_usage_get_most_used (ShellAppUsage *self)
const char *context)
{ {
GSList *apps; GSList *apps;
GList *appids, *iter; char *appid;
GHashTable *usages;
ShellAppSystem *appsys; ShellAppSystem *appsys;
SortAppsByUsageData data; GHashTableIter iter;
usages = g_hash_table_lookup (self->app_usages_for_context, context);
if (usages == NULL)
return NULL;
appsys = shell_app_system_get_default (); appsys = shell_app_system_get_default ();
appids = g_hash_table_get_keys (usages); g_hash_table_iter_init (&iter, self->app_usages);
apps = NULL; apps = NULL;
for (iter = appids; iter; iter = iter->next) while (g_hash_table_iter_next (&iter, (gpointer *) &appid, NULL))
{ {
const char *appid = iter->data;
ShellApp *app; ShellApp *app;
app = shell_app_system_lookup_app (appsys, appid); app = shell_app_system_lookup_app (appsys, appid);
@ -513,11 +399,7 @@ shell_app_usage_get_most_used (ShellAppUsage *self,
apps = g_slist_prepend (apps, g_object_ref (app)); apps = g_slist_prepend (apps, g_object_ref (app));
} }
g_list_free (appids); apps = g_slist_sort_with_data (apps, sort_apps_by_usage, self);
data.usage = self;
data.context_usages = usages;
apps = g_slist_sort_with_data (apps, sort_apps_by_usage, &data);
return apps; return apps;
} }
@ -526,7 +408,6 @@ shell_app_usage_get_most_used (ShellAppUsage *self,
/** /**
* shell_app_usage_compare: * shell_app_usage_compare:
* @self: the usage instance to request * @self: the usage instance to request
* @context: Activity identifier
* @id_a: ID of first app * @id_a: ID of first app
* @id_b: ID of second app * @id_b: ID of second app
* *
@ -537,19 +418,13 @@ shell_app_usage_get_most_used (ShellAppUsage *self,
*/ */
int int
shell_app_usage_compare (ShellAppUsage *self, shell_app_usage_compare (ShellAppUsage *self,
const char *context,
const char *id_a, const char *id_a,
const char *id_b) const char *id_b)
{ {
GHashTable *usages;
UsageData *usage_a, *usage_b; UsageData *usage_a, *usage_b;
usages = g_hash_table_lookup (self->app_usages_for_context, context); usage_a = g_hash_table_lookup (self->app_usages, id_a);
if (usages == NULL) usage_b = g_hash_table_lookup (self->app_usages, id_b);
return 0;
usage_a = g_hash_table_lookup (usages, id_a);
usage_b = g_hash_table_lookup (usages, id_b);
if (usage_a == NULL && usage_b == NULL) if (usage_a == NULL && usage_b == NULL)
return 0; return 0;
@ -577,9 +452,7 @@ ensure_queued_save (ShellAppUsage *self)
static gboolean static gboolean
idle_clean_usage (ShellAppUsage *self) idle_clean_usage (ShellAppUsage *self)
{ {
UsageIterator iter; GHashTableIter iter;
const char *context;
const char *id;
UsageData *usage; UsageData *usage;
long current_time; long current_time;
long week_ago; long week_ago;
@ -587,13 +460,13 @@ idle_clean_usage (ShellAppUsage *self)
current_time = get_time (); current_time = get_time ();
week_ago = current_time - (7 * 24 * 60 * 60); week_ago = current_time - (7 * 24 * 60 * 60);
usage_iterator_init (self, &iter); g_hash_table_iter_init (&iter, self->app_usages);
while (usage_iterator_next (self, &iter, &context, &id, &usage)) while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &usage))
{ {
if ((usage->score < SCORE_MIN) && if ((usage->score < SCORE_MIN) &&
(usage->last_seen < week_ago)) (usage->last_seen < week_ago))
usage_iterator_remove (self, &iter); g_hash_table_iter_remove (&iter);
} }
return FALSE; return FALSE;
@ -672,10 +545,8 @@ static gboolean
idle_save_application_usage (gpointer data) idle_save_application_usage (gpointer data)
{ {
ShellAppUsage *self = SHELL_APP_USAGE (data); ShellAppUsage *self = SHELL_APP_USAGE (data);
UsageIterator iter; char *id;
const char *current_context; GHashTableIter iter;
const char *context;
const char *id;
UsageData *usage; UsageData *usage;
GFileOutputStream *output; GFileOutputStream *output;
GOutputStream *buffered_output; GOutputStream *buffered_output;
@ -699,11 +570,12 @@ idle_save_application_usage (gpointer data)
if (!g_data_output_stream_put_string (data_output, "<?xml version=\"1.0\"?>\n<application-state>\n", NULL, &error)) if (!g_data_output_stream_put_string (data_output, "<?xml version=\"1.0\"?>\n<application-state>\n", NULL, &error))
goto out; goto out;
if (!g_data_output_stream_put_string (data_output, " <context id=\"\">\n", NULL, &error))
goto out;
usage_iterator_init (self, &iter); g_hash_table_iter_init (&iter, self->app_usages);
current_context = NULL; while (g_hash_table_iter_next (&iter, (gpointer *) &id, (gpointer *) &usage))
while (usage_iterator_next (self, &iter, &context, &id, &usage))
{ {
ShellApp *app; ShellApp *app;
@ -712,28 +584,12 @@ idle_save_application_usage (gpointer data)
if (!app) if (!app)
continue; continue;
if (context != current_context)
{
if (current_context != NULL)
{
if (!g_data_output_stream_put_string (data_output, " </context>", NULL, &error))
goto out;
}
current_context = context;
if (!g_data_output_stream_put_string (data_output, " <context", NULL, &error))
goto out;
if (!write_attribute_string (data_output, "id", context, &error))
goto out;
if (!g_data_output_stream_put_string (data_output, ">\n", NULL, &error))
goto out;
}
if (!g_data_output_stream_put_string (data_output, " <application", NULL, &error)) if (!g_data_output_stream_put_string (data_output, " <application", NULL, &error))
goto out; goto out;
if (!write_attribute_string (data_output, "id", id, &error)) if (!write_attribute_string (data_output, "id", id, &error))
goto out; goto out;
if (!write_attribute_uint (data_output, "open-window-count", shell_app_get_n_windows (app), &error)) if (!write_attribute_uint (data_output, "open-window-count", shell_app_get_n_windows (app), &error))
goto out; goto out;
if (!write_attribute_double (data_output, "score", usage->score, &error)) if (!write_attribute_double (data_output, "score", usage->score, &error))
goto out; goto out;
if (!write_attribute_uint (data_output, "last-seen", usage->last_seen, &error)) if (!write_attribute_uint (data_output, "last-seen", usage->last_seen, &error))
@ -741,11 +597,8 @@ idle_save_application_usage (gpointer data)
if (!g_data_output_stream_put_string (data_output, "/>\n", NULL, &error)) if (!g_data_output_stream_put_string (data_output, "/>\n", NULL, &error))
goto out; goto out;
} }
if (current_context != NULL)
{
if (!g_data_output_stream_put_string (data_output, " </context>\n", NULL, &error)) if (!g_data_output_stream_put_string (data_output, " </context>\n", NULL, &error))
goto out; goto out;
}
if (!g_data_output_stream_put_string (data_output, "</application-state>\n", NULL, &error)) if (!g_data_output_stream_put_string (data_output, "</application-state>\n", NULL, &error))
goto out; goto out;
@ -761,11 +614,6 @@ out:
return FALSE; return FALSE;
} }
typedef struct {
ShellAppUsage *self;
char *context;
} ParseData;
static void static void
shell_app_usage_start_element_handler (GMarkupParseContext *context, shell_app_usage_start_element_handler (GMarkupParseContext *context,
const gchar *element_name, const gchar *element_name,
@ -774,35 +622,13 @@ shell_app_usage_start_element_handler (GMarkupParseContext *context,
gpointer user_data, gpointer user_data,
GError **error) GError **error)
{ {
ParseData *data = user_data; ShellAppUsage *self = user_data;
if (strcmp (element_name, "application-state") == 0) if (strcmp (element_name, "application-state") == 0)
{ {
} }
else if (strcmp (element_name, "context") == 0) else if (strcmp (element_name, "context") == 0)
{ {
char *id = NULL;
const char **attribute;
const char **value;
for (attribute = attribute_names, value = attribute_values; *attribute; attribute++, value++)
{
if (strcmp (*attribute, "id") == 0)
{
id = g_strdup (*value);
break;
}
}
if (!id)
{
g_set_error (error,
G_MARKUP_ERROR,
G_MARKUP_ERROR_PARSE,
"Missing attribute id on <%s> element",
element_name);
return;
}
data->context = id;
} }
else if (strcmp (element_name, "application") == 0) else if (strcmp (element_name, "application") == 0)
{ {
@ -810,7 +636,6 @@ shell_app_usage_start_element_handler (GMarkupParseContext *context,
const char **value; const char **value;
UsageData *usage; UsageData *usage;
char *appid = NULL; char *appid = NULL;
GHashTable *usage_table;
for (attribute = attribute_names, value = attribute_values; *attribute; attribute++, value++) for (attribute = attribute_names, value = attribute_values; *attribute; attribute++, value++)
{ {
@ -831,10 +656,8 @@ shell_app_usage_start_element_handler (GMarkupParseContext *context,
return; return;
} }
usage_table = get_usages_for_context (data->self, data->context);
usage = g_new0 (UsageData, 1); usage = g_new0 (UsageData, 1);
g_hash_table_insert (usage_table, appid, usage); g_hash_table_insert (self->app_usages, appid, usage);
for (attribute = attribute_names, value = attribute_values; *attribute; attribute++, value++) for (attribute = attribute_names, value = attribute_values; *attribute; attribute++, value++)
{ {
@ -842,7 +665,7 @@ shell_app_usage_start_element_handler (GMarkupParseContext *context,
{ {
guint count = strtoul (*value, NULL, 10); guint count = strtoul (*value, NULL, 10);
if (count > 0) if (count > 0)
data->self->previously_running = g_slist_prepend (data->self->previously_running, self->previously_running = g_slist_prepend (self->previously_running,
g_strdup (appid)); g_strdup (appid));
} }
else if (strcmp (*attribute, "score") == 0) else if (strcmp (*attribute, "score") == 0)
@ -865,36 +688,11 @@ shell_app_usage_start_element_handler (GMarkupParseContext *context,
} }
} }
static void
shell_app_usage_end_element_handler (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error)
{
ParseData *data = user_data;
if (strcmp (element_name, "context") == 0)
{
g_free (data->context);
data->context = NULL;
}
}
static void
shell_app_usage_text_handler (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error)
{
/* do nothing, very very fast */
}
static GMarkupParser app_state_parse_funcs = static GMarkupParser app_state_parse_funcs =
{ {
shell_app_usage_start_element_handler, shell_app_usage_start_element_handler,
shell_app_usage_end_element_handler, NULL,
shell_app_usage_text_handler, NULL,
NULL, NULL,
NULL NULL
}; };
@ -904,7 +702,6 @@ static void
restore_from_file (ShellAppUsage *self) restore_from_file (ShellAppUsage *self)
{ {
GFileInputStream *input; GFileInputStream *input;
ParseData parse_data;
GMarkupParseContext *parse_context; GMarkupParseContext *parse_context;
GError *error = NULL; GError *error = NULL;
char buf[1024]; char buf[1024];
@ -919,10 +716,7 @@ restore_from_file (ShellAppUsage *self)
return; return;
} }
memset (&parse_data, 0, sizeof (ParseData)); parse_context = g_markup_parse_context_new (&app_state_parse_funcs, 0, self, NULL);
parse_data.self = self;
parse_data.context = NULL;
parse_context = g_markup_parse_context_new (&app_state_parse_funcs, 0, &parse_data, NULL);
while (TRUE) while (TRUE)
{ {
@ -934,7 +728,6 @@ restore_from_file (ShellAppUsage *self)
} }
out: out:
g_free (parse_data.context);
g_markup_parse_context_free (parse_context); g_markup_parse_context_free (parse_context);
g_input_stream_close ((GInputStream*)input, NULL, NULL); g_input_stream_close ((GInputStream*)input, NULL, NULL);
g_object_unref (input); g_object_unref (input);

View File

@ -13,10 +13,8 @@ G_DECLARE_FINAL_TYPE (ShellAppUsage, shell_app_usage,
ShellAppUsage* shell_app_usage_get_default(void); ShellAppUsage* shell_app_usage_get_default(void);
GSList *shell_app_usage_get_most_used (ShellAppUsage *usage, GSList *shell_app_usage_get_most_used (ShellAppUsage *usage);
const char *context);
int shell_app_usage_compare (ShellAppUsage *self, int shell_app_usage_compare (ShellAppUsage *self,
const char *context,
const char *id_a, const char *id_a,
const char *id_b); const char *id_b);

View File

@ -457,12 +457,6 @@ get_app_for_window (ShellWindowTracker *tracker,
return result; return result;
} }
const char *
_shell_window_tracker_get_app_context (ShellWindowTracker *tracker, ShellApp *app)
{
return "";
}
static void static void
update_focus_app (ShellWindowTracker *self) update_focus_app (ShellWindowTracker *self)
{ {

View File

@ -21,8 +21,6 @@ ShellApp *shell_window_tracker_get_window_app (ShellWindowTracker *tracker, Meta
ShellApp *shell_window_tracker_get_app_from_pid (ShellWindowTracker *tracker, int pid); ShellApp *shell_window_tracker_get_app_from_pid (ShellWindowTracker *tracker, int pid);
const char *_shell_window_tracker_get_app_context (ShellWindowTracker *tracker, ShellApp *app);
GSList *shell_window_tracker_get_startup_sequences (ShellWindowTracker *tracker); GSList *shell_window_tracker_get_startup_sequences (ShellWindowTracker *tracker);
/* Hidden typedef for SnStartupSequence */ /* Hidden typedef for SnStartupSequence */