~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-bt.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 Red Hat, Inc.
 
20
 * (C) Copyright 2008 - 2011 Red Hat, Inc.
21
21
 * (C) Copyright 2008 Novell, Inc.
22
22
 */
23
23
 
35
35
#include <nm-setting-gsm.h>
36
36
#include <nm-device-bt.h>
37
37
#include <nm-utils.h>
 
38
#include <nm-secret-agent.h>
38
39
 
39
40
#include "applet.h"
40
41
#include "applet-device-bt.h"
41
42
#include "wired-dialog.h"
42
43
#include "utils.h"
43
 
#include "gconf-helpers.h"
44
44
#include "applet-dialogs.h"
45
45
 
46
46
typedef struct {
251
251
}
252
252
 
253
253
typedef struct {
254
 
        NMANewSecretsRequestedFunc callback;
255
 
        gpointer callback_data;
256
 
        NMApplet *applet;
257
 
        NMSettingsConnectionInterface *connection;
258
 
        NMActiveConnection *active_connection;
 
254
        SecretsRequest req;
259
255
        GtkWidget *dialog;
260
256
        GtkEntry *secret_entry;
261
257
        char *secret_name;
262
 
        char *setting_name;
263
258
} NMBtSecretsInfo;
264
259
 
265
260
static void
266
 
destroy_secrets_dialog (gpointer user_data, GObject *finalized)
 
261
free_bt_secrets_info (SecretsRequest *req)
267
262
{
268
 
        NMBtSecretsInfo *info = user_data;
269
 
 
270
 
        gtk_widget_hide (info->dialog);
271
 
        gtk_widget_destroy (info->dialog);
272
 
 
273
 
        g_object_unref (info->connection);
 
263
        NMBtSecretsInfo *info = (NMBtSecretsInfo *) req;
 
264
 
 
265
        if (info->dialog) {
 
266
                gtk_widget_hide (info->dialog);
 
267
                gtk_widget_destroy (info->dialog);
 
268
        }
274
269
        g_free (info->secret_name);
275
 
        g_free (info->setting_name);
276
 
        g_free (info);
277
 
}
278
 
 
279
 
static void
280
 
update_cb (NMSettingsConnectionInterface *connection,
281
 
           GError *error,
282
 
           gpointer user_data)
283
 
{
284
 
        if (error) {
285
 
                g_warning ("%s: failed to update connection: (%d) %s",
286
 
                           __func__, error->code, error->message);
287
 
        }
288
270
}
289
271
 
290
272
static void
292
274
                   gint response,
293
275
                   gpointer user_data)
294
276
{
295
 
        NMBtSecretsInfo *info = user_data;
 
277
        SecretsRequest *req = user_data;
 
278
        NMBtSecretsInfo *info = (NMBtSecretsInfo *) req;
296
279
        NMSetting *setting;
297
 
        GHashTable *settings_hash;
298
 
        GHashTable *secrets;
299
 
        GError *err = NULL;
300
 
 
301
 
        /* Got a user response, clear the NMActiveConnection destroy handler for
302
 
         * this dialog since this function will now take over dialog destruction.
303
 
         */
304
 
        g_object_weak_unref (G_OBJECT (info->active_connection), destroy_secrets_dialog, info);
305
 
 
306
 
        if (response != GTK_RESPONSE_OK) {
307
 
                g_set_error (&err,
308
 
                             NM_SETTINGS_INTERFACE_ERROR,
309
 
                             NM_SETTINGS_INTERFACE_ERROR_INTERNAL_ERROR,
 
280
        GError *error = NULL;
 
281
 
 
282
        if (response == GTK_RESPONSE_OK) {
 
283
                setting = nm_connection_get_setting_by_name (req->connection, req->setting_name);
 
284
                if (setting) {
 
285
                        /* Normally we'd want to get all the settings's secrets and return those
 
286
                         * to NM too (since NM wants them), but since the only other secrets for 3G
 
287
                         * connections are PINs, and since the phone obviously has to be unlocked
 
288
                         * to even make the Bluetooth connection, we can skip doing that here for
 
289
                         * Bluetooth devices.
 
290
                         */
 
291
 
 
292
                        /* Update the password */
 
293
                        g_object_set (G_OBJECT (setting),
 
294
                                              info->secret_name, gtk_entry_get_text (info->secret_entry),
 
295
                                              NULL);
 
296
                } else {
 
297
                        g_set_error (&error,
 
298
                                         NM_SECRET_AGENT_ERROR,
 
299
                                         NM_SECRET_AGENT_ERROR_INTERNAL_ERROR,
 
300
                                         "%s.%d (%s): unhandled setting '%s'",
 
301
                                         __FILE__, __LINE__, __func__, req->setting_name);
 
302
                }
 
303
        } else {
 
304
                g_set_error (&error,
 
305
                             NM_SECRET_AGENT_ERROR,
 
306
                             NM_SECRET_AGENT_ERROR_INTERNAL_ERROR,
310
307
                             "%s.%d (%s): canceled",
311
308
                             __FILE__, __LINE__, __func__);
312
 
                goto done;
313
 
        }
314
 
 
315
 
        setting = nm_connection_get_setting_by_name (NM_CONNECTION (info->connection), info->setting_name);
316
 
        if (!setting) {
317
 
                g_set_error (&err,
318
 
                             NM_SETTINGS_INTERFACE_ERROR,
319
 
                             NM_SETTINGS_INTERFACE_ERROR_INTERNAL_ERROR,
320
 
                             "%s.%d (%s): unhandled setting '%s'",
321
 
                             __FILE__, __LINE__, __func__, info->setting_name);
322
 
                goto done;
323
 
        }
324
 
 
325
 
        /* Normally we'd want to get all the settings's secrets and return those
326
 
         * to NM too (since NM wants them), but since the only other secrets for 3G
327
 
         * connections are PINs, and since the phone obviously has to be unlocked
328
 
         * to even make the Bluetooth connection, we can skip doing that here for
329
 
         * Bluetooth devices.
330
 
         */
331
 
 
332
 
        /* Update the password */
333
 
        g_object_set (G_OBJECT (setting),
334
 
                      info->secret_name, gtk_entry_get_text (info->secret_entry),
335
 
                      NULL);
336
 
 
337
 
        secrets = nm_setting_to_hash (NM_SETTING (setting));
338
 
        if (!secrets) {
339
 
                g_set_error (&err,
340
 
                             NM_SETTINGS_INTERFACE_ERROR,
341
 
                             NM_SETTINGS_INTERFACE_ERROR_INTERNAL_ERROR,
342
 
                             "%s.%d (%s): failed to hash setting '%s'.",
343
 
                             __FILE__, __LINE__, __func__,
344
 
                             nm_setting_get_name (NM_SETTING (setting)));
345
 
                goto done;
346
 
        }
347
 
 
348
 
        /* Returned secrets are a{sa{sv}}; this is the outer a{s...} hash that
349
 
         * will contain all the individual settings hashes.
350
 
         */
351
 
        settings_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
352
 
                                               g_free, (GDestroyNotify) g_hash_table_destroy);
353
 
 
354
 
        g_hash_table_insert (settings_hash, g_strdup (nm_setting_get_name (NM_SETTING (setting))), secrets);
355
 
        info->callback (info->connection, settings_hash, NULL, info->callback_data);
356
 
        g_hash_table_destroy (settings_hash);
357
 
 
358
 
        /* Save the connection back to GConf _after_ hashing it, because
359
 
         * saving to GConf might trigger the GConf change notifiers, resulting
360
 
         * in the connection being read back in from GConf which clears secrets.
361
 
         */
362
 
        if (NMA_IS_GCONF_CONNECTION (info->connection))
363
 
                nm_settings_connection_interface_update (info->connection, update_cb, NULL);
364
 
 
365
 
 done:
366
 
        if (err) {
367
 
                g_warning ("%s", err->message);
368
 
                info->callback (info->connection, NULL, err, info->callback_data);
369
 
                g_error_free (err);
370
 
        }
371
 
 
372
 
        nm_connection_clear_secrets (NM_CONNECTION (info->connection));
373
 
        destroy_secrets_dialog (info, NULL);
 
309
        }
 
310
 
 
311
        applet_secrets_request_complete_setting (req, req->setting_name, error);
 
312
        applet_secrets_request_free (req);
 
313
        g_clear_error (&error);
374
314
}
375
315
 
376
316
static gboolean
377
 
bt_get_secrets (NMDevice *device,
378
 
                NMSettingsConnectionInterface *connection,
379
 
                NMActiveConnection *active_connection,
380
 
                const char *setting_name,
381
 
                const char **hints,
382
 
                NMANewSecretsRequestedFunc callback,
383
 
                gpointer callback_data,
384
 
                NMApplet *applet,
385
 
                GError **error)
 
317
bt_get_secrets (SecretsRequest *req, GError **error)
386
318
{
387
 
        NMBtSecretsInfo *info;
 
319
        NMBtSecretsInfo *info = (NMBtSecretsInfo *) req;
388
320
        GtkWidget *widget;
389
321
        GtkEntry *secret_entry = NULL;
390
322
 
391
 
        if (!hints || !g_strv_length ((char **) hints)) {
 
323
        applet_secrets_request_set_free_func (req, free_bt_secrets_info);
 
324
 
 
325
        if (!req->hints || !g_strv_length (req->hints)) {
392
326
                g_set_error (error,
393
 
                             NM_SETTINGS_INTERFACE_ERROR,
394
 
                             NM_SETTINGS_INTERFACE_ERROR_INTERNAL_ERROR,
 
327
                             NM_SECRET_AGENT_ERROR,
 
328
                             NM_SECRET_AGENT_ERROR_INTERNAL_ERROR,
395
329
                             "%s.%d (%s): missing secrets hints.",
396
330
                             __FILE__, __LINE__, __func__);
397
331
                return FALSE;
398
332
        }
 
333
        info->secret_name = g_strdup (req->hints[0]);
399
334
 
400
 
        if (   (!strcmp (setting_name, NM_SETTING_CDMA_SETTING_NAME) && !strcmp (hints[0], NM_SETTING_CDMA_PASSWORD))
401
 
            || (!strcmp (setting_name, NM_SETTING_GSM_SETTING_NAME) && !strcmp (hints[0], NM_SETTING_GSM_PASSWORD)))
402
 
                widget = applet_mobile_password_dialog_new (device, NM_CONNECTION (connection), &secret_entry);
 
335
        if (   (!strcmp (req->setting_name, NM_SETTING_CDMA_SETTING_NAME) && !strcmp (info->secret_name, NM_SETTING_CDMA_PASSWORD))
 
336
            || (!strcmp (req->setting_name, NM_SETTING_GSM_SETTING_NAME) && !strcmp (info->secret_name, NM_SETTING_GSM_PASSWORD)))
 
337
                widget = applet_mobile_password_dialog_new (req->connection, &secret_entry);
403
338
        else {
404
339
                g_set_error (error,
405
 
                             NM_SETTINGS_INTERFACE_ERROR,
406
 
                             NM_SETTINGS_INTERFACE_ERROR_INTERNAL_ERROR,
 
340
                             NM_SECRET_AGENT_ERROR,
 
341
                             NM_SECRET_AGENT_ERROR_INTERNAL_ERROR,
407
342
                             "%s.%d (%s): unknown secrets hint '%s'.",
408
 
                             __FILE__, __LINE__, __func__, hints[0]);
 
343
                             __FILE__, __LINE__, __func__, info->secret_name);
409
344
                return FALSE;
410
345
        }
 
346
        info->dialog = widget;
 
347
        info->secret_entry = secret_entry;
411
348
 
412
349
        if (!widget || !secret_entry) {
413
350
                g_set_error (error,
414
 
                             NM_SETTINGS_INTERFACE_ERROR,
415
 
                             NM_SETTINGS_INTERFACE_ERROR_INTERNAL_ERROR,
 
351
                             NM_SECRET_AGENT_ERROR,
 
352
                             NM_SECRET_AGENT_ERROR_INTERNAL_ERROR,
416
353
                             "%s.%d (%s): error asking for CDMA secrets.",
417
354
                             __FILE__, __LINE__, __func__);
418
355
                return FALSE;
419
356
        }
420
357
 
421
 
        info = g_malloc0 (sizeof (NMBtSecretsInfo));
422
 
        info->callback = callback;
423
 
        info->callback_data = callback_data;
424
 
        info->applet = applet;
425
 
        info->active_connection = active_connection;
426
 
        info->connection = g_object_ref (connection);
427
 
        info->secret_name = g_strdup (hints[0]);
428
 
        info->setting_name = g_strdup (setting_name);
429
 
        info->dialog = widget;
430
 
        info->secret_entry = secret_entry;
431
 
 
432
358
        g_signal_connect (widget, "response", G_CALLBACK (get_bt_secrets_cb), info);
433
359
 
434
 
        /* Attach a destroy notifier to the NMActiveConnection so we can destroy
435
 
         * the dialog when the active connection goes away.
436
 
         */
437
 
        g_object_weak_ref (G_OBJECT (active_connection), destroy_secrets_dialog, info);
438
 
 
439
360
        gtk_window_set_position (GTK_WINDOW (widget), GTK_WIN_POS_CENTER_ALWAYS);
440
361
        gtk_widget_realize (GTK_WIDGET (widget));
441
362
        gtk_window_present (GTK_WINDOW (widget));
457
378
        dclass->device_state_changed = bt_device_state_changed;
458
379
        dclass->get_icon = bt_get_icon;
459
380
        dclass->get_secrets = bt_get_secrets;
 
381
        dclass->secrets_request_size = sizeof (NMBtSecretsInfo);
460
382
 
461
383
        return dclass;
462
384
}