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
39 #include "gtksearchenginesimple.h"
40 #include "gtkprivate.h"
44 #define BATCH_SIZE 500
48 GtkSearchEngineSimple *engine;
54 gint n_processed_files;
57 /* accessed on both threads: */
58 volatile gboolean cancelled;
62 struct _GtkSearchEngineSimplePrivate
66 SearchThreadData *active_search;
68 gboolean query_finished;
72 G_DEFINE_TYPE (GtkSearchEngineSimple, _gtk_search_engine_simple, GTK_TYPE_SEARCH_ENGINE);
75 gtk_search_engine_simple_dispose (GObject *object)
77 GtkSearchEngineSimple *simple;
78 GtkSearchEngineSimplePrivate *priv;
80 simple = GTK_SEARCH_ENGINE_SIMPLE (object);
85 g_object_unref (priv->query);
89 if (priv->active_search)
91 priv->active_search->cancelled = TRUE;
92 priv->active_search = NULL;
95 G_OBJECT_CLASS (_gtk_search_engine_simple_parent_class)->dispose (object);
98 static SearchThreadData *
99 search_thread_data_new (GtkSearchEngineSimple *engine,
102 SearchThreadData *data;
103 char *text, *lower, *uri;
105 data = g_new0 (SearchThreadData, 1);
107 data->engine = g_object_ref (engine);
108 uri = _gtk_query_get_location (query);
111 data->path = g_filename_from_uri (uri, NULL, NULL);
114 if (data->path == NULL)
115 data->path = g_strdup (g_get_home_dir ());
117 text = _gtk_query_get_text (query);
118 lower = g_ascii_strdown (text, -1);
119 data->words = g_strsplit (lower, " ", -1);
127 search_thread_data_free (SearchThreadData *data)
129 g_object_unref (data->engine);
131 g_strfreev (data->words);
136 search_thread_done_idle (gpointer user_data)
138 SearchThreadData *data;
142 if (!data->cancelled)
143 _gtk_search_engine_finished (GTK_SEARCH_ENGINE (data->engine));
145 data->engine->priv->active_search = NULL;
146 search_thread_data_free (data);
154 SearchThreadData *thread_data;
159 search_thread_add_hits_idle (gpointer user_data)
165 if (!hits->thread_data->cancelled)
167 _gtk_search_engine_hits_added (GTK_SEARCH_ENGINE (hits->thread_data->engine),
171 g_list_free_full (hits->uris, g_free);
178 send_batch (SearchThreadData *data)
182 data->n_processed_files = 0;
186 hits = g_new (SearchHits, 1);
187 hits->uris = data->uri_hits;
188 hits->thread_data = data;
190 gdk_threads_add_idle (search_thread_add_hits_idle, hits);
193 data->uri_hits = NULL;
196 static GPrivate search_thread_data;
200 search_visit_func (const char *fpath,
201 const struct stat *sb,
205 SearchThreadData *data;
213 data = (SearchThreadData*)g_private_get (&search_thread_data);
220 #endif /* HAVE_GNU_FTW */
222 name = strrchr (fpath, '/');
228 is_hidden = *name == '.';
234 lower_name = g_ascii_strdown (name, -1);
237 for (i = 0; data->words[i] != NULL; i++)
239 if (strstr (lower_name, data->words[i]) == NULL)
250 uri = g_filename_to_uri (fpath, NULL, NULL);
251 data->uri_hits = g_list_prepend (data->uri_hits, uri);
254 data->n_processed_files++;
256 if (data->n_processed_files > BATCH_SIZE)
261 return FTW_SKIP_SUBTREE;
266 #endif /* HAVE_GNU_FTW */
268 #endif /* HAVE_FTW_H */
271 search_thread_func (gpointer user_data)
274 SearchThreadData *data;
278 g_private_set (&search_thread_data, data);
280 nftw (data->path, search_visit_func, 20,
288 gdk_threads_add_idle (search_thread_done_idle, data);
289 #endif /* HAVE_FTW_H */
295 gtk_search_engine_simple_start (GtkSearchEngine *engine)
297 GtkSearchEngineSimple *simple;
298 SearchThreadData *data;
300 simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
302 if (simple->priv->active_search != NULL)
305 if (simple->priv->query == NULL)
308 data = search_thread_data_new (simple, simple->priv->query);
310 g_thread_unref (g_thread_new ("file-search", search_thread_func, data));
312 simple->priv->active_search = data;
316 gtk_search_engine_simple_stop (GtkSearchEngine *engine)
318 GtkSearchEngineSimple *simple;
320 simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
322 if (simple->priv->active_search != NULL)
324 simple->priv->active_search->cancelled = TRUE;
325 simple->priv->active_search = NULL;
330 gtk_search_engine_simple_is_indexed (GtkSearchEngine *engine)
336 gtk_search_engine_simple_set_query (GtkSearchEngine *engine,
339 GtkSearchEngineSimple *simple;
341 simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
344 g_object_ref (query);
346 if (simple->priv->query)
347 g_object_unref (simple->priv->query);
349 simple->priv->query = query;
353 _gtk_search_engine_simple_class_init (GtkSearchEngineSimpleClass *class)
355 GObjectClass *gobject_class;
356 GtkSearchEngineClass *engine_class;
358 gobject_class = G_OBJECT_CLASS (class);
359 gobject_class->dispose = gtk_search_engine_simple_dispose;
361 engine_class = GTK_SEARCH_ENGINE_CLASS (class);
362 engine_class->set_query = gtk_search_engine_simple_set_query;
363 engine_class->start = gtk_search_engine_simple_start;
364 engine_class->stop = gtk_search_engine_simple_stop;
365 engine_class->is_indexed = gtk_search_engine_simple_is_indexed;
367 g_type_class_add_private (gobject_class, sizeof (GtkSearchEngineSimplePrivate));
371 _gtk_search_engine_simple_init (GtkSearchEngineSimple *engine)
373 engine->priv = G_TYPE_INSTANCE_GET_PRIVATE (engine, GTK_TYPE_SEARCH_ENGINE_SIMPLE, GtkSearchEngineSimplePrivate);
377 _gtk_search_engine_simple_new (void)
380 return g_object_new (GTK_TYPE_SEARCH_ENGINE_SIMPLE, NULL);