~dylanmccall/ubuntu/oneiric/network-manager-applet/lp852961-disable-autostart-for-gnome-shell

« back to all changes in this revision

Viewing changes to src/applet-device-cdma.c

  • Committer: Bazaar Package Importer
  • Author(s): Mathieu Trudel-Lapierre
  • Date: 2011-05-30 13:25:18 UTC
  • mto: This revision was merged to the branch mainline in revision 68.
  • Revision ID: james.westby@ubuntu.com-20110530132518-ya5i5mcrl8szsmoj
Tags: upstream-0.8.9997+git.20110529t170033.9ec4c5d
ImportĀ upstreamĀ versionĀ 0.8.9997+git.20110529t170033.9ec4c5d

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 * with this program; if not, write to the Free Software Foundation, Inc.,
18
18
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
19
 *
20
 
 * (C) Copyright 2008 - 2010 Red Hat, Inc.
 
20
 * (C) Copyright 2008 - 2011 Red Hat, Inc.
21
21
 */
22
22
 
23
23
#ifdef HAVE_CONFIG_H
32
32
#include <nm-setting-cdma.h>
33
33
#include <nm-setting-serial.h>
34
34
#include <nm-setting-ppp.h>
35
 
#include <nm-cdma-device.h>
 
35
#include <nm-device-modem.h>
36
36
#include <nm-utils.h>
 
37
#include <nm-secret-agent.h>
37
38
 
38
39
#include "applet.h"
39
40
#include "applet-device-cdma.h"
48
49
        NMApplet *applet;
49
50
        NMDevice *device;
50
51
 
 
52
        DBusGConnection *bus;
51
53
        DBusGProxy *props_proxy;
52
54
        DBusGProxy *cdma_proxy;
53
55
        gboolean quality_valid;
104
106
                NMSetting *setting;
105
107
                char *uuid, *id;
106
108
 
107
 
                if (method->devtype != NM_DEVICE_TYPE_CDMA) {
 
109
                if (method->devtype != NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO) {
108
110
                        g_warning ("Unexpected device type (not CDMA).");
109
111
                        canceled = TRUE;
110
112
                        goto done;
158
160
}
159
161
 
160
162
static gboolean
161
 
cdma_new_auto_connection (NMDevice *device,
162
 
                          gpointer dclass_data,
163
 
                          AppletNewAutoConnectionCallback callback,
164
 
                          gpointer callback_data)
 
163
do_mobile_wizard (AppletNewAutoConnectionCallback callback,
 
164
                  gpointer callback_data)
165
165
{
166
166
        MobileWizard *wizard;
167
167
        AutoCdmaWizardInfo *info;
171
171
        info->callback = callback;
172
172
        info->callback_data = callback_data;
173
173
 
174
 
        wizard = mobile_wizard_new (NULL, NULL, NM_DEVICE_TYPE_CDMA, FALSE,
 
174
        wizard = mobile_wizard_new (NULL, NULL, NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO, FALSE,
175
175
                                    mobile_wizard_done, info);
176
176
        if (wizard) {
177
177
                mobile_wizard_present (wizard);
180
180
 
181
181
        /* Fall back to something */
182
182
        method = g_malloc0 (sizeof (MobileWizardAccessMethod));
183
 
        method->devtype = NM_DEVICE_TYPE_CDMA;
 
183
        method->devtype = NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO;
184
184
        method->provider_name = _("CDMA");
185
185
        mobile_wizard_done (NULL, FALSE, method, info);
186
186
        g_free (method);
188
188
        return TRUE;
189
189
}
190
190
 
 
191
static gboolean
 
192
cdma_new_auto_connection (NMDevice *device,
 
193
                          gpointer dclass_data,
 
194
                          AppletNewAutoConnectionCallback callback,
 
195
                          gpointer callback_data)
 
196
{
 
197
        return do_mobile_wizard (callback, callback_data);
 
198
}
 
199
 
 
200
static void
 
201
dbus_3g_add_and_activate_cb (NMClient *client,
 
202
                             NMActiveConnection *active,
 
203
                             const char *connection_path,
 
204
                             GError *error,
 
205
                             gpointer user_data)
 
206
{
 
207
        if (error)
 
208
                g_warning ("Failed to add/activate connection: (%d) %s", error->code, error->message);
 
209
}
 
210
 
 
211
typedef struct {
 
212
        NMApplet *applet;
 
213
        NMDevice *device;
 
214
} Dbus3gInfo;
 
215
 
 
216
static void
 
217
dbus_connect_3g_cb (NMConnection *connection,
 
218
                    gboolean auto_created,
 
219
                    gboolean canceled,
 
220
                    gpointer user_data)
 
221
{
 
222
        Dbus3gInfo *info = user_data;
 
223
 
 
224
        if (canceled == FALSE) {
 
225
                g_return_if_fail (connection != NULL);
 
226
 
 
227
                /* Ask NM to add the new connection and activate it; NM will fill in the
 
228
                 * missing details based on the specific object and the device.
 
229
                 */
 
230
                nm_client_add_and_activate_connection (info->applet->nm_client,
 
231
                                                       connection,
 
232
                                                       info->device,
 
233
                                                       "/",
 
234
                                                       dbus_3g_add_and_activate_cb,
 
235
                                                       info->applet);
 
236
        }
 
237
 
 
238
        g_object_unref (info->device);
 
239
        memset (info, 0, sizeof (*info));
 
240
        g_free (info);
 
241
}
 
242
 
 
243
void
 
244
applet_cdma_connect_network (NMApplet *applet, NMDevice *device)
 
245
{
 
246
        Dbus3gInfo *info;
 
247
 
 
248
        info = g_malloc0 (sizeof (*info));
 
249
        info->applet = applet;
 
250
        info->device = g_object_ref (device);
 
251
 
 
252
        do_mobile_wizard (dbus_connect_3g_cb, info);
 
253
}
 
254
 
191
255
static void
192
256
cdma_menu_item_activate (GtkMenuItem *item, gpointer user_data)
193
257
{
299
363
                item = nm_mb_menu_item_new (nm_setting_connection_get_id (s_con),
300
364
                                            info->quality_valid ? info->quality : 0,
301
365
                                            info->provider_name,
 
366
                                            TRUE,
302
367
                                            cdma_act_to_mb_act (info),
303
368
                                            cdma_state_to_mb_state (info),
304
369
                                            info->modem_enabled,
305
370
                                            applet);
306
 
 
 
371
                gtk_widget_set_sensitive (GTK_WIDGET (item), TRUE);
307
372
                add_connection_item (device, active, item, menu, applet);
308
373
        }
309
374
 
319
384
                item = nm_mb_menu_item_new (NULL,
320
385
                                            info->quality_valid ? info->quality : 0,
321
386
                                            info->provider_name,
 
387
                                            FALSE,
322
388
                                            cdma_act_to_mb_act (info),
323
389
                                            cdma_state_to_mb_state (info),
324
390
                                            info->modem_enabled,
325
391
                                            applet);
 
392
                gtk_widget_set_sensitive (GTK_WIDGET (item), FALSE);
326
393
                gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
327
394
        }
328
395
 
448
515
}
449
516
 
450
517
typedef struct {
451
 
        NMANewSecretsRequestedFunc callback;
452
 
        gpointer callback_data;
453
 
        NMApplet *applet;
454
 
        NMSettingsConnectionInterface *connection;
455
 
        NMActiveConnection *active_connection;
 
518
        SecretsRequest req;
456
519
        GtkWidget *dialog;
457
520
        GtkEntry *secret_entry;
458
521
        char *secret_name;
459
 
} NMCdmaInfo;
 
522
} NMCdmaSecretsInfo;
460
523
 
461
524
static void
462
 
destroy_cdma_dialog (gpointer user_data, GObject *finalized)
 
525
free_cdma_secrets_info (SecretsRequest *req)
463
526
{
464
 
        NMCdmaInfo *info = user_data;
465
 
 
466
 
        gtk_widget_hide (info->dialog);
467
 
        gtk_widget_destroy (info->dialog);
468
 
 
469
 
        g_object_unref (info->connection);
 
527
        NMCdmaSecretsInfo *info = (NMCdmaSecretsInfo *) req;
 
528
 
 
529
        if (info->dialog) {
 
530
                gtk_widget_hide (info->dialog);
 
531
                gtk_widget_destroy (info->dialog);
 
532
        }
470
533
        g_free (info->secret_name);
471
 
        g_free (info);
472
 
}
473
 
 
474
 
static void
475
 
update_cb (NMSettingsConnectionInterface *connection,
476
 
           GError *error,
477
 
           gpointer user_data)
478
 
{
479
 
        if (error) {
480
 
                g_warning ("%s: failed to update connection: (%d) %s",
481
 
                           __func__, error->code, error->message);
482
 
        }
483
534
}
484
535
 
485
536
static void
487
538
                     gint response,
488
539
                     gpointer user_data)
489
540
{
490
 
        NMCdmaInfo *info = (NMCdmaInfo *) user_data;
491
 
        NMSettingCdma *setting;
492
 
        GHashTable *settings_hash;
493
 
        GHashTable *secrets;
494
 
        GError *err = NULL;
495
 
 
496
 
        /* Got a user response, clear the NMActiveConnection destroy handler for
497
 
         * this dialog since this function will now take over dialog destruction.
498
 
         */
499
 
        g_object_weak_unref (G_OBJECT (info->active_connection), destroy_cdma_dialog, info);
500
 
 
501
 
        if (response != GTK_RESPONSE_OK) {
502
 
                g_set_error (&err,
503
 
                             NM_SETTINGS_INTERFACE_ERROR,
504
 
                             NM_SETTINGS_INTERFACE_ERROR_INTERNAL_ERROR,
505
 
                             "%s.%d (%s): canceled",
506
 
                             __FILE__, __LINE__, __func__);
507
 
                goto done;
508
 
        }
509
 
 
510
 
        setting = NM_SETTING_CDMA (nm_connection_get_setting (NM_CONNECTION (info->connection), NM_TYPE_SETTING_CDMA));
511
 
 
512
 
        if (!strcmp (info->secret_name, NM_SETTING_CDMA_PASSWORD)) {
513
 
                g_object_set (setting, 
514
 
                              NM_SETTING_CDMA_PASSWORD, gtk_entry_get_text (info->secret_entry),
515
 
                              NULL);
516
 
        }
517
 
 
518
 
        secrets = nm_setting_to_hash (NM_SETTING (setting));
519
 
        if (!secrets) {
520
 
                g_set_error (&err,
521
 
                             NM_SETTINGS_INTERFACE_ERROR,
522
 
                             NM_SETTINGS_INTERFACE_ERROR_INTERNAL_ERROR,
523
 
                             "%s.%d (%s): failed to hash setting '%s'.",
524
 
                             __FILE__, __LINE__, __func__, nm_setting_get_name (NM_SETTING (setting)));
525
 
                goto done;
526
 
        }
527
 
 
528
 
        /* Returned secrets are a{sa{sv}}; this is the outer a{s...} hash that
529
 
         * will contain all the individual settings hashes.
530
 
         */
531
 
        settings_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
532
 
                                                                    g_free, (GDestroyNotify) g_hash_table_destroy);
533
 
 
534
 
        g_hash_table_insert (settings_hash, g_strdup (nm_setting_get_name (NM_SETTING (setting))), secrets);
535
 
        info->callback (info->connection, settings_hash, NULL, info->callback_data);
536
 
        g_hash_table_destroy (settings_hash);
537
 
 
538
 
        /* Save the connection back to GConf _after_ hashing it, because
539
 
         * saving to GConf might trigger the GConf change notifiers, resulting
540
 
         * in the connection being read back in from GConf which clears secrets.
541
 
         */
542
 
        if (NMA_IS_GCONF_CONNECTION (info->connection))
543
 
                nm_settings_connection_interface_update (info->connection, update_cb, NULL);
544
 
 
545
 
 done:
546
 
        if (err) {
547
 
                g_warning ("%s", err->message);
548
 
                info->callback (info->connection, NULL, err, info->callback_data);
549
 
                g_error_free (err);
550
 
        }
551
 
 
552
 
        nm_connection_clear_secrets (NM_CONNECTION (info->connection));
553
 
        destroy_cdma_dialog (info, NULL);
 
541
        SecretsRequest *req = user_data;
 
542
        NMCdmaSecretsInfo *info = (NMCdmaSecretsInfo *) req;
 
543
        NMSetting *setting;
 
544
        GError *error = NULL;
 
545
 
 
546
        if (response == GTK_RESPONSE_OK) {
 
547
                setting = nm_connection_get_setting (req->connection, NM_TYPE_SETTING_CDMA);
 
548
                if (setting) {
 
549
                        g_object_set (G_OBJECT (setting),
 
550
                                          info->secret_name, gtk_entry_get_text (info->secret_entry),
 
551
                                          NULL);
 
552
                } else {
 
553
                        error = g_error_new (NM_SECRET_AGENT_ERROR,
 
554
                                                 NM_SECRET_AGENT_ERROR_INTERNAL_ERROR,
 
555
                                                 "%s.%d (%s): no GSM setting",
 
556
                                                 __FILE__, __LINE__, __func__);
 
557
                }
 
558
        } else {
 
559
                error = g_error_new (NM_SECRET_AGENT_ERROR,
 
560
                                     NM_SECRET_AGENT_ERROR_USER_CANCELED,
 
561
                                     "%s.%d (%s): canceled",
 
562
                                     __FILE__, __LINE__, __func__);
 
563
        }
 
564
 
 
565
        applet_secrets_request_complete_setting (req, NM_SETTING_CDMA_SETTING_NAME, error);
 
566
        applet_secrets_request_free (req);
 
567
        g_clear_error (&error);
554
568
}
555
569
 
556
570
static gboolean
557
 
cdma_get_secrets (NMDevice *device,
558
 
                  NMSettingsConnectionInterface *connection,
559
 
                  NMActiveConnection *active_connection,
560
 
                  const char *setting_name,
561
 
                  const char **hints,
562
 
                  NMANewSecretsRequestedFunc callback,
563
 
                  gpointer callback_data,
564
 
                  NMApplet *applet,
565
 
                  GError **error)
 
571
cdma_get_secrets (SecretsRequest *req, GError **error)
566
572
{
567
 
        NMCdmaInfo *info;
 
573
        NMCdmaSecretsInfo *info = (NMCdmaSecretsInfo *) req;
568
574
        GtkWidget *widget;
569
575
        GtkEntry *secret_entry = NULL;
570
576
 
571
 
        if (!hints || !g_strv_length ((char **) hints)) {
 
577
        applet_secrets_request_set_free_func (req, free_cdma_secrets_info);
 
578
 
 
579
        if (!req->hints || !g_strv_length (req->hints)) {
572
580
                g_set_error (error,
573
 
                             NM_SETTINGS_INTERFACE_ERROR,
574
 
                             NM_SETTINGS_INTERFACE_ERROR_INTERNAL_ERROR,
 
581
                             NM_SECRET_AGENT_ERROR,
 
582
                             NM_SECRET_AGENT_ERROR_INTERNAL_ERROR,
575
583
                             "%s.%d (%s): missing secrets hints.",
576
584
                             __FILE__, __LINE__, __func__);
577
585
                return FALSE;
578
586
        }
 
587
        info->secret_name = g_strdup (req->hints[0]);
579
588
 
580
 
        if (!strcmp (hints[0], NM_SETTING_CDMA_PASSWORD))
581
 
                widget = applet_mobile_password_dialog_new (device, NM_CONNECTION (connection), &secret_entry);
 
589
        if (!strcmp (info->secret_name, NM_SETTING_CDMA_PASSWORD))
 
590
                widget = applet_mobile_password_dialog_new (req->connection, &secret_entry);
582
591
        else {
583
592
                g_set_error (error,
584
 
                             NM_SETTINGS_INTERFACE_ERROR,
585
 
                             NM_SETTINGS_INTERFACE_ERROR_INTERNAL_ERROR,
 
593
                             NM_SECRET_AGENT_ERROR,
 
594
                             NM_SECRET_AGENT_ERROR_INTERNAL_ERROR,
586
595
                             "%s.%d (%s): unknown secrets hint '%s'.",
587
 
                             __FILE__, __LINE__, __func__, hints[0]);
 
596
                             __FILE__, __LINE__, __func__, info->secret_name);
588
597
                return FALSE;
589
598
        }
 
599
        info->dialog = widget;
 
600
        info->secret_entry = secret_entry;
590
601
 
591
602
        if (!widget || !secret_entry) {
592
603
                g_set_error (error,
593
 
                             NM_SETTINGS_INTERFACE_ERROR,
594
 
                             NM_SETTINGS_INTERFACE_ERROR_INTERNAL_ERROR,
 
604
                             NM_SECRET_AGENT_ERROR,
 
605
                             NM_SECRET_AGENT_ERROR_INTERNAL_ERROR,
595
606
                             "%s.%d (%s): error asking for CDMA secrets.",
596
607
                             __FILE__, __LINE__, __func__);
597
608
                return FALSE;
598
609
        }
599
610
 
600
 
        info = g_new (NMCdmaInfo, 1);
601
 
        info->callback = callback;
602
 
        info->callback_data = callback_data;
603
 
        info->applet = applet;
604
 
        info->active_connection = active_connection;
605
 
        info->connection = g_object_ref (connection);
606
 
        info->secret_name = g_strdup (hints[0]);
607
 
        info->dialog = widget;
608
 
        info->secret_entry = secret_entry;
609
 
 
610
611
        g_signal_connect (widget, "response", G_CALLBACK (get_cdma_secrets_cb), info);
611
612
 
612
 
        /* Attach a destroy notifier to the NMActiveConnection so we can destroy
613
 
         * the dialog when the active connection goes away.
614
 
         */
615
 
        g_object_weak_ref (G_OBJECT (active_connection), destroy_cdma_dialog, info);
616
 
 
617
613
        gtk_window_set_position (GTK_WINDOW (widget), GTK_WIN_POS_CENTER_ALWAYS);
618
614
        gtk_widget_realize (GTK_WIDGET (widget));
619
615
        gtk_window_present (GTK_WINDOW (widget));
630
626
                g_object_unref (info->props_proxy);
631
627
        if (info->cdma_proxy)
632
628
                g_object_unref (info->cdma_proxy);
 
629
        if (info->bus)
 
630
                dbus_g_connection_unref (info->bus);
633
631
        if (info->poll_id)
634
632
                g_source_remove (info->poll_id);
635
633
        if (info->providers)
893
891
static void
894
892
cdma_device_added (NMDevice *device, NMApplet *applet)
895
893
{
896
 
        NMCdmaDevice *cdma = NM_CDMA_DEVICE (device);
897
 
        AppletDBusManager *dbus_mgr = applet_dbus_manager_get ();
898
 
        DBusGConnection *bus = applet_dbus_manager_get_connection (dbus_mgr);
 
894
        NMDeviceModem *modem = NM_DEVICE_MODEM (device);
899
895
        CdmaDeviceInfo *info;
 
896
        DBusGConnection *bus;
900
897
        const char *udi;
 
898
        GError *error = NULL;
901
899
 
902
900
        udi = nm_device_get_udi (device);
903
901
        if (!udi)
904
902
                return;
905
903
 
 
904
        bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
 
905
        if (!bus) {
 
906
                g_warning ("%s: failed to connect to D-Bus: (%d) %s", __func__, error->code, error->message);
 
907
                g_clear_error (&error);
 
908
                return;
 
909
        }
 
910
 
906
911
        info = g_malloc0 (sizeof (CdmaDeviceInfo));
907
912
        info->applet = applet;
908
913
        info->device = device;
 
914
        info->bus = bus;
909
915
        info->quality_valid = FALSE;
910
916
 
911
917
        info->providers = nmn_mobile_providers_parse (NULL);
930
936
                return;
931
937
        }
932
938
 
933
 
        g_object_set_data_full (G_OBJECT (cdma), "devinfo", info, cdma_device_info_free);
 
939
        g_object_set_data_full (G_OBJECT (modem), "devinfo", info, cdma_device_info_free);
934
940
 
935
941
        /* Registration state change signal */
936
942
        dbus_g_object_register_marshaller (nma_marshal_VOID__UINT_UINT,
964
970
                                 G_TYPE_STRING, MM_DBUS_INTERFACE_MODEM,
965
971
                                 G_TYPE_STRING, "Enabled",
966
972
                                 G_TYPE_INVALID);
967
 
 
968
 
        g_object_unref (dbus_mgr);
969
973
}
970
974
 
971
975
NMADeviceClass *
982
986
        dclass->device_state_changed = cdma_device_state_changed;
983
987
        dclass->get_icon = cdma_get_icon;
984
988
        dclass->get_secrets = cdma_get_secrets;
 
989
        dclass->secrets_request_size = sizeof (NMCdmaSecretsInfo);
985
990
        dclass->device_added = cdma_device_added;
986
991
 
987
992
        return dclass;