~ubuntu-branches/ubuntu/quantal/gnome-bluetooth/quantal-proposed

« back to all changes in this revision

Viewing changes to applet/bluetooth-applet.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2012-05-15 15:30:36 UTC
  • mfrom: (1.1.29)
  • Revision ID: package-import@ubuntu.com-20120515153036-t5ieibhgp6wruqzy
Tags: 3.4.0-0ubuntu1
* New upstream release
* debian/control:
  - Use standards version 3.9.3
  - Packages renamed from 8 to 10
  - Put introspection package in correct section
  - Bump build-depends on libglib2.0-dev
  - Drop build-depends on libdbus-glib-1-dev
  - Drop unneeded build-depends on gir packages
* debian/rules:
  - Packages renamed from 8 to 10
* debian/gnome-bluetooth.install:
  - No longer provides a control center panel
* debian/libgnome-bluetooth10.symbols:
  - Updated
* debian/patches/fix_browse_devices_selector.patch:
  - Patched code removed upstream
* debian/patches/git_fix_automake_use.patch:
  - Applied upstream
* debian/patches/onlyshowin_unity.patch:
  - Obsolete, preferences now in control center

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
#include <glib/gi18n.h>
25
25
#include <gtk/gtk.h>
26
 
#include <dbus/dbus-glib.h>
27
26
#include <gio/gio.h>
28
27
 
29
28
#include <bluetooth-applet.h>
33
32
#include <bluetooth-killswitch.h>
34
33
#include <bluetooth-agent.h>
35
34
 
36
 
#include <marshal.h>
37
 
 
38
35
static gpointer
39
36
bluetooth_simple_device_copy (gpointer boxed)
40
37
{
75
72
        gulong signal_row_added;
76
73
        gulong signal_row_changed;
77
74
        gulong signal_row_deleted;
78
 
        DBusGProxy* default_adapter;
 
75
        char* default_adapter;
79
76
        BluetoothAgent* agent;
80
77
        GHashTable* pending_requests;
81
78
 
147
144
{
148
145
        g_return_if_fail (BLUETOOTH_IS_APPLET (applet));
149
146
 
150
 
        bluetooth_browse_address (applet,
 
147
        bluetooth_browse_address (G_OBJECT (applet),
151
148
                                  address,
152
149
                                  timestamp,
153
150
                                  callback,
183
180
                                      const char      *request_key,
184
181
                                      const char      *pincode)
185
182
{
186
 
        DBusGMethodInvocation* context;
 
183
        GDBusMethodInvocation* invocation;
187
184
 
188
185
        g_return_if_fail (BLUETOOTH_IS_APPLET (self));
189
186
        g_return_if_fail (request_key != NULL);
190
187
 
191
 
        context = g_hash_table_lookup (self->pending_requests, request_key);
 
188
        invocation = g_hash_table_lookup (self->pending_requests, request_key);
192
189
 
193
190
        if (pincode != NULL) {
194
 
                dbus_g_method_return (context, pincode);
 
191
                g_dbus_method_invocation_return_value (invocation,
 
192
                                                       g_variant_new ("(s)", pincode));
195
193
        } else {
196
194
                GError *error;
197
195
                error = g_error_new (AGENT_ERROR, AGENT_ERROR_REJECT,
198
196
                                     "Pairing request rejected");
199
 
                dbus_g_method_return_error (context, error);
 
197
                g_dbus_method_invocation_return_gerror (invocation, error);
 
198
                g_error_free (error);
200
199
        }
201
200
 
202
201
        g_hash_table_remove (self->pending_requests, request_key);
214
213
                                      const char      *request_key,
215
214
                                      int              passkey)
216
215
{
217
 
        DBusGMethodInvocation* context;
 
216
        GDBusMethodInvocation* invocation;
218
217
 
219
218
        g_return_if_fail (BLUETOOTH_IS_APPLET (self));
220
219
        g_return_if_fail (request_key != NULL);
221
220
 
222
 
        context = g_hash_table_lookup (self->pending_requests, request_key);
 
221
        invocation = g_hash_table_lookup (self->pending_requests, request_key);
223
222
 
224
223
        if (passkey != -1) {
225
 
                dbus_g_method_return (context, passkey);
 
224
                g_dbus_method_invocation_return_value (invocation,
 
225
                                                       g_variant_new ("(u)", passkey));
226
226
        } else {
227
227
                GError *error;
228
228
                error = g_error_new (AGENT_ERROR, AGENT_ERROR_REJECT,
229
229
                                     "Pairing request rejected");
230
 
                dbus_g_method_return_error (context, error);
 
230
                g_dbus_method_invocation_return_gerror (invocation, error);
 
231
                g_error_free (error);
231
232
        }
232
233
 
233
234
        g_hash_table_remove (self->pending_requests, request_key);
245
246
                                      const char      *request_key,
246
247
                                      gboolean         confirm)
247
248
{
248
 
        DBusGMethodInvocation* context;
 
249
        GDBusMethodInvocation* invocation;
249
250
 
250
251
        g_return_if_fail (BLUETOOTH_IS_APPLET (self));
251
252
        g_return_if_fail (request_key != NULL);
252
253
 
253
 
        context = g_hash_table_lookup (self->pending_requests, request_key);
 
254
        invocation = g_hash_table_lookup (self->pending_requests, request_key);
254
255
 
255
256
        if (confirm) {
256
 
                dbus_g_method_return (context);
 
257
                g_dbus_method_invocation_return_value (invocation, NULL);
257
258
        } else {
258
259
                GError *error;
259
260
                error = g_error_new (AGENT_ERROR, AGENT_ERROR_REJECT,
260
261
                                     "Confirmation request rejected");
261
 
                dbus_g_method_return_error (context, error);
 
262
                g_dbus_method_invocation_return_gerror (invocation, error);
 
263
                g_error_free (error);
262
264
        }
263
265
 
264
266
        g_hash_table_remove (self->pending_requests, request_key);
278
280
                                   gboolean         auth,
279
281
                                   gboolean         trusted)
280
282
{
281
 
        DBusGMethodInvocation* context;
 
283
        GDBusMethodInvocation* invocation;
282
284
 
283
285
        g_return_if_fail (BLUETOOTH_IS_APPLET (self));
284
286
        g_return_if_fail (request_key != NULL);
285
287
 
286
 
        context = g_hash_table_lookup (self->pending_requests, request_key);
 
288
        invocation = g_hash_table_lookup (self->pending_requests, request_key);
287
289
 
288
290
        if (auth) {
289
291
                if (trusted)
290
292
                        bluetooth_client_set_trusted (self->client, request_key, TRUE);
291
293
 
292
 
                dbus_g_method_return (context);
 
294
                g_dbus_method_invocation_return_value (invocation, NULL);
293
295
        } else {
294
296
                GError *error;
295
297
                error = g_error_new (AGENT_ERROR, AGENT_ERROR_REJECT,
296
298
                                     "Confirmation request rejected");
297
 
                dbus_g_method_return_error (context, error);
 
299
                g_dbus_method_invocation_return_gerror (invocation, error);
 
300
                g_error_free (error);
298
301
        }
299
302
 
300
303
        g_hash_table_remove (self->pending_requests, request_key);
301
304
}
302
305
 
303
 
#ifndef DBUS_TYPE_G_DICTIONARY
304
 
#define DBUS_TYPE_G_DICTIONARY \
305
 
        (dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE))
306
 
#endif
307
 
 
308
306
static char *
309
 
device_get_name (DBusGProxy *proxy, char **long_name)
 
307
device_get_name (GDBusProxy *proxy, char **long_name)
310
308
{
311
 
        GHashTable *hash;
312
 
        GValue *value;
 
309
        GVariant *value;
 
310
        GVariant *result;
313
311
        char *alias, *address;
314
312
 
315
313
        g_return_val_if_fail (long_name != NULL, NULL);
316
314
 
317
 
        if (dbus_g_proxy_call (proxy, "GetProperties",  NULL,
318
 
                               G_TYPE_INVALID,
319
 
                               DBUS_TYPE_G_DICTIONARY, &hash,
320
 
                               G_TYPE_INVALID) == FALSE) {
 
315
        result = g_dbus_proxy_call_sync (proxy, "GetProperties",  NULL,
 
316
                                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
 
317
        if (result == NULL)
321
318
                return NULL;
322
 
        }
323
319
 
324
 
        value = g_hash_table_lookup (hash, "Address");
 
320
        value = g_variant_lookup_value (result, "Address", G_VARIANT_TYPE_STRING);
325
321
        if (value == NULL) {
326
 
                g_hash_table_destroy (hash);
 
322
                g_variant_unref (result);
327
323
                return NULL;
328
324
        }
329
 
        address = g_value_dup_string (value);
330
 
 
331
 
        value = g_hash_table_lookup (hash, "Name");
332
 
        alias = value ? g_value_dup_string (value) : address;
333
 
 
334
 
        g_hash_table_destroy (hash);
 
325
        address = g_strdup (g_variant_get_string (value, NULL));
 
326
 
 
327
        value = g_variant_lookup_value (result, "Name", G_VARIANT_TYPE_STRING);
 
328
        alias = value ? g_strdup (g_variant_get_string (value, NULL)) : g_strdup (address);
 
329
 
 
330
        g_variant_unref (result);
335
331
 
336
332
        if (value)
337
333
                *long_name = g_strdup_printf ("'%s' (%s)", alias, address);
338
334
        else
339
335
                *long_name = g_strdup_printf ("'%s'", address);
340
336
 
341
 
        if (alias != address)
342
 
                g_free (address);
 
337
        g_free (address);
343
338
        return alias;
344
339
}
345
340
 
346
341
static gboolean
347
 
pincode_request (DBusGMethodInvocation *context,
348
 
                 DBusGProxy            *device,
 
342
pincode_request (GDBusMethodInvocation *invocation,
 
343
                 GDBusProxy            *device,
349
344
                 gpointer               user_data)
350
345
{
351
346
        BluetoothApplet* self = BLUETOOTH_APPLET (user_data);
354
349
        const char *path;
355
350
 
356
351
        name = device_get_name (device, &long_name);
357
 
        path = dbus_g_proxy_get_path (device);
358
 
        g_hash_table_insert (self->pending_requests, g_strdup (path), context);
 
352
        path = g_dbus_proxy_get_object_path (device);
 
353
        g_hash_table_insert (self->pending_requests, g_strdup (path), invocation);
359
354
 
360
355
        g_signal_emit (self, signals[SIGNAL_PINCODE_REQUEST], 0, path, name, long_name, FALSE);
361
356
 
366
361
}
367
362
 
368
363
static gboolean
369
 
passkey_request (DBusGMethodInvocation *context,
370
 
                 DBusGProxy            *device,
 
364
passkey_request (GDBusMethodInvocation *invocation,
 
365
                 GDBusProxy            *device,
371
366
                 gpointer               user_data)
372
367
{
373
368
        BluetoothApplet* self = BLUETOOTH_APPLET (user_data);
376
371
        const char *path;
377
372
 
378
373
        name = device_get_name (device, &long_name);
379
 
        path = dbus_g_proxy_get_path (device);
380
 
        g_hash_table_insert (self->pending_requests, g_strdup (path), context);
 
374
        path = g_dbus_proxy_get_object_path (device);
 
375
        g_hash_table_insert (self->pending_requests, g_strdup (path), invocation);
381
376
 
382
377
        g_signal_emit (self, signals[SIGNAL_PINCODE_REQUEST], 0, path, name, long_name, TRUE);
383
378
 
388
383
}
389
384
 
390
385
static gboolean
391
 
confirm_request (DBusGMethodInvocation *context,
392
 
                 DBusGProxy *device,
 
386
confirm_request (GDBusMethodInvocation *invocation,
 
387
                 GDBusProxy *device,
393
388
                 guint pin,
394
389
                 gpointer user_data)
395
390
{
399
394
        const char *path;
400
395
 
401
396
        name = device_get_name (device, &long_name);
402
 
        path = dbus_g_proxy_get_path (device);
403
 
        g_hash_table_insert (self->pending_requests, g_strdup (path), context);
 
397
        path = g_dbus_proxy_get_object_path (device);
 
398
        g_hash_table_insert (self->pending_requests, g_strdup (path), invocation);
404
399
 
405
400
        g_signal_emit (self, signals[SIGNAL_CONFIRM_REQUEST], 0, path, name, long_name, pin);
406
401
 
411
406
}
412
407
 
413
408
static gboolean
414
 
authorize_request (DBusGMethodInvocation *context,
415
 
                   DBusGProxy *device,
 
409
authorize_request (GDBusMethodInvocation *invocation,
 
410
                   GDBusProxy *device,
416
411
                   const char *uuid,
417
412
                   gpointer user_data)
418
413
{
422
417
        const char *path;
423
418
 
424
419
        name = device_get_name (device, &long_name);
425
 
        path = dbus_g_proxy_get_path (device);
426
 
        g_hash_table_insert (self->pending_requests, g_strdup (path), context);
 
420
        path = g_dbus_proxy_get_object_path (device);
 
421
        g_hash_table_insert (self->pending_requests, g_strdup (path), invocation);
427
422
 
428
423
        g_signal_emit (self, signals[SIGNAL_AUTHORIZE_REQUEST], 0, path, name, long_name, uuid);
429
424
 
436
431
static void
437
432
cancel_request_single (gpointer key, gpointer value, gpointer user_data)
438
433
{
439
 
        DBusGMethodInvocation* request_context = value;
440
 
        GError* result;
 
434
        GDBusMethodInvocation* request_invocation = value;
 
435
        GError* error;
441
436
 
442
437
        if (value) {
443
 
                result = g_error_new (AGENT_ERROR, AGENT_ERROR_REJECT, "Agent callback cancelled");
444
 
                dbus_g_method_return_error (request_context, result);
 
438
                error = g_error_new (AGENT_ERROR, AGENT_ERROR_REJECT, "Agent callback cancelled");
 
439
                g_dbus_method_invocation_return_gerror (request_invocation, error);
 
440
                g_error_free (error);
445
441
        }
446
442
}
447
443
 
448
444
static gboolean
449
 
cancel_request(DBusGMethodInvocation *context,
 
445
cancel_request(GDBusMethodInvocation *invocation,
450
446
               gpointer user_data)
451
447
{
452
448
        BluetoothApplet* self = BLUETOOTH_APPLET (user_data);
504
500
        BluetoothApplet* self = BLUETOOTH_APPLET (data);
505
501
        GtkTreeModel *child_model;
506
502
 
507
 
        if (self->default_adapter)
508
 
                g_object_unref (self->default_adapter);
509
 
        self->default_adapter = bluetooth_client_get_default_adapter (self->client);
 
503
        if (self->default_adapter) {
 
504
                g_free (self->default_adapter);
 
505
                self->default_adapter = NULL;
 
506
        }
 
507
        g_object_get (G_OBJECT (self->client), "default-adapter", &self->default_adapter, NULL);
510
508
 
511
509
        /* The old model */
512
510
        child_model = get_child_model (self->device_model);
520
518
                g_object_unref (self->device_model);
521
519
 
522
520
        /* The new model */
523
 
        if (self->default_adapter)
524
 
                self->device_model = bluetooth_client_get_device_model (self->client, self->default_adapter);
525
 
        else
526
 
                self->device_model = NULL;
 
521
        self->device_model = bluetooth_client_get_device_model (self->client);
527
522
 
528
523
        child_model = get_child_model (self->device_model);
529
524
        if (child_model) {
548
543
                bluetooth_agent_set_confirm_func (self->agent, confirm_request, self);
549
544
                bluetooth_agent_set_cancel_func (self->agent, cancel_request, self);
550
545
 
551
 
                bluetooth_agent_register (self->agent, self->default_adapter);
 
546
                bluetooth_agent_register (self->agent);
552
547
        }
553
548
 
554
549
        g_signal_emit (self, signals[SIGNAL_DEVICES_CHANGED], 0);
580
575
                     GtkTreeIter  *iter,
581
576
                     gpointer      data)
582
577
{
583
 
        DBusGProxy *proxy = NULL;
584
 
        GValue value = { 0, };
 
578
        GDBusProxy *proxy = NULL;
585
579
 
586
580
        gtk_tree_model_get (model, iter,
587
581
                            BLUETOOTH_COLUMN_PROXY, &proxy, -1);
 
582
 
588
583
        if (proxy == NULL)
589
584
                return FALSE;
590
585
 
591
 
        g_value_init (&value, G_TYPE_BOOLEAN);
592
 
        g_value_set_boolean (&value, TRUE);
593
 
 
594
 
        dbus_g_proxy_call_no_reply (proxy, "SetProperty",
595
 
                                    G_TYPE_STRING, "Powered",
596
 
                                    G_TYPE_VALUE, &value,
597
 
                                    G_TYPE_INVALID,
598
 
                                    G_TYPE_INVALID);
599
 
 
600
 
        g_value_unset (&value);
 
586
        g_dbus_proxy_call (proxy,
 
587
                           "SetProperty",
 
588
                           g_variant_new ("sv", "Powered", g_variant_new_boolean (TRUE)),
 
589
                           G_DBUS_CALL_FLAGS_NO_AUTO_START,
 
590
                           -1,
 
591
                           NULL,
 
592
                           NULL,
 
593
                           NULL);
 
594
 
601
595
        g_object_unref (proxy);
602
596
 
603
597
        return FALSE;
644
638
} ConnectionClosure;
645
639
 
646
640
static void
647
 
connection_callback (BluetoothClient* client, gboolean success, gpointer data)
 
641
 
 
642
connection_callback (GObject      *source_object,
 
643
                     GAsyncResult *res,
 
644
                     gpointer      user_data)
648
645
{
649
 
        ConnectionClosure *closure = (ConnectionClosure*) data;
 
646
        ConnectionClosure *closure = (ConnectionClosure*) user_data;
 
647
        gboolean success;
 
648
 
 
649
        success = bluetooth_client_connect_service_finish (BLUETOOTH_CLIENT (source_object), res, NULL);
650
650
 
651
651
        (*(closure->func)) (closure->self, success, closure->user_data);
652
652
 
678
678
        closure->func = func;
679
679
        closure->user_data = data;
680
680
 
681
 
        return bluetooth_client_connect_service (applet->client, device, connection_callback, closure);
 
681
        bluetooth_client_connect_service (applet->client, device, TRUE, NULL, connection_callback, closure);
 
682
 
 
683
        return TRUE;
682
684
}
683
685
 
684
686
/**
706
708
        closure->func = func;
707
709
        closure->user_data = data;
708
710
 
709
 
        return bluetooth_client_disconnect_service (applet->client, device, connection_callback, closure);
 
711
        bluetooth_client_connect_service (applet->client, device, FALSE, NULL, connection_callback, closure);
 
712
 
 
713
        return TRUE;
710
714
}
711
715
 
712
716
/**
719
723
gboolean
720
724
bluetooth_applet_get_discoverable (BluetoothApplet* self)
721
725
{
 
726
        gboolean ret;
722
727
        g_return_val_if_fail (BLUETOOTH_IS_APPLET (self), FALSE);
723
728
 
724
 
        return bluetooth_client_get_discoverable (self->client);
 
729
        g_object_get (G_OBJECT (self->client), "default-adapter-discoverable", &ret, NULL);
 
730
        return ret;
725
731
}
726
732
 
727
733
/**
735
741
{
736
742
        g_return_if_fail (BLUETOOTH_IS_APPLET (self));
737
743
 
738
 
        bluetooth_client_set_discoverable (self->client, disc, 0);
 
744
        g_object_set (G_OBJECT (self->client), "default-adapter-discoverable", disc, NULL);
739
745
}
740
746
 
741
747
/**
809
815
{
810
816
        BluetoothSimpleDevice *dev;
811
817
        GHashTable *services;
812
 
        DBusGProxy *proxy;
 
818
        GDBusProxy *proxy;
813
819
        char **uuids;
814
820
 
815
821
        dev = g_new0 (BluetoothSimpleDevice, 1);
836
842
        }
837
843
 
838
844
        if (proxy != NULL) {
839
 
                dev->device_path = g_strdup (dbus_g_proxy_get_path (proxy));
 
845
                dev->device_path = g_strdup (g_dbus_proxy_get_object_path (proxy));
840
846
                g_object_unref (proxy);
841
847
        }
842
848
 
961
967
        g_signal_connect (self->killswitch_manager, "state-changed", G_CALLBACK(killswitch_state_change), self);
962
968
 
963
969
        self->pending_requests = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
964
 
        dbus_g_error_domain_register (AGENT_ERROR, "org.bluez.Error", AGENT_ERROR_TYPE);
 
970
        g_dbus_error_register_error (AGENT_ERROR, AGENT_ERROR_REJECT, "org.bluez.Error.Rejected");
965
971
 
966
972
        /* Make sure all the unblocked adapters are powered,
967
973
         * so as to avoid seeing unpowered, but unblocked
1031
1037
        g_object_class_install_property (gobject_class, PROP_FULL_MENU, properties[PROP_FULL_MENU]);
1032
1038
 
1033
1039
        signals[SIGNAL_DEVICES_CHANGED] = g_signal_new ("devices-changed", G_TYPE_FROM_CLASS (gobject_class),
1034
 
                                                        G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID,
 
1040
                                                        G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE, 0, NULL, NULL, NULL,
1035
1041
                                                        G_TYPE_NONE, 0);
1036
1042
 
1037
1043
        signals[SIGNAL_PINCODE_REQUEST] = g_signal_new ("pincode-request", G_TYPE_FROM_CLASS (gobject_class),
1038
 
                                                        G_SIGNAL_RUN_FIRST, 0, NULL, NULL, marshal_VOID__STRING_STRING_STRING_BOOLEAN,
 
1044
                                                        G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL,
1039
1045
                                                        G_TYPE_NONE, 4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN);
1040
1046
 
1041
1047
        signals[SIGNAL_CONFIRM_REQUEST] = g_signal_new ("confirm-request", G_TYPE_FROM_CLASS (gobject_class),
1042
 
                                                        G_SIGNAL_RUN_FIRST, 0, NULL, NULL, marshal_VOID__STRING_STRING_STRING_UINT,
 
1048
                                                        G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL,
1043
1049
                                                        G_TYPE_NONE, 4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT);
1044
1050
 
1045
1051
        signals[SIGNAL_AUTHORIZE_REQUEST] = g_signal_new ("auth-request", G_TYPE_FROM_CLASS (gobject_class),
1046
 
                                                          G_SIGNAL_RUN_FIRST, 0, NULL, NULL, marshal_VOID__STRING_STRING_STRING_STRING,
 
1052
                                                          G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL,
1047
1053
                                                          G_TYPE_NONE, 4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
1048
1054
 
1049
1055
        signals[SIGNAL_CANCEL_REQUEST] = g_signal_new ("cancel-request", G_TYPE_FROM_CLASS (gobject_class),
1050
 
                                                       G_SIGNAL_RUN_FIRST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID,
 
1056
                                                       G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL,
1051
1057
                                                       G_TYPE_NONE, 0);
1052
1058
}
1053
1059