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
|
|
|
|
2015-04-30 17:46:27 +00:00
|
|
|
#include <gio/gio.h>
|
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
|
|
|
|
|
2015-02-28 21:13:18 +00:00
|
|
|
typedef struct
|
2007-05-02 22:51:43 +00:00
|
|
|
{
|
|
|
|
GtkSearchEngineSimple *engine;
|
2015-05-16 04:37:07 +00:00
|
|
|
GCancellable *cancellable;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2015-05-16 04:37:07 +00:00
|
|
|
GQueue *directories;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
gint n_processed_files;
|
2015-05-16 04:37:07 +00:00
|
|
|
GList *hits;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2015-05-16 04:37:07 +00:00
|
|
|
GtkQuery *query;
|
2007-05-02 22:51:43 +00:00
|
|
|
} SearchThreadData;
|
|
|
|
|
|
|
|
|
2015-02-28 21:13:18 +00:00
|
|
|
struct _GtkSearchEngineSimplePrivate
|
2007-05-02 22:51:43 +00:00
|
|
|
{
|
|
|
|
GtkQuery *query;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
SearchThreadData *active_search;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
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;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
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;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
|
|
|
if (priv->query)
|
2007-09-25 20:59:15 +00:00
|
|
|
{
|
|
|
|
g_object_unref (priv->query);
|
|
|
|
priv->query = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->active_search)
|
2007-05-02 22:51:43 +00:00
|
|
|
{
|
2015-05-16 04:37:07 +00:00
|
|
|
g_cancellable_cancel (priv->active_search->cancellable);
|
2007-09-25 20:59:15 +00:00
|
|
|
priv->active_search = NULL;
|
2007-05-02 22:51:43 +00:00
|
|
|
}
|
2015-02-28 21:13:18 +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;
|
2015-05-16 04:37:07 +00:00
|
|
|
const gchar *uri;
|
|
|
|
GFile *location;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
data = g_new0 (SearchThreadData, 1);
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-07-09 18:02:38 +00:00
|
|
|
data->engine = g_object_ref (engine);
|
2015-05-16 04:37:07 +00:00
|
|
|
data->directories = g_queue_new ();
|
|
|
|
data->query = g_object_ref (query);
|
2015-05-16 04:45:25 +00:00
|
|
|
uri = gtk_query_get_location (query);
|
2015-02-28 21:13:18 +00:00
|
|
|
if (uri != NULL)
|
2015-05-16 04:37:07 +00:00
|
|
|
location = g_file_new_for_uri (uri);
|
|
|
|
else
|
|
|
|
location = g_file_new_for_path (g_get_home_dir ());
|
|
|
|
g_queue_push_tail (data->directories, location);
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2015-05-16 04:37:07 +00:00
|
|
|
data->cancellable = g_cancellable_new ();
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2015-02-28 21:13:18 +00:00
|
|
|
static void
|
2007-05-02 22:51:43 +00:00
|
|
|
search_thread_data_free (SearchThreadData *data)
|
|
|
|
{
|
2015-05-16 04:37:07 +00:00
|
|
|
g_queue_foreach (data->directories, (GFunc)g_object_unref, NULL);
|
|
|
|
g_queue_free (data->directories);
|
|
|
|
g_object_unref (data->cancellable);
|
|
|
|
g_object_unref (data->query);
|
2007-07-09 18:02:38 +00:00
|
|
|
g_object_unref (data->engine);
|
2015-05-16 04:37:07 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
g_free (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
search_thread_done_idle (gpointer user_data)
|
|
|
|
{
|
|
|
|
SearchThreadData *data;
|
|
|
|
|
|
|
|
data = user_data;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2015-05-16 04:37:07 +00:00
|
|
|
if (!g_cancellable_is_cancelled (data->cancellable))
|
2007-09-25 20:59:15 +00:00
|
|
|
_gtk_search_engine_finished (GTK_SEARCH_ENGINE (data->engine));
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-09-25 20:59:15 +00:00
|
|
|
data->engine->priv->active_search = NULL;
|
2007-05-02 22:51:43 +00:00
|
|
|
search_thread_data_free (data);
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-02-28 21:13:18 +00:00
|
|
|
typedef struct
|
2007-05-02 22:51:43 +00:00
|
|
|
{
|
2015-06-18 18:31:02 +00:00
|
|
|
GList *hits;
|
2007-05-02 22:51:43 +00:00
|
|
|
SearchThreadData *thread_data;
|
2015-06-18 18:31:02 +00:00
|
|
|
} Batch;
|
2007-05-02 22:51:43 +00:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
search_thread_add_hits_idle (gpointer user_data)
|
|
|
|
{
|
2015-06-18 18:31:02 +00:00
|
|
|
Batch *batch = user_data;
|
2007-05-02 22:51:43 +00:00
|
|
|
|
2015-06-18 18:31:02 +00:00
|
|
|
if (!g_cancellable_is_cancelled (batch->thread_data->cancellable))
|
|
|
|
_gtk_search_engine_hits_added (GTK_SEARCH_ENGINE (batch->thread_data->engine), batch->hits);
|
2007-05-02 22:51:43 +00:00
|
|
|
|
2015-06-18 18:31:02 +00:00
|
|
|
g_list_free_full (batch->hits, (GDestroyNotify)_gtk_search_hit_free);
|
|
|
|
g_free (batch);
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
send_batch (SearchThreadData *data)
|
|
|
|
{
|
2015-06-18 18:31:02 +00:00
|
|
|
Batch *batch;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
data->n_processed_files = 0;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2015-05-16 04:37:07 +00:00
|
|
|
if (data->hits)
|
2007-05-02 22:51:43 +00:00
|
|
|
{
|
2014-03-22 11:44:01 +00:00
|
|
|
guint id;
|
|
|
|
|
2015-06-18 18:31:02 +00:00
|
|
|
batch = g_new (Batch, 1);
|
|
|
|
batch->hits = data->hits;
|
|
|
|
batch->thread_data = data;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2015-06-18 18:31:02 +00:00
|
|
|
id = gdk_threads_add_idle (search_thread_add_hits_idle, batch);
|
2014-03-22 11:44:01 +00:00
|
|
|
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
|
|
|
|
2015-05-16 04:37:07 +00:00
|
|
|
data->hits = NULL;
|
2007-05-02 22:51:43 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 17:46:27 +00:00
|
|
|
static void
|
2015-05-16 04:37:07 +00:00
|
|
|
visit_directory (GFile *dir, SearchThreadData *data)
|
2015-04-30 17:46:27 +00:00
|
|
|
{
|
2015-05-16 04:37:07 +00:00
|
|
|
GFileEnumerator *enumerator;
|
|
|
|
GFileInfo *info;
|
|
|
|
GFile *child;
|
|
|
|
const gchar *display_name;
|
|
|
|
|
|
|
|
enumerator = g_file_enumerate_children (dir,
|
|
|
|
G_FILE_ATTRIBUTE_STANDARD_NAME ","
|
|
|
|
G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
|
|
|
|
G_FILE_ATTRIBUTE_STANDARD_TYPE ","
|
|
|
|
G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN,
|
|
|
|
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
|
|
|
|
data->cancellable, NULL);
|
|
|
|
if (enumerator == NULL)
|
|
|
|
return;
|
2015-04-30 17:46:27 +00:00
|
|
|
|
2015-05-16 04:37:07 +00:00
|
|
|
while (g_file_enumerator_iterate (enumerator, &info, &child, data->cancellable, NULL))
|
2015-04-30 17:46:27 +00:00
|
|
|
{
|
2015-05-16 04:37:07 +00:00
|
|
|
if (info == NULL)
|
|
|
|
break;
|
2015-04-30 17:46:27 +00:00
|
|
|
|
2015-05-16 04:37:07 +00:00
|
|
|
display_name = g_file_info_get_display_name (info);
|
|
|
|
if (display_name == NULL)
|
|
|
|
continue;
|
2015-04-30 17:46:27 +00:00
|
|
|
|
|
|
|
if (g_file_info_get_is_hidden (info))
|
|
|
|
continue;
|
|
|
|
|
2015-05-16 04:37:07 +00:00
|
|
|
if (gtk_query_matches_string (data->query, display_name))
|
2015-06-18 18:31:02 +00:00
|
|
|
{
|
|
|
|
GtkSearchHit *hit;
|
|
|
|
|
|
|
|
hit = g_new (GtkSearchHit, 1);
|
|
|
|
hit->uri = g_file_get_uri (child);
|
|
|
|
hit->info = g_object_ref (info);
|
|
|
|
data->hits = g_list_prepend (data->hits, hit);
|
|
|
|
}
|
2015-04-30 17:46:27 +00:00
|
|
|
|
2015-05-16 04:37:07 +00:00
|
|
|
data->n_processed_files++;
|
|
|
|
if (data->n_processed_files > BATCH_SIZE)
|
|
|
|
send_batch (data);
|
2015-04-30 17:46:27 +00:00
|
|
|
|
2015-05-16 04:37:07 +00:00
|
|
|
if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
|
|
|
|
g_queue_push_tail (data->directories, g_object_ref (child));
|
2015-04-30 17:46:27 +00:00
|
|
|
}
|
2007-05-02 22:51:43 +00:00
|
|
|
|
2015-05-16 04:37:07 +00:00
|
|
|
g_object_unref (enumerator);
|
2007-05-02 22:51:43 +00:00
|
|
|
}
|
|
|
|
|
2015-02-28 21:13:18 +00:00
|
|
|
static gpointer
|
2007-05-02 22:51:43 +00:00
|
|
|
search_thread_func (gpointer user_data)
|
|
|
|
{
|
|
|
|
SearchThreadData *data;
|
2015-05-16 04:37:07 +00:00
|
|
|
GFile *dir;
|
|
|
|
guint id;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
data = user_data;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2015-05-16 04:37:07 +00:00
|
|
|
while (!g_cancellable_is_cancelled (data->cancellable) &&
|
|
|
|
(dir = g_queue_pop_head (data->directories)) != NULL)
|
|
|
|
{
|
|
|
|
visit_directory (dir, data);
|
|
|
|
g_object_unref (dir);
|
|
|
|
}
|
2007-05-02 22:51:43 +00:00
|
|
|
|
2015-05-16 04:37:07 +00:00
|
|
|
if (!g_cancellable_is_cancelled (data->cancellable))
|
|
|
|
send_batch (data);
|
2015-02-28 21:13:18 +00:00
|
|
|
|
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");
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_search_engine_simple_start (GtkSearchEngine *engine)
|
|
|
|
{
|
|
|
|
GtkSearchEngineSimple *simple;
|
|
|
|
SearchThreadData *data;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
if (simple->priv->active_search != NULL)
|
|
|
|
return;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
if (simple->priv->query == NULL)
|
|
|
|
return;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
data = search_thread_data_new (simple, simple->priv->query);
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2011-11-02 01:06:09 +00:00
|
|
|
g_thread_unref (g_thread_new ("file-search", search_thread_func, data));
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
simple->priv->active_search = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_search_engine_simple_stop (GtkSearchEngine *engine)
|
|
|
|
{
|
|
|
|
GtkSearchEngineSimple *simple;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
|
2015-02-28 21:13:18 +00:00
|
|
|
|
|
|
|
if (simple->priv->active_search != NULL)
|
2007-05-02 22:51:43 +00:00
|
|
|
{
|
2015-05-16 04:37:07 +00:00
|
|
|
g_cancellable_cancel (simple->priv->active_search->cancellable);
|
2007-05-02 22:51:43 +00:00
|
|
|
simple->priv->active_search = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-02-28 21:13:18 +00:00
|
|
|
gtk_search_engine_simple_set_query (GtkSearchEngine *engine,
|
2007-05-02 22:51:43 +00:00
|
|
|
GtkQuery *query)
|
|
|
|
{
|
|
|
|
GtkSearchEngineSimple *simple;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
if (query)
|
|
|
|
g_object_ref (query);
|
|
|
|
|
2015-02-28 21:13:18 +00:00
|
|
|
if (simple->priv->query)
|
2007-05-02 22:51:43 +00:00
|
|
|
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;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
2007-05-02 22:51:43 +00:00
|
|
|
gobject_class = G_OBJECT_CLASS (class);
|
2007-09-25 20:59:15 +00:00
|
|
|
gobject_class->dispose = gtk_search_engine_simple_dispose;
|
2015-02-28 21:13:18 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
return g_object_new (GTK_TYPE_SEARCH_ENGINE_SIMPLE, NULL);
|
2007-05-02 22:51:43 +00:00
|
|
|
}
|