~timchen119/ubuntu/trusty/gnome-bluetooth/lp1035431

« back to all changes in this revision

Viewing changes to lib/bluetooth-client.c

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2011-02-27 15:45:22 UTC
  • mfrom: (1.3.2 upstream)
  • mto: (2.2.3 experimental) (1.5.1)
  • mto: This revision was merged to the branch mainline in revision 53.
  • Revision ID: james.westby@ubuntu.com-20110227154522-dnnoqasv5v3mv42a
Tags: upstream-2.91.5
ImportĀ upstreamĀ versionĀ 2.91.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 *  BlueZ - Bluetooth protocol stack for Linux
4
4
 *
5
5
 *  Copyright (C) 2005-2008  Marcel Holtmann <marcel@holtmann.org>
 
6
 *  Copyright (C) 2010       Giovanni Campagna <scampa.giovanni@gmail.com>
6
7
 *
7
8
 *
8
9
 *  This library is free software; you can redistribute it and/or
21
22
 *
22
23
 */
23
24
 
 
25
/**
 
26
 * SECTION:bluetooth-client
 
27
 * @short_description: Bluetooth client object
 
28
 * @stability: Stable
 
29
 * @include: bluetooth-client.h
 
30
 *
 
31
 * The #BluetoothClient object is used to query the state of Bluetooth
 
32
 * devices and adapters.
 
33
 **/
 
34
 
24
35
#ifdef HAVE_CONFIG_H
25
36
#include <config.h>
26
37
#endif
90
101
        DBusGProxy *dbus;
91
102
        DBusGProxy *manager;
92
103
        GtkTreeStore *store;
93
 
        char *default_adapter;
94
 
        gboolean default_adapter_powered;
 
104
        GtkTreeRowReference *default_adapter;
95
105
};
96
106
 
97
107
enum {
98
108
        PROP_0,
99
109
        PROP_DEFAULT_ADAPTER,
100
110
        PROP_DEFAULT_ADAPTER_POWERED,
 
111
        PROP_DEFAULT_ADAPTER_DISCOVERABLE,
101
112
};
102
113
 
103
114
G_DEFINE_TYPE(BluetoothClient, bluetooth_client, G_TYPE_OBJECT)
106
117
 * bluetooth_type_to_string:
107
118
 * @type: a #BluetoothType
108
119
 *
109
 
 * Return value: the string representation of the @type passed. Do not free the return value.
 
120
 * Returns the string representation of the @type passed. Do not free the return value.
 
121
 *
 
122
 * Return value: a string.
110
123
 **/
111
124
const gchar *bluetooth_type_to_string(BluetoothType type)
112
125
{
113
126
        switch (type) {
114
127
        case BLUETOOTH_TYPE_ANY:
115
 
                return N_("All types");
 
128
                return _("All types");
116
129
        case BLUETOOTH_TYPE_PHONE:
117
 
                return N_("Phone");
 
130
                return _("Phone");
118
131
        case BLUETOOTH_TYPE_MODEM:
119
 
                return N_("Modem");
 
132
                return _("Modem");
120
133
        case BLUETOOTH_TYPE_COMPUTER:
121
 
                return N_("Computer");
 
134
                return _("Computer");
122
135
        case BLUETOOTH_TYPE_NETWORK:
123
 
                return N_("Network");
 
136
                return _("Network");
124
137
        case BLUETOOTH_TYPE_HEADSET:
125
138
                /* translators: a hands-free headset, a combination of a single speaker with a microphone */
126
 
                return N_("Headset");
 
139
                return _("Headset");
127
140
        case BLUETOOTH_TYPE_HEADPHONES:
128
 
                return N_("Headphones");
 
141
                return _("Headphones");
129
142
        case BLUETOOTH_TYPE_OTHER_AUDIO:
130
 
                return N_("Audio device");
 
143
                return _("Audio device");
131
144
        case BLUETOOTH_TYPE_KEYBOARD:
132
 
                return N_("Keyboard");
 
145
                return _("Keyboard");
133
146
        case BLUETOOTH_TYPE_MOUSE:
134
 
                return N_("Mouse");
 
147
                return _("Mouse");
135
148
        case BLUETOOTH_TYPE_CAMERA:
136
 
                return N_("Camera");
 
149
                return _("Camera");
137
150
        case BLUETOOTH_TYPE_PRINTER:
138
 
                return N_("Printer");
 
151
                return _("Printer");
139
152
        case BLUETOOTH_TYPE_JOYPAD:
140
 
                return N_("Joypad");
 
153
                return _("Joypad");
141
154
        case BLUETOOTH_TYPE_TABLET:
142
 
                return N_("Tablet");
 
155
                return _("Tablet");
 
156
        case BLUETOOTH_TYPE_VIDEO:
 
157
                return _("Video device");
143
158
        default:
144
 
                return N_("Unknown");
 
159
                return _("Unknown");
145
160
        }
146
161
}
147
162
 
149
164
 * bluetooth_verify_address:
150
165
 * @bdaddr: a string representing a Bluetooth address
151
166
 *
152
 
 * Return value: Whether the string is a valid Bluetooth address. This does not contact the device in any way.
 
167
 * Returns whether the string is a valid Bluetooth address. This does not contact the device in any way.
 
168
 *
 
169
 * Return value: %TRUE if the address is valid, %FALSE if not.
153
170
 **/
154
171
gboolean
155
172
bluetooth_verify_address (const char *bdaddr)
209
226
                        return BLUETOOTH_TYPE_HEADSET;
210
227
                case 0x06:
211
228
                        return BLUETOOTH_TYPE_HEADPHONES;
 
229
                case 0x0b: /* VCR */
 
230
                case 0x0c: /* Video Camera */
 
231
                case 0x0d: /* Camcorder */
 
232
                        return BLUETOOTH_TYPE_VIDEO;
212
233
                default:
213
234
                        return BLUETOOTH_TYPE_OTHER_AUDIO;
214
235
                }
540
561
                return NULL;
541
562
        case 0x1201:
542
563
                return "GenericNetworking";
 
564
        case 0x1303:
 
565
                return "VideoSource";
543
566
        case 0x8e771303:
544
567
        case 0x8e771301:
545
568
                return "SEMC HLA";
555
578
 * bluetooth_uuid_to_string:
556
579
 * @uuid: a string representing a Bluetooth UUID
557
580
 *
558
 
 * Return value: a string representing a human-readable (but not usable for display to users) version of the @uuid. Do not free the return value.
 
581
 * Returns a string representing a human-readable (but not usable for display to users) version of the @uuid. Do not free the return value.
 
582
 *
 
583
 * Return value: a string.
559
584
 **/
560
585
const char *
561
586
bluetooth_uuid_to_string (const char *uuid)
881
906
                                   BLUETOOTH_COLUMN_POWERED, powered, -1);
882
907
                gtk_tree_model_get(GTK_TREE_MODEL(priv->store), &iter,
883
908
                                   BLUETOOTH_COLUMN_DEFAULT, &is_default, -1);
884
 
                if (is_default != FALSE && powered != priv->default_adapter_powered) {
885
 
                        priv->default_adapter_powered = powered;
 
909
                if (is_default != FALSE)
886
910
                        g_object_notify (G_OBJECT (client), "default-adapter-powered");
887
 
                }
 
911
                notify = TRUE;
888
912
        } else if (g_str_equal(property, "Discoverable") == TRUE) {
889
913
                gboolean discoverable = g_value_get_boolean(value);
 
914
                gboolean is_default;
890
915
 
891
916
                gtk_tree_store_set(priv->store, &iter,
892
 
                                BLUETOOTH_COLUMN_DISCOVERABLE, discoverable, -1);
 
917
                                   BLUETOOTH_COLUMN_DISCOVERABLE, discoverable, -1);
 
918
                gtk_tree_model_get(GTK_TREE_MODEL(priv->store), &iter,
 
919
                                   BLUETOOTH_COLUMN_DEFAULT, &is_default, -1);
 
920
                if (is_default != FALSE)
 
921
                        g_object_notify (G_OBJECT (client), "default-adapter-discoverable");
893
922
                notify = TRUE;
894
923
        }
895
924
 
1007
1036
        while (cont == TRUE) {
1008
1037
                DBusGProxy *adapter;
1009
1038
                const char *adapter_path;
1010
 
                gboolean found;
 
1039
                gboolean found, was_default;
1011
1040
 
1012
1041
                gtk_tree_model_get(GTK_TREE_MODEL(priv->store), &iter,
1013
 
                                        BLUETOOTH_COLUMN_PROXY, &adapter, -1);
 
1042
                                   BLUETOOTH_COLUMN_PROXY, &adapter,
 
1043
                                   BLUETOOTH_COLUMN_DEFAULT, &was_default, -1);
1014
1044
 
1015
1045
                adapter_path = dbus_g_proxy_get_path(adapter);
1016
1046
 
1029
1059
                g_object_unref(adapter);
1030
1060
 
1031
1061
                if (found == TRUE) {
1032
 
                        cont = gtk_tree_store_remove(priv->store, &iter);
1033
 
                        continue;
 
1062
                        if (was_default) {
 
1063
                                gtk_tree_row_reference_free (priv->default_adapter);
 
1064
                                priv->default_adapter = NULL;
 
1065
                                g_object_notify (G_OBJECT (client), "default-adapter");
 
1066
                                g_object_notify (G_OBJECT (client), "default-adapter-powered");
 
1067
                                g_object_notify (G_OBJECT (client), "default-adapter-discoverable");
 
1068
                        }
 
1069
                        gtk_tree_store_remove(priv->store, &iter);
 
1070
                        break;
1034
1071
                }
1035
1072
 
1036
1073
                cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(priv->store),
1037
1074
                                                                        &iter);
1038
1075
        }
1039
 
 
1040
 
        /* No adapters left in the tree? */
1041
 
        if (gtk_tree_model_iter_n_children (GTK_TREE_MODEL(priv->store), NULL) == 0) {
1042
 
                g_free(priv->default_adapter);
1043
 
                priv->default_adapter = NULL;
1044
 
                priv->default_adapter_powered = FALSE;
1045
 
                g_object_notify (G_OBJECT (client), "default-adapter");
1046
 
                g_object_notify (G_OBJECT (client), "default-adapter-powered");
1047
 
        }
1048
1076
}
1049
1077
 
1050
1078
static void default_adapter_changed(DBusGProxy *manager,
1059
1087
 
1060
1088
        cont = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(priv->store),
1061
1089
                                                                        &iter);
 
1090
        if (priv->default_adapter) {
 
1091
                gtk_tree_row_reference_free (priv->default_adapter);
 
1092
                priv->default_adapter = NULL;
 
1093
        }
1062
1094
 
1063
1095
        while (cont == TRUE) {
1064
1096
                DBusGProxy *adapter;
1075
1107
 
1076
1108
                g_object_unref(adapter);
1077
1109
 
1078
 
                if (found != FALSE)
1079
 
                        priv->default_adapter_powered = powered;
 
1110
                if (found != FALSE) {
 
1111
                        GtkTreePath *tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->store), &iter);
 
1112
                        priv->default_adapter = gtk_tree_row_reference_new (GTK_TREE_MODEL (priv->store), tree_path);
 
1113
                        gtk_tree_path_free (tree_path);
 
1114
                }
1080
1115
 
1081
1116
                gtk_tree_store_set(priv->store, &iter,
1082
1117
                                        BLUETOOTH_COLUMN_DEFAULT, found, -1);
1086
1121
        }
1087
1122
 
1088
1123
        /* Record the new default adapter */
1089
 
        g_free(priv->default_adapter);
1090
 
        priv->default_adapter = g_strdup(path);
1091
1124
        g_object_notify (G_OBJECT (client), "default-adapter");
1092
1125
        g_object_notify (G_OBJECT (client), "default-adapter-powered");
 
1126
        g_object_notify (G_OBJECT (client), "default-adapter-discoverable");
1093
1127
}
1094
1128
 
1095
1129
static void name_owner_changed(DBusGProxy *dbus, const char *name,
1105
1139
 
1106
1140
        DBG("client %p name %s", client, name);
1107
1141
 
 
1142
        if (priv->default_adapter) {
 
1143
                gtk_tree_row_reference_free (priv->default_adapter);
 
1144
                priv->default_adapter = NULL;
 
1145
        }
 
1146
 
1108
1147
        cont = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(priv->store),
1109
1148
                                                                        &iter);
1110
1149
 
1172
1211
        }
1173
1212
}
1174
1213
 
 
1214
static const char*
 
1215
_bluetooth_client_get_default_adapter_path (BluetoothClient *self)
 
1216
{
 
1217
        DBusGProxy *adapter = bluetooth_client_get_default_adapter (self);
 
1218
 
 
1219
        if (adapter != NULL) {
 
1220
                const char *ret = dbus_g_proxy_get_path (adapter);
 
1221
                g_object_unref (adapter);
 
1222
                return ret;
 
1223
        }
 
1224
        return NULL;
 
1225
}
 
1226
 
 
1227
static gboolean
 
1228
_bluetooth_client_get_default_adapter_powered (BluetoothClient *self)
 
1229
{
 
1230
        BluetoothClientPrivate *priv = BLUETOOTH_CLIENT_GET_PRIVATE (self);
 
1231
        GtkTreePath *path;
 
1232
        GtkTreeIter iter;
 
1233
        gboolean ret;
 
1234
 
 
1235
        if (priv->default_adapter == NULL)
 
1236
                return FALSE;
 
1237
 
 
1238
        path = gtk_tree_row_reference_get_path (priv->default_adapter);
 
1239
        gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->store), &iter, path);
 
1240
        gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter, BLUETOOTH_COLUMN_POWERED, &ret, -1);
 
1241
        
 
1242
        return ret;
 
1243
}
 
1244
 
1175
1245
static void
1176
1246
bluetooth_client_get_property (GObject        *object,
1177
1247
                               guint           property_id,
1178
1248
                               GValue         *value,
1179
1249
                               GParamSpec     *pspec)
1180
1250
{
1181
 
        BluetoothClientPrivate *priv = BLUETOOTH_CLIENT_GET_PRIVATE(object);
 
1251
        BluetoothClient *self = BLUETOOTH_CLIENT (object);
1182
1252
 
1183
1253
        switch (property_id) {
1184
1254
        case PROP_DEFAULT_ADAPTER:
1185
 
                g_value_set_string (value, priv->default_adapter);
 
1255
                g_value_set_string (value, _bluetooth_client_get_default_adapter_path (self));
1186
1256
                break;
1187
1257
        case PROP_DEFAULT_ADAPTER_POWERED:
1188
 
                g_value_set_boolean (value, priv->default_adapter_powered);
 
1258
                g_value_set_boolean (value, _bluetooth_client_get_default_adapter_powered (self));
 
1259
                break;
 
1260
        case PROP_DEFAULT_ADAPTER_DISCOVERABLE:
 
1261
                g_value_set_boolean (value, bluetooth_client_get_discoverable (self));
 
1262
                break;
 
1263
        default:
 
1264
                G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
 
1265
                break;
 
1266
        }
 
1267
}
 
1268
 
 
1269
static void
 
1270
bluetooth_client_set_property (GObject        *object,
 
1271
                               guint           property_id,
 
1272
                               const GValue   *value,
 
1273
                               GParamSpec     *pspec)
 
1274
{
 
1275
        BluetoothClient *self = BLUETOOTH_CLIENT (object);
 
1276
 
 
1277
        switch (property_id) {
 
1278
        case PROP_DEFAULT_ADAPTER_DISCOVERABLE:
 
1279
                bluetooth_client_set_discoverable (self, g_value_get_boolean (value), 0);
1189
1280
                break;
1190
1281
        default:
1191
1282
                G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
1213
1304
 
1214
1305
        g_object_unref(priv->store);
1215
1306
 
1216
 
        g_free (priv->default_adapter);
1217
 
        priv->default_adapter = NULL;
 
1307
        if (priv->default_adapter)
 
1308
                gtk_tree_row_reference_free (priv->default_adapter);
1218
1309
 
1219
1310
        G_OBJECT_CLASS(bluetooth_client_parent_class)->finalize(client);
1220
1311
}
1230
1321
 
1231
1322
        object_class->finalize = bluetooth_client_finalize;
1232
1323
        object_class->get_property = bluetooth_client_get_property;
1233
 
 
 
1324
        object_class->set_property = bluetooth_client_set_property;
1234
1325
 
1235
1326
        g_object_class_install_property (object_class, PROP_DEFAULT_ADAPTER,
1236
1327
                                         g_param_spec_string ("default-adapter", NULL, NULL,
1238
1329
        g_object_class_install_property (object_class, PROP_DEFAULT_ADAPTER_POWERED,
1239
1330
                                         g_param_spec_boolean ("default-adapter-powered", NULL, NULL,
1240
1331
                                                               FALSE, G_PARAM_READABLE));
 
1332
        g_object_class_install_property (object_class, PROP_DEFAULT_ADAPTER_DISCOVERABLE,
 
1333
                                         g_param_spec_boolean ("default-adapter-discoverable", NULL, NULL,
 
1334
                                                               FALSE, G_PARAM_READWRITE));
1241
1335
 
1242
1336
        dbus_g_object_register_marshaller(marshal_VOID__STRING_BOXED,
1243
1337
                                                G_TYPE_NONE, G_TYPE_STRING,
1255
1349
/**
1256
1350
 * bluetooth_client_new:
1257
1351
 *
1258
 
 * Return value: a reference to the #BluetoothClient singleton. Unref the object when done.
 
1352
 * Returns a reference to the #BluetoothClient singleton. Use #g_object_unref() the object when done.
 
1353
 *
 
1354
 * Return value: (transfer full): a #BluetoothClient object.
1259
1355
 **/
1260
1356
BluetoothClient *bluetooth_client_new(void)
1261
1357
{
1277
1373
 * bluetooth_client_get_model:
1278
1374
 * @client: a #BluetoothClient object
1279
1375
 *
1280
 
 * Return value: an unfiltered #GtkTreeModel representing the adapter and devices available on the system.
 
1376
 * Returns an unfiltered #GtkTreeModel representing the adapter and devices available on the system.
 
1377
 *
 
1378
 * Return value: (transfer full): a #GtkTreeModel object.
1281
1379
 **/
1282
1380
GtkTreeModel *bluetooth_client_get_model (BluetoothClient *client)
1283
1381
{
1301
1399
 * @data: user data to pass to gtk_tree_model_filter_set_visible_func()
1302
1400
 * @destroy: a destroy function for gtk_tree_model_filter_set_visible_func()
1303
1401
 *
1304
 
 * Return value: a #GtkTreeModel of devices filtered using the @func, @data and @destroy arguments to pass to gtk_tree_model_filter_set_visible_func().
 
1402
 * Returns a #GtkTreeModel of devices filtered using the @func, @data and @destroy arguments to pass to gtk_tree_model_filter_set_visible_func().
 
1403
 *
 
1404
 * Return value: (transfer full): a #GtkTreeModel object.
1305
1405
 **/
1306
1406
GtkTreeModel *bluetooth_client_get_filter_model (BluetoothClient *client,
1307
1407
                                                 GtkTreeModelFilterVisibleFunc func,
1346
1446
 * bluetooth_client_get_adapter_model:
1347
1447
 * @client: a #BluetoothClient object
1348
1448
 *
1349
 
 * Return value: a filtered #GtkTreeModel with only adapters present.
 
1449
 * Returns a filtered #GtkTreeModel with only adapters present.
 
1450
 *
 
1451
 * Return value: (transfer full): a #GtkTreeModel object.
1350
1452
 **/
1351
1453
GtkTreeModel *bluetooth_client_get_adapter_model (BluetoothClient *client)
1352
1454
{
1361
1463
 * @client: a #BluetoothClient object
1362
1464
 * @adapter: a #DBusGProxy of the adapter object, or %NULL to get the default adapter.
1363
1465
 *
1364
 
 * Return value: a filtered #GtkTreeModel with only devices belonging to the selected adapter listed. Note that the model will follow a specific adapter, and will not follow the default-adapter when %NULL is passed.
 
1466
 * Returns a filtered #GtkTreeModel with only devices belonging to the selected adapter listed. Note that the model will follow a specific adapter, and will not follow the default-adapter when %NULL is passed.
 
1467
 *
 
1468
 * Return value: (transfer full): a #GtkTreeModel object.
1365
1469
 **/
1366
1470
GtkTreeModel *bluetooth_client_get_device_model (BluetoothClient *client,
1367
1471
                                                 DBusGProxy *adapter)
1418
1522
/**
1419
1523
 * bluetooth_client_get_device_filter_model:
1420
1524
 * @client: a #BluetoothClient object
 
1525
 * @adapter: a #DBusGProxy representing a particular adapter, or %NULL for the default adapter.
1421
1526
 * @func: a #GtkTreeModelFilterVisibleFunc
1422
1527
 * @data: user data to pass to gtk_tree_model_filter_set_visible_func()
1423
1528
 * @destroy: a destroy function for gtk_tree_model_filter_set_visible_func()
1424
1529
 *
1425
 
 * Return value: a #GtkTreeModel of adapters filtered using the @func, @data and @destroy arguments to pass to gtk_tree_model_filter_set_visible_func().
 
1530
 * Returns a #GtkTreeModel of adapters filtered using the @func, @data and
 
1531
 * @destroy arguments to pass to gtk_tree_model_filter_set_visible_func().
 
1532
 *
 
1533
 * Return value: (transfer full): a #GtkTreeModel object.
1426
1534
 **/
1427
1535
GtkTreeModel *bluetooth_client_get_device_filter_model(BluetoothClient *client,
1428
1536
                DBusGProxy *adapter, GtkTreeModelFilterVisibleFunc func,
1446
1554
 * bluetooth_client_get_default_adapter:
1447
1555
 * @client: a #BluetoothClient object
1448
1556
 *
1449
 
 * Return value: a #DBusGProxy object representing the default adapter, or %NULL if no adapters are present.
 
1557
 * Returns a #DBusGProxy object representing the default adapter, or %NULL if no adapters are present.
 
1558
 *
 
1559
 * Return value: (transfer full): a #DBusGProxy object.
1450
1560
 **/
1451
1561
DBusGProxy *bluetooth_client_get_default_adapter(BluetoothClient *client)
1452
1562
{
1453
1563
        BluetoothClientPrivate *priv = BLUETOOTH_CLIENT_GET_PRIVATE(client);
 
1564
        GtkTreePath *path;
1454
1565
        GtkTreeIter iter;
1455
 
        gboolean cont;
 
1566
        DBusGProxy *adapter;
1456
1567
 
1457
1568
        g_return_val_if_fail (BLUETOOTH_IS_CLIENT (client), NULL);
1458
1569
 
1459
1570
        DBG("client %p", client);
1460
1571
 
1461
 
        cont = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(priv->store),
1462
 
                                                                        &iter);
1463
 
 
1464
 
        while (cont == TRUE) {
1465
 
                DBusGProxy *adapter;
1466
 
                gboolean is_default;
1467
 
 
1468
 
                gtk_tree_model_get(GTK_TREE_MODEL(priv->store), &iter,
1469
 
                                BLUETOOTH_COLUMN_PROXY, &adapter,
1470
 
                                BLUETOOTH_COLUMN_DEFAULT, &is_default, -1);
1471
 
 
1472
 
                if (is_default == TRUE)
1473
 
                        return adapter;
1474
 
 
1475
 
                g_object_unref(adapter);
1476
 
 
1477
 
                cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(priv->store),
1478
 
                                                                        &iter);
1479
 
        }
1480
 
 
1481
 
        return NULL;
 
1572
        if (priv->default_adapter == NULL)
 
1573
                return NULL;
 
1574
 
 
1575
        path = gtk_tree_row_reference_get_path (priv->default_adapter);
 
1576
        gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->store), &iter, path);
 
1577
        gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
 
1578
                            BLUETOOTH_COLUMN_PROXY, &adapter, -1);
 
1579
 
 
1580
        return adapter;
1482
1581
}
1483
1582
 
1484
1583
/**
1536
1635
}
1537
1636
 
1538
1637
/**
 
1638
 * bluetooth_client_get_discoverable:
 
1639
 * @client: a #BluetoothClient
 
1640
 *
 
1641
 * Gets the default adapter's discoverable status, cached in the adapter model.
 
1642
 *
 
1643
 * Returns: the discoverable status, or FALSE if no default adapter exists
 
1644
 */
 
1645
gboolean
 
1646
bluetooth_client_get_discoverable (BluetoothClient *client)
 
1647
{
 
1648
        BluetoothClientPrivate *priv;
 
1649
        GtkTreePath *path;
 
1650
        GtkTreeIter iter;
 
1651
        gboolean ret;
 
1652
 
 
1653
        g_return_val_if_fail (BLUETOOTH_IS_CLIENT (client), FALSE);
 
1654
 
 
1655
        priv = BLUETOOTH_CLIENT_GET_PRIVATE (client);
 
1656
        if (priv->default_adapter == NULL)
 
1657
                return FALSE;
 
1658
 
 
1659
        path = gtk_tree_row_reference_get_path (priv->default_adapter);
 
1660
        gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->store), &iter, path);
 
1661
        gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
 
1662
                            BLUETOOTH_COLUMN_DISCOVERABLE, &ret, -1);
 
1663
 
 
1664
        return ret;
 
1665
}
 
1666
 
 
1667
/**
1539
1668
 * bluetooth_client_set_discoverable:
1540
1669
 * @client: a #BluetoothClient object
1541
1670
 * @discoverable: whether the device should be discoverable
 
1671
 * @timeout: timeout in seconds for making undiscoverable, or 0 for never.
1542
1672
 *
1543
1673
 * Sets the default adapter's discoverable status.
1544
1674
 *
1546
1676
 **/
1547
1677
gboolean
1548
1678
bluetooth_client_set_discoverable (BluetoothClient *client,
1549
 
                                   gboolean discoverable)
 
1679
                                   gboolean discoverable,
 
1680
                                   guint timeout)
1550
1681
{
 
1682
        GError *error = NULL;
1551
1683
        DBusGProxy *adapter;
1552
1684
        GValue disco = { 0 };
1553
 
        GValue timeout = { 0 };
 
1685
        GValue timeoutv = { 0 };
1554
1686
        gboolean ret;
1555
1687
 
1556
1688
        g_return_val_if_fail (BLUETOOTH_IS_CLIENT (client), FALSE);
1562
1694
                return FALSE;
1563
1695
 
1564
1696
        g_value_init (&disco, G_TYPE_BOOLEAN);
1565
 
        g_value_init (&timeout, G_TYPE_UINT);
 
1697
        g_value_init (&timeoutv, G_TYPE_UINT);
1566
1698
 
1567
1699
        g_value_set_boolean (&disco, discoverable);
1568
 
        g_value_set_uint (&timeout, 0);
1569
 
 
1570
 
        ret = dbus_g_proxy_call (adapter, "SetProperty", NULL,
 
1700
        g_value_set_uint (&timeoutv, timeout);
 
1701
 
 
1702
        if (discoverable) {
 
1703
                ret = dbus_g_proxy_call (adapter, "SetProperty", &error,
 
1704
                                         G_TYPE_STRING, "DiscoverableTimeout",
 
1705
                                         G_TYPE_VALUE, &timeoutv,
 
1706
                                         G_TYPE_INVALID, G_TYPE_INVALID);
 
1707
                if (ret == FALSE)
 
1708
                        goto bail;
 
1709
        }
 
1710
 
 
1711
        ret = dbus_g_proxy_call (adapter, "SetProperty", &error,
1571
1712
                                 G_TYPE_STRING, "Discoverable",
1572
1713
                                 G_TYPE_VALUE, &disco,
1573
1714
                                 G_TYPE_INVALID, G_TYPE_INVALID);
1574
 
        if (ret == FALSE)
1575
 
                goto bail;
1576
1715
 
1577
 
        ret = dbus_g_proxy_call (adapter, "SetProperty", NULL,
1578
 
                                 G_TYPE_STRING, "DiscoverableTimeout",
1579
 
                                 G_TYPE_VALUE, &timeout,
1580
 
                                 G_TYPE_INVALID, G_TYPE_INVALID);
1581
1716
 
1582
1717
bail:
1583
1718
        g_value_unset (&disco);
1584
 
        g_value_unset (&timeout);
 
1719
        g_value_unset (&timeoutv);
1585
1720
 
1586
1721
        g_object_unref(adapter);
1587
1722
 
 
1723
        if (error) {
 
1724
                g_warning ("Cannot set discoverable: %s", error->message);
 
1725
                g_error_free (error);
 
1726
        }
 
1727
 
1588
1728
        return ret;
1589
1729
}
1590
1730
 
1655
1795
                                   err->message);
1656
1796
                        g_error_free (err);
1657
1797
                }
1658
 
                g_object_unref (device);
 
1798
                if (device != NULL)
 
1799
                        g_object_unref (device);
1659
1800
        }
1660
1801
 
1661
1802
        devdata = g_try_new0(CreateDeviceData, 1);
1743
1884
        g_object_unref(proxy);
1744
1885
}
1745
1886
 
 
1887
/**
 
1888
 * bluetooth_client_connect_service:
 
1889
 *
 
1890
 * @client: a #BluetoothClient
 
1891
 * @device: the DBUS path on which to operate
 
1892
 * @func: (scope async): a callback to call when the connection is complete
 
1893
 * @user_data:
 
1894
 *
 
1895
 * Returns: TRUE if the operation was started successfully, FALSE otherwise
 
1896
 */
1746
1897
gboolean bluetooth_client_connect_service(BluetoothClient *client,
1747
1898
                                          const char *device,
1748
1899
                                          BluetoothClientConnectFunc func,
1874
2025
        return 0;
1875
2026
}
1876
2027
 
 
2028
/**
 
2029
 * bluetooth_client_disconnect_service:
 
2030
 *
 
2031
 * @client: a #BluetoothClient
 
2032
 * @device: the DBUS path on which to operate
 
2033
 * @func: (scope async): a callback to call when the disconnection is complete
 
2034
 * @user_data:
 
2035
 *
 
2036
 * Returns: TRUE if the operation was started successfully, FALSE otherwise
 
2037
 */
1877
2038
gboolean bluetooth_client_disconnect_service (BluetoothClient *client,
1878
2039
                                              const char *device,
1879
2040
                                              BluetoothClientConnectFunc func,
1938
2099
static void
1939
2100
services_foreach (const char *service, gpointer _value, GString *str)
1940
2101
{
1941
 
        gboolean value = GPOINTER_TO_INT (_value);
1942
 
        g_string_append_printf (str, "%s (%s) ", service, value ? "connected" : "not connected");
 
2102
        GEnumClass *eclass;
 
2103
        GEnumValue *ev;
 
2104
        BluetoothStatus status = GPOINTER_TO_INT (_value);
 
2105
 
 
2106
        eclass = g_type_class_ref (BLUETOOTH_TYPE_STATUS);
 
2107
        ev = g_enum_get_value (eclass, status);
 
2108
        if (ev == NULL)
 
2109
                g_warning ("Unknown status value %d", status);
 
2110
 
 
2111
        g_string_append_printf (str, "%s (%s) ", service, ev ? ev->value_nick : "unknown");
 
2112
        g_type_class_unref (eclass);
1943
2113
}
1944
2114
 
1945
2115
void