2 Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 #endif /* HAVE_CONFIG_H */
22 #include <tracker-sparql.h>
24 #include <atalk/util.h>
25 #include <atalk/errchk.h>
26 #include <atalk/logger.h>
28 #include "spotlight.h"
30 static TrackerSparqlConnection *connection;
32 char *tracker_to_unix_path(const char *uri)
38 EC_NULL_LOG( f = g_file_new_for_uri(uri) );
39 EC_NULL_LOG( path = g_file_get_path(f) );
49 static int sl_mod_init(void *p)
55 LOG(log_note, logtype_sl, "sl_mod_init: %s", msg);
56 setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=/tmp/spotlight.ipc", 1);
59 connection = tracker_sparql_connection_get(NULL, &error);
61 LOG(log_error, logtype_sl, "Couldn't obtain a direct connection to the Tracker store: %s",
62 error ? error->message : "unknown error");
63 g_clear_error(&error);
72 * Return talloced query from query string
75 static const gchar *map_spotlight_to_sparql_query(slq_t *slq)
78 const gchar *sparql_query;
79 const char *sparql_query_format = "SELECT nie:url(?uri) WHERE {?uri fts:match '%s' . ?uri nie:url ?url FILTER(fn:starts-with(?url, 'file://%s')) }";
80 const char *slquery = slq->slq_qstring;
83 LOG(log_debug, logtype_sl, "query_word_from_sl_query: \"%s\"", slquery);
85 EC_NULL_LOG( word = strstr(slquery, "*==") );
86 word += 4; /* skip *== and the left enclosing quote */
87 EC_NULL( word = dalloc_strdup(slq, word) );
89 EC_NULL_LOG( p = strchr(word, '*') );
91 sparql_query = talloc_asprintf(slq, sparql_query_format, word, slq->slq_vol->v_path);
93 LOG(log_debug, logtype_sl, "query_word_from_sl_query: \"%s\"", sparql_query);
101 static void tracker_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
103 slq_t *slq = user_data;
104 TrackerSparqlCursor *cursor;
105 GError *error = NULL;
107 LOG(log_debug, logtype_sl, "tracker_cb");
109 cursor = tracker_sparql_connection_query_finish(connection, res, &error);
112 LOG(log_error, logtype_sl, "sl_mod_fetch_result: Couldn't query the Tracker Store: '%s'",
113 error ? error->message : "unknown error");
114 g_clear_error(&error);
118 slq->slq_tracker_cursor = cursor;
121 static int sl_mod_start_search(void *p)
125 const gchar *sparql_query;
126 GError *error = NULL;
128 LOG(log_debug, logtype_sl, "sl_mod_start_search: Spotlight query string: \"%s\"", slq->slq_qstring);
130 EC_NULL_LOG( sparql_query = map_spotlight_to_sparql_query(slq) );
131 LOG(log_debug, logtype_sl, "sl_mod_start_search: SPARQL query: \"%s\"", sparql_query);
134 /* Start the async query */
135 tracker_sparql_connection_query_async(connection, sparql_query, NULL, tracker_cb, slq);
138 slq->slq_tracker_cursor = tracker_sparql_connection_query(connection, sparql_query, NULL, &error);
140 LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
141 error ? error->message : "unknown error");
142 g_clear_error(&error);
145 slq->slq_state = SLQ_STATE_RUNNING;
151 static int add_filemeta(sl_array_t *reqinfo, sl_array_t *fm_array, cnid_t id, const char *path)
158 if ((metacount = talloc_array_length(reqinfo->dd_talloc_array)) == 0)
161 LOG(log_debug, logtype_sl, "add_filemeta: metadata count: %d", metacount);
163 meta = talloc_zero(fm_array, sl_array_t);
165 for (i = 0; i < metacount; i++) {
166 if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemDisplayName")) {
168 if ((p = strrchr(path, '/'))) {
169 name = dalloc_strdup(meta, p + 1);
170 dalloc_add(meta, name, "char *");
173 dalloc_add_copy(meta, &nil, sl_nil_t);
177 dalloc_add(fm_array, meta, sl_array_t);
183 static int sl_mod_fetch_result(void *p)
187 GError *error = NULL;
194 sl_array_t *fm_array;
197 if (!slq->slq_tracker_cursor) {
198 LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
203 cnids = talloc_zero(slq->slq_reply, sl_cnids_t);
204 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
205 cnids->ca_unkn1 = 0xadd;
206 cnids->ca_context = slq->slq_ctx2;
208 /* Prepare FileMeta */
209 fm = talloc_zero(slq->slq_reply, sl_filemeta_t);
210 fm_array = talloc_zero(fm, sl_array_t);
211 dalloc_add(fm, fm_array, sl_array_t);
213 /* For some reason the list of results always starts with a nil entry */
215 dalloc_add_copy(fm_array, &nil, sl_nil_t);
217 LOG(log_debug, logtype_sl, "sl_mod_fetch_result: now interating Tracker results cursor");
219 while (tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error)) {
220 uri = tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL);
221 LOG(log_debug, logtype_sl, "Result %d: uri: \"%s\"", i, uri);
223 EC_NULL_LOG( path = tracker_to_unix_path(uri) );
224 LOG(log_debug, logtype_sl, "sl_mod_fetch_result: path(volpath: %s): \"%s\"", slq->slq_vol->v_path, path);
226 if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did)) == CNID_INVALID) {
227 LOG(log_error, logtype_sl, "sl_mod_fetch_result: cnid_for_path error");
230 LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i++, ntohl(id), path);
233 dalloc_add_copy(cnids->ca_cnids, &uint64, uint64_t);
234 add_filemeta(slq->slq_reqinfo, fm_array, id, path);
241 LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
242 error ? error->message : "unknown error");
243 g_clear_error (&error);
247 dalloc_add(slq->slq_reply, cnids, sl_cnids_t);
248 dalloc_add(slq->slq_reply, fm, sl_filemeta_t);
251 if (slq->slq_tracker_cursor) {
252 g_object_unref(slq->slq_tracker_cursor);
253 slq->slq_tracker_cursor = NULL;
258 /* Free ressources allocated in this module */
259 static int sl_mod_close_query(void *p)
264 if (slq->slq_tracker_cursor) {
265 g_object_unref(slq->slq_tracker_cursor);
266 slq->slq_tracker_cursor = NULL;
273 static int sl_mod_error(void *p)
281 if (slq->slq_tracker_cursor) {
282 g_object_unref(slq->slq_tracker_cursor);
283 slq->slq_tracker_cursor = NULL;
290 struct sl_module_export sl_mod = {