2007-05-02 22:51:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2005 Red Hat, Inc
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*
|
|
|
|
* Author: Alexander Larsson <alexl@redhat.com>
|
|
|
|
*
|
|
|
|
* Based on nautilus-search-engine-simple.c
|
|
|
|
*/
|
|
|
|
|
2008-06-22 14:28:52 +00:00
|
|
|
#include "config.h"
|
2007-05-14 15:35:37 +00:00
|
|
|
|
2007-06-10 17:25:16 +00:00
|
|
|
/* these must be defined even when HAVE_GNU_FTW is not defined
|
|
|
|
* because (really) old versions of GNU libc have ftw.h but do
|
|
|
|
* export ftw() and friends only if _XOPEN_SOURCE and _GNU_SOURCE
|
|
|
|
* are defined. see bug #444097.
|
|
|
|
*/
|
2012-05-26 03:13:42 +00:00
|
|
|
#define _XOPEN_SOURCE 600
|
|
|
|
#define _GNU_SOURCE
|
2007-05-14 15:35:37 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_FTW_H
|
|
|
|
#include <ftw.h>
|
|
|
|
#endif
|
2007-05-02 22:51:43 +00:00
|
|
|
|
2010-10-19 17:14:46 +00:00
|
|
|
#include <gdk/gdk.h>
|
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
#include "gtksearchenginesimple.h"
|
2007-07-09 18:04:59 +00:00
|
|
|
#include "gtkprivate.h"
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
2007-05-13 21:21:39 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
#define BATCH_SIZE 500
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GtkSearchEngineSimple *engine;
|
|
|
|
|
|
|
|
gchar *path;
|
|
|
|
gchar **words;
|
|
|
|
GList *found_list;
|
|
|
|
|
|
|
|
gint n_processed_files;
|
|
|
|
GList *uri_hits;
|
|
|
|
|
|
|
|
/* accessed on both threads: */
|
|
|
|
volatile gboolean cancelled;
|
|
|
|
} SearchThreadData;
|
|
|
|
|
|
|
|
|
|
|
|
struct _GtkSearchEngineSimplePrivate
|
|
|
|
{
|
|
|
|
GtkQuery *query;
|
|
|
|
|
|
|
|
SearchThreadData *active_search;
|
|
|
|
|
|
|
|
gboolean query_finished;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-06-27 19:02:52 +00:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GtkSearchEngineSimple, _gtk_search_engine_simple, GTK_TYPE_SEARCH_ENGINE)
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
|
|
static void
|
2007-09-25 20:59:15 +00:00
|
|
|
gtk_search_engine_simple_dispose (GObject *object)
|
2007-05-02 22:51:43 +00:00
|
|
|
{
|
|
|
|
GtkSearchEngineSimple *simple;
|
2007-09-25 20:59:15 +00:00
|
|
|
GtkSearchEngineSimplePrivate *priv;
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
|
|
simple = GTK_SEARCH_ENGINE_SIMPLE (object);
|
2007-09-25 20:59:15 +00:00
|
|
|
priv = simple->priv;
|
2007-05-02 22:51:43 +00:00
|
|
|
|
2007-09-25 20:59:15 +00:00
|
|
|
if (priv->query)
|
|
|
|
{
|
|
|
|
g_object_unref (priv->query);
|
|
|
|
priv->query = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->active_search)
|
2007-05-02 22:51:43 +00:00
|
|
|
{
|
2007-09-25 20:59:15 +00:00
|
|
|
priv->active_search->cancelled = TRUE;
|
|
|
|
priv->active_search = NULL;
|
2007-05-02 22:51:43 +00:00
|
|
|
}
|
|
|
|
|
2007-09-25 20:59:15 +00:00
|
|
|
G_OBJECT_CLASS (_gtk_search_engine_simple_parent_class)->dispose (object);
|
2007-05-02 22:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SearchThreadData *
|
|
|
|
search_thread_data_new (GtkSearchEngineSimple *engine,
|
|
|
|
GtkQuery *query)
|
|
|
|
{
|
|
|
|
SearchThreadData *data;
|
|
|
|
char *text, *lower, *uri;
|
|
|
|
|
|
|
|
data = g_new0 (SearchThreadData, 1);
|
|
|
|
|
2007-07-09 18:02:38 +00:00
|
|
|
data->engine = g_object_ref (engine);
|
2007-05-02 22:51:43 +00:00
|
|
|
uri = _gtk_query_get_location (query);
|
|
|
|
if (uri != NULL)
|
|
|
|
{
|
|
|
|
data->path = g_filename_from_uri (uri, NULL, NULL);
|
|
|
|
g_free (uri);
|
2007-07-09 18:04:59 +00:00
|
|
|
}
|
2007-05-02 22:51:43 +00:00
|
|
|
if (data->path == NULL)
|
|
|
|
data->path = g_strdup (g_get_home_dir ());
|
|
|
|
|
|
|
|
text = _gtk_query_get_text (query);
|
|
|
|
lower = g_ascii_strdown (text, -1);
|
|
|
|
data->words = g_strsplit (lower, " ", -1);
|
|
|
|
g_free (text);
|
|
|
|
g_free (lower);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
search_thread_data_free (SearchThreadData *data)
|
|
|
|
{
|
2007-07-09 18:02:38 +00:00
|
|
|
g_object_unref (data->engine);
|
2007-05-02 22:51:43 +00:00
|
|
|
g_free (data->path);
|
|
|
|
g_strfreev (data->words);
|
|
|
|
g_free (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
search_thread_done_idle (gpointer user_data)
|
|
|
|
{
|
|
|
|
SearchThreadData *data;
|
|
|
|
|
|
|
|
data = user_data;
|
|
|
|
|
2007-09-25 20:59:15 +00:00
|
|
|
if (!data->cancelled)
|
|
|
|
_gtk_search_engine_finished (GTK_SEARCH_ENGINE (data->engine));
|
|
|
|
|
|
|
|
data->engine->priv->active_search = NULL;
|
2007-05-02 22:51:43 +00:00
|
|
|
search_thread_data_free (data);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GList *uris;
|
|
|
|
SearchThreadData *thread_data;
|
|
|
|
} SearchHits;
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
search_thread_add_hits_idle (gpointer user_data)
|
|
|
|
{
|
|
|
|
SearchHits *hits;
|
|
|
|
|
|
|
|
hits = user_data;
|
|
|
|
|
|
|
|
if (!hits->thread_data->cancelled)
|
|
|
|
{
|
|
|
|
_gtk_search_engine_hits_added (GTK_SEARCH_ENGINE (hits->thread_data->engine),
|
|
|
|
hits->uris);
|
|
|
|
}
|
|
|
|
|
2012-01-02 03:20:59 +00:00
|
|
|
g_list_free_full (hits->uris, g_free);
|
2007-05-02 22:51:43 +00:00
|
|
|
g_free (hits);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
send_batch (SearchThreadData *data)
|
|
|
|
{
|
|
|
|
SearchHits *hits;
|
|
|
|
|
|
|
|
data->n_processed_files = 0;
|
|
|
|
|
|
|
|
if (data->uri_hits)
|
|
|
|
{
|
2014-03-22 11:44:01 +00:00
|
|
|
guint id;
|
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
hits = g_new (SearchHits, 1);
|
|
|
|
hits->uris = data->uri_hits;
|
|
|
|
hits->thread_data = data;
|
2007-09-25 20:59:15 +00:00
|
|
|
|
2014-03-22 11:44:01 +00:00
|
|
|
id = gdk_threads_add_idle (search_thread_add_hits_idle, hits);
|
|
|
|
g_source_set_name_by_id (id, "[gtk+] search_thread_add_hits_idle");
|
2007-05-02 22:51:43 +00:00
|
|
|
}
|
2010-10-19 17:14:46 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
data->uri_hits = NULL;
|
|
|
|
}
|
|
|
|
|
2011-11-02 01:06:09 +00:00
|
|
|
static GPrivate search_thread_data;
|
2007-05-02 22:51:43 +00:00
|
|
|
|
2007-05-13 21:21:39 +00:00
|
|
|
#ifdef HAVE_FTW_H
|
2007-05-02 22:51:43 +00:00
|
|
|
static int
|
|
|
|
search_visit_func (const char *fpath,
|
|
|
|
const struct stat *sb,
|
|
|
|
int typeflag,
|
|
|
|
struct FTW *ftwbuf)
|
|
|
|
{
|
|
|
|
SearchThreadData *data;
|
|
|
|
gint i;
|
2011-11-02 01:06:09 +00:00
|
|
|
const gchar *name;
|
2007-06-22 14:32:22 +00:00
|
|
|
gchar *lower_name;
|
2007-05-02 22:51:43 +00:00
|
|
|
gchar *uri;
|
|
|
|
gboolean hit;
|
|
|
|
gboolean is_hidden;
|
2011-11-02 01:06:09 +00:00
|
|
|
|
|
|
|
data = (SearchThreadData*)g_private_get (&search_thread_data);
|
2007-05-14 15:35:37 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
if (data->cancelled)
|
2007-05-14 15:35:37 +00:00
|
|
|
#ifdef HAVE_GNU_FTW
|
2007-05-02 22:51:43 +00:00
|
|
|
return FTW_STOP;
|
2007-05-14 15:35:37 +00:00
|
|
|
#else
|
|
|
|
return 1;
|
|
|
|
#endif /* HAVE_GNU_FTW */
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
|
|
name = strrchr (fpath, '/');
|
|
|
|
if (name)
|
|
|
|
name++;
|
|
|
|
else
|
|
|
|
name = fpath;
|
|
|
|
|
|
|
|
is_hidden = *name == '.';
|
|
|
|
|
|
|
|
hit = FALSE;
|
|
|
|
|
|
|
|
if (!is_hidden)
|
|
|
|
{
|
|
|
|
lower_name = g_ascii_strdown (name, -1);
|
|
|
|
|
|
|
|
hit = TRUE;
|
|
|
|
for (i = 0; data->words[i] != NULL; i++)
|
|
|
|
{
|
|
|
|
if (strstr (lower_name, data->words[i]) == NULL)
|
|
|
|
{
|
|
|
|
hit = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free (lower_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hit)
|
|
|
|
{
|
2007-05-10 15:04:51 +00:00
|
|
|
uri = g_filename_to_uri (fpath, NULL, NULL);
|
2007-05-02 22:51:43 +00:00
|
|
|
data->uri_hits = g_list_prepend (data->uri_hits, uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
data->n_processed_files++;
|
|
|
|
|
|
|
|
if (data->n_processed_files > BATCH_SIZE)
|
|
|
|
send_batch (data);
|
2007-05-13 21:21:39 +00:00
|
|
|
|
2007-05-14 15:35:37 +00:00
|
|
|
#ifdef HAVE_GNU_FTW
|
2007-05-02 22:51:43 +00:00
|
|
|
if (is_hidden)
|
|
|
|
return FTW_SKIP_SUBTREE;
|
|
|
|
else
|
|
|
|
return FTW_CONTINUE;
|
2007-05-14 15:35:37 +00:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif /* HAVE_GNU_FTW */
|
2007-05-02 22:51:43 +00:00
|
|
|
}
|
2007-05-13 21:21:39 +00:00
|
|
|
#endif /* HAVE_FTW_H */
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
|
|
static gpointer
|
|
|
|
search_thread_func (gpointer user_data)
|
|
|
|
{
|
2014-03-22 11:44:01 +00:00
|
|
|
guint id;
|
|
|
|
|
2007-05-13 21:21:39 +00:00
|
|
|
#ifdef HAVE_FTW_H
|
2007-05-02 22:51:43 +00:00
|
|
|
SearchThreadData *data;
|
|
|
|
|
|
|
|
data = user_data;
|
|
|
|
|
2011-11-02 01:06:09 +00:00
|
|
|
g_private_set (&search_thread_data, data);
|
2007-05-02 22:51:43 +00:00
|
|
|
|
2007-05-14 15:35:37 +00:00
|
|
|
nftw (data->path, search_visit_func, 20,
|
|
|
|
#ifdef HAVE_GNU_FTW
|
|
|
|
FTW_ACTIONRETVAL |
|
|
|
|
#endif
|
|
|
|
FTW_PHYS);
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
|
|
send_batch (data);
|
|
|
|
|
2014-03-22 11:44:01 +00:00
|
|
|
id = gdk_threads_add_idle (search_thread_done_idle, data);
|
|
|
|
g_source_set_name_by_id (id, "[gtk+] search_thread_done_idle");
|
2007-05-14 15:35:37 +00:00
|
|
|
#endif /* HAVE_FTW_H */
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_search_engine_simple_start (GtkSearchEngine *engine)
|
|
|
|
{
|
|
|
|
GtkSearchEngineSimple *simple;
|
|
|
|
SearchThreadData *data;
|
|
|
|
|
|
|
|
simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
|
|
|
|
|
|
|
|
if (simple->priv->active_search != NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (simple->priv->query == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
data = search_thread_data_new (simple, simple->priv->query);
|
|
|
|
|
2011-11-02 01:06:09 +00:00
|
|
|
g_thread_unref (g_thread_new ("file-search", search_thread_func, data));
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
|
|
simple->priv->active_search = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_search_engine_simple_stop (GtkSearchEngine *engine)
|
|
|
|
{
|
|
|
|
GtkSearchEngineSimple *simple;
|
|
|
|
|
|
|
|
simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
|
|
|
|
|
|
|
|
if (simple->priv->active_search != NULL)
|
|
|
|
{
|
|
|
|
simple->priv->active_search->cancelled = TRUE;
|
|
|
|
simple->priv->active_search = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_search_engine_simple_is_indexed (GtkSearchEngine *engine)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_search_engine_simple_set_query (GtkSearchEngine *engine,
|
|
|
|
GtkQuery *query)
|
|
|
|
{
|
|
|
|
GtkSearchEngineSimple *simple;
|
|
|
|
|
|
|
|
simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
|
|
|
|
|
|
|
|
if (query)
|
|
|
|
g_object_ref (query);
|
|
|
|
|
|
|
|
if (simple->priv->query)
|
|
|
|
g_object_unref (simple->priv->query);
|
|
|
|
|
|
|
|
simple->priv->query = query;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_gtk_search_engine_simple_class_init (GtkSearchEngineSimpleClass *class)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GtkSearchEngineClass *engine_class;
|
|
|
|
|
|
|
|
gobject_class = G_OBJECT_CLASS (class);
|
2007-09-25 20:59:15 +00:00
|
|
|
gobject_class->dispose = gtk_search_engine_simple_dispose;
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
|
|
engine_class = GTK_SEARCH_ENGINE_CLASS (class);
|
|
|
|
engine_class->set_query = gtk_search_engine_simple_set_query;
|
|
|
|
engine_class->start = gtk_search_engine_simple_start;
|
|
|
|
engine_class->stop = gtk_search_engine_simple_stop;
|
|
|
|
engine_class->is_indexed = gtk_search_engine_simple_is_indexed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_gtk_search_engine_simple_init (GtkSearchEngineSimple *engine)
|
|
|
|
{
|
2013-06-27 19:02:52 +00:00
|
|
|
engine->priv = _gtk_search_engine_simple_get_instance_private (engine);
|
2007-05-02 22:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkSearchEngine *
|
|
|
|
_gtk_search_engine_simple_new (void)
|
|
|
|
{
|
2007-05-13 21:21:39 +00:00
|
|
|
#ifdef HAVE_FTW_H
|
|
|
|
return g_object_new (GTK_TYPE_SEARCH_ENGINE_SIMPLE, NULL);
|
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
2007-05-02 22:51:43 +00:00
|
|
|
}
|