gsearchtool.c 104 KB
Newer Older
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
/*
3 4 5 6
 * GNOME Search Tool
 *
 *  File:  gsearchtool.c
 *
7
 *  (C) 1998,2002 the Free Software Foundation
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *
 *  Authors:    Dennis Cranston  <dennis_cranston@yahoo.com>
 *              George Lebl
 *
 *  This program 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.
 *
 *  This program 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; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <fnmatch.h>
#ifndef FNM_CASEFOLD
#  define FNM_CASEFOLD 0
#endif

#include <string.h>
#include <signal.h>
#include <unistd.h>
40 41
#include <errno.h>
#include <stdlib.h>
42
#include <sys/wait.h>
43
#include <glib/gi18n.h>
44
#include <gdk/gdk.h>
45
#include <gio/gio.h>
46
#include <locale.h>
47 48 49 50

#include "gsearchtool.h"
#include "gsearchtool-callbacks.h"
#include "gsearchtool-support.h"
51
#include "gsearchtool-entry.h"
52

53
#define GNOME_SEARCH_TOOL_DEFAULT_ICON_SIZE 16
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
#define GNOME_SEARCH_TOOL_STOCK "panel-searchtool"
#define GNOME_SEARCH_TOOL_REFRESH_DURATION  50000
#define LEFT_LABEL_SPACING "     "

static GObjectClass * parent_class;

typedef enum {
	SEARCH_CONSTRAINT_TYPE_BOOLEAN,
	SEARCH_CONSTRAINT_TYPE_NUMERIC,
	SEARCH_CONSTRAINT_TYPE_TEXT,
	SEARCH_CONSTRAINT_TYPE_DATE_BEFORE,
	SEARCH_CONSTRAINT_TYPE_DATE_AFTER,
	SEARCH_CONSTRAINT_TYPE_SEPARATOR,
	SEARCH_CONSTRAINT_TYPE_NONE
} GSearchConstraintType;

typedef struct _GSearchOptionTemplate GSearchOptionTemplate;

struct _GSearchOptionTemplate {
	GSearchConstraintType type; /* The available option type */
	gchar * option;             /* An option string to pass to the command */
	gchar * desc;               /* The description for display */
	gchar * units;              /* Optional units for display */
	gboolean is_selected;
};

static GSearchOptionTemplate GSearchOptionTemplates[] = {
81
	{ SEARCH_CONSTRAINT_TYPE_TEXT, NULL, N_("Contains the _text"), NULL, FALSE },
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	{ SEARCH_CONSTRAINT_TYPE_SEPARATOR, NULL, NULL, NULL, TRUE },
	{ SEARCH_CONSTRAINT_TYPE_DATE_BEFORE, "-mtime -%d", N_("_Date modified less than"), N_("days"), FALSE },
	{ SEARCH_CONSTRAINT_TYPE_DATE_AFTER, "\\( -mtime +%d -o -mtime %d \\)", N_("Date modified more than"), N_("days"), FALSE },
	{ SEARCH_CONSTRAINT_TYPE_SEPARATOR, NULL, NULL, NULL, TRUE },
	{ SEARCH_CONSTRAINT_TYPE_NUMERIC, "\\( -size %uc -o -size +%uc \\)", N_("S_ize at least"), N_("kilobytes"), FALSE },
	{ SEARCH_CONSTRAINT_TYPE_NUMERIC, "\\( -size %uc -o -size -%uc \\)", N_("Si_ze at most"), N_("kilobytes"), FALSE },
	{ SEARCH_CONSTRAINT_TYPE_BOOLEAN, "-size 0c \\( -type f -o -type d \\)", N_("File is empty"), NULL, FALSE },
	{ SEARCH_CONSTRAINT_TYPE_SEPARATOR, NULL, NULL, NULL, TRUE },
	{ SEARCH_CONSTRAINT_TYPE_TEXT, "-user '%s'", N_("Owned by _user"), NULL, FALSE },
	{ SEARCH_CONSTRAINT_TYPE_TEXT, "-group '%s'", N_("Owned by _group"), NULL, FALSE },
	{ SEARCH_CONSTRAINT_TYPE_BOOLEAN, "\\( -nouser -o -nogroup \\)", N_("Owner is unrecognized"), NULL, FALSE },
	{ SEARCH_CONSTRAINT_TYPE_SEPARATOR, NULL, NULL, NULL, TRUE },
	{ SEARCH_CONSTRAINT_TYPE_TEXT, "'!' -name '*%s*'", N_("Na_me does not contain"), NULL, FALSE },
	{ SEARCH_CONSTRAINT_TYPE_TEXT, "-regex '%s'", N_("Name matches regular e_xpression"), NULL, FALSE },
	{ SEARCH_CONSTRAINT_TYPE_SEPARATOR, NULL, NULL, NULL, TRUE },
	{ SEARCH_CONSTRAINT_TYPE_BOOLEAN, "SHOW_HIDDEN_FILES", N_("Show hidden and backup files"), NULL, FALSE },
	{ SEARCH_CONSTRAINT_TYPE_BOOLEAN, "-follow", N_("Follow symbolic links"), NULL, FALSE },
99
	{ SEARCH_CONSTRAINT_TYPE_BOOLEAN, "EXCLUDE_OTHER_FILESYSTEMS", N_("Exclude other filesystems"), NULL, FALSE },
100
	{ SEARCH_CONSTRAINT_TYPE_NONE, NULL, NULL, NULL, FALSE}
101
};
102 103 104 105 106 107 108

enum {
	SEARCH_CONSTRAINT_CONTAINS_THE_TEXT,
	SEARCH_CONSTRAINT_TYPE_SEPARATOR_00,
	SEARCH_CONSTRAINT_DATE_MODIFIED_BEFORE,
	SEARCH_CONSTRAINT_DATE_MODIFIED_AFTER,
	SEARCH_CONSTRAINT_TYPE_SEPARATOR_01,
109
	SEARCH_CONSTRAINT_SIZE_IS_MORE_THAN,
110 111 112 113 114 115
	SEARCH_CONSTRAINT_SIZE_IS_LESS_THAN,
	SEARCH_CONSTRAINT_FILE_IS_EMPTY,
	SEARCH_CONSTRAINT_TYPE_SEPARATOR_02,
	SEARCH_CONSTRAINT_OWNED_BY_USER,
	SEARCH_CONSTRAINT_OWNED_BY_GROUP,
	SEARCH_CONSTRAINT_OWNER_IS_UNRECOGNIZED,
116
	SEARCH_CONSTRAINT_TYPE_SEPARATOR_03,
117 118 119 120 121 122 123 124 125 126
	SEARCH_CONSTRAINT_FILE_IS_NOT_NAMED,
	SEARCH_CONSTRAINT_FILE_MATCHES_REGULAR_EXPRESSION,
	SEARCH_CONSTRAINT_TYPE_SEPARATOR_04,
	SEARCH_CONSTRAINT_SHOW_HIDDEN_FILES_AND_FOLDERS,
	SEARCH_CONSTRAINT_FOLLOW_SYMBOLIC_LINKS,
	SEARCH_CONSTRAINT_SEARCH_OTHER_FILESYSTEMS,
	SEARCH_CONSTRAINT_MAXIMUM_POSSIBLE
};

static GtkTargetEntry GSearchDndTable[] = {
127
	{ "text/uri-list", 0, 1 },
128
	{ "text/plain",    0, 0 },
129
	{ "STRING",        0, 0 }
130 131 132 133
};

static guint GSearchTotalDnds = sizeof (GSearchDndTable) / sizeof (GSearchDndTable[0]);

134
struct _GSearchGOptionArguments {
135 136 137 138 139 140 141 142
	gchar * name;
	gchar * path;
	gchar * contains;
	gchar * user;
	gchar * group;
	gboolean nouser;
	gchar * mtimeless;
	gchar * mtimemore;
143
	gchar * sizeless;
144 145 146 147 148 149
	gchar * sizemore;
	gboolean empty;
	gchar * notnamed;
	gchar * regex;
	gboolean hidden;
	gboolean follow;
150
	gboolean mounts;
151 152 153
	gchar * sortby;
	gboolean descending;
	gboolean start;
154 155 156
} GSearchGOptionArguments;

static GOptionEntry GSearchGOptionEntries[] = {
157
	{ "version", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, version_cb, N_("Show version of the application"), NULL},
158 159 160
	{ "named", 0, 0, G_OPTION_ARG_STRING, &GSearchGOptionArguments.name, NULL, N_("STRING") },
	{ "path", 0, 0, G_OPTION_ARG_STRING, &GSearchGOptionArguments.path, NULL, N_("PATH") },
	{ "sortby", 0, 0, G_OPTION_ARG_STRING, &GSearchGOptionArguments.sortby, NULL, N_("VALUE") },
161 162
	{ "descending", 0, 0, G_OPTION_ARG_NONE, &GSearchGOptionArguments.descending, NULL, NULL },
	{ "start", 0, 0, G_OPTION_ARG_NONE, &GSearchGOptionArguments.start, NULL, NULL },
163 164 165 166 167
	{ "contains", 0, 0, G_OPTION_ARG_STRING, &GSearchGOptionArguments.contains, NULL, N_("STRING") },
	{ "mtimeless", 0, 0, G_OPTION_ARG_STRING, &GSearchGOptionArguments.mtimeless, NULL, N_("DAYS") },
	{ "mtimemore", 0, 0, G_OPTION_ARG_STRING, &GSearchGOptionArguments.mtimemore, NULL, N_("DAYS") },
	{ "sizemore", 0, 0, G_OPTION_ARG_STRING, &GSearchGOptionArguments.sizemore, NULL, N_("KILOBYTES") },
	{ "sizeless", 0, 0, G_OPTION_ARG_STRING, &GSearchGOptionArguments.sizeless, NULL, N_("KILOBYTES") },
168
	{ "empty", 0, 0, G_OPTION_ARG_NONE, &GSearchGOptionArguments.empty, NULL, NULL },
169 170
	{ "user", 0, 0, G_OPTION_ARG_STRING, &GSearchGOptionArguments.user, NULL, N_("USER") },
	{ "group", 0, 0, G_OPTION_ARG_STRING, &GSearchGOptionArguments.group, NULL, N_("GROUP") },
171
	{ "nouser", 0, 0, G_OPTION_ARG_NONE, &GSearchGOptionArguments.nouser, NULL, NULL },
172 173
	{ "notnamed", 0, 0, G_OPTION_ARG_STRING, &GSearchGOptionArguments.notnamed, NULL, N_("STRING") },
	{ "regex", 0, 0, G_OPTION_ARG_STRING, &GSearchGOptionArguments.regex, NULL, N_("PATTERN") },
174 175
	{ "hidden", 0, 0, G_OPTION_ARG_NONE, &GSearchGOptionArguments.hidden, NULL, NULL },
	{ "follow", 0, 0, G_OPTION_ARG_NONE, &GSearchGOptionArguments.follow, NULL, NULL },
176
	{ "mounts", 0, 0, G_OPTION_ARG_NONE, &GSearchGOptionArguments.mounts, NULL, NULL },
177
	{ NULL }
178 179 180 181
};

static gchar * find_command_default_name_argument;
static gchar * locate_command_default_options;
182 183 184
pid_t locate_database_check_command_pid;

static gboolean
185 186 187
handle_locate_command_stdout_io (GIOChannel * ioc,
                                 GIOCondition condition,
                                 gpointer data)
188 189 190
{
	GSearchWindow * gsearch = data;
	gboolean broken_pipe = FALSE;
191 192 193

	if (condition & G_IO_IN) {

194 195
		GError * error = NULL;
		GString * string;
196

197 198 199 200 201 202
		string = g_string_new (NULL);

		while (ioc->is_readable != TRUE);

		do {
			gint status;
203

204 205
			do {
				status = g_io_channel_read_line_string (ioc, string, NULL, &error);
206

207 208 209 210 211 212 213 214 215 216
				if (status == G_IO_STATUS_EOF) {
					broken_pipe = TRUE;
				}
				else if (status == G_IO_STATUS_AGAIN) {
					if (gtk_events_pending ()) {
						while (gtk_events_pending ()) {
							gtk_main_iteration ();
						}
					}
				}
217 218
				else if ((string->len != 0) && (strncmp (string->str, "/", 1) == 0)) {
					gsearch->is_locate_database_available = TRUE;
219 220
					broken_pipe = TRUE;
				}
221

222
			} while (status == G_IO_STATUS_AGAIN && broken_pipe == FALSE);
223

224 225 226
			if (broken_pipe == TRUE) {
				break;
			}
227

228 229 230 231 232 233
			if (status != G_IO_STATUS_NORMAL) {
				if (error != NULL) {
					g_warning ("handle_locate_command_stdout_io(): %s", error->message);
					g_error_free (error);
				}
			}
234

235
		} while (g_io_channel_get_buffer_condition (ioc) & G_IO_IN);
236

237
		waitpid (locate_database_check_command_pid, NULL, 0);
238 239 240
		g_string_free (string, TRUE);
	}

241
	if (!(condition & G_IO_IN) || broken_pipe == TRUE) {
242 243 244 245 246 247
		gsearch->is_locate_database_check_finished = TRUE;
		g_io_channel_shutdown (ioc, TRUE, NULL);
		return FALSE;
	}
	return TRUE;
}
248 249

static void
250
setup_case_insensitive_arguments (GSearchWindow * gsearch)
251
{
252
	static gboolean case_insensitive_arguments_initialized = FALSE;
253
	gchar * cmd_stderr;
254
	gchar * grep_cmd;
255
	gchar * locate;
256

257
	if (case_insensitive_arguments_initialized == TRUE) {
258 259
		return;
	}
260
	case_insensitive_arguments_initialized = TRUE;
261 262 263 264 265 266 267 268 269 270 271 272 273 274

	/* check find command for -iname argument compatibility */
	g_spawn_command_line_sync ("find /dev/null -iname 'string'", NULL, &cmd_stderr, NULL, NULL);

	if ((cmd_stderr != NULL) && (strlen (cmd_stderr) == 0)) {
		find_command_default_name_argument = g_strdup ("-iname");
		GSearchOptionTemplates[SEARCH_CONSTRAINT_FILE_IS_NOT_NAMED].option = g_strdup ("'!' -iname '*%s*'");
	}
	else {
		find_command_default_name_argument = g_strdup ("-name");
	}
	g_free (cmd_stderr);

	/* check grep command for -i argument compatibility */
275 276
	grep_cmd = g_strdup_printf ("%s -i 'string' /dev/null", GREP_COMMAND);
 	g_spawn_command_line_sync (grep_cmd, NULL, &cmd_stderr, NULL, NULL);
277 278

	if ((cmd_stderr != NULL) && (strlen (cmd_stderr) == 0)) {
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
		g_free (cmd_stderr);
		g_free (grep_cmd);
		
		/* check grep command for -I argument compatibility, bug 568840 */
		grep_cmd = g_strdup_printf ("%s -i -I 'string' /dev/null", GREP_COMMAND);
	 	g_spawn_command_line_sync (grep_cmd, NULL, &cmd_stderr, NULL, NULL);
		
		if ((cmd_stderr != NULL) && (strlen (cmd_stderr) == 0)) {
	 		GSearchOptionTemplates[SEARCH_CONSTRAINT_CONTAINS_THE_TEXT].option =
			    g_strdup_printf ("'!' -type p -exec %s -i -I -c '%%s' {} \\;", GREP_COMMAND);
 		}
		else {
	 		GSearchOptionTemplates[SEARCH_CONSTRAINT_CONTAINS_THE_TEXT].option =
			    g_strdup_printf ("'!' -type p -exec %s -i -c '%%s' {} \\;", GREP_COMMAND);		
		}		
	}
295
	else {
296
 		GSearchOptionTemplates[SEARCH_CONSTRAINT_CONTAINS_THE_TEXT].option =
297
		    g_strdup_printf ("'!' -type p -exec %s -c '%%s' {} \\;", GREP_COMMAND);
298 299 300
	}
	g_free (cmd_stderr);

301
	locate = g_find_program_in_path ("locate");
302

303
	if (locate != NULL) {
304 305 306 307
		GIOChannel * ioc_stdout;
		gchar ** argv  = NULL;
		gchar *command = NULL;
		gint child_stdout;
308

309 310 311 312
		/* check locate command for -i argument compatibility */
		command = g_strconcat (locate, " -i /", NULL);
		g_shell_parse_argv (command, NULL, &argv, NULL);
		g_free (command);
313

314 315
		gsearch->is_locate_database_check_finished = FALSE;
		gsearch->is_locate_database_available = FALSE;
316

317
		/* run locate command asynchronously because on some systems it can be slow */
318 319 320
		if (g_spawn_async_with_pipes (g_get_home_dir (), argv, NULL,
				       G_SPAWN_SEARCH_PATH, NULL, NULL,
				       &locate_database_check_command_pid, NULL, &child_stdout,
321 322 323 324 325 326
				       NULL, NULL)) {

			ioc_stdout = g_io_channel_unix_new (child_stdout);
			g_io_channel_set_encoding (ioc_stdout, NULL, NULL);
			g_io_channel_set_flags (ioc_stdout, G_IO_FLAG_NONBLOCK, NULL);
			g_io_add_watch (ioc_stdout, G_IO_IN | G_IO_HUP,
327
					handle_locate_command_stdout_io, gsearch);
328 329 330 331 332
			g_io_channel_unref (ioc_stdout);
		}
		else {
			gsearch->is_locate_database_check_finished = TRUE;
		}
333

334
		g_strfreev (argv);
335 336

		while (gsearch->is_locate_database_check_finished == FALSE) {
337 338 339 340
			if (gtk_events_pending ()) {
				while (gtk_events_pending ()) {
					gtk_main_iteration ();
				}
341
			}
342 343
		}

344 345
		if (gsearch->is_locate_database_available == TRUE) {
			locate_command_default_options = g_strdup ("-i");
346 347
		}
		else {
348 349 350 351
			/* run locate again to check if it can find anything */
			command = g_strconcat (locate, " /", NULL);
			g_shell_parse_argv (command, NULL, &argv, NULL);
			g_free (command);
352

353
			gsearch->is_locate_database_check_finished = FALSE;
354 355
			locate_command_default_options = g_strdup ("");

356
			/* run locate command asynchronously because on some systems it can be slow */
357 358 359
			if (g_spawn_async_with_pipes (g_get_home_dir (), argv, NULL,
					       G_SPAWN_SEARCH_PATH, NULL, NULL,
					       &locate_database_check_command_pid, NULL, &child_stdout,
360 361 362 363 364 365
					       NULL, NULL)) {

				ioc_stdout = g_io_channel_unix_new (child_stdout);
				g_io_channel_set_encoding (ioc_stdout, NULL, NULL);
				g_io_channel_set_flags (ioc_stdout, G_IO_FLAG_NONBLOCK, NULL);
				g_io_add_watch (ioc_stdout, G_IO_IN | G_IO_HUP,
366
						handle_locate_command_stdout_io, gsearch);
367
				g_io_channel_unref (ioc_stdout);
368 369
			}
			else {
370 371
				gsearch->is_locate_database_check_finished = TRUE;
			}
372

373 374
			g_strfreev (argv);

375
			while (gsearch->is_locate_database_check_finished == FALSE) {
376 377
				if (gtk_events_pending ()) {
					while (gtk_events_pending ()) {
378
						gtk_main_iteration ();
379 380 381
					}
				}
			}
382

383
			if (gsearch->is_locate_database_available == FALSE) {
384 385
				g_warning (_("A locate database has probably not been created."));
			}
386
		}
387 388
	}
	else {
389
		/* locate is not installed */
390
		locate_command_default_options = g_strdup ("");
391
		gsearch->is_locate_database_available = FALSE;
392
	}
393
	g_free (grep_cmd);
394
	g_free (locate);
395
}
396

397 398 399 400 401 402 403 404 405 406 407 408
static gchar *
setup_find_name_options (gchar * file)
{
	/* This function builds the name options for the find command.  This in
	   done to insure that the find command returns hidden files and folders. */

	GString * command;
	command = g_string_new ("");

	if (strstr (file, "*") == NULL) {

		if ((strlen (file) == 0) || (file[0] != '.')) {
409
	 		g_string_append_printf (command, "\\( %s \"*%s*\" -o %s \".*%s*\" \\) ",
410 411 412 413
					find_command_default_name_argument, file,
					find_command_default_name_argument, file);
		}
		else {
414
			g_string_append_printf (command, "\\( %s \"*%s*\" -o %s \".*%s*\" -o %s \"%s*\" \\) ",
415 416 417 418 419 420 421
					find_command_default_name_argument, file,
					find_command_default_name_argument, file,
					find_command_default_name_argument, file);
		}
	}
	else {
		if (file[0] == '.') {
422
			g_string_append_printf (command, "\\( %s \"%s\" -o %s \".*%s\" \\) ",
423
					find_command_default_name_argument, file,
424
					find_command_default_name_argument, file);
425 426
		}
		else if (file[0] != '*') {
427
			g_string_append_printf (command, "%s \"%s\" ",
428 429 430 431
					find_command_default_name_argument, file);
		}
		else {
			if ((strlen (file) >= 1) && (file[1] == '.')) {
432
				g_string_append_printf (command, "\\( %s \"%s\" -o %s \"%s\" \\) ",
433 434 435 436
					find_command_default_name_argument, file,
					find_command_default_name_argument, &file[1]);
			}
			else {
437
				g_string_append_printf (command, "\\( %s \"%s\" -o %s \".%s\" \\) ",
438 439 440 441 442 443 444 445 446 447 448 449
					find_command_default_name_argument, file,
					find_command_default_name_argument, file);
			}
		}
	}
	return g_string_free (command, FALSE);
}

static gboolean
has_additional_constraints (GSearchWindow * gsearch)
{
	GList * list;
450

451
	if (gsearch->available_options_selected_list != NULL) {
452

453
		for (list = gsearch->available_options_selected_list; list != NULL; list = g_list_next (list)) {
454

455
			GSearchConstraint * constraint = list->data;
456

457 458 459
			switch (GSearchOptionTemplates[constraint->constraint_id].type) {
			case SEARCH_CONSTRAINT_TYPE_BOOLEAN:
			case SEARCH_CONSTRAINT_TYPE_NUMERIC:
460
			case SEARCH_CONSTRAINT_TYPE_DATE_BEFORE:
461 462 463 464 465
			case SEARCH_CONSTRAINT_TYPE_DATE_AFTER:
				return TRUE;
			case SEARCH_CONSTRAINT_TYPE_TEXT:
				if (strlen (constraint->data.text) > 0) {
					return TRUE;
466
				}
467 468 469 470 471 472 473 474
			default:
				break;
			}
		}
	}
	return FALSE;
}

475 476
static void
display_dialog_character_set_conversion_error (GtkWidget * window,
477
                                               gchar * string,
478 479 480 481 482 483 484 485 486 487 488 489
                                               GError * error)
{
	GtkWidget * dialog;

	dialog = gtk_message_dialog_new (GTK_WINDOW (window),
 	                                 GTK_DIALOG_DESTROY_WITH_PARENT,
	                                 GTK_MESSAGE_ERROR,
	                                 GTK_BUTTONS_OK,
	                                 _("Character set conversion failed for \"%s\""),
					 string);

	gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
490
	                                          (error == NULL) ? " " : error->message, NULL);
491 492 493 494

	g_signal_connect (G_OBJECT (dialog),
	                  "response",
	                   G_CALLBACK (gtk_widget_destroy), NULL);
495

496 497 498
	gtk_widget_show (dialog);
}

499 500 501 502
static void
start_animation (GSearchWindow * gsearch, gboolean first_pass)
{
	if (first_pass == TRUE) {
503

504
		gchar *title = NULL;
505

506 507
		title = g_strconcat (_("Searching..."), " - ", _("Search for Files"), NULL);
		gtk_window_set_title (GTK_WINDOW (gsearch->window), title);
508

509 510
		gtk_label_set_text (GTK_LABEL (gsearch->files_found_label), "");
		if (gsearchtool_gconf_get_boolean ("/desktop/gnome/interface/enable_animations")) {
511
			gtk_spinner_start (GTK_SPINNER (gsearch->progress_spinner));
512
			gtk_widget_show (gsearch->progress_spinner);
513 514
		}
		g_free (title);
515

516
		gsearch->focus = gtk_window_get_focus (GTK_WINDOW (gsearch->window));
517

518 519 520 521 522 523 524 525 526 527 528
		gtk_window_set_default (GTK_WINDOW (gsearch->window), gsearch->stop_button);
		gtk_widget_show (gsearch->stop_button);
		gtk_widget_set_sensitive (gsearch->stop_button, TRUE);
		gtk_widget_hide (gsearch->find_button);
		gtk_widget_set_sensitive (gsearch->find_button, FALSE);
		gtk_widget_set_sensitive (gsearch->search_results_vbox, TRUE);
		gtk_widget_set_sensitive (GTK_WIDGET (gsearch->search_results_tree_view), TRUE);
		gtk_widget_set_sensitive (gsearch->available_options_vbox, FALSE);
		gtk_widget_set_sensitive (gsearch->show_more_options_expander, FALSE);
		gtk_widget_set_sensitive (gsearch->name_and_folder_table, FALSE);
	}
529
}
530 531 532 533

static void
stop_animation (GSearchWindow * gsearch)
{
534
	gtk_spinner_stop (GTK_SPINNER (gsearch->progress_spinner));
535 536 537 538 539 540

	gtk_window_set_default (GTK_WINDOW (gsearch->window), gsearch->find_button);
	gtk_widget_set_sensitive (gsearch->available_options_vbox, TRUE);
	gtk_widget_set_sensitive (gsearch->show_more_options_expander, TRUE);
	gtk_widget_set_sensitive (gsearch->name_and_folder_table, TRUE);
	gtk_widget_set_sensitive (gsearch->find_button, TRUE);
541
	gtk_widget_hide (gsearch->progress_spinner);
542
	gtk_widget_hide (gsearch->stop_button);
543
	gtk_widget_show (gsearch->find_button);
544

545 546 547 548
	if (GTK_IS_MENU_ITEM (gsearch->search_results_save_results_as_item) == TRUE) {
		gtk_widget_set_sensitive (gsearch->search_results_save_results_as_item, TRUE);
	}

549 550 551
	if (gtk_window_get_focus (GTK_WINDOW (gsearch->window)) == NULL) {
		gtk_window_set_focus (GTK_WINDOW (gsearch->window), gsearch->focus);
	}
552
}
553

554 555 556 557 558
gchar *
build_search_command (GSearchWindow * gsearch,
                      gboolean first_pass)
{
	GString * command;
559
	GError * error = NULL;
560 561 562 563 564
	gchar * file_is_named_utf8;
	gchar * file_is_named_locale;
	gchar * file_is_named_escaped;
	gchar * file_is_named_backslashed;
	gchar * look_in_folder_locale;
565 566
	gchar * look_in_folder_escaped;
	gchar * look_in_folder_backslashed;
567

568
	start_animation (gsearch, first_pass);
569
	setup_case_insensitive_arguments (gsearch);
570

571 572
	file_is_named_utf8 = g_strdup ((gchar *) gtk_entry_get_text (GTK_ENTRY (gsearch_history_entry_get_entry
	                                         (GSEARCH_HISTORY_ENTRY (gsearch->name_contains_entry)))));
573 574 575 576

	if (!file_is_named_utf8 || !*file_is_named_utf8) {
		g_free (file_is_named_utf8);
		file_is_named_utf8 = g_strdup ("*");
577
	}
578 579 580
	else {
		gchar * locale;

581 582
		locale = g_locale_from_utf8 (file_is_named_utf8, -1, NULL, NULL, &error);
		if (locale == NULL) {
583
			stop_animation (gsearch);
584 585 586 587 588
			display_dialog_character_set_conversion_error (gsearch->window, file_is_named_utf8, error);
			g_free (file_is_named_utf8);
			g_error_free (error);
			return NULL;
		}
589
		gsearch_history_entry_prepend_text (GSEARCH_HISTORY_ENTRY (gsearch->name_contains_entry), file_is_named_utf8);
590

591
		if ((strstr (locale, "*") == NULL) && (strstr (locale, "?") == NULL)) {
592
			gchar *tmp;
593

594 595 596 597
			tmp = file_is_named_utf8;
			file_is_named_utf8 = g_strconcat ("*", file_is_named_utf8, "*", NULL);
			g_free (tmp);
		}
598

599 600
		g_free (locale);
	}
601 602 603

	file_is_named_locale = g_locale_from_utf8 (file_is_named_utf8, -1, NULL, NULL, &error);
	if (file_is_named_locale == NULL) {
604
		stop_animation (gsearch);
605 606 607 608
		display_dialog_character_set_conversion_error (gsearch->window, file_is_named_utf8, error);
		g_free (file_is_named_utf8);
		g_error_free (error);
		return NULL;
609
	}
610

611
	look_in_folder_locale = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (gsearch->look_in_folder_button));
612

613
	if (look_in_folder_locale == NULL) {
614 615 616 617 618 619 620
		/* If for some reason a path was not returned fallback to the user's home directory. */
		look_in_folder_locale = g_strdup (g_get_home_dir ());
		gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (gsearch->look_in_folder_button), look_in_folder_locale);
	}

	if (!g_str_has_suffix (look_in_folder_locale, G_DIR_SEPARATOR_S)) {
		gchar *tmp;
621

622 623 624 625
		tmp = look_in_folder_locale;
		look_in_folder_locale = g_strconcat (look_in_folder_locale, G_DIR_SEPARATOR_S, NULL);
		g_free (tmp);
	}
626
	g_free (gsearch->command_details->look_in_folder_string);
627

628 629
	look_in_folder_backslashed = backslash_backslash_characters (look_in_folder_locale);
	look_in_folder_escaped = escape_double_quotes (look_in_folder_backslashed);
630
	gsearch->command_details->look_in_folder_string = g_strdup (look_in_folder_locale);
631

632 633 634 635 636
	command = g_string_new ("");
	gsearch->command_details->is_command_show_hidden_files_enabled = FALSE;
	gsearch->command_details->name_contains_regex_string = NULL;
	gsearch->search_results_date_format_string = NULL;
	gsearch->command_details->name_contains_pattern_string = NULL;
637

638 639 640 641
	gsearch->command_details->is_command_first_pass = first_pass;
	if (gsearch->command_details->is_command_first_pass == TRUE) {
		gsearch->command_details->is_command_using_quick_mode = FALSE;
	}
642

643
	if ((gtk_widget_get_visible (gsearch->available_options_vbox) == FALSE) ||
644
	    (has_additional_constraints (gsearch) == FALSE)) {
645

646 647
		file_is_named_backslashed = backslash_backslash_characters (file_is_named_locale);
		file_is_named_escaped = escape_double_quotes (file_is_named_backslashed);
648
		gsearch->command_details->name_contains_pattern_string = g_strdup (file_is_named_utf8);
649

650
		if (gsearch->command_details->is_command_first_pass == TRUE) {
651

652
			gchar * locate;
653
			gchar * show_thumbnails_string;
654
			gboolean disable_quick_search;
655

656
			locate = g_find_program_in_path ("locate");
657
			disable_quick_search = gsearchtool_gconf_get_boolean ("/apps/gnome-search-tool/disable_quick_search");
658
			gsearch->command_details->is_command_second_pass_enabled = !gsearchtool_gconf_get_boolean ("/apps/gnome-search-tool/disable_quick_search_second_scan");
659

660 661 662 663 664 665 666
			show_thumbnails_string = gsearchtool_gconf_get_string ("/apps/nautilus/preferences/show_image_thumbnails");
			if ((show_thumbnails_string != NULL) &&
			    ((strcmp (show_thumbnails_string, "always") == 0) ||
			     (strcmp (show_thumbnails_string, "local_only") == 0))) {
			    	gsearch->show_thumbnails = TRUE;
				gsearch->show_thumbnails_file_size_limit = gsearchtool_gconf_get_int ("/apps/nautilus/preferences/thumbnail_limit");
			}
667 668 669 670
			else if (show_thumbnails_string == NULL) { /* FIXME: Nautilus 3.0 uses dconf -- for now fallback to the defaults. */
				gsearch->show_thumbnails = TRUE;
				gsearch->show_thumbnails_file_size_limit = 10485760;
			}
671 672 673 674
			else {
				gsearch->show_thumbnails = FALSE;
				gsearch->show_thumbnails_file_size_limit = 0;
			}
675

676 677 678
			if ((disable_quick_search == FALSE)
			    && (gsearch->is_locate_database_available == TRUE)
			    && (locate != NULL)
679
			    && (is_quick_search_excluded_path (look_in_folder_locale) == FALSE)) {
680

681
					g_string_append_printf (command, "%s %s \"%s*%s\"",
682 683
							locate,
							locate_command_default_options,
684
							look_in_folder_escaped,
685 686 687
							file_is_named_escaped);
					gsearch->command_details->is_command_using_quick_mode = TRUE;
			}
688
			else {
689 690
				g_string_append_printf (command, "find \"%s\" %s \"%s\" -print",
							look_in_folder_escaped,
691
							find_command_default_name_argument,
692 693 694
							file_is_named_escaped);
			}
			g_free (locate);
695
			g_free (show_thumbnails_string);
696 697
		}
		else {
698 699
			g_string_append_printf (command, "find \"%s\" %s \"%s\" -print",
						look_in_folder_escaped,
700
						find_command_default_name_argument,
701 702 703 704 705
						file_is_named_escaped);
		}
	}
	else {
		GList * list;
706
		gboolean disable_mount_argument = TRUE;
707

708
		gsearch->command_details->is_command_regex_matching_enabled = FALSE;
709 710
		file_is_named_backslashed = backslash_backslash_characters (file_is_named_locale);
		file_is_named_escaped = escape_double_quotes (file_is_named_backslashed);
711 712

		g_string_append_printf (command, "find \"%s\" %s",
713
					look_in_folder_escaped,
714
					setup_find_name_options (file_is_named_escaped));
715

716
		for (list = gsearch->available_options_selected_list; list != NULL; list = g_list_next (list)) {
717

718
			GSearchConstraint * constraint = list->data;
719

720 721
			switch (GSearchOptionTemplates[constraint->constraint_id].type) {
			case SEARCH_CONSTRAINT_TYPE_BOOLEAN:
722 723
				if (strcmp (GSearchOptionTemplates[constraint->constraint_id].option, "EXCLUDE_OTHER_FILESYSTEMS") == 0) {
					disable_mount_argument = FALSE;
724 725 726 727 728 729 730 731 732 733 734
				}
				else if (strcmp (GSearchOptionTemplates[constraint->constraint_id].option, "SHOW_HIDDEN_FILES") == 0) {
					gsearch->command_details->is_command_show_hidden_files_enabled = TRUE;
				}
				else {
					g_string_append_printf (command, "%s ",
						GSearchOptionTemplates[constraint->constraint_id].option);
				}
				break;
			case SEARCH_CONSTRAINT_TYPE_TEXT:
				if (strcmp (GSearchOptionTemplates[constraint->constraint_id].option, "-regex '%s'") == 0) {
735

736 737
					gchar * escaped;
					gchar * regex;
738

739 740
					escaped = backslash_special_characters (constraint->data.text);
					regex = escape_single_quotes (escaped);
741 742

					if (regex != NULL) {
743 744 745
						gsearch->command_details->is_command_regex_matching_enabled = TRUE;
						gsearch->command_details->name_contains_regex_string = g_locale_from_utf8 (regex, -1, NULL, NULL, NULL);
					}
746

747 748
					g_free (escaped);
					g_free (regex);
749
				}
750 751 752 753
				else {
					gchar * escaped;
					gchar * backslashed;
					gchar * locale;
754

755 756
					backslashed = backslash_special_characters (constraint->data.text);
					escaped = escape_single_quotes (backslashed);
757

758
					locale = g_locale_from_utf8 (escaped, -1, NULL, NULL, NULL);
759 760

					if (strlen (locale) != 0) {
761 762 763 764 765 766
						g_string_append_printf (command,
							  	        GSearchOptionTemplates[constraint->constraint_id].option,
						  		        locale);

						g_string_append_c (command, ' ');
					}
767

768 769
					g_free (escaped);
					g_free (backslashed);
770
					g_free (locale);
771 772 773 774 775 776 777 778 779 780 781 782 783 784
				}
				break;
			case SEARCH_CONSTRAINT_TYPE_NUMERIC:
				g_string_append_printf (command,
					  		GSearchOptionTemplates[constraint->constraint_id].option,
							(constraint->data.number * 1024),
					  		(constraint->data.number * 1024));
				g_string_append_c (command, ' ');
				break;
			case SEARCH_CONSTRAINT_TYPE_DATE_BEFORE:
				g_string_append_printf (command,
					 		GSearchOptionTemplates[constraint->constraint_id].option,
					  		constraint->data.time);
				g_string_append_c (command, ' ');
785
				break;
786 787 788 789 790 791 792 793 794 795 796 797
			case SEARCH_CONSTRAINT_TYPE_DATE_AFTER:
				g_string_append_printf (command,
					 		GSearchOptionTemplates[constraint->constraint_id].option,
					  		constraint->data.time,
					  		constraint->data.time);
				g_string_append_c (command, ' ');
				break;
			default:
		        	break;
			}
		}
		gsearch->command_details->name_contains_pattern_string = g_strdup ("*");
798

799 800 801
		if (disable_mount_argument != TRUE) {
			g_string_append (command, "-xdev ");
		}
802

803 804 805 806 807 808 809
		g_string_append (command, "-print ");
	}
	g_free (file_is_named_locale);
	g_free (file_is_named_utf8);
	g_free (file_is_named_backslashed);
	g_free (file_is_named_escaped);
	g_free (look_in_folder_locale);
810 811
	g_free (look_in_folder_backslashed);
	g_free (look_in_folder_escaped);
812 813 814 815 816

	return g_string_free (command, FALSE);
}

static void
817 818
add_file_to_search_results (const gchar * file,
			    GtkListStore * store,
819 820
			    GtkTreeIter * iter,
			    GSearchWindow * gsearch)
821
{
822
	GdkPixbuf * pixbuf;
823
	GSearchMonitor * monitor;
824 825 826 827 828 829 830
	GFileMonitor * handle;
	GFileInfo * file_info;
	GFile * g_file;
	GError * error = NULL;
	GTimeVal time_val;
	GtkTreePath * path;
	GtkTreeRowReference * reference;
831
	gchar * description;
832 833 834 835 836 837 838 839
	gchar * readable_size;
	gchar * readable_date;
	gchar * utf8_base_name;
	gchar * utf8_relative_dir_name;
	gchar * base_name;
	gchar * dir_name;
	gchar * relative_dir_name;
	gchar * look_in_folder;
840

841 842 843
	if (g_hash_table_lookup_extended (gsearch->search_results_filename_hash_table, file, NULL, NULL) == TRUE) {
		return;
	}
844 845

	if ((g_file_test (file, G_FILE_TEST_EXISTS) != TRUE) &&
846
	    (g_file_test (file, G_FILE_TEST_IS_SYMLINK) != TRUE)) {
847 848
		return;
	}
849

850
	g_hash_table_insert (gsearch->search_results_filename_hash_table, g_strdup (file), NULL);
851

852 853 854
	if (gtk_tree_view_get_headers_visible (GTK_TREE_VIEW (gsearch->search_results_tree_view)) == FALSE) {
		gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (gsearch->search_results_tree_view), TRUE);
	}
855 856 857
	
	g_file = g_file_new_for_path (file);
	file_info = g_file_query_info (g_file, "standard::*,time::modified,thumbnail::path", 0, NULL, NULL);
858

859 860
	pixbuf = get_file_pixbuf (gsearch, file_info);
	description = get_file_type_description (file, file_info);
861
	readable_size = g_format_size (g_file_info_get_size (file_info));
862

863 864
	g_file_info_get_modification_time (file_info, &time_val);
	readable_date = get_readable_date (gsearch->search_results_date_format_string, time_val.tv_sec); 
865

866 867
	base_name = g_path_get_basename (file);
	dir_name = g_path_get_dirname (file);
868

869 870
	look_in_folder = g_strdup (gsearch->command_details->look_in_folder_string);
	if (strlen (look_in_folder) > 1) {
871
		gchar * path_str;
872 873

		if (g_str_has_suffix (look_in_folder, G_DIR_SEPARATOR_S) == TRUE) {
874
			look_in_folder[strlen (look_in_folder) - 1] = '\0';
875
		}
876 877 878
		path_str = g_path_get_dirname (look_in_folder);
		if (strcmp (path_str, G_DIR_SEPARATOR_S) == 0) {
			relative_dir_name = g_strconcat (&dir_name[strlen (path_str)], NULL);
879
		}
880
		else {
881
			relative_dir_name = g_strconcat (&dir_name[strlen (path_str) + 1], NULL);
882
		}
883
		g_free (path_str);
884 885 886 887 888
	}
	else {
		relative_dir_name = g_strdup (dir_name);
	}

889 890
	utf8_base_name = g_filename_display_basename (file);
	utf8_relative_dir_name = g_filename_display_name (relative_dir_name);
891 892

	gtk_list_store_append (GTK_LIST_STORE (store), iter);
893
	gtk_list_store_set (GTK_LIST_STORE (store), iter,
894
			    COLUMN_ICON, pixbuf,
895 896
			    COLUMN_NAME, utf8_base_name,
			    COLUMN_RELATIVE_PATH, utf8_relative_dir_name,
897
			    COLUMN_LOCALE_FILE, file,
898
			    COLUMN_READABLE_SIZE, readable_size,
899 900
			    COLUMN_SIZE, (-1) * (gdouble) g_file_info_get_size(file_info),
			    COLUMN_TYPE, (description != NULL) ? description : g_strdup (g_file_info_get_content_type (file_info)),
901
			    COLUMN_READABLE_DATE, readable_date,
902
			    COLUMN_DATE, (-1) * (gdouble) time_val.tv_sec,
903 904 905
			    COLUMN_NO_FILES_FOUND, FALSE,
			    -1);

906
	monitor = g_slice_new0 (GSearchMonitor);
907
	if (monitor) {
908
		path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), iter);
909 910
		reference = gtk_tree_row_reference_new (GTK_TREE_MODEL (store), path);
		gtk_tree_path_free (path);
911

912 913 914
		handle = g_file_monitor_file (g_file, G_FILE_MONITOR_EVENT_DELETED, NULL, &error);

		if (error == NULL) {
915 916 917 918 919
			monitor->gsearch = gsearch;
			monitor->reference = reference;
			monitor->handle = handle;
			gtk_list_store_set (GTK_LIST_STORE (store), iter,
			                    COLUMN_MONITOR, monitor, -1);
920 921 922

			g_signal_connect (handle, "changed",
					  G_CALLBACK (file_changed_cb), monitor);
923
		}
924 925
		else {
			gtk_tree_row_reference_free (reference);
926
			g_slice_free (GSearchMonitor, monitor);
927
			g_clear_error (&error);
928 929 930
		}
	}

931 932
	g_object_unref (g_file);
	g_object_unref (file_info);
933 934 935 936 937 938
	g_free (base_name);
	g_free (dir_name);
	g_free (relative_dir_name);
	g_free (utf8_base_name);
	g_free (utf8_relative_dir_name);
	g_free (look_in_folder);
939
	g_free (description);
940 941 942 943 944 945 946 947 948 949
	g_free (readable_size);
	g_free (readable_date);
}

static void
add_no_files_found_message (GSearchWindow * gsearch)
{
	/* When the list is empty append a 'No Files Found.' message. */
	gtk_widget_set_sensitive (GTK_WIDGET (gsearch->search_results_tree_view), FALSE);
	gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (gsearch->search_results_tree_view), FALSE);
950 951 952 953
	gtk_tree_view_column_set_visible (gsearch->search_results_folder_column, FALSE);
	gtk_tree_view_column_set_visible (gsearch->search_results_size_column, FALSE);
	gtk_tree_view_column_set_visible (gsearch->search_results_type_column, FALSE);
	gtk_tree_view_column_set_visible (gsearch->search_results_date_column, FALSE);
954 955 956 957 958
	gtk_tree_view_columns_autosize (GTK_TREE_VIEW (gsearch->search_results_tree_view));
	g_object_set (gsearch->search_results_name_cell_renderer,
	              "underline", PANGO_UNDERLINE_NONE,
	              "underline-set", FALSE,
	              NULL);
959
	gtk_list_store_append (GTK_LIST_STORE (gsearch->search_results_list_store), &gsearch->search_results_iter);
960
	gtk_list_store_set (GTK_LIST_STORE (gsearch->search_results_list_store), &gsearch->search_results_iter,
961
		    	    COLUMN_ICON, NULL,
962 963
			    COLUMN_NAME, _("No files found"),
			    COLUMN_RELATIVE_PATH, "",
964
			    COLUMN_LOCALE_FILE, "",
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
			    COLUMN_READABLE_SIZE, "",
			    COLUMN_SIZE, (gdouble) 0,
			    COLUMN_TYPE, "",
		    	    COLUMN_READABLE_DATE, "",
		    	    COLUMN_DATE, (gdouble) 0,
			    COLUMN_NO_FILES_FOUND, TRUE,
		    	    -1);
}

void
update_search_counts (GSearchWindow * gsearch)
{
	gchar * title_bar_string = NULL;
	gchar * message_string = NULL;
	gchar * stopped_string = NULL;
	gchar * tmp;
	gint total_files;

	if (gsearch->command_details->command_status == ABORTED) {
		stopped_string = g_strdup (_("(stopped)"));
	}
986

987 988 989 990 991 992 993 994
	total_files = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (gsearch->search_results_list_store), NULL);

	if (total_files == 0) {
		title_bar_string = g_strdup (_("No Files Found"));
		message_string = g_strdup (_("No files found"));
		add_no_files_found_message (gsearch);
	}
	else {
995 996
		title_bar_string = g_strdup_printf (ngettext ("%'d File Found",
					                      "%'d Files Found",
997 998
					                      total_files),
						    total_files);
999 1000
		message_string = g_strdup_printf (ngettext ("%'d file found",
					                    "%'d files found",
1001 1002 1003 1004 1005 1006 1007 1008
					                    total_files),
						  total_files);
	}

	if (stopped_string != NULL) {
		tmp = message_string;
		message_string = g_strconcat (message_string, " ", stopped_string, NULL);
		g_free (tmp);
1009

1010 1011 1012 1013
		tmp = title_bar_string;
		title_bar_string = g_strconcat (title_bar_string, " ", stopped_string, NULL);
		g_free (tmp);
	}
1014

1015 1016 1017 1018
	tmp = title_bar_string;
	title_bar_string = g_strconcat (title_bar_string, " - ", _("Search for Files"), NULL);
	gtk_window_set_title (GTK_WINDOW (gsearch->window), title_bar_string);
	g_free (tmp);
1019

1020
	gtk_label_set_text (GTK_LABEL (gsearch->files_found_label), message_string);
1021

1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
	g_free (title_bar_string);
	g_free (message_string);
	g_free (stopped_string);
}

static void
intermediate_file_count_update (GSearchWindow * gsearch)
{
	gchar * string;
	gint count;
1032

1033
	count = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (gsearch->search_results_list_store), NULL);
1034

1035
	if (count > 0) {
1036

1037 1038
		string = g_strdup_printf (ngettext ("%'d file found",
		                                    "%'d files found",
1039 1040
		                                    count),
		                          count);
1041

1042 1043 1044 1045 1046
		gtk_label_set_text (GTK_LABEL (gsearch->files_found_label), string);
		g_free (string);
	}
}

1047
gboolean
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
tree_model_iter_free_monitor (GtkTreeModel * model,
                              GtkTreePath * path,
                              GtkTreeIter * iter,
                              gpointer data)
{
	GSearchMonitor * monitor;

	g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);

	gtk_tree_model_get (model, iter, COLUMN_MONITOR, &monitor, -1);
	if (monitor) {
1059
		g_file_monitor_cancel (monitor->handle);
1060
		gtk_tree_row_reference_free (monitor->reference);
1061
		g_slice_free (GSearchMonitor, monitor);
1062 1063 1064 1065
	}
	return FALSE;
}

1066 1067 1068 1069 1070
static GtkTreeModel *
gsearch_create_list_of_templates (void)
{
	GtkListStore * store;
	GtkTreeIter iter;
1071
	gint idx;
1072 1073 1074

	store = gtk_list_store_new (1, G_TYPE_STRING);

1075
	for (idx = 0; GSearchOptionTemplates[idx].type != SEARCH_CONSTRAINT_TYPE_NONE; idx++) {
1076

1077
		if (GSearchOptionTemplates[idx].type == SEARCH_CONSTRAINT_TYPE_SEPARATOR) {
1078 1079
		        gtk_list_store_append (store, &iter);
		        gtk_list_store_set (store, &iter, 0, "separator", -1);
1080
		}
1081
		else {
1082
			gchar * text = remove_mnemonic_character (_(GSearchOptionTemplates[idx].desc));
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
			gtk_list_store_append (store, &iter);
		        gtk_list_store_set (store, &iter, 0, text, -1);
			g_free (text);
		}
	}
	return GTK_TREE_MODEL (store);
}

static void
set_constraint_info_defaults (GSearchConstraint * opt)
1093
{
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
	switch (GSearchOptionTemplates[opt->constraint_id].type) {
	case SEARCH_CONSTRAINT_TYPE_BOOLEAN:
		break;
	case SEARCH_CONSTRAINT_TYPE_TEXT:
		opt->data.text = "";
		break;
	case SEARCH_CONSTRAINT_TYPE_NUMERIC:
		opt->data.number = 0;
		break;
	case SEARCH_CONSTRAINT_TYPE_DATE_BEFORE:
	case SEARCH_CONSTRAINT_TYPE_DATE_AFTER:
		opt->data.time = 0;
		break;
	default:
	        break;
	}
}

void
1113
update_constraint_info (GSearchConstraint * constraint,
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
			gchar * info)
{
	switch (GSearchOptionTemplates[constraint->constraint_id].type) {
	case SEARCH_CONSTRAINT_TYPE_TEXT:
		constraint->data.text = info;
		break;
	case SEARCH_CONSTRAINT_TYPE_NUMERIC:
		sscanf (info, "%d", &constraint->data.number);
		break;
	case SEARCH_CONSTRAINT_TYPE_DATE_BEFORE:
	case SEARCH_CONSTRAINT_TYPE_DATE_AFTER:
		sscanf (info, "%d", &constraint->data.time);
		break;
	default:
		g_warning (_("Entry changed called for a non entry option!"));
		break;
	}
}

void
set_constraint_selected_state (GSearchWindow * gsearch,
1135
                               gint constraint_id,
1136 1137
			       gboolean state)
{
1138
	gint idx;
1139

1140
	GSearchOptionTemplates[constraint_id].is_selected = state;
1141

1142 1143 1144
	for (idx = 0; GSearchOptionTemplates[idx].type != SEARCH_CONSTRAINT_TYPE_NONE; idx++) {
		if (GSearchOptionTemplates[idx].is_selected == FALSE) {
			gtk_combo_box_set_active (GTK_COMBO_BOX (gsearch->available_options_combo_box), idx);
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
			gtk_widget_set_sensitive (gsearch->available_options_add_button, TRUE);
			gtk_widget_set_sensitive (gsearch->available_options_combo_box, TRUE);
			gtk_widget_set_sensitive (gsearch->available_options_label, TRUE);
			return;
		}
	}
	gtk_widget_set_sensitive (gsearch->available_options_add_button, FALSE);
	gtk_widget_set_sensitive (gsearch->available_options_combo_box, FALSE);
	gtk_widget_set_sensitive (gsearch->available_options_label, FALSE);
}

void
set_constraint_gconf_boolean (gint constraint_id,
                              gboolean flag)
{
	switch (constraint_id) {
1161

1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
		case SEARCH_CONSTRAINT_CONTAINS_THE_TEXT:
			gsearchtool_gconf_set_boolean ("/apps/gnome-search-tool/select/contains_the_text",
	   		       	       	       	       flag);
			break;
		case SEARCH_CONSTRAINT_DATE_MODIFIED_BEFORE:
			gsearchtool_gconf_set_boolean ("/apps/gnome-search-tool/select/date_modified_less_than",
	   		       	       	       	       flag);
			break;
		case SEARCH_CONSTRAINT_DATE_MODIFIED_AFTER:
			gsearchtool_gconf_set_boolean ("/apps/gnome-search-tool/select/date_modified_more_than",
	   		       	       	       	       flag);
			break;
		case SEARCH_CONSTRAINT_SIZE_IS_MORE_THAN:
			gsearchtool_gconf_set_boolean ("/apps/gnome-search-tool/select/size_at_least",
	   		       	       		       flag);
			break;
		case SEARCH_CONSTRAINT_SIZE_IS_LESS_THAN:
			gsearchtool_gconf_set_boolean ("/apps/gnome-search-tool/select/size_at_most",
	   		       	       	  	       flag);
			break;
		case SEARCH_CONSTRAINT_FILE_IS_EMPTY:
			gsearchtool_gconf_set_boolean ("/apps/gnome-search-tool/select/file_is_empty",
	   		       	       	               flag);
			break;
		case SEARCH_CONSTRAINT_OWNED_BY_USER:
			gsearchtool_gconf_set_boolean ("/apps/gnome-search-tool/select/owned_by_user",
	   		       	       	               flag);
			break;
		case SEARCH_CONSTRAINT_OWNED_BY_GROUP:
			gsearchtool_gconf_set_boolean ("/apps/gnome-search-tool/select/owned_by_group",
	   		       	       	               flag);
			break;
		case SEARCH_CONSTRAINT_OWNER_IS_UNRECOGNIZED:
			gsearchtool_gconf_set_boolean ("/apps/gnome-search-tool/select/owner_is_unrecognized",
	   		       	       	               flag);
			break;
		case SEARCH_CONSTRAINT_FILE_IS_NOT_NAMED:
			gsearchtool_gconf_set_boolean ("/apps/gnome-search-tool/select/name_does_not_contain",
	   		       	       	               flag);
			break;
		case SEARCH_CONSTRAINT_FILE_MATCHES_REGULAR_EXPRESSION:
			gsearchtool_gconf_set_boolean ("/apps/gnome-search-tool/select/name_matches_regular_expression",
	   		       	       	               flag);
			break;
		case SEARCH_CONSTRAINT_SHOW_HIDDEN_FILES_AND_FOLDERS:
			gsearchtool_gconf_set_boolean ("/apps/gnome-search-tool/select/show_hidden_files_and_folders",
	   		       	       	               flag);
			break;
		case SEARCH_CONSTRAINT_FOLLOW_SYMBOLIC_LINKS:
			gsearchtool_gconf_set_boolean ("/apps/gnome-search-tool/select/follow_symbolic_links",
	   		       	       	               flag);
			break;
		case SEARCH_CONSTRAINT_SEARCH_OTHER_FILESYSTEMS:
Dennis Cranston's avatar
Dennis Cranston committed
1215
			gsearchtool_gconf_set_boolean ("/apps/gnome-search-tool/select/exclude_other_filesystems",
1216 1217
	   		       	       	               flag);
			break;
1218

1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
		default:
			break;
	}
}

/*
 * add_atk_namedesc
 * @widget    : The Gtk Widget for which @name and @desc are added.
 * @name      : Accessible Name
 * @desc      : Accessible Description
 * Description: This function adds accessible name and description to a
 *              Gtk widget.
 */

static void
1234 1235
add_atk_namedesc (GtkWidget * widget,
		  const gchar * name,
1236 1237 1238 1239
		  const gchar * desc)
{
	AtkObject * atk_widget;

1240
	g_assert (GTK_IS_WIDGET (widget));
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259

	atk_widget = gtk_widget_get_accessible (widget);

	if (name != NULL)
		atk_object_set_name (atk_widget, name);
	if (desc !=NULL)
		atk_object_set_description (atk_widget, desc);
}

/*
 * add_atk_relation
 * @obj1      : The first widget in the relation @rel_type
 * @obj2      : The second widget in the relation @rel_type.
 * @rel_type  : Relation type which relates @obj1 and @obj2
 * Description: This function establishes Atk Relation between two given
 *              objects.
 */

static void
1260 1261
add_atk_relation (GtkWidget * obj1,
		  GtkWidget * obj2,
1262 1263 1264 1265 1266 1267
		  AtkRelationType rel_type)
{
	AtkObject * atk_obj1, * atk_obj2;
	AtkRelationSet * relation_set;
	AtkRelation * relation;

1268 1269
	g_assert (GTK_IS_WIDGET (obj1));
	g_assert (GTK_IS_WIDGET (obj2));
1270

1271
	atk_obj1 = gtk_widget_get_accessible (obj1);
1272

1273
	atk_obj2 = gtk_widget_get_accessible (obj2);
1274

1275 1276 1277 1278
	relation_set = atk_object_ref_relation_set (atk_obj1);
	relation = atk_relation_new (&atk_obj2, 1, rel_type);
	atk_relation_set_add (relation_set, relation);
	g_object_unref (G_OBJECT (relation));
1279

1280 1281 1282
}

static void
1283
gsearch_setup_goption_descriptions (void)
1284
{
1285
	gint i = 1;
1286 1287
	gint j;

1288 1289 1290 1291 1292
	GSearchGOptionEntries[i++].description = g_strdup (_("Set the text of \"Name contains\" search option"));
	GSearchGOptionEntries[i++].description = g_strdup (_("Set the text of \"Look in folder\" search option"));
  	GSearchGOptionEntries[i++].description = g_strdup (_("Sort files by one of the following: name, folder, size, type, or date"));
  	GSearchGOptionEntries[i++].description = g_strdup (_("Set sort order to descending, the default is ascending"));
  	GSearchGOptionEntries[i++].description = g_strdup (_("Automatically start a search"));
1293 1294 1295

	for (j = 0; GSearchOptionTemplates[j].type != SEARCH_CONSTRAINT_TYPE_NONE; j++) {
		if (GSearchOptionTemplates[j].type != SEARCH_CONSTRAINT_TYPE_SEPARATOR) {
1296
			gchar *text = remove_mnemonic_character (_(GSearchOptionTemplates[j].desc));