gtksearchenginetracker.c 13 KB
Newer Older
1
/*
2 3
 * Copyright (C) 2005 Jamie McCracken <jamiemcc@gnome.org>
 * Copyright (C) 2009-2010 Nokia <ivan.frade@nokia.com>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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 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., 59 Temple Place - Suite 330,
 *
19 20 21
 * Authors: Jamie McCracken <jamiemcc@gnome.org>
 *          Jürg Billeter <juerg.billeter@codethink.co.uk>
 *          Martyn Russell <martyn@lanedo.com>
22 23 24 25
 *
 * Based on nautilus-search-engine-tracker.c
 */

26
#include "config.h"
27 28 29
#include <gmodule.h>
#include "gtksearchenginetracker.h"

30 31
/* we dlopen() libtracker at runtime */

32 33
typedef struct _TrackerClient TrackerClient;

Jürg Billeter's avatar
Jürg Billeter committed
34 35
typedef enum
{
36
  TRACKER_UNAVAILABLE = 0,
Jürg Billeter's avatar
Jürg Billeter committed
37
  TRACKER_0_6 = 1 << 0,
38
  TRACKER_0_7 = 1 << 1,
39 40
  TRACKER_0_8 = 1 << 2,
  TRACKER_0_9 = 1 << 3
Jürg Billeter's avatar
Jürg Billeter committed
41 42
} TrackerVersion;

43

44
/* Tracker 0.6 API */
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
typedef void (*TrackerArrayReply) (char **result,
				   GError *error,
				   gpointer user_data);

static TrackerClient *
            (*tracker_connect)                                    (gboolean           enable_warnings,
		                                                   gint               timeout)   = NULL;
static void (*tracker_disconnect)                                 (TrackerClient     *client)    = NULL;
static int  (*tracker_get_version)                                (TrackerClient     *client,
								   GError           **error)     = NULL;
static void (*tracker_cancel_last_call)                           (TrackerClient     *client)    = NULL;

static void (*tracker_search_metadata_by_text_async)              (TrackerClient     *client,
								   const char        *query,
								   TrackerArrayReply  callback,
								   gpointer           user_data) = NULL;
static void (*tracker_search_metadata_by_text_and_location_async) (TrackerClient     *client,
								   const char        *query,
								   const char        *location,
								   TrackerArrayReply  callback,
								   gpointer           user_data) = NULL;


68
/* Tracker 0.7->0.9 API */
69 70 71 72 73 74 75 76
typedef enum {
	TRACKER_CLIENT_ENABLE_WARNINGS = 1 << 0
} TrackerClientFlags;

typedef void (*TrackerReplyGPtrArray) (GPtrArray *result,
				       GError    *error,
				       gpointer   user_data);

77 78 79 80 81 82 83
static TrackerClient *	(*tracker_client_new)                    (TrackerClientFlags      flags,
								  gint                    timeout)   = NULL;
static gchar *		(*tracker_sparql_escape)		 (const gchar            *str)       = NULL;
static guint		(*tracker_resources_sparql_query_async)	 (TrackerClient          *client,
								  const gchar            *query,
								  TrackerReplyGPtrArray   callback,
								  gpointer                user_data) = NULL;
84

85 86 87 88 89

static struct TrackerDlMapping
{
  const char *fn_name;
  gpointer *fn_ptr_ref;
Jürg Billeter's avatar
Jürg Billeter committed
90
  TrackerVersion versions;
91 92
} tracker_dl_mapping[] =
{
Jürg Billeter's avatar
Jürg Billeter committed
93 94 95 96
#define MAP(a,v) { #a, (gpointer *)&a, v }
  MAP (tracker_connect, TRACKER_0_6 | TRACKER_0_7),
  MAP (tracker_disconnect, TRACKER_0_6 | TRACKER_0_7),
  MAP (tracker_get_version, TRACKER_0_6),
97
  MAP (tracker_cancel_last_call, TRACKER_0_6 | TRACKER_0_7 | TRACKER_0_8 | TRACKER_0_9),
Jürg Billeter's avatar
Jürg Billeter committed
98 99
  MAP (tracker_search_metadata_by_text_async, TRACKER_0_6 | TRACKER_0_7),
  MAP (tracker_search_metadata_by_text_and_location_async, TRACKER_0_6 | TRACKER_0_7),
100 101 102
  MAP (tracker_client_new, TRACKER_0_8 | TRACKER_0_9),
  MAP (tracker_sparql_escape, TRACKER_0_8 | TRACKER_0_9),
  MAP (tracker_resources_sparql_query_async, TRACKER_0_8 | TRACKER_0_9)
103 104 105
#undef MAP
};

Jürg Billeter's avatar
Jürg Billeter committed
106
static TrackerVersion
107 108 109
open_libtracker (void)
{
  static gboolean done = FALSE;
110
  static TrackerVersion version = TRACKER_UNAVAILABLE;
111 112 113

  if (!done)
    {
114
      gint i;
115
      GModule *tracker;
116
      GModuleFlags flags;
117

118
      done = TRUE;
119 120
      flags = G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL;

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
      /* So this is the order:
       *
       * - 0.9 (latest unstable)
       * - 0.8 (stable)
       * - 0.7 (unstable, 0.6 sucks so badly)
       * - 0.6 (stable)
       */
      if ((tracker = g_module_open ("libtracker-client-0.9.so.0", flags)) != NULL)
	version = TRACKER_0_9;
      else if ((tracker = g_module_open ("libtracker-client-0.8.so.0", flags)) != NULL)
	version = TRACKER_0_8;
      else if ((tracker = g_module_open ("libtracker-client-0.7.so.0", flags)) != NULL)
	version = TRACKER_0_7;
      else if ((tracker = g_module_open ("libtrackerclient.so.0", flags)) != NULL)
	version = TRACKER_0_6;
      else
	{
	  g_debug ("No tracker backend available");
	  return TRACKER_UNAVAILABLE;
	}
141

142 143
      for (i = 0; i < G_N_ELEMENTS (tracker_dl_mapping); i++)
	{
Jürg Billeter's avatar
Jürg Billeter committed
144 145 146
	  if ((tracker_dl_mapping[i].versions & version) == 0)
	    continue;

147
	  if (!g_module_symbol (tracker,
148
				tracker_dl_mapping[i].fn_name,
149 150 151 152 153 154 155 156 157
				tracker_dl_mapping[i].fn_ptr_ref))
	    {
	      g_warning ("Missing symbol '%s' in libtracker\n",
			 tracker_dl_mapping[i].fn_name);
	      g_module_close (tracker);

	      for (i = 0; i < G_N_ELEMENTS (tracker_dl_mapping); i++)
		tracker_dl_mapping[i].fn_ptr_ref = NULL;

158
	      return TRACKER_UNAVAILABLE;
159 160 161
	    }
	}
    }
Jürg Billeter's avatar
Jürg Billeter committed
162 163

  return version;
164 165
}

166
struct _GtkSearchEngineTrackerPrivate
167
{
168
  GtkQuery	*query;
169
  TrackerClient *client;
170
  gboolean	 query_pending;
Jürg Billeter's avatar
Jürg Billeter committed
171
  TrackerVersion version;
172 173 174 175 176 177 178 179 180
};

G_DEFINE_TYPE (GtkSearchEngineTracker, _gtk_search_engine_tracker, GTK_TYPE_SEARCH_ENGINE);


static void
finalize (GObject *object)
{
  GtkSearchEngineTracker *tracker;
181

182
  tracker = GTK_SEARCH_ENGINE_TRACKER (object);
183 184

  if (tracker->priv->query)
185 186 187 188 189
    {
      g_object_unref (tracker->priv->query);
      tracker->priv->query = NULL;
    }

190 191
  if (tracker->priv->version == TRACKER_0_8 ||
      tracker->priv->version == TRACKER_0_9)
192 193 194
    g_object_unref (tracker->priv->client);
  else
    tracker_disconnect (tracker->priv->client);
195 196 197 198 199

  G_OBJECT_CLASS (_gtk_search_engine_tracker_parent_class)->finalize (object);
}


200 201 202 203 204
/* stolen from tracker sources, tracker.c */
static void
sparql_append_string_literal (GString     *sparql,
                              const gchar *str)
{
205
  gchar *s;
206 207 208 209 210 211 212 213 214 215 216

  s = tracker_sparql_escape (str);

  g_string_append_c (sparql, '"');
  g_string_append (sparql, s);
  g_string_append_c (sparql, '"');

  g_free (s);
}


217
static void
218
search_callback (gpointer results,
219
		 GError  *error,
220 221 222 223 224
		 gpointer user_data)
{
  GtkSearchEngineTracker *tracker;
  gchar **results_p;
  GList *hit_uris;
225 226 227
  GPtrArray *OUT_result;
  gchar *uri;
  gint i;
228

229 230
  tracker = GTK_SEARCH_ENGINE_TRACKER (user_data);
  hit_uris = NULL;
231

232 233
  tracker->priv->query_pending = FALSE;

234
  if (error)
235
    {
236
      _gtk_search_engine_error (GTK_SEARCH_ENGINE (tracker), error->message);
237 238 239 240 241 242
      g_error_free (error);
      return;
    }

  if (!results)
    return;
243

244 245
  if (tracker->priv->version == TRACKER_0_8 ||
      tracker->priv->version == TRACKER_0_9)
246
    {
247
      OUT_result = (GPtrArray*) results;
248

249 250 251 252 253 254
      for (i = 0; i < OUT_result->len; i++)
        {
          uri = g_strdup (((gchar **) OUT_result->pdata[i])[0]);
          if (uri)
            hit_uris = g_list_prepend (hit_uris, uri);
        }
Jürg Billeter's avatar
Jürg Billeter committed
255

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
      g_ptr_array_foreach (OUT_result, (GFunc) g_free, NULL);
      g_ptr_array_free (OUT_result, TRUE);
    }
  else
    {
      for (results_p = results; *results_p; results_p++)
        {
          if (tracker->priv->version == TRACKER_0_6)
            uri = g_filename_to_uri (*results_p, NULL, NULL);
          else
            uri = g_strdup (*results_p);

          if (uri)
            hit_uris = g_list_prepend (hit_uris, uri);
        }
      g_strfreev ((gchar **) results);
272 273 274 275 276
    }

  _gtk_search_engine_hits_added (GTK_SEARCH_ENGINE (tracker), hit_uris);
  _gtk_search_engine_finished (GTK_SEARCH_ENGINE (tracker));

277
  g_list_foreach (hit_uris, (GFunc) g_free, NULL);
278 279 280 281 282 283 284 285
  g_list_free (hit_uris);
}


static void
gtk_search_engine_tracker_start (GtkSearchEngine *engine)
{
  GtkSearchEngineTracker *tracker;
286
  gchar	*search_text, *location, *location_uri;
287
  GString *sparql;
288 289 290 291 292 293 294 295

  tracker = GTK_SEARCH_ENGINE_TRACKER (engine);

  if (tracker->priv->query_pending)
    return;

  if (tracker->priv->query == NULL)
    return;
296

297 298
  search_text = _gtk_query_get_text (tracker->priv->query);
  location_uri = _gtk_query_get_location (tracker->priv->query);
299 300

  location = NULL;
301 302
  if (location_uri)
    {
Jürg Billeter's avatar
Jürg Billeter committed
303 304 305 306 307 308 309 310
      if (tracker->priv->version == TRACKER_0_6)
        {
          location = g_filename_from_uri (location_uri, NULL, NULL);
          g_free (location_uri);
        }
      else
        location = location_uri;
    }
311

312 313
  if (tracker->priv->version == TRACKER_0_8 ||
      tracker->priv->version == TRACKER_0_9)
314
    {
315
      sparql = g_string_new ("SELECT nie:url(?urn) WHERE { ?urn a nfo:FileDataObject; fts:match ");
316 317 318
      sparql_append_string_literal (sparql, search_text);
      if (location)
        {
319
          g_string_append (sparql, " . FILTER (fn:starts-with(nie:url(?urn),");
320 321 322
          sparql_append_string_literal (sparql, location);
          g_string_append (sparql, "))");
        }
323
      g_string_append (sparql, " } ORDER BY DESC(fts:rank(?urn)) ASC(nie:url(?urn))");
324 325 326 327 328 329

      tracker_resources_sparql_query_async (tracker->priv->client,
                                            sparql->str,
                                            (TrackerReplyGPtrArray) search_callback,
                                            tracker);
      g_string_free (sparql, TRUE);
330
    }
331
  else
332
    {
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
      if (location)
        {
          tracker_search_metadata_by_text_and_location_async (tracker->priv->client,
                                                              search_text,
                                                              location,
                                                              (TrackerArrayReply) search_callback,
                                                              tracker);
        }
      else
        {
          tracker_search_metadata_by_text_async (tracker->priv->client,
                                                 search_text,
                                                 (TrackerArrayReply) search_callback,
                                                 tracker);
        }
348 349 350 351
    }

  tracker->priv->query_pending = TRUE;
  g_free (search_text);
352
  g_free (location);
353 354 355 356 357 358
}

static void
gtk_search_engine_tracker_stop (GtkSearchEngine *engine)
{
  GtkSearchEngineTracker *tracker;
359

360
  tracker = GTK_SEARCH_ENGINE_TRACKER (engine);
361 362

  if (tracker->priv->query && tracker->priv->query_pending)
363 364 365 366 367 368 369 370 371 372 373 374 375
    {
      tracker_cancel_last_call (tracker->priv->client);
      tracker->priv->query_pending = FALSE;
    }
}

static gboolean
gtk_search_engine_tracker_is_indexed (GtkSearchEngine *engine)
{
  return TRUE;
}

static void
376
gtk_search_engine_tracker_set_query (GtkSearchEngine *engine,
377 378 379
				     GtkQuery        *query)
{
  GtkSearchEngineTracker *tracker;
380

381
  tracker = GTK_SEARCH_ENGINE_TRACKER (engine);
382 383

  if (query)
384 385 386 387 388 389 390 391 392 393 394 395 396
    g_object_ref (query);

  if (tracker->priv->query)
    g_object_unref (tracker->priv->query);

  tracker->priv->query = query;
}

static void
_gtk_search_engine_tracker_class_init (GtkSearchEngineTrackerClass *class)
{
  GObjectClass *gobject_class;
  GtkSearchEngineClass *engine_class;
397

398 399
  gobject_class = G_OBJECT_CLASS (class);
  gobject_class->finalize = finalize;
400

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
  engine_class = GTK_SEARCH_ENGINE_CLASS (class);
  engine_class->set_query = gtk_search_engine_tracker_set_query;
  engine_class->start = gtk_search_engine_tracker_start;
  engine_class->stop = gtk_search_engine_tracker_stop;
  engine_class->is_indexed = gtk_search_engine_tracker_is_indexed;

  g_type_class_add_private (gobject_class, sizeof (GtkSearchEngineTrackerPrivate));
}

static void
_gtk_search_engine_tracker_init (GtkSearchEngineTracker *engine)
{
  engine->priv = G_TYPE_INSTANCE_GET_PRIVATE (engine, GTK_TYPE_SEARCH_ENGINE_TRACKER, GtkSearchEngineTrackerPrivate);
}


GtkSearchEngine *
_gtk_search_engine_tracker_new (void)
{
  GtkSearchEngineTracker *engine;
  TrackerClient *tracker_client;
Jürg Billeter's avatar
Jürg Billeter committed
422
  TrackerVersion version;
423
  GError *err = NULL;
424

Jürg Billeter's avatar
Jürg Billeter committed
425
  version = open_libtracker ();
426

427 428
  if (version == TRACKER_0_8 ||
      version == TRACKER_0_9)
429 430 431 432 433 434 435 436 437 438
    {
      tracker_client = tracker_client_new (TRACKER_CLIENT_ENABLE_WARNINGS, G_MAXINT);
    }
  else
    {
      if (!tracker_connect)
        return NULL;

      tracker_client = tracker_connect (FALSE, -1);
    }
439 440 441 442

  if (!tracker_client)
    return NULL;

443

Jürg Billeter's avatar
Jürg Billeter committed
444 445 446 447
  if (version == TRACKER_0_6)
    {
      if (!tracker_get_version)
        return NULL;
448

Jürg Billeter's avatar
Jürg Billeter committed
449
      tracker_get_version (tracker_client, &err);
450

Jürg Billeter's avatar
Jürg Billeter committed
451 452 453 454 455 456
      if (err != NULL)
        {
          g_error_free (err);
          tracker_disconnect (tracker_client);
          return NULL;
        }
457 458
    }

459 460 461 462
  engine = g_object_new (GTK_TYPE_SEARCH_ENGINE_TRACKER, NULL);

  engine->priv->client = tracker_client;
  engine->priv->query_pending = FALSE;
Jürg Billeter's avatar
Jürg Billeter committed
463
  engine->priv->version = version;
464

465 466
  return GTK_SEARCH_ENGINE (engine);
}