~ubuntu-branches/ubuntu/trusty/hud/trusty-updates

« back to all changes in this revision

Viewing changes to src/item.c

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-01-20 19:43:59 UTC
  • mfrom: (1.1.26)
  • Revision ID: package-import@ubuntu.com-20140120194359-jxxxqtd4ql9elvpf
Tags: 13.10.1+14.04.20140120-0ubuntu1
* New rebuild forced
* Automatic snapshot from revision 362

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2012 Canonical Ltd.
3
 
 *
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.
7
 
 *
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.
12
 
 *
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/>.
15
 
 *
16
 
 * Author: Ryan Lortie <desrt@desrt.ca>
17
 
 */
18
 
 
19
 
#include <glib/gi18n.h>
20
 
 
21
 
#include "item.h"
22
 
 
23
 
#include "usage-tracker.h"
24
 
#include "token.h"
25
 
 
26
 
/**
27
 
 * SECTION:huditem
28
 
 * @title: HudItem
29
 
 * @short_description: a user-interesting item that can be activated
30
 
 *
31
 
 * A #HudItem represents a user-interesting action that can be activated
32
 
 * from the Hud user interface.
33
 
 **/
34
 
 
35
 
/**
36
 
 * HudItem:
37
 
 *
38
 
 * This is an opaque structure type.
39
 
 **/
40
 
 
41
 
/**
42
 
 * HudItemClass:
43
 
 * @parent_class: the #GObjectClass
44
 
 * @activate: virtual function pointer for hud_item_activate()
45
 
 *
46
 
 * This is the class vtable for #HudItem.
47
 
 **/
48
 
 
49
 
static GHashTable *hud_item_table;
50
 
static guint64 hud_item_next_id;
51
 
 
52
 
struct _HudItemPrivate
53
 
{
54
 
  GObject parent_instance;
55
 
 
56
 
  gchar *app_id;
57
 
 
58
 
  HudTokenList *token_list;
59
 
  HudStringList *tokens;
60
 
  HudStringList *keywords;
61
 
  gchar *usage_tag;
62
 
  gchar *app_icon;
63
 
  gchar *shortcut;
64
 
  gchar *description;
65
 
  gchar *pretty_keywords;
66
 
  gchar *pretty_context;
67
 
  gboolean enabled;
68
 
  guint usage;
69
 
  guint64 id;
70
 
};
71
 
 
72
 
G_DEFINE_TYPE (HudItem, hud_item, G_TYPE_OBJECT)
73
 
 
74
 
static void
75
 
hud_item_finalize (GObject *object)
76
 
{
77
 
  HudItem *item = HUD_ITEM (object);
78
 
 
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);
90
 
 
91
 
  G_OBJECT_CLASS (hud_item_parent_class)
92
 
    ->finalize (object);
93
 
}
94
 
 
95
 
static void
96
 
hud_item_init (HudItem *item)
97
 
{
98
 
  item->priv = G_TYPE_INSTANCE_GET_PRIVATE (item, HUD_TYPE_ITEM, HudItemPrivate);
99
 
}
100
 
 
101
 
static void
102
 
hud_item_class_init (HudItemClass *class)
103
 
{
104
 
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
105
 
 
106
 
  hud_item_table = g_hash_table_new (g_int64_hash, g_int64_equal);
107
 
 
108
 
  gobject_class->finalize = hud_item_finalize;
109
 
 
110
 
  g_type_class_add_private (class, sizeof (HudItemPrivate));
111
 
}
112
 
 
113
 
static void
114
 
hud_item_format_tokens (GString       *string,
115
 
                        HudStringList *tokens)
116
 
{
117
 
  HudStringList *tail;
118
 
 
119
 
  tail = hud_string_list_get_tail (tokens);
120
 
 
121
 
  if (tail)
122
 
    {
123
 
      hud_item_format_tokens (string, tail);
124
 
      g_string_append (string, "||");
125
 
    }
126
 
 
127
 
  g_string_append (string, hud_string_list_get_head (tokens));
128
 
}
129
 
 
130
 
static void
131
 
hud_item_setup_usage (HudItem *item)
132
 
{
133
 
  GString *tag;
134
 
 
135
 
  if (item->priv->tokens && item->priv->enabled)
136
 
    {
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);
142
 
    }
143
 
}
144
 
 
145
 
/**
146
 
 * hud_item_construct:
147
 
 * @g_type: a #GType
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
154
 
 *
155
 
 * This is the Vala-style chain-up constructor corresponding to
156
 
 * hud_item_new().  @g_type must be a subtype of #HudItem.
157
 
 *
158
 
 * Only subclasses of #HudItem should call this.
159
 
 *
160
 
 * Returns: a new #HudItem or #HudItem subclass
161
 
 **/
162
 
gpointer
163
 
hud_item_construct (GType          g_type,
164
 
                    HudStringList *tokens,
165
 
                    HudStringList *keywords,
166
 
                    const gchar   *shortcut,
167
 
                    const gchar   *app_id,
168
 
                    const gchar   *app_icon,
169
 
                    const gchar   *description,
170
 
                    gboolean       enabled)
171
 
{
172
 
  HudItem *item;
173
 
 
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);
184
 
 
185
 
  g_hash_table_insert (hud_item_table, &item->priv->id, item);
186
 
 
187
 
  if (app_id)
188
 
    hud_item_setup_usage (item);
189
 
 
190
 
  return item;
191
 
}
192
 
 
193
 
/**
194
 
 * hud_item_new:
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
201
 
 *
202
 
 * Creates a new #HudItem.
203
 
 *
204
 
 * If @enabled is %FALSE then the item will never be in the result of a
205
 
 * search.
206
 
 *
207
 
 * Returns: a new #HudItem
208
 
 **/
209
 
HudItem *
210
 
hud_item_new (HudStringList *tokens,
211
 
              HudStringList *keywords,
212
 
              const gchar   *shortcut,
213
 
              const gchar   *app_id,
214
 
              const gchar   *app_icon,
215
 
              const gchar   *description,
216
 
              gboolean       enabled)
217
 
{
218
 
  return hud_item_construct (HUD_TYPE_ITEM, tokens, keywords, shortcut, app_id, app_icon, description, enabled);
219
 
}
220
 
 
221
 
/**
222
 
 * hud_item_activate:
223
 
 * @item: a #HudItem
224
 
 * @platform_data: platform data
225
 
 *
226
 
 * Activates @item.
227
 
 *
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>.
231
 
 **/
232
 
void
233
 
hud_item_activate (HudItem  *item,
234
 
                   GVariant *platform_data)
235
 
{
236
 
  g_return_if_fail (HUD_IS_ITEM (item));
237
 
 
238
 
  HUD_ITEM_GET_CLASS (item)
239
 
    ->activate (item, platform_data);
240
 
 
241
 
  hud_item_mark_usage(item);
242
 
}
243
 
 
244
 
/**
245
 
 * hud_item_mark_usage:
246
 
 * @item: a #HudItem
247
 
 *
248
 
 * Marks usage on @item.
249
 
 **/
250
 
void
251
 
hud_item_mark_usage (HudItem  *item)
252
 
{
253
 
  g_return_if_fail (HUD_IS_ITEM (item));
254
 
 
255
 
  if (item->priv->usage_tag)
256
 
    {
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);
260
 
    }
261
 
}
262
 
 
263
 
/**
264
 
 * hud_item_get_tokens:
265
 
 * @item: a #HudItem
266
 
 *
267
 
 * Gets the tokens that represent the description of @item.
268
 
 *
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>.
272
 
 *
273
 
 * Returns: (transfer none): the tokens
274
 
 **/
275
 
HudStringList *
276
 
hud_item_get_tokens (HudItem *item)
277
 
{
278
 
  g_return_val_if_fail (HUD_IS_ITEM (item), NULL);
279
 
 
280
 
  return item->priv->tokens;
281
 
}
282
 
 
283
 
/**
284
 
 * hud_item_get_keywords:
285
 
 * @item: a #HudItem
286
 
 *
287
 
 * Gets the additional keywords that represent the description of @item.
288
 
 *
289
 
 * Returns: (transfer none): the tokens
290
 
 **/
291
 
HudStringList *
292
 
hud_item_get_keywords (HudItem *item)
293
 
{
294
 
  g_return_val_if_fail (HUD_IS_ITEM (item), NULL);
295
 
 
296
 
  return item->priv->keywords;
297
 
}
298
 
 
299
 
/**
300
 
 * hud_item_get_item_icon:
301
 
 * @item: a #HudItem
302
 
 *
303
 
 * Gets the icon for the action represented by @item, if one exists.
304
 
 *
305
 
 * Returns: the icon name, or "" if there is no icon
306
 
 **/
307
 
const gchar *
308
 
hud_item_get_item_icon (HudItem *item)
309
 
{
310
 
  return "";
311
 
}
312
 
 
313
 
/**
314
 
 * hud_item_get_app_id:
315
 
 * @item: a #HudItem
316
 
 *
317
 
 * Gets the desktop file of the application that @item lies within.
318
 
 *
319
 
 * Returns: the desktop file , or "" if there is no desktop file
320
 
 **/
321
 
 
322
 
const gchar *
323
 
hud_item_get_app_id (HudItem *item)
324
 
{
325
 
  return item->priv->app_id ? item->priv->app_id : "";
326
 
}
327
 
 
328
 
/**
329
 
 * hud_item_get_app_icon:
330
 
 * @item: a #HudItem
331
 
 *
332
 
 * Gets the icon of the application that @item lies within.
333
 
 *
334
 
 * Returns: the icon name, or "" if there is no icon
335
 
 **/
336
 
const gchar *
337
 
hud_item_get_app_icon (HudItem *item)
338
 
{
339
 
  return item->priv->app_icon ? item->priv->app_icon : "";
340
 
}
341
 
 
342
 
/**
343
 
 * hud_item_get_usage:
344
 
 * @item: a #HudItem
345
 
 *
346
 
 * Gets the use-count of @item.
347
 
 *
348
 
 * This is the number of times the item has been activated in recent
349
 
 * history.
350
 
 *
351
 
 * Returns: the usage count
352
 
 **/
353
 
guint
354
 
hud_item_get_usage (HudItem *item)
355
 
{
356
 
  return item->priv->usage;
357
 
}
358
 
 
359
 
/**
360
 
 * hud_item_get_enabled:
361
 
 * @item: a #HudItem
362
 
 *
363
 
 * Checks if the item is disabled or enabled.
364
 
 *
365
 
 * Disabled items should never appear in search results.
366
 
 *
367
 
 * Returns: if the item is enabled
368
 
 **/
369
 
gboolean
370
 
hud_item_get_enabled (HudItem *item)
371
 
{
372
 
  return item->priv->enabled;
373
 
}
374
 
 
375
 
/**
376
 
 * hud_item_get_id:
377
 
 * @item: a #HudItem
378
 
 *
379
 
 * Gets the unique identifier of this #HudItem.
380
 
 *
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.
383
 
 *
384
 
 * Returns: the ID of the item
385
 
 **/
386
 
guint64
387
 
hud_item_get_id (HudItem *item)
388
 
{
389
 
  return item->priv->id;
390
 
}
391
 
 
392
 
/**
393
 
 * hud_item_lookup:
394
 
 * @id: an item identifier
395
 
 *
396
 
 * Looks up a #HudItem by its ID.
397
 
 *
398
 
 * The ID for a #HudItem can be queried with hud_item_get_id().
399
 
 *
400
 
 * Returns: (transfer none): the #HudItem with the given @id, or %NULL
401
 
 *   if none exists
402
 
 **/
403
 
HudItem *
404
 
hud_item_lookup (guint64 id)
405
 
{
406
 
  return g_hash_table_lookup (hud_item_table, &id);
407
 
}
408
 
 
409
 
HudTokenList *
410
 
hud_item_get_token_list (HudItem *item)
411
 
{
412
 
  return item->priv->token_list;
413
 
}
414
 
 
415
 
/**
416
 
 * hud_item_get_command:
417
 
 * @item: a #HudItem
418
 
 *
419
 
 * Returns the user visible string that describes this command
420
 
 *
421
 
 * Returns: A string that can be shown to the user
422
 
 */
423
 
const gchar *
424
 
hud_item_get_command (HudItem *item)
425
 
{
426
 
        g_return_val_if_fail(HUD_IS_ITEM(item), NULL);
427
 
 
428
 
        if (item->priv->tokens == NULL) {
429
 
                return _("No Command");
430
 
        }
431
 
 
432
 
        return hud_string_list_get_head(item->priv->tokens);
433
 
}
434
 
 
435
 
/**
436
 
 * hud_item_get_description:
437
 
 * @item: a #HudItem
438
 
 *
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
442
 
 * cool.
443
 
 *
444
 
 * Returns: A string that can be shown to the user
445
 
 */
446
 
const gchar *
447
 
hud_item_get_description (HudItem *item)
448
 
{
449
 
        g_return_val_if_fail(HUD_IS_ITEM(item), NULL);
450
 
 
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, _(", "));
454
 
                }
455
 
                return item->priv->pretty_keywords;
456
 
        }
457
 
 
458
 
        if (item->priv->description != NULL) {
459
 
                return item->priv->description;
460
 
        }
461
 
 
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), _(", "));
465
 
                }
466
 
                return item->priv->pretty_context;
467
 
        }
468
 
 
469
 
        return "";
470
 
}
471
 
 
472
 
/**
473
 
 * hud_item_get_shortcut:
474
 
 * @item: a #HudItem
475
 
 *
476
 
 * Returns the shortcut for activating this command
477
 
 *
478
 
 * Returns: A string that can be shown to the user
479
 
 */
480
 
const gchar *
481
 
hud_item_get_shortcut (HudItem *item)
482
 
{
483
 
        g_return_val_if_fail(HUD_IS_ITEM(item), "");
484
 
 
485
 
        return item->priv->shortcut;
486
 
}
487
 
 
488
 
/**
489
 
 * hud_item_insert_pronounciation:
490
 
 * @item: A #HudItem
491
 
 * @user_data: Composite of a #GHashTable of (gchar *, gchar**) and a regex for removing undesirable characters
492
 
 *
493
 
 * Get the pronounciations of all the tokens for this item.
494
 
 */
495
 
void
496
 
hud_item_insert_pronounciation (HudItem * item, HudItemPronunciationData * user_data)
497
 
{
498
 
        g_return_if_fail(HUD_IS_ITEM(item));
499
 
 
500
 
        hud_string_list_insert_pronounciation(item->priv->tokens, user_data);
501
 
        return;
502
 
}