1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
3
* Copyright (C) 2011 Giovanni Campagna <scampa.giovanni@gmail.com>
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.
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.
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.
19
* Portions of this code were taken from network-manager-applet.
20
* Copyright 2008 - 2011 Red Hat, Inc.
23
#include <shell/cc-shell.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>
31
#include "network-dialogs.h"
32
#include "nm-wireless-dialog.h"
33
#include "nm-mobile-wizard.h"
37
NMRemoteSettings *settings;
38
} WirelessDialogClosure;
42
NMRemoteSettings *settings;
44
} MobileDialogClosure;
47
wireless_dialog_closure_closure_notify (gpointer data,
50
WirelessDialogClosure *closure = data;
51
g_object_unref (closure->client);
52
g_object_unref (closure->settings);
54
g_slice_free (WirelessDialogClosure, data);
58
mobile_dialog_closure_free (gpointer data)
60
MobileDialogClosure *closure = data;
61
g_object_unref (closure->client);
62
g_object_unref (closure->settings);
63
g_object_unref (closure->device);
65
g_slice_free (MobileDialogClosure, data);
69
wifi_can_create_wifi_network (NMClient *client)
71
NMClientPermissionResult perm;
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.
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)
85
activate_existing_cb (NMClient *client,
86
NMActiveConnection *active,
91
g_warning ("Failed to activate connection: (%d) %s", error->code, error->message);
95
activate_new_cb (NMClient *client,
96
NMActiveConnection *active,
97
const char *connection_path,
102
g_warning ("Failed to add new connection: (%d) %s", error->code, error->message);
106
nag_dialog_response_cb (GtkDialog *nag_dialog,
110
NMAWirelessDialog *wireless_dialog = NMA_WIRELESS_DIALOG (user_data);
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);
119
wireless_dialog_response_cb (GtkDialog *foo,
123
NMAWirelessDialog *dialog = NMA_WIRELESS_DIALOG (foo);
124
WirelessDialogClosure *closure = user_data;
125
NMConnection *connection, *fuzzy_match = NULL;
130
if (response != GTK_RESPONSE_OK)
133
if (!nma_wireless_dialog_get_nag_ignored (dialog)) {
134
GtkWidget *nag_dialog;
136
/* Nag the user about certificates or whatever. Only destroy the dialog
137
* if no nagging was done.
139
nag_dialog = nma_wireless_dialog_nag_user (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),
149
/* nma_wireless_dialog_get_connection() returns a connection with the
150
* refcount incremented, so the caller must remember to unref it.
152
connection = nma_wireless_dialog_get_connection (dialog, &device, &ap);
153
g_assert (connection);
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);
169
nm_client_activate_connection (closure->client,
172
ap ? nm_object_get_path (NM_OBJECT (ap)) : NULL,
173
activate_existing_cb,
177
NMSettingWireless *s_wifi;
178
const char *mode = NULL;
180
/* Entirely new connection */
182
/* Don't autoconnect adhoc networks by default for now */
183
s_wifi = (NMSettingWireless *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS);
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);
189
s_con = nm_setting_connection_new ();
190
nm_connection_add_setting (connection, s_con);
192
g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_AUTOCONNECT, FALSE, NULL);
195
nm_client_add_and_activate_connection (closure->client,
198
ap ? nm_object_get_path (NM_OBJECT (ap)) : NULL,
203
/* Balance nma_wireless_dialog_get_connection() */
204
g_object_unref (connection);
207
gtk_widget_hide (GTK_WIDGET (dialog));
208
gtk_widget_destroy (GTK_WIDGET (dialog));
212
show_wireless_dialog (CcNetworkPanel *panel,
214
NMRemoteSettings *settings,
217
GtkWidget *toplevel = cc_shell_get_toplevel (cc_panel_get_shell (CC_PANEL (panel)));
218
WirelessDialogClosure *closure;
220
g_assert (gtk_widget_is_toplevel (toplevel));
221
g_object_set (G_OBJECT (dialog),
223
"transient-for", toplevel,
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);
233
gtk_widget_show (dialog);
237
cc_network_panel_create_wifi_network (CcNetworkPanel *panel,
239
NMRemoteSettings *settings)
241
if (wifi_can_create_wifi_network (client)) {
242
show_wireless_dialog (panel, client, settings,
243
nma_wireless_dialog_new_for_create (client, settings));
248
cc_network_panel_connect_to_hidden_network (CcNetworkPanel *panel,
250
NMRemoteSettings *settings)
252
show_wireless_dialog (panel, client, settings,
253
nma_wireless_dialog_new_for_other (client, settings));
257
cc_network_panel_connect_to_8021x_network (CcNetworkPanel *panel,
259
NMRemoteSettings *settings,
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;
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.
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");
284
connection = nm_connection_new ();
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);
291
nm_connection_add_setting (connection, NM_SETTING (s_con));
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,
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));
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));
309
dialog = nma_wireless_dialog_new (client, settings, connection, device, ap, FALSE);
310
show_wireless_dialog (panel, client, settings, dialog);
314
connect_3g (NMConnection *connection,
318
MobileDialogClosure *closure = user_data;
320
if (canceled == FALSE) {
321
g_return_if_fail (connection != NULL);
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.
326
nm_client_add_and_activate_connection (closure->client,
334
mobile_dialog_closure_free (closure);
338
cdma_mobile_wizard_done (NMAMobileWizard *wizard,
340
NMAMobileWizardAccessMethod *method,
343
NMConnection *connection = NULL;
345
if (!canceled && method) {
349
if (method->devtype != NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO) {
350
g_warning ("Unexpected device type (not CDMA).");
355
connection = nm_connection_new ();
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,
363
nm_connection_add_setting (connection, 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,
373
nm_connection_add_setting (connection, setting);
375
nm_connection_add_setting (connection, nm_setting_ppp_new ());
377
setting = nm_setting_connection_new ();
378
if (method->plan_name)
379
id = g_strdup_printf ("%s %s", method->provider_name, method->plan_name);
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,
391
nm_connection_add_setting (connection, setting);
395
connect_3g (connection, canceled, user_data);
396
nma_mobile_wizard_destroy (wizard);
400
gsm_mobile_wizard_done (NMAMobileWizard *wizard,
402
NMAMobileWizardAccessMethod *method,
405
NMConnection *connection = NULL;
407
if (!canceled && method) {
411
if (method->devtype != NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) {
412
g_warning ("Unexpected device type (not GSM).");
417
connection = nm_connection_new ();
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,
426
nm_connection_add_setting (connection, 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,
436
nm_connection_add_setting (connection, setting);
438
nm_connection_add_setting (connection, nm_setting_ppp_new ());
440
setting = nm_setting_connection_new ();
441
if (method->plan_name)
442
id = g_strdup_printf ("%s %s", method->provider_name, method->plan_name);
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,
454
nm_connection_add_setting (connection, setting);
458
connect_3g (connection, canceled, user_data);
459
nma_mobile_wizard_destroy (wizard);
463
cc_network_panel_connect_to_3g_network (CcNetworkPanel *panel,
465
NMRemoteSettings *settings,
468
GtkWidget *toplevel = cc_shell_get_toplevel (cc_panel_get_shell (CC_PANEL (panel)));
469
MobileDialogClosure *closure;
470
NMAMobileWizard *wizard;
471
NMDeviceModemCapabilities caps;
473
if (!NM_IS_DEVICE_MODEM (device)) {
474
g_warning ("Network panel loaded with connect-3g but the selected device"
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);
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);
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);
498
nma_mobile_wizard_present (wizard);