Debian Codegraph @

src/gtktechnology.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 <gtktechnology.h>
23
24 #include <connman-ui-gtk.h>
25
26 struct _GtkTechnologyPrivate {
27 GtkBox *box;
28
29 GtkSwitch *enabler;
30 GtkLabel *name;
31
32 GtkCheckMenuItem *tethering;
33 };
34
35 static void gtk_technology_destroy(GtkWidget *widget);
36 static void gtk_technology_class_init(GtkTechnologyClass *klass);
37 static void gtk_technology_init(GtkTechnology *technology);
38 static void gtk_technology_dispose(GObject *object);
39 static gboolean gtk_technology_button_release_event(GtkWidget *widget,
40 GdkEventButton *event);
41
42 G_DEFINE_TYPE(GtkTechnology, gtk_technology, GTK_TYPE_MENU_ITEM);
43
44 static void gtk_technology_class_init(GtkTechnologyClass *klass)
45 {
46 GObjectClass *object_class;
47 GtkWidgetClass *widget_class;
48 GtkMenuItemClass *menu_item_class;
49
50 object_class = G_OBJECT_CLASS(klass);
51 widget_class = GTK_WIDGET_CLASS(klass);
52 menu_item_class = GTK_MENU_ITEM_CLASS(klass);
53
54 object_class->dispose = gtk_technology_dispose;
55
56 widget_class->destroy = gtk_technology_destroy;
57 widget_class->button_release_event =
58 gtk_technology_button_release_event;
59
60 menu_item_class->hide_on_activate = FALSE;
61
62 g_type_class_add_private(object_class,
63 sizeof(GtkTechnologyPrivate));
64 }
65
66 static void gtk_technology_init(GtkTechnology *technology)
67 {
68 GtkTechnologyPrivate *priv;
69
70 priv = G_TYPE_INSTANCE_GET_PRIVATE(technology,
71 GTK_TYPE_TECHNOLOGY,
72 GtkTechnologyPrivate);
73 technology->priv = priv;
74
75 priv->box = (GtkBox *) gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
76 priv->enabler = (GtkSwitch *) gtk_switch_new();
77 priv->name = (GtkLabel *) gtk_label_new(NULL);
78
79
80 gtk_widget_set_margin_start((GtkWidget *)priv->enabler, 0);
81 gtk_widget_set_margin_end((GtkWidget *)priv->enabler, 0);
82 gtk_widget_set_margin_top((GtkWidget *)priv->enabler, 0);
83 gtk_widget_set_margin_bottom((GtkWidget *)priv->enabler, 0);
84
85 gtk_widget_set_margin_start((GtkWidget *)priv->name, 0);
86 gtk_widget_set_margin_end((GtkWidget *)priv->name, 0);
87 gtk_widget_set_margin_top((GtkWidget *)priv->name, 0);
88 gtk_widget_set_margin_bottom((GtkWidget *)priv->name, 0);
89
90 gtk_box_set_spacing(priv->box, 0);
91 gtk_box_set_homogeneous(priv->box, TRUE);
92
93 //gtk_widget_set_halign((GtkWidget *)priv->box, GTK_ALIGN_START);
94 gtk_widget_set_halign((GtkWidget *)priv->name, GTK_ALIGN_START);
95 //gtk_widget_set_halign((GtkWidget *)technology, GTK_ALIGN_START);
96
97 gtk_box_pack_start(priv->box,
98 (GtkWidget *)priv->enabler, FALSE, FALSE, 0);
99 gtk_box_pack_start(priv->box,
100 (GtkWidget *)priv->name, FALSE, FALSE, 0);
101
102 gtk_widget_set_visible((GtkWidget *)priv->box, TRUE);
103 gtk_widget_set_visible((GtkWidget *)priv->enabler, TRUE);
104 gtk_widget_set_visible((GtkWidget *)priv->name, TRUE);
105
106 gtk_container_add(GTK_CONTAINER(technology), (GtkWidget *)priv->box);
107
108 gtk_widget_set_can_focus((GtkWidget *)priv->box, TRUE);
109 gtk_widget_set_can_focus((GtkWidget *)priv->enabler, TRUE);
110 }
111
112 static void gtk_technology_dispose(GObject *object)
113 {
114 (*G_OBJECT_CLASS(gtk_technology_parent_class)->dispose)(object);
115 }
116
117 static void gtk_technology_destroy(GtkWidget *widget)
118 {
119 GtkTechnology *technology = GTK_TECHNOLOGY(widget);
120 GtkTechnologyPrivate *priv = technology->priv;
121
122 connman_technology_set_property_changed_callback(technology->path,
123 NULL, technology);
124 connman_technology_set_property_error_callback(technology->path,
125 NULL, technology);
126
127 if (priv->tethering != NULL) {
128 gtk_widget_destroy((GtkWidget *)priv->tethering);
129 priv->tethering = NULL;
130 }
131
132 GTK_WIDGET_CLASS(gtk_technology_parent_class)->destroy(widget);
133 }
134
135 static gboolean gtk_technology_button_release_event(GtkWidget *widget,
136 GdkEventButton *event)
137 {
138 GtkTechnology *technology = GTK_TECHNOLOGY(widget);
139 GtkTechnologyPrivate *priv = technology->priv;
140 gboolean enable;
141
142 if (event->button != 1 && event->button != 3)
143 return TRUE;
144
145 if (event->button == 3) {
146 GtkWidget *parent;
147 const char *type;
148
149 type = connman_technology_get_type(technology->path);
150 if (g_strcmp0(type, "wifi") == 0)
151 cui_agent_set_wifi_tethering_settings(technology->path,
152 FALSE);
153 parent = gtk_widget_get_parent (widget);
154 if (parent != NULL && GTK_IS_MENU_SHELL(parent) == TRUE) {
155 GtkMenuShell *menu_shell = GTK_MENU_SHELL(parent);
156 gtk_menu_shell_activate_item(menu_shell, widget, TRUE);
157 }
158
159 return TRUE;
160 }
161
162 enable = !gtk_switch_get_active(priv->enabler);
163
164 if (connman_technology_enable(technology->path, enable) == 0)
165 gtk_widget_set_sensitive((GtkWidget *)priv->enabler, FALSE);
166
167 return TRUE;
168 }
169
170 static void technology_property_error_cb(const char *path,
171 const char *property, int error, void *user_data)
172 {
173 if (error != 0)
174 printf("Could not set property %s\n", property);
175 }
176
177 static gboolean gtk_technology_tethering_button(GtkWidget *widget,
178 GdkEventButton *event, gpointer user_data)
179 {
180 GtkTechnology *technology = user_data;
181 GtkTechnologyPrivate *priv = technology->priv;
182 gboolean tethering;
183
184 if (event->button != 1)
185 return TRUE;
186
187 gtk_widget_set_sensitive((GtkWidget *)priv->tethering, FALSE);
188
189 tethering = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
190 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), !tethering);
191
192 connman_technology_tether(technology->path, !tethering);
193
194 return TRUE;
195 }
196
197 static void set_technology_name(GtkTechnology *technology, gboolean enabled)
198 {
199 const char *name = connman_technology_get_name(technology->path);
200
201 if (enabled == TRUE) {
202 char *markup;
203
204 markup = g_markup_printf_escaped("<b>%s</b>", name);
205 gtk_label_set_markup(technology->priv->name, markup);
206 g_free(markup);
207 } else
208 gtk_label_set_text(technology->priv->name, name);
209 }
210
211 static void technology_property_changed_cb(const char *path,
212 const char *property, void *user_data)
213 {
214 GtkTechnology *technology = user_data;
215 GtkTechnologyPrivate *priv = technology->priv;
216
217 if (g_strcmp0(property, "Powered") == 0) {
218 gboolean enabled;
219
220 enabled = connman_technology_is_enabled(path);
221
222 gtk_switch_set_active(priv->enabler, enabled);
223 gtk_widget_set_sensitive((GtkWidget *)priv->enabler, TRUE);
224
225 gtk_widget_set_sensitive((GtkWidget *)priv->tethering,
226 enabled);
227
228 set_technology_name(technology, enabled);
229 } else if (g_strcmp0(property, "Tethering") == 0) {
230 gtk_check_menu_item_set_active(priv->tethering,
231 connman_technology_is_tethering(path));
232 gtk_widget_set_sensitive((GtkWidget *)priv->tethering,
233 connman_technology_is_enabled(path));
234 }
235 }
236
237 GtkTechnology *gtk_technology_new(const gchar *path)
238 {
239 GtkTechnology *technology;
240 GtkTechnologyPrivate *priv;
241 gchar *path_copy, *label;
242 gboolean enabled;
243
244 if (path == NULL)
245 return NULL;
246
247 path_copy = g_strdup(path);
248 if (path_copy == NULL)
249 return NULL;
250
251 technology = g_object_new(GTK_TYPE_TECHNOLOGY, NULL);
252 if (technology == NULL) {
253 g_free(path_copy);
254 return NULL;
255 }
256
257 priv = technology->priv;
258 technology->path = path_copy;
259
260 connman_technology_set_property_changed_callback(path_copy,
261 technology_property_changed_cb,
262 technology);
263 connman_technology_set_property_error_callback(technology->path,
264 technology_property_error_cb, technology);
265
266 if (g_strcmp0(connman_technology_get_type(path_copy), "wifi") == 0) {
267 gtk_widget_set_tooltip_text(GTK_WIDGET(technology),
268 _("Left click to enable/disable\n"
269 "Right click to set tethering information"));
270 } else
271 gtk_widget_set_tooltip_text(GTK_WIDGET(technology),
272 _("Left to enable/disable"));
273
274 enabled = connman_technology_is_enabled(path_copy);
275 gtk_switch_set_active(priv->enabler, enabled);
276
277 label = g_strdup_printf("via: %s",
278 connman_technology_get_name(path_copy));
279
280 priv->tethering = (GtkCheckMenuItem *)
281 gtk_check_menu_item_new_with_label(label);
282 if (enabled == FALSE)
283 gtk_widget_set_sensitive((GtkWidget *)priv->tethering, FALSE);
284
285 set_technology_name(technology, enabled);
286
287 gtk_check_menu_item_set_active(priv->tethering,
288 connman_technology_is_tethering(path_copy));
289
290 gtk_widget_set_visible((GtkWidget *)priv->tethering, TRUE);
291
292 g_signal_connect(priv->tethering, "button-release-event",
293 G_CALLBACK(gtk_technology_tethering_button),
294 technology);
295
296 return technology;
297 }
298
299 GtkMenuItem *gtk_technology_get_tethering_item(GtkTechnology *technology)
300 {
301 GtkTechnologyPrivate *priv = technology->priv;
302
303 return GTK_MENU_ITEM(priv->tethering);
304 }