No issues found
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
2
3 nautilus-search-directory-file.c: Subclass of NautilusFile to help implement the
4 searches
5
6 Copyright (C) 2005 Novell, Inc.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public
19 License along with this program; if not, write to the
20 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
22
23 Author: Anders Carlsson <andersca@imendio.com>
24 */
25
26 #include <config.h>
27 #include "nautilus-search-directory-file.h"
28
29 #include "nautilus-directory-notify.h"
30 #include "nautilus-directory-private.h"
31 #include "nautilus-file-attributes.h"
32 #include "nautilus-file-private.h"
33 #include "nautilus-file-utilities.h"
34 #include <eel/eel-glib-extensions.h>
35 #include "nautilus-search-directory.h"
36 #include <gtk/gtk.h>
37 #include <glib/gi18n.h>
38 #include <string.h>
39
40 struct NautilusSearchDirectoryFileDetails {
41 NautilusSearchDirectory *search_directory;
42 };
43
44 G_DEFINE_TYPE(NautilusSearchDirectoryFile, nautilus_search_directory_file, NAUTILUS_TYPE_FILE);
45
46
47 static void
48 search_directory_file_monitor_add (NautilusFile *file,
49 gconstpointer client,
50 NautilusFileAttributes attributes)
51 {
52 /* No need for monitoring, we always emit changed when files
53 are added/removed, and no other metadata changes */
54
55 /* Update display name, in case this didn't happen yet */
56 nautilus_search_directory_file_update_display_name (NAUTILUS_SEARCH_DIRECTORY_FILE (file));
57 }
58
59 static void
60 search_directory_file_monitor_remove (NautilusFile *file,
61 gconstpointer client)
62 {
63 /* Do nothing here, we don't have any monitors */
64 }
65
66 static void
67 search_directory_file_call_when_ready (NautilusFile *file,
68 NautilusFileAttributes file_attributes,
69 NautilusFileCallback callback,
70 gpointer callback_data)
71
72 {
73 /* Update display name, in case this didn't happen yet */
74 nautilus_search_directory_file_update_display_name (NAUTILUS_SEARCH_DIRECTORY_FILE (file));
75
76 /* All data for directory-as-file is always uptodate */
77 (* callback) (file, callback_data);
78 }
79
80 static void
81 search_directory_file_cancel_call_when_ready (NautilusFile *file,
82 NautilusFileCallback callback,
83 gpointer callback_data)
84 {
85 /* Do nothing here, we don't have any pending calls */
86 }
87
88 static gboolean
89 search_directory_file_check_if_ready (NautilusFile *file,
90 NautilusFileAttributes attributes)
91 {
92 return TRUE;
93 }
94
95 static gboolean
96 search_directory_file_get_item_count (NautilusFile *file,
97 guint *count,
98 gboolean *count_unreadable)
99 {
100 GList *file_list;
101
102 if (count) {
103 file_list = nautilus_directory_get_file_list (file->details->directory);
104
105 *count = g_list_length (file_list);
106
107 nautilus_file_list_free (file_list);
108 }
109
110 return TRUE;
111 }
112
113 static NautilusRequestStatus
114 search_directory_file_get_deep_counts (NautilusFile *file,
115 guint *directory_count,
116 guint *file_count,
117 guint *unreadable_directory_count,
118 goffset *total_size)
119 {
120 NautilusFile *dir_file;
121 GList *file_list, *l;
122 guint dirs, files;
123 GFileType type;
124
125 file_list = nautilus_directory_get_file_list (file->details->directory);
126
127 dirs = files = 0;
128 for (l = file_list; l != NULL; l = l->next) {
129 dir_file = NAUTILUS_FILE (l->data);
130 type = nautilus_file_get_file_type (dir_file);
131 if (type == G_FILE_TYPE_DIRECTORY) {
132 dirs++;
133 } else {
134 files++;
135 }
136 }
137
138 if (directory_count != NULL) {
139 *directory_count = dirs;
140 }
141 if (file_count != NULL) {
142 *file_count = files;
143 }
144 if (unreadable_directory_count != NULL) {
145 *unreadable_directory_count = 0;
146 }
147 if (total_size != NULL) {
148 /* FIXME: Maybe we want to calculate this? */
149 *total_size = 0;
150 }
151
152 nautilus_file_list_free (file_list);
153
154 return NAUTILUS_REQUEST_DONE;
155 }
156
157 static char *
158 search_directory_file_get_where_string (NautilusFile *file)
159 {
160 return g_strdup (_("Search"));
161 }
162
163 void
164 nautilus_search_directory_file_update_display_name (NautilusSearchDirectoryFile *search_file)
165 {
166 NautilusFile *file;
167 NautilusSearchDirectory *search_dir;
168 NautilusQuery *query;
169 char *display_name;
170 gboolean changed;
171
172
173 display_name = NULL;
174 file = NAUTILUS_FILE (search_file);
175 if (file->details->directory) {
176 search_dir = NAUTILUS_SEARCH_DIRECTORY (file->details->directory);
177 query = nautilus_search_directory_get_query (search_dir);
178
179 if (query != NULL) {
180 display_name = nautilus_query_to_readable_string (query);
181 g_object_unref (query);
182 }
183 }
184
185 if (display_name == NULL) {
186 display_name = g_strdup (_("Search"));
187 }
188
189 changed = nautilus_file_set_display_name (file, display_name, NULL, TRUE);
190 if (changed) {
191 nautilus_file_emit_changed (file);
192 }
193
194 g_free (display_name);
195 }
196
197 static void
198 nautilus_search_directory_file_init (NautilusSearchDirectoryFile *search_file)
199 {
200 NautilusFile *file;
201
202 file = NAUTILUS_FILE (search_file);
203
204 file->details->got_file_info = TRUE;
205 file->details->mime_type = eel_ref_str_get_unique ("x-directory/normal");
206 file->details->type = G_FILE_TYPE_DIRECTORY;
207 file->details->size = 0;
208
209 file->details->file_info_is_up_to_date = TRUE;
210
211 file->details->custom_icon = NULL;
212 file->details->activation_uri = NULL;
213 file->details->got_link_info = TRUE;
214 file->details->link_info_is_up_to_date = TRUE;
215
216 file->details->directory_count = 0;
217 file->details->got_directory_count = TRUE;
218 file->details->directory_count_is_up_to_date = TRUE;
219
220 nautilus_file_set_display_name (file, _("Search"), NULL, TRUE);
221 }
222
223 static void
224 nautilus_search_directory_file_class_init (NautilusSearchDirectoryFileClass *klass)
225 {
226 NautilusFileClass *file_class;
227
228 file_class = NAUTILUS_FILE_CLASS (klass);
229
230 file_class->default_file_type = G_FILE_TYPE_DIRECTORY;
231
232 file_class->monitor_add = search_directory_file_monitor_add;
233 file_class->monitor_remove = search_directory_file_monitor_remove;
234 file_class->call_when_ready = search_directory_file_call_when_ready;
235 file_class->cancel_call_when_ready = search_directory_file_cancel_call_when_ready;
236 file_class->check_if_ready = search_directory_file_check_if_ready;
237 file_class->get_item_count = search_directory_file_get_item_count;
238 file_class->get_deep_counts = search_directory_file_get_deep_counts;
239 file_class->get_where_string = search_directory_file_get_where_string;
240 }