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>
19
#include <glib/gi18n.h>
23
#include "usage-tracker.h"
29
* @short_description: a user-interesting item that can be activated
31
* A #HudItem represents a user-interesting action that can be activated
32
* from the Hud user interface.
38
* This is an opaque structure type.
43
* @parent_class: the #GObjectClass
44
* @activate: virtual function pointer for hud_item_activate()
46
* This is the class vtable for #HudItem.
49
static GHashTable *hud_item_table;
50
static guint64 hud_item_next_id;
52
struct _HudItemPrivate
54
GObject parent_instance;
58
HudTokenList *token_list;
59
HudStringList *tokens;
60
HudStringList *keywords;
65
gchar *pretty_keywords;
66
gchar *pretty_context;
72
G_DEFINE_TYPE (HudItem, hud_item, G_TYPE_OBJECT)
75
hud_item_finalize (GObject *object)
77
HudItem *item = HUD_ITEM (object);
79
g_hash_table_remove (hud_item_table, &item->priv->id);
80
hud_token_list_free (item->priv->token_list);
81
hud_string_list_unref (item->priv->tokens);
82
hud_string_list_unref (item->priv->keywords);
83
g_free (item->priv->app_id);
84
g_free (item->priv->app_icon);
85
g_free (item->priv->usage_tag);
86
g_free (item->priv->shortcut);
87
g_free (item->priv->description);
88
g_free (item->priv->pretty_keywords);
89
g_free (item->priv->pretty_context);
91
G_OBJECT_CLASS (hud_item_parent_class)
96
hud_item_init (HudItem *item)
98
item->priv = G_TYPE_INSTANCE_GET_PRIVATE (item, HUD_TYPE_ITEM, HudItemPrivate);
102
hud_item_class_init (HudItemClass *class)
104
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
106
hud_item_table = g_hash_table_new (g_int64_hash, g_int64_equal);
108
gobject_class->finalize = hud_item_finalize;
110
g_type_class_add_private (class, sizeof (HudItemPrivate));
114
hud_item_format_tokens (GString *string,
115
HudStringList *tokens)
119
tail = hud_string_list_get_tail (tokens);
123
hud_item_format_tokens (string, tail);
124
g_string_append (string, "||");
127
g_string_append (string, hud_string_list_get_head (tokens));
131
hud_item_setup_usage (HudItem *item)
135
if (item->priv->tokens && item->priv->enabled)
137
tag = g_string_new (NULL);
138
hud_item_format_tokens (tag, item->priv->tokens);
139
item->priv->usage_tag = g_string_free (tag, FALSE);
140
item->priv->usage = usage_tracker_get_usage (usage_tracker_get_instance (),
141
item->priv->app_id, item->priv->usage_tag);
146
* hud_item_construct:
148
* @tokens: the search tokens for the item
149
* @shortcut: Keyboard shortcut for the item
150
* @app_id: a string identifying the application
151
* @app_icon: the icon name for the application that created this item
152
* @description: A textual description of the item
153
* @enabled: if the item is enabled
155
* This is the Vala-style chain-up constructor corresponding to
156
* hud_item_new(). @g_type must be a subtype of #HudItem.
158
* Only subclasses of #HudItem should call this.
160
* Returns: a new #HudItem or #HudItem subclass
163
hud_item_construct (GType g_type,
164
HudStringList *tokens,
165
HudStringList *keywords,
166
const gchar *shortcut,
168
const gchar *app_icon,
169
const gchar *description,
174
item = g_object_new (g_type, NULL);
175
item->priv->tokens = hud_string_list_ref (tokens);
176
item->priv->keywords = hud_string_list_ref (keywords);
177
item->priv->app_id = g_strdup (app_id);
178
item->priv->app_icon = g_strdup (app_icon);
179
item->priv->shortcut = g_strdup (shortcut);
180
item->priv->description = g_strdup (description);
181
item->priv->enabled = enabled;
182
item->priv->id = hud_item_next_id++;
183
item->priv->token_list = hud_token_list_new_from_string_list (tokens, keywords);
185
g_hash_table_insert (hud_item_table, &item->priv->id, item);
188
hud_item_setup_usage (item);
195
* @tokens: the search tokens for the item
196
* @shortcut: Keyboard shortcut for the item
197
* @app_id: a string identifying the application
198
* @app_icon: the icon name for the application that created this item
199
* @description: A textual description of the item
200
* @enabled: if the item is enabled
202
* Creates a new #HudItem.
204
* If @enabled is %FALSE then the item will never be in the result of a
207
* Returns: a new #HudItem
210
hud_item_new (HudStringList *tokens,
211
HudStringList *keywords,
212
const gchar *shortcut,
214
const gchar *app_icon,
215
const gchar *description,
218
return hud_item_construct (HUD_TYPE_ITEM, tokens, keywords, shortcut, app_id, app_icon, description, enabled);
224
* @platform_data: platform data
228
* @platform_data is platform data in the #GApplication or
229
* #GRemoteActionGroup sense. It should be a #GVariant with the type
230
* <literal>a{sv}</literal>.
233
hud_item_activate (HudItem *item,
234
GVariant *platform_data)
236
g_return_if_fail (HUD_IS_ITEM (item));
238
HUD_ITEM_GET_CLASS (item)
239
->activate (item, platform_data);
241
hud_item_mark_usage(item);
245
* hud_item_mark_usage:
248
* Marks usage on @item.
251
hud_item_mark_usage (HudItem *item)
253
g_return_if_fail (HUD_IS_ITEM (item));
255
if (item->priv->usage_tag)
257
usage_tracker_mark_usage (usage_tracker_get_instance (), item->priv->app_id, item->priv->usage_tag);
258
item->priv->usage = usage_tracker_get_usage (usage_tracker_get_instance (),
259
item->priv->app_id, item->priv->usage_tag);
264
* hud_item_get_tokens:
267
* Gets the tokens that represent the description of @item.
269
* This is a #HudStringList in reverse order of how the item should
270
* appear in the Hud. For example, "File > Open" would be represneted
271
* by the list <code>['Open', 'File']</code>.
273
* Returns: (transfer none): the tokens
276
hud_item_get_tokens (HudItem *item)
278
g_return_val_if_fail (HUD_IS_ITEM (item), NULL);
280
return item->priv->tokens;
284
* hud_item_get_keywords:
287
* Gets the additional keywords that represent the description of @item.
289
* Returns: (transfer none): the tokens
292
hud_item_get_keywords (HudItem *item)
294
g_return_val_if_fail (HUD_IS_ITEM (item), NULL);
296
return item->priv->keywords;
300
* hud_item_get_item_icon:
303
* Gets the icon for the action represented by @item, if one exists.
305
* Returns: the icon name, or "" if there is no icon
308
hud_item_get_item_icon (HudItem *item)
314
* hud_item_get_app_id:
317
* Gets the desktop file of the application that @item lies within.
319
* Returns: the desktop file , or "" if there is no desktop file
323
hud_item_get_app_id (HudItem *item)
325
return item->priv->app_id ? item->priv->app_id : "";
329
* hud_item_get_app_icon:
332
* Gets the icon of the application that @item lies within.
334
* Returns: the icon name, or "" if there is no icon
337
hud_item_get_app_icon (HudItem *item)
339
return item->priv->app_icon ? item->priv->app_icon : "";
343
* hud_item_get_usage:
346
* Gets the use-count of @item.
348
* This is the number of times the item has been activated in recent
351
* Returns: the usage count
354
hud_item_get_usage (HudItem *item)
356
return item->priv->usage;
360
* hud_item_get_enabled:
363
* Checks if the item is disabled or enabled.
365
* Disabled items should never appear in search results.
367
* Returns: if the item is enabled
370
hud_item_get_enabled (HudItem *item)
372
return item->priv->enabled;
379
* Gets the unique identifier of this #HudItem.
381
* The identifier can be used to refetch the item using
382
* hud_item_lookup() for as long as the item has not been destroyed.
384
* Returns: the ID of the item
387
hud_item_get_id (HudItem *item)
389
return item->priv->id;
394
* @id: an item identifier
396
* Looks up a #HudItem by its ID.
398
* The ID for a #HudItem can be queried with hud_item_get_id().
400
* Returns: (transfer none): the #HudItem with the given @id, or %NULL
404
hud_item_lookup (guint64 id)
406
return g_hash_table_lookup (hud_item_table, &id);
410
hud_item_get_token_list (HudItem *item)
412
return item->priv->token_list;
416
* hud_item_get_command:
419
* Returns the user visible string that describes this command
421
* Returns: A string that can be shown to the user
424
hud_item_get_command (HudItem *item)
426
g_return_val_if_fail(HUD_IS_ITEM(item), NULL);
428
if (item->priv->tokens == NULL) {
429
return _("No Command");
432
return hud_string_list_get_head(item->priv->tokens);
436
* hud_item_get_description:
439
* Determines which description should be shown based on the data
440
* available. From the menu context, to a formal description, to
441
* the keywords. This is the guy that makes that into something
444
* Returns: A string that can be shown to the user
447
hud_item_get_description (HudItem *item)
449
g_return_val_if_fail(HUD_IS_ITEM(item), NULL);
451
if (item->priv->keywords != NULL) {
452
if (item->priv->pretty_keywords == NULL) {
453
item->priv->pretty_keywords = hud_string_list_pretty_print(item->priv->keywords, _(", "));
455
return item->priv->pretty_keywords;
458
if (item->priv->description != NULL) {
459
return item->priv->description;
462
if (item->priv->tokens != NULL && hud_string_list_get_tail(item->priv->tokens) != NULL) {
463
if (item->priv->pretty_context == NULL) {
464
item->priv->pretty_context = hud_string_list_pretty_print(hud_string_list_get_tail(item->priv->tokens), _(", "));
466
return item->priv->pretty_context;
473
* hud_item_get_shortcut:
476
* Returns the shortcut for activating this command
478
* Returns: A string that can be shown to the user
481
hud_item_get_shortcut (HudItem *item)
483
g_return_val_if_fail(HUD_IS_ITEM(item), "");
485
return item->priv->shortcut;
489
* hud_item_insert_pronounciation:
491
* @user_data: Composite of a #GHashTable of (gchar *, gchar**) and a regex for removing undesirable characters
493
* Get the pronounciations of all the tokens for this item.
496
hud_item_insert_pronounciation (HudItem * item, HudItemPronunciationData * user_data)
498
g_return_if_fail(HUD_IS_ITEM(item));
500
hud_string_list_insert_pronounciation(item->priv->tokens, user_data);