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/>.
17
#define G_LOG_DOMAIN "hudrandomsource"
23
#include "hudsource.h"
24
#include "hudrandomsource.h"
25
#include "word-list.h"
31
/* Longest word in the word-list (upper bound) */
32
#define MAX_LETTERS 20
34
struct _HudRandomSource
36
GObject parent_instance;
38
gchar *application_id;
41
/* instance members */
44
/* Max nested depth of menu items */
47
/* Max number of items per submenu */
50
/* Max number of words per label.
51
* NB: keep MAX_WORDS * MAX_DEPTH under 32
56
typedef GObjectClass HudRandomSourceClass;
58
static void hud_random_source_iface_init (HudSourceInterface *iface);
59
G_DEFINE_TYPE_WITH_CODE (HudRandomSource, hud_random_source, G_TYPE_OBJECT,
60
G_IMPLEMENT_INTERFACE (HUD_TYPE_SOURCE, hud_random_source_iface_init))
63
hud_random_source_search (HudSource *hud_source,
64
HudTokenList *search_tokens,
65
void (*append_func) (HudResult * result, gpointer user_data),
68
HudRandomSource *source = (HudRandomSource *) hud_source;
72
g_hash_table_iter_init (&iter, source->items);
73
while (g_hash_table_iter_next (&iter, &item, NULL))
77
result = hud_result_get_if_matched (item, search_tokens, 0);
79
append_func(result, user_data);
84
hud_random_source_list_applications (HudSource *hud_source,
85
HudTokenList *search_tokens,
86
void (*append_func) (const gchar *application_id, const gchar *application_icon, HudSourceItemType type, gpointer user_data),
89
HudRandomSource *source = (HudRandomSource *) hud_source;
93
g_hash_table_iter_init (&iter, source->items);
94
while (g_hash_table_iter_next (&iter, &item, NULL))
98
result = hud_result_get_if_matched (item, search_tokens, 0);
101
append_func (source->application_id, source->app_icon, HUD_SOURCE_ITEM_TYPE_BACKGROUND_APP, user_data);
102
g_object_unref(result);
109
hud_random_source_get (HudSource *hud_source,
110
const gchar *application_id)
112
HudRandomSource *source = (HudRandomSource *) hud_source;
113
if (g_strcmp0(source->application_id, application_id) == 0)
119
hud_random_source_ignore_use (HudSource *source)
124
hud_random_source_make_word (GRand *rand,
130
choice = g_rand_int_range (rand, 0, G_N_ELEMENTS (word_list));
131
word = word_list[choice];
140
hud_random_source_make_words (GRand *rand,
147
buffer = g_malloc ((MAX_LETTERS + 1) * n_words);
150
for (i = 0; i < n_words; i++)
155
ptr = hud_random_source_make_word (rand, ptr);
163
static HudStringList *
164
hud_random_source_make_name (HudRandomSource *self,
166
HudStringList *context)
171
label = hud_random_source_make_words (rand, g_rand_int_range (rand, 1, self->max_words + 1));
172
name = hud_string_list_cons (label, context);
179
hud_random_source_populate_table (HudRandomSource *self,
181
HudStringList *context,
187
n_items = g_rand_int_range (rand, 1, self->max_items + 1);
189
for (i = 0; i < n_items; i++)
195
name = hud_random_source_make_name (self, rand, context);
197
if (depth != self->max_depth)
198
/* Decrease the chances of a particular item being a submenu as we
199
* go deeper into the menu structure.
201
is_submenu = g_rand_int_range (rand, 0, depth + 1) == 0;
203
/* At the maximum depth, prevent any items from being submenus. */
206
item = hud_item_new (name, name, "", self->application_id, self->app_icon, NULL, !is_submenu);
207
g_hash_table_add (self->items, item);
210
hud_random_source_populate_table (self, rand, name, depth + 1);
212
hud_string_list_unref (name);
217
hud_random_source_finalize (GObject *object)
219
HudRandomSource *source = (HudRandomSource *) object;
221
g_hash_table_unref (source->items);
223
G_OBJECT_CLASS (hud_random_source_parent_class)
228
hud_random_source_init (HudRandomSource *source)
230
source->items = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
234
hud_random_source_iface_init (HudSourceInterface *iface)
236
iface->use = hud_random_source_ignore_use;
237
iface->unuse = hud_random_source_ignore_use;
238
iface->search = hud_random_source_search;
239
iface->list_applications = hud_random_source_list_applications;
240
iface->get = hud_random_source_get;
244
hud_random_source_class_init (HudRandomSourceClass *class)
246
class->finalize = hud_random_source_finalize;
250
hud_random_source_new (GRand *rand)
252
return hud_random_source_new_full (rand, MAX_DEPTH, MAX_ITEMS, MAX_WORDS, "random_source", "random_icon");
256
hud_random_source_new_full (GRand *rand, const gint max_depth, const gint max_items,
257
const gint max_words, const gchar *application_id, const gchar *app_icon)
259
HudRandomSource *source;
261
source = g_object_new (HUD_TYPE_RANDOM_SOURCE, NULL);
263
source->max_depth = max_depth;
264
source->max_items = max_items;
265
source->max_words = max_words;
266
source->application_id = g_strdup(application_id);
267
source->app_icon = g_strdup(app_icon);
269
hud_random_source_populate_table (source, rand, NULL, 0);
271
return HUD_SOURCE (source);