[tracker/wip/carlosg/tracker-3.0-api-breaks: 18/79] libtracker-sparql: Add methods to subscribe to GraphUpdated from services



commit 4b25e580bf544b7e05cb3fcb27eaba46e43217a0
Author: Carlos Garnacho <carlosg gnome org>
Date:   Fri Dec 20 16:16:23 2019 +0100

    libtracker-sparql: Add methods to subscribe to GraphUpdated from services
    
    We may set up the TrackerNotifier so it makes sense for the used connection,
    but can't cater for the other services that an user might be interested in.
    Add some methods so it's possible to tell the TrackerNotifier which services
    to listen to.
    
    As there's no longer a single default GraphUpdated source, TrackerNotifier
    no longer sets up one by default.

 .../libtracker-sparql-intermediate-c.vapi          |   3 +
 src/libtracker-sparql/tracker-notifier.c           | 123 ++++++++++++++-------
 src/libtracker-sparql/tracker-notifier.h           |   7 ++
 3 files changed, 95 insertions(+), 38 deletions(-)
---
diff --git a/src/libtracker-sparql/libtracker-sparql-intermediate-c.vapi 
b/src/libtracker-sparql/libtracker-sparql-intermediate-c.vapi
index 0c18473c2..74fa7c0b0 100644
--- a/src/libtracker-sparql/libtracker-sparql-intermediate-c.vapi
+++ b/src/libtracker-sparql/libtracker-sparql-intermediate-c.vapi
@@ -99,6 +99,9 @@ namespace Tracker {
                        public string get_urn ();
                        public string get_location ();
                }
+
+               public uint signal_subscribe (GLib.DBusConnection dbus_conn, string dbus_name, string? graph);
+               public void signal_unsubscribe (uint handler_id);
        }
 
        [CCode (cheader_filename = "libtracker-sparql/tracker-endpoint.h")]
diff --git a/src/libtracker-sparql/tracker-notifier.c b/src/libtracker-sparql/tracker-notifier.c
index f4b5e738b..fc867744a 100644
--- a/src/libtracker-sparql/tracker-notifier.c
+++ b/src/libtracker-sparql/tracker-notifier.c
@@ -73,16 +73,20 @@
 #include <libtracker-common/tracker-common.h>
 
 typedef struct _TrackerNotifierPrivate TrackerNotifierPrivate;
+typedef struct _TrackerNotifierSubscription TrackerNotifierSubscription;
 typedef struct _TrackerNotifierEventCache TrackerNotifierEventCache;
 
+struct _TrackerNotifierSubscription {
+       GDBusConnection *connection;
+       guint handler_id;
+};
+
 struct _TrackerNotifierPrivate {
        TrackerSparqlConnection *connection;
-       GDBusConnection *dbus_connection;
        TrackerNotifierFlags flags;
+       GHashTable *subscriptions; /* guint -> TrackerNotifierSubscription */
        gchar **expanded_classes;
        gchar **classes;
-       guint graph_updated_signal_id;
-       guint has_arg0_filter : 1;
 };
 
 struct _TrackerNotifierEventCache {
@@ -120,6 +124,28 @@ G_DEFINE_TYPE_WITH_CODE (TrackerNotifier, tracker_notifier, G_TYPE_OBJECT,
                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
                                                 tracker_notifier_initable_iface_init))
 
+static TrackerNotifierSubscription *
+tracker_notifier_subscription_new (GDBusConnection *connection,
+                                   guint            handler_id)
+{
+       TrackerNotifierSubscription *subscription;
+
+       subscription = g_new0 (TrackerNotifierSubscription, 1);
+       subscription->connection = g_object_ref (connection);
+       subscription->handler_id = handler_id;
+
+       return subscription;
+}
+
+static void
+tracker_notifier_subscription_free (TrackerNotifierSubscription *subscription)
+{
+       g_dbus_connection_signal_unsubscribe (subscription->connection,
+                                             subscription->handler_id);
+       g_object_unref (subscription->connection);
+       g_free (subscription);
+}
+
 static TrackerNotifierEvent *
 tracker_notifier_event_new (gint64       id,
                             const gchar *rdf_type)
@@ -467,9 +493,7 @@ tracker_notifier_initable_init (GInitable     *initable,
                                 GError       **error)
 {
        TrackerNotifier *notifier = TRACKER_NOTIFIER (initable);
-       TrackerDomainOntology *domain_ontology;
        TrackerNotifierPrivate *priv;
-       gchar *dbus_name;
 
        priv = tracker_notifier_get_instance_private (notifier);
        priv->connection = tracker_sparql_connection_get (cancellable, error);
@@ -479,32 +503,6 @@ tracker_notifier_initable_init (GInitable     *initable,
        if (!expand_class_iris (notifier, cancellable, error))
                return FALSE;
 
-       priv->dbus_connection = tracker_sparql_connection_get_dbus_connection ();
-       if (!priv->dbus_connection)
-               priv->dbus_connection = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, error);
-       if (!priv->dbus_connection)
-               return FALSE;
-
-       domain_ontology = tracker_domain_ontology_new (tracker_sparql_connection_get_domain (),
-                                                      cancellable, error);
-       if (!domain_ontology)
-               return FALSE;
-
-       dbus_name = tracker_domain_ontology_get_domain (domain_ontology, "Tracker1");
-
-       priv->graph_updated_signal_id =
-               g_dbus_connection_signal_subscribe (priv->dbus_connection,
-                                                   dbus_name,
-                                                   "org.freedesktop.Tracker1.Endpoint",
-                                                   "GraphUpdated",
-                                                   "/org/freedesktop/Tracker1/Endpoint",
-                                                   NULL,
-                                                   G_DBUS_SIGNAL_FLAGS_NONE,
-                                                   graph_updated_cb,
-                                                   initable, NULL);
-       tracker_domain_ontology_unref (domain_ontology);
-       g_free (dbus_name);
-
        return TRUE;
 }
 
@@ -565,16 +563,10 @@ tracker_notifier_finalize (GObject *object)
 
        priv = tracker_notifier_get_instance_private (TRACKER_NOTIFIER (object));
 
-       if (priv->dbus_connection) {
-               g_dbus_connection_signal_unsubscribe (priv->dbus_connection,
-                                                     priv->graph_updated_signal_id);
-
-               g_object_unref (priv->dbus_connection);
-       }
-
        if (priv->connection)
                g_object_unref (priv->connection);
 
+       g_hash_table_unref (priv->subscriptions);
        g_strfreev (priv->expanded_classes);
        g_strfreev (priv->classes);
 
@@ -641,6 +633,11 @@ tracker_notifier_class_init (TrackerNotifierClass *klass)
 static void
 tracker_notifier_init (TrackerNotifier *notifier)
 {
+       TrackerNotifierPrivate *priv;
+
+       priv = tracker_notifier_get_instance_private (notifier);
+       priv->subscriptions = g_hash_table_new_full (NULL, NULL, NULL,
+                                                    (GDestroyNotify) tracker_notifier_subscription_free);
 }
 
 /**
@@ -671,6 +668,56 @@ tracker_notifier_new (const gchar * const   *classes,
                               NULL);
 }
 
+guint
+tracker_notifier_signal_subscribe (TrackerNotifier *notifier,
+                                   GDBusConnection *connection,
+                                   const gchar     *service,
+                                   const gchar     *graph)
+{
+       TrackerNotifierSubscription *subscription;
+       TrackerNotifierPrivate *priv;
+       guint handler_id;
+
+       g_return_val_if_fail (TRACKER_IS_NOTIFIER (notifier), 0);
+       g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
+       g_return_val_if_fail (service != NULL, 0);
+
+       priv = tracker_notifier_get_instance_private (notifier);
+
+       handler_id =
+               g_dbus_connection_signal_subscribe (connection,
+                                                   service,
+                                                   "org.freedesktop.Tracker1.Endpoint",
+                                                   "GraphUpdated",
+                                                   "/org/freedesktop/Tracker1/Endpoint",
+                                                   graph,
+                                                   G_DBUS_SIGNAL_FLAGS_NONE,
+                                                   graph_updated_cb,
+                                                   notifier, NULL);
+
+       subscription = tracker_notifier_subscription_new (connection, handler_id);
+
+       g_hash_table_insert (priv->subscriptions,
+                            GUINT_TO_POINTER (subscription->handler_id),
+                            subscription);
+
+       return handler_id;
+}
+
+void
+tracker_notifier_signal_unsubscribe (TrackerNotifier *notifier,
+                                     guint            handler_id)
+{
+       TrackerNotifierPrivate *priv;
+
+       g_return_if_fail (TRACKER_IS_NOTIFIER (notifier));
+       g_return_if_fail (handler_id != 0);
+
+       priv = tracker_notifier_get_instance_private (notifier);
+
+       g_hash_table_remove (priv->subscriptions, GUINT_TO_POINTER (handler_id));
+}
+
 /**
  * tracker_notifier_event_get_event_type:
  * @event: A #TrackerNotifierEvent
diff --git a/src/libtracker-sparql/tracker-notifier.h b/src/libtracker-sparql/tracker-notifier.h
index 2d8613703..8e0458556 100644
--- a/src/libtracker-sparql/tracker-notifier.h
+++ b/src/libtracker-sparql/tracker-notifier.h
@@ -73,6 +73,13 @@ TrackerNotifier * tracker_notifier_new (const gchar * const   *classes,
                                         GCancellable          *cancellable,
                                         GError               **error);
 
+guint tracker_notifier_signal_subscribe   (TrackerNotifier *notifier,
+                                           GDBusConnection *connection,
+                                           const gchar     *service,
+                                           const gchar     *graph);
+void  tracker_notifier_signal_unsubscribe (TrackerNotifier *notifier,
+                                           guint            handler_id);
+
 TrackerNotifierEventType
               tracker_notifier_event_get_event_type (TrackerNotifierEvent *event);
 gint64        tracker_notifier_event_get_id         (TrackerNotifierEvent *event);


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