summaryrefslogtreecommitdiff
path: root/src/mb-menu-item.c
blob: 5fbe383be5dbb5ce7075afc44d4994fb18cb832f (plain)
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* ap-menu-item.c - Class to represent a Wifi access point 
 *
 * Jonathan Blandford <jrb@redhat.com>
 * Dan Williams <dcbw@redhat.com>
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2005 - 2010 Red Hat, Inc.
 */

#include "config.h"

#include "nm-default.h"

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

#include "mb-menu-item.h"

G_DEFINE_TYPE (NMMbMenuItem, nm_mb_menu_item, GTK_TYPE_IMAGE_MENU_ITEM);

#define NM_MB_MENU_ITEM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_MB_MENU_ITEM, NMMbMenuItemPrivate))

typedef struct {
#ifndef ENABLE_INDICATOR
	GtkWidget *strength;
	GtkWidget *detail;
	GtkWidget *hbox;
	GtkWidget *desc;
#endif

	char *desc_string;
	guint32    int_strength;
} NMMbMenuItemPrivate;

static const char *
get_tech_name (guint32 tech)
{
	switch (tech) {
	case MB_TECH_1XRTT:
		return _("CDMA");
	case MB_TECH_EVDO:
		return _("EVDO");
	case MB_TECH_GSM:
		return _("GSM");
	case MB_TECH_GPRS:
		return _("GPRS");
	case MB_TECH_EDGE:
		return _("EDGE");
	case MB_TECH_UMTS:
		return _("UMTS");
	case MB_TECH_HSDPA:
		return _("HSDPA");
	case MB_TECH_HSUPA:
		return _("HSUPA");
	case MB_TECH_HSPA:
		return _("HSPA");
	case MB_TECH_HSPA_PLUS:
		return _("HSPA+");
	case MB_TECH_LTE:
		return _("LTE");
	default:
		return NULL;
	}
}

static void
update_label (NMMbMenuItem *item, gboolean use_bold)
{
	NMMbMenuItemPrivate *priv = NM_MB_MENU_ITEM_GET_PRIVATE (item);

#ifdef ENABLE_INDICATOR
	gtk_menu_item_set_label (GTK_MENU_ITEM (item), priv->desc_string);
#else
	gtk_label_set_use_markup (GTK_LABEL (priv->desc), use_bold);
	if (use_bold) {
		char *markup = g_markup_printf_escaped ("<b>%s</b>", priv->desc_string);

		gtk_label_set_markup (GTK_LABEL (priv->desc), markup);
		g_free (markup);
	} else
		gtk_label_set_text (GTK_LABEL (priv->desc), priv->desc_string);
#endif
}

GtkWidget *
nm_mb_menu_item_new (const char *connection_name,
                     guint32 strength,
                     const char *provider,
                     gboolean active,
                     guint32 technology,
                     guint32 state,
                     gboolean enabled,
                     NMApplet *applet)
{
	NMMbMenuItem *item;
	NMMbMenuItemPrivate *priv;
	const char *tech_name;

	item = g_object_new (NM_TYPE_MB_MENU_ITEM, NULL);
	g_assert (item);

	priv = NM_MB_MENU_ITEM_GET_PRIVATE (item);
	priv->int_strength = strength;

	tech_name = get_tech_name (technology);

	/* Construct the description string */
	switch (state) {
	default:
	case MB_STATE_UNKNOWN:
		priv->desc_string = g_strdup (_("not enabled"));
		break;
	case MB_STATE_IDLE:
		if (connection_name)
			priv->desc_string = g_strdup (connection_name);
		else
			priv->desc_string = g_strdup (_("not registered"));
		break;
	case MB_STATE_HOME:
		if (connection_name) {
			if (provider && tech_name)
				priv->desc_string = g_strdup_printf ("%s (%s %s)", connection_name, provider, tech_name);
			else if (provider || tech_name)
				priv->desc_string = g_strdup_printf ("%s (%s)", connection_name, provider ? provider : tech_name);
			else
				priv->desc_string = g_strdup_printf ("%s", connection_name);
		} else {
			if (provider) {
				if (tech_name)
					priv->desc_string = g_strdup_printf ("%s %s", provider, tech_name);
				else
					priv->desc_string = g_strdup_printf ("%s", provider);
			} else {
				if (tech_name)
					priv->desc_string = g_strdup_printf (_("Home network (%s)"), tech_name);
				else
					priv->desc_string = g_strdup_printf (_("Home network"));
			}
		}
		break;
	case MB_STATE_SEARCHING:
		if (connection_name)
			priv->desc_string = g_strdup (connection_name);
		else
			priv->desc_string = g_strdup (_("searching"));
		break;
	case MB_STATE_DENIED:
		priv->desc_string = g_strdup (_("registration denied"));
		break;
	case MB_STATE_ROAMING:
		if (connection_name) {
			if (tech_name)
				priv->desc_string = g_strdup_printf (_("%s (%s roaming)"), connection_name, tech_name);
			else
				priv->desc_string = g_strdup_printf (_("%s (roaming)"), connection_name);
		} else {
			if (provider) {
				if (tech_name)
					priv->desc_string = g_strdup_printf (_("%s (%s roaming)"), provider, tech_name);
				else
					priv->desc_string = g_strdup_printf (_("%s (roaming)"), provider);
			} else {
				if (tech_name)
					priv->desc_string = g_strdup_printf (_("Roaming network (%s)"), tech_name);
				else
					priv->desc_string = g_strdup_printf (_("Roaming network"));
			}
		}
		break;
	}

	update_label (item, (enabled && connection_name && active));

	/* And the strength icon, if we have strength information at all */
	if (enabled && strength) {
		const char *icon_name = mobile_helper_get_quality_icon_name (strength);
		GdkPixbuf *pixbuf = nma_icon_check_and_load (icon_name, applet);

#ifdef ENABLE_INDICATOR
#ifdef DBUSMENU_PIXMAP_SUPPORT
		gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), gtk_image_new_from_pixbuf (pixbuf));
#else
		gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
		                               gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU));
		pixbuf = NULL;
#endif
		/* For some reason we must always re-set always-show after setting the image */
		gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
#else
		gtk_image_set_from_pixbuf (GTK_IMAGE (priv->strength), pixbuf);
#endif
	}

	return GTK_WIDGET (item);
}

/*******************************************************/

static void
nm_mb_menu_item_init (NMMbMenuItem *self)
{
#ifndef ENABLE_INDICATOR
	NMMbMenuItemPrivate *priv = NM_MB_MENU_ITEM_GET_PRIVATE (self);

	priv->hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
	priv->desc = gtk_label_new (NULL);
	gtk_misc_set_alignment (GTK_MISC (priv->desc), 0.0, 0.5);

	gtk_container_add (GTK_CONTAINER (self), priv->hbox);
	gtk_box_pack_start (GTK_BOX (priv->hbox), priv->desc, TRUE, TRUE, 0);

	priv->strength = gtk_image_new ();
	gtk_box_pack_end (GTK_BOX (priv->hbox), priv->strength, FALSE, TRUE, 0);

	gtk_widget_show (priv->desc);
	gtk_widget_show (priv->strength);
	gtk_widget_show (priv->hbox);
#else
	gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (self), TRUE);
#endif
}

static void
finalize (GObject *object)
{
	g_free (NM_MB_MENU_ITEM_GET_PRIVATE (object)->desc_string);

	G_OBJECT_CLASS (nm_mb_menu_item_parent_class)->finalize (object);
}

static void
nm_mb_menu_item_class_init (NMMbMenuItemClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	g_type_class_add_private (klass, sizeof (NMMbMenuItemPrivate));

	/* virtual methods */
	object_class->finalize = finalize;
}