~elementary-os/elementaryos/os-patch-gnome-control-center-precise

« back to all changes in this revision

Viewing changes to panels/network/network-dialogs.c

  • Committer: Sergey "Shnatsel" Davidoff
  • Date: 2012-05-18 13:02:50 UTC
  • Revision ID: shnatsel@gmail.com-20120518130250-2u99ldq61a42rbt7
Initial import, version 1:3.4.1-0ubuntu2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2011 Giovanni Campagna <scampa.giovanni@gmail.com>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
18
 *
 
19
 * Portions of this code were taken from network-manager-applet.
 
20
 * Copyright 2008 - 2011 Red Hat, Inc. 
 
21
 */
 
22
 
 
23
#include <shell/cc-shell.h>
 
24
#include <nm-utils.h>
 
25
#include <nm-connection.h>
 
26
#include <nm-setting-gsm.h>
 
27
#include <nm-setting-cdma.h>
 
28
#include <nm-setting-serial.h>
 
29
#include <nm-device-modem.h>
 
30
 
 
31
#include "network-dialogs.h"
 
32
#include "nm-wireless-dialog.h"
 
33
#include "nm-mobile-wizard.h"
 
34
 
 
35
typedef struct {
 
36
        NMClient *client;
 
37
        NMRemoteSettings *settings;
 
38
} WirelessDialogClosure;
 
39
 
 
40
typedef struct {
 
41
        NMClient *client;
 
42
        NMRemoteSettings *settings;
 
43
        NMDevice *device;
 
44
} MobileDialogClosure;
 
45
 
 
46
static void
 
47
wireless_dialog_closure_closure_notify (gpointer data,
 
48
                                        GClosure *gclosure)
 
49
{
 
50
        WirelessDialogClosure *closure = data;
 
51
        g_object_unref (closure->client);
 
52
        g_object_unref (closure->settings);
 
53
 
 
54
        g_slice_free (WirelessDialogClosure, data);
 
55
}
 
56
 
 
57
static void
 
58
mobile_dialog_closure_free (gpointer data)
 
59
{
 
60
        MobileDialogClosure *closure = data;
 
61
        g_object_unref (closure->client);
 
62
        g_object_unref (closure->settings);
 
63
        g_object_unref (closure->device);
 
64
 
 
65
        g_slice_free (MobileDialogClosure, data);
 
66
}
 
67
 
 
68
static gboolean
 
69
wifi_can_create_wifi_network (NMClient *client)
 
70
{
 
71
        NMClientPermissionResult perm;
 
72
 
 
73
        /* FIXME: check WIFI_SHARE_PROTECTED too, and make the wireless dialog
 
74
         * handle the permissions as well so that admins can restrict open network
 
75
         * creation separately from protected network creation.
 
76
         */
 
77
        perm = nm_client_get_permission_result (client, NM_CLIENT_PERMISSION_WIFI_SHARE_OPEN);
 
78
        if (perm == NM_CLIENT_PERMISSION_RESULT_YES || perm == NM_CLIENT_PERMISSION_RESULT_AUTH)
 
79
          return TRUE;
 
80
 
 
81
        return FALSE;
 
82
}
 
83
 
 
84
static void
 
85
activate_existing_cb (NMClient *client,
 
86
                      NMActiveConnection *active,
 
87
                      GError *error,
 
88
                      gpointer user_data)
 
89
{
 
90
        if (error)
 
91
                g_warning ("Failed to activate connection: (%d) %s", error->code, error->message);
 
92
}
 
93
 
 
94
static void
 
95
activate_new_cb (NMClient *client,
 
96
                 NMActiveConnection *active,
 
97
                 const char *connection_path,
 
98
                 GError *error,
 
99
                 gpointer user_data)
 
100
{
 
101
        if (error)
 
102
                g_warning ("Failed to add new connection: (%d) %s", error->code, error->message);
 
103
}
 
104
 
 
105
static void
 
106
nag_dialog_response_cb (GtkDialog *nag_dialog,
 
107
                        gint response,
 
108
                        gpointer user_data)
 
109
{
 
110
        NMAWirelessDialog *wireless_dialog = NMA_WIRELESS_DIALOG (user_data);
 
111
 
 
112
        if (response == GTK_RESPONSE_NO) {  /* user opted not to correct the warning */
 
113
                nma_wireless_dialog_set_nag_ignored (wireless_dialog, TRUE);
 
114
                gtk_dialog_response (GTK_DIALOG (wireless_dialog), GTK_RESPONSE_OK);
 
115
        }
 
116
}
 
117
 
 
118
static void
 
119
wireless_dialog_response_cb (GtkDialog *foo,
 
120
                             gint response,
 
121
                             gpointer user_data)
 
122
{
 
123
        NMAWirelessDialog *dialog = NMA_WIRELESS_DIALOG (foo);
 
124
        WirelessDialogClosure *closure = user_data;
 
125
        NMConnection *connection, *fuzzy_match = NULL;
 
126
        NMDevice *device;
 
127
        NMAccessPoint *ap;
 
128
        GSList *all, *iter;
 
129
 
 
130
        if (response != GTK_RESPONSE_OK)
 
131
                goto done;
 
132
 
 
133
        if (!nma_wireless_dialog_get_nag_ignored (dialog)) {
 
134
                GtkWidget *nag_dialog;
 
135
 
 
136
                /* Nag the user about certificates or whatever.  Only destroy the dialog
 
137
                 * if no nagging was done.
 
138
                 */
 
139
                nag_dialog = nma_wireless_dialog_nag_user (dialog);
 
140
                if (nag_dialog) {
 
141
                        gtk_window_set_transient_for (GTK_WINDOW (nag_dialog), GTK_WINDOW (dialog));
 
142
                        g_signal_connect (nag_dialog, "response",
 
143
                                          G_CALLBACK (nag_dialog_response_cb),
 
144
                                          dialog);
 
145
                        return;
 
146
                }
 
147
        }
 
148
 
 
149
        /* nma_wireless_dialog_get_connection() returns a connection with the
 
150
         * refcount incremented, so the caller must remember to unref it.
 
151
         */
 
152
        connection = nma_wireless_dialog_get_connection (dialog, &device, &ap);
 
153
        g_assert (connection);
 
154
        g_assert (device);
 
155
 
 
156
        /* Find a similar connection and use that instead */
 
157
        all = nm_remote_settings_list_connections (closure->settings);
 
158
        for (iter = all; iter; iter = g_slist_next (iter)) {
 
159
                if (nm_connection_compare (connection,
 
160
                                           NM_CONNECTION (iter->data),
 
161
                                           (NM_SETTING_COMPARE_FLAG_FUZZY | NM_SETTING_COMPARE_FLAG_IGNORE_ID))) {
 
162
                        fuzzy_match = NM_CONNECTION (iter->data);
 
163
                        break;
 
164
                }
 
165
        }
 
166
        g_slist_free (all);
 
167
 
 
168
        if (fuzzy_match) {
 
169
                nm_client_activate_connection (closure->client,
 
170
                                               fuzzy_match,
 
171
                                               device,
 
172
                                               ap ? nm_object_get_path (NM_OBJECT (ap)) : NULL,
 
173
                                               activate_existing_cb,
 
174
                                               NULL);
 
175
        } else {
 
176
                NMSetting *s_con;
 
177
                NMSettingWireless *s_wifi;
 
178
                const char *mode = NULL;
 
179
 
 
180
                /* Entirely new connection */
 
181
 
 
182
                /* Don't autoconnect adhoc networks by default for now */
 
183
                s_wifi = (NMSettingWireless *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS);
 
184
                if (s_wifi)
 
185
                        mode = nm_setting_wireless_get_mode (s_wifi);
 
186
                if (g_strcmp0 (mode, "adhoc") == 0) {
 
187
                        s_con = nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
 
188
                        if (!s_con) {
 
189
                                s_con = nm_setting_connection_new ();
 
190
                                nm_connection_add_setting (connection, s_con);
 
191
                        }
 
192
                        g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_AUTOCONNECT, FALSE, NULL);
 
193
                }
 
194
 
 
195
                nm_client_add_and_activate_connection (closure->client,
 
196
                                                       connection,
 
197
                                                       device,
 
198
                                                       ap ? nm_object_get_path (NM_OBJECT (ap)) : NULL,
 
199
                                                       activate_new_cb,
 
200
                                                       NULL);
 
201
        }
 
202
 
 
203
        /* Balance nma_wireless_dialog_get_connection() */
 
204
        g_object_unref (connection);
 
205
 
 
206
done:
 
207
        gtk_widget_hide (GTK_WIDGET (dialog));
 
208
        gtk_widget_destroy (GTK_WIDGET (dialog));
 
209
}
 
210
 
 
211
static void
 
212
show_wireless_dialog (CcNetworkPanel   *panel,
 
213
                      NMClient         *client,
 
214
                      NMRemoteSettings *settings,
 
215
                      GtkWidget        *dialog)
 
216
{
 
217
        GtkWidget *toplevel = cc_shell_get_toplevel (cc_panel_get_shell (CC_PANEL (panel)));
 
218
        WirelessDialogClosure *closure;
 
219
 
 
220
        g_assert (gtk_widget_is_toplevel (toplevel));
 
221
        g_object_set (G_OBJECT (dialog),
 
222
                      "modal", TRUE,
 
223
                      "transient-for", toplevel,
 
224
                      NULL);
 
225
 
 
226
        closure = g_slice_new (WirelessDialogClosure);
 
227
        closure->client = g_object_ref (client);
 
228
        closure->settings = g_object_ref (settings);
 
229
        g_signal_connect_data (dialog, "response",
 
230
                               G_CALLBACK (wireless_dialog_response_cb),
 
231
                               closure, wireless_dialog_closure_closure_notify, 0);
 
232
 
 
233
        gtk_widget_show (dialog);
 
234
}
 
235
 
 
236
void
 
237
cc_network_panel_create_wifi_network (CcNetworkPanel   *panel,
 
238
                                      NMClient         *client,
 
239
                                      NMRemoteSettings *settings)
 
240
{
 
241
  if (wifi_can_create_wifi_network (client)) {
 
242
          show_wireless_dialog (panel, client, settings,
 
243
                                nma_wireless_dialog_new_for_create (client, settings));
 
244
  }
 
245
}
 
246
 
 
247
void
 
248
cc_network_panel_connect_to_hidden_network (CcNetworkPanel   *panel,
 
249
                                            NMClient         *client,
 
250
                                            NMRemoteSettings *settings)
 
251
{
 
252
        show_wireless_dialog (panel, client, settings,
 
253
                              nma_wireless_dialog_new_for_other (client, settings));
 
254
}
 
255
 
 
256
void
 
257
cc_network_panel_connect_to_8021x_network (CcNetworkPanel   *panel,
 
258
                                           NMClient         *client,
 
259
                                           NMRemoteSettings *settings,
 
260
                                           NMDevice         *device,
 
261
                                           NMAccessPoint    *ap)
 
262
{
 
263
        NMConnection *connection;
 
264
        NMSettingConnection *s_con;
 
265
        NMSettingWireless *s_wifi;
 
266
        NMSettingWirelessSecurity *s_wsec;
 
267
        NMSetting8021x *s_8021x;
 
268
        NM80211ApSecurityFlags wpa_flags, rsn_flags;
 
269
        GtkWidget *dialog;
 
270
        char *uuid;
 
271
 
 
272
        /* If the AP is WPA[2]-Enterprise then we need to set up a minimal 802.1x
 
273
         * setting and ask the user for more information.
 
274
         */
 
275
        rsn_flags = nm_access_point_get_rsn_flags (ap);
 
276
        wpa_flags = nm_access_point_get_wpa_flags (ap);
 
277
        if (!(rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)
 
278
            && !(wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)) {
 
279
                g_warning ("Network panel loaded with connect-8021x-wifi but the "
 
280
                           "access point does not support 802.1x");
 
281
                return;
 
282
        }
 
283
 
 
284
        connection = nm_connection_new ();
 
285
 
 
286
        /* Need a UUID for the "always ask" stuff in the Dialog of Doom */
 
287
        s_con = (NMSettingConnection *) nm_setting_connection_new ();
 
288
        uuid = nm_utils_uuid_generate ();
 
289
        g_object_set (s_con, NM_SETTING_CONNECTION_UUID, uuid, NULL);
 
290
        g_free (uuid);
 
291
        nm_connection_add_setting (connection, NM_SETTING (s_con));
 
292
 
 
293
        s_wifi = (NMSettingWireless *) nm_setting_wireless_new ();
 
294
        nm_connection_add_setting (connection, NM_SETTING (s_wifi));
 
295
        g_object_set (s_wifi,
 
296
                      NM_SETTING_WIRELESS_SSID, nm_access_point_get_ssid (ap),
 
297
                      NM_SETTING_WIRELESS_SEC, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME,
 
298
                      NULL);
 
299
 
 
300
        s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new ();
 
301
        g_object_set (s_wsec, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-eap", NULL);
 
302
        nm_connection_add_setting (connection, NM_SETTING (s_wsec));
 
303
 
 
304
        s_8021x = (NMSetting8021x *) nm_setting_802_1x_new ();
 
305
        nm_setting_802_1x_add_eap_method (s_8021x, "ttls");
 
306
        g_object_set (s_8021x, NM_SETTING_802_1X_PHASE2_AUTH, "mschapv2", NULL);
 
307
        nm_connection_add_setting (connection, NM_SETTING (s_8021x));
 
308
 
 
309
        dialog = nma_wireless_dialog_new (client, settings, connection, device, ap, FALSE);
 
310
        show_wireless_dialog (panel, client, settings, dialog);
 
311
}
 
312
 
 
313
static void
 
314
connect_3g (NMConnection *connection,
 
315
            gboolean canceled,
 
316
            gpointer user_data)
 
317
{
 
318
        MobileDialogClosure *closure = user_data;
 
319
 
 
320
        if (canceled == FALSE) {
 
321
                g_return_if_fail (connection != NULL);
 
322
 
 
323
                /* Ask NM to add the new connection and activate it; NM will fill in the
 
324
                 * missing details based on the specific object and the device.
 
325
                 */
 
326
                nm_client_add_and_activate_connection (closure->client,
 
327
                                                       connection,
 
328
                                                       closure->device,
 
329
                                                       "/",
 
330
                                                       activate_new_cb,
 
331
                                                       NULL);
 
332
        }
 
333
 
 
334
        mobile_dialog_closure_free (closure);
 
335
}
 
336
 
 
337
static void
 
338
cdma_mobile_wizard_done (NMAMobileWizard *wizard,
 
339
                         gboolean canceled,
 
340
                         NMAMobileWizardAccessMethod *method,
 
341
                         gpointer user_data)
 
342
{
 
343
        NMConnection *connection = NULL;
 
344
 
 
345
        if (!canceled && method) {
 
346
                NMSetting *setting;
 
347
                char *uuid, *id;
 
348
 
 
349
                if (method->devtype != NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO) {
 
350
                        g_warning ("Unexpected device type (not CDMA).");
 
351
                        canceled = TRUE;
 
352
                        goto done;
 
353
                }
 
354
 
 
355
                connection = nm_connection_new ();
 
356
 
 
357
                setting = nm_setting_cdma_new ();
 
358
                g_object_set (setting,
 
359
                              NM_SETTING_CDMA_NUMBER, "#777",
 
360
                              NM_SETTING_CDMA_USERNAME, method->username,
 
361
                              NM_SETTING_CDMA_PASSWORD, method->password,
 
362
                              NULL);
 
363
                nm_connection_add_setting (connection, setting);
 
364
 
 
365
                /* Serial setting */
 
366
                setting = nm_setting_serial_new ();
 
367
                g_object_set (setting,
 
368
                              NM_SETTING_SERIAL_BAUD, 115200,
 
369
                              NM_SETTING_SERIAL_BITS, 8,
 
370
                              NM_SETTING_SERIAL_PARITY, 'n',
 
371
                              NM_SETTING_SERIAL_STOPBITS, 1,
 
372
                              NULL);
 
373
                nm_connection_add_setting (connection, setting);
 
374
 
 
375
                nm_connection_add_setting (connection, nm_setting_ppp_new ());
 
376
 
 
377
                setting = nm_setting_connection_new ();
 
378
                if (method->plan_name)
 
379
                        id = g_strdup_printf ("%s %s", method->provider_name, method->plan_name);
 
380
                else
 
381
                        id = g_strdup_printf ("%s connection", method->provider_name);
 
382
                uuid = nm_utils_uuid_generate ();
 
383
                g_object_set (setting,
 
384
                              NM_SETTING_CONNECTION_ID, id,
 
385
                              NM_SETTING_CONNECTION_TYPE, NM_SETTING_CDMA_SETTING_NAME,
 
386
                              NM_SETTING_CONNECTION_AUTOCONNECT, FALSE,
 
387
                              NM_SETTING_CONNECTION_UUID, uuid,
 
388
                              NULL);
 
389
                g_free (uuid);
 
390
                g_free (id);
 
391
                nm_connection_add_setting (connection, setting);
 
392
        }
 
393
 
 
394
 done:
 
395
        connect_3g (connection, canceled, user_data);
 
396
        nma_mobile_wizard_destroy (wizard);
 
397
}
 
398
 
 
399
static void
 
400
gsm_mobile_wizard_done (NMAMobileWizard *wizard,
 
401
                        gboolean canceled,
 
402
                        NMAMobileWizardAccessMethod *method,
 
403
                        gpointer user_data)
 
404
{
 
405
        NMConnection *connection = NULL;
 
406
 
 
407
        if (!canceled && method) {
 
408
                NMSetting *setting;
 
409
                char *uuid, *id;
 
410
 
 
411
                if (method->devtype != NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) {
 
412
                        g_warning ("Unexpected device type (not GSM).");
 
413
                        canceled = TRUE;
 
414
                        goto done;
 
415
                }
 
416
 
 
417
                connection = nm_connection_new ();
 
418
 
 
419
                setting = nm_setting_gsm_new ();
 
420
                g_object_set (setting,
 
421
                              NM_SETTING_GSM_NUMBER, "*99#",
 
422
                              NM_SETTING_GSM_USERNAME, method->username,
 
423
                              NM_SETTING_GSM_PASSWORD, method->password,
 
424
                              NM_SETTING_GSM_APN, method->gsm_apn,
 
425
                              NULL);
 
426
                nm_connection_add_setting (connection, setting);
 
427
 
 
428
                /* Serial setting */
 
429
                setting = nm_setting_serial_new ();
 
430
                g_object_set (setting,
 
431
                              NM_SETTING_SERIAL_BAUD, 115200,
 
432
                              NM_SETTING_SERIAL_BITS, 8,
 
433
                              NM_SETTING_SERIAL_PARITY, 'n',
 
434
                              NM_SETTING_SERIAL_STOPBITS, 1,
 
435
                              NULL);
 
436
                nm_connection_add_setting (connection, setting);
 
437
 
 
438
                nm_connection_add_setting (connection, nm_setting_ppp_new ());
 
439
 
 
440
                setting = nm_setting_connection_new ();
 
441
                if (method->plan_name)
 
442
                        id = g_strdup_printf ("%s %s", method->provider_name, method->plan_name);
 
443
                else
 
444
                        id = g_strdup_printf ("%s connection", method->provider_name);
 
445
                uuid = nm_utils_uuid_generate ();
 
446
                g_object_set (setting,
 
447
                              NM_SETTING_CONNECTION_ID, id,
 
448
                              NM_SETTING_CONNECTION_TYPE, NM_SETTING_GSM_SETTING_NAME,
 
449
                              NM_SETTING_CONNECTION_AUTOCONNECT, FALSE,
 
450
                              NM_SETTING_CONNECTION_UUID, uuid,
 
451
                              NULL);
 
452
                g_free (uuid);
 
453
                g_free (id);
 
454
                nm_connection_add_setting (connection, setting);
 
455
        }
 
456
 
 
457
done:
 
458
        connect_3g (connection, canceled, user_data);
 
459
        nma_mobile_wizard_destroy (wizard);
 
460
}
 
461
 
 
462
void
 
463
cc_network_panel_connect_to_3g_network (CcNetworkPanel   *panel,
 
464
                                        NMClient         *client,
 
465
                                        NMRemoteSettings *settings,
 
466
                                        NMDevice         *device)
 
467
{
 
468
        GtkWidget *toplevel = cc_shell_get_toplevel (cc_panel_get_shell (CC_PANEL (panel)));
 
469
        MobileDialogClosure *closure;
 
470
        NMAMobileWizard *wizard;
 
471
        NMDeviceModemCapabilities caps;
 
472
 
 
473
        if (!NM_IS_DEVICE_MODEM (device)) {
 
474
                g_warning ("Network panel loaded with connect-3g but the selected device"
 
475
                           " is not a modem");
 
476
                return;
 
477
        }
 
478
 
 
479
        closure = g_slice_new (MobileDialogClosure);
 
480
        closure->client = g_object_ref (client);
 
481
        closure->settings = g_object_ref (settings);
 
482
        closure->device = g_object_ref (device);
 
483
 
 
484
        caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (device));
 
485
        if (caps & NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) {
 
486
                wizard = nma_mobile_wizard_new (GTK_WINDOW (toplevel), NULL, NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS, FALSE,
 
487
                                                gsm_mobile_wizard_done, closure);
 
488
        } else if (caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO) {
 
489
                wizard = nma_mobile_wizard_new (GTK_WINDOW (toplevel), NULL, NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO, FALSE,
 
490
                                                cdma_mobile_wizard_done, closure);
 
491
        } else {
 
492
                g_warning ("Network panel loaded with connect-3g but the selected device"
 
493
                           " does not support GSM or CDMA");
 
494
                mobile_dialog_closure_free (closure);
 
495
                return;
 
496
        }
 
497
 
 
498
        nma_mobile_wizard_present (wizard);
 
499
}