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
23 #define _XOPEN_SOURCE 500
27 #include "gtksearchenginesimple.h"
29 #define XDG_PREFIX _gtk_xdg
30 #include "xdgmime/xdgmime.h"
34 #include <glib/gstrfuncs.h>
36 #define BATCH_SIZE 500
40 GtkSearchEngineSimple *engine;
47 gint n_processed_files;
50 /* accessed on both threads: */
51 volatile gboolean cancelled;
55 struct _GtkSearchEngineSimplePrivate
59 SearchThreadData *active_search;
61 gboolean query_finished;
65 G_DEFINE_TYPE (GtkSearchEngineSimple, _gtk_search_engine_simple, GTK_TYPE_SEARCH_ENGINE);
68 finalize (GObject *object)
70 GtkSearchEngineSimple *simple;
72 simple = GTK_SEARCH_ENGINE_SIMPLE (object);
74 if (simple->priv->query)
76 g_object_unref (simple->priv->query);
77 simple->priv->query = NULL;
80 g_free (simple->priv);
82 G_OBJECT_CLASS (_gtk_search_engine_simple_parent_class)->finalize (object);
85 static SearchThreadData *
86 search_thread_data_new (GtkSearchEngineSimple *engine,
89 SearchThreadData *data;
90 char *text, *lower, *uri;
92 data = g_new0 (SearchThreadData, 1);
94 data->engine = engine;
95 uri = _gtk_query_get_location (query);
98 data->path = g_filename_from_uri (uri, NULL, NULL);
101 if (data->path == NULL)
102 data->path = g_strdup (g_get_home_dir ());
104 text = _gtk_query_get_text (query);
105 lower = g_ascii_strdown (text, -1);
106 data->words = g_strsplit (lower, " ", -1);
110 data->mime_types = _gtk_query_get_mime_types (query);
116 search_thread_data_free (SearchThreadData *data)
119 g_strfreev (data->words);
120 g_list_foreach (data->mime_types, (GFunc)g_free, NULL);
121 g_list_free (data->mime_types);
126 search_thread_done_idle (gpointer user_data)
128 SearchThreadData *data;
132 if (!data->cancelled)
134 _gtk_search_engine_finished (GTK_SEARCH_ENGINE (data->engine));
135 data->engine->priv->active_search = NULL;
138 search_thread_data_free (data);
146 SearchThreadData *thread_data;
151 search_thread_add_hits_idle (gpointer user_data)
157 if (!hits->thread_data->cancelled)
159 _gtk_search_engine_hits_added (GTK_SEARCH_ENGINE (hits->thread_data->engine),
163 g_list_foreach (hits->uris, (GFunc)g_free, NULL);
164 g_list_free (hits->uris);
171 send_batch (SearchThreadData *data)
175 data->n_processed_files = 0;
179 hits = g_new (SearchHits, 1);
180 hits->uris = data->uri_hits;
181 hits->thread_data = data;
182 g_idle_add (search_thread_add_hits_idle, hits);
184 data->uri_hits = NULL;
187 static GStaticPrivate search_thread_data = G_STATIC_PRIVATE_INIT;
190 search_visit_func (const char *fpath,
191 const struct stat *sb,
195 SearchThreadData *data;
198 gchar *lower_name, *path, *mime_type;
204 data = (SearchThreadData*)g_static_private_get (&search_thread_data);
209 name = strrchr (fpath, '/');
215 path = g_build_filename (data->path, fpath, NULL);
217 is_hidden = *name == '.';
223 lower_name = g_ascii_strdown (name, -1);
226 for (i = 0; data->words[i] != NULL; i++)
228 if (strstr (lower_name, data->words[i]) == NULL)
237 if (hit && data->mime_types != NULL)
240 mime_type = xdg_mime_get_mime_type_for_file (path, (struct stat *)sb);
241 for (l = data->mime_types; l != NULL; l = l->next)
243 if (strcmp (mime_type, l->data) == 0)
255 uri = g_filename_to_uri (path, NULL, NULL);
256 data->uri_hits = g_list_prepend (data->uri_hits, uri);
259 data->n_processed_files++;
261 if (data->n_processed_files > BATCH_SIZE)
265 return FTW_SKIP_SUBTREE;
271 search_thread_func (gpointer user_data)
273 SearchThreadData *data;
277 g_static_private_set (&search_thread_data, data, NULL);
279 nftw (data->path, search_visit_func, 20, FTW_ACTIONRETVAL | FTW_PHYS);
283 g_idle_add (search_thread_done_idle, data);
289 gtk_search_engine_simple_start (GtkSearchEngine *engine)
291 GtkSearchEngineSimple *simple;
292 SearchThreadData *data;
294 simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
296 if (simple->priv->active_search != NULL)
299 if (simple->priv->query == NULL)
302 data = search_thread_data_new (simple, simple->priv->query);
304 g_thread_create (search_thread_func, data, FALSE, NULL);
306 simple->priv->active_search = data;
310 gtk_search_engine_simple_stop (GtkSearchEngine *engine)
312 GtkSearchEngineSimple *simple;
314 simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
316 if (simple->priv->active_search != NULL)
318 simple->priv->active_search->cancelled = TRUE;
319 simple->priv->active_search = NULL;
324 gtk_search_engine_simple_is_indexed (GtkSearchEngine *engine)
330 gtk_search_engine_simple_set_query (GtkSearchEngine *engine,
333 GtkSearchEngineSimple *simple;
335 simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
338 g_object_ref (query);
340 if (simple->priv->query)
341 g_object_unref (simple->priv->query);
343 simple->priv->query = query;
347 _gtk_search_engine_simple_class_init (GtkSearchEngineSimpleClass *class)
349 GObjectClass *gobject_class;
350 GtkSearchEngineClass *engine_class;
352 gobject_class = G_OBJECT_CLASS (class);
353 gobject_class->finalize = finalize;
355 engine_class = GTK_SEARCH_ENGINE_CLASS (class);
356 engine_class->set_query = gtk_search_engine_simple_set_query;
357 engine_class->start = gtk_search_engine_simple_start;
358 engine_class->stop = gtk_search_engine_simple_stop;
359 engine_class->is_indexed = gtk_search_engine_simple_is_indexed;
361 g_type_class_add_private (gobject_class, sizeof (GtkSearchEngineSimplePrivate));
365 _gtk_search_engine_simple_init (GtkSearchEngineSimple *engine)
367 engine->priv = G_TYPE_INSTANCE_GET_PRIVATE (engine, GTK_TYPE_SEARCH_ENGINE_SIMPLE, GtkSearchEngineSimplePrivate);
371 _gtk_search_engine_simple_new (void)
373 GtkSearchEngine *engine;
375 engine = g_object_new (GTK_TYPE_SEARCH_ENGINE_SIMPLE, NULL);