~sil2100/indicator-appmenu/indicator-appmenu-qt5

« back to all changes in this revision

Viewing changes to src/huditem.c

  • Committer: Tarmac
  • Author(s): Ted Gould
  • Date: 2013-02-19 17:55:05 UTC
  • mfrom: (216.1.15 hudectomy)
  • Revision ID: tarmac-20130219175505-jjynbyonwdf8ui8p
Remove the HUD files from indicator-appmenu.

Approved by PS Jenkins bot, Mathieu Trudel-Lapierre, Charles Kerr.

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 "huditem.h"
20
 
 
21
 
#include "usage-tracker.h"
22
 
#include "hudtoken.h"
23
 
 
24
 
/**
25
 
 * SECTION:huditem
26
 
 * @title: HudItem
27
 
 * @short_description: a user-interesting item that can be activated
28
 
 *
29
 
 * A #HudItem represents a user-interesting action that can be activated
30
 
 * from the Hud user interface.
31
 
 **/
32
 
 
33
 
/**
34
 
 * HudItem:
35
 
 *
36
 
 * This is an opaque structure type.
37
 
 **/
38
 
 
39
 
/**
40
 
 * HudItemClass:
41
 
 * @parent_class: the #GObjectClass
42
 
 * @activate: virtual function pointer for hud_item_activate()
43
 
 *
44
 
 * This is the class vtable for #HudItem.
45
 
 **/
46
 
 
47
 
static GHashTable *hud_item_table;
48
 
static guint64 hud_item_next_id;
49
 
 
50
 
struct _HudItemPrivate
51
 
{
52
 
  GObject parent_instance;
53
 
 
54
 
  gchar *desktop_file;
55
 
 
56
 
  HudTokenList *token_list;
57
 
  HudStringList *tokens;
58
 
  gchar *usage_tag;
59
 
  gchar *app_icon;
60
 
  gboolean enabled;
61
 
  guint usage;
62
 
  guint64 id;
63
 
};
64
 
 
65
 
G_DEFINE_TYPE (HudItem, hud_item, G_TYPE_OBJECT)
66
 
 
67
 
static void
68
 
hud_item_finalize (GObject *object)
69
 
{
70
 
  HudItem *item = HUD_ITEM (object);
71
 
 
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);
78
 
 
79
 
  G_OBJECT_CLASS (hud_item_parent_class)
80
 
    ->finalize (object);
81
 
}
82
 
 
83
 
static void
84
 
hud_item_init (HudItem *item)
85
 
{
86
 
  item->priv = G_TYPE_INSTANCE_GET_PRIVATE (item, HUD_TYPE_ITEM, HudItemPrivate);
87
 
}
88
 
 
89
 
static void
90
 
hud_item_class_init (HudItemClass *class)
91
 
{
92
 
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
93
 
 
94
 
  hud_item_table = g_hash_table_new (g_int64_hash, g_int64_equal);
95
 
 
96
 
  gobject_class->finalize = hud_item_finalize;
97
 
 
98
 
  g_type_class_add_private (class, sizeof (HudItemPrivate));
99
 
}
100
 
 
101
 
static void
102
 
hud_item_format_tokens (GString       *string,
103
 
                        HudStringList *tokens)
104
 
{
105
 
  HudStringList *tail;
106
 
 
107
 
  tail = hud_string_list_get_tail (tokens);
108
 
 
109
 
  if (tail)
110
 
    {
111
 
      hud_item_format_tokens (string, tail);
112
 
      g_string_append (string, "||");
113
 
    }
114
 
 
115
 
  g_string_append (string, hud_string_list_get_head (tokens));
116
 
}
117
 
 
118
 
static void
119
 
hud_item_setup_usage (HudItem *item)
120
 
{
121
 
  GString *tag;
122
 
 
123
 
  if (item->priv->tokens && item->priv->enabled)
124
 
    {
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);
130
 
    }
131
 
}
132
 
 
133
 
/**
134
 
 * hud_item_construct:
135
 
 * @g_type: a #GType
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
140
 
 *
141
 
 * This is the Vala-style chain-up constructor corresponding to
142
 
 * hud_item_new().  @g_type must be a subtype of #HudItem.
143
 
 *
144
 
 * Only subclasses of #HudItem should call this.
145
 
 *
146
 
 * Returns: a new #HudItem or #HudItem subclass
147
 
 **/
148
 
gpointer
149
 
hud_item_construct (GType          g_type,
150
 
                    HudStringList *tokens,
151
 
                    const gchar   *desktop_file,
152
 
                    const gchar   *app_icon,
153
 
                    gboolean       enabled)
154
 
{
155
 
  HudItem *item;
156
 
 
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);
164
 
 
165
 
  g_hash_table_insert (hud_item_table, &item->priv->id, item);
166
 
 
167
 
  if (desktop_file)
168
 
    hud_item_setup_usage (item);
169
 
 
170
 
  return item;
171
 
}
172
 
 
173
 
/**
174
 
 * hud_item_new:
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
179
 
 *
180
 
 * Creates a new #HudItem.
181
 
 *
182
 
 * If @enabled is %FALSE then the item will never be in the result of a
183
 
 * search.
184
 
 *
185
 
 * Returns: a new #HudItem
186
 
 **/
187
 
HudItem *
188
 
hud_item_new (HudStringList *tokens,
189
 
              const gchar   *desktop_file,
190
 
              const gchar   *app_icon,
191
 
              gboolean       enabled)
192
 
{
193
 
  return hud_item_construct (HUD_TYPE_ITEM, tokens, desktop_file, app_icon, enabled);
194
 
}
195
 
 
196
 
/**
197
 
 * hud_item_activate:
198
 
 * @item: a #HudItem
199
 
 * @platform_data: platform data
200
 
 *
201
 
 * Activates @item.
202
 
 *
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>.
206
 
 **/
207
 
void
208
 
hud_item_activate (HudItem  *item,
209
 
                   GVariant *platform_data)
210
 
{
211
 
  g_return_if_fail (HUD_IS_ITEM (item));
212
 
 
213
 
  HUD_ITEM_GET_CLASS (item)
214
 
    ->activate (item, platform_data);
215
 
 
216
 
  if (item->priv->usage_tag)
217
 
    {
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);
221
 
    }
222
 
}
223
 
 
224
 
/**
225
 
 * hud_item_get_tokens:
226
 
 * @item: a #HudItem
227
 
 *
228
 
 * Gets the tokens that represent the description of @item.
229
 
 *
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>.
233
 
 *
234
 
 * Returns: (transfer none): the tokens
235
 
 **/
236
 
HudStringList *
237
 
hud_item_get_tokens (HudItem *item)
238
 
{
239
 
  g_return_val_if_fail (HUD_IS_ITEM (item), NULL);
240
 
 
241
 
  return item->priv->tokens;
242
 
}
243
 
 
244
 
/**
245
 
 * hud_item_get_item_icon:
246
 
 * @item: a #HudItem
247
 
 *
248
 
 * Gets the icon for the action represented by @item, if one exists.
249
 
 *
250
 
 * Returns: the icon name, or "" if there is no icon
251
 
 **/
252
 
const gchar *
253
 
hud_item_get_item_icon (HudItem *item)
254
 
{
255
 
  return "";
256
 
}
257
 
 
258
 
/**
259
 
 * hud_item_get_app_icon:
260
 
 * @item: a #HudItem
261
 
 *
262
 
 * Gets the icon of the application that @item lies within.
263
 
 *
264
 
 * Returns: the icon name, or "" if there is no icon
265
 
 **/
266
 
const gchar *
267
 
hud_item_get_app_icon (HudItem *item)
268
 
{
269
 
  return item->priv->app_icon ? item->priv->app_icon : "";
270
 
}
271
 
 
272
 
/**
273
 
 * hud_item_get_usage:
274
 
 * @item: a #HudItem
275
 
 *
276
 
 * Gets the use-count of @item.
277
 
 *
278
 
 * This is the number of times the item has been activated in recent
279
 
 * history.
280
 
 *
281
 
 * Returns: the usage count
282
 
 **/
283
 
guint
284
 
hud_item_get_usage (HudItem *item)
285
 
{
286
 
  return item->priv->usage;
287
 
}
288
 
 
289
 
/**
290
 
 * hud_item_get_enabled:
291
 
 * @item: a #HudItem
292
 
 *
293
 
 * Checks if the item is disabled or enabled.
294
 
 *
295
 
 * Disabled items should never appear in search results.
296
 
 *
297
 
 * Returns: if the item is enabled
298
 
 **/
299
 
gboolean
300
 
hud_item_get_enabled (HudItem *item)
301
 
{
302
 
  return item->priv->enabled;
303
 
}
304
 
 
305
 
/**
306
 
 * hud_item_get_id:
307
 
 * @item: a #HudItem
308
 
 *
309
 
 * Gets the unique identifier of this #HudItem.
310
 
 *
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.
313
 
 *
314
 
 * Returns: the ID of the item
315
 
 **/
316
 
guint64
317
 
hud_item_get_id (HudItem *item)
318
 
{
319
 
  return item->priv->id;
320
 
}
321
 
 
322
 
/**
323
 
 * hud_item_lookup:
324
 
 * @id: an item identifier
325
 
 *
326
 
 * Looks up a #HudItem by its ID.
327
 
 *
328
 
 * The ID for a #HudItem can be queried with hud_item_get_id().
329
 
 *
330
 
 * Returns: (transfer none): the #HudItem with the given @id, or %NULL
331
 
 *   if none exists
332
 
 **/
333
 
HudItem *
334
 
hud_item_lookup (guint64 id)
335
 
{
336
 
  return g_hash_table_lookup (hud_item_table, &id);
337
 
}
338
 
 
339
 
HudTokenList *
340
 
hud_item_get_token_list (HudItem *item)
341
 
{
342
 
  return item->priv->token_list;
343
 
}