summaryrefslogtreecommitdiff
path: root/src/portal/tracker-portal-endpoint.c
blob: 79028a1b6cff328126a21909446142d6bb5f90cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * Copyright (C) 2020, Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * Author: Carlos Garnacho <carlosg@gnome.org>
 */
#include "config.h"

#include <libtracker-sparql/tracker-sparql.h>

#include "libtracker-sparql/tracker-private.h"

#include "tracker-portal-endpoint.h"

#define GRAPH_ALL "*"
#define GRAPH_DEFAULT "default"

typedef struct _TrackerPortalEndpoint TrackerPortalEndpoint;
typedef struct _TrackerPortalEndpointClass TrackerPortalEndpointClass;

struct _TrackerPortalEndpoint
{
	TrackerEndpointDBus parent_instance;
	gchar *peer;
	gchar *prologue;
	GStrv graphs;
	guint watch_id;
};

struct _TrackerPortalEndpointClass
{
	TrackerEndpointDBusClass parent_class;
};

enum {
	PROP_GRAPHS = 1,
	PROP_PEER,
	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)

static gboolean
tracker_portal_endpoint_forbid_operation (TrackerEndpointDBus   *endpoint_dbus,
                                          GDBusMethodInvocation *invocation,
                                          TrackerOperationType   operation_type)
{
	TrackerPortalEndpoint *endpoint = TRACKER_PORTAL_ENDPOINT (endpoint_dbus);

	if (operation_type == TRACKER_OPERATION_TYPE_UPDATE)
		return TRUE;
	if (g_strcmp0 (endpoint->peer, g_dbus_method_invocation_get_sender (invocation)) != 0)
		return TRUE;

	return FALSE;
}

static gboolean
tracker_portal_endpoint_filter_graph (TrackerEndpointDBus *endpoint_dbus,
                                      const gchar         *graph_name)
{
	TrackerPortalEndpoint *endpoint = TRACKER_PORTAL_ENDPOINT (endpoint_dbus);
	gint i;

	for (i = 0; endpoint->graphs[i]; i++) {
		if (g_strcmp0 (endpoint->graphs[i], GRAPH_ALL) == 0) {
			return FALSE;
		} else if (!graph_name &&
		           g_strcmp0 (endpoint->graphs[i], GRAPH_DEFAULT) == 0) {
			return FALSE;
		} else if (g_strcmp0 (graph_name, endpoint->graphs[i]) == 0) {
			return FALSE;
		} else {
			TrackerNamespaceManager *namespaces;
			gchar *expanded;

			/* We may have been given a prefixed name instead of an
			 * URI, expand it in order to check with the given graph.
			 *
			 * FIXME: This is not going to work for prefixes outside
			 * the well-known namespaces.
			 */
			namespaces = tracker_namespace_manager_get_default ();
			expanded = tracker_namespace_manager_expand_uri (namespaces,
			                                                 endpoint->graphs[i]);

			if (g_strcmp0 (graph_name, expanded) == 0) {
				g_free (expanded);
				return FALSE;
			}

			g_free (expanded);
		}
	}

	return TRUE;
}

static gchar *
tracker_portal_endpoint_add_prologue (TrackerEndpointDBus *endpoint_dbus)
{
	TrackerPortalEndpoint *endpoint = TRACKER_PORTAL_ENDPOINT (endpoint_dbus);

	if (!endpoint->prologue) {
		GString *str;
		gint i;

		str = g_string_new ("CONSTRAINT SERVICE \n"
		                    "CONSTRAINT GRAPH ");

		for (i = 0; endpoint->graphs[i]; i++) {
			if (i != 0)
				g_string_append (str, ", ");

			if (g_strcmp0 (endpoint->graphs[i], GRAPH_ALL) == 0)
				g_string_append (str, "ALL ");
			else if (g_strcmp0 (endpoint->graphs[i], GRAPH_DEFAULT) == 0)
				g_string_append (str, "DEFAULT ");
			else if (strstr (endpoint->graphs[i], ":/"))
				g_string_append_printf (str, "<%s> ", endpoint->graphs[i]);
			else
				g_string_append_printf (str, "%s ", endpoint->graphs[i]);
		}

		endpoint->prologue = g_string_free (str, FALSE);
	}

	return g_strdup (endpoint->prologue);
}

static void
tracker_portal_endpoint_set_property (GObject      *object,
                                      guint         prop_id,
                                      const GValue *value,
                                      GParamSpec   *pspec)
{
	TrackerPortalEndpoint *endpoint = TRACKER_PORTAL_ENDPOINT (object);

	switch (prop_id) {
	case PROP_GRAPHS:
		endpoint->graphs = g_value_dup_boxed (value);
		break;
	case PROP_PEER:
		endpoint->peer = g_value_dup_string (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
	}
}

static void
tracker_portal_endpoint_get_property (GObject    *object,
                                      guint       prop_id,
                                      GValue     *value,
                                      GParamSpec *pspec)
{
	TrackerPortalEndpoint *endpoint = TRACKER_PORTAL_ENDPOINT (object);

	switch (prop_id) {
	case PROP_GRAPHS:
		g_value_set_boxed (value, endpoint->graphs);
		break;
	case PROP_PEER:
		g_value_set_string (value, endpoint->peer);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
	}
}

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);

	G_OBJECT_CLASS (tracker_portal_endpoint_parent_class)->finalize (object);
}

static void
tracker_portal_endpoint_class_init (TrackerPortalEndpointClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	TrackerEndpointDBusClass *endpoint_dbus_class = TRACKER_ENDPOINT_DBUS_CLASS (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",
		                    "Graphs",
		                    G_TYPE_STRV,
		                    G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
	props[PROP_PEER] =
		g_param_spec_string ("peer",
		                     "DBus peer",
		                     "DBus peer",
		                     NULL,
		                     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);

	g_object_class_install_properties (object_class, N_PROPS, props);
}

static void
tracker_portal_endpoint_init (TrackerPortalEndpoint *portal_endpoint)
{
}

TrackerEndpoint *
tracker_portal_endpoint_new (TrackerSparqlConnection  *sparql_connection,
                             GDBusConnection          *dbus_connection,
                             const gchar              *object_path,
                             const gchar              *peer,
                             const gchar * const      *graphs,
                             GCancellable             *cancellable,
                             GError                  **error)
{
	g_return_val_if_fail (TRACKER_IS_SPARQL_CONNECTION (sparql_connection), NULL);
	g_return_val_if_fail (G_IS_DBUS_CONNECTION (dbus_connection), NULL);
	g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), NULL);
	g_return_val_if_fail (!error || !*error, NULL);

	return g_initable_new (TRACKER_TYPE_PORTAL_ENDPOINT, cancellable, error,
	                       "dbus-connection", dbus_connection,
	                       "sparql-connection", sparql_connection,
	                       "object-path", object_path,
	                       "peer", peer,
	                       "graphs", graphs,
	                       NULL);

}