~ubuntu-branches/ubuntu/saucy/network-manager-applet/saucy-updates

« back to all changes in this revision

Viewing changes to .pc/position_dialogs_to_center_of_the_screen.patch/src/utils/utils.c

  • Committer: Package Import Robot
  • Author(s): Mathieu Trudel-Lapierre
  • Date: 2012-08-30 14:27:42 UTC
  • Revision ID: package-import@ubuntu.com-20120830142742-wy114kfhl2cy643j
Tags: 0.9.6.2-0ubuntu2
* debian/patches/position_dialogs_to_center_of_the_screen.patch: make sure
  created dialogs (like password requests) are properly centered on the
  screen. (LP: #1043934)
* debian/patches/make_menu_items_invisible_based_on_permissions.patch,
  debian/patches/hide_policy_items_env_var.patch: items that are disallowed
  by the effective PolicyKit policy for a user should be consistently showing
  as such: they should either be insensitive (greyed), or hidden altogether.
  (LP: #1043929)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
 
2
/* NetworkManager Wireless Applet -- Display wireless access points and allow user control
 
3
 *
 
4
 * Dan Williams <dcbw@redhat.com>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
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 along
 
17
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
18
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
19
 *
 
20
 * (C) Copyright 2007 - 2011 Red Hat, Inc.
 
21
 */
 
22
 
 
23
#include <config.h>
 
24
#include <string.h>
 
25
#include <netinet/ether.h>
 
26
#include <glib.h>
 
27
#include <glib/gi18n.h>
 
28
#include <gtk/gtk.h>
 
29
 
 
30
#include <nm-setting-connection.h>
 
31
#include <nm-utils.h>
 
32
 
 
33
#include "utils.h"
 
34
 
 
35
static char *ignored_words[] = {
 
36
        "Semiconductor",
 
37
        "Components",
 
38
        "Corporation",
 
39
        "Communications",
 
40
        "Company",
 
41
        "Corp.",
 
42
        "Corp",
 
43
        "Co.",
 
44
        "Inc.",
 
45
        "Inc",
 
46
        "Incorporated",
 
47
        "Ltd.",
 
48
        "Limited.",
 
49
        "Intel?",
 
50
        "chipset",
 
51
        "adapter",
 
52
        "[hex]",
 
53
        "NDIS",
 
54
        "Module",
 
55
        NULL
 
56
};
 
57
 
 
58
static char *ignored_phrases[] = {
 
59
        "Multiprotocol MAC/baseband processor",
 
60
        "Wireless LAN Controller",
 
61
        "Wireless LAN Adapter",
 
62
        "Wireless Adapter",
 
63
        "Network Connection",
 
64
        "Wireless Cardbus Adapter",
 
65
        "Wireless CardBus Adapter",
 
66
        "54 Mbps Wireless PC Card",
 
67
        "Wireless PC Card",
 
68
        "Wireless PC",
 
69
        "PC Card with XJACK(r) Antenna",
 
70
        "Wireless cardbus",
 
71
        "Wireless LAN PC Card",
 
72
        "Technology Group Ltd.",
 
73
        "Communication S.p.A.",
 
74
        "Business Mobile Networks BV",
 
75
        "Mobile Broadband Minicard Composite Device",
 
76
        "Mobile Communications AB",
 
77
        "(PC-Suite Mode)",
 
78
        NULL
 
79
};
 
80
 
 
81
static char *
 
82
fixup_desc_string (const char *desc)
 
83
{
 
84
        char *p, *temp;
 
85
        char **words, **item;
 
86
        GString *str;
 
87
 
 
88
        p = temp = g_strdup (desc);
 
89
        while (*p) {
 
90
                if (*p == '_' || *p == ',')
 
91
                        *p = ' ';
 
92
                p++;
 
93
        }
 
94
 
 
95
        /* Attempt to shorten ID by ignoring certain phrases */
 
96
        for (item = ignored_phrases; *item; item++) {
 
97
                guint32 ignored_len = strlen (*item);
 
98
 
 
99
                p = strstr (temp, *item);
 
100
                if (p)
 
101
                        memmove (p, p + ignored_len, strlen (p + ignored_len) + 1); /* +1 for the \0 */
 
102
        }
 
103
 
 
104
        /* Attmept to shorten ID by ignoring certain individual words */
 
105
        words = g_strsplit (temp, " ", 0);
 
106
        str = g_string_new_len (NULL, strlen (temp));
 
107
        g_free (temp);
 
108
 
 
109
        for (item = words; *item; item++) {
 
110
                int i = 0;
 
111
                gboolean ignore = FALSE;
 
112
 
 
113
                if (g_ascii_isspace (**item) || (**item == '\0'))
 
114
                        continue;
 
115
 
 
116
                while (ignored_words[i] && !ignore) {
 
117
                        if (!strcmp (*item, ignored_words[i]))
 
118
                                ignore = TRUE;
 
119
                        i++;
 
120
                }
 
121
 
 
122
                if (!ignore) {
 
123
                        if (str->len)
 
124
                                g_string_append_c (str, ' ');
 
125
                        g_string_append (str, *item);
 
126
                }
 
127
        }
 
128
        g_strfreev (words);
 
129
 
 
130
        temp = str->str;
 
131
        g_string_free (str, FALSE);
 
132
 
 
133
        return temp;
 
134
}
 
135
 
 
136
#define DESC_TAG "description"
 
137
 
 
138
const char *
 
139
utils_get_device_description (NMDevice *device)
 
140
{
 
141
        char *description = NULL;
 
142
        const char *dev_product;
 
143
        const char *dev_vendor;
 
144
        char *product = NULL;
 
145
        char *vendor = NULL;
 
146
        GString *str;
 
147
 
 
148
        g_return_val_if_fail (device != NULL, NULL);
 
149
 
 
150
        description = g_object_get_data (G_OBJECT (device), DESC_TAG);
 
151
        if (description)
 
152
                return description;
 
153
 
 
154
        dev_product = nm_device_get_product (device);
 
155
        dev_vendor = nm_device_get_vendor (device);
 
156
        if (!dev_product || !dev_vendor)
 
157
                return NULL;
 
158
 
 
159
        product = fixup_desc_string (dev_product);
 
160
        vendor = fixup_desc_string (dev_vendor);
 
161
 
 
162
        str = g_string_new_len (NULL, strlen (vendor) + strlen (product) + 1);
 
163
 
 
164
        /* Another quick hack; if all of the fixed up vendor string
 
165
         * is found in product, ignore the vendor.
 
166
         */
 
167
        if (!strcasestr (product, vendor)) {
 
168
                g_string_append (str, vendor);
 
169
                g_string_append_c (str, ' ');
 
170
        }
 
171
 
 
172
        g_string_append (str, product);
 
173
        g_free (product);
 
174
        g_free (vendor);
 
175
 
 
176
        description = str->str;
 
177
        g_string_free (str, FALSE);
 
178
 
 
179
        g_object_set_data_full (G_OBJECT (device),
 
180
                                "description", description,
 
181
                                (GDestroyNotify) g_free);
 
182
 
 
183
        return description;
 
184
}
 
185
 
 
186
/*
 
187
 * utils_ether_addr_valid
 
188
 *
 
189
 * Compares an Ethernet address against known invalid addresses.
 
190
 *
 
191
 */
 
192
gboolean
 
193
utils_ether_addr_valid (const struct ether_addr *test_addr)
 
194
{
 
195
        guint8 invalid_addr1[ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
 
196
        guint8 invalid_addr2[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
 
197
        guint8 invalid_addr3[ETH_ALEN] = {0x44, 0x44, 0x44, 0x44, 0x44, 0x44};
 
198
        guint8 invalid_addr4[ETH_ALEN] = {0x00, 0x30, 0xb4, 0x00, 0x00, 0x00}; /* prism54 dummy MAC */
 
199
 
 
200
        g_return_val_if_fail (test_addr != NULL, FALSE);
 
201
 
 
202
        /* Compare the AP address the card has with invalid ethernet MAC addresses. */
 
203
        if (!memcmp (test_addr->ether_addr_octet, &invalid_addr1, ETH_ALEN))
 
204
                return FALSE;
 
205
 
 
206
        if (!memcmp (test_addr->ether_addr_octet, &invalid_addr2, ETH_ALEN))
 
207
                return FALSE;
 
208
 
 
209
        if (!memcmp (test_addr->ether_addr_octet, &invalid_addr3, ETH_ALEN))
 
210
                return FALSE;
 
211
 
 
212
        if (!memcmp (test_addr->ether_addr_octet, &invalid_addr4, ETH_ALEN))
 
213
                return FALSE;
 
214
 
 
215
        if (test_addr->ether_addr_octet[0] & 1)                 /* Multicast addresses */
 
216
                return FALSE;
 
217
        
 
218
        return TRUE;
 
219
}
 
220
 
 
221
char *
 
222
utils_hash_ap (const GByteArray *ssid,
 
223
               NM80211Mode mode,
 
224
               guint32 flags,
 
225
               guint32 wpa_flags,
 
226
               guint32 rsn_flags)
 
227
{
 
228
        unsigned char input[66];
 
229
 
 
230
        memset (&input[0], 0, sizeof (input));
 
231
 
 
232
        if (ssid)
 
233
                memcpy (input, ssid->data, ssid->len);
 
234
 
 
235
        if (mode == NM_802_11_MODE_INFRA)
 
236
                input[32] |= (1 << 0);
 
237
        else if (mode == NM_802_11_MODE_ADHOC)
 
238
                input[32] |= (1 << 1);
 
239
        else
 
240
                input[32] |= (1 << 2);
 
241
 
 
242
        /* Separate out no encryption, WEP-only, and WPA-capable */
 
243
        if (  !(flags & NM_802_11_AP_FLAGS_PRIVACY)
 
244
            && (wpa_flags == NM_802_11_AP_SEC_NONE)
 
245
            && (rsn_flags == NM_802_11_AP_SEC_NONE))
 
246
                input[32] |= (1 << 3);
 
247
        else if (   (flags & NM_802_11_AP_FLAGS_PRIVACY)
 
248
                 && (wpa_flags == NM_802_11_AP_SEC_NONE)
 
249
                 && (rsn_flags == NM_802_11_AP_SEC_NONE))
 
250
                input[32] |= (1 << 4);
 
251
        else if (   !(flags & NM_802_11_AP_FLAGS_PRIVACY)
 
252
                 &&  (wpa_flags != NM_802_11_AP_SEC_NONE)
 
253
                 &&  (rsn_flags != NM_802_11_AP_SEC_NONE))
 
254
                input[32] |= (1 << 5);
 
255
        else
 
256
                input[32] |= (1 << 6);
 
257
 
 
258
        /* duplicate it */
 
259
        memcpy (&input[33], &input[0], 32);
 
260
        return g_compute_checksum_for_data (G_CHECKSUM_MD5, input, sizeof (input));
 
261
}
 
262
 
 
263
typedef struct {
 
264
        const char *tag;
 
265
        const char *replacement;
 
266
} Tag;
 
267
 
 
268
static Tag escaped_tags[] = {
 
269
        { "<center>", NULL },
 
270
        { "</center>", NULL },
 
271
        { "<p>", "\n" },
 
272
        { "</p>", NULL },
 
273
        { "<B>", "<b>" },
 
274
        { "</B>", "</b>" },
 
275
        { "<I>", "<i>" },
 
276
        { "</I>", "</i>" },
 
277
        { "<u>", "<u>" },
 
278
        { "</u>", "</u>" },
 
279
        { "&", "&amp;" },
 
280
        { NULL, NULL }
 
281
};
 
282
 
 
283
char *
 
284
utils_escape_notify_message (const char *src)
 
285
{
 
286
        const char *p = src;
 
287
        GString *escaped;
 
288
 
 
289
        /* Filter the source text and get rid of some HTML tags since the
 
290
         * notification spec only allows a subset of HTML.  Substitute
 
291
         * HTML code for characters like & that are invalid in HTML.
 
292
         */
 
293
 
 
294
        escaped = g_string_sized_new (strlen (src) + 5);
 
295
        while (*p) {
 
296
                Tag *t = &escaped_tags[0];
 
297
                gboolean found = FALSE;
 
298
 
 
299
                while (t->tag) {
 
300
                        if (strncasecmp (p, t->tag, strlen (t->tag)) == 0) {
 
301
                                p += strlen (t->tag);
 
302
                                if (t->replacement)
 
303
                                        g_string_append (escaped, t->replacement);
 
304
                                found = TRUE;
 
305
                                break;
 
306
                        }
 
307
                        t++;
 
308
                }
 
309
                if (!found)
 
310
                        g_string_append_c (escaped, *p++);
 
311
        }
 
312
 
 
313
        return g_string_free (escaped, FALSE);
 
314
}
 
315
 
 
316
char *
 
317
utils_create_mobile_connection_id (const char *provider, const char *plan_name)
 
318
{
 
319
        g_return_val_if_fail (provider != NULL, NULL);
 
320
 
 
321
        if (plan_name)
 
322
                return g_strdup_printf ("%s %s", provider, plan_name);
 
323
 
 
324
        /* The %s is a mobile provider name, eg "T-Mobile" */
 
325
        return g_strdup_printf (_("%s connection"), provider);
 
326
}
 
327
 
 
328
void
 
329
utils_show_error_dialog (const char *title,
 
330
                         const char *text1,
 
331
                         const char *text2,
 
332
                         gboolean modal,
 
333
                         GtkWindow *parent)
 
334
{
 
335
        GtkWidget *err_dialog;
 
336
 
 
337
        g_return_if_fail (text1 != NULL);
 
338
 
 
339
        err_dialog = gtk_message_dialog_new (parent,
 
340
                                             GTK_DIALOG_DESTROY_WITH_PARENT,
 
341
                                             GTK_MESSAGE_ERROR,
 
342
                                             GTK_BUTTONS_CLOSE,
 
343
                                             "%s",
 
344
                                             text1);
 
345
 
 
346
        if (text2)
 
347
                gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (err_dialog), "%s", text2);
 
348
        if (title)
 
349
                gtk_window_set_title (GTK_WINDOW (err_dialog), title);
 
350
 
 
351
        if (modal) {
 
352
                gtk_dialog_run (GTK_DIALOG (err_dialog));
 
353
                gtk_widget_destroy (err_dialog);
 
354
        } else {
 
355
                g_signal_connect (err_dialog, "delete-event", G_CALLBACK (gtk_widget_destroy), NULL);
 
356
                g_signal_connect (err_dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL);
 
357
 
 
358
                gtk_widget_show_all (err_dialog);
 
359
                gtk_window_present (GTK_WINDOW (err_dialog));
 
360
        }
 
361
}
 
362