File: network-status-tile.c

package info (click to toggle)
gnome-main-menu 0.9.14-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,476 kB
  • ctags: 976
  • sloc: sh: 10,242; ansic: 4,757; makefile: 143; xml: 8
file content (453 lines) | stat: -rw-r--r-- 12,669 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
 * This file is part of the Main Menu.
 *
 * Copyright (c) 2006 Novell, Inc.
 *
 * The Main Menu is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * The Main Menu is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * the Main Menu; if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "network-status-tile.h"

#include <string.h>
#include <glib/gi18n.h>

#include "network-status-agent.h"
#include "network-status-info.h"

G_DEFINE_TYPE (NetworkStatusTile, network_status_tile, NAMEPLATE_TILE_TYPE)

static void network_status_tile_finalize (GObject *);
static void network_status_tile_style_set (GtkWidget *, GtkStyle *);

static void network_status_tile_activated (Tile *, TileEvent *);

static void network_status_tile_open (Tile *, TileEvent *, TileAction *);

static void update_tile (NetworkStatusTile *);
static void refresh_status (NetworkStatusTile *);

static void build_info_dialog (NetworkStatusTile *);
static void update_info_dialog (NetworkStatusTile *);

static void tile_show_event_cb (GtkWidget *, gpointer);
static void status_changed_cb (NetworkStatusAgent *, gpointer);
static void info_dialog_cfg_button_clicked_cb (GtkButton *, gpointer);

static void set_ui_label (GtkBuilder *, const gchar *, const gchar *);
static void launch_network_config (const gchar * desktop_key);

typedef struct
{
	NetworkStatusAgent *agent;
	NetworkStatusInfo *status_info;
	
	GtkBuilder *info_dialog_ui;
	GtkWidget *info_dialog;
} NetworkStatusTilePrivate;

#define NETWORK_STATUS_TILE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NETWORK_STATUS_TILE_TYPE, NetworkStatusTilePrivate))

static void network_status_tile_class_init (NetworkStatusTileClass * net_tile_class)
{
	GObjectClass *g_obj_class = G_OBJECT_CLASS (net_tile_class);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (net_tile_class);
	TileClass *tile_class = TILE_CLASS (net_tile_class);

	g_obj_class->finalize = network_status_tile_finalize;

	widget_class->style_set = network_status_tile_style_set;

	tile_class->tile_activated = network_status_tile_activated;

	g_type_class_add_private (net_tile_class, sizeof (NetworkStatusTilePrivate));
}

GtkWidget *
network_status_tile_new ()
{
	NetworkStatusTile *tile;

	GtkWidget *image;
	GtkWidget *header;
	GtkWidget *subheader;

	image = gtk_image_new ();
	slab_load_image (GTK_IMAGE (image), GTK_ICON_SIZE_BUTTON, "nm-no-connection");

	header = gtk_label_new (_("Network: None"));
	gtk_misc_set_alignment (GTK_MISC (header), 0.0, 0.5);

	subheader = gtk_label_new (_("Click to configure network"));
	gtk_label_set_ellipsize (GTK_LABEL (subheader), PANGO_ELLIPSIZE_END);
	gtk_misc_set_alignment (GTK_MISC (subheader), 0.0, 0.5);
	gtk_widget_modify_fg (subheader, GTK_STATE_NORMAL,
		&subheader->style->fg[GTK_STATE_INSENSITIVE]);

	tile = g_object_new (NETWORK_STATUS_TILE_TYPE, "tile-uri", "tile://network-status",
		"nameplate-image", image, "nameplate-header", header, "nameplate-subheader",
		subheader, NULL);

	TILE (tile)->actions = g_new0 (TileAction *, 1);
	TILE (tile)->n_actions = 1;

	TILE (tile)->actions[NETWORK_STATUS_TILE_ACTION_OPEN] =
		tile_action_new (TILE (tile), network_status_tile_open, NULL,
		TILE_ACTION_OPENS_NEW_WINDOW);

	TILE (tile)->default_action = TILE (tile)->actions[NETWORK_STATUS_TILE_ACTION_OPEN];

	g_signal_connect (G_OBJECT (tile), "show", G_CALLBACK (tile_show_event_cb), NULL);

	return GTK_WIDGET (tile);
}

static void
network_status_tile_init (NetworkStatusTile * tile)
{
	NetworkStatusTilePrivate *priv = NETWORK_STATUS_TILE_GET_PRIVATE (tile);

	priv->agent = NULL;
	priv->info_dialog_ui = NULL;
	priv->info_dialog = NULL;
}

static void
network_status_tile_finalize (GObject * g_object)
{
	NetworkStatusTilePrivate *priv = NETWORK_STATUS_TILE_GET_PRIVATE (g_object);
	if (priv->status_info)
		g_object_unref (priv->status_info);
	if (priv->agent)
		g_object_unref (priv->agent);
	if (priv->info_dialog_ui)
		g_object_unref (priv->info_dialog_ui);

	(*G_OBJECT_CLASS (network_status_tile_parent_class)->finalize) (g_object);
}

static void
network_status_tile_style_set (GtkWidget * widget, GtkStyle * prev_style)
{
	update_tile (NETWORK_STATUS_TILE (widget));
}

static void
network_status_tile_activated (Tile * tile, TileEvent * event)
{
	if (event->type != TILE_EVENT_ACTIVATED_DOUBLE_CLICK)
		tile_trigger_action_with_time (tile, tile->default_action, event->time);
}

static void
network_status_tile_open (Tile * tile, TileEvent * event, TileAction * action)
{
	NetworkStatusTilePrivate *priv = NETWORK_STATUS_TILE_GET_PRIVATE (tile);

	if (!priv->status_info || !priv->agent->nm_present)
	{
		launch_network_config (SLAB_NETWORK_CONFIG_TOOL_KEY);

		return;
	}
	else
	{
		launch_network_config (SLAB_NETWORK_CONFIG_TOOL_NM_KEY);
		return;
	}

	/*  Using NM's glade file directly is a big maintenance problem. We need to get
	* NM to provide a API or .desktop file to show their status. For now just call the config
	if (!priv->info_dialog)
		build_info_dialog (NETWORK_STATUS_TILE (tile));

	if (!priv->info_dialog)
	{
		g_warning
			("network_status_tile_open: could not find NetworkManager's applet.glade\n");

		return;
	}

	update_info_dialog (NETWORK_STATUS_TILE (tile));

	gtk_window_present_with_time (GTK_WINDOW (priv->info_dialog), event->time);
	*/
}

static void
update_tile (NetworkStatusTile * tile)
{
	NetworkStatusTilePrivate *priv = NETWORK_STATUS_TILE_GET_PRIVATE (tile);
	AtkObject *accessible;

	gchar *icon_name;
	gchar *header_text;
	gchar *subheader_text;

	gchar *markup = NULL;

	if (!priv->status_info)
	{
		icon_name = "nm-no-connection";
		header_text = _("Networ_k: None");
		subheader_text = _("Click to configure network");
	}

	else
	{
		switch (priv->status_info->type)
		{
		case DEVICE_TYPE_802_11_WIRELESS:
			markup = g_strdup_printf (_("Connected to: %s"), priv->status_info->essid);

			icon_name = "nm-device-wireless";
			header_text = _("Networ_k: Wireless");
			subheader_text = markup;
			break;

		case DEVICE_TYPE_802_3_ETHERNET:
			markup = g_strdup_printf (_("Using ethernet (%s)"),
				priv->status_info->iface);

			icon_name = "nm-device-wired";
			header_text = _("Networ_k: Wired");
			subheader_text = markup;
			break;

		case DEVICE_TYPE_GSM:
			markup = g_strdup_printf (_("Connected to: %s"), priv->status_info->iface);

			icon_name = "nm-device-wireless";
			header_text = _("Networ_k: GSM");
			subheader_text = markup;
			break;

		case DEVICE_TYPE_CDMA:
			markup = g_strdup_printf (_("Connected to: %s"), priv->status_info->iface);

			icon_name = "nm-device-wireless";
			header_text = _("Networ_k: CDMA");
			subheader_text = markup;
			break;

		default:
			icon_name = "";
			header_text = "";
			subheader_text = "";
			break;
		}
	}

	slab_load_image (GTK_IMAGE (NAMEPLATE_TILE (tile)->image), GTK_ICON_SIZE_BUTTON, icon_name);
	gtk_label_set_text (GTK_LABEL (NAMEPLATE_TILE (tile)->header), header_text);
	gtk_label_set_text (GTK_LABEL (NAMEPLATE_TILE (tile)->subheader), subheader_text);

	gtk_label_set_use_underline (GTK_LABEL (NAMEPLATE_TILE (tile)->header), TRUE);
	gtk_label_set_mnemonic_widget (GTK_LABEL (NAMEPLATE_TILE (tile)->header), GTK_WIDGET (tile));

	accessible = gtk_widget_get_accessible (GTK_WIDGET (tile));
	if (header_text)
	  atk_object_set_name (accessible, header_text);
	if (subheader_text)
	  atk_object_set_description (accessible, subheader_text);

	g_free (markup);
}

static void
refresh_status (NetworkStatusTile * tile)
{
	NetworkStatusTilePrivate *priv = NETWORK_STATUS_TILE_GET_PRIVATE (tile);

	if (!priv->agent)
	{
		priv->agent = network_status_agent_new ();

		g_signal_connect (G_OBJECT (priv->agent), "status-changed",
			G_CALLBACK (status_changed_cb), tile);
	}

	if (priv->agent)
	{
		if (priv->status_info)
			g_object_unref (priv->status_info);

		priv->status_info = network_status_agent_get_first_active_device_info (priv->agent);
	}
}

static void
build_info_dialog (NetworkStatusTile * tile)
{
	/*
	NetworkStatusTilePrivate *priv = NETWORK_STATUS_TILE_GET_PRIVATE (tile);

	gchar *filename;

	GtkWidget *close_button;
	GtkWidget *net_cfg_button;

	filename = g_build_filename (DATADIR, "nm-applet/applet.glade", NULL);

	if (!g_file_test (filename, G_FILE_TEST_EXISTS))
		return;

	priv->info_dialog_xml = glade_xml_new (filename, "info_dialog", "NetworkManager");

	priv->info_dialog = glade_xml_get_widget (priv->info_dialog_xml, "info_dialog");

	close_button = glade_xml_get_widget (priv->info_dialog_xml, "closebutton1");
	net_cfg_button = glade_xml_get_widget (priv->info_dialog_xml, "configure_button");

	g_signal_connect (priv->info_dialog, "delete-event", G_CALLBACK (gtk_widget_hide_on_delete),
		NULL);

	g_signal_connect_swapped (close_button, "clicked", G_CALLBACK (gtk_widget_hide),
		priv->info_dialog);

	g_signal_connect (net_cfg_button, "clicked", G_CALLBACK (info_dialog_cfg_button_clicked_cb),
		priv->info_dialog);

	g_free (filename);
	*/
}

static void
update_info_dialog (NetworkStatusTile * tile)
{
	NetworkStatusTilePrivate *priv = NETWORK_STATUS_TILE_GET_PRIVATE (tile);

	GtkBuilder *ui = priv->info_dialog_ui;

	gchar *iface_and_type = NULL;
	gchar *speed = NULL;

	if (!priv->status_info)
		return;

	switch (priv->status_info->type)
	{
	case DEVICE_TYPE_802_11_WIRELESS:
		iface_and_type =
			g_strdup_printf (_("Wireless Ethernet (%s)"), priv->status_info->iface);
		break;

	case DEVICE_TYPE_802_3_ETHERNET:
		iface_and_type =
			g_strdup_printf (_("Wired Ethernet (%s)"), priv->status_info->iface);
		break;

	case DEVICE_TYPE_GSM:
		iface_and_type =
			g_strdup_printf (_("Mobile Ethernet (%s)"), priv->status_info->iface);
		break;

	case DEVICE_TYPE_CDMA:
		iface_and_type =
			g_strdup_printf (_("Mobile Ethernet (%s)"), priv->status_info->iface);
		break;

	default:
		iface_and_type = g_strdup_printf (_("Unknown"));
		break;
	}

	set_ui_label (ui, "label-interface", iface_and_type);
	g_free (iface_and_type);

	if (priv->status_info->speed_mbs)
		speed = g_strdup_printf (_("%d Mb/s"), priv->status_info->speed_mbs);
	else
		speed = g_strdup_printf (_("Unknown"));

	set_ui_label (ui, "label-speed", speed);
	g_free (speed);

	set_ui_label (ui, "label-driver", priv->status_info->driver);
	set_ui_label (ui, "label-ip-address", priv->status_info->ip4_addr);
	set_ui_label (ui, "label-broadcast-address", priv->status_info->broadcast);
	set_ui_label (ui, "label-subnet-mask", priv->status_info->subnet_mask);
	set_ui_label (ui, "label-default-route", priv->status_info->route);
	set_ui_label (ui, "label-primary-dns", priv->status_info->primary_dns);
	set_ui_label (ui, "label-secondary-dns", priv->status_info->secondary_dns);
	if (priv->status_info->hw_addr)
		set_ui_label (ui, "label-hardware-address", priv->status_info->hw_addr);
}

static void
tile_show_event_cb (GtkWidget * widget, gpointer user_data)
{
	NetworkStatusTile *tile = NETWORK_STATUS_TILE (widget);
	NetworkStatusTilePrivate *priv = NETWORK_STATUS_TILE_GET_PRIVATE (tile);

	if (!priv->status_info)
		refresh_status (tile);

	update_tile (tile);
}

void
network_tile_update_status (GtkWidget * widget)
{
	NetworkStatusTile *tile = NETWORK_STATUS_TILE (widget);
	NetworkStatusTilePrivate *priv = NETWORK_STATUS_TILE_GET_PRIVATE (tile);

	if (priv->agent->nm_present)
		return;

	refresh_status (tile);
	update_tile (tile);
}

static void
status_changed_cb (NetworkStatusAgent * agent, gpointer user_data)
{
	refresh_status (NETWORK_STATUS_TILE (user_data));
	update_tile (NETWORK_STATUS_TILE (user_data));
}

static void
info_dialog_cfg_button_clicked_cb (GtkButton * button, gpointer user_data)
{
	/*
	GtkWidget *dialog = GTK_WIDGET (user_data);

	launch_network_config ();

	gtk_widget_hide (dialog);
	*/
}

static void
set_ui_label (GtkBuilder * ui, const gchar * id, const gchar * text)
{
	GtkLabel *label = NULL;

	label = GTK_LABEL (gtk_builder_get_object (ui, id));

	if (label)
		gtk_label_set_text (label, text);
}

static void
launch_network_config (const gchar * desktop_key)
{
	GnomeDesktopItem *desktop_item =
		load_desktop_item_from_gconf_key (desktop_key);

	if (!open_desktop_item_exec (desktop_item))
		g_warning ("network_status_tile_open: couldn't exec item\n");
	gnome_desktop_item_unref (desktop_item);
}