Adding upstream version 0.8.0.
[debian/lxpanel.git] / plugins / netstatus / netstatus-dialog.c
CommitLineData
6cc5e1a6
DB
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
2ba86315 30#include <glib/gi18n.h>
6cc5e1a6 31#include "netstatus-dialog.h"
6b775dbb 32#include <libfm/fm-gtk.h>
6cc5e1a6
DB
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. */
44static 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
55typedef struct
56{
2ba86315 57 GtkBuilder* builder;
6cc5e1a6
DB
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;
2ba86315 68 GtkWidget *name_entry; /* child GtkEntry of name (GtkComboBoxEntry) */
6cc5e1a6
DB
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
97static void
98netstatus_dialog_update_state (NetstatusDialogData *data)
99{
100 NetstatusState state;
6b775dbb 101
6cc5e1a6
DB
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
107static void
108netstatus_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 }
6b775dbb 127
6cc5e1a6 128 UNKNOWN_STR (text, iface_name);
2ba86315 129 gtk_entry_set_text (GTK_ENTRY (data->name_entry), text);
6cc5e1a6
DB
130}
131
132static inline void
133print_packets_string (GString *str,
134 gulong packets)
135{
136 g_string_printf (str, ngettext ("%lu packet", "%lu packets", packets), packets);
137}
138
139static inline void
140print_bytes_string (GString *str,
141 guint64 bytes)
142{
143 if (bytes > 1 << 30)
144 {
145 bytes = (bytes * 10) / (1 << 30);
146
19ab5cea
AG
147 g_string_append_printf (str, " (%"G_GUINT64_FORMAT".%"G_GUINT64_FORMAT" GiB)",
148 bytes / 10, bytes % 10);
6cc5e1a6
DB
149 }
150 else if (bytes > 1 << 20)
151 {
152 bytes = (bytes * 10) / (1 << 20);
153
19ab5cea
AG
154 g_string_append_printf (str, " (%"G_GUINT64_FORMAT".%"G_GUINT64_FORMAT" MiB)",
155 bytes / 10, bytes % 10);
6cc5e1a6
DB
156 }
157 else if (bytes > 1 << 10)
158 {
159 bytes = (bytes * 10) / (1 << 10);
160
19ab5cea
AG
161 g_string_append_printf (str, " (%"G_GUINT64_FORMAT".%"G_GUINT64_FORMAT" KiB)",
162 bytes / 10, bytes % 10);
6cc5e1a6 163 }
6b775dbb 164 else if (bytes > 0)
6cc5e1a6 165 {
19ab5cea 166 g_string_append_printf (str, " (%"G_GUINT64_FORMAT" B)", bytes);
6cc5e1a6
DB
167 }
168}
169
170static void
171netstatus_dialog_update_activity (NetstatusDialogData *data)
172{
173 NetstatusStats stats = { 0, };
174 GString *str;
175
176 netstatus_iface_get_statistics (data->iface, &stats);
177
178 str = g_string_new (NULL);
179
180 print_packets_string (str, stats.out_packets);
181 print_bytes_string (str, stats.out_bytes);
182 gtk_label_set_text (GTK_LABEL (data->sent), str->str);
6b775dbb 183
6cc5e1a6
DB
184 print_packets_string (str, stats.in_packets);
185 print_bytes_string (str, stats.in_bytes);
186 gtk_label_set_text (GTK_LABEL (data->received), str->str);
6b775dbb 187
6cc5e1a6
DB
188 g_string_free (str, TRUE);
189}
190
191static void
192netstatus_dialog_update_signal_strength (NetstatusDialogData *data)
193{
194 if (netstatus_iface_get_is_wireless (data->iface))
195 {
196 int signal_strength;
197 char *str;
198
199 gtk_widget_show (data->signal_strength_frame);
200
201 signal_strength = netstatus_iface_get_signal_strength (data->iface);
202
203 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (data->signal_strength_bar),
204 signal_strength == 0 ? 0.0 : signal_strength / 100.0);
205
206 str = g_strdup_printf ("%d%%\n", signal_strength);
207 gtk_label_set_text (GTK_LABEL (data->signal_strength_label), str);
208 g_free (str);
209 }
210 else
211 {
212 gtk_widget_hide (data->signal_strength_frame);
213 }
214}
215
216static void
217netstatus_dialog_update_inet4_support (NetstatusDialogData *data)
218{
219 char *addr = NULL;
220 char *dest = NULL;
221 char *bcast = NULL;
222 char *mask = NULL;
223
224 if (netstatus_iface_get_inet4_details (data->iface, &addr, &dest, &bcast, &mask))
225 {
226 gtk_widget_show (data->inet4_frame);
227
228 /* Address */
229 if (addr)
230 {
231 gtk_table_set_row_spacing (GTK_TABLE (data->inet4_table), 0, 6);
232 gtk_label_set_text (GTK_LABEL (data->inet4_addr), addr);
233 gtk_widget_show (data->inet4_addr);
234 gtk_widget_show (data->inet4_addr_title);
235 }
236 else
237 {
238 gtk_table_set_row_spacing (GTK_TABLE (data->inet4_table), 0, 0);
239 gtk_widget_hide (data->inet4_addr);
240 gtk_widget_hide (data->inet4_addr_title);
241 }
242
243 /* Destination */
244 if (dest)
245 {
246 gtk_table_set_row_spacing (GTK_TABLE (data->inet4_table), 1, 6);
247 gtk_label_set_text (GTK_LABEL (data->inet4_dest), dest);
248 gtk_widget_show (data->inet4_dest);
249 gtk_widget_show (data->inet4_dest_title);
250 }
251 else
252 {
253 gtk_table_set_row_spacing (GTK_TABLE (data->inet4_table), 1, 0);
254 gtk_widget_hide (data->inet4_dest);
255 gtk_widget_hide (data->inet4_dest_title);
256 }
257
258 /* Broadcast */
259 if (bcast)
260 {
261 gtk_table_set_row_spacing (GTK_TABLE (data->inet4_table), 2, 6);
262 gtk_label_set_text (GTK_LABEL (data->inet4_bcast), bcast);
263 gtk_widget_show (data->inet4_bcast);
264 gtk_widget_show (data->inet4_bcast_title);
265 }
266 else
267 {
268 gtk_table_set_row_spacing (GTK_TABLE (data->inet4_table), 2, 0);
269 gtk_widget_hide (data->inet4_bcast);
270 gtk_widget_hide (data->inet4_bcast_title);
271 }
272
273 /* Subnet Mask */
274 if (mask)
275 {
276 gtk_label_set_text (GTK_LABEL (data->inet4_mask), mask);
277 gtk_widget_show (data->inet4_mask);
278 gtk_widget_show (data->inet4_mask_title);
279 }
280 else
281 {
282 gtk_widget_hide (data->inet4_mask);
283 gtk_widget_hide (data->inet4_mask_title);
284 }
285
286 g_free (addr);
287 g_free (dest);
288 g_free (bcast);
289 g_free (mask);
290 }
291 else
292 {
293 gtk_widget_hide (data->inet4_frame);
294 }
295}
296
297static void
298netstatus_dialog_update_device_support (NetstatusDialogData *data)
299{
300 const char *type = NULL;
301 char *addr = NULL;
302
303 if (netstatus_iface_get_device_details (data->iface, &type, &addr))
304 {
305 g_assert (type != NULL);
306
307 gtk_widget_show (data->dev_frame);
308
309 /* Device type */
310 gtk_label_set_text (GTK_LABEL (data->dev_type), type);
6b775dbb 311
6cc5e1a6
DB
312 /* Address */
313 if (addr)
314 gtk_label_set_text (GTK_LABEL (data->dev_addr), addr);
315 else
316 gtk_label_set_text (GTK_LABEL (data->dev_addr), _("Unknown"));
317
318 g_free (addr);
319 }
320 else
321 {
322 gtk_widget_hide (data->dev_frame);
323 }
324}
325
326static void
327netstatus_dialog_iface_state_changed (NetstatusIface *iface,
328 GParamSpec *pspec,
329 NetstatusDialogData *data)
330{
331 netstatus_dialog_update_state (data);
332 netstatus_dialog_update_inet4_support (data);
333 netstatus_dialog_update_device_support (data);
334}
335
336static void
337netstatus_dialog_iface_stats_changed (NetstatusIface *iface,
338 GParamSpec *pspec,
339 NetstatusDialogData *data)
340{
341 netstatus_dialog_update_activity (data);
342}
343
344static void
345netstatus_dialog_iface_signal_strength_changed (NetstatusIface *iface,
346 GParamSpec *pspec,
347 NetstatusDialogData *data)
348{
349 netstatus_dialog_update_signal_strength (data);
350}
351
352static void
353netstatus_dialog_iface_name_changed (NetstatusIface *iface,
354 GParamSpec *pspec,
355 NetstatusDialogData *data)
356{
357 netstatus_dialog_update_name (data);
358 netstatus_dialog_update_signal_strength (data);
359}
360
361static void
362netstatus_dialog_set_iface_name (NetstatusDialogData *data,
363 GtkEntry *entry)
364{
365 const char *iface_name;
366
367 iface_name = gtk_entry_get_text (entry);
368 if (iface_name && iface_name [0])
369 {
370 netstatus_iface_set_name (data->iface, iface_name);
371
372 gtk_widget_set_sensitive (data->configure_button,
373 !netstatus_iface_get_is_loopback (data->iface));
374 }
375}
376
377static void
378netstatus_dialog_response (GtkWidget *dialog,
379 int response)
380{
381 switch (response)
382 {
383 case GTK_RESPONSE_HELP:
384 {
385 GError *error = NULL;
386 GdkScreen *screen;
387
388 screen = gtk_window_get_screen (GTK_WINDOW (dialog));
389
390 // gnome_help_display_on_screen ("gnome-netstatus", "gnome-netstatus-props", screen, &error);
391
392 if (error)
393 {
394 GtkWidget *message_dialog;
6b775dbb 395
6cc5e1a6
DB
396 message_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
397 GTK_DIALOG_DESTROY_WITH_PARENT,
398 GTK_MESSAGE_ERROR,
399 GTK_BUTTONS_CLOSE,
400 _("There was an error displaying help:\n%s"),
401 error->message);
402
403 gtk_window_set_resizable (GTK_WINDOW (message_dialog), FALSE);
404 gtk_window_set_screen (GTK_WINDOW (message_dialog), screen);
6b775dbb 405
6cc5e1a6
DB
406 g_signal_connect (message_dialog, "response",
407 G_CALLBACK (gtk_widget_destroy),
408 NULL);
6b775dbb 409
6cc5e1a6
DB
410 gtk_widget_show (message_dialog);
411
412 g_error_free (error);
413 }
414 }
415 break;
416 default:
417 gtk_widget_hide (dialog);
418 break;
419 }
420}
421
422static void
423netstatus_dialog_destroy (GtkWidget *dialog)
424{
425 NetstatusDialogData *data;
426
427 data = g_object_get_data (G_OBJECT (dialog), "netstatus-dialog-data");
428 if (!data)
429 return;
430
431 if (data->config_tool)
432 g_free (data->config_tool);
433 data->config_tool = NULL;
434
435 /*
436 if (data->listener)
437 {
438 g_assert (data->client != NULL);
439
440 gconf_client_notify_remove (data->client, data->listener);
441 data->listener = 0;
442
443 gconf_client_remove_dir (data->client, NETWORK_CONFIG_TOOL_DIR, NULL);
444 }
445
446 if (data->client)
447 g_object_unref (data->client);
448 data->client = NULL;
449 */
450
451 if (data->iface_list_monitor)
452 g_source_remove (data->iface_list_monitor);
453 data->iface_list_monitor = 0;
454
455 if (data->iface)
456 g_object_unref (data->iface);
457 data->iface = NULL;
458
459 g_free (data);
460}
461
462static gboolean
463netstatus_dialog_check_config_tool (NetstatusDialogData *dialog_data,
464 const char *config_tool)
465{
466 char **argv = NULL;
467 gboolean found = FALSE;
468
469 if (config_tool && g_shell_parse_argv (config_tool, NULL, &argv, NULL))
470 {
471 char *path;
472
473 g_assert (argv != NULL);
474
475 if ((path = g_find_program_in_path (argv [0])))
476 {
477 if (dialog_data->config_tool)
478 g_free (dialog_data->config_tool);
479 dialog_data->config_tool = g_strdup (config_tool);
480 found = TRUE;
481 }
482
483 g_free (path);
484 g_strfreev (argv);
485 }
486
487 return found;
488}
489
490/*
491static void
492netstatus_dialog_config_tool_notify (GConfClient *client,
493 guint cnx_id,
494 GConfEntry *entry,
495 NetstatusDialogData *dialog_data)
496{
497 if (!entry->value || !entry->value->type == GCONF_VALUE_STRING)
498 return;
499
500 netstatus_dialog_check_config_tool (dialog_data,
501 gconf_value_get_string (entry->value));
502}
503*/
504
505static void
506netstatus_dialog_detect_configuration_tool (NetstatusDialogData *dialog_data)
507{
508 /*
509 char *config_tool;
510 int i;
511
512 dialog_data->client = gconf_client_get_default ();
513
514 gconf_client_add_dir (dialog_data->client,
515 NETWORK_CONFIG_TOOL_DIR,
516 GCONF_CLIENT_PRELOAD_NONE,
517 NULL);
518
519 dialog_data->listener =
520 gconf_client_notify_add (dialog_data->client,
521 NETWORK_CONFIG_TOOL_KEY,
522 (GConfClientNotifyFunc) netstatus_dialog_config_tool_notify,
523 dialog_data, NULL, NULL);
524
525 config_tool = gconf_client_get_string (dialog_data->client,
526 NETWORK_CONFIG_TOOL_KEY,
527 NULL);
528 if (netstatus_dialog_check_config_tool (dialog_data, config_tool))
529 {
530 g_free (config_tool);
531 return;
532 }
533
534 for (i = 0; i < G_N_ELEMENTS (network_config_tools); i++)
535 {
536 if (netstatus_dialog_check_config_tool (dialog_data, network_config_tools [i]))
537 break;
538 }
539 */
540}
541
542static void
543netstatus_iface_configure (GtkWidget *configure_button,
544 NetstatusDialogData *dialog_data)
545{
6cc5e1a6
DB
546 GString *command;
547 char **argv = NULL;
548 int i;
549
550 g_return_if_fail (dialog_data != NULL);
551 g_return_if_fail (dialog_data->config_tool != NULL);
552
553 g_shell_parse_argv (dialog_data->config_tool, NULL, &argv, NULL);
554
555 /* We've already verified config_tool as valid */
556 g_assert (argv != NULL);
557
558 command = g_string_new (argv [0]);
559
560 for (i = 1; argv [i]; i++)
561 {
562 command = g_string_append_c (command, ' ');
563
564 if (!strcmp (argv [i], "%i"))
565 command = g_string_append (command,
566 netstatus_iface_get_name (dialog_data->iface));
567 else
568 command = g_string_append (command, argv [i]);
569 }
570
6b775dbb
AG
571 fm_launch_command_simple(GTK_WINDOW (dialog_data->dialog), NULL,
572 G_APP_INFO_CREATE_NONE, command->str, NULL);
6cc5e1a6
DB
573
574 g_string_free (command, TRUE);
575 g_strfreev (argv);
576}
577
578static void
579netstatus_dialog_setup_configure_button (NetstatusDialogData *data)
580{
2ba86315 581 data->configure_button = (GtkWidget*)gtk_builder_get_object(data->builder, "configure_button");
6cc5e1a6
DB
582
583 g_signal_connect (data->configure_button, "clicked",
584 G_CALLBACK (netstatus_iface_configure), data);
585
586 netstatus_dialog_detect_configuration_tool (data);
6b775dbb 587
6cc5e1a6
DB
588 if (!data->config_tool)
589 gtk_widget_hide (data->configure_button);
590
591 gtk_widget_set_sensitive (data->configure_button,
592 !netstatus_iface_get_is_loopback (data->iface));
593}
594
f7ecd6ce
AG
595#if !GTK_CHECK_VERSION(2, 24, 0)
596static void _combo_box_entry_active_changed (GtkComboBox *combo_box,
597 gpointer user_data)
598{
599 GtkTreeModel *model;
600 GtkTreeIter iter;
601 gchar *str = NULL;
602
603 if (gtk_combo_box_get_active_iter (combo_box, &iter))
604 {
605 GtkEntry *entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo_box)));
606
607 if (entry)
608 {
609 model = gtk_combo_box_get_model (combo_box);
610
611 gtk_tree_model_get (model, &iter, 0, &str, -1);
612 gtk_entry_set_text (entry, str);
613 g_free (str);
614 }
615 }
616}
617#endif
618
6cc5e1a6
DB
619static void
620netstatus_dialog_setup_connection (NetstatusDialogData *data)
621{
622 GtkWidget *hbox;
623 GtkWidget *icon;
2ba86315 624 GtkListStore* model;
6b775dbb 625
2ba86315 626 hbox = (GtkWidget*)gtk_builder_get_object(data->builder, "connection_hbox");
6cc5e1a6
DB
627
628 icon = netstatus_icon_new (data->iface);
629 netstatus_icon_set_tooltips_enabled (NETSTATUS_ICON (icon), FALSE);
630 netstatus_icon_set_show_signal (NETSTATUS_ICON (icon), FALSE);
631 gtk_box_pack_end (GTK_BOX (hbox), icon, FALSE, TRUE, 4);
632 gtk_widget_show (icon);
633
634 data->icon = NETSTATUS_ICON (icon);
635
2ba86315
DB
636 data->name = (GtkWidget*)gtk_builder_get_object(data->builder, "name_combo");
637 data->name_entry = gtk_bin_get_child((GtkBin*)data->name);
638 model = gtk_list_store_new(1, G_TYPE_STRING);
514580cf 639 gtk_combo_box_set_model(GTK_COMBO_BOX(data->name), GTK_TREE_MODEL(model));
6b775dbb
AG
640#if GTK_CHECK_VERSION(2, 24, 0)
641 gtk_combo_box_set_entry_text_column(GTK_COMBO_BOX(data->name), 0);
642#else
f7ecd6ce
AG
643 /* emulate 2.24/GtkComboBoxEntry behavior */
644 GtkCellRenderer *text_renderer = gtk_cell_renderer_text_new ();
645 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (data->name),
646 text_renderer, TRUE);
647
648 gtk_combo_box_set_active (GTK_COMBO_BOX (data->name), -1);
649
650 g_signal_connect (data->name, "changed",
651 G_CALLBACK (_combo_box_entry_active_changed), NULL);
652
653 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (data->name),
654 text_renderer, "text", 0, NULL);
6b775dbb 655#endif
2ba86315
DB
656 g_object_unref(model);
657
658 data->status = (GtkWidget*)gtk_builder_get_object(data->builder, "status_label");
6cc5e1a6
DB
659
660 netstatus_dialog_update_name (data);
661 netstatus_dialog_update_state (data);
662}
663
664static void
665netstatus_dialog_setup_activity (NetstatusDialogData *data)
666{
2ba86315
DB
667 data->sent = (GtkWidget*)gtk_builder_get_object(data->builder, "sent_label");
668 data->received = (GtkWidget*)gtk_builder_get_object(data->builder, "received_label");
6cc5e1a6
DB
669
670 netstatus_dialog_update_activity (data);
671}
672
673static void
674netstatus_dialog_setup_signal_strength_details (NetstatusDialogData *data)
675{
2ba86315
DB
676 data->signal_strength_frame = (GtkWidget*)gtk_builder_get_object(data->builder, "signal_strength_frame");
677 data->signal_strength_bar = (GtkWidget*)gtk_builder_get_object(data->builder, "signal_strength_bar");
678 data->signal_strength_label = (GtkWidget*)gtk_builder_get_object(data->builder, "signal_strength_label");
6cc5e1a6
DB
679
680 netstatus_dialog_update_signal_strength (data);
681}
682
683static void
684netstatus_dialog_setup_inet4_support (NetstatusDialogData *data)
685{
2ba86315
DB
686 data->inet4_frame = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_frame");
687 data->inet4_table = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_table");
688 data->inet4_addr = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_addr_label");
689 data->inet4_addr_title = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_addr_title");
690 data->inet4_dest = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_dest_label");
691 data->inet4_dest_title = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_dest_title");
692 data->inet4_bcast = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_bcast_label");
693 data->inet4_bcast_title = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_bcast_title");
694 data->inet4_mask = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_mask_label");
695 data->inet4_mask_title = (GtkWidget*)gtk_builder_get_object(data->builder, "inet4_mask_title");
6cc5e1a6
DB
696
697 netstatus_dialog_update_inet4_support (data);
698}
699static void
700netstatus_dialog_setup_device_support (NetstatusDialogData *data)
701{
2ba86315
DB
702 data->dev_frame = (GtkWidget*)gtk_builder_get_object(data->builder, "dev_frame");
703 data->dev_type = (GtkWidget*)gtk_builder_get_object(data->builder, "dev_type_label");
704 data->dev_addr = (GtkWidget*)gtk_builder_get_object(data->builder, "dev_addr_label");
6b775dbb 705
6cc5e1a6
DB
706 netstatus_dialog_update_device_support (data);
707}
708
709static void
710netstatus_dialog_set_icon (GtkWidget *dialog)
711{
712 GtkIconTheme *icon_theme;
713 GtkIconInfo *icon_info;
714
715 icon_theme = gtk_icon_theme_get_for_screen (gtk_window_get_screen (GTK_WINDOW (dialog)));
716
717 if ((icon_info = gtk_icon_theme_lookup_icon (icon_theme, "gnome-netstatus-tx", 48, 0)))
718 {
719 gtk_window_set_icon_from_file (GTK_WINDOW (dialog),
720 gtk_icon_info_get_filename (icon_info),
721 NULL);
722 gtk_icon_info_free (icon_info);
723 }
724}
725
726static gboolean
6b775dbb 727_netstatus_dialog_iface_list_monitor (NetstatusDialogData *data)
6cc5e1a6
DB
728{
729 GList *iface_names, *l;
730 int n_ifaces;
2ba86315 731 GtkListStore* model;
6cc5e1a6
DB
732
733 iface_names = netstatus_list_interface_names (NULL);
734
735 n_ifaces = g_list_length (iface_names);
736
737 if (data->n_ifaces != n_ifaces)
738 {
514580cf 739 model = (GtkListStore*)gtk_combo_box_get_model(GTK_COMBO_BOX(data->name));
2ba86315
DB
740 gtk_list_store_clear(model);
741 g_signal_handlers_block_by_func (data->name_entry,
6cc5e1a6 742 G_CALLBACK (netstatus_dialog_set_iface_name), data);
6b775dbb 743
2ba86315
DB
744 for (l = iface_names; l; l = l->next)
745 {
746 GtkTreeIter it;
747 gtk_list_store_append(model, &it);
748 gtk_list_store_set(model, &it, 0, (char*)l->data, -1);
749 }
750 /* GtkCombo is deprecated. */
751 /* gtk_combo_set_popdown_strings (GTK_COMBO (data->name), iface_names); */
6cc5e1a6
DB
752 netstatus_dialog_update_name (data);
753
2ba86315 754 g_signal_handlers_unblock_by_func (data->name_entry,
6cc5e1a6
DB
755 G_CALLBACK (netstatus_dialog_set_iface_name), data);
756 }
757
758 data->n_ifaces = n_ifaces;
759
760 for (l = iface_names; l; l = l->next)
761 g_free (l->data);
762 g_list_free (iface_names);
763
764 return TRUE;
765}
766
6b775dbb
AG
767static gboolean
768netstatus_dialog_iface_list_monitor (gpointer data)
769{
770 if (g_source_is_destroyed(g_main_current_source()))
771 return FALSE;
772 return _netstatus_dialog_iface_list_monitor (data);
773}
774
6cc5e1a6
DB
775GtkWidget *
776netstatus_dialog_new (NetstatusIface *iface)
777{
778 NetstatusDialogData *data;
779 data = g_new0 (NetstatusDialogData, 1);
780
2ba86315
DB
781 data->builder = gtk_builder_new();
782 gtk_builder_add_from_file(data->builder, PACKAGE_UI_DIR "/netstatus.ui", NULL);
514580cf 783 data->dialog = (GtkWidget*)gtk_builder_get_object(data->builder, "network_status_dialog");
6cc5e1a6
DB
784
785 g_object_set_data (G_OBJECT (data->dialog), "netstatus-dialog-data", data);
786
787 netstatus_dialog_set_icon (data->dialog);
788
789 data->iface = g_object_ref (iface);
790 netstatus_connect_signal_while_alive (data->iface,
791 "notify::state",
792 G_CALLBACK (netstatus_dialog_iface_state_changed),
793 data,
794 data->dialog);
795
796 netstatus_connect_signal_while_alive (data->iface,
797 "notify::stats",
798 G_CALLBACK (netstatus_dialog_iface_stats_changed),
799 data,
800 data->dialog);
801
802 netstatus_connect_signal_while_alive (data->iface,
803 "notify::name",
804 G_CALLBACK (netstatus_dialog_iface_name_changed),
805 data,
806 data->dialog);
6b775dbb 807
6cc5e1a6
DB
808 netstatus_connect_signal_while_alive (data->iface,
809 "notify::wireless",
810 G_CALLBACK (netstatus_dialog_iface_signal_strength_changed),
811 data,
812 data->dialog);
813
814 netstatus_connect_signal_while_alive (data->iface,
815 "notify::signal-strength",
816 G_CALLBACK (netstatus_dialog_iface_signal_strength_changed),
817 data,
818 data->dialog);
819
820 g_signal_connect (data->dialog, "response",
821 G_CALLBACK (netstatus_dialog_response), NULL);
822
823 g_signal_connect (data->dialog, "destroy",
824 G_CALLBACK (netstatus_dialog_destroy), NULL);
825
826 netstatus_dialog_setup_connection (data);
827 netstatus_dialog_setup_activity (data);
828 netstatus_dialog_setup_signal_strength_details (data);
829 netstatus_dialog_setup_inet4_support (data);
830 netstatus_dialog_setup_device_support (data);
831 netstatus_dialog_setup_configure_button (data);
832
833 data->iface_list_monitor = g_timeout_add (2 * 1000,
6b775dbb 834 netstatus_dialog_iface_list_monitor,
6cc5e1a6 835 data);
6b775dbb 836 _netstatus_dialog_iface_list_monitor (data);
6cc5e1a6 837
2ba86315 838 g_signal_connect_swapped (data->name_entry, "changed",
6cc5e1a6
DB
839 G_CALLBACK (netstatus_dialog_set_iface_name),
840 data);
841
2ba86315
DB
842 g_object_unref(data->builder);
843 data->builder = NULL;
844
6cc5e1a6
DB
845 return data->dialog;
846}
847
848/* 2006.10.08 Add by Hong Jen Yee (PCMan) to be used in lxpanel plugin */
849void netstatus_dialog_set_configuration_tool( GtkWidget* dialog, const char* tool )
850{
851 NetstatusDialogData *data;
852 data = g_object_get_data (G_OBJECT (dialog), "netstatus-dialog-data");
853
854 netstatus_dialog_check_config_tool( data, tool );
855
856 if (data->config_tool)
857 gtk_widget_show (data->configure_button);
858 else
859 gtk_widget_hide (data->configure_button);
860}
861
862const char* netstatus_dialog_get_configuration_tool( GtkWidget* dialog )
863{
864 NetstatusDialogData *data;
865 data = g_object_get_data (G_OBJECT (dialog), "netstatus-dialog-data");
866 return data->config_tool;
867}
7486d297
DB
868
869/* 2009.05.10 Add by Hong Jen Yee (PCMan) to be used in lxpanel plugin */
870const char* netstatus_dialog_get_iface_name( GtkWidget* dialog )
871{
872 NetstatusDialogData *data;
873 data = g_object_get_data (G_OBJECT (dialog), "netstatus-dialog-data");
874 return netstatus_iface_get_name (data->iface);
875}