aboutsummaryrefslogtreecommitdiff
path: root/tools/indicator-loader.c
blob: ad4fdf4337b87ebd8d0b0c8e17df0f5728617e26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 * A small test loader for loading indicators in test suites
 * and during development of them.
 *
 * Copyright 2009 Canonical Ltd.
 *
 * Authors:
 *   Ted Gould <ted@canonical.com>
 *   Lars Uebernickel <lars.uebernickel@canonical.com>
 *   Charles Kerr <charles.kerr@canonical.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3.0 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License version 3.0 for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library. If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <gtk/gtk.h>
#include <libayatana-ido/libayatana-ido.h>
#include "src/indicator-object.h"
#if GTK_CHECK_VERSION (3,0,0)
 #include "src/indicator-ng.h"
#endif

static GHashTable * entry_to_menu_item = NULL;

G_DEFINE_QUARK (indicator_loader, entry_data)

static void
activate_entry (GtkWidget * widget, gpointer user_data)
{
  gpointer entry;

  g_return_if_fail (INDICATOR_IS_OBJECT(user_data));

  entry = g_object_get_qdata (G_OBJECT(widget), entry_data_quark());

  if (entry == NULL)
    {
      g_debug("Activation on: (null)");
    }
  else
    {
      indicator_object_entry_activate (INDICATOR_OBJECT(user_data),
                                       entry,
                                       gtk_get_current_event_time());
    }
}

static void
scroll_entry (GtkWidget *widget, GdkEventScroll* event, gpointer user_data)
{
  gpointer entry;

  g_return_if_fail (INDICATOR_IS_OBJECT(user_data));

  entry = g_object_get_qdata (G_OBJECT(widget), entry_data_quark());
  IndicatorScrollDirection direction = G_MAXINT;

  switch (event->direction)
    {
      case GDK_SCROLL_UP:
        direction = INDICATOR_OBJECT_SCROLL_UP;
        break;
      case GDK_SCROLL_DOWN:
        direction = INDICATOR_OBJECT_SCROLL_DOWN;
        break;
      case GDK_SCROLL_LEFT:
        direction = INDICATOR_OBJECT_SCROLL_LEFT;
        break;
      case GDK_SCROLL_RIGHT:
        direction = INDICATOR_OBJECT_SCROLL_RIGHT;
        break;
      default:
        break;
    }

  if (entry == NULL)
    {
      g_debug("Scroll on: (null)");
    }
  else if (direction == G_MAXINT)
    {
      g_debug("Scroll direction not supported");
    }
  else
    {
      g_signal_emit_by_name(INDICATOR_OBJECT(user_data),
                            INDICATOR_OBJECT_SIGNAL_ENTRY_SCROLLED,
                            entry, 1, direction);
    }
}

static GtkWidget*
create_menu_item (IndicatorObjectEntry * entry)
{
  GtkWidget * menu_item;
  GtkWidget * hbox;
  gpointer w;

  menu_item = gtk_menu_item_new();

#if GTK_CHECK_VERSION (3,0,0)
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 3);
#else
  hbox = gtk_hbox_new (FALSE, 3);
#endif

  if ((w = entry->image))
    gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET(w), FALSE, FALSE, 0);
  if ((w = entry->label))
    gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET(w), FALSE, FALSE, 0);

  gtk_container_add (GTK_CONTAINER(menu_item), hbox);
  gtk_widget_show (hbox);

  if ((w = entry->menu))
    gtk_menu_item_set_submenu (GTK_MENU_ITEM(menu_item), GTK_WIDGET(w));

  return menu_item;
}

static void
entry_added (IndicatorObject      * io,
             IndicatorObjectEntry * entry,
             gpointer               user_data)
{
  GtkWidget * menu_item;

  g_debug ("Signal: Entry Added");

  g_warn_if_fail (entry->parent_object != NULL);

  menu_item = g_hash_table_lookup (entry_to_menu_item, entry);

  if (menu_item == NULL)
    {
      g_debug ("creating a menuitem for new entry %p", entry);
      menu_item = create_menu_item (entry);
      g_hash_table_insert (entry_to_menu_item, entry, menu_item);

      g_object_set_qdata (G_OBJECT(menu_item), entry_data_quark(), entry);
      g_signal_connect (menu_item, "activate", G_CALLBACK(activate_entry), io);

      gtk_widget_set_events (menu_item, gtk_widget_get_events (menu_item) | GDK_SCROLL_MASK);
      g_signal_connect (menu_item, "scroll-event", G_CALLBACK(scroll_entry), io);

      gtk_menu_shell_append (GTK_MENU_SHELL(user_data), menu_item);
    }

  gtk_widget_show (menu_item);
}

static void
entry_removed (__attribute__((unused)) IndicatorObject      * io,
               IndicatorObjectEntry * entry,
               __attribute__((unused)) gpointer               user_data)
{
  GtkWidget * w;

  g_debug ("Signal: Entry Removed");

  if ((w = g_hash_table_lookup (entry_to_menu_item, entry)))
    gtk_widget_hide (w);
}

static void
menu_show (__attribute__((unused)) IndicatorObject      * io,
           IndicatorObjectEntry * entry,
           __attribute__((unused)) guint                  timestamp,
           __attribute__((unused)) gpointer               user_data)
{
  const char * text;

  if (entry == NULL)
    text = "(null)";
  else if (entry->label == NULL)
    text = "(no label)";
  else
    text = gtk_label_get_text (entry->label);

  g_debug ("Show Menu: %s", text);
}

/***
****
***/

static IndicatorObject *
load_module (const gchar * file_name)
{
  IndicatorObject * io = NULL;

  if (file_name && g_str_has_suffix (file_name, G_MODULE_SUFFIX))
    {
      io = indicator_object_new_from_file (file_name);

      if (io == NULL)
        g_warning ("could not load indicator from '%s'", file_name);
    }

  return io;
}

static IndicatorObject *
load_profile (const char * file_name, const char * profile)
{
  IndicatorObject * io = NULL;

#if GTK_CHECK_VERSION (3,0,0)

  GError * error = NULL;

  io = INDICATOR_OBJECT (indicator_ng_new_for_profile (file_name,
                                                       profile,
                                                       &error));
  if (error != NULL)
    {
      g_warning ("couldn't load profile '%s' from '%s': %s",
                 profile, file_name, error->message);
      g_error_free (error);
    }

#endif

  return io;
}

/***
****
***/

static void
add_indicator_to_menu (GtkMenuShell * menu_shell, IndicatorObject * io)
{
  GList * entries;
  GList * entry;

  g_return_if_fail (INDICATOR_IS_OBJECT (io));

  /* connect to its signals */
  g_signal_connect (io, INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED,
                    G_CALLBACK(entry_added), menu_shell);
  g_signal_connect (io, INDICATOR_OBJECT_SIGNAL_ENTRY_REMOVED,
                    G_CALLBACK(entry_removed),  menu_shell);
  g_signal_connect (io, INDICATOR_OBJECT_SIGNAL_MENU_SHOW,
                    G_CALLBACK(menu_show), NULL);

  /* process the entries */
  entries = indicator_object_get_entries(io);
  for (entry=entries; entry!=NULL; entry=entry->next)
    entry_added (io, entry->data, menu_shell);
  g_list_free (entries);
}

static void
add_menu_to_grid (GtkGrid    * grid,
                  int          top,
                  const char * text_,
                  GtkWidget  * menu)
{
  gchar * text;
  GtkWidget * label;

  text = g_strdup_printf ("%s:", text_);
  label = gtk_label_new (text);
  g_free (text);

  gtk_grid_attach (GTK_GRID(grid), label, 0, top, 1, 1);
  gtk_grid_attach (GTK_GRID(grid), menu,  1, top, 1, 1);

  g_object_set (label, "halign", GTK_ALIGN_START,
                       "hexpand", FALSE,
                       "margin-right", 6,
                       "valign", GTK_ALIGN_CENTER,
                       NULL);

  g_object_set (menu, "halign", GTK_ALIGN_START,
                      "hexpand", TRUE,
                      NULL);
}

/***
****
***/

int
main (int argc, char ** argv)
{
  int menu_count = 0;
  const gchar * file_name;
  gchar * base_name;
  GtkWidget * grid;

  if (argc != 2)
    {
      base_name = g_path_get_basename (argv[0]);
      g_warning ("Use: %s filename", base_name);
      g_free (base_name);
      return 0;
    }

  /* make sure we don't proxy to ourselves */
  g_setenv ("UBUNTU_MENUPROXY", "0", TRUE);

  gtk_init (&argc, &argv);
  ido_init ();

  entry_to_menu_item = g_hash_table_new (g_direct_hash, g_direct_equal);

  file_name = argv[1];

  grid = g_object_new (GTK_TYPE_GRID, "margin", 4,
                                      "column-spacing", 6,
                                      "row-spacing", 12,
                                      NULL);

  /* if it's an old-style indicator... */
  if (g_str_has_suffix (file_name, G_MODULE_SUFFIX))
    {
      IndicatorObject * io = load_module (file_name);
      GtkWidget * menu = gtk_menu_bar_new ();
      add_indicator_to_menu (GTK_MENU_SHELL(menu), io);
      base_name = g_path_get_basename (file_name);
      add_menu_to_grid (GTK_GRID(grid), menu_count++, base_name, menu);
      g_free (base_name);
    }
  else /* treat it as a GMenu indicator's keyfile */
    {
      GError * error;
      GKeyFile * key_file;

      key_file = g_key_file_new ();
      error = NULL;
      g_key_file_load_from_file (key_file, file_name, G_KEY_FILE_NONE, &error);
      if (error != NULL)
        {
          g_warning ("loading '%s' failed: %s", file_name, error->message);
          g_error_free (error);
        }
      else
        {
          gchar ** groups;
          int i;

          groups = g_key_file_get_groups (key_file, NULL);
          for (i=0; groups && groups[i]; i++)
            {
              const gchar * const profile = groups[i];
              IndicatorObject * io;

              if (!g_strcmp0 (profile, "Indicator Service"))
                continue;

              if ((io = load_profile (file_name, profile)))
                {
                  GtkWidget * menu = gtk_menu_bar_new ();
                  add_indicator_to_menu (GTK_MENU_SHELL(menu), io);
                  add_menu_to_grid (GTK_GRID(grid), menu_count++, profile, menu);
                }
            }

          g_strfreev (groups);
        }

      g_key_file_free (key_file);
    }

  if (menu_count > 0)
    {
      GtkWidget * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      base_name = g_path_get_basename (file_name);
      gtk_window_set_title (GTK_WINDOW(window), base_name);
      g_free (base_name);
      g_signal_connect (window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
      gtk_container_add (GTK_CONTAINER(window), grid);
      gtk_widget_show_all (window);
      gtk_main ();
    }

  /* cleanup */
  g_hash_table_destroy (entry_to_menu_item);
  return 0;
}