Debian Codegraph @

src/right-menu.c

raw symbols SCIP
1 /*
2 *
3 * Connection Manager UI
4 *
5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include <connman-ui-gtk.h>
23 #include <gtktechnology.h>
24
25 #define CUI_RIGHT_MENU_UI_PATH CUI_UI_PATH "/right_menu.ui"
26
27 static GtkMenu *cui_right_menu = NULL;
28 static GtkWidget *cui_item_mode_off = NULL;
29 static GtkWidget *cui_item_mode_on = NULL;
30 static GtkMenu *cui_tethering_menu = NULL;
31 static GHashTable *tech_items = NULL;
32 static int item_position = 3;
33 static gboolean disabled = TRUE;
34
35 static void add_technology(const char *path)
36 {
37 GtkTechnology *t;
38
39 t = gtk_technology_new(path);
40 gtk_menu_shell_insert(GTK_MENU_SHELL(cui_right_menu),
41 (GtkWidget *)t, item_position);
42 item_position++;
43
44 gtk_widget_set_visible((GtkWidget *)t, TRUE);
45 gtk_widget_show((GtkWidget *)t);
46
47 gtk_menu_shell_append(GTK_MENU_SHELL(cui_tethering_menu),
48 (GtkWidget *)gtk_technology_get_tethering_item(t));
49
50 g_hash_table_insert(tech_items, t->path, t);
51 }
52
53 static void technology_added_cb(const char *path)
54 {
55 add_technology(path);
56 }
57
58 static void technology_removed_cb(const char *path)
59 {
60 g_hash_table_remove(tech_items, path);
61 }
62
63 static void delete_technology_item(gpointer data)
64 {
65 GtkWidget *item = data;
66
67 gtk_widget_destroy(item);
68 item_position--;
69 }
70
71 static void cui_item_offlinemode_activate(GtkMenuItem *menuitem,
72 gpointer user_data)
73 {
74 gboolean offlinemode;
75
76 offlinemode = !connman_manager_get_offlinemode();
77
78 connman_manager_set_offlinemode(offlinemode);
79 }
80
81 static void cui_item_quit_activate(GtkMenuItem *menuitem,
82 gpointer user_data)
83 {
84 gtk_main_quit();
85 }
86
87 static void cui_popup_right_menu(GtkStatusIcon *trayicon,
88 guint button,
89 guint activate_time,
90 gpointer user_data)
91 {
92 GList *tech_list, *list;
93
94 if (disabled == TRUE)
95 goto popup;
96
97 if (connman_manager_get_offlinemode()) {
98 gtk_widget_show(cui_item_mode_on);
99 gtk_widget_hide(cui_item_mode_off);
100 } else {
101 gtk_widget_show(cui_item_mode_off);
102 gtk_widget_hide(cui_item_mode_on);
103 }
104
105 tech_list = connman_technology_get_technologies();
106 if (tech_list == NULL)
107 goto popup;
108
109 for (list = tech_list; list != NULL; list = list->next)
110 add_technology((const char *)list->data);
111
112 g_list_free(tech_list);
113
114 popup:
115 connman_technology_set_removed_callback(technology_removed_cb);
116 connman_technology_set_added_callback(technology_added_cb);
117
118 gtk_menu_popup(cui_right_menu, NULL, NULL,
119 NULL, NULL, button, activate_time);
120 }
121
122 static void cui_popdown_right_menu(GtkMenu *menu, gpointer user_data)
123 {
124 connman_technology_set_removed_callback(NULL);
125 connman_technology_set_added_callback(NULL);
126
127 g_hash_table_remove_all(tech_items);
128 }
129
130 static void cui_enable_item(gboolean enable)
131 {
132 set_widget_hidden(cui_builder, "cui_item_offlinemode_on", enable);
133 set_widget_hidden(cui_builder, "cui_item_offlinemode_off", enable);
134 set_widget_hidden(cui_builder, "cui_sep_technology_up", enable);
135 set_widget_hidden(cui_builder, "cui_sep_technology_down", enable);
136 set_widget_hidden(cui_builder, "cui_item_tethering", enable);
137 set_widget_hidden(cui_builder, "cui_sep_tethering", enable);
138 set_widget_hidden(cui_builder, "cui_item_quit", FALSE);
139
140 disabled = enable;
141 }
142
143 void cui_right_menu_enable_only_quit(void)
144 {
145 cui_enable_item(TRUE);
146 }
147
148 void cui_right_menu_enable_all(void)
149 {
150 cui_enable_item(FALSE);
151 }
152
153 gint cui_load_right_menu(GtkBuilder *builder, GtkStatusIcon *trayicon)
154 {
155 GtkImageMenuItem *cui_item_tethering;
156 GtkMenuItem *cui_item_quit;
157 GdkPixbuf *image = NULL;
158 GError *error = NULL;
159
160 gtk_builder_add_from_file(builder, CUI_RIGHT_MENU_UI_PATH, &error);
161 if (error != NULL) {
162 printf("Error: %s\n", error->message);
163 g_error_free(error);
164
165 return -EINVAL;
166 }
167
168 cui_builder = builder;
169
170 cui_right_menu = (GtkMenu *) gtk_builder_get_object(builder,
171 "cui_right_menu");
172 cui_item_quit = (GtkMenuItem *) gtk_builder_get_object(builder,
173 "cui_item_quit");
174 cui_item_mode_off = (GtkWidget *) gtk_builder_get_object(builder,
175 "cui_item_offlinemode_off");
176 cui_item_mode_on = (GtkWidget *) gtk_builder_get_object(builder,
177 "cui_item_offlinemode_on");
178 cui_tethering_menu = (GtkMenu *) gtk_builder_get_object(builder,
179 "cui_tethering_menu");
180
181 g_signal_connect(cui_right_menu, "deactivate",
182 G_CALLBACK(cui_popdown_right_menu), NULL);
183 g_signal_connect(cui_item_quit, "activate",
184 G_CALLBACK(cui_item_quit_activate), NULL);
185 g_signal_connect(cui_item_mode_off, "activate",
186 G_CALLBACK(cui_item_offlinemode_activate), NULL);
187 g_signal_connect(cui_item_mode_on, "activate",
188 G_CALLBACK(cui_item_offlinemode_activate), NULL);
189
190 tech_items = g_hash_table_new_full(g_str_hash, g_str_equal,
191 NULL, delete_technology_item);
192
193 cui_item_tethering = (GtkImageMenuItem *) gtk_builder_get_object(
194 builder, "cui_item_tethering");
195
196 cui_theme_get_tethering_icone_and_info(&image, NULL);
197 if (image != NULL)
198 gtk_image_menu_item_set_image(cui_item_tethering,
199 gtk_image_new_from_pixbuf(image));
200
201 cui_tray_hook_right_menu(cui_popup_right_menu);
202
203 return 0;
204 }