[gnome-bluetooth/wip/hadess/lib-changes: 3/18] lib: Populate devices GListStore




commit 6ca12615faf839648ba6d56f34488578ffb77ca1
Author: Bastien Nocera <hadess hadess net>
Date:   Wed Nov 24 17:13:15 2021 +0100

    lib: Populate devices GListStore

 lib/bluetooth-client.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 110 insertions(+), 4 deletions(-)
---
diff --git a/lib/bluetooth-client.c b/lib/bluetooth-client.c
index 90f010ab..0310d36d 100644
--- a/lib/bluetooth-client.c
+++ b/lib/bluetooth-client.c
@@ -390,11 +390,13 @@ device_notify_cb (Device1         *device,
 static void
 device_added (GDBusObjectManager   *manager,
              Device1              *device,
-             BluetoothClient      *client)
+             BluetoothClient      *client,
+             gboolean              coldplug)
 {
        g_autoptr (GDBusProxy) adapter = NULL;
        const char *adapter_path, *address, *alias, *name, *icon;
        g_auto(GStrv) uuids = NULL;
+       gboolean default_adapter;
        gboolean paired, trusted, connected;
        int legacypairing;
        BluetoothType type = BLUETOOTH_TYPE_ANY;
@@ -421,7 +423,9 @@ device_added (GDBusObjectManager   *manager,
                return;
 
        gtk_tree_model_get (GTK_TREE_MODEL(client->store), &parent,
-                           BLUETOOTH_COLUMN_PROXY, &adapter, -1);
+                           BLUETOOTH_COLUMN_PROXY, &adapter,
+                           BLUETOOTH_COLUMN_DEFAULT, &default_adapter,
+                           -1);
 
        if (get_iter_from_address (client->store, &iter, address, adapter) == FALSE) {
                gtk_tree_store_insert_with_values (client->store, &iter, &parent, -1,
@@ -452,6 +456,25 @@ device_added (GDBusObjectManager   *manager,
                                   BLUETOOTH_COLUMN_PROXY, device,
                                   -1);
        }
+
+       if (default_adapter && !coldplug) {
+               BluetoothDevice *device_obj;
+
+               device_obj = g_object_new (BLUETOOTH_TYPE_DEVICE,
+                                          "address", address,
+                                          "alias", alias,
+                                          "name", name,
+                                          "type", type,
+                                          "icon", icon,
+                                          "legacy-pairing", legacypairing,
+                                          "uuids", uuids,
+                                          "paired", paired,
+                                          "connected", connected,
+                                          "trusted", trusted,
+                                          "proxy", device,
+                                          NULL);
+               g_list_store_append (client->list_store, device_obj);
+       }
 }
 
 static void
@@ -459,6 +482,7 @@ device_removed (const char      *path,
                BluetoothClient *client)
 {
        GtkTreeIter iter;
+       guint i, n_items;
 
        g_debug ("Removing device '%s'", path);
 
@@ -467,6 +491,18 @@ device_removed (const char      *path,
                g_signal_emit (G_OBJECT (client), signals[DEVICE_REMOVED], 0, path);
                gtk_tree_store_remove(client->store, &iter);
        }
+
+       n_items = g_list_model_get_n_items (G_LIST_MODEL (client->list_store));
+       for (i = 0; i < n_items; i++) {
+               g_autoptr (BluetoothDevice) device = NULL;
+
+               device = g_list_model_get_item (G_LIST_MODEL (client->list_store), i);
+               if (g_str_equal (path, bluetooth_device_get_object_path (device))) {
+                       g_list_store_remove (client->list_store, i);
+                       return;
+               }
+       }
+       g_debug ("Device %s was not known, so not removed", path);
 }
 
 static void
@@ -513,6 +549,72 @@ adapter_set_powered (BluetoothClient *client,
                           NULL, (GAsyncReadyCallback) adapter_set_powered_cb, client);
 }
 
+static void
+add_devices_to_list_store (BluetoothClient *client)
+{
+       GList *object_list, *l;
+
+       g_debug ("Emptying list store as default adapter changed");
+       g_list_store_remove_all (client->list_store);
+
+       g_debug ("Coldplugging devices for new default adapter");
+       object_list = g_dbus_object_manager_get_objects (client->manager);
+       for (l = object_list; l != NULL; l = l->next) {
+               GDBusObject *object = l->data;
+               GDBusInterface *iface;
+               const char *adapter_path, *address, *alias, *name, *icon;
+               g_auto(GStrv) uuids = NULL;
+               gboolean default_adapter;
+               gboolean paired, trusted, connected;
+               int legacypairing;
+               BluetoothType type = BLUETOOTH_TYPE_ANY;
+               GtkTreeIter parent;
+               BluetoothDevice *device_obj;
+
+               iface = g_dbus_object_get_interface (object, BLUEZ_DEVICE_INTERFACE);
+               if (!iface)
+                       continue;
+
+               adapter_path = device1_get_adapter (DEVICE1 (iface));
+               if (get_iter_from_path (client->store, &parent, adapter_path) == FALSE)
+                       continue;
+               gtk_tree_model_get (GTK_TREE_MODEL(client->store), &parent,
+                                   BLUETOOTH_COLUMN_DEFAULT, &default_adapter,
+                                   -1);
+               if (!default_adapter)
+                       continue;
+
+               address = device1_get_address (DEVICE1 (iface));
+               alias = device1_get_alias (DEVICE1 (iface));
+               name = device1_get_name (DEVICE1 (iface));
+               paired = device1_get_paired (DEVICE1 (iface));
+               trusted = device1_get_trusted (DEVICE1 (iface));
+               connected = device1_get_connected (DEVICE1 (iface));
+               uuids = device_list_uuids (device1_get_uuids (DEVICE1 (iface)));
+               legacypairing = device1_get_legacy_pairing (DEVICE1 (iface));
+
+               device_resolve_type_and_icon (DEVICE1 (iface), &type, &icon);
+
+               g_debug ("Adding device '%s' on adapter '%s' to list store", address, adapter_path);
+
+               device_obj = g_object_new (BLUETOOTH_TYPE_DEVICE,
+                                          "address", address,
+                                          "alias", alias,
+                                          "name", name,
+                                          "type", type,
+                                          "icon", icon,
+                                          "legacy-pairing", legacypairing,
+                                          "uuids", uuids,
+                                          "paired", paired,
+                                          "connected", connected,
+                                          "trusted", trusted,
+                                          "proxy", DEVICE1 (iface),
+                                          NULL);
+               g_list_store_append (client->list_store, device_obj);
+       }
+       g_list_free_full (object_list, g_object_unref);
+}
+
 static void
 default_adapter_changed (GDBusObjectManager   *manager,
                         const char           *path,
@@ -536,6 +638,8 @@ default_adapter_changed (GDBusObjectManager   *manager,
        gtk_tree_store_set (client->store, &iter,
                            BLUETOOTH_COLUMN_DEFAULT, TRUE, -1);
 
+       add_devices_to_list_store (client);
+
        gtk_tree_model_get (GTK_TREE_MODEL(client->store), &iter,
                           BLUETOOTH_COLUMN_POWERED, &powered, -1);
 
@@ -767,7 +871,8 @@ interface_added (GDBusObjectManager *manager,
        } else if (IS_DEVICE1 (interface)) {
                device_added (manager,
                              DEVICE1 (interface),
-                             client);
+                             client,
+                             FALSE);
        }
 }
 
@@ -882,7 +987,8 @@ object_manager_new_callback(GObject      *source_object,
 
                device_added (client->manager,
                              DEVICE1 (iface),
-                             client);
+                             client,
+                             TRUE);
        }
        g_list_free_full (object_list, g_object_unref);
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]