nautilus-search-engine-simple.c 9.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * Copyright (C) 2005 Red Hat, Inc
 *
 * Nautilus is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * Nautilus 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; see the file COPYING.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 *
 */

#include <config.h>
#include "nautilus-search-engine-simple.h"

#include <string.h>
28
#include <glib.h>
29
#include <eel/eel-gtk-macros.h>
30
#include <gio/gio.h>
31 32 33 34 35

#define BATCH_SIZE 500

typedef struct {
	NautilusSearchEngineSimple *engine;
Alexander Larsson's avatar
Alexander Larsson committed
36
	GCancellable *cancellable;
37 38 39 40

	GList *mime_types;
	char **words;
	GList *found_list;
Alexander Larsson's avatar
Alexander Larsson committed
41 42 43 44

	GQueue *directories; /* GFiles */

	GHashTable *visited;
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	
	gint n_processed_files;
	GList *uri_hits;
} SearchThreadData;


struct NautilusSearchEngineSimpleDetails {
	NautilusQuery *query;

	SearchThreadData *active_search;
	
	gboolean query_finished;
};


static void  nautilus_search_engine_simple_class_init       (NautilusSearchEngineSimpleClass *class);
static void  nautilus_search_engine_simple_init             (NautilusSearchEngineSimple      *engine);

G_DEFINE_TYPE (NautilusSearchEngineSimple,
	       nautilus_search_engine_simple,
	       NAUTILUS_TYPE_SEARCH_ENGINE);

static NautilusSearchEngineClass *parent_class = NULL;

static void
finalize (GObject *object)
{
	NautilusSearchEngineSimple *simple;

	simple = NAUTILUS_SEARCH_ENGINE_SIMPLE (object);
	
	if (simple->details->query) {
		g_object_unref (simple->details->query);
		simple->details->query = NULL;
	}

	g_free (simple->details);

	EEL_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
}

static SearchThreadData *
search_thread_data_new (NautilusSearchEngineSimple *engine,
			NautilusQuery *query)
{
	SearchThreadData *data;
91
	char *text, *lower, *normalized, *uri;
Alexander Larsson's avatar
Alexander Larsson committed
92 93
	GFile *location;
	
94 95 96
	data = g_new0 (SearchThreadData, 1);

	data->engine = engine;
Alexander Larsson's avatar
Alexander Larsson committed
97 98
	data->directories = g_queue_new ();
	data->visited = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
99
	uri = nautilus_query_get_location (query);
Alexander Larsson's avatar
Alexander Larsson committed
100
	location = NULL;
101
	if (uri != NULL) {
Alexander Larsson's avatar
Alexander Larsson committed
102
		location = g_file_new_for_uri (uri);
103 104
		g_free (uri);
	}
Alexander Larsson's avatar
Alexander Larsson committed
105 106
	if (location == NULL) {
		location = g_file_new_for_path ("/");
107
	}
Alexander Larsson's avatar
Alexander Larsson committed
108
	g_queue_push_tail (data->directories, location);
109 110
	
	text = nautilus_query_get_text (query);
111 112
	normalized = g_utf8_normalize (text, -1, G_NORMALIZE_NFD);
	lower = g_utf8_strdown (normalized, -1);
113 114 115
	data->words = g_strsplit (lower, " ", -1);
	g_free (text);
	g_free (lower);
116
	g_free (normalized);
117 118 119

	data->mime_types = nautilus_query_get_mime_types (query);

Alexander Larsson's avatar
Alexander Larsson committed
120 121
	data->cancellable = g_cancellable_new ();
	
122 123 124 125 126 127
	return data;
}

static void 
search_thread_data_free (SearchThreadData *data)
{
Alexander Larsson's avatar
Alexander Larsson committed
128 129 130 131 132
	g_queue_foreach (data->directories,
			 (GFunc)g_object_unref, NULL);
	g_queue_free (data->directories);
	g_hash_table_destroy (data->visited);
	g_object_unref (data->cancellable);
133
	g_strfreev (data->words);	
134 135
	g_list_free_full (data->mime_types, g_free);
	g_list_free_full (data->uri_hits, g_free);
136 137 138 139 140 141 142 143 144 145
	g_free (data);
}

static gboolean
search_thread_done_idle (gpointer user_data)
{
	SearchThreadData *data;

	data = user_data;

Alexander Larsson's avatar
Alexander Larsson committed
146
	if (!g_cancellable_is_cancelled (data->cancellable)) {
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
		nautilus_search_engine_finished (NAUTILUS_SEARCH_ENGINE (data->engine));
		data->engine->details->active_search = NULL;
	}
	
	search_thread_data_free (data);
	
	return FALSE;
}

typedef struct {
	GList *uris;
	SearchThreadData *thread_data;
} SearchHits;


static gboolean
search_thread_add_hits_idle (gpointer user_data)
{
	SearchHits *hits;

	hits = user_data;

Alexander Larsson's avatar
Alexander Larsson committed
169
	if (!g_cancellable_is_cancelled (hits->thread_data->cancellable)) {
170 171 172
		nautilus_search_engine_hits_added (NAUTILUS_SEARCH_ENGINE (hits->thread_data->engine),
						   hits->uris);
	}
173 174

	g_list_free_full (hits->uris, g_free);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	g_free (hits);
	
	return FALSE;
}

static void
send_batch (SearchThreadData *data)
{
	SearchHits *hits;
	
	data->n_processed_files = 0;
	
	if (data->uri_hits) {
		hits = g_new (SearchHits, 1);
		hits->uris = data->uri_hits;
		hits->thread_data = data;
		g_idle_add (search_thread_add_hits_idle, hits);
	}
	data->uri_hits = NULL;
}

Alexander Larsson's avatar
Alexander Larsson committed
196
#define STD_ATTRIBUTES \
197 198 199 200
	G_FILE_ATTRIBUTE_STANDARD_NAME "," \
	G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME "," \
	G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN "," \
	G_FILE_ATTRIBUTE_STANDARD_TYPE "," \
Alexander Larsson's avatar
Alexander Larsson committed
201 202 203 204
	G_FILE_ATTRIBUTE_ID_FILE

static void
visit_directory (GFile *dir, SearchThreadData *data)
205
{
Alexander Larsson's avatar
Alexander Larsson committed
206 207 208 209
	GFileEnumerator *enumerator;
	GFileInfo *info;
	GFile *child;
	const char *mime_type, *display_name;
210
	char *lower_name, *normalized;
211
	gboolean hit;
Alexander Larsson's avatar
Alexander Larsson committed
212
	int i;
213
	GList *l;
Alexander Larsson's avatar
Alexander Larsson committed
214 215 216 217 218 219
	const char *id;
	gboolean visited;

	enumerator = g_file_enumerate_children (dir,
						data->mime_types != NULL ?
						STD_ATTRIBUTES ","
220
						G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
Alexander Larsson's avatar
Alexander Larsson committed
221 222 223 224
						:
						STD_ATTRIBUTES
						,
						0, data->cancellable, NULL);
225
	
Alexander Larsson's avatar
Alexander Larsson committed
226 227
	if (enumerator == NULL) {
		return;
228 229
	}

Alexander Larsson's avatar
Alexander Larsson committed
230 231 232 233 234 235 236 237
	while ((info = g_file_enumerator_next_file (enumerator, data->cancellable, NULL)) != NULL) {
		if (g_file_info_get_is_hidden (info)) {
			goto next;
		}
		
		display_name = g_file_info_get_display_name (info);
		if (display_name == NULL) {
			goto next;
238
		}
239
		
Alexander Larsson's avatar
Alexander Larsson committed
240 241 242 243
		normalized = g_utf8_normalize (display_name, -1, G_NORMALIZE_NFD);
		lower_name = g_utf8_strdown (normalized, -1);
		g_free (normalized);
		
244 245 246 247 248 249 250 251
		hit = TRUE;
		for (i = 0; data->words[i] != NULL; i++) {
			if (strstr (lower_name, data->words[i]) == NULL) {
				hit = FALSE;
				break;
			}
		}
		g_free (lower_name);
Alexander Larsson's avatar
Alexander Larsson committed
252 253 254 255 256 257
		
		if (hit && data->mime_types) {
			mime_type = g_file_info_get_content_type (info);
			hit = FALSE;
			
			for (l = data->mime_types; mime_type != NULL && l != NULL; l = l->next) {
258
				if (g_content_type_equals (mime_type, l->data)) {
259 260 261 262 263
					hit = TRUE;
					break;
				}
			}
		}
Alexander Larsson's avatar
Alexander Larsson committed
264 265 266 267 268 269 270 271 272 273 274
		
		child = g_file_get_child (dir, g_file_info_get_name (info));
		
		if (hit) {
			data->uri_hits = g_list_prepend (data->uri_hits, g_file_get_uri (child));
		}
		
		data->n_processed_files++;
		if (data->n_processed_files > BATCH_SIZE) {
			send_batch (data);
		}
275

Alexander Larsson's avatar
Alexander Larsson committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
		if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) {
			id = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILE);
			visited = FALSE;
			if (id) {
				if (g_hash_table_lookup_extended (data->visited,
								  id, NULL, NULL)) {
					visited = TRUE;
				} else {
					g_hash_table_insert (data->visited, g_strdup (id), NULL);
				}
			}
			
			if (!visited) {
				g_queue_push_tail (data->directories, g_object_ref (child));
			}
		}
		
		g_object_unref (child);
	next:
		g_object_unref (info);
296 297
	}

Alexander Larsson's avatar
Alexander Larsson committed
298
	g_object_unref (enumerator);
299 300 301 302 303 304 305
}


static gpointer 
search_thread_func (gpointer user_data)
{
	SearchThreadData *data;
Alexander Larsson's avatar
Alexander Larsson committed
306 307 308
	GFile *dir;
	GFileInfo *info;
	const char *id;
309 310 311

	data = user_data;

Alexander Larsson's avatar
Alexander Larsson committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
	/* Insert id for toplevel directory into visited */
	dir = g_queue_peek_head (data->directories);
	info = g_file_query_info (dir, G_FILE_ATTRIBUTE_ID_FILE, 0, data->cancellable, NULL);
	if (info) {
		id = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILE);
		if (id) {
			g_hash_table_insert (data->visited, g_strdup (id), NULL);
		}
		g_object_unref (info);
	}
	
	while (!g_cancellable_is_cancelled (data->cancellable) &&
	       (dir = g_queue_pop_head (data->directories)) != NULL) {
		visit_directory (dir, data);
		g_object_unref (dir);
	}
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	send_batch (data);

	g_idle_add (search_thread_done_idle, data);
	
	return NULL;
}

static void
nautilus_search_engine_simple_start (NautilusSearchEngine *engine)
{
	NautilusSearchEngineSimple *simple;
	SearchThreadData *data;
	
	simple = NAUTILUS_SEARCH_ENGINE_SIMPLE (engine);

	if (simple->details->active_search != NULL) {
		return;
	}

	if (simple->details->query == NULL) {
		return;
	}
	
	data = search_thread_data_new (simple, simple->details->query);

	g_thread_create (search_thread_func, data, FALSE, NULL);

	simple->details->active_search = data;
}

static void
nautilus_search_engine_simple_stop (NautilusSearchEngine *engine)
{
	NautilusSearchEngineSimple *simple;

	simple = NAUTILUS_SEARCH_ENGINE_SIMPLE (engine);

	if (simple->details->active_search != NULL) {
Alexander Larsson's avatar
Alexander Larsson committed
366
		g_cancellable_cancel (simple->details->active_search->cancellable);
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
		simple->details->active_search = NULL;
	}
}

static gboolean
nautilus_search_engine_simple_is_indexed (NautilusSearchEngine *engine)
{
	return FALSE;
}

static void
nautilus_search_engine_simple_set_query (NautilusSearchEngine *engine, NautilusQuery *query)
{
	NautilusSearchEngineSimple *simple;

	simple = NAUTILUS_SEARCH_ENGINE_SIMPLE (engine);

	if (query) {
		g_object_ref (query);
	}

	if (simple->details->query) {
		g_object_unref (simple->details->query);
	}

	simple->details->query = query;
}

static void
nautilus_search_engine_simple_class_init (NautilusSearchEngineSimpleClass *class)
{
	GObjectClass *gobject_class;
	NautilusSearchEngineClass *engine_class;

	parent_class = g_type_class_peek_parent (class);

	gobject_class = G_OBJECT_CLASS (class);
	gobject_class->finalize = finalize;

	engine_class = NAUTILUS_SEARCH_ENGINE_CLASS (class);
	engine_class->set_query = nautilus_search_engine_simple_set_query;
	engine_class->start = nautilus_search_engine_simple_start;
	engine_class->stop = nautilus_search_engine_simple_stop;
	engine_class->is_indexed = nautilus_search_engine_simple_is_indexed;
}

static void
nautilus_search_engine_simple_init (NautilusSearchEngineSimple *engine)
{
	engine->details = g_new0 (NautilusSearchEngineSimpleDetails, 1);
}


NautilusSearchEngine *
nautilus_search_engine_simple_new (void)
{
	NautilusSearchEngine *engine;

	engine = g_object_new (NAUTILUS_TYPE_SEARCH_ENGINE_SIMPLE, NULL);

	return engine;
}