summaryrefslogtreecommitdiff
path: root/src/libtracker-bus/tracker-bus.vala
blob: 3f1f9f4543cf3debc1f5c7b4198d036358975ca8 (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
/*
 * Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

[DBus (name = "org.freedesktop.Tracker1.Resources", timeout = 2147483647 /* INT_MAX */)]
private interface Tracker.Bus.Resources : GLib.Object {
	public abstract void load (string uri) throws Sparql.Error, DBus.Error;
	[DBus (name = "Load")]
	public abstract async void load_async (string uri) throws Sparql.Error, DBus.Error;
}

[DBus (name = "org.freedesktop.Tracker1.Statistics")]
private interface Tracker.Bus.Statistics : GLib.Object {
	public abstract string[,] Get () throws DBus.Error;
	public async abstract string[,] Get_async () throws DBus.Error;
}

// Imported DBus FD API until we have support with Vala
public extern Tracker.Sparql.Cursor tracker_bus_fd_query (DBus.Connection connection, string query, Cancellable? cancellable) throws Tracker.Sparql.Error, DBus.Error, GLib.IOError;

// Actual class definition
public class Tracker.Bus.Connection : Tracker.Sparql.Connection {
	static DBus.Connection connection;
	static Resources resources_object;
	static Statistics statistics_object;
	static bool initialized;

	public Connection ()
	requires (!initialized) {
		initialized = true;
		
		try {
			connection = DBus.Bus.get (DBus.BusType.SESSION);

			// FIXME: Ideally we would just get these as and when we need them
			resources_object = (Resources) connection.get_object (TRACKER_DBUS_SERVICE,
			                                                      TRACKER_DBUS_OBJECT_RESOURCES,
			                                                      TRACKER_DBUS_INTERFACE_RESOURCES);
			statistics_object = (Statistics) connection.get_object (TRACKER_DBUS_SERVICE,
			                                                        TRACKER_DBUS_OBJECT_STATISTICS,
			                                                        TRACKER_DBUS_INTERFACE_STATISTICS);
		} catch (DBus.Error e) {
			warning ("Could not connect to D-Bus service:'%s': %s", TRACKER_DBUS_INTERFACE_RESOURCES, e.message);
			initialized = false;
			return;
		}
		
		initialized = true;
	}
 
	~Connection () {
		initialized = false;
	}

	public override Sparql.Cursor query (string sparql, Cancellable? cancellable) throws Sparql.Error, IOError {
		try {
			return tracker_bus_fd_query (connection, sparql, cancellable);
		} catch (DBus.Error e) {
			throw new Sparql.Error.INTERNAL (e.message);
		}
	}

	public async override Sparql.Cursor query_async (string sparql, Cancellable? cancellable = null) throws Sparql.Error, IOError {
		try {
			return yield tracker_bus_fd_query_async (connection, sparql, cancellable);
		} catch (DBus.Error e) {
			throw new Sparql.Error.INTERNAL (e.message);
		}
	}

	public override void update (string sparql, int priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws Sparql.Error, IOError {
		try {
			if (priority >= GLib.Priority.DEFAULT) {
				tracker_bus_fd_sparql_update (connection, sparql);
			} else {
				tracker_bus_fd_sparql_batch_update (connection, sparql);
			}
		} catch (DBus.Error e) {
			throw new Sparql.Error.INTERNAL (e.message);
		}
	}

	public async override void update_async (string sparql, int priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws Sparql.Error, IOError {
		try {
			if (priority >= GLib.Priority.DEFAULT) {
				yield tracker_bus_fd_sparql_update_async (connection, sparql, cancellable);
			} else {
				yield tracker_bus_fd_sparql_batch_update_async (connection, sparql, cancellable);
			}
		} catch (DBus.Error e) {
			throw new Sparql.Error.INTERNAL (e.message);
		}
	}

	public async override GLib.PtrArray? update_array_async (string[] sparql, int priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws Sparql.Error, IOError {
		try {
			if (priority >= GLib.Priority.DEFAULT) {
				return yield tracker_bus_fd_sparql_update_array_async (connection, sparql, cancellable);
			} else {
				return yield tracker_bus_fd_sparql_batch_update_array_async (connection, sparql, cancellable);
			}
		} catch (DBus.Error e) {
			throw new Sparql.Error.INTERNAL (e.message);
		}
	}

	public override GLib.Variant? update_blank (string sparql, int priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws Sparql.Error, IOError {
		try {
			GLib.Variant res = null;
			res = tracker_bus_fd_sparql_update_blank (connection, sparql);
			return res;
		} catch (DBus.Error e) {
			throw new Sparql.Error.INTERNAL (e.message);
		}
	}

	public async override GLib.Variant? update_blank_async (string sparql, int priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws Sparql.Error, IOError {
		try {
			GLib.Variant res = null;
			res = yield tracker_bus_fd_sparql_update_blank_async (connection, sparql, cancellable);
			return res;
		} catch (DBus.Error e) {
			throw new Sparql.Error.INTERNAL (e.message);
		}
	}

	public override void load (File file, Cancellable? cancellable = null) throws Sparql.Error, IOError {
		try {
			resources_object.load (file.get_uri ());

			if (cancellable != null && cancellable.is_cancelled ()) {
				throw new IOError.CANCELLED ("Operation was cancelled");
			}
		} catch (DBus.Error e) {
			throw new Sparql.Error.INTERNAL (e.message);
		}
	}
	public async override void load_async (File file, Cancellable? cancellable = null) throws Sparql.Error, IOError {
		try {
			yield resources_object.load_async (file.get_uri ());

			if (cancellable != null && cancellable.is_cancelled ()) {
				throw new IOError.CANCELLED ("Operation was cancelled");
			}
		} catch (DBus.Error e) {
			throw new Sparql.Error.INTERNAL (e.message);
		}
	}

	public override Sparql.Cursor? statistics (Cancellable? cancellable = null) throws Sparql.Error, IOError {
		try {
			string[,] results = statistics_object.Get ();
			Sparql.ValueType[] types = new Sparql.ValueType[2];
			string[] var_names = new string[2];

			if (cancellable != null && cancellable.is_cancelled ()) {
				throw new IOError.CANCELLED ("Operation was cancelled");
			}

			var_names[0] = "class";
			var_names[1] = "count";
			types[0] = Sparql.ValueType.STRING;
			types[1] = Sparql.ValueType.INTEGER;

			return new Tracker.Bus.ArrayCursor ((owned) results,
			                                    results.length[0],
			                                    results.length[1],
			                                    var_names,
			                                    types);
		} catch (DBus.Error e) {
			throw new Sparql.Error.INTERNAL (e.message);
		}
	}

	public async override Sparql.Cursor? statistics_async (Cancellable? cancellable = null) throws Sparql.Error, IOError {
		try {
			string[,] results = yield statistics_object.Get_async ();
			Sparql.ValueType[] types = new Sparql.ValueType[2];
			string[] var_names = new string[2];

			if (cancellable != null && cancellable.is_cancelled ()) {
				throw new IOError.CANCELLED ("Operation was cancelled");
			}

			var_names[0] = "class";
			var_names[1] = "count";
			types[0] = Sparql.ValueType.STRING;
			types[1] = Sparql.ValueType.INTEGER;

			return new Tracker.Bus.ArrayCursor ((owned) results,
			                                    results.length[0],
			                                    results.length[1],
			                                    var_names,
			                                    types);
		} catch (DBus.Error e) {
			throw new Sparql.Error.INTERNAL (e.message);
		}
	}
}

public Tracker.Sparql.Connection module_init () {
	Tracker.Sparql.Connection plugin = new Tracker.Bus.Connection ();
	return plugin;
}