[tracker/wip/carlosg/tracker-3.0-api-breaks: 55/92] libtracker-data: Expand dbus uris to observe bus and object path



commit 6f8af359d4aeb04a86dcd9147ead5c77235e933b
Author: Carlos Garnacho <carlosg gnome org>
Date:   Tue Dec 31 19:50:39 2019 +0100

    libtracker-data: Expand dbus uris to observe bus and object path
    
    The URI scheme is dbus:[system|session]?:<interface>:<path>, and allows
    us to connect with endpoints in either bus, with arbitrary object paths.

 src/libtracker-common/tracker-utils.c      | 38 ++++++++++++++++++++++++++++++
 src/libtracker-common/tracker-utils.h      |  5 ++++
 src/libtracker-data/tracker-vtab-service.c | 20 ++++++++++++++--
 3 files changed, 61 insertions(+), 2 deletions(-)
---
diff --git a/src/libtracker-common/tracker-utils.c b/src/libtracker-common/tracker-utils.c
index 530bb4a70..7c03530b4 100644
--- a/src/libtracker-common/tracker-utils.c
+++ b/src/libtracker-common/tracker-utils.c
@@ -439,3 +439,41 @@ tracker_resolve_relative_uri (const gchar  *base,
 
        return g_string_free (str, FALSE);
 }
+
+gboolean
+tracker_util_parse_dbus_uri (const gchar  *uri,
+                             GBusType     *bus_type,
+                             gchar       **service,
+                             gchar       **path)
+{
+       const gchar *separator;
+
+       if (!g_str_has_prefix (uri, "dbus:"))
+               return FALSE;
+
+       uri += strlen ("dbus:");
+
+       if (g_str_has_prefix (uri, "system:")) {
+               *bus_type = G_BUS_TYPE_SYSTEM;
+               uri += strlen ("system:");
+       } else if (g_str_has_prefix (uri, "session:")) {
+               *bus_type = G_BUS_TYPE_SESSION;
+               uri += strlen ("session:");
+       } else {
+               /* Fall back to session bus by default */
+               *bus_type = G_BUS_TYPE_SESSION;
+       }
+
+       separator = strstr (uri, ":/");
+
+       if (separator) {
+               *service = g_strndup (uri, separator - uri);
+               separator += 1;
+               *path = g_strdup (separator);
+       } else {
+               *service = g_strdup (uri);
+               *path = NULL;
+       }
+
+       return TRUE;
+}
diff --git a/src/libtracker-common/tracker-utils.h b/src/libtracker-common/tracker-utils.h
index c12c9ccae..28ccee621 100644
--- a/src/libtracker-common/tracker-utils.h
+++ b/src/libtracker-common/tracker-utils.h
@@ -22,6 +22,7 @@
 #define __LIBTRACKER_COMMON_UTILS_H__
 
 #include <glib.h>
+#include <gio/gio.h>
 
 G_BEGIN_DECLS
 
@@ -49,6 +50,10 @@ gchar *  tracker_unescape_unichars          (const gchar  *str,
                                             gssize        len);
 gchar *  tracker_resolve_relative_uri       (const gchar  *base,
                                              const gchar  *rel_uri);
+gboolean tracker_util_parse_dbus_uri        (const gchar  *uri,
+                                             GBusType     *bus_type,
+                                             gchar       **service,
+                                             gchar       **path);
 
 G_END_DECLS
 
diff --git a/src/libtracker-data/tracker-vtab-service.c b/src/libtracker-data/tracker-vtab-service.c
index cb800da2c..b48efacdc 100644
--- a/src/libtracker-data/tracker-vtab-service.c
+++ b/src/libtracker-data/tracker-vtab-service.c
@@ -20,6 +20,7 @@
  */
 #include "config.h"
 
+#include "libtracker-common/tracker-utils.h"
 #include "tracker-vtab-service.h"
 #include <libtracker-sparql/tracker-sparql.h>
 
@@ -228,9 +229,24 @@ service_filter (sqlite3_vtab_cursor  *vtab_cursor,
 
        uri_scheme = g_uri_parse_scheme (cursor->service);
        if (g_strcmp0 (uri_scheme, "dbus") == 0) {
-               const gchar *bus_name = &cursor->service[strlen (uri_scheme) + 1];
+               gchar *bus_name, *object_path;
+               GDBusConnection *dbus_connection;
+               GBusType bus_type;
+
+               if (!tracker_util_parse_dbus_uri (cursor->service,
+                                                 &bus_type,
+                                                 &bus_name, &object_path))
+                       goto fail;
+
+               dbus_connection = g_bus_get_sync (bus_type, NULL, &error);
+               if (!dbus_connection)
+                       goto fail;
+
+               cursor->conn = tracker_sparql_connection_bus_new (bus_name, object_path,
+                                                                 dbus_connection, &error);
+               g_free (bus_name);
+               g_free (object_path);
 
-               cursor->conn = tracker_sparql_connection_bus_new (bus_name, NULL, NULL, &error);
                if (!cursor->conn)
                        goto fail;
        } else if (g_strcmp0 (uri_scheme, "http") == 0) {


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