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 */
25 #include <atalk/util.h>
26 #include <atalk/errchk.h>
27 #include <atalk/logger.h>
28 #include <atalk/unix.h>
29 #include <atalk/spotlight.h>
31 #include "slmod_tracker_0_6_parser.h"
33 #define MAX_SL_RESULTS 20
35 TrackerClient *client;
37 static int sl_mod_init(void *p)
43 LOG(log_info, logtype_sl, "Initializing Tracker 0.6 Spotlight module");
46 setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=/tmp/spotlight.ipc", 1);
49 client = tracker_connect(FALSE);
53 LOG(log_error, logtype_sl, "Failed connecting to Tracker");
62 static int sl_mod_start_search(void *p)
68 LOG(log_debug, logtype_sl, "sl_mod_start_search: Spotlight query: \"%s\"", slq->slq_qstring);
70 EC_ZERO_LOG( map_spotlight_to_tracker_0_6_query(slq,
71 (ServiceType *)(&slq->slq_service),
72 &slq->slq_trackerquery) );
74 LOG(log_debug, logtype_sl, "sl_mod_start_search: Tracker service: %s, query: \"%s\"",
75 tracker_type_to_service_name(slq->slq_service),
76 slq->slq_trackerquery ? slq->slq_trackerquery : "false");
78 if (slq->slq_trackerquery)
79 slq->slq_state = SLQ_STATE_RUNNING;
81 slq->slq_state = SLQ_STATE_DONE;
87 static int add_filemeta(sl_array_t *reqinfo, sl_array_t *fm_array, cnid_t id, const char *path)
94 if ((metacount = talloc_array_length(reqinfo->dd_talloc_array)) == 0) {
95 dalloc_add_copy(fm_array, &nil, sl_nil_t);
99 LOG(log_debug, logtype_sl, "add_filemeta: metadata count: %d", metacount);
101 meta = talloc_zero(fm_array, sl_array_t);
103 for (i = 0; i < metacount; i++) {
104 if (STRCMP(reqinfo->dd_talloc_array[i], ==, "kMDItemDisplayName")) {
106 if ((p = strrchr(path, '/'))) {
107 name = dalloc_strdup(meta, p + 1);
108 dalloc_add(meta, name, "char *");
111 dalloc_add_copy(meta, &nil, sl_nil_t);
115 dalloc_add(fm_array, meta, sl_array_t);
121 static int sl_mod_fetch_result(void *p)
125 GError *error = NULL;
130 sl_array_t *fm_array;
133 gboolean qres, firstmatch = true;
134 gchar **results = NULL, **result;
137 cnids = talloc_zero(slq->slq_reply, sl_cnids_t);
138 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
139 cnids->ca_unkn1 = 0xadd;
140 cnids->ca_context = slq->slq_ctx2;
142 /* Prepare FileMeta */
143 fm = talloc_zero(slq->slq_reply, sl_filemeta_t);
144 fm_array = talloc_zero(fm, sl_array_t);
145 dalloc_add(fm, fm_array, sl_array_t);
147 if (slq->slq_state == SLQ_STATE_RUNNING) {
150 results = tracker_search_text(client,
153 slq->slq_trackerquery,
160 slq->slq_state = SLQ_STATE_DONE;
161 LOG(log_error, logtype_sl, "Couldn't query Tracker: '%s'", error->message);
162 g_clear_error(&error);
167 slq->slq_state = SLQ_STATE_DONE;
168 LOG(log_debug, logtype_sl, "sl_mod_fetch_result: no results found");
172 for (result = results; *result; result++) {
173 LOG(log_debug, logtype_sl, "sl_mod_fetch_result: result %d: %s", slq->slq_offset, *result);
176 /* For some reason the list of results always starts with a nil entry */
177 dalloc_add_copy(fm_array, &nil, sl_nil_t);
181 if ((id = cnid_for_path(slq->slq_vol->v_cdb, slq->slq_vol->v_path, *result, &did)) == CNID_INVALID) {
182 LOG(log_error, logtype_sl, "sl_mod_fetch_result: cnid_for_path error: %s", *result);
185 LOG(log_debug, logtype_sl, "Result %d: CNID: %" PRIu32 ", path: \"%s\"", i, ntohl(id), *result);
188 dalloc_add_copy(cnids->ca_cnids, &uint64, uint64_t);
189 add_filemeta(slq->slq_reqinfo, fm_array, id, *result);
196 if (i < MAX_SL_RESULTS)
197 slq->slq_state = SLQ_STATE_DONE;
200 uint64 = (i > 0) ? 35 : 0; /* OS X AFP server returns 35 here if results are found */
201 dalloc_add_copy(slq->slq_reply, &uint64, uint64_t);
202 dalloc_add(slq->slq_reply, cnids, sl_cnids_t);
203 dalloc_add(slq->slq_reply, fm, sl_filemeta_t);
213 /* Free ressources allocated in this module */
214 static int sl_mod_close_query(void *p)
223 static int sl_mod_error(void *p)
235 static int sl_mod_index_file(const void *p)
240 struct sl_module_export sl_mod = {