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"
39 #define XDG_PREFIX _gtk_xdg
40 #include "xdgmime/xdgmime.h"
44 #include <glib/gstrfuncs.h>
46 #define BATCH_SIZE 500
50 GtkSearchEngineSimple *engine;
57 gint n_processed_files;
60 /* accessed on both threads: */
61 volatile gboolean cancelled;
65 struct _GtkSearchEngineSimplePrivate
69 SearchThreadData *active_search;
71 gboolean query_finished;
75 G_DEFINE_TYPE (GtkSearchEngineSimple, _gtk_search_engine_simple, GTK_TYPE_SEARCH_ENGINE);
78 finalize (GObject *object)
80 GtkSearchEngineSimple *simple;
82 simple = GTK_SEARCH_ENGINE_SIMPLE (object);
84 if (simple->priv->query)
86 g_object_unref (simple->priv->query);
87 simple->priv->query = NULL;
90 G_OBJECT_CLASS (_gtk_search_engine_simple_parent_class)->finalize (object);
93 static SearchThreadData *
94 search_thread_data_new (GtkSearchEngineSimple *engine,
97 SearchThreadData *data;
98 char *text, *lower, *uri;
100 data = g_new0 (SearchThreadData, 1);
102 data->engine = engine;
103 uri = _gtk_query_get_location (query);
106 data->path = g_filename_from_uri (uri, NULL, NULL);
109 if (data->path == NULL)
110 data->path = g_strdup (g_get_home_dir ());
112 text = _gtk_query_get_text (query);
113 lower = g_ascii_strdown (text, -1);
114 data->words = g_strsplit (lower, " ", -1);
118 data->mime_types = _gtk_query_get_mime_types (query);
124 search_thread_data_free (SearchThreadData *data)
127 g_strfreev (data->words);
128 g_list_foreach (data->mime_types, (GFunc)g_free, NULL);
129 g_list_free (data->mime_types);
134 search_thread_done_idle (gpointer user_data)
136 SearchThreadData *data;
140 if (!data->cancelled)
142 _gtk_search_engine_finished (GTK_SEARCH_ENGINE (data->engine));
143 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_foreach (hits->uris, (GFunc)g_free, NULL);
172 g_list_free (hits->uris);
179 send_batch (SearchThreadData *data)
183 data->n_processed_files = 0;
187 hits = g_new (SearchHits, 1);
188 hits->uris = data->uri_hits;
189 hits->thread_data = data;
190 g_idle_add (search_thread_add_hits_idle, hits);
192 data->uri_hits = NULL;
195 static GStaticPrivate search_thread_data = G_STATIC_PRIVATE_INIT;
199 search_visit_func (const char *fpath,
200 const struct stat *sb,
204 SearchThreadData *data;
207 gchar *lower_name, *mime_type;
213 data = (SearchThreadData*)g_static_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)
248 if (hit && data->mime_types != NULL)
251 mime_type = xdg_mime_get_mime_type_for_file (fpath, (struct stat *)sb);
252 for (l = data->mime_types; l != NULL; l = l->next)
254 if (strcmp (mime_type, l->data) == 0)
266 uri = g_filename_to_uri (fpath, NULL, NULL);
267 data->uri_hits = g_list_prepend (data->uri_hits, uri);
270 data->n_processed_files++;
272 if (data->n_processed_files > BATCH_SIZE)
277 return FTW_SKIP_SUBTREE;
282 #endif /* HAVE_GNU_FTW */
284 #endif /* HAVE_FTW_H */
287 search_thread_func (gpointer user_data)
290 SearchThreadData *data;
294 g_static_private_set (&search_thread_data, data, NULL);
296 nftw (data->path, search_visit_func, 20,
304 g_idle_add (search_thread_done_idle, data);
305 #endif /* HAVE_FTW_H */
311 gtk_search_engine_simple_start (GtkSearchEngine *engine)
313 GtkSearchEngineSimple *simple;
314 SearchThreadData *data;
316 simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
318 if (simple->priv->active_search != NULL)
321 if (simple->priv->query == NULL)
324 data = search_thread_data_new (simple, simple->priv->query);
326 g_thread_create (search_thread_func, data, FALSE, NULL);
328 simple->priv->active_search = data;
332 gtk_search_engine_simple_stop (GtkSearchEngine *engine)
334 GtkSearchEngineSimple *simple;
336 simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
338 if (simple->priv->active_search != NULL)
340 simple->priv->active_search->cancelled = TRUE;
341 simple->priv->active_search = NULL;
346 gtk_search_engine_simple_is_indexed (GtkSearchEngine *engine)
352 gtk_search_engine_simple_set_query (GtkSearchEngine *engine,
355 GtkSearchEngineSimple *simple;
357 simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
360 g_object_ref (query);
362 if (simple->priv->query)
363 g_object_unref (simple->priv->query);
365 simple->priv->query = query;
369 _gtk_search_engine_simple_class_init (GtkSearchEngineSimpleClass *class)
371 GObjectClass *gobject_class;
372 GtkSearchEngineClass *engine_class;
374 gobject_class = G_OBJECT_CLASS (class);
375 gobject_class->finalize = finalize;
377 engine_class = GTK_SEARCH_ENGINE_CLASS (class);
378 engine_class->set_query = gtk_search_engine_simple_set_query;
379 engine_class->start = gtk_search_engine_simple_start;
380 engine_class->stop = gtk_search_engine_simple_stop;
381 engine_class->is_indexed = gtk_search_engine_simple_is_indexed;
383 g_type_class_add_private (gobject_class, sizeof (GtkSearchEngineSimplePrivate));
387 _gtk_search_engine_simple_init (GtkSearchEngineSimple *engine)
389 engine->priv = G_TYPE_INSTANCE_GET_PRIVATE (engine, GTK_TYPE_SEARCH_ENGINE_SIMPLE, GtkSearchEngineSimplePrivate);
393 _gtk_search_engine_simple_new (void)
396 return g_object_new (GTK_TYPE_SEARCH_ENGINE_SIMPLE, NULL);