2
* Copyright © 2012 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3, as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License along
14
* with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Author: Ryan Lortie <desrt@desrt.ca>
21
#include "usage-tracker.h"
27
* @short_description: a user-interesting item that can be activated
29
* A #HudItem represents a user-interesting action that can be activated
30
* from the Hud user interface.
36
* This is an opaque structure type.
41
* @parent_class: the #GObjectClass
42
* @activate: virtual function pointer for hud_item_activate()
44
* This is the class vtable for #HudItem.
47
static GHashTable *hud_item_table;
48
static guint64 hud_item_next_id;
50
struct _HudItemPrivate
52
GObject parent_instance;
56
HudTokenList *token_list;
57
HudStringList *tokens;
65
G_DEFINE_TYPE (HudItem, hud_item, G_TYPE_OBJECT)
68
hud_item_finalize (GObject *object)
70
HudItem *item = HUD_ITEM (object);
72
g_hash_table_remove (hud_item_table, &item->priv->id);
73
hud_token_list_free (item->priv->token_list);
74
hud_string_list_unref (item->priv->tokens);
75
g_free (item->priv->desktop_file);
76
g_free (item->priv->app_icon);
77
g_free (item->priv->usage_tag);
79
G_OBJECT_CLASS (hud_item_parent_class)
84
hud_item_init (HudItem *item)
86
item->priv = G_TYPE_INSTANCE_GET_PRIVATE (item, HUD_TYPE_ITEM, HudItemPrivate);
90
hud_item_class_init (HudItemClass *class)
92
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
94
hud_item_table = g_hash_table_new (g_int64_hash, g_int64_equal);
96
gobject_class->finalize = hud_item_finalize;
98
g_type_class_add_private (class, sizeof (HudItemPrivate));
102
hud_item_format_tokens (GString *string,
103
HudStringList *tokens)
107
tail = hud_string_list_get_tail (tokens);
111
hud_item_format_tokens (string, tail);
112
g_string_append (string, "||");
115
g_string_append (string, hud_string_list_get_head (tokens));
119
hud_item_setup_usage (HudItem *item)
123
if (item->priv->tokens && item->priv->enabled)
125
tag = g_string_new (NULL);
126
hud_item_format_tokens (tag, item->priv->tokens);
127
item->priv->usage_tag = g_string_free (tag, FALSE);
128
item->priv->usage = usage_tracker_get_usage (usage_tracker_get_instance (),
129
item->priv->desktop_file, item->priv->usage_tag);
134
* hud_item_construct:
136
* @tokens: the search tokens for the item
137
* @desktop_file: the desktop file of the provider of the item
138
* @app_icon: the icon name for the application that created this item
139
* @enabled: if the item is enabled
141
* This is the Vala-style chain-up constructor corresponding to
142
* hud_item_new(). @g_type must be a subtype of #HudItem.
144
* Only subclasses of #HudItem should call this.
146
* Returns: a new #HudItem or #HudItem subclass
149
hud_item_construct (GType g_type,
150
HudStringList *tokens,
151
const gchar *desktop_file,
152
const gchar *app_icon,
157
item = g_object_new (g_type, NULL);
158
item->priv->tokens = hud_string_list_ref (tokens);
159
item->priv->desktop_file = g_strdup (desktop_file);
160
item->priv->app_icon = g_strdup (app_icon);
161
item->priv->enabled = enabled;
162
item->priv->id = hud_item_next_id++;
163
item->priv->token_list = hud_token_list_new_from_string_list (tokens);
165
g_hash_table_insert (hud_item_table, &item->priv->id, item);
168
hud_item_setup_usage (item);
175
* @tokens: the search tokens for the item
176
* @desktop_file: the desktop file of the provider of the item
177
* @app_icon: the icon name for the application that created this item
178
* @enabled: if the item is enabled
180
* Creates a new #HudItem.
182
* If @enabled is %FALSE then the item will never be in the result of a
185
* Returns: a new #HudItem
188
hud_item_new (HudStringList *tokens,
189
const gchar *desktop_file,
190
const gchar *app_icon,
193
return hud_item_construct (HUD_TYPE_ITEM, tokens, desktop_file, app_icon, enabled);
199
* @platform_data: platform data
203
* @platform_data is platform data in the #GApplication or
204
* #GRemoteActionGroup sense. It should be a #GVariant with the type
205
* <literal>a{sv}</literal>.
208
hud_item_activate (HudItem *item,
209
GVariant *platform_data)
211
g_return_if_fail (HUD_IS_ITEM (item));
213
HUD_ITEM_GET_CLASS (item)
214
->activate (item, platform_data);
216
if (item->priv->usage_tag)
218
usage_tracker_mark_usage (usage_tracker_get_instance (), item->priv->desktop_file, item->priv->usage_tag);
219
item->priv->usage = usage_tracker_get_usage (usage_tracker_get_instance (),
220
item->priv->desktop_file, item->priv->usage_tag);
225
* hud_item_get_tokens:
228
* Gets the tokens that represent the description of @item.
230
* This is a #HudStringList in reverse order of how the item should
231
* appear in the Hud. For example, "File > Open" would be represneted
232
* by the list <code>['Open', 'File']</code>.
234
* Returns: (transfer none): the tokens
237
hud_item_get_tokens (HudItem *item)
239
g_return_val_if_fail (HUD_IS_ITEM (item), NULL);
241
return item->priv->tokens;
245
* hud_item_get_item_icon:
248
* Gets the icon for the action represented by @item, if one exists.
250
* Returns: the icon name, or "" if there is no icon
253
hud_item_get_item_icon (HudItem *item)
259
* hud_item_get_app_icon:
262
* Gets the icon of the application that @item lies within.
264
* Returns: the icon name, or "" if there is no icon
267
hud_item_get_app_icon (HudItem *item)
269
return item->priv->app_icon ? item->priv->app_icon : "";
273
* hud_item_get_usage:
276
* Gets the use-count of @item.
278
* This is the number of times the item has been activated in recent
281
* Returns: the usage count
284
hud_item_get_usage (HudItem *item)
286
return item->priv->usage;
290
* hud_item_get_enabled:
293
* Checks if the item is disabled or enabled.
295
* Disabled items should never appear in search results.
297
* Returns: if the item is enabled
300
hud_item_get_enabled (HudItem *item)
302
return item->priv->enabled;
309
* Gets the unique identifier of this #HudItem.
311
* The identifier can be used to refetch the item using
312
* hud_item_lookup() for as long as the item has not been destroyed.
314
* Returns: the ID of the item
317
hud_item_get_id (HudItem *item)
319
return item->priv->id;
324
* @id: an item identifier
326
* Looks up a #HudItem by its ID.
328
* The ID for a #HudItem can be queried with hud_item_get_id().
330
* Returns: (transfer none): the #HudItem with the given @id, or %NULL
334
hud_item_lookup (guint64 id)
336
return g_hash_table_lookup (hud_item_table, &id);
340
hud_item_get_token_list (HudItem *item)
342
return item->priv->token_list;