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 = talloc_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);
139 static int add_filemeta(DALLOC_CTX *reqinfo, const int metacount, sl_array_t *fm_array, cnid_t id, const char *path)
146 LOG(log_debug, logtype_sl, "add_filemeta: metadata count: %d", metacount);
148 meta = talloc_zero(fm_array, sl_array_t);
151 dalloc_add(meta, &nil, sl_nil_t);
154 dalloc_add(fm_array, meta, sl_array_t);
160 static int sl_mod_fetch_result(void *p)
164 GError *error = NULL;
170 sl_array_t *fm_array;
173 if (!slq->slq_tracker_cursor) {
174 LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
179 cnids = talloc_zero(slq->slq_reply, sl_cnids_t);
180 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
181 cnids->ca_unkn1 = 0xadd;
182 cnids->ca_context = slq->slq_ctx2;
184 /* Prepare FileMeta */
185 fm = talloc_zero(slq->slq_reply, sl_filemeta_t);
186 fm_array = talloc_zero(fm, sl_array_t);
187 dalloc_add(fm, fm_array, sl_array_t);
189 while (tracker_sparql_cursor_next(slq->slq_tracker_cursor, NULL, &error)) {
190 EC_NULL_LOG( path = tracker_sparql_cursor_get_string(slq->slq_tracker_cursor, 0, NULL) );
191 path = tracker_to_unix_path(path);
192 LOG(log_debug, logtype_sl, "sl_mod_fetch_result: path(volpath: %s): \"%s\"", slq->slq_vol->v_path, path);
193 if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, path, &did)) == CNID_INVALID) {
194 LOG(log_error, logtype_sl, "sl_mod_fetch_result: cnid_for_path error");
197 LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i++, ntohl(id), path);
199 dalloc_add(cnids->ca_cnids, &uint64, uint64_t);
200 add_filemeta(slq->slq_reqinfo, slq->slq_metacount, fm_array, id, path);
202 dalloc_add(slq->slq_reply, cnids, sl_cnids_t);
203 dalloc_add(slq->slq_reply, fm, sl_filemeta_t);
206 if (slq->slq_tracker_cursor)
207 g_object_unref(slq->slq_tracker_cursor);
211 struct sl_module_export sl_mod = {