[tracker/wip/carlosg/portal: 132/141] portal: Set up watch on peers



commit 28de70d13df3675b06755a73e1eb07a7a6775450
Author: Carlos Garnacho <carlosg gnome org>
Date:   Fri Jan 24 13:28:17 2020 +0100

    portal: Set up watch on peers
    
    And close sessions when those vanish

 src/portal/tracker-portal-endpoint.c | 45 ++++++++++++++++++++++++++++++++++++
 src/portal/tracker-portal.c          | 23 ++++++++++++++++++
 2 files changed, 68 insertions(+)
---
diff --git a/src/portal/tracker-portal-endpoint.c b/src/portal/tracker-portal-endpoint.c
index a357d5383..56478df2c 100644
--- a/src/portal/tracker-portal-endpoint.c
+++ b/src/portal/tracker-portal-endpoint.c
@@ -38,6 +38,7 @@ struct _TrackerPortalEndpoint
        gchar *peer;
        gchar *prologue;
        GStrv graphs;
+       guint watch_id;
 };
 
 struct _TrackerPortalEndpointClass
@@ -51,7 +52,13 @@ enum {
        N_PROPS
 };
 
+enum {
+       CLOSED,
+       N_SIGNALS
+};
+
 static GParamSpec *props[N_PROPS] = { 0 };
+static guint signals[N_SIGNALS] = { 0 };
 
 G_DEFINE_TYPE (TrackerPortalEndpoint, tracker_portal_endpoint, TRACKER_TYPE_ENDPOINT_DBUS)
 
@@ -161,11 +168,42 @@ tracker_portal_endpoint_get_property (GObject    *object,
        }
 }
 
+static void
+peer_vanished_cb (GDBusConnection *connection,
+                  const gchar     *name,
+                  gpointer         user_data)
+{
+       TrackerPortalEndpoint *endpoint = user_data;
+
+       g_signal_emit (endpoint, signals[CLOSED], 0);
+}
+
+static void
+tracker_portal_endpoint_constructed (GObject *object)
+{
+       TrackerPortalEndpoint *endpoint = TRACKER_PORTAL_ENDPOINT (object);
+       GDBusConnection *connection;
+
+       g_object_get (object, "dbus-connection", &connection, NULL);
+       endpoint->watch_id = g_bus_watch_name_on_connection (connection,
+                                                            endpoint->peer,
+                                                            G_BUS_NAME_WATCHER_FLAGS_NONE,
+                                                            NULL,
+                                                            peer_vanished_cb,
+                                                            object,
+                                                            NULL);
+       g_object_unref (connection);
+
+       if (G_OBJECT_CLASS (tracker_portal_endpoint_parent_class)->constructed)
+               G_OBJECT_CLASS (tracker_portal_endpoint_parent_class)->constructed (object);
+}
+
 static void
 tracker_portal_endpoint_finalize (GObject *object)
 {
        TrackerPortalEndpoint *endpoint = TRACKER_PORTAL_ENDPOINT (object);
 
+       g_bus_unwatch_name (endpoint->watch_id);
        g_strfreev (endpoint->graphs);
        g_free (endpoint->peer);
        g_free (endpoint->prologue);
@@ -181,12 +219,19 @@ tracker_portal_endpoint_class_init (TrackerPortalEndpointClass *klass)
 
        object_class->set_property = tracker_portal_endpoint_set_property;
        object_class->get_property = tracker_portal_endpoint_get_property;
+       object_class->constructed = tracker_portal_endpoint_constructed;
        object_class->finalize = tracker_portal_endpoint_finalize;
 
        endpoint_dbus_class->forbid_operation = tracker_portal_endpoint_forbid_operation;
        endpoint_dbus_class->filter_graph = tracker_portal_endpoint_filter_graph;
        endpoint_dbus_class->add_prologue = tracker_portal_endpoint_add_prologue;
 
+       signals[CLOSED] =
+               g_signal_new ("closed",
+                             TRACKER_TYPE_PORTAL_ENDPOINT, 0,
+                             0, NULL, NULL, NULL,
+                             G_TYPE_NONE, 0, G_TYPE_NONE);
+
        props[PROP_GRAPHS] =
                g_param_spec_boxed ("graphs",
                                    "Graphs",
diff --git a/src/portal/tracker-portal.c b/src/portal/tracker-portal.c
index 15c3ec783..fd2119d54 100644
--- a/src/portal/tracker-portal.c
+++ b/src/portal/tracker-portal.c
@@ -172,6 +172,25 @@ tracker_portal_init (TrackerPortal *portal)
        g_array_set_clear_func (portal->sessions, clear_session);
 }
 
+static void
+endpoint_closed_cb (TrackerPortalEndpoint *endpoint,
+                    gpointer               user_data)
+{
+       TrackerPortal *portal = user_data;
+       gint i;
+
+       for (i = 0; i < portal->sessions->len; i++) {
+               TrackerSession *session;
+
+               session = &g_array_index (portal->sessions, TrackerSession, i);
+               if (session->endpoint != TRACKER_ENDPOINT (endpoint))
+                       continue;
+
+               g_array_remove_index_fast (portal->sessions, i);
+               break;
+       }
+}
+
 static GStrv
 load_client_configuration (GDBusMethodInvocation  *invocation,
                            const gchar            *service_uri,
@@ -279,6 +298,10 @@ portal_iface_method_call (GDBusConnection       *connection,
                        return;
                }
 
+               g_signal_connect (endpoint, "closed",
+                                 G_CALLBACK (endpoint_closed_cb),
+                                 portal);
+
                session = (TrackerSession) {
                        .dbus_connection = g_steal_pointer (&dbus_connection),
                        .connection = g_steal_pointer (&connection),


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