2 * Copyright (C) 2005 Red Hat, Inc
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Author: Alexander Larsson <alexl@redhat.com>
20 * Based on nautilus-search-engine-simple.c
25 /* these must be defined even when HAVE_GNU_FTW is not defined
26 * because (really) old versions of GNU libc have ftw.h but do
27 * export ftw() and friends only if _XOPEN_SOURCE and _GNU_SOURCE
28 * are defined. see bug #444097.
30 #define _XOPEN_SOURCE 500
37 #include "gtksearchenginesimple.h"
38 #include "gtkprivate.h"
42 #define BATCH_SIZE 500
46 GtkSearchEngineSimple *engine;
52 gint n_processed_files;
55 /* accessed on both threads: */
56 volatile gboolean cancelled;
60 struct _GtkSearchEngineSimplePrivate
64 SearchThreadData *active_search;
66 gboolean query_finished;
70 G_DEFINE_TYPE (GtkSearchEngineSimple, _gtk_search_engine_simple, GTK_TYPE_SEARCH_ENGINE);
73 gtk_search_engine_simple_dispose (GObject *object)
75 GtkSearchEngineSimple *simple;
76 GtkSearchEngineSimplePrivate *priv;
78 simple = GTK_SEARCH_ENGINE_SIMPLE (object);
83 g_object_unref (priv->query);
87 if (priv->active_search)
89 priv->active_search->cancelled = TRUE;
90 priv->active_search = NULL;
93 G_OBJECT_CLASS (_gtk_search_engine_simple_parent_class)->dispose (object);
96 static SearchThreadData *
97 search_thread_data_new (GtkSearchEngineSimple *engine,
100 SearchThreadData *data;
101 char *text, *lower, *uri;
103 data = g_new0 (SearchThreadData, 1);
105 data->engine = g_object_ref (engine);
106 uri = _gtk_query_get_location (query);
109 data->path = g_filename_from_uri (uri, NULL, NULL);
112 if (data->path == NULL)
113 data->path = g_strdup (g_get_home_dir ());
115 text = _gtk_query_get_text (query);
116 lower = g_ascii_strdown (text, -1);
117 data->words = g_strsplit (lower, " ", -1);
125 search_thread_data_free (SearchThreadData *data)
127 g_object_unref (data->engine);
129 g_strfreev (data->words);
134 search_thread_done_idle (gpointer user_data)
136 SearchThreadData *data;
140 if (!data->cancelled)
141 _gtk_search_engine_finished (GTK_SEARCH_ENGINE (data->engine));
143 data->engine->priv->active_search = NULL;
144 search_thread_data_free (data);
152 SearchThreadData *thread_data;
157 search_thread_add_hits_idle (gpointer user_data)
163 if (!hits->thread_data->cancelled)
165 _gtk_search_engine_hits_added (GTK_SEARCH_ENGINE (hits->thread_data->engine),
169 g_list_foreach (hits->uris, (GFunc)g_free, NULL);
170 g_list_free (hits->uris);
177 send_batch (SearchThreadData *data)
181 data->n_processed_files = 0;
185 hits = g_new (SearchHits, 1);
186 hits->uris = data->uri_hits;
187 hits->thread_data = data;
189 gdk_threads_add_idle (search_thread_add_hits_idle, hits);
191 data->uri_hits = NULL;
194 static GStaticPrivate search_thread_data = G_STATIC_PRIVATE_INIT;
198 search_visit_func (const char *fpath,
199 const struct stat *sb,
203 SearchThreadData *data;
211 data = (SearchThreadData*)g_static_private_get (&search_thread_data);
218 #endif /* HAVE_GNU_FTW */
220 name = strrchr (fpath, '/');
226 is_hidden = *name == '.';
232 lower_name = g_ascii_strdown (name, -1);
235 for (i = 0; data->words[i] != NULL; i++)
237 if (strstr (lower_name, data->words[i]) == NULL)
248 uri = g_filename_to_uri (fpath, NULL, NULL);
249 data->uri_hits = g_list_prepend (data->uri_hits, uri);
252 data->n_processed_files++;
254 if (data->n_processed_files > BATCH_SIZE)
259 return FTW_SKIP_SUBTREE;
264 #endif /* HAVE_GNU_FTW */
266 #endif /* HAVE_FTW_H */
269 search_thread_func (gpointer user_data)
272 SearchThreadData *data;
276 g_static_private_set (&search_thread_data, data, NULL);
278 nftw (data->path, search_visit_func, 20,
286 gdk_threads_add_idle (search_thread_done_idle, data);
287 #endif /* HAVE_FTW_H */
293 gtk_search_engine_simple_start (GtkSearchEngine *engine)
295 GtkSearchEngineSimple *simple;
296 SearchThreadData *data;
298 simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
300 if (simple->priv->active_search != NULL)
303 if (simple->priv->query == NULL)
306 data = search_thread_data_new (simple, simple->priv->query);
308 g_thread_create (search_thread_func, data, FALSE, NULL);
310 simple->priv->active_search = data;
314 gtk_search_engine_simple_stop (GtkSearchEngine *engine)
316 GtkSearchEngineSimple *simple;
318 simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
320 if (simple->priv->active_search != NULL)
322 simple->priv->active_search->cancelled = TRUE;
323 simple->priv->active_search = NULL;
328 gtk_search_engine_simple_is_indexed (GtkSearchEngine *engine)
334 gtk_search_engine_simple_set_query (GtkSearchEngine *engine,
337 GtkSearchEngineSimple *simple;
339 simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
342 g_object_ref (query);
344 if (simple->priv->query)
345 g_object_unref (simple->priv->query);
347 simple->priv->query = query;
351 _gtk_search_engine_simple_class_init (GtkSearchEngineSimpleClass *class)
353 GObjectClass *gobject_class;
354 GtkSearchEngineClass *engine_class;
356 gobject_class = G_OBJECT_CLASS (class);
357 gobject_class->dispose = gtk_search_engine_simple_dispose;
359 engine_class = GTK_SEARCH_ENGINE_CLASS (class);
360 engine_class->set_query = gtk_search_engine_simple_set_query;
361 engine_class->start = gtk_search_engine_simple_start;
362 engine_class->stop = gtk_search_engine_simple_stop;
363 engine_class->is_indexed = gtk_search_engine_simple_is_indexed;
365 g_type_class_add_private (gobject_class, sizeof (GtkSearchEngineSimplePrivate));
369 _gtk_search_engine_simple_init (GtkSearchEngineSimple *engine)
371 engine->priv = G_TYPE_INSTANCE_GET_PRIVATE (engine, GTK_TYPE_SEARCH_ENGINE_SIMPLE, GtkSearchEngineSimplePrivate);
375 _gtk_search_engine_simple_new (void)
378 return g_object_new (GTK_TYPE_SEARCH_ENGINE_SIMPLE, NULL);