Debian Codegraph @

src/status.c

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * ConnMan GTK GUI
3 *
4 * Copyright (C) 2015 Intel Corporation. All rights reserved.
5 * Author: Jaakko Hannikainen <[email protected]>
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 as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <gtk/gtk.h>
22 #include <glib/gi18n.h>
23
24 #include "config.h"
25 #include "configurator.h"
26 #include "connection.h"
27 #include "main.h"
28 #include "status.h"
29 #include "service.h"
30 #include "technology.h"
31
32 #ifdef USE_STATUS_ICON
33 GtkStatusIcon *icon;
34
35 static void status_activate(gpointer *ignored, gpointer user_data)
36 {
37 g_signal_emit_by_name(user_data, "activate");
38 }
39
40 static void status_exit(gpointer *ignored, gpointer user_data)
41 {
42 g_signal_emit_by_name(main_window, "destroy");
43 }
44
45 static void status_toggle_connection(gpointer *ignored, gpointer user_data)
46 {
47 service_toggle_connection(user_data);
48 }
49
50 static GtkWidget *create_service_item(struct service *serv)
51 {
52 gchar *name, *state, *state_r, *label;
53 GtkWidget *item, *child;
54
55 name = service_get_property_string(serv, "Name", NULL);
56 state = service_get_property_string(serv, "State", NULL);
57 state_r = service_get_property_string_raw(serv, "State", NULL);
58
59 /* Todo: is autoupdate needed here? */
60 if(strcmp(state_r, "idle"))
61 label = g_markup_printf_escaped("<b>%s - %s</b>", name, state);
62 else
63 label = g_markup_printf_escaped("%s - %s", name, state);
64
65 item = gtk_menu_item_new_with_label(NULL);
66 child = gtk_bin_get_child(GTK_BIN(item));
67 gtk_label_set_markup(GTK_LABEL(child), label);
68 g_signal_connect(item, "activate", G_CALLBACK(status_toggle_connection),
69 serv);
70
71 g_free(name);
72 g_free(state);
73 g_free(state_r);
74 g_free(label);
75
76 return item;
77 }
78
79 static void status_menu(GtkStatusIcon *icon, guint button, guint activate_time,
80 gpointer user_data)
81 {
82 GtkMenu *menu;
83 GtkWidget *open, *exit;
84 int index;
85
86 menu = GTK_MENU(gtk_menu_new());
87
88 open = gtk_menu_item_new_with_label(_("Open app"));
89 exit = gtk_menu_item_new_with_label(_("Exit"));
90 g_signal_connect(open, "activate", G_CALLBACK(status_activate),
91 user_data);
92 g_signal_connect(exit, "activate", G_CALLBACK(status_exit), user_data);
93 gtk_container_add(GTK_CONTAINER(menu), open);
94 gtk_container_add(GTK_CONTAINER(menu), gtk_separator_menu_item_new());
95
96 for(index = CONNECTION_TYPE_ETHERNET; index < CONNECTION_TYPE_COUNT; index++) {
97 const gchar *label;
98 struct technology *tech;
99 GHashTableIter iter;
100 gpointer key, service;
101 GtkMenuItem *item;
102 GtkMenu *submenu;
103 gboolean has_items = FALSE;
104
105 tech = technologies[index];
106 if(!tech)
107 continue;
108 if(!technology_get_property_bool(tech, "Powered"))
109 continue;
110 submenu = GTK_MENU(gtk_menu_new());
111 label = translated_tech_name(tech->type);
112 item = GTK_MENU_ITEM(gtk_menu_item_new_with_label(label));
113
114 g_hash_table_iter_init(&iter, tech->services);
115 while(g_hash_table_iter_next(&iter, &key, &service)) {
116 GtkWidget *item = create_service_item(service);
117 gtk_container_add(GTK_CONTAINER(submenu), item);
118 has_items = TRUE;
119 }
120
121 gtk_menu_item_set_submenu(item, GTK_WIDGET(submenu));
122 gtk_container_add(GTK_CONTAINER(menu), GTK_WIDGET(item));
•123 gtk_widget_set_sensitive(GTK_WIDGET(item), has_items);
124
125 }
126
127 gtk_container_add(GTK_CONTAINER(menu), gtk_separator_menu_item_new());
128 gtk_container_add(GTK_CONTAINER(menu), exit);
129 gtk_widget_show_all(GTK_WIDGET(menu));
130 gtk_menu_popup(menu, NULL, NULL, NULL, NULL, button, activate_time);
131 }
132
133 void status_update(void) {
134 int best_status = 0;
135 int index;
136
137 if(!status_icon_enabled)
138 return;
139
140 for(index = CONNECTION_TYPE_ETHERNET; index < CONNECTION_TYPE_COUNT; index++) {
141 struct technology *tech;
142 GHashTableIter iter;
143 gpointer key, service;
144
145 tech = technologies[index];
146 if(!tech)
147 continue;
148
149 g_hash_table_iter_init(&iter, tech->services);
150 while(g_hash_table_iter_next(&iter, &key, &service)) {
151 gchar *state;
152 state = service_get_property_string_raw(service, "State",
153 NULL);
154 switch(best_status) {
155 case 0:
156 if(!strcmp(state, "association"))
157 best_status = 1;
158 case 1:
159 if(!strcmp(state, "configuration"))
160 best_status = 2;
161 case 2:
162 if(!strcmp(state, "ready"))
163 best_status = 3;
164 case 3:
165 if(!strcmp(state, "online"))
166 best_status = 4;
167 }
168
169 g_free(state);
170
171 if(best_status == 4)
172 break;
173 }
174 }
175
176 switch(best_status) {
177 case 0:
178 gtk_status_icon_set_from_icon_name(icon, "network-offline");
179 break;
180 case 1:
181 case 2:
182 gtk_status_icon_set_from_icon_name(icon, "preferences-system-network");
183 break;
184 case 3:
185 case 4:
186 gtk_status_icon_set_from_icon_name(icon, "network-transmit-receive");
187 break;
188 }
189 }
190
191 void status_init(GtkApplication *app)
192 {
193 if(!status_icon_enabled)
194 return;
195
196 icon = gtk_status_icon_new_from_icon_name("preferences-system-network");
197 g_signal_connect(icon, "activate", G_CALLBACK(status_activate), app);
198 g_signal_connect(icon, "popup-menu", G_CALLBACK(status_menu), app);
199
200 status_update();
201 }
202 #else
203 void status_init(GtkApplication *app)
204 {
205 }
206 void status_update(void)
207 {
208 }
209 #endif /* USE_STATUS_ICON */