No issues found
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /*
3 Copyright (C) 2005 Novell, Inc
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with this program; if not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19
20 Author: Anders Carlsson <andersca@imendio.com>
21 */
22
23 #include <config.h>
24 #include "nautilus-search-directory.h"
25 #include "nautilus-search-directory-file.h"
26
27 #include "nautilus-directory-private.h"
28 #include "nautilus-file.h"
29 #include "nautilus-file-private.h"
30 #include "nautilus-file-utilities.h"
31 #include "nautilus-search-provider.h"
32 #include "nautilus-search-engine.h"
33 #include "nautilus-search-engine-model.h"
34
35 #include <eel/eel-glib-extensions.h>
36 #include <gtk/gtk.h>
37 #include <gio/gio.h>
38 #include <string.h>
39 #include <sys/time.h>
40
41 struct NautilusSearchDirectoryDetails {
42 NautilusQuery *query;
43 char *saved_search_uri;
44 gboolean modified;
45
46 NautilusSearchEngine *engine;
47
48 gboolean search_running;
49 gboolean search_finished;
50
51 GList *files;
52 GHashTable *files_hash;
53
54 GList *monitor_list;
55 GList *callback_list;
56 GList *pending_callback_list;
57
58 NautilusDirectory *base_model;
59 };
60
61 typedef struct {
62 gboolean monitor_hidden_files;
63 NautilusFileAttributes monitor_attributes;
64
65 gconstpointer client;
66 } SearchMonitor;
67
68 typedef struct {
69 NautilusSearchDirectory *search_directory;
70
71 NautilusDirectoryCallback callback;
72 gpointer callback_data;
73
74 NautilusFileAttributes wait_for_attributes;
75 gboolean wait_for_file_list;
76 GList *file_list;
77 GHashTable *non_ready_hash;
78 } SearchCallback;
79
80 enum {
81 PROP_0,
82 PROP_BASE_MODEL,
83 PROP_QUERY,
84 NUM_PROPERTIES
85 };
86
87 G_DEFINE_TYPE (NautilusSearchDirectory, nautilus_search_directory,
88 NAUTILUS_TYPE_DIRECTORY);
89
90 static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
91
92 static void search_engine_hits_added (NautilusSearchEngine *engine, GList *hits, NautilusSearchDirectory *search);
93 static void search_engine_finished (NautilusSearchEngine *engine, NautilusSearchDirectory *search);
94 static void search_engine_error (NautilusSearchEngine *engine, const char *error, NautilusSearchDirectory *search);
95 static void search_callback_file_ready_callback (NautilusFile *file, gpointer data);
96 static void file_changed (NautilusFile *file, NautilusSearchDirectory *search);
97
98 static void
99 reset_file_list (NautilusSearchDirectory *search)
100 {
101 GList *list, *monitor_list;
102 NautilusFile *file;
103 SearchMonitor *monitor;
104
105 /* Remove file connections */
106 for (list = search->details->files; list != NULL; list = list->next) {
107 file = list->data;
108
109 /* Disconnect change handler */
110 g_signal_handlers_disconnect_by_func (file, file_changed, search);
111
112 /* Remove monitors */
113 for (monitor_list = search->details->monitor_list; monitor_list;
114 monitor_list = monitor_list->next) {
115 monitor = monitor_list->data;
116 nautilus_file_monitor_remove (file, monitor);
117 }
118 }
119
120 nautilus_file_list_free (search->details->files);
121 search->details->files = NULL;
122
123 g_hash_table_remove_all (search->details->files_hash);
124 }
125
126 static void
127 set_hidden_files (NautilusSearchDirectory *search)
128 {
129 GList *l;
130 SearchMonitor *monitor;
131 gboolean monitor_hidden = FALSE;
132
133 for (l = search->details->monitor_list; l != NULL; l = l->next) {
134 monitor = l->data;
135 monitor_hidden |= monitor->monitor_hidden_files;
136
137 if (monitor_hidden) {
138 break;
139 }
140 }
141
142 nautilus_query_set_show_hidden_files (search->details->query, monitor_hidden);
143 }
144
145 static void
146 start_search (NautilusSearchDirectory *search)
147 {
148 NautilusSearchEngineModel *model_provider;
149
150 if (!search->details->query) {
151 return;
152 }
153
154 if (search->details->search_running) {
155 return;
156 }
157
158 if (!search->details->monitor_list && !search->details->pending_callback_list) {
159 return;
160 }
161
162 /* We need to start the search engine */
163 search->details->search_running = TRUE;
164 search->details->search_finished = FALSE;
165
166 set_hidden_files (search);
167 nautilus_search_provider_set_query (NAUTILUS_SEARCH_PROVIDER (search->details->engine),
168 search->details->query);
169
170 model_provider = nautilus_search_engine_get_model_provider (search->details->engine);
171 nautilus_search_engine_model_set_model (model_provider, search->details->base_model);
172
173 reset_file_list (search);
174
175 nautilus_search_provider_start (NAUTILUS_SEARCH_PROVIDER (search->details->engine));
176 }
177
178 static void
179 stop_search (NautilusSearchDirectory *search)
180 {
181 if (!search->details->search_running) {
182 return;
183 }
184
185 search->details->search_running = FALSE;
186 nautilus_search_provider_stop (NAUTILUS_SEARCH_PROVIDER (search->details->engine));
187
188 reset_file_list (search);
189 }
190
191 static void
192 file_changed (NautilusFile *file, NautilusSearchDirectory *search)
193 {
194 GList list;
195
196 list.data = file;
197 list.next = NULL;
198
199 nautilus_directory_emit_files_changed (NAUTILUS_DIRECTORY (search), &list);
200 }
201
202 static void
203 search_monitor_add (NautilusDirectory *directory,
204 gconstpointer client,
205 gboolean monitor_hidden_files,
206 NautilusFileAttributes file_attributes,
207 NautilusDirectoryCallback callback,
208 gpointer callback_data)
209 {
210 GList *list;
211 SearchMonitor *monitor;
212 NautilusSearchDirectory *search;
213 NautilusFile *file;
214
215 search = NAUTILUS_SEARCH_DIRECTORY (directory);
216
217 monitor = g_new0 (SearchMonitor, 1);
218 monitor->monitor_hidden_files = monitor_hidden_files;
219 monitor->monitor_attributes = file_attributes;
220 monitor->client = client;
221
222 search->details->monitor_list = g_list_prepend (search->details->monitor_list, monitor);
223
224 if (callback != NULL) {
225 (* callback) (directory, search->details->files, callback_data);
226 }
227
228 for (list = search->details->files; list != NULL; list = list->next) {
229 file = list->data;
230
231 /* Add monitors */
232 nautilus_file_monitor_add (file, monitor, file_attributes);
233 }
234
235 start_search (search);
236 }
237
238 static void
239 search_monitor_remove_file_monitors (SearchMonitor *monitor, NautilusSearchDirectory *search)
240 {
241 GList *list;
242 NautilusFile *file;
243
244 for (list = search->details->files; list != NULL; list = list->next) {
245 file = list->data;
246
247 nautilus_file_monitor_remove (file, monitor);
248 }
249 }
250
251 static void
252 search_monitor_destroy (SearchMonitor *monitor, NautilusSearchDirectory *search)
253 {
254 search_monitor_remove_file_monitors (monitor, search);
255
256 g_free (monitor);
257 }
258
259 static void
260 search_monitor_remove (NautilusDirectory *directory,
261 gconstpointer client)
262 {
263 NautilusSearchDirectory *search;
264 SearchMonitor *monitor;
265 GList *list;
266
267 search = NAUTILUS_SEARCH_DIRECTORY (directory);
268
269 for (list = search->details->monitor_list; list != NULL; list = list->next) {
270 monitor = list->data;
271
272 if (monitor->client == client) {
273 search->details->monitor_list = g_list_delete_link (search->details->monitor_list, list);
274
275 search_monitor_destroy (monitor, search);
276
277 break;
278 }
279 }
280
281 if (!search->details->monitor_list) {
282 stop_search (search);
283 }
284 }
285
286 static void
287 cancel_call_when_ready (gpointer key, gpointer value, gpointer user_data)
288 {
289 SearchCallback *search_callback;
290 NautilusFile *file;
291
292 file = key;
293 search_callback = user_data;
294
295 nautilus_file_cancel_call_when_ready (file, search_callback_file_ready_callback,
296 search_callback);
297 }
298
299 static void
300 search_callback_destroy (SearchCallback *search_callback)
301 {
302 if (search_callback->non_ready_hash) {
303 g_hash_table_foreach (search_callback->non_ready_hash, cancel_call_when_ready, search_callback);
304 g_hash_table_destroy (search_callback->non_ready_hash);
305 }
306
307 nautilus_file_list_free (search_callback->file_list);
308
309 g_free (search_callback);
310 }
311
312 static void
313 search_callback_invoke_and_destroy (SearchCallback *search_callback)
314 {
315 search_callback->callback (NAUTILUS_DIRECTORY (search_callback->search_directory),
316 search_callback->file_list,
317 search_callback->callback_data);
318
319 search_callback->search_directory->details->callback_list =
320 g_list_remove (search_callback->search_directory->details->callback_list, search_callback);
321
322 search_callback_destroy (search_callback);
323 }
324
325 static void
326 search_callback_file_ready_callback (NautilusFile *file, gpointer data)
327 {
328 SearchCallback *search_callback = data;
329
330 g_hash_table_remove (search_callback->non_ready_hash, file);
331
332 if (g_hash_table_size (search_callback->non_ready_hash) == 0) {
333 search_callback_invoke_and_destroy (search_callback);
334 }
335 }
336
337 static void
338 search_callback_add_file_callbacks (SearchCallback *callback)
339 {
340 GList *file_list_copy, *list;
341 NautilusFile *file;
342
343 file_list_copy = g_list_copy (callback->file_list);
344
345 for (list = file_list_copy; list != NULL; list = list->next) {
346 file = list->data;
347
348 nautilus_file_call_when_ready (file,
349 callback->wait_for_attributes,
350 search_callback_file_ready_callback,
351 callback);
352 }
353 g_list_free (file_list_copy);
354 }
355
356 static SearchCallback *
357 search_callback_find (NautilusSearchDirectory *search, NautilusDirectoryCallback callback, gpointer callback_data)
358 {
359 SearchCallback *search_callback;
360 GList *list;
361
362 for (list = search->details->callback_list; list != NULL; list = list->next) {
363 search_callback = list->data;
364
365 if (search_callback->callback == callback &&
366 search_callback->callback_data == callback_data) {
367 return search_callback;
368 }
369 }
370
371 return NULL;
372 }
373
374 static SearchCallback *
375 search_callback_find_pending (NautilusSearchDirectory *search, NautilusDirectoryCallback callback, gpointer callback_data)
376 {
377 SearchCallback *search_callback;
378 GList *list;
379
380 for (list = search->details->pending_callback_list; list != NULL; list = list->next) {
381 search_callback = list->data;
382
383 if (search_callback->callback == callback &&
384 search_callback->callback_data == callback_data) {
385 return search_callback;
386 }
387 }
388
389 return NULL;
390 }
391
392 static GHashTable *
393 file_list_to_hash_table (GList *file_list)
394 {
395 GList *list;
396 GHashTable *table;
397
398 if (!file_list)
399 return NULL;
400
401 table = g_hash_table_new (NULL, NULL);
402
403 for (list = file_list; list != NULL; list = list->next) {
404 g_hash_table_insert (table, list->data, list->data);
405 }
406
407 return table;
408 }
409
410 static void
411 search_call_when_ready (NautilusDirectory *directory,
412 NautilusFileAttributes file_attributes,
413 gboolean wait_for_file_list,
414 NautilusDirectoryCallback callback,
415 gpointer callback_data)
416 {
417 NautilusSearchDirectory *search;
418 SearchCallback *search_callback;
419
420 search = NAUTILUS_SEARCH_DIRECTORY (directory);
421
422 search_callback = search_callback_find (search, callback, callback_data);
423 if (search_callback == NULL) {
424 search_callback = search_callback_find_pending (search, callback, callback_data);
425 }
426
427 if (search_callback) {
428 g_warning ("tried to add a new callback while an old one was pending");
429 return;
430 }
431
432 search_callback = g_new0 (SearchCallback, 1);
433 search_callback->search_directory = search;
434 search_callback->callback = callback;
435 search_callback->callback_data = callback_data;
436 search_callback->wait_for_attributes = file_attributes;
437 search_callback->wait_for_file_list = wait_for_file_list;
438
439 if (wait_for_file_list && !search->details->search_finished) {
440 /* Add it to the pending callback list, which will be
441 * processed when the directory has finished loading
442 */
443 search->details->pending_callback_list =
444 g_list_prepend (search->details->pending_callback_list, search_callback);
445
446 /* We might need to start the search engine */
447 start_search (search);
448 } else {
449 search_callback->file_list = nautilus_file_list_copy (search->details->files);
450 search_callback->non_ready_hash = file_list_to_hash_table (search->details->files);
451
452 if (!search_callback->non_ready_hash) {
453 /* If there are no ready files, we invoke the callback
454 with an empty list.
455 */
456 search_callback_invoke_and_destroy (search_callback);
457 } else {
458 search->details->callback_list = g_list_prepend (search->details->callback_list, search_callback);
459 search_callback_add_file_callbacks (search_callback);
460 }
461 }
462 }
463
464 static void
465 search_cancel_callback (NautilusDirectory *directory,
466 NautilusDirectoryCallback callback,
467 gpointer callback_data)
468 {
469 NautilusSearchDirectory *search;
470 SearchCallback *search_callback;
471
472 search = NAUTILUS_SEARCH_DIRECTORY (directory);
473 search_callback = search_callback_find (search, callback, callback_data);
474
475 if (search_callback) {
476 search->details->callback_list = g_list_remove (search->details->callback_list, search_callback);
477
478 search_callback_destroy (search_callback);
479
480 return;
481 }
482
483 /* Check for a pending callback */
484 search_callback = search_callback_find_pending (search, callback, callback_data);
485
486 if (search_callback) {
487 search->details->pending_callback_list = g_list_remove (search->details->pending_callback_list, search_callback);
488
489 search_callback_destroy (search_callback);
490
491 if (!search->details->pending_callback_list) {
492 stop_search (search);
493 }
494 }
495 }
496
497
498 static void
499 search_engine_hits_added (NautilusSearchEngine *engine, GList *hits,
500 NautilusSearchDirectory *search)
501 {
502 GList *hit_list;
503 GList *file_list;
504 NautilusFile *file;
505 SearchMonitor *monitor;
506 GList *monitor_list;
507
508 file_list = NULL;
509
510 for (hit_list = hits; hit_list != NULL; hit_list = hit_list->next) {
511 NautilusSearchHit *hit = hit_list->data;
512 const char *uri;
513
514 uri = nautilus_search_hit_get_uri (hit);
515 if (g_str_has_suffix (uri, NAUTILUS_SAVED_SEARCH_EXTENSION)) {
516 /* Never return saved searches themselves as hits */
517 continue;
518 }
519
520 nautilus_search_hit_compute_scores (hit, search->details->query);
521
522 file = nautilus_file_get_by_uri (uri);
523 nautilus_file_set_search_relevance (file, nautilus_search_hit_get_relevance (hit));
524
525 for (monitor_list = search->details->monitor_list; monitor_list; monitor_list = monitor_list->next) {
526 monitor = monitor_list->data;
527
528 /* Add monitors */
529 nautilus_file_monitor_add (file, monitor, monitor->monitor_attributes);
530 }
531
532 g_signal_connect (file, "changed", G_CALLBACK (file_changed), search),
533
534 file_list = g_list_prepend (file_list, file);
535 g_hash_table_add (search->details->files_hash, file);
536 }
537
538 search->details->files = g_list_concat (search->details->files, file_list);
539
540 nautilus_directory_emit_files_added (NAUTILUS_DIRECTORY (search), file_list);
541
542 file = nautilus_directory_get_corresponding_file (NAUTILUS_DIRECTORY (search));
543 nautilus_file_emit_changed (file);
544 nautilus_file_unref (file);
545 }
546
547 static void
548 search_callback_add_pending_file_callbacks (SearchCallback *callback)
549 {
550 callback->file_list = nautilus_file_list_copy (callback->search_directory->details->files);
551 callback->non_ready_hash = file_list_to_hash_table (callback->search_directory->details->files);
552
553 search_callback_add_file_callbacks (callback);
554 }
555
556 static void
557 search_engine_error (NautilusSearchEngine *engine, const char *error_message, NautilusSearchDirectory *search)
558 {
559 GError *error;
560
561 error = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_FAILED,
562 error_message);
563 nautilus_directory_emit_load_error (NAUTILUS_DIRECTORY (search),
564 error);
565 g_error_free (error);
566 }
567
568 static void
569 search_engine_finished (NautilusSearchEngine *engine, NautilusSearchDirectory *search)
570 {
571 search->details->search_finished = TRUE;
572
573 nautilus_directory_emit_done_loading (NAUTILUS_DIRECTORY (search));
574
575 /* Add all file callbacks */
576 g_list_foreach (search->details->pending_callback_list,
577 (GFunc)search_callback_add_pending_file_callbacks, NULL);
578 search->details->callback_list = g_list_concat (search->details->callback_list,
579 search->details->pending_callback_list);
580
581 g_list_free (search->details->pending_callback_list);
582 search->details->pending_callback_list = NULL;
583 }
584
585 static void
586 search_force_reload (NautilusDirectory *directory)
587 {
588 NautilusSearchDirectory *search;
589
590 search = NAUTILUS_SEARCH_DIRECTORY (directory);
591
592 if (!search->details->query) {
593 return;
594 }
595
596 search->details->search_finished = FALSE;
597
598 /* Remove file monitors */
599 reset_file_list (search);
600 stop_search (search);
601 }
602
603 static gboolean
604 search_are_all_files_seen (NautilusDirectory *directory)
605 {
606 NautilusSearchDirectory *search;
607
608 search = NAUTILUS_SEARCH_DIRECTORY (directory);
609
610 return (!search->details->query ||
611 search->details->search_finished);
612 }
613
614 static gboolean
615 search_contains_file (NautilusDirectory *directory,
616 NautilusFile *file)
617 {
618 NautilusSearchDirectory *search;
619
620 search = NAUTILUS_SEARCH_DIRECTORY (directory);
621 return (g_hash_table_lookup (search->details->files_hash, file) != NULL);
622 }
623
624 static GList *
625 search_get_file_list (NautilusDirectory *directory)
626 {
627 NautilusSearchDirectory *search;
628
629 search = NAUTILUS_SEARCH_DIRECTORY (directory);
630
631 return nautilus_file_list_copy (search->details->files);
632 }
633
634
635 static gboolean
636 search_is_editable (NautilusDirectory *directory)
637 {
638 return FALSE;
639 }
640
641 static void
642 search_set_property (GObject *object,
643 guint property_id,
644 const GValue *value,
645 GParamSpec *pspec)
646 {
647 NautilusSearchDirectory *search = NAUTILUS_SEARCH_DIRECTORY (object);
648
649 switch (property_id) {
650 case PROP_BASE_MODEL:
651 nautilus_search_directory_set_base_model (search, g_value_get_object (value));
652 break;
653 case PROP_QUERY:
654 nautilus_search_directory_set_query (search, g_value_get_object (value));
655 break;
656 default:
657 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
658 break;
659 }
660 }
661
662 static void
663 search_get_property (GObject *object,
664 guint property_id,
665 GValue *value,
666 GParamSpec *pspec)
667 {
668 NautilusSearchDirectory *search = NAUTILUS_SEARCH_DIRECTORY (object);
669
670 switch (property_id) {
671 case PROP_BASE_MODEL:
672 g_value_set_object (value, nautilus_search_directory_get_base_model (search));
673 break;
674 case PROP_QUERY:
675 g_value_take_object (value, nautilus_search_directory_get_query (search));
676 break;
677 default:
678 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
679 break;
680 }
681 }
682
683 static void
684 clear_base_model (NautilusSearchDirectory *search)
685 {
686 if (search->details->base_model != NULL) {
687 nautilus_directory_file_monitor_remove (search->details->base_model,
688 &search->details->base_model);
689 g_clear_object (&search->details->base_model);
690 }
691 }
692
693 static void
694 search_dispose (GObject *object)
695 {
696 NautilusSearchDirectory *search;
697 GList *list;
698
699 search = NAUTILUS_SEARCH_DIRECTORY (object);
700
701 clear_base_model (search);
702
703 /* Remove search monitors */
704 if (search->details->monitor_list) {
705 for (list = search->details->monitor_list; list != NULL; list = list->next) {
706 search_monitor_destroy ((SearchMonitor *)list->data, search);
707 }
708
709 g_list_free (search->details->monitor_list);
710 search->details->monitor_list = NULL;
711 }
712
713 reset_file_list (search);
714
715 if (search->details->callback_list) {
716 /* Remove callbacks */
717 g_list_foreach (search->details->callback_list,
718 (GFunc)search_callback_destroy, NULL);
719 g_list_free (search->details->callback_list);
720 search->details->callback_list = NULL;
721 }
722
723 if (search->details->pending_callback_list) {
724 g_list_foreach (search->details->pending_callback_list,
725 (GFunc)search_callback_destroy, NULL);
726 g_list_free (search->details->pending_callback_list);
727 search->details->pending_callback_list = NULL;
728 }
729
730 g_clear_object (&search->details->query);
731 stop_search (search);
732
733 g_clear_object (&search->details->engine);
734
735 G_OBJECT_CLASS (nautilus_search_directory_parent_class)->dispose (object);
736 }
737
738 static void
739 search_finalize (GObject *object)
740 {
741 NautilusSearchDirectory *search;
742
743 search = NAUTILUS_SEARCH_DIRECTORY (object);
744 g_free (search->details->saved_search_uri);
745
746 g_hash_table_destroy (search->details->files_hash);
747
748 G_OBJECT_CLASS (nautilus_search_directory_parent_class)->finalize (object);
749 }
750
751 static void
752 nautilus_search_directory_init (NautilusSearchDirectory *search)
753 {
754 search->details = G_TYPE_INSTANCE_GET_PRIVATE (search, NAUTILUS_TYPE_SEARCH_DIRECTORY,
755 NautilusSearchDirectoryDetails);
756
757 search->details->files_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
758
759 search->details->engine = nautilus_search_engine_new ();
760 g_signal_connect (search->details->engine, "hits-added",
761 G_CALLBACK (search_engine_hits_added),
762 search);
763 g_signal_connect (search->details->engine, "finished",
764 G_CALLBACK (search_engine_finished),
765 search);
766 g_signal_connect (search->details->engine, "error",
767 G_CALLBACK (search_engine_error),
768 search);
769 }
770
771 static void
772 nautilus_search_directory_class_init (NautilusSearchDirectoryClass *class)
773 {
774 NautilusDirectoryClass *directory_class = NAUTILUS_DIRECTORY_CLASS (class);
775 GObjectClass *oclass = G_OBJECT_CLASS (class);
776
777 oclass->dispose = search_dispose;
778 oclass->finalize = search_finalize;
779 oclass->get_property = search_get_property;
780 oclass->set_property = search_set_property;
781
782 directory_class->are_all_files_seen = search_are_all_files_seen;
783 directory_class->contains_file = search_contains_file;
784 directory_class->force_reload = search_force_reload;
785 directory_class->call_when_ready = search_call_when_ready;
786 directory_class->cancel_callback = search_cancel_callback;
787
788 directory_class->file_monitor_add = search_monitor_add;
789 directory_class->file_monitor_remove = search_monitor_remove;
790
791 directory_class->get_file_list = search_get_file_list;
792 directory_class->is_editable = search_is_editable;
793
794 properties[PROP_BASE_MODEL] =
795 g_param_spec_object ("base-model",
796 "The base model",
797 "The base directory model for this directory",
798 NAUTILUS_TYPE_DIRECTORY,
799 G_PARAM_READWRITE);
800 properties[PROP_QUERY] =
801 g_param_spec_object ("query",
802 "The query",
803 "The query for this search directory",
804 NAUTILUS_TYPE_QUERY,
805 G_PARAM_READWRITE);
806
807 g_object_class_install_properties (oclass, NUM_PROPERTIES, properties);
808 g_type_class_add_private (class, sizeof (NautilusSearchDirectoryDetails));
809 }
810
811 void
812 nautilus_search_directory_set_base_model (NautilusSearchDirectory *search,
813 NautilusDirectory *base_model)
814 {
815 if (search->details->base_model == base_model) {
816 return;
817 }
818
819 if (search->details->query != NULL) {
820 gchar *uri;
821 GFile *query_location, *model_location;
822 gboolean is_equal;
823
824 uri = nautilus_query_get_location (search->details->query);
825 query_location = g_file_new_for_uri (uri);
826 model_location = nautilus_directory_get_location (base_model);
827
828 is_equal = g_file_equal (model_location, query_location);
829
830 g_object_unref (model_location);
831 g_object_unref (query_location);
832 g_free (uri);
833
834 if (!is_equal) {
835 return;
836 }
837 }
838
839 clear_base_model (search);
840 search->details->base_model = nautilus_directory_ref (base_model);
841
842 if (search->details->base_model != NULL) {
843 nautilus_directory_file_monitor_add (base_model, &search->details->base_model,
844 TRUE, NAUTILUS_FILE_ATTRIBUTE_INFO,
845 NULL, NULL);
846 }
847
848 g_object_notify_by_pspec (G_OBJECT (search), properties[PROP_BASE_MODEL]);
849 }
850
851 NautilusDirectory *
852 nautilus_search_directory_get_base_model (NautilusSearchDirectory *search)
853 {
854 return search->details->base_model;
855 }
856
857 char *
858 nautilus_search_directory_generate_new_uri (void)
859 {
860 static int counter = 0;
861 char *uri;
862
863 uri = g_strdup_printf (EEL_SEARCH_URI"//%d/", counter++);
864
865 return uri;
866 }
867
868 void
869 nautilus_search_directory_set_query (NautilusSearchDirectory *search,
870 NautilusQuery *query)
871 {
872 NautilusFile *file;
873
874 if (search->details->query != query) {
875 search->details->modified = TRUE;
876
877 g_object_ref (query);
878 g_clear_object (&search->details->query);
879 search->details->query = query;
880
881 g_object_notify_by_pspec (G_OBJECT (search), properties[PROP_QUERY]);
882 }
883
884 file = nautilus_directory_get_existing_corresponding_file (NAUTILUS_DIRECTORY (search));
885 if ((file != NULL) && (search->details->saved_search_uri == NULL)) {
886 nautilus_search_directory_file_update_display_name (NAUTILUS_SEARCH_DIRECTORY_FILE (file));
887 }
888 nautilus_file_unref (file);
889 }
890
891 NautilusQuery *
892 nautilus_search_directory_get_query (NautilusSearchDirectory *search)
893 {
894 if (search->details->query != NULL) {
895 return g_object_ref (search->details->query);
896 }
897
898 return NULL;
899 }
900
901 void
902 nautilus_search_directory_set_saved_search (NautilusSearchDirectory *search,
903 GFile *saved_search)
904 {
905 NautilusQuery *query;
906 char *file;
907
908 search->details->saved_search_uri = g_file_get_uri (saved_search);
909 file = g_file_get_path (saved_search);
910
911 if (file != NULL) {
912 query = nautilus_query_load (file);
913 if (query != NULL) {
914 nautilus_search_directory_set_query (search, query);
915 g_object_unref (query);
916 }
917 g_free (file);
918 } else {
919 g_warning ("Non-local saved searches not supported");
920 }
921
922 search->details->modified = FALSE;
923 }
924
925 gboolean
926 nautilus_search_directory_is_saved_search (NautilusSearchDirectory *search)
927 {
928 return search->details->saved_search_uri != NULL;
929 }
930
931 gboolean
932 nautilus_search_directory_is_modified (NautilusSearchDirectory *search)
933 {
934 return search->details->modified;
935 }
936
937 void
938 nautilus_search_directory_save_to_file (NautilusSearchDirectory *search,
939 const char *save_file_uri)
940 {
941 char *file;
942
943 file = g_filename_from_uri (save_file_uri, NULL, NULL);
944 if (file == NULL) {
945 return;
946 }
947
948 if (search->details->query != NULL) {
949 nautilus_query_save (search->details->query, file);
950 }
951
952 g_free (file);
953 }
954
955 void
956 nautilus_search_directory_save_search (NautilusSearchDirectory *search)
957 {
958 if (search->details->saved_search_uri == NULL) {
959 return;
960 }
961
962 nautilus_search_directory_save_to_file (search,
963 search->details->saved_search_uri);
964 search->details->modified = FALSE;
965 }