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 */
21 #include <tracker-sparql.h>
23 #include <atalk/util.h>
24 #include <atalk/errchk.h>
25 #include <atalk/logger.h>
27 #include "spotlight.h"
29 static TrackerSparqlConnection *connection;
31 const char *tracker_to_unix_path(const char *path)
33 /* just skip 'file://' */
37 static int sl_mod_init(void *p)
43 LOG(log_note, logtype_sl, "sl_mod_init: %s", msg);
44 setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=/tmp/spotlight.ipc", 1);
47 connection = tracker_sparql_connection_get(NULL, &error);
49 LOG(log_error, logtype_sl, "Couldn't obtain a direct connection to the Tracker store: %s",
50 error ? error->message : "unknown error");
51 g_clear_error(&error);
60 * Return talloced query from query string
63 static const gchar *map_spotlight_to_sparql_query(slq_t *slq)
66 const gchar *sparql_query;
67 const char *sparql_query_format = "SELECT nie:url(?f) WHERE { ?f nie:url ?name FILTER regex(?name, \"%s\")}";
68 const char *slquery = slq->slq_qstring;
71 LOG(log_debug, logtype_sl, "query_word_from_sl_query: \"%s\"", slquery);
73 EC_NULL_LOG( word = strstr(slquery, "*==") );
74 word += 4; /* skip *== and the left enclosing quote */
75 EC_NULL( word = dalloc_strdup(slq, word) );
77 EC_NULL_LOG( p = strchr(word, '*') );
80 sparql_query = talloc_asprintf(slq, sparql_query_format, word);
82 LOG(log_debug, logtype_sl, "query_word_from_sl_query: \"%s\"", sparql_query);
90 static void tracker_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
92 slq_t *slq = user_data;
93 TrackerSparqlCursor *cursor;
96 LOG(log_debug, logtype_sl, "tracker_cb");
98 cursor = tracker_sparql_connection_query_finish(connection, res, &error);
101 LOG(log_error, logtype_sl, "sl_mod_fetch_result: Couldn't query the Tracker Store: '%s'",
102 error ? error->message : "unknown error");
103 g_clear_error(&error);
107 slq->slq_tracker_cursor = cursor;
110 static int sl_mod_start_search(void *p)
114 const gchar *sparql_query;
115 GError *error = NULL;
117 LOG(log_debug, logtype_sl, "sl_mod_start_search: Spotlight query string: \"%s\"", slq->slq_qstring);
119 EC_NULL_LOG( sparql_query = map_spotlight_to_sparql_query(slq) );
120 LOG(log_debug, logtype_sl, "sl_mod_start_search: SPARQL query: \"%s\"", sparql_query);
123 /* Start the async query */
124 tracker_sparql_connection_query_async(connection, sparql_query, NULL, tracker_cb, slq);
127 slq->slq_tracker_cursor = tracker_sparql_connection_query(connection, sparql_query, NULL, &error);
129 LOG(log_error, logtype_sl, "Couldn't query the Tracker Store: '%s'",
130 error ? error->message : "unknown error");
131 g_clear_error(&error);
134 slq->slq_state = SLQ_STATE_RUNNING;
140 static int add_filemeta(sl_array_t *reqinfo, sl_array_t *fm_array, cnid_t id, const char *path)
147 if ((metacount = talloc_array_length(reqinfo->dd_talloc_array)) == 0)
150 LOG(log_debug, logtype_sl, "add_filemeta: metadata count: %d", metacount);
152 meta = talloc_zero(fm_array, sl_array_t);
154 for (i = 0; i < metacount; i++) {
155 if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemDisplayName")) {
157 if ((p = strrchr(path, '/'))) {
158 name = dalloc_strdup(meta, p + 1);
159 dalloc_add(meta, name, "char *");
162 dalloc_add_copy(meta, &nil, sl_nil_t);
166 dalloc_add(fm_array, meta, sl_array_t);
172 static int sl_mod_fetch_result(void *p)
176 GError *error = NULL;
182 sl_array_t *fm_array;
185 if (!slq->slq_tracker_cursor) {
186 LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
191 cnids = talloc_zero(slq->slq_reply, sl_cnids_t);
192 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
193 cnids->ca_unkn1 = 0xadd;
194 cnids->ca_context = slq->slq_ctx2;
196 /* Prepare FileMeta */
197 fm = talloc_zero(slq->slq_reply, sl_filemeta_t);
198 fm_array = talloc_zero(fm, sl_array_t);
199 dalloc_add(fm, fm_array, sl_array_t);
201 /* For some reason the list of results always starts with a nil entry */
203 dalloc_add_copy(fm_array, &nil, sl_nil_t);
205 while (tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error)) {
206 EC_NULL_LOG( path = tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL) );
207 path = tracker_to_unix_path(path);
208 LOG(log_debug, logtype_sl, "sl_mod_fetch_result: path(volpath: %s): \"%s\"", slq->slq_vol->v_path, path);
209 if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did)) == CNID_INVALID) {
210 LOG(log_error, logtype_sl, "sl_mod_fetch_result: cnid_for_path error");
213 LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i++, ntohl(id), path);
215 dalloc_add_copy(cnids->ca_cnids, &uint64, uint64_t);
216 add_filemeta(slq->slq_reqinfo, fm_array, id, path);
217 //dalloc_add_copy(fm_array, &nil, sl_nil_t);
219 dalloc_add(slq->slq_reply, cnids, sl_cnids_t);
220 dalloc_add(slq->slq_reply, fm, sl_filemeta_t);
223 if (slq->slq_tracker_cursor) {
224 g_object_unref(slq->slq_tracker_cursor);
225 slq->slq_tracker_cursor = NULL;
230 /* Free ressources allocated in this module */
231 static int sl_mod_close_query(void *p)
236 if (slq->slq_tracker_cursor) {
237 g_object_unref(slq->slq_tracker_cursor);
238 slq->slq_tracker_cursor = NULL;
245 static int sl_mod_error(void *p)
253 if (slq->slq_tracker_cursor) {
254 g_object_unref(slq->slq_tracker_cursor);
255 slq->slq_tracker_cursor = NULL;
262 struct sl_module_export sl_mod = {