2009-10-15 23:28:29 +00:00
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
|
2009-11-24 14:07:40 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2009-10-15 23:28:29 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/Xatom.h>
|
|
|
|
#include <gdk/gdk.h>
|
|
|
|
#include <gdk/gdkx.h>
|
2010-05-05 21:05:42 +00:00
|
|
|
#include <glib.h>
|
2009-10-15 23:28:29 +00:00
|
|
|
#include <gio/gio.h>
|
2011-03-05 15:49:24 +00:00
|
|
|
#include <meta/display.h>
|
|
|
|
#include <meta/group.h>
|
|
|
|
#include <meta/window.h>
|
2009-10-15 23:28:29 +00:00
|
|
|
|
|
|
|
#include "shell-app-usage.h"
|
|
|
|
#include "shell-window-tracker.h"
|
|
|
|
#include "shell-global.h"
|
|
|
|
|
|
|
|
/* This file includes modified code from
|
|
|
|
* desktop-data-engine/engine-dbus/hippo-application-monitor.c
|
|
|
|
* in the functions collecting application usage data.
|
|
|
|
* Written by Owen Taylor, originally licensed under LGPL 2.1.
|
|
|
|
* Copyright Red Hat, Inc. 2006-2008
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:shell-app-usage
|
|
|
|
* @short_description: Track application usage/state data
|
|
|
|
*
|
|
|
|
* This class maintains some usage and state statistics for
|
|
|
|
* applications by keeping track of the approximate time an application's
|
|
|
|
* windows are focused, as well as the last workspace it was seen on.
|
|
|
|
* This time tracking is implemented by watching for focus notifications,
|
|
|
|
* and computing a time delta between them. Also we watch the
|
|
|
|
* GNOME Session "StatusChanged" signal which by default is emitted after 5
|
|
|
|
* minutes to signify idle.
|
|
|
|
*/
|
|
|
|
|
2013-03-01 23:56:18 +00:00
|
|
|
#define PRIVACY_SCHEMA "org.gnome.desktop.privacy"
|
|
|
|
#define ENABLE_MONITORING_KEY "remember-app-usage"
|
2009-10-15 23:28:29 +00:00
|
|
|
|
|
|
|
#define FOCUS_TIME_MIN_SECONDS 7 /* Need 7 continuous seconds of focus */
|
|
|
|
|
|
|
|
#define USAGE_CLEAN_DAYS 7 /* If after 7 days we haven't seen an app, purge it */
|
|
|
|
|
|
|
|
/* Data is saved to file SHELL_CONFIG_DIR/DATA_FILENAME */
|
|
|
|
#define DATA_FILENAME "application_state"
|
|
|
|
|
|
|
|
#define IDLE_TIME_TRANSITION_SECONDS 30 /* If we transition to idle, only count
|
|
|
|
* this many seconds of usage */
|
|
|
|
|
|
|
|
/* The ranking algorithm we use is: every time an app score reaches SCORE_MAX,
|
|
|
|
* divide all scores by 2. Scores are raised by 1 unit every SAVE_APPS_TIMEOUT
|
|
|
|
* seconds. This mechanism allows the list to update relatively fast when
|
|
|
|
* a new app is used intensively.
|
|
|
|
* To keep the list clean, and avoid being Big Brother, apps that have not been
|
|
|
|
* seen for a week and whose score is below SCORE_MIN are removed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* How often we save internally app data, in seconds */
|
2009-11-20 01:15:46 +00:00
|
|
|
#define SAVE_APPS_TIMEOUT_SECONDS (5 * 60)
|
2009-10-15 23:28:29 +00:00
|
|
|
|
|
|
|
/* With this value, an app goes from bottom to top of the
|
|
|
|
* usage list in 50 hours of use */
|
|
|
|
#define SCORE_MAX (3600 * 50 / FOCUS_TIME_MIN_SECONDS)
|
|
|
|
|
|
|
|
/* If an app's score in lower than this and the app has not been used in a week,
|
|
|
|
* remove it */
|
|
|
|
#define SCORE_MIN (SCORE_MAX >> 3)
|
|
|
|
|
|
|
|
/* http://www.gnome.org/~mccann/gnome-session/docs/gnome-session.html#org.gnome.SessionManager.Presence */
|
|
|
|
#define GNOME_SESSION_STATUS_IDLE 3
|
|
|
|
|
|
|
|
typedef struct UsageData UsageData;
|
|
|
|
|
|
|
|
struct _ShellAppUsage
|
|
|
|
{
|
|
|
|
GObject parent;
|
|
|
|
|
|
|
|
GFile *configfile;
|
2011-08-09 15:44:44 +00:00
|
|
|
GDBusProxy *session_proxy;
|
2013-03-01 23:56:18 +00:00
|
|
|
GSettings *privacy_settings;
|
2009-10-15 23:28:29 +00:00
|
|
|
guint idle_focus_change_id;
|
|
|
|
guint save_id;
|
|
|
|
gboolean currently_idle;
|
|
|
|
gboolean enable_monitoring;
|
|
|
|
|
|
|
|
long watch_start_time;
|
|
|
|
ShellApp *watched_app;
|
|
|
|
|
2012-04-09 06:27:47 +00:00
|
|
|
/* <char *appid, UsageData *usage> */
|
|
|
|
GHashTable *app_usages;
|
2009-10-15 23:28:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (ShellAppUsage, shell_app_usage, G_TYPE_OBJECT);
|
|
|
|
|
|
|
|
/* Represents an application record for a given context */
|
|
|
|
struct UsageData
|
|
|
|
{
|
|
|
|
gdouble score; /* Based on the number of times we'e seen the app and normalized */
|
|
|
|
long last_seen; /* Used to clear old apps we've only seen a few times */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void shell_app_usage_finalize (GObject *object);
|
|
|
|
|
2011-08-09 15:44:44 +00:00
|
|
|
static void on_session_status_changed (GDBusProxy *proxy, guint status, ShellAppUsage *self);
|
2009-10-15 23:28:29 +00:00
|
|
|
static void on_focus_app_changed (ShellWindowTracker *tracker, GParamSpec *spec, ShellAppUsage *self);
|
|
|
|
static void ensure_queued_save (ShellAppUsage *self);
|
|
|
|
|
|
|
|
static gboolean idle_save_application_usage (gpointer data);
|
|
|
|
|
|
|
|
static void restore_from_file (ShellAppUsage *self);
|
|
|
|
|
|
|
|
static void update_enable_monitoring (ShellAppUsage *self);
|
|
|
|
|
2010-05-05 21:05:42 +00:00
|
|
|
static void on_enable_monitoring_key_changed (GSettings *settings,
|
|
|
|
const gchar *key,
|
|
|
|
ShellAppUsage *self);
|
2009-10-15 23:28:29 +00:00
|
|
|
|
|
|
|
static long
|
|
|
|
get_time (void)
|
|
|
|
{
|
2019-07-31 18:29:46 +00:00
|
|
|
return g_get_real_time () / G_TIME_SPAN_SECOND;
|
2009-10-15 23:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
shell_app_usage_class_init (ShellAppUsageClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
gobject_class->finalize = shell_app_usage_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UsageData *
|
2012-04-09 06:27:47 +00:00
|
|
|
get_usage_for_app (ShellAppUsage *self,
|
|
|
|
ShellApp *app)
|
2009-10-15 23:28:29 +00:00
|
|
|
{
|
|
|
|
UsageData *usage;
|
2012-04-09 06:27:47 +00:00
|
|
|
const char *appid = shell_app_get_id (app);
|
2009-10-15 23:28:29 +00:00
|
|
|
|
2012-04-09 06:27:47 +00:00
|
|
|
usage = g_hash_table_lookup (self->app_usages, appid);
|
2009-10-15 23:28:29 +00:00
|
|
|
if (usage)
|
|
|
|
return usage;
|
|
|
|
|
|
|
|
usage = g_new0 (UsageData, 1);
|
2012-04-09 06:27:47 +00:00
|
|
|
g_hash_table_insert (self->app_usages, g_strdup (appid), usage);
|
2009-10-15 23:28:29 +00:00
|
|
|
|
|
|
|
return usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Limit the score to a certain level so that most used apps can change */
|
|
|
|
static void
|
|
|
|
normalize_usage (ShellAppUsage *self)
|
|
|
|
{
|
2012-04-09 06:27:47 +00:00
|
|
|
GHashTableIter iter;
|
2009-10-15 23:28:29 +00:00
|
|
|
UsageData *usage;
|
|
|
|
|
2012-04-09 06:27:47 +00:00
|
|
|
g_hash_table_iter_init (&iter, self->app_usages);
|
2009-10-15 23:28:29 +00:00
|
|
|
|
2012-04-09 06:27:47 +00:00
|
|
|
while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &usage))
|
|
|
|
usage->score /= 2;
|
2009-10-15 23:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
increment_usage_for_app_at_time (ShellAppUsage *self,
|
|
|
|
ShellApp *app,
|
|
|
|
long time)
|
|
|
|
{
|
|
|
|
UsageData *usage;
|
|
|
|
guint elapsed;
|
|
|
|
guint usage_count;
|
|
|
|
|
|
|
|
usage = get_usage_for_app (self, app);
|
|
|
|
|
|
|
|
usage->last_seen = time;
|
|
|
|
|
|
|
|
elapsed = time - self->watch_start_time;
|
|
|
|
usage_count = elapsed / FOCUS_TIME_MIN_SECONDS;
|
|
|
|
if (usage_count > 0)
|
|
|
|
{
|
|
|
|
usage->score += usage_count;
|
|
|
|
if (usage->score > SCORE_MAX)
|
|
|
|
normalize_usage (self);
|
|
|
|
ensure_queued_save (self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
increment_usage_for_app (ShellAppUsage *self,
|
|
|
|
ShellApp *app)
|
|
|
|
{
|
|
|
|
long curtime = get_time ();
|
|
|
|
increment_usage_for_app_at_time (self, app, curtime);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-09-07 19:20:10 +00:00
|
|
|
on_app_state_changed (ShellAppSystem *app_system,
|
|
|
|
ShellApp *app,
|
|
|
|
gpointer user_data)
|
2009-10-15 23:28:29 +00:00
|
|
|
{
|
|
|
|
ShellAppUsage *self = SHELL_APP_USAGE (user_data);
|
|
|
|
UsageData *usage;
|
|
|
|
gboolean running;
|
|
|
|
|
Kill off ShellAppInfo, move into ShellApp
This dramatically thins down and sanitizes the application code.
The ShellAppSystem changes in a number of ways:
* Preferences are special cased more explicitly; they aren't apps,
they're shortcuts for an app), and we don't have many of them, so
don't need e.g. the optimizations in ShellAppSystem for searching.
* get_app() changes to lookup_app() and returns null if an app isn't
found. The semantics where it tried to find the .desktop file
if we didn't know about it were just broken; I am pretty sure no
caller needs this, and if they do we'll fix them.
* ShellAppSystem maintains two indexes on apps (by desktop file id
and by GMenuTreeEntry), but is no longer in the business of
dealing with GMenuTree as far as hierarchy and categories go. That
is moved up into js/ui/appDisplay.js. Actually, it flattens both
apps and settings.
Also, ShellWindowTracker is now the sole reference-owner for
window-backed apps. We still do the weird "window:0x1234beef" id
for these apps, but a reference is not stored in ShellAppSystem.
The js/ui/appDisplay.js code is rewritten, and sucks a lot less.
Variable names are clearer:
_apps -> _appIcons
_filterApp -> _visibleApps
_filters -> _categoryBox
Similarly for function names. We no longer call (for every app) a
recursive lookup in GMenuTree to see if it's in a particular section
on every category switch; it's all cached.
NOTE - this intentionally reverts the incremental loading code from
commit 7813c5b93f6bcde8c4beae286e82bfc472b2b656. It's fast enough
here without that.
https://bugzilla.gnome.org/show_bug.cgi?id=648149
2011-04-21 17:35:01 +00:00
|
|
|
if (shell_app_is_window_backed (app))
|
2009-10-15 23:28:29 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
usage = get_usage_for_app (self, app);
|
|
|
|
|
2010-06-07 20:31:30 +00:00
|
|
|
running = shell_app_get_state (app) == SHELL_APP_STATE_RUNNING;
|
2009-10-15 23:28:29 +00:00
|
|
|
|
2011-01-24 18:25:09 +00:00
|
|
|
if (running)
|
|
|
|
usage->last_seen = get_time ();
|
2009-10-15 23:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_focus_app_changed (ShellWindowTracker *tracker,
|
|
|
|
GParamSpec *spec,
|
|
|
|
ShellAppUsage *self)
|
|
|
|
{
|
|
|
|
if (self->watched_app != NULL)
|
|
|
|
increment_usage_for_app (self, self->watched_app);
|
|
|
|
|
|
|
|
if (self->watched_app)
|
|
|
|
g_object_unref (self->watched_app);
|
|
|
|
|
|
|
|
g_object_get (tracker, "focus-app", &(self->watched_app), NULL);
|
|
|
|
self->watch_start_time = get_time ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-08-09 15:44:44 +00:00
|
|
|
on_session_status_changed (GDBusProxy *proxy,
|
2009-10-15 23:28:29 +00:00
|
|
|
guint status,
|
|
|
|
ShellAppUsage *self)
|
|
|
|
{
|
|
|
|
gboolean idle;
|
|
|
|
|
|
|
|
idle = (status >= GNOME_SESSION_STATUS_IDLE);
|
|
|
|
if (self->currently_idle == idle)
|
|
|
|
return;
|
|
|
|
|
|
|
|
self->currently_idle = idle;
|
|
|
|
if (idle)
|
|
|
|
{
|
|
|
|
long end_time;
|
|
|
|
|
|
|
|
/* The GNOME Session signal we watch is 5 minutes, but that's a long
|
|
|
|
* time for this purpose. Instead, just add a base 30 seconds.
|
|
|
|
*/
|
|
|
|
if (self->watched_app)
|
|
|
|
{
|
|
|
|
end_time = self->watch_start_time + IDLE_TIME_TRANSITION_SECONDS;
|
|
|
|
increment_usage_for_app_at_time (self, self->watched_app, end_time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Transitioning to !idle, reset the start time */
|
|
|
|
self->watch_start_time = get_time ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-09 15:44:44 +00:00
|
|
|
static void
|
|
|
|
session_proxy_signal (GDBusProxy *proxy, gchar *sender_name, gchar *signal_name, GVariant *parameters, gpointer user_data)
|
|
|
|
{
|
|
|
|
if (g_str_equal (signal_name, "StatusChanged"))
|
|
|
|
{
|
|
|
|
guint status;
|
|
|
|
g_variant_get (parameters, "(u)", &status);
|
|
|
|
on_session_status_changed (proxy, status, SHELL_APP_USAGE (user_data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-15 23:28:29 +00:00
|
|
|
static void
|
|
|
|
shell_app_usage_init (ShellAppUsage *self)
|
|
|
|
{
|
2010-05-05 21:05:42 +00:00
|
|
|
ShellGlobal *global;
|
2010-05-04 11:24:06 +00:00
|
|
|
char *shell_userdata_dir, *path;
|
2011-08-09 15:44:44 +00:00
|
|
|
GDBusConnection *session_bus;
|
2009-10-15 23:28:29 +00:00
|
|
|
ShellWindowTracker *tracker;
|
2011-09-07 19:20:10 +00:00
|
|
|
ShellAppSystem *app_system;
|
2009-10-15 23:28:29 +00:00
|
|
|
|
2010-05-05 21:05:42 +00:00
|
|
|
global = shell_global_get ();
|
|
|
|
|
2012-04-09 06:27:47 +00:00
|
|
|
self->app_usages = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
|
2009-10-15 23:28:29 +00:00
|
|
|
|
|
|
|
tracker = shell_window_tracker_get_default ();
|
|
|
|
g_signal_connect (tracker, "notify::focus-app", G_CALLBACK (on_focus_app_changed), self);
|
2011-09-07 19:20:10 +00:00
|
|
|
|
|
|
|
app_system = shell_app_system_get_default ();
|
|
|
|
g_signal_connect (app_system, "app-state-changed", G_CALLBACK (on_app_state_changed), self);
|
2009-10-15 23:28:29 +00:00
|
|
|
|
2011-08-09 15:44:44 +00:00
|
|
|
session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
|
|
|
|
self->session_proxy = g_dbus_proxy_new_sync (session_bus,
|
|
|
|
G_DBUS_PROXY_FLAGS_NONE,
|
|
|
|
NULL, /* interface info */
|
|
|
|
"org.gnome.SessionManager",
|
|
|
|
"/org/gnome/SessionManager/Presence",
|
|
|
|
"org.gnome.SessionManager",
|
|
|
|
NULL, /* cancellable */
|
|
|
|
NULL /* error */);
|
|
|
|
g_signal_connect (self->session_proxy, "g-signal", G_CALLBACK (session_proxy_signal), self);
|
|
|
|
g_object_unref (session_bus);
|
2009-10-15 23:28:29 +00:00
|
|
|
|
|
|
|
self->currently_idle = FALSE;
|
|
|
|
self->enable_monitoring = FALSE;
|
|
|
|
|
2013-08-23 16:34:38 +00:00
|
|
|
g_object_get (global, "userdatadir", &shell_userdata_dir, NULL),
|
2010-05-04 11:24:06 +00:00
|
|
|
path = g_build_filename (shell_userdata_dir, DATA_FILENAME, NULL);
|
|
|
|
g_free (shell_userdata_dir);
|
2009-10-15 23:28:29 +00:00
|
|
|
self->configfile = g_file_new_for_path (path);
|
|
|
|
g_free (path);
|
|
|
|
restore_from_file (self);
|
|
|
|
|
2013-03-01 23:56:18 +00:00
|
|
|
self->privacy_settings = g_settings_new(PRIVACY_SCHEMA);
|
|
|
|
g_signal_connect (self->privacy_settings,
|
|
|
|
"changed::" ENABLE_MONITORING_KEY,
|
|
|
|
G_CALLBACK (on_enable_monitoring_key_changed),
|
|
|
|
self);
|
2009-10-15 23:28:29 +00:00
|
|
|
update_enable_monitoring (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
shell_app_usage_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
ShellAppUsage *self = SHELL_APP_USAGE (object);
|
|
|
|
|
2019-11-22 00:42:14 +00:00
|
|
|
g_clear_handle_id (&self->save_id, g_source_remove);
|
2010-05-05 21:05:42 +00:00
|
|
|
|
2013-03-01 23:56:18 +00:00
|
|
|
g_object_unref (self->privacy_settings);
|
2009-10-15 23:28:29 +00:00
|
|
|
|
|
|
|
g_object_unref (self->configfile);
|
|
|
|
|
2011-08-09 15:44:44 +00:00
|
|
|
g_object_unref (self->session_proxy);
|
|
|
|
|
2009-10-15 23:28:29 +00:00
|
|
|
G_OBJECT_CLASS (shell_app_usage_parent_class)->finalize(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sort_apps_by_usage (gconstpointer a,
|
|
|
|
gconstpointer b,
|
|
|
|
gpointer datap)
|
|
|
|
{
|
2012-04-09 06:27:47 +00:00
|
|
|
ShellAppUsage *self = datap;
|
2009-10-15 23:28:29 +00:00
|
|
|
ShellApp *app_a, *app_b;
|
|
|
|
UsageData *usage_a, *usage_b;
|
|
|
|
|
|
|
|
app_a = (ShellApp*)a;
|
|
|
|
app_b = (ShellApp*)b;
|
|
|
|
|
2012-04-09 06:27:47 +00:00
|
|
|
usage_a = g_hash_table_lookup (self->app_usages, shell_app_get_id (app_a));
|
|
|
|
usage_b = g_hash_table_lookup (self->app_usages, shell_app_get_id (app_b));
|
2009-10-15 23:28:29 +00:00
|
|
|
|
|
|
|
return usage_b->score - usage_a->score;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* shell_app_usage_get_most_used:
|
|
|
|
* @usage: the usage instance to request
|
|
|
|
*
|
|
|
|
* Returns: (element-type ShellApp) (transfer full): List of applications
|
|
|
|
*/
|
|
|
|
GSList *
|
2012-04-09 06:27:47 +00:00
|
|
|
shell_app_usage_get_most_used (ShellAppUsage *self)
|
2009-10-15 23:28:29 +00:00
|
|
|
{
|
|
|
|
GSList *apps;
|
2012-04-09 06:27:47 +00:00
|
|
|
char *appid;
|
2009-10-15 23:28:29 +00:00
|
|
|
ShellAppSystem *appsys;
|
2012-04-09 06:27:47 +00:00
|
|
|
GHashTableIter iter;
|
2009-10-15 23:28:29 +00:00
|
|
|
|
|
|
|
appsys = shell_app_system_get_default ();
|
|
|
|
|
2012-04-09 06:27:47 +00:00
|
|
|
g_hash_table_iter_init (&iter, self->app_usages);
|
2009-10-15 23:28:29 +00:00
|
|
|
apps = NULL;
|
2012-04-09 06:27:47 +00:00
|
|
|
while (g_hash_table_iter_next (&iter, (gpointer *) &appid, NULL))
|
2009-10-15 23:28:29 +00:00
|
|
|
{
|
|
|
|
ShellApp *app;
|
|
|
|
|
Kill off ShellAppInfo, move into ShellApp
This dramatically thins down and sanitizes the application code.
The ShellAppSystem changes in a number of ways:
* Preferences are special cased more explicitly; they aren't apps,
they're shortcuts for an app), and we don't have many of them, so
don't need e.g. the optimizations in ShellAppSystem for searching.
* get_app() changes to lookup_app() and returns null if an app isn't
found. The semantics where it tried to find the .desktop file
if we didn't know about it were just broken; I am pretty sure no
caller needs this, and if they do we'll fix them.
* ShellAppSystem maintains two indexes on apps (by desktop file id
and by GMenuTreeEntry), but is no longer in the business of
dealing with GMenuTree as far as hierarchy and categories go. That
is moved up into js/ui/appDisplay.js. Actually, it flattens both
apps and settings.
Also, ShellWindowTracker is now the sole reference-owner for
window-backed apps. We still do the weird "window:0x1234beef" id
for these apps, but a reference is not stored in ShellAppSystem.
The js/ui/appDisplay.js code is rewritten, and sucks a lot less.
Variable names are clearer:
_apps -> _appIcons
_filterApp -> _visibleApps
_filters -> _categoryBox
Similarly for function names. We no longer call (for every app) a
recursive lookup in GMenuTree to see if it's in a particular section
on every category switch; it's all cached.
NOTE - this intentionally reverts the incremental loading code from
commit 7813c5b93f6bcde8c4beae286e82bfc472b2b656. It's fast enough
here without that.
https://bugzilla.gnome.org/show_bug.cgi?id=648149
2011-04-21 17:35:01 +00:00
|
|
|
app = shell_app_system_lookup_app (appsys, appid);
|
2009-10-15 23:28:29 +00:00
|
|
|
if (!app)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
apps = g_slist_prepend (apps, g_object_ref (app));
|
|
|
|
}
|
|
|
|
|
2012-04-09 06:27:47 +00:00
|
|
|
apps = g_slist_sort_with_data (apps, sort_apps_by_usage, self);
|
2009-10-15 23:28:29 +00:00
|
|
|
|
|
|
|
return apps;
|
|
|
|
}
|
|
|
|
|
2011-10-05 14:44:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* shell_app_usage_compare:
|
|
|
|
* @self: the usage instance to request
|
2013-11-03 00:13:42 +00:00
|
|
|
* @id_a: ID of first app
|
|
|
|
* @id_b: ID of second app
|
2011-10-05 14:44:50 +00:00
|
|
|
*
|
2013-11-03 00:13:42 +00:00
|
|
|
* Compare @id_a and @id_b based on frequency of use.
|
2011-10-05 14:44:50 +00:00
|
|
|
*
|
2013-11-03 00:13:42 +00:00
|
|
|
* Returns: -1 if @id_a ranks higher than @id_b, 1 if @id_b ranks higher
|
|
|
|
* than @id_a, and 0 if both rank equally.
|
2011-10-05 14:44:50 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
shell_app_usage_compare (ShellAppUsage *self,
|
2013-11-03 00:13:42 +00:00
|
|
|
const char *id_a,
|
|
|
|
const char *id_b)
|
2011-10-05 14:44:50 +00:00
|
|
|
{
|
|
|
|
UsageData *usage_a, *usage_b;
|
|
|
|
|
2012-04-09 06:27:47 +00:00
|
|
|
usage_a = g_hash_table_lookup (self->app_usages, id_a);
|
|
|
|
usage_b = g_hash_table_lookup (self->app_usages, id_b);
|
2011-10-05 14:44:50 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-10-15 23:28:29 +00:00
|
|
|
static void
|
|
|
|
ensure_queued_save (ShellAppUsage *self)
|
|
|
|
{
|
|
|
|
if (self->save_id != 0)
|
|
|
|
return;
|
|
|
|
self->save_id = g_timeout_add_seconds (SAVE_APPS_TIMEOUT_SECONDS, idle_save_application_usage, self);
|
2014-04-10 17:27:23 +00:00
|
|
|
g_source_set_name_by_id (self->save_id, "[gnome-shell] idle_save_application_usage");
|
2009-10-15 23:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean up apps we see rarely.
|
|
|
|
* The logic behind this is that if an app was seen less than SCORE_MIN times
|
|
|
|
* and not seen for a week, it can probably be forgotten about.
|
|
|
|
* This should much reduce the size of the list and avoid 'pollution'. */
|
|
|
|
static gboolean
|
|
|
|
idle_clean_usage (ShellAppUsage *self)
|
|
|
|
{
|
2012-04-09 06:27:47 +00:00
|
|
|
GHashTableIter iter;
|
2009-10-15 23:28:29 +00:00
|
|
|
UsageData *usage;
|
|
|
|
long current_time;
|
|
|
|
long week_ago;
|
|
|
|
|
|
|
|
current_time = get_time ();
|
|
|
|
week_ago = current_time - (7 * 24 * 60 * 60);
|
|
|
|
|
2012-04-09 06:27:47 +00:00
|
|
|
g_hash_table_iter_init (&iter, self->app_usages);
|
2009-10-15 23:28:29 +00:00
|
|
|
|
2012-04-09 06:27:47 +00:00
|
|
|
while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &usage))
|
2009-10-15 23:28:29 +00:00
|
|
|
{
|
|
|
|
if ((usage->score < SCORE_MIN) &&
|
|
|
|
(usage->last_seen < week_ago))
|
2012-04-09 06:27:47 +00:00
|
|
|
g_hash_table_iter_remove (&iter);
|
2009-10-15 23:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
write_escaped (GDataOutputStream *stream,
|
|
|
|
const char *str,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
gboolean ret;
|
|
|
|
char *quoted = g_markup_escape_text (str, -1);
|
|
|
|
ret = g_data_output_stream_put_string (stream, quoted, NULL, error);
|
|
|
|
g_free (quoted);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
write_attribute_string (GDataOutputStream *stream,
|
|
|
|
const char *elt_name,
|
|
|
|
const char *str,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
char *elt;
|
|
|
|
|
|
|
|
elt = g_strdup_printf (" %s=\"", elt_name);
|
|
|
|
ret = g_data_output_stream_put_string (stream, elt, NULL, error);
|
|
|
|
g_free (elt);
|
|
|
|
if (!ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = write_escaped (stream, str, error);
|
|
|
|
if (!ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = g_data_output_stream_put_string (stream, "\"", NULL, error);
|
|
|
|
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
write_attribute_uint (GDataOutputStream *stream,
|
|
|
|
const char *elt_name,
|
|
|
|
guint value,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
gboolean ret;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
buf = g_strdup_printf ("%u", value);
|
|
|
|
ret = write_attribute_string (stream, elt_name, buf, error);
|
|
|
|
g_free (buf);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
write_attribute_double (GDataOutputStream *stream,
|
|
|
|
const char *elt_name,
|
|
|
|
double value,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
g_ascii_dtostr (buf, sizeof (buf), value);
|
|
|
|
ret = write_attribute_string (stream, elt_name, buf, error);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save app data lists to file */
|
|
|
|
static gboolean
|
|
|
|
idle_save_application_usage (gpointer data)
|
|
|
|
{
|
|
|
|
ShellAppUsage *self = SHELL_APP_USAGE (data);
|
2012-04-09 06:27:47 +00:00
|
|
|
char *id;
|
|
|
|
GHashTableIter iter;
|
2009-10-15 23:28:29 +00:00
|
|
|
UsageData *usage;
|
|
|
|
GFileOutputStream *output;
|
|
|
|
GOutputStream *buffered_output;
|
|
|
|
GDataOutputStream *data_output;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
self->save_id = 0;
|
|
|
|
|
|
|
|
/* Parent directory is already created by shell-global */
|
|
|
|
output = g_file_replace (self->configfile, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error);
|
|
|
|
if (!output)
|
|
|
|
{
|
|
|
|
g_debug ("Could not save applications usage data: %s", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
buffered_output = g_buffered_output_stream_new (G_OUTPUT_STREAM (output));
|
|
|
|
g_object_unref (output);
|
|
|
|
data_output = g_data_output_stream_new (G_OUTPUT_STREAM (buffered_output));
|
|
|
|
g_object_unref (buffered_output);
|
|
|
|
|
|
|
|
if (!g_data_output_stream_put_string (data_output, "<?xml version=\"1.0\"?>\n<application-state>\n", NULL, &error))
|
|
|
|
goto out;
|
2012-04-09 06:27:47 +00:00
|
|
|
if (!g_data_output_stream_put_string (data_output, " <context id=\"\">\n", NULL, &error))
|
|
|
|
goto out;
|
2009-10-15 23:28:29 +00:00
|
|
|
|
2012-04-09 06:27:47 +00:00
|
|
|
g_hash_table_iter_init (&iter, self->app_usages);
|
2009-10-15 23:28:29 +00:00
|
|
|
|
2012-04-09 06:27:47 +00:00
|
|
|
while (g_hash_table_iter_next (&iter, (gpointer *) &id, (gpointer *) &usage))
|
2009-10-15 23:28:29 +00:00
|
|
|
{
|
|
|
|
ShellApp *app;
|
|
|
|
|
Kill off ShellAppInfo, move into ShellApp
This dramatically thins down and sanitizes the application code.
The ShellAppSystem changes in a number of ways:
* Preferences are special cased more explicitly; they aren't apps,
they're shortcuts for an app), and we don't have many of them, so
don't need e.g. the optimizations in ShellAppSystem for searching.
* get_app() changes to lookup_app() and returns null if an app isn't
found. The semantics where it tried to find the .desktop file
if we didn't know about it were just broken; I am pretty sure no
caller needs this, and if they do we'll fix them.
* ShellAppSystem maintains two indexes on apps (by desktop file id
and by GMenuTreeEntry), but is no longer in the business of
dealing with GMenuTree as far as hierarchy and categories go. That
is moved up into js/ui/appDisplay.js. Actually, it flattens both
apps and settings.
Also, ShellWindowTracker is now the sole reference-owner for
window-backed apps. We still do the weird "window:0x1234beef" id
for these apps, but a reference is not stored in ShellAppSystem.
The js/ui/appDisplay.js code is rewritten, and sucks a lot less.
Variable names are clearer:
_apps -> _appIcons
_filterApp -> _visibleApps
_filters -> _categoryBox
Similarly for function names. We no longer call (for every app) a
recursive lookup in GMenuTree to see if it's in a particular section
on every category switch; it's all cached.
NOTE - this intentionally reverts the incremental loading code from
commit 7813c5b93f6bcde8c4beae286e82bfc472b2b656. It's fast enough
here without that.
https://bugzilla.gnome.org/show_bug.cgi?id=648149
2011-04-21 17:35:01 +00:00
|
|
|
app = shell_app_system_lookup_app (shell_app_system_get_default(), id);
|
2009-10-15 23:28:29 +00:00
|
|
|
|
|
|
|
if (!app)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!g_data_output_stream_put_string (data_output, " <application", NULL, &error))
|
|
|
|
goto out;
|
|
|
|
if (!write_attribute_string (data_output, "id", id, &error))
|
|
|
|
goto out;
|
|
|
|
if (!write_attribute_double (data_output, "score", usage->score, &error))
|
|
|
|
goto out;
|
|
|
|
if (!write_attribute_uint (data_output, "last-seen", usage->last_seen, &error))
|
|
|
|
goto out;
|
|
|
|
if (!g_data_output_stream_put_string (data_output, "/>\n", NULL, &error))
|
|
|
|
goto out;
|
|
|
|
}
|
2012-04-09 06:27:47 +00:00
|
|
|
if (!g_data_output_stream_put_string (data_output, " </context>\n", NULL, &error))
|
|
|
|
goto out;
|
2009-10-15 23:28:29 +00:00
|
|
|
if (!g_data_output_stream_put_string (data_output, "</application-state>\n", NULL, &error))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (!error)
|
2009-11-20 01:15:46 +00:00
|
|
|
g_output_stream_close_async (G_OUTPUT_STREAM (data_output), 0, NULL, NULL, NULL);
|
2009-10-15 23:28:29 +00:00
|
|
|
g_object_unref (data_output);
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
g_debug ("Could not save applications usage data: %s", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
shell_app_usage_start_element_handler (GMarkupParseContext *context,
|
2012-04-09 06:27:47 +00:00
|
|
|
const gchar *element_name,
|
|
|
|
const gchar **attribute_names,
|
|
|
|
const gchar **attribute_values,
|
|
|
|
gpointer user_data,
|
|
|
|
GError **error)
|
2009-10-15 23:28:29 +00:00
|
|
|
{
|
2012-04-09 06:27:47 +00:00
|
|
|
ShellAppUsage *self = user_data;
|
2009-10-15 23:28:29 +00:00
|
|
|
|
|
|
|
if (strcmp (element_name, "application-state") == 0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (strcmp (element_name, "context") == 0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (strcmp (element_name, "application") == 0)
|
|
|
|
{
|
|
|
|
const char **attribute;
|
|
|
|
const char **value;
|
|
|
|
UsageData *usage;
|
|
|
|
char *appid = NULL;
|
|
|
|
|
|
|
|
for (attribute = attribute_names, value = attribute_values; *attribute; attribute++, value++)
|
|
|
|
{
|
|
|
|
if (strcmp (*attribute, "id") == 0)
|
2012-11-30 04:39:11 +00:00
|
|
|
{
|
|
|
|
appid = g_strdup (*value);
|
|
|
|
break;
|
|
|
|
}
|
2009-10-15 23:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!appid)
|
|
|
|
{
|
|
|
|
g_set_error (error,
|
|
|
|
G_MARKUP_ERROR,
|
|
|
|
G_MARKUP_ERROR_PARSE,
|
|
|
|
"Missing attribute id on <%s> element",
|
|
|
|
element_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
usage = g_new0 (UsageData, 1);
|
2012-04-09 06:27:47 +00:00
|
|
|
g_hash_table_insert (self->app_usages, appid, usage);
|
2009-10-15 23:28:29 +00:00
|
|
|
|
|
|
|
for (attribute = attribute_names, value = attribute_values; *attribute; attribute++, value++)
|
|
|
|
{
|
2012-04-09 07:08:07 +00:00
|
|
|
if (strcmp (*attribute, "score") == 0)
|
2009-10-15 23:28:29 +00:00
|
|
|
{
|
|
|
|
usage->score = g_ascii_strtod (*value, NULL);
|
|
|
|
}
|
|
|
|
else if (strcmp (*attribute, "last-seen") == 0)
|
|
|
|
{
|
|
|
|
usage->last_seen = (guint) g_ascii_strtoull (*value, NULL, 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_set_error (error,
|
|
|
|
G_MARKUP_ERROR,
|
|
|
|
G_MARKUP_ERROR_PARSE,
|
|
|
|
"Unknown element <%s>",
|
|
|
|
element_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GMarkupParser app_state_parse_funcs =
|
|
|
|
{
|
|
|
|
shell_app_usage_start_element_handler,
|
2012-04-09 06:27:47 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2009-10-15 23:28:29 +00:00
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Load data about apps usage from file */
|
|
|
|
static void
|
|
|
|
restore_from_file (ShellAppUsage *self)
|
|
|
|
{
|
|
|
|
GFileInputStream *input;
|
|
|
|
GMarkupParseContext *parse_context;
|
|
|
|
GError *error = NULL;
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
input = g_file_read (self->configfile, NULL, &error);
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
if (error->code != G_IO_ERROR_NOT_FOUND)
|
|
|
|
g_warning ("Could not load applications usage data: %s", error->message);
|
|
|
|
|
|
|
|
g_error_free (error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-09 06:27:47 +00:00
|
|
|
parse_context = g_markup_parse_context_new (&app_state_parse_funcs, 0, self, NULL);
|
2009-10-15 23:28:29 +00:00
|
|
|
|
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
gssize count = g_input_stream_read ((GInputStream*) input, buf, sizeof(buf), NULL, &error);
|
|
|
|
if (count <= 0)
|
|
|
|
goto out;
|
|
|
|
if (!g_markup_parse_context_parse (parse_context, buf, count, &error))
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
g_markup_parse_context_free (parse_context);
|
|
|
|
g_input_stream_close ((GInputStream*)input, NULL, NULL);
|
|
|
|
g_object_unref (input);
|
|
|
|
|
|
|
|
idle_clean_usage (self);
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
g_warning ("Could not load applications usage data: %s", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable or disable the timers, depending on the value of ENABLE_MONITORING_KEY
|
|
|
|
* and taking care of the previous state. If selfing is disabled, we still
|
|
|
|
* report apps usage based on (possibly) saved data, but don't collect data.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
update_enable_monitoring (ShellAppUsage *self)
|
|
|
|
{
|
|
|
|
gboolean enable;
|
|
|
|
|
2013-03-01 23:56:18 +00:00
|
|
|
enable = g_settings_get_boolean (self->privacy_settings,
|
2010-05-05 21:05:42 +00:00
|
|
|
ENABLE_MONITORING_KEY);
|
2009-10-15 23:28:29 +00:00
|
|
|
|
|
|
|
/* Be sure not to start the timers if they were already set */
|
|
|
|
if (enable && !self->enable_monitoring)
|
|
|
|
{
|
|
|
|
on_focus_app_changed (shell_window_tracker_get_default (), NULL, self);
|
|
|
|
}
|
|
|
|
/* ...and don't try to stop them if they were not running */
|
|
|
|
else if (!enable && self->enable_monitoring)
|
|
|
|
{
|
|
|
|
if (self->watched_app)
|
|
|
|
g_object_unref (self->watched_app);
|
|
|
|
self->watched_app = NULL;
|
2019-11-22 00:42:14 +00:00
|
|
|
g_clear_handle_id (&self->save_id, g_source_remove);
|
2009-10-15 23:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self->enable_monitoring = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called when the ENABLE_MONITORING_KEY boolean has changed */
|
|
|
|
static void
|
2010-05-05 21:05:42 +00:00
|
|
|
on_enable_monitoring_key_changed (GSettings *settings,
|
|
|
|
const gchar *key,
|
|
|
|
ShellAppUsage *self)
|
2009-10-15 23:28:29 +00:00
|
|
|
{
|
2010-05-05 21:05:42 +00:00
|
|
|
update_enable_monitoring (self);
|
2009-10-15 23:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* shell_app_usage_get_default:
|
|
|
|
*
|
|
|
|
* Return Value: (transfer none): The global #ShellAppUsage instance
|
|
|
|
*/
|
|
|
|
ShellAppUsage *
|
2015-09-23 18:40:03 +00:00
|
|
|
shell_app_usage_get_default (void)
|
2009-10-15 23:28:29 +00:00
|
|
|
{
|
|
|
|
static ShellAppUsage *instance;
|
|
|
|
|
|
|
|
if (instance == NULL)
|
|
|
|
instance = g_object_new (SHELL_TYPE_APP_USAGE, NULL);
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|