~jbicha/hud/build-depend-on-valac-not-gir

« back to all changes in this revision

Viewing changes to tests/hudrandomsource.c

  • Committer: Tarmac
  • Author(s): Ted Gould, Pete Woods, Antti Kaijanmäki, Ted Gould, Albert Astals, Ryan Lortie, Łukasz 'sil2100' Zemczak, Albert Astals Cid, Mathieu Trudel-Lapierre, Kaleo, Tarmac, Ricardo Salveti de Araujo, Michael Terry, Automatic PS uploader
  • Date: 2013-04-10 16:04:51 UTC
  • mfrom: (227.3.148 phablet)
  • Revision ID: tarmac-20130410160451-o3owpv3zaxulm5of
HUD 2.0 Merge.

Approved by PS Jenkins bot, Mathieu Trudel-Lapierre.

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
 
 
17
#define G_LOG_DOMAIN "hudrandomsource"
 
18
 
 
19
#include <glib.h>
 
20
 
 
21
#include "hudquery.h"
 
22
#include "hudtoken.h"
 
23
#include "hudsource.h"
 
24
#include "hudrandomsource.h"
 
25
#include "word-list.h"
 
26
 
 
27
#define MAX_DEPTH 6
 
28
#define MAX_ITEMS 20
 
29
#define MAX_WORDS 4
 
30
 
 
31
/* Longest word in the word-list (upper bound) */
 
32
#define MAX_LETTERS 20
 
33
 
 
34
struct _HudRandomSource
 
35
{
 
36
  GObject parent_instance;
 
37
 
 
38
  gchar *application_id;
 
39
  gchar *app_icon;
 
40
 
 
41
  /* instance members */
 
42
  GHashTable *items;
 
43
 
 
44
  /* Max nested depth of menu items */
 
45
  gint max_depth;
 
46
 
 
47
  /* Max number of items per submenu */
 
48
  gint max_items;
 
49
 
 
50
  /* Max number of words per label.
 
51
   * NB: keep MAX_WORDS * MAX_DEPTH under 32
 
52
   */
 
53
  gint max_words;
 
54
};
 
55
 
 
56
typedef GObjectClass HudRandomSourceClass;
 
57
 
 
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))
 
61
 
 
62
void
 
63
hud_random_source_search (HudSource    *hud_source,
 
64
    HudTokenList *search_tokens,
 
65
    void        (*append_func) (HudResult * result, gpointer user_data),
 
66
    gpointer      user_data)
 
67
{
 
68
  HudRandomSource *source = (HudRandomSource *) hud_source;
 
69
  GHashTableIter iter;
 
70
  gpointer item;
 
71
 
 
72
  g_hash_table_iter_init (&iter, source->items);
 
73
  while (g_hash_table_iter_next (&iter, &item, NULL))
 
74
    {
 
75
      HudResult *result;
 
76
 
 
77
      result = hud_result_get_if_matched (item, search_tokens, 0);
 
78
      if (result)
 
79
        append_func(result, user_data);
 
80
    }
 
81
}
 
82
 
 
83
void
 
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),
 
87
    gpointer      user_data)
 
88
{
 
89
  HudRandomSource *source = (HudRandomSource *) hud_source;
 
90
  GHashTableIter iter;
 
91
  gpointer item;
 
92
 
 
93
  g_hash_table_iter_init (&iter, source->items);
 
94
  while (g_hash_table_iter_next (&iter, &item, NULL))
 
95
    {
 
96
      HudResult *result;
 
97
 
 
98
      result = hud_result_get_if_matched (item, search_tokens, 0);
 
99
      if (result)
 
100
        {
 
101
          append_func (source->application_id, source->app_icon, HUD_SOURCE_ITEM_TYPE_BACKGROUND_APP, user_data);
 
102
          g_object_unref(result);
 
103
          break;
 
104
        }
 
105
    }
 
106
}
 
107
 
 
108
static HudSource *
 
109
hud_random_source_get (HudSource    *hud_source,
 
110
                       const gchar *application_id)
 
111
{
 
112
  HudRandomSource *source = (HudRandomSource *) hud_source;
 
113
  if (g_strcmp0(source->application_id, application_id) == 0)
 
114
    return hud_source;
 
115
  return NULL;
 
116
}
 
117
 
 
118
static void
 
119
hud_random_source_ignore_use (HudSource *source)
 
120
{
 
121
}
 
122
 
 
123
static gchar *
 
124
hud_random_source_make_word (GRand *rand,
 
125
           gchar *buffer)
 
126
{
 
127
  const gchar *word;
 
128
  gint choice;
 
129
 
 
130
  choice = g_rand_int_range (rand, 0, G_N_ELEMENTS (word_list));
 
131
  word = word_list[choice];
 
132
 
 
133
  while (*word)
 
134
    *buffer++ = *word++;
 
135
 
 
136
  return buffer;
 
137
}
 
138
 
 
139
gchar *
 
140
hud_random_source_make_words (GRand *rand,
 
141
            gint   n_words)
 
142
{
 
143
  gchar *buffer;
 
144
  gchar *ptr;
 
145
  gint i;
 
146
 
 
147
  buffer = g_malloc ((MAX_LETTERS + 1) * n_words);
 
148
 
 
149
  ptr = buffer;
 
150
  for (i = 0; i < n_words; i++)
 
151
    {
 
152
      if (i)
 
153
        *ptr++ = ' ';
 
154
 
 
155
      ptr = hud_random_source_make_word (rand, ptr);
 
156
    }
 
157
 
 
158
  *ptr = '\0';
 
159
 
 
160
  return buffer;
 
161
}
 
162
 
 
163
static HudStringList *
 
164
hud_random_source_make_name (HudRandomSource *self,
 
165
                         GRand         *rand,
 
166
                         HudStringList *context)
 
167
{
 
168
  HudStringList *name;
 
169
  gchar *label;
 
170
 
 
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);
 
173
  g_free (label);
 
174
 
 
175
  return name;
 
176
}
 
177
 
 
178
static void
 
179
hud_random_source_populate_table (HudRandomSource *self,
 
180
                              GRand         *rand,
 
181
                              HudStringList *context,
 
182
                              gint           depth)
 
183
{
 
184
  gint n_items;
 
185
  gint i;
 
186
 
 
187
  n_items = g_rand_int_range (rand, 1, self->max_items + 1);
 
188
 
 
189
  for (i = 0; i < n_items; i++)
 
190
    {
 
191
      HudStringList *name;
 
192
      gboolean is_submenu;
 
193
      HudItem *item;
 
194
 
 
195
      name = hud_random_source_make_name (self, rand, context);
 
196
 
 
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.
 
200
         */
 
201
        is_submenu = g_rand_int_range (rand, 0, depth + 1) == 0;
 
202
      else
 
203
        /* At the maximum depth, prevent any items from being submenus. */
 
204
        is_submenu = FALSE;
 
205
 
 
206
      item = hud_item_new (name, name, "", self->application_id, self->app_icon, NULL, !is_submenu);
 
207
      g_hash_table_add (self->items, item);
 
208
 
 
209
      if (is_submenu)
 
210
        hud_random_source_populate_table (self, rand, name, depth + 1);
 
211
 
 
212
      hud_string_list_unref (name);
 
213
    }
 
214
}
 
215
 
 
216
static void
 
217
hud_random_source_finalize (GObject *object)
 
218
{
 
219
  HudRandomSource *source = (HudRandomSource *) object;
 
220
 
 
221
  g_hash_table_unref (source->items);
 
222
 
 
223
  G_OBJECT_CLASS (hud_random_source_parent_class)
 
224
    ->finalize (object);
 
225
}
 
226
 
 
227
static void
 
228
hud_random_source_init (HudRandomSource *source)
 
229
{
 
230
  source->items = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
 
231
}
 
232
 
 
233
static void
 
234
hud_random_source_iface_init (HudSourceInterface *iface)
 
235
{
 
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;
 
241
}
 
242
 
 
243
static void
 
244
hud_random_source_class_init (HudRandomSourceClass *class)
 
245
{
 
246
  class->finalize = hud_random_source_finalize;
 
247
}
 
248
 
 
249
HudSource *
 
250
hud_random_source_new (GRand *rand)
 
251
{
 
252
  return hud_random_source_new_full (rand, MAX_DEPTH, MAX_ITEMS, MAX_WORDS, "random_source", "random_icon");
 
253
}
 
254
 
 
255
HudSource *
 
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)
 
258
{
 
259
  HudRandomSource *source;
 
260
 
 
261
  source = g_object_new (HUD_TYPE_RANDOM_SOURCE, NULL);
 
262
 
 
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);
 
268
 
 
269
  hud_random_source_populate_table (source, rand, NULL, 0);
 
270
 
 
271
  return HUD_SOURCE (source);
 
272
}