2007-05-02 22:51:43 +00:00
|
|
|
|
/*
|
2011-09-13 01:31:12 +00:00
|
|
|
|
* Copyright (C) 2009-2011 Nokia <ivan.frade@nokia.com>
|
2007-05-02 22:51:43 +00:00
|
|
|
|
*
|
|
|
|
|
* 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
|
2012-02-27 13:01:10 +00:00
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2007-05-02 22:51:43 +00:00
|
|
|
|
*
|
2010-12-07 12:15:42 +00:00
|
|
|
|
* Authors: Jürg Billeter <juerg.billeter@codethink.co.uk>
|
2010-04-09 13:21:45 +00:00
|
|
|
|
* Martyn Russell <martyn@lanedo.com>
|
2007-05-02 22:51:43 +00:00
|
|
|
|
*
|
|
|
|
|
* Based on nautilus-search-engine-tracker.c
|
|
|
|
|
*/
|
|
|
|
|
|
2008-06-22 14:28:52 +00:00
|
|
|
|
#include "config.h"
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
2011-09-13 01:31:12 +00:00
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2010-12-07 12:15:42 +00:00
|
|
|
|
#include <gio/gio.h>
|
|
|
|
|
#include <gmodule.h>
|
2011-03-29 00:27:16 +00:00
|
|
|
|
#include <gdk/gdk.h>
|
2011-09-13 01:31:12 +00:00
|
|
|
|
#include <gtk/gtk.h>
|
2007-06-22 14:32:22 +00:00
|
|
|
|
|
2010-12-07 12:15:42 +00:00
|
|
|
|
#include "gtksearchenginetracker.h"
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
2011-09-13 01:31:12 +00:00
|
|
|
|
#define DBUS_SERVICE_RESOURCES "org.freedesktop.Tracker1"
|
|
|
|
|
#define DBUS_PATH_RESOURCES "/org/freedesktop/Tracker1/Resources"
|
|
|
|
|
#define DBUS_INTERFACE_RESOURCES "org.freedesktop.Tracker1.Resources"
|
|
|
|
|
|
|
|
|
|
#define DBUS_SERVICE_STATUS "org.freedesktop.Tracker1"
|
|
|
|
|
#define DBUS_PATH_STATUS "/org/freedesktop/Tracker1/Status"
|
|
|
|
|
#define DBUS_INTERFACE_STATUS "org.freedesktop.Tracker1.Status"
|
|
|
|
|
|
|
|
|
|
/* Time in second to wait for service before deciding it's not available */
|
|
|
|
|
#define WAIT_TIMEOUT_SECONDS 1
|
|
|
|
|
|
|
|
|
|
/* Time in second to wait for query results to come back */
|
|
|
|
|
#define QUERY_TIMEOUT_SECONDS 10
|
|
|
|
|
|
2010-12-07 12:15:42 +00:00
|
|
|
|
/* If defined, we use fts:match, this has to be enabled in Tracker to
|
|
|
|
|
* work which it usually is. The alternative is to undefine it and
|
2014-02-07 18:32:47 +00:00
|
|
|
|
* use filename matching instead. This doesn’t use the content of the
|
2010-12-07 12:15:42 +00:00
|
|
|
|
* file however.
|
|
|
|
|
*/
|
2015-02-28 21:42:39 +00:00
|
|
|
|
#define FTS_MATCHING
|
2010-12-07 12:15:42 +00:00
|
|
|
|
|
2015-06-19 04:41:10 +00:00
|
|
|
|
struct _GtkSearchEngineTracker
|
2007-05-02 22:51:43 +00:00
|
|
|
|
{
|
2015-06-19 04:41:10 +00:00
|
|
|
|
GtkSearchEngine parent;
|
2011-09-13 01:31:12 +00:00
|
|
|
|
GDBusConnection *connection;
|
|
|
|
|
GCancellable *cancellable;
|
2010-12-07 12:15:42 +00:00
|
|
|
|
GtkQuery *query;
|
|
|
|
|
gboolean query_pending;
|
2015-06-19 04:12:46 +00:00
|
|
|
|
GPtrArray *indexed_locations;
|
2007-05-02 22:51:43 +00:00
|
|
|
|
};
|
|
|
|
|
|
2015-06-19 04:41:10 +00:00
|
|
|
|
struct _GtkSearchEngineTrackerClass
|
|
|
|
|
{
|
|
|
|
|
GtkSearchEngineClass parent_class;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GtkSearchEngineTracker, _gtk_search_engine_tracker, GTK_TYPE_SEARCH_ENGINE)
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
finalize (GObject *object)
|
|
|
|
|
{
|
|
|
|
|
GtkSearchEngineTracker *tracker;
|
2010-04-09 13:21:45 +00:00
|
|
|
|
|
2011-09-13 01:31:12 +00:00
|
|
|
|
g_debug ("Finalizing GtkSearchEngineTracker");
|
|
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
|
tracker = GTK_SEARCH_ENGINE_TRACKER (object);
|
2010-04-09 13:21:45 +00:00
|
|
|
|
|
2015-06-19 04:41:10 +00:00
|
|
|
|
if (tracker->cancellable)
|
2011-09-13 01:31:12 +00:00
|
|
|
|
{
|
2015-06-19 04:41:10 +00:00
|
|
|
|
g_cancellable_cancel (tracker->cancellable);
|
|
|
|
|
g_object_unref (tracker->cancellable);
|
2011-09-13 01:31:12 +00:00
|
|
|
|
}
|
2010-12-07 12:15:42 +00:00
|
|
|
|
|
2015-06-19 04:41:10 +00:00
|
|
|
|
g_clear_object (&tracker->query);
|
|
|
|
|
g_clear_object (&tracker->connection);
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
2015-06-19 04:41:10 +00:00
|
|
|
|
g_ptr_array_unref (tracker->indexed_locations);
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (_gtk_search_engine_tracker_parent_class)->finalize (object);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-13 01:31:12 +00:00
|
|
|
|
static GDBusConnection *
|
|
|
|
|
get_connection (void)
|
2010-12-07 12:15:42 +00:00
|
|
|
|
{
|
2011-09-13 01:31:12 +00:00
|
|
|
|
GDBusConnection *connection;
|
2010-12-07 12:15:42 +00:00
|
|
|
|
GError *error = NULL;
|
2011-09-13 01:31:12 +00:00
|
|
|
|
GVariant *reply;
|
2010-03-19 13:40:11 +00:00
|
|
|
|
|
2011-09-13 01:31:12 +00:00
|
|
|
|
/* Normally I hate sync calls with UIs, but we need to return NULL
|
|
|
|
|
* or a GtkSearchEngine as a result of this function.
|
|
|
|
|
*/
|
|
|
|
|
connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
|
2010-03-19 13:40:11 +00:00
|
|
|
|
|
2010-12-07 12:15:42 +00:00
|
|
|
|
if (error)
|
|
|
|
|
{
|
2011-09-13 01:31:12 +00:00
|
|
|
|
g_debug ("Couldn't connect to D-Bus session bus, %s", error->message);
|
2010-12-07 12:15:42 +00:00
|
|
|
|
g_error_free (error);
|
2011-09-13 01:31:12 +00:00
|
|
|
|
return NULL;
|
2010-12-07 12:15:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-13 01:31:12 +00:00
|
|
|
|
/* If connection is set, we know it worked. */
|
|
|
|
|
g_debug ("Finding out if Tracker is available via D-Bus...");
|
|
|
|
|
|
|
|
|
|
/* We only wait 1 second max, we expect it to be very fast. If we
|
|
|
|
|
* don't get a response by then, clearly we're replaying a journal
|
|
|
|
|
* or cleaning up the DB internally. Either way, services is not
|
|
|
|
|
* available.
|
|
|
|
|
*
|
|
|
|
|
* We use the sync call here because we don't expect to be waiting
|
|
|
|
|
* long enough to block UI painting.
|
|
|
|
|
*/
|
|
|
|
|
reply = g_dbus_connection_call_sync (connection,
|
|
|
|
|
DBUS_SERVICE_STATUS,
|
|
|
|
|
DBUS_PATH_STATUS,
|
|
|
|
|
DBUS_INTERFACE_STATUS,
|
|
|
|
|
"Wait",
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
G_DBUS_CALL_FLAGS_NONE,
|
|
|
|
|
WAIT_TIMEOUT_SECONDS * 1000,
|
|
|
|
|
NULL,
|
|
|
|
|
&error);
|
2010-12-07 12:15:42 +00:00
|
|
|
|
|
2011-09-13 01:31:12 +00:00
|
|
|
|
if (error)
|
|
|
|
|
{
|
|
|
|
|
g_debug ("Tracker is not available, %s", error->message);
|
|
|
|
|
g_error_free (error);
|
|
|
|
|
g_object_unref (connection);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2010-03-19 13:40:11 +00:00
|
|
|
|
|
2011-09-13 01:31:12 +00:00
|
|
|
|
g_variant_unref (reply);
|
2011-03-25 09:53:05 +00:00
|
|
|
|
|
2011-09-13 01:31:12 +00:00
|
|
|
|
g_debug ("Tracker is ready");
|
2011-03-25 09:53:05 +00:00
|
|
|
|
|
2011-09-13 01:31:12 +00:00
|
|
|
|
return connection;
|
2010-12-07 12:15:42 +00:00
|
|
|
|
}
|
2010-03-19 13:40:11 +00:00
|
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
|
static void
|
2011-09-13 01:31:12 +00:00
|
|
|
|
get_query_results (GtkSearchEngineTracker *engine,
|
|
|
|
|
const gchar *sparql,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
2007-05-02 22:51:43 +00:00
|
|
|
|
{
|
2015-06-19 04:41:10 +00:00
|
|
|
|
g_dbus_connection_call (engine->connection,
|
2011-09-13 01:31:12 +00:00
|
|
|
|
DBUS_SERVICE_RESOURCES,
|
|
|
|
|
DBUS_PATH_RESOURCES,
|
|
|
|
|
DBUS_INTERFACE_RESOURCES,
|
|
|
|
|
"SparqlQuery",
|
|
|
|
|
g_variant_new ("(s)", sparql),
|
|
|
|
|
NULL,
|
|
|
|
|
G_DBUS_CALL_FLAGS_NONE,
|
|
|
|
|
QUERY_TIMEOUT_SECONDS * 1000,
|
2015-06-19 04:41:10 +00:00
|
|
|
|
engine->cancellable,
|
2011-09-13 01:31:12 +00:00
|
|
|
|
callback,
|
|
|
|
|
user_data);
|
|
|
|
|
}
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
2011-09-13 01:31:12 +00:00
|
|
|
|
/* Stolen from libtracker-sparql */
|
|
|
|
|
static gchar *
|
|
|
|
|
sparql_escape_string (const gchar *literal)
|
|
|
|
|
{
|
|
|
|
|
GString *str;
|
|
|
|
|
const gchar *p;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (literal != NULL, NULL);
|
|
|
|
|
|
|
|
|
|
str = g_string_new ("");
|
|
|
|
|
p = literal;
|
|
|
|
|
|
|
|
|
|
while (TRUE)
|
|
|
|
|
{
|
|
|
|
|
gsize len;
|
|
|
|
|
|
|
|
|
|
if (!((*p) != '\0'))
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
len = strcspn ((const gchar *) p, "\t\n\r\b\f\"\\");
|
|
|
|
|
g_string_append_len (str, (const gchar *) p, (gssize) ((glong) len));
|
|
|
|
|
p = p + len;
|
|
|
|
|
|
|
|
|
|
switch (*p)
|
|
|
|
|
{
|
|
|
|
|
case '\t':
|
|
|
|
|
g_string_append (str, "\\t");
|
|
|
|
|
break;
|
|
|
|
|
case '\n':
|
|
|
|
|
g_string_append (str, "\\n");
|
|
|
|
|
break;
|
|
|
|
|
case '\r':
|
|
|
|
|
g_string_append (str, "\\r");
|
|
|
|
|
break;
|
|
|
|
|
case '\b':
|
|
|
|
|
g_string_append (str, "\\b");
|
|
|
|
|
break;
|
|
|
|
|
case '\f':
|
|
|
|
|
g_string_append (str, "\\f");
|
|
|
|
|
break;
|
|
|
|
|
case '"':
|
|
|
|
|
g_string_append (str, "\\\"");
|
|
|
|
|
break;
|
|
|
|
|
case '\\':
|
|
|
|
|
g_string_append (str, "\\\\");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p++;
|
|
|
|
|
}
|
|
|
|
|
return g_string_free (str, FALSE);
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-07 12:15:42 +00:00
|
|
|
|
static void
|
|
|
|
|
sparql_append_string_literal (GString *sparql,
|
2015-02-28 21:59:36 +00:00
|
|
|
|
const gchar *str,
|
2015-11-20 18:04:39 +00:00
|
|
|
|
gboolean glob,
|
2016-05-04 12:23:17 +00:00
|
|
|
|
gboolean is_dir_uri,
|
|
|
|
|
gboolean quoted)
|
2010-12-07 12:15:42 +00:00
|
|
|
|
{
|
|
|
|
|
gchar *s;
|
2009-09-30 04:43:51 +00:00
|
|
|
|
|
2011-09-13 01:31:12 +00:00
|
|
|
|
s = sparql_escape_string (str);
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
2010-12-07 12:15:42 +00:00
|
|
|
|
g_string_append_c (sparql, '"');
|
2016-05-04 12:23:17 +00:00
|
|
|
|
if (quoted)
|
|
|
|
|
g_string_append (sparql, "\\\"");
|
2010-12-07 12:15:42 +00:00
|
|
|
|
g_string_append (sparql, s);
|
2015-02-28 21:59:36 +00:00
|
|
|
|
|
2015-11-20 18:04:39 +00:00
|
|
|
|
if (is_dir_uri)
|
|
|
|
|
g_string_append_c (sparql, '/');
|
2016-05-04 12:23:17 +00:00
|
|
|
|
if (quoted)
|
|
|
|
|
g_string_append (sparql, "\\\"");
|
2016-06-06 22:27:19 +00:00
|
|
|
|
if (glob)
|
|
|
|
|
g_string_append_c (sparql, '*');
|
2010-12-07 12:15:42 +00:00
|
|
|
|
g_string_append_c (sparql, '"');
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
2010-12-07 12:15:42 +00:00
|
|
|
|
g_free (s);
|
2007-05-02 22:51:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-19 18:08:10 +00:00
|
|
|
|
static void
|
|
|
|
|
sparql_append_string_literal_lower_case (GString *sparql,
|
|
|
|
|
const gchar *str)
|
|
|
|
|
{
|
|
|
|
|
gchar *s;
|
|
|
|
|
|
|
|
|
|
s = g_utf8_strdown (str, -1);
|
2016-05-04 12:23:17 +00:00
|
|
|
|
sparql_append_string_literal (sparql, s, FALSE, FALSE, FALSE);
|
2011-02-19 18:08:10 +00:00
|
|
|
|
g_free (s);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-13 01:31:12 +00:00
|
|
|
|
static void
|
|
|
|
|
query_callback (GObject *object,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GtkSearchEngineTracker *tracker;
|
|
|
|
|
GList *hits;
|
|
|
|
|
GVariant *reply;
|
|
|
|
|
GVariant *r;
|
|
|
|
|
GVariantIter iter;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
gint i, n;
|
2015-06-18 18:31:02 +00:00
|
|
|
|
GtkSearchHit *hit;
|
2011-09-13 01:31:12 +00:00
|
|
|
|
|
|
|
|
|
tracker = GTK_SEARCH_ENGINE_TRACKER (user_data);
|
|
|
|
|
|
2015-06-19 04:41:10 +00:00
|
|
|
|
tracker->query_pending = FALSE;
|
2011-09-13 01:31:12 +00:00
|
|
|
|
|
2015-06-19 04:41:10 +00:00
|
|
|
|
reply = g_dbus_connection_call_finish (tracker->connection, res, &error);
|
2011-09-13 01:31:12 +00:00
|
|
|
|
if (error)
|
|
|
|
|
{
|
|
|
|
|
_gtk_search_engine_error (GTK_SEARCH_ENGINE (tracker), error->message);
|
|
|
|
|
g_error_free (error);
|
2015-03-03 12:36:07 +00:00
|
|
|
|
g_object_unref (tracker);
|
2011-09-13 01:31:12 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!reply)
|
|
|
|
|
{
|
2020-05-16 22:00:22 +00:00
|
|
|
|
_gtk_search_engine_finished (GTK_SEARCH_ENGINE (tracker), FALSE);
|
2015-03-03 12:36:07 +00:00
|
|
|
|
g_object_unref (tracker);
|
2011-09-13 01:31:12 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = g_variant_get_child_value (reply, 0);
|
|
|
|
|
g_variant_iter_init (&iter, r);
|
|
|
|
|
n = g_variant_iter_n_children (&iter);
|
2015-06-18 18:31:02 +00:00
|
|
|
|
hit = g_new (GtkSearchHit, n);
|
|
|
|
|
hits = NULL;
|
2011-09-13 01:31:12 +00:00
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
|
{
|
|
|
|
|
GVariant *v;
|
|
|
|
|
const gchar **strv;
|
|
|
|
|
|
|
|
|
|
v = g_variant_iter_next_value (&iter);
|
|
|
|
|
strv = g_variant_get_strv (v, NULL);
|
2015-07-24 04:40:59 +00:00
|
|
|
|
hit[i].file = g_file_new_for_uri (strv[0]);
|
2015-06-18 18:31:02 +00:00
|
|
|
|
hit[i].info = NULL;
|
2011-09-13 01:31:12 +00:00
|
|
|
|
g_free (strv);
|
2015-06-18 18:31:02 +00:00
|
|
|
|
hits = g_list_prepend (hits, &hit[i]);
|
2011-09-13 01:31:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_gtk_search_engine_hits_added (GTK_SEARCH_ENGINE (tracker), hits);
|
2020-05-16 22:00:22 +00:00
|
|
|
|
_gtk_search_engine_finished (GTK_SEARCH_ENGINE (tracker), i > 0);
|
2018-06-20 08:54:16 +00:00
|
|
|
|
|
2011-09-13 01:31:12 +00:00
|
|
|
|
g_list_free (hits);
|
2018-06-20 08:54:16 +00:00
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
|
g_object_unref (hit[i].file);
|
2015-06-18 18:31:02 +00:00
|
|
|
|
g_free (hit);
|
2018-06-20 08:54:16 +00:00
|
|
|
|
|
2011-09-13 01:31:12 +00:00
|
|
|
|
g_variant_unref (reply);
|
|
|
|
|
g_variant_unref (r);
|
|
|
|
|
|
2015-03-03 12:36:07 +00:00
|
|
|
|
g_object_unref (tracker);
|
2011-09-13 01:31:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_search_engine_tracker_start (GtkSearchEngine *engine)
|
|
|
|
|
{
|
|
|
|
|
GtkSearchEngineTracker *tracker;
|
2015-07-22 00:39:29 +00:00
|
|
|
|
const gchar *search_text;
|
2015-07-24 19:29:51 +00:00
|
|
|
|
GFile *location;
|
2010-03-19 13:40:11 +00:00
|
|
|
|
GString *sparql;
|
2015-06-19 02:44:11 +00:00
|
|
|
|
gboolean recursive;
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
|
|
|
|
tracker = GTK_SEARCH_ENGINE_TRACKER (engine);
|
|
|
|
|
|
2015-06-19 04:41:10 +00:00
|
|
|
|
if (tracker->query_pending)
|
2011-09-13 01:31:12 +00:00
|
|
|
|
{
|
|
|
|
|
g_debug ("Attempt to start a new search while one is pending, doing nothing");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
2015-06-19 04:41:10 +00:00
|
|
|
|
if (tracker->query == NULL)
|
2011-09-13 01:31:12 +00:00
|
|
|
|
{
|
|
|
|
|
g_debug ("Attempt to start a new search with no GtkQuery, doing nothing");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2010-04-09 13:21:45 +00:00
|
|
|
|
|
2015-06-19 04:41:10 +00:00
|
|
|
|
search_text = gtk_query_get_text (tracker->query);
|
2015-07-24 19:29:51 +00:00
|
|
|
|
location = gtk_query_get_location (tracker->query);
|
2015-06-19 02:44:11 +00:00
|
|
|
|
recursive = _gtk_search_engine_get_recursive (engine);
|
2015-02-28 21:59:36 +00:00
|
|
|
|
|
2010-12-07 12:15:42 +00:00
|
|
|
|
sparql = g_string_new ("SELECT nie:url(?urn) "
|
|
|
|
|
"WHERE {"
|
|
|
|
|
" ?urn a nfo:FileDataObject ;"
|
2015-11-23 17:18:46 +00:00
|
|
|
|
" tracker:available true ; "
|
2015-11-20 18:04:39 +00:00
|
|
|
|
" nfo:belongsToContainer ?parent; ");
|
2015-02-28 21:59:36 +00:00
|
|
|
|
|
|
|
|
|
#ifdef FTS_MATCHING
|
|
|
|
|
/* Using FTS: */
|
|
|
|
|
g_string_append (sparql, "fts:match ");
|
2016-05-04 12:23:17 +00:00
|
|
|
|
sparql_append_string_literal (sparql, search_text, TRUE, FALSE, TRUE);
|
2015-02-28 21:59:36 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2015-11-20 18:02:17 +00:00
|
|
|
|
g_string_append (sparql, ". FILTER (BOUND(nie:url(?urn)) && ");
|
2015-06-19 02:44:11 +00:00
|
|
|
|
|
|
|
|
|
g_string_append (sparql, "fn:contains(fn:lower-case(nfo:fileName(?urn)),");
|
2015-02-28 21:59:36 +00:00
|
|
|
|
sparql_append_string_literal_lower_case (sparql, search_text);
|
2015-06-19 02:44:11 +00:00
|
|
|
|
g_string_append (sparql, ")");
|
2010-12-07 12:15:42 +00:00
|
|
|
|
|
2015-07-24 19:29:51 +00:00
|
|
|
|
if (location)
|
2011-09-13 01:31:12 +00:00
|
|
|
|
{
|
2015-07-24 19:29:51 +00:00
|
|
|
|
gchar *location_uri = g_file_get_uri (location);
|
2015-06-19 02:44:11 +00:00
|
|
|
|
g_string_append (sparql, " && ");
|
|
|
|
|
if (recursive)
|
2015-11-20 18:04:39 +00:00
|
|
|
|
{
|
|
|
|
|
g_string_append (sparql, "fn:starts-with(nie:url(?urn),");
|
2016-05-04 12:23:17 +00:00
|
|
|
|
sparql_append_string_literal (sparql, location_uri, FALSE, TRUE, FALSE);
|
2015-11-20 18:04:39 +00:00
|
|
|
|
g_string_append (sparql, ")");
|
|
|
|
|
}
|
2015-06-19 02:44:11 +00:00
|
|
|
|
else
|
2015-11-20 18:04:39 +00:00
|
|
|
|
{
|
|
|
|
|
g_string_append (sparql, "nie:url(?parent) = ");
|
2016-05-04 12:23:17 +00:00
|
|
|
|
sparql_append_string_literal (sparql, location_uri, FALSE, FALSE, FALSE);
|
2015-11-20 18:04:39 +00:00
|
|
|
|
}
|
2015-07-24 19:29:51 +00:00
|
|
|
|
g_free (location_uri);
|
2011-09-13 01:31:12 +00:00
|
|
|
|
}
|
2010-12-07 12:15:42 +00:00
|
|
|
|
|
2015-06-19 02:44:11 +00:00
|
|
|
|
g_string_append (sparql, ")");
|
2010-12-07 12:15:42 +00:00
|
|
|
|
|
2015-02-28 21:59:36 +00:00
|
|
|
|
#ifdef FTS_MATCHING
|
|
|
|
|
g_string_append (sparql, " } ORDER BY DESC(fts:rank(?urn)) DESC(nie:url(?urn))");
|
|
|
|
|
#else /* FTS_MATCHING */
|
|
|
|
|
g_string_append (sparql, "} ORDER BY DESC(nie:url(?urn)) DESC(nfo:fileName(?urn))");
|
2010-12-07 12:15:42 +00:00
|
|
|
|
#endif /* FTS_MATCHING */
|
|
|
|
|
|
2015-06-19 04:41:10 +00:00
|
|
|
|
tracker->query_pending = TRUE;
|
2011-09-13 01:31:12 +00:00
|
|
|
|
|
2015-02-28 21:42:39 +00:00
|
|
|
|
g_debug ("SearchEngineTracker: query: %s", sparql->str);
|
|
|
|
|
|
2015-03-03 12:36:07 +00:00
|
|
|
|
get_query_results (tracker, sparql->str, query_callback, g_object_ref (tracker));
|
2011-09-13 01:31:12 +00:00
|
|
|
|
|
|
|
|
|
g_string_free (sparql, TRUE);
|
2007-05-02 22:51:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
gtk_search_engine_tracker_stop (GtkSearchEngine *engine)
|
|
|
|
|
{
|
|
|
|
|
GtkSearchEngineTracker *tracker;
|
2010-04-09 13:21:45 +00:00
|
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
|
tracker = GTK_SEARCH_ENGINE_TRACKER (engine);
|
2010-04-09 13:21:45 +00:00
|
|
|
|
|
2015-06-19 04:41:10 +00:00
|
|
|
|
if (tracker->query && tracker->query_pending)
|
2007-05-02 22:51:43 +00:00
|
|
|
|
{
|
2015-06-19 04:41:10 +00:00
|
|
|
|
g_cancellable_cancel (tracker->cancellable);
|
|
|
|
|
tracker->query_pending = FALSE;
|
2007-05-02 22:51:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2010-04-09 13:21:45 +00:00
|
|
|
|
gtk_search_engine_tracker_set_query (GtkSearchEngine *engine,
|
2010-12-07 12:15:42 +00:00
|
|
|
|
GtkQuery *query)
|
2007-05-02 22:51:43 +00:00
|
|
|
|
{
|
|
|
|
|
GtkSearchEngineTracker *tracker;
|
2010-04-09 13:21:45 +00:00
|
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
|
tracker = GTK_SEARCH_ENGINE_TRACKER (engine);
|
2010-04-09 13:21:45 +00:00
|
|
|
|
|
|
|
|
|
if (query)
|
2007-05-02 22:51:43 +00:00
|
|
|
|
g_object_ref (query);
|
|
|
|
|
|
2015-06-19 04:41:10 +00:00
|
|
|
|
if (tracker->query)
|
|
|
|
|
g_object_unref (tracker->query);
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
2015-06-19 04:41:10 +00:00
|
|
|
|
tracker->query = query;
|
2007-05-02 22:51:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_gtk_search_engine_tracker_class_init (GtkSearchEngineTrackerClass *class)
|
|
|
|
|
{
|
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
|
GtkSearchEngineClass *engine_class;
|
2010-04-09 13:21:45 +00:00
|
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
|
gobject_class = G_OBJECT_CLASS (class);
|
|
|
|
|
gobject_class->finalize = finalize;
|
2010-04-09 13:21:45 +00:00
|
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-19 04:12:46 +00:00
|
|
|
|
static void get_indexed_locations (GtkSearchEngineTracker *engine);
|
|
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
|
static void
|
|
|
|
|
_gtk_search_engine_tracker_init (GtkSearchEngineTracker *engine)
|
|
|
|
|
{
|
2015-06-19 04:41:10 +00:00
|
|
|
|
engine->cancellable = g_cancellable_new ();
|
|
|
|
|
engine->query_pending = FALSE;
|
|
|
|
|
engine->indexed_locations = g_ptr_array_new_with_free_func (g_object_unref);
|
2015-06-19 04:12:46 +00:00
|
|
|
|
|
|
|
|
|
get_indexed_locations (engine);
|
|
|
|
|
}
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
|
|
|
|
GtkSearchEngine *
|
|
|
|
|
_gtk_search_engine_tracker_new (void)
|
|
|
|
|
{
|
|
|
|
|
GtkSearchEngineTracker *engine;
|
2011-09-13 01:31:12 +00:00
|
|
|
|
GDBusConnection *connection;
|
2010-12-07 12:15:42 +00:00
|
|
|
|
|
2011-09-13 01:31:12 +00:00
|
|
|
|
g_debug ("--");
|
2010-12-07 12:15:42 +00:00
|
|
|
|
|
2011-09-13 01:31:12 +00:00
|
|
|
|
connection = get_connection ();
|
|
|
|
|
if (!connection)
|
|
|
|
|
return NULL;
|
2010-12-07 12:15:42 +00:00
|
|
|
|
|
2011-09-13 01:31:12 +00:00
|
|
|
|
g_debug ("Creating GtkSearchEngineTracker...");
|
2008-05-25 14:19:16 +00:00
|
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
|
engine = g_object_new (GTK_TYPE_SEARCH_ENGINE_TRACKER, NULL);
|
|
|
|
|
|
2015-06-19 04:41:10 +00:00
|
|
|
|
engine->connection = connection;
|
2010-04-09 13:21:45 +00:00
|
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
|
return GTK_SEARCH_ENGINE (engine);
|
|
|
|
|
}
|
2015-06-19 04:12:46 +00:00
|
|
|
|
|
|
|
|
|
#define TRACKER_SCHEMA "org.freedesktop.Tracker.Miner.Files"
|
|
|
|
|
#define TRACKER_KEY_RECURSIVE_DIRECTORIES "index-recursive-directories"
|
|
|
|
|
|
|
|
|
|
static const gchar *
|
|
|
|
|
get_user_special_dir_if_not_home (GUserDirectory idx)
|
|
|
|
|
{
|
|
|
|
|
const gchar *path;
|
|
|
|
|
path = g_get_user_special_dir (idx);
|
|
|
|
|
if (g_strcmp0 (path, g_get_home_dir ()) == 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const gchar *
|
|
|
|
|
path_from_tracker_dir (const gchar *value)
|
|
|
|
|
{
|
|
|
|
|
const gchar *path;
|
|
|
|
|
|
|
|
|
|
if (g_strcmp0 (value, "&DESKTOP") == 0)
|
|
|
|
|
path = get_user_special_dir_if_not_home (G_USER_DIRECTORY_DESKTOP);
|
|
|
|
|
else if (g_strcmp0 (value, "&DOCUMENTS") == 0)
|
|
|
|
|
path = get_user_special_dir_if_not_home (G_USER_DIRECTORY_DOCUMENTS);
|
|
|
|
|
else if (g_strcmp0 (value, "&DOWNLOAD") == 0)
|
|
|
|
|
path = get_user_special_dir_if_not_home (G_USER_DIRECTORY_DOWNLOAD);
|
|
|
|
|
else if (g_strcmp0 (value, "&MUSIC") == 0)
|
|
|
|
|
path = get_user_special_dir_if_not_home (G_USER_DIRECTORY_MUSIC);
|
|
|
|
|
else if (g_strcmp0 (value, "&PICTURES") == 0)
|
|
|
|
|
path = get_user_special_dir_if_not_home (G_USER_DIRECTORY_PICTURES);
|
|
|
|
|
else if (g_strcmp0 (value, "&PUBLIC_SHARE") == 0)
|
|
|
|
|
path = get_user_special_dir_if_not_home (G_USER_DIRECTORY_PUBLIC_SHARE);
|
|
|
|
|
else if (g_strcmp0 (value, "&TEMPLATES") == 0)
|
|
|
|
|
path = get_user_special_dir_if_not_home (G_USER_DIRECTORY_TEMPLATES);
|
|
|
|
|
else if (g_strcmp0 (value, "&VIDEOS") == 0)
|
|
|
|
|
path = get_user_special_dir_if_not_home (G_USER_DIRECTORY_VIDEOS);
|
|
|
|
|
else if (g_strcmp0 (value, "$HOME") == 0)
|
|
|
|
|
path = g_get_home_dir ();
|
|
|
|
|
else
|
|
|
|
|
path = value;
|
|
|
|
|
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
get_indexed_locations (GtkSearchEngineTracker *engine)
|
|
|
|
|
{
|
|
|
|
|
GSettingsSchemaSource *source;
|
|
|
|
|
GSettingsSchema *schema;
|
|
|
|
|
GSettings *settings;
|
|
|
|
|
gchar **locations;
|
|
|
|
|
gint i;
|
|
|
|
|
GFile *location;
|
|
|
|
|
const gchar *path;
|
|
|
|
|
|
|
|
|
|
source = g_settings_schema_source_get_default ();
|
|
|
|
|
schema = g_settings_schema_source_lookup (source, TRACKER_SCHEMA, FALSE);
|
|
|
|
|
if (!schema)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
settings = g_settings_new_full (schema, NULL, NULL);
|
|
|
|
|
g_settings_schema_unref (schema);
|
|
|
|
|
|
|
|
|
|
locations = g_settings_get_strv (settings, TRACKER_KEY_RECURSIVE_DIRECTORIES);
|
|
|
|
|
|
|
|
|
|
for (i = 0; locations[i] != NULL; i++)
|
|
|
|
|
{
|
|
|
|
|
path = path_from_tracker_dir (locations[i]);
|
2015-08-01 03:49:54 +00:00
|
|
|
|
if (path == NULL)
|
|
|
|
|
continue;
|
|
|
|
|
|
2015-06-19 04:12:46 +00:00
|
|
|
|
location = g_file_new_for_path (path);
|
2015-06-19 04:41:10 +00:00
|
|
|
|
g_ptr_array_add (engine->indexed_locations, location);
|
2015-06-19 04:12:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_strfreev (locations);
|
|
|
|
|
g_object_unref (settings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
_gtk_search_engine_tracker_is_indexed (GFile *location,
|
|
|
|
|
gpointer data)
|
|
|
|
|
{
|
|
|
|
|
GtkSearchEngineTracker *engine = data;
|
|
|
|
|
gint i;
|
|
|
|
|
GFile *place;
|
|
|
|
|
|
2015-06-19 04:41:10 +00:00
|
|
|
|
for (i = 0; i < engine->indexed_locations->len; i++)
|
2015-06-19 04:12:46 +00:00
|
|
|
|
{
|
2015-06-19 04:41:10 +00:00
|
|
|
|
place = g_ptr_array_index (engine->indexed_locations, i);
|
2015-06-19 04:12:46 +00:00
|
|
|
|
if (g_file_equal (location, place) || g_file_has_prefix (location, place))
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|