Merging upstream version 0.7.0 (Closes: #493243, #510888, #567617, #699414, #709777...
[debian/lxpanel.git] / plugins / netstatus / netstatus-dialog.c
1 /*
2 * Copyright (C) 2003 Sun Microsystems, Inc.
3 * Copyright (C) 2004 Red Hat Inc.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 * 02111-1307, USA.
19 *
20 * Authors:
21 * Mark McLoughlin <mark@skynet.ie>
22 */
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include <gtk/gtk.h>
29
30 #include <glib/gi18n.h>
31 #include "netstatus-dialog.h"
32 #include <libfm/fm-gtk.h>
33
34 #include <string.h>
35 /* #include <gconf/gconf-client.h> */
36
37 #include "netstatus-icon.h"
38 #include "netstatus-util.h"
39
40 #define NETWORK_CONFIG_TOOL_DIR "/apps/netstatus_applet"
41 #define NETWORK_CONFIG_TOOL_KEY NETWORK_CONFIG_TOOL_DIR "/config_tool"
42
43 #if 0 /* stripped-down version does nothing to configurators. */
44 static const char *network_config_tools[] = {
45 "network-admin --configure %i",
46 "redhat-config-network",
47 "system-control-network"
48 };
49 #endif
50
51 #define UNKNOWN_STR(t,s) G_STMT_START { \
52 if (!((t) = (s))) (t) = _("Unknown"); \
53 } G_STMT_END
54
55 typedef struct
56 {
57 GtkBuilder* builder;
58 GtkWidget *dialog;
59
60 NetstatusIface *iface;
61 NetstatusIcon *icon;
62
63 /* GConfClient *client; */
64 char *config_tool;
65 /* guint listener; */
66
67 GtkWidget *name;
68 GtkWidget *name_entry; /* child GtkEntry of name (GtkComboBoxEntry) */
69 GtkWidget *status;
70 GtkWidget *received;
71 GtkWidget *sent;
72 GtkWidget *signal_strength_frame;
73 GtkWidget *signal_strength_bar;
74 GtkWidget *signal_strength_label;
75 GtkWidget *configure_button;
76
77 GtkWidget *inet4_frame;
78 GtkWidget *inet4_table;
79 GtkWidget *inet4_addr;
80 GtkWidget *inet4_addr_title;
81 GtkWidget *inet4_dest;
82 GtkWidget *inet4_dest_title;
83 GtkWidget *inet4_bcast;
84 GtkWidget *inet4_bcast_title;
85 GtkWidget *inet4_mask;
86 GtkWidget *inet4_mask_title;
87
88 GtkWidget *dev_frame;
89 GtkWidget *dev_type;
90 GtkWidget *dev_addr;
91
92 guint iface_list_monitor;
93 int n_ifaces;
94 } NetstatusDialogData;
95
96
97 static void
98 netstatus_dialog_update_state (NetstatusDialogData *data)
99 {
100 NetstatusState state;
101
102 state = netstatus_iface_get_state (data->iface);
103 gtk_label_set_text (GTK_LABEL (data->status),
104 netstatus_get_state_string (state));
105 }
106
107 static void
108 netstatus_dialog_update_name (NetstatusDialogData *data)
109 {
110 const char *iface_name;
111 const char *text;
112
113 iface_name = netstatus_iface_get_name (data->iface);
114 if (!iface_name)
115 {
116 gtk_window_set_title (GTK_WINDOW (data->dialog),
117 _("Connection Properties"));
118 }
119 else
120 {
121 char *p;
122
123 p = g_strdup_printf (_("Connection Properties: %s"), iface_name);
124 gtk_window_set_title (GTK_WINDOW (data->dialog), p);
125 g_free (p);
126 }
127
128 UNKNOWN_STR (text, iface_name);
129 gtk_entry_set_text (GTK_ENTRY (data->name_entry), text);
130 }
131
132 static inline void
133 print_packets_string (GString *str,
134 gulong packets)
135 {
136 g_string_printf (str, ngettext ("%lu packet", "%lu packets", packets), packets);
137 }
138
139 static inline void
140 print_bytes_string (GString *str,
141 guint64 bytes)
142 {
143 if (bytes > 1 << 30)
144 {
145 bytes = (bytes * 10) / (1 << 30);
146
147 g_string_append_printf (str, " (%lld.%lld GiB)", bytes / 10, bytes % 10);
148 }
149 else if (bytes > 1 << 20)
150 {
151 bytes = (bytes * 10) / (1 << 20);
152
153 g_string_append_printf (str, " (%lld.%lld MiB)", bytes / 10, bytes % 10);
154 }
155 else if (bytes > 1 << 10)
156 {
157 bytes = (bytes * 10) / (1 << 10);
158
159 g_string_append_printf (str, " (%lld.%lld KiB)", bytes / 10, bytes % 10);
160 }
161 else if (bytes > 0)
162 {
163 g_string_append_printf (str, " (%lld B)", bytes);
164 }
165 }
166
167 static void
168 netstatus_dialog_update_activity (NetstatusDialogData *data)
169 {
170 NetstatusStats stats = { 0, };
171 GString *str;
172
173 netstatus_iface_get_statistics (data->iface, &stats);
174
175 str = g_string_new (NULL);
176
177 print_packets_string (str, stats.out_packets);
178 print_bytes_string (str, stats.out_bytes);
179 gtk_label_set_text (GTK_LABEL (data->sent), str->str);
180
181 print_packets_string (str, stats.in_packets);
182 print_bytes_string (str, stats.in_bytes);
183 gtk_label_set_text (GTK_LABEL (data->received), str->str);
184
185 g_string_free (str, TRUE);
186 }
187
188 static void
189 netstatus_dialog_update_signal_strength (NetstatusDialogData *data)
190 {
191 if (netstatus_iface_get_is_wireless (data->iface))
192 {
193 int signal_strength;
194 char *str;
195
196 gtk_widget_show (data->signal_strength_frame);
197
198 signal_strength = netstatus_iface_get_signal_strength (data->iface);
199
200 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (data->signal_strength_bar),
201 signal_strength == 0 ? 0.0 : signal_strength / 100.0);
202
203 str = g_strdup_printf ("%d%%\n", signal_strength);
204 gtk_label_set_text (GTK_LABEL (data->signal_strength_label), str);
205 g_free (str);
206 }
207 else
208 {
209 gtk_widget_hide (data->signal_strength_frame);
210 }
211 }
212
213 static void
214 netstatus_dialog_update_inet4_support (NetstatusDialogData *data)
215 {
216 char *addr = NULL;
217 char *dest = NULL;
218 char *bcast = NULL;
219 char *mask = NULL;
220
221 if (netstatus_iface_get_inet4_details (data->iface, &addr, &dest, &bcast, &mask))
222 {
223 gtk_widget_show (data->inet4_frame);
224
225 /* Address */
226 if (addr)
227 {
228 gtk_table_set_row_spacing (GTK_TABLE (data->inet4_table), 0, 6);
229 gtk_label_set_text (GTK_LABEL (data->inet4_addr), addr);
230 gtk_widget_show (data->inet4_addr);
231 gtk_widget_show (data->inet4_addr_title);
232 }
233 else
234 {
235 gtk_table_set_row_spacing (GTK_TABLE (data->inet4_table), 0, 0);
236 gtk_widget_hide (data->inet4_addr);
237 gtk_widget_hide (data->inet4_addr_title);
238 }
239
240 /* Destination */
241 if (dest)
242 {
243 gtk_table_set_row_spacing (GTK_TABLE (data->inet4_table), 1, 6);
244 gtk_label_set_text (GTK_LABEL (data->inet4_dest), dest);
245 gtk_widget_show (data->inet4_dest);
246 gtk_widget_show (data->inet4_dest_title);
247 }
248 else
249 {
250 gtk_table_set_row_spacing (GTK_TABLE (data->inet4_table), 1, 0);
251 gtk_widget_hide (data->inet4_dest);
252 gtk_widget_hide (data->inet4_dest_title);
253 }
254
255 /* Broadcast */
256 if (bcast)
257 {
258 gtk_table_set_row_spacing (GTK_TABLE (data->inet4_table), 2, 6);
259 gtk_label_set_text (GTK_LABEL (data->inet4_bcast), bcast);
260 gtk_widget_show (data->inet4_bcast);
261 gtk_widget_show (data->inet4_bcast_title);
262 }
263 else
264 {
265 gtk_table_set_row_spacing (GTK_TABLE (data->inet4_table), 2, 0);
266 gtk_widget_hide (data->inet4_bcast);
267 gtk_widget_hide (data->inet4_bcast_title);
268 }
269
270 /* Subnet Mask */
271 if (mask)
272 {
273 gtk_label_set_text (GTK_LABEL (data->inet4_mask), mask);
274 gtk_widget_show (data->inet4_mask);
275 gtk_widget_show (data->inet4_mask_title);
276 }
277 else
278 {
279 gtk_widget_hide (data->inet4_mask);
280 gtk_widget_hide (data->inet4_mask_title);
281 }
282
283 g_free (addr);
284 g_free (dest);
285 g_free (bcast);
286 g_free (mask);
287 }
288 else
289 {
290 gtk_widget_hide (data->inet4_frame);
291 }
292 }
293
294 static void
295 netstatus_dialog_update_device_support (NetstatusDialogData *data)
296 {
297 const char *type = NULL;
298 char *addr = NULL;
299
300 if (netstatus_iface_get_device_details (data->iface, &type, &addr))
301 {
302 g_assert (type != NULL);
303
304 gtk_widget_show (data->dev_frame);
305
306 /* Device type */
307 gtk_label_set_text (GTK_LABEL (data->dev_type), type);
308
309 /* Address */
310 if (addr)
311 gtk_label_set_text (GTK_LABEL (data->dev_addr), addr);
312 else
313 gtk_label_set_text (GTK_LABEL (data->dev_addr), _("Unknown"));
314
315 g_free (addr);
316 }
317 else
318 {
319 gtk_widget_hide (data->dev_frame);
320 }
321 }
322
323 static void
324 netstatus_dialog_iface_state_changed (NetstatusIface *iface,
325 GParamSpec *pspec,
326 NetstatusDialogData *data)
327 {
328 netstatus_dialog_update_state (data);
329 netstatus_dialog_update_inet4_support (data);
330 netstatus_dialog_update_device_support (data);
331 }
332
333 static void
334 netstatus_dialog_iface_stats_changed (NetstatusIface *iface,
335 GParamSpec *pspec,
336 NetstatusDialogData *data)
337 {
338 netstatus_dialog_update_activity (data);
339 }
340
341 static void
342 netstatus_dialog_iface_signal_strength_changed (NetstatusIface *iface,
343 GParamSpec *pspec,
344 NetstatusDialogData *data)
345 {
346 netstatus_dialog_update_signal_strength (data);
347 }
348
349 static void
350 netstatus_dialog_iface_name_changed (NetstatusIface *iface,
351 GParamSpec *pspec,
352 NetstatusDialogData *data)
353 {
354 netstatus_dialog_update_name (data);
355 netstatus_dialog_update_signal_strength (data);
356 }
357
358 static void
359 netstatus_dialog_set_iface_name (NetstatusDialogData *data,
360 GtkEntry *entry)
361 {
362 const char *iface_name;
363
364 iface_name = gtk_entry_get_text (entry);
365 if (iface_name && iface_name [0])
366 {
367 netstatus_iface_set_name (data->iface, iface_name);
368
369 gtk_widget_set_sensitive (data->configure_button,
370 !netstatus_iface_get_is_loopback (data->iface));
371 }
372 }
373
374 static void
375 netstatus_dialog_response (GtkWidget *dialog,
376 int response)
377 {
378 switch (response)
379 {
380 case GTK_RESPONSE_HELP:
381 {
382 GError *error = NULL;
383 GdkScreen *screen;
384
385 screen = gtk_window_get_screen (GTK_WINDOW (dialog));
386
387 // gnome_help_display_on_screen ("gnome-netstatus", "gnome-netstatus-props", screen, &error);
388
389 if (error)
390 {
391 GtkWidget *message_dialog;
392
393 message_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
394 GTK_DIALOG_DESTROY_WITH_PARENT,
395 GTK_MESSAGE_ERROR,
396 GTK_BUTTONS_CLOSE,
397 _("There was an error displaying help:\n%s"),
398 error->message);
399
400 gtk_window_set_resizable (GTK_WINDOW (message_dialog), FALSE);
401 gtk_window_set_screen (GTK_WINDOW (message_dialog), screen);
402
403 g_signal_connect (message_dialog, "response",
404 G_CALLBACK (gtk_widget_destroy),
405 NULL);
406
407 gtk_widget_show (message_dialog);
408
409 g_error_free (error);
410 }
411 }
412 break;
413 default:
414 gtk_widget_hide (dialog);
415 break;
416 }
417 }
418
419 static void
420 netstatus_dialog_destroy (GtkWidget *dialog)
421 {
422 NetstatusDialogData *data;
423
424 data = g_object_get_data (G_OBJECT (dialog), "netstatus-dialog-data");
425 if (!data)
426 return;
427
428 if (data->config_tool)
429 g_free (data->config_tool);
430 data->config_tool = NULL;
431
432 /*
433 if (data->listener)
434 {
435 g_assert (data->client != NULL);
436
437 gconf_client_notify_remove (data->client, data->listener);
438 data->listener = 0;
439
440 gconf_client_remove_dir (data->client, NETWORK_CONFIG_TOOL_DIR, NULL);
441 }
442
443 if (data->client)
444 g_object_unref (data->client);
445 data->client = NULL;
446 */
447
448 if (data->iface_list_monitor)
449 g_source_remove (data->iface_list_monitor);
450 data->iface_list_monitor = 0;
451
452 if (data->iface)
453 g_object_unref (data->iface);
454 data->iface = NULL;
455
456 g_free (data);
457 }
458
459 static gboolean
460 netstatus_dialog_check_config_tool (NetstatusDialogData *dialog_data,
461 const char *config_tool)
462 {
463 char **argv = NULL;
464 gboolean found = FALSE;
465
466 if (config_tool && g_shell_parse_argv (config_tool, NULL, &argv, NULL))
467 {
468 char *path;
469
470 g_assert (argv != NULL);
471
472 if ((path = g_find_program_in_path (argv [0])))
473 {
474 if (dialog_data->config_tool)
475 g_free (dialog_data->config_tool);
476 dialog_data->config_tool = g_strdup (config_tool);
477 found = TRUE;
478 }
479
480 g_free (path);
481 g_strfreev (argv);
482 }
483
484 return found;
485 }
486
487 /*
488 static void
489 netstatus_dialog_config_tool_notify (GConfClient *client,
490 guint cnx_id,
491 GConfEntry *entry,
492 NetstatusDialogData *dialog_data)
493 {
494 if (!entry->value || !entry->value->type == GCONF_VALUE_STRING)
495 return;
496
497 netstatus_dialog_check_config_tool (dialog_data,
498 gconf_value_get_string (entry->value));
499 }
500 */
501
502 static void
503 netstatus_dialog_detect_configuration_tool (NetstatusDialogData *dialog_data)
504 {
505 /*
506 char *config_tool;
507 int i;
508
509 dialog_data->client = gconf_client_get_default ();
510
511 gconf_client_add_dir (dialog_data->client,
512 NETWORK_CONFIG_TOOL_DIR,
513 GCONF_CLIENT_PRELOAD_NONE,
514 NULL);
515
516 dialog_data->listener =
517 gconf_client_notify_add (dialog_data->client,
518 NETWORK_CONFIG_TOOL_KEY,
519 (GConfClientNotifyFunc) netstatus_dialog_config_tool_notify,
520 dialog_data, NULL, NULL);
521
522 config_tool = gconf_client_get_string (dialog_data->client,
523 NETWORK_CONFIG_TOOL_KEY,
524 NULL);
525 if (netstatus_dialog_check_config_tool (dialog_data, config_tool))
526 {
527 g_free (config_tool);
528 return;
529 }
530
531 for (i = 0; i < G_N_ELEMENTS (network_config_tools); i++)
532 {
533 if (netstatus_dialog_check_config_tool (dialog_data, network_config_tools [i]))
534 break;
535 }
536 */
537 }
538
539 static void
540 netstatus_iface_configure (GtkWidget *configure_button,
541 NetstatusDialogData *dialog_data)
542 {
543 GString *command;
544 char **argv = NULL;
545 int i;
546
547 g_return_if_fail (dialog_data != NULL);
548 g_return_if_fail (dialog_data->config_tool != NULL);
549
550 g_shell_parse_argv (dialog_data->config_tool, NULL, &argv, NULL);
551
552 /* We've already verified config_tool as valid */
553 g_assert (argv != NULL);
554
555 command = g_string_new (argv [0]);
556
557 for (i = 1; argv [i]; i++)
558 {
559 command = g_string_append_c (command, ' ');
560
561 if (!strcmp (argv [i], "%i"))
562 command = g_string_append (command,
563 netstatus_iface_get_name (dialog_data->iface));
564 else
565 command = g_string_append (command, argv [i]);
566 }
567
568 fm_launch_command_simple(GTK_WINDOW (dialog_data->dialog), NULL,
569 G_APP_INFO_CREATE_NONE, command->str, NULL);
570
571 g_string_free (command, TRUE);
572 g_strfreev (argv);
573 }
574
575 static void
576 netstatus_dialog_setup_configure_button (NetstatusDialogData *data)
577 {
578 data->configure_button = (GtkWidget*)gtk_builder_get_object(data->builder, "configure_button");
579
580 g_signal_connect (data->configure_button, "clicked",
581 G_CALLBACK (netstatus_iface_configure), data);
582
583 netstatus_dialog_detect_configuration_tool (data);
584
585 if (!data->config_tool)
586 gtk_widget_hide (data->configure_button);
587
588 gtk_widget_set_sensitive (data->configure_button,
589 !netstatus_iface_get_is_loopback (data->iface));
590 }
591
592 static void
593 netstatus_dialog_setup_connection (NetstatusDialogData *data)
594 {
595 GtkWidget *hbox;
596 GtkWidget *icon;
597 GtkListStore* model;
598
599 hbox = (GtkWidget*)gtk_builder_get_object(data->builder, "connection_hbox");
600
601 icon = netstatus_icon_new (data->iface);
602 netstatus_icon_set_tooltips_enabled (NETSTATUS_ICON (icon), FALSE);
603 netstatus_icon_set_show_signal (NETSTATUS_ICON (icon), FALSE);
604 gtk_box_pack_end (GTK_BOX (hbox), icon, FALSE, TRUE, 4);
605 gtk_widget_show (icon);
606
607 data->icon = NETSTATUS_ICON (icon);
608
609 data->name = (GtkWidget*)gtk_builder_get_object(data->builder, "name_combo");
610 data->name_entry = gtk_bin_get_child((GtkBin*)data->name);
611 model = gtk_list_store_new(1, G_TYPE_STRING);
612 gtk_combo_box_set_model(GTK_COMBO_BOX(data->name), GTK_TREE_MODEL(model));
613 #if GTK_CHECK_VERSION(2, 24, 0)
614 gtk_combo_box_set_entry_text_column(GTK_COMBO_BOX(data->name), 0);
615 #else
616 gtk_combo_box_entry_set_text_column(GTK_COMBO_BOX_ENTRY(data->name), 0);
617 #endif
618 g_object_unref(model);
619
620 data->status = (GtkWidget*)gtk_builder_get_object(data->builder, "status_label");
621
622 netstatus_dialog_update_name (data);
623 netstatus_dialog_update_state (data);
624 }
625
626 static void
627 netstatus_dialog_setup_activity (NetstatusDialogData *data)
628 {
629 data->sent = (GtkWidget*)gtk_builder_get_object(data->builder, "sent_label");
630 data->received = (GtkWidget*)gtk_builder_get_object(data->builder, "received_label");
631
632 netstatus_dialog_update_activity (data);
633 }
634
635 static void
636 netstatus_dialog_setup_signal_strength_details (NetstatusDialogData *data)
637 {
638 data->signal_strength_frame = (GtkWidget*)gtk_builder_get_object(data->builder, "signal_strength_frame");
639 data->signal_strength_bar = (GtkWidget*)gtk_builder_get_object(data->builder, "signal_strength_bar");
640 data->signal_strength_label = (GtkWidget*)gtk_builder_get_object(data->builder, "signal_strength_label");
641
642 netstatus_dialog_update_signal_strength (data);
643 }
644
645 static void
646 netstatus_dialog_setup_inet4_support (NetstatusDialogData *data)
647 {
648 data->inet4_frame = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_frame");
649 data->inet4_table = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_table");
650 data->inet4_addr = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_addr_label");
651 data->inet4_addr_title = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_addr_title");
652 data->inet4_dest = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_dest_label");
653 data->inet4_dest_title = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_dest_title");
654 data->inet4_bcast = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_bcast_label");
655 data->inet4_bcast_title = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_bcast_title");
656 data->inet4_mask = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_mask_label");
657 data->inet4_mask_title = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_mask_title");
658
659 netstatus_dialog_update_inet4_support (data);
660 }
661 static void
662 netstatus_dialog_setup_device_support (NetstatusDialogData *data)
663 {
664 data->dev_frame = (GtkWidget*)gtk_builder_get_object(data->builder, "dev_frame");
665 data->dev_type = (GtkWidget*)gtk_builder_get_object(data->builder, "dev_type_label");
666 data->dev_addr = (GtkWidget*)gtk_builder_get_object(data->builder, "dev_addr_label");
667
668 netstatus_dialog_update_device_support (data);
669 }
670
671 static void
672 netstatus_dialog_set_icon (GtkWidget *dialog)
673 {
674 GtkIconTheme *icon_theme;
675 GtkIconInfo *icon_info;
676
677 icon_theme = gtk_icon_theme_get_for_screen (gtk_window_get_screen (GTK_WINDOW (dialog)));
678
679 if ((icon_info = gtk_icon_theme_lookup_icon (icon_theme, "gnome-netstatus-tx", 48, 0)))
680 {
681 gtk_window_set_icon_from_file (GTK_WINDOW (dialog),
682 gtk_icon_info_get_filename (icon_info),
683 NULL);
684 gtk_icon_info_free (icon_info);
685 }
686 }
687
688 static gboolean
689 _netstatus_dialog_iface_list_monitor (NetstatusDialogData *data)
690 {
691 GList *iface_names, *l;
692 int n_ifaces;
693 GtkListStore* model;
694
695 iface_names = netstatus_list_interface_names (NULL);
696
697 n_ifaces = g_list_length (iface_names);
698
699 if (data->n_ifaces != n_ifaces)
700 {
701 model = (GtkListStore*)gtk_combo_box_get_model(GTK_COMBO_BOX(data->name));
702 gtk_list_store_clear(model);
703 g_signal_handlers_block_by_func (data->name_entry,
704 G_CALLBACK (netstatus_dialog_set_iface_name), data);
705
706 for (l = iface_names; l; l = l->next)
707 {
708 GtkTreeIter it;
709 gtk_list_store_append(model, &it);
710 gtk_list_store_set(model, &it, 0, (char*)l->data, -1);
711 }
712 /* GtkCombo is deprecated. */
713 /* gtk_combo_set_popdown_strings (GTK_COMBO (data->name), iface_names); */
714 netstatus_dialog_update_name (data);
715
716 g_signal_handlers_unblock_by_func (data->name_entry,
717 G_CALLBACK (netstatus_dialog_set_iface_name), data);
718 }
719
720 data->n_ifaces = n_ifaces;
721
722 for (l = iface_names; l; l = l->next)
723 g_free (l->data);
724 g_list_free (iface_names);
725
726 return TRUE;
727 }
728
729 static gboolean
730 netstatus_dialog_iface_list_monitor (gpointer data)
731 {
732 if (g_source_is_destroyed(g_main_current_source()))
733 return FALSE;
734 return _netstatus_dialog_iface_list_monitor (data);
735 }
736
737 GtkWidget *
738 netstatus_dialog_new (NetstatusIface *iface)
739 {
740 NetstatusDialogData *data;
741 data = g_new0 (NetstatusDialogData, 1);
742
743 data->builder = gtk_builder_new();
744 gtk_builder_add_from_file(data->builder, PACKAGE_UI_DIR "/netstatus.ui", NULL);
745 data->dialog = (GtkWidget*)gtk_builder_get_object(data->builder, "network_status_dialog");
746
747 g_object_set_data (G_OBJECT (data->dialog), "netstatus-dialog-data", data);
748
749 netstatus_dialog_set_icon (data->dialog);
750
751 data->iface = g_object_ref (iface);
752 netstatus_connect_signal_while_alive (data->iface,
753 "notify::state",
754 G_CALLBACK (netstatus_dialog_iface_state_changed),
755 data,
756 data->dialog);
757
758 netstatus_connect_signal_while_alive (data->iface,
759 "notify::stats",
760 G_CALLBACK (netstatus_dialog_iface_stats_changed),
761 data,
762 data->dialog);
763
764 netstatus_connect_signal_while_alive (data->iface,
765 "notify::name",
766 G_CALLBACK (netstatus_dialog_iface_name_changed),
767 data,
768 data->dialog);
769
770 netstatus_connect_signal_while_alive (data->iface,
771 "notify::wireless",
772 G_CALLBACK (netstatus_dialog_iface_signal_strength_changed),
773 data,
774 data->dialog);
775
776 netstatus_connect_signal_while_alive (data->iface,
777 "notify::signal-strength",
778 G_CALLBACK (netstatus_dialog_iface_signal_strength_changed),
779 data,
780 data->dialog);
781
782 g_signal_connect (data->dialog, "response",
783 G_CALLBACK (netstatus_dialog_response), NULL);
784
785 g_signal_connect (data->dialog, "destroy",
786 G_CALLBACK (netstatus_dialog_destroy), NULL);
787
788 netstatus_dialog_setup_connection (data);
789 netstatus_dialog_setup_activity (data);
790 netstatus_dialog_setup_signal_strength_details (data);
791 netstatus_dialog_setup_inet4_support (data);
792 netstatus_dialog_setup_device_support (data);
793 netstatus_dialog_setup_configure_button (data);
794
795 data->iface_list_monitor = g_timeout_add (2 * 1000,
796 netstatus_dialog_iface_list_monitor,
797 data);
798 _netstatus_dialog_iface_list_monitor (data);
799
800 g_signal_connect_swapped (data->name_entry, "changed",
801 G_CALLBACK (netstatus_dialog_set_iface_name),
802 data);
803
804 g_object_unref(data->builder);
805 data->builder = NULL;
806
807 return data->dialog;
808 }
809
810 /* 2006.10.08 Add by Hong Jen Yee (PCMan) to be used in lxpanel plugin */
811 void netstatus_dialog_set_configuration_tool( GtkWidget* dialog, const char* tool )
812 {
813 NetstatusDialogData *data;
814 data = g_object_get_data (G_OBJECT (dialog), "netstatus-dialog-data");
815
816 netstatus_dialog_check_config_tool( data, tool );
817
818 if (data->config_tool)
819 gtk_widget_show (data->configure_button);
820 else
821 gtk_widget_hide (data->configure_button);
822 }
823
824 const char* netstatus_dialog_get_configuration_tool( GtkWidget* dialog )
825 {
826 NetstatusDialogData *data;
827 data = g_object_get_data (G_OBJECT (dialog), "netstatus-dialog-data");
828 return data->config_tool;
829 }
830
831 /* 2009.05.10 Add by Hong Jen Yee (PCMan) to be used in lxpanel plugin */
832 const char* netstatus_dialog_get_iface_name( GtkWidget* dialog )
833 {
834 NetstatusDialogData *data;
835 data = g_object_get_data (G_OBJECT (dialog), "netstatus-dialog-data");
836 return netstatus_iface_get_name (data->iface);
837 }