aboutsummaryrefslogtreecommitdiff
path: root/tools/indicator-loader.c
blob: 5659dea3719e7751368cd535a8840ed9ee0322e7 (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
/*
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>

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 <libindicator/indicator-object.h>

#if GTK_MAJOR_VERSION == 3
#include <libindicator/indicator-ng.h>
#endif

static gchar * profile = "desktop";
static gchar * title = NULL;

static GOptionEntry entries[] =
{
  { "profile", 'p', 0, G_OPTION_ARG_STRING, &profile, "Profile [default: 'desktop']", NULL },
  { NULL }
};

static GHashTable * entry_to_menuitem = NULL;

#define ENTRY_DATA_NAME "indicator-custom-entry-data"

static void
activate_entry (GtkWidget * widget, gpointer user_data)
{
	g_return_if_fail(INDICATOR_IS_OBJECT(user_data));
	gpointer entry = g_object_get_data(G_OBJECT(widget), ENTRY_DATA_NAME);
	if (entry == NULL) {
		g_debug("Activation on: (null)");
	}

	indicator_object_entry_activate(INDICATOR_OBJECT(user_data), (IndicatorObjectEntry *)entry, gtk_get_current_event_time());
	return;
}

static GtkWidget*
create_menu_item (IndicatorObjectEntry * entry)
{
	GtkWidget * hbox;
	GtkWidget * menuitem;

	menuitem = 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 (entry->image != NULL) {
		gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(entry->image), FALSE, FALSE, 0);
	}
	if (entry->label != NULL) {
		gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(entry->label), FALSE, FALSE, 0);
	}
	gtk_container_add(GTK_CONTAINER(menuitem), hbox);
	gtk_widget_show(hbox);

	if (entry->menu != NULL) {
		gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), GTK_WIDGET(entry->menu));
	}

	return menuitem;
}

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

	g_debug("Signal: Entry Added");

	if (entry->parent_object == NULL) {
		g_warning("Entry '%p' does not have a parent object", entry);
	}

	menuitem = g_hash_table_lookup (entry_to_menuitem, entry);
	if (menuitem == NULL) {
		g_debug ("This is the first time this entry's been added -- creating a new menuitem for it");
		menuitem = create_menu_item (entry);
		g_hash_table_insert (entry_to_menuitem, entry, menuitem);

		g_object_set_data(G_OBJECT(menuitem), ENTRY_DATA_NAME, entry);
		g_signal_connect (G_OBJECT(menuitem), "activate", G_CALLBACK(activate_entry), io);

		gtk_menu_shell_append (GTK_MENU_SHELL(user_data), menuitem);

	}
	gtk_widget_show (menuitem);

	return;
}

static void 
entry_removed (IndicatorObject * io, IndicatorObjectEntry * entry, gpointer user_data)
{
	g_debug("Signal: Entry Removed");

	GtkWidget * menuitem = g_hash_table_lookup (entry_to_menuitem, entry);
	if (menuitem != NULL)
		gtk_widget_hide (menuitem);

	return;
}

static void
menu_show (IndicatorObject * io, IndicatorObjectEntry * entry, guint timestamp, gpointer user_data)
{
	if (entry != NULL) {
		g_debug("Show Menu: %s", entry->label != NULL ? gtk_label_get_text(entry->label) : "No Label");
	} else {
		g_debug("Show Menu: (null)");
	}
	return;
}

static gboolean
load_module (const gchar * name, GtkWidget * menu)
{
	g_debug("Looking at Module: %s", name);
	g_return_val_if_fail(name != NULL, FALSE);

	g_debug("Loading Module: %s", name);

	/* Build the object for the module */
	IndicatorObject *io;
	if (g_str_has_suffix(name, G_MODULE_SUFFIX)) {
		io = indicator_object_new_from_file(name);
	}
#if GTK_MAJOR_VERSION == 3
	else if (g_str_has_suffix(name, ".indicator")) {
		GError *error = NULL;

		io = INDICATOR_OBJECT (indicator_ng_new_for_profile (name, profile, &error));
		if (!io) {
			g_warning ("could not load indicator from '%s': %s", name, error->message);
			g_error_free (error);
			return FALSE;
		}

		title = g_strdup_printf ("%s %s", profile, name);
	}
#endif
	else
		return FALSE;

	/* Connect to it's signals */
	g_signal_connect(G_OBJECT(io), INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED,   G_CALLBACK(entry_added),    menu);
	g_signal_connect(G_OBJECT(io), INDICATOR_OBJECT_SIGNAL_ENTRY_REMOVED, G_CALLBACK(entry_removed),  menu);
	g_signal_connect(G_OBJECT(io), INDICATOR_OBJECT_SIGNAL_MENU_SHOW,     G_CALLBACK(menu_show),      NULL);

	/* Work on the entries */
	GList * entries = indicator_object_get_entries(io);
	GList * entry = NULL;

	for (entry = entries; entry != NULL; entry = g_list_next(entry)) {
		IndicatorObjectEntry * entrydata = (IndicatorObjectEntry *)entry->data;
		entry_added(io, entrydata, menu);
	}

	g_list_free(entries);

	return TRUE;
}

static void
destroy (gpointer data)
{
	gtk_main_quit();
	return;
}

int
main (int argc, char ** argv)
{
	/* Parse the command line options */
	GError * error = NULL;
	GOptionContext * context;
	context = g_option_context_new ("/path/to/file.indicator");
	g_option_context_add_main_entries (context, entries, NULL);
	g_option_context_add_group (context, gtk_get_option_group (TRUE));
	if (!g_option_context_parse (context, &argc, &argv, &error)) {
		g_print ("option parsing failed: %s\n", error->message);
		g_error_free (error);
		return 1;
	}

	/* Make sure we don't proxy to ourselves */
	g_unsetenv("UBUNTU_MENUPROXY");

	gtk_init(&argc, &argv);

	entry_to_menuitem = g_hash_table_new (g_direct_hash, g_direct_equal);

	if (argc != 2) {
		g_error("Need filename");
		return 1;
	}

	GtkWidget * menubar = gtk_menu_bar_new();
	if (!load_module(argv[1], menubar)) {
		g_error("Unable to load module");
		return 1;
	}

	GtkWidget * window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	if (title != NULL)
		gtk_window_set_title (GTK_WINDOW(window), title);
	g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);

	gtk_container_add(GTK_CONTAINER(window), menubar);

	gtk_widget_show(menubar);
	gtk_widget_show(window);

	gtk_main();

	/* cleanup */
	g_hash_table_destroy (entry_to_menuitem);
	g_free (title);
	g_option_context_free (context);
	return 0;
}