2008-05-05 Paolo Borelli <pborelli@katamail.com>
[nautilus.git] / libnautilus-private / nautilus-search-directory-file.c
blob598d5af565fc1f52763ec2fd6b24d481f66e7b8e
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
3 nautilus-search-directory-file.c: Subclass of NautilusFile to help implement the
4 searches
6 Copyright (C) 2005 Novell, Inc.
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.
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.
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.
23 Author: Anders Carlsson <andersca@imendio.com>
26 #include <config.h>
27 #include "nautilus-search-directory-file.h"
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/gtksignal.h>
37 #include <glib/gi18n.h>
38 #include <string.h>
40 struct NautilusSearchDirectoryFileDetails {
41 NautilusSearchDirectory *search_directory;
44 G_DEFINE_TYPE(NautilusSearchDirectoryFile, nautilus_search_directory_file, NAUTILUS_TYPE_FILE);
47 static void
48 search_directory_file_monitor_add (NautilusFile *file,
49 gconstpointer client,
50 NautilusFileAttributes attributes)
52 /* No need for monitoring, we always emit changed when files
53 are added/removed, and no other metadata changes */
55 /* Update display name, in case this didn't happen yet */
56 nautilus_search_directory_file_update_display_name (NAUTILUS_SEARCH_DIRECTORY_FILE (file));
59 static void
60 search_directory_file_monitor_remove (NautilusFile *file,
61 gconstpointer client)
63 /* Do nothing here, we don't have any monitors */
66 static void
67 search_directory_file_call_when_ready (NautilusFile *file,
68 NautilusFileAttributes file_attributes,
69 NautilusFileCallback callback,
70 gpointer callback_data)
73 /* Update display name, in case this didn't happen yet */
74 nautilus_search_directory_file_update_display_name (NAUTILUS_SEARCH_DIRECTORY_FILE (file));
76 /* All data for directory-as-file is always uptodate */
77 (* callback) (file, callback_data);
80 static void
81 search_directory_file_cancel_call_when_ready (NautilusFile *file,
82 NautilusFileCallback callback,
83 gpointer callback_data)
85 /* Do nothing here, we don't have any pending calls */
88 static gboolean
89 search_directory_file_check_if_ready (NautilusFile *file,
90 NautilusFileAttributes attributes)
92 return TRUE;
95 static gboolean
96 search_directory_file_get_item_count (NautilusFile *file,
97 guint *count,
98 gboolean *count_unreadable)
100 NautilusSearchDirectory *search_dir;
101 GList *file_list;
103 if (count) {
104 search_dir = NAUTILUS_SEARCH_DIRECTORY (file->details->directory);
106 file_list = nautilus_directory_get_file_list (file->details->directory);
108 *count = g_list_length (file_list);
110 nautilus_file_list_free (file_list);
113 return TRUE;
116 static NautilusRequestStatus
117 search_directory_file_get_deep_counts (NautilusFile *file,
118 guint *directory_count,
119 guint *file_count,
120 guint *unreadable_directory_count,
121 goffset *total_size)
123 NautilusSearchDirectory *search_dir;
124 NautilusFile *dir_file;
125 GList *file_list, *l;
126 guint dirs, files;
127 GFileType type;
129 search_dir = NAUTILUS_SEARCH_DIRECTORY (file->details->directory);
131 file_list = nautilus_directory_get_file_list (file->details->directory);
133 dirs = files = 0;
134 for (l = file_list; l != NULL; l = l->next) {
135 dir_file = NAUTILUS_FILE (l->data);
136 type = nautilus_file_get_file_type (dir_file);
137 if (type == G_FILE_TYPE_DIRECTORY) {
138 dirs++;
139 } else {
140 files++;
144 if (directory_count != NULL) {
145 *directory_count = dirs;
147 if (file_count != NULL) {
148 *file_count = files;
150 if (unreadable_directory_count != NULL) {
151 *unreadable_directory_count = 0;
153 if (total_size != NULL) {
154 /* FIXME: Maybe we want to calculate this? */
155 *total_size = 0;
158 nautilus_file_list_free (file_list);
160 return NAUTILUS_REQUEST_DONE;
163 static char *
164 search_directory_file_get_where_string (NautilusFile *file)
166 return g_strdup (_("Search"));
169 void
170 nautilus_search_directory_file_update_display_name (NautilusSearchDirectoryFile *search_file)
172 NautilusFile *file;
173 NautilusSearchDirectory *search_dir;
174 NautilusQuery *query;
175 char *display_name;
176 gboolean changed;
179 display_name = NULL;
180 file = NAUTILUS_FILE (search_file);
181 if (file->details->directory) {
182 search_dir = NAUTILUS_SEARCH_DIRECTORY (file->details->directory);
183 query = nautilus_search_directory_get_query (search_dir);
185 if (query != NULL) {
186 display_name = nautilus_query_to_readable_string (query);
187 g_object_unref (query);
191 if (display_name == NULL) {
192 display_name = g_strdup (_("Search"));
195 changed = nautilus_file_set_display_name (file, display_name, NULL, FALSE);
196 if (changed) {
197 nautilus_file_emit_changed (file);
201 static void
202 nautilus_search_directory_file_init (NautilusSearchDirectoryFile *search_file)
204 NautilusFile *file;
206 file = NAUTILUS_FILE (search_file);
208 file->details->got_file_info = TRUE;
209 file->details->mime_type = eel_ref_str_get_unique ("x-directory/normal");
210 file->details->type = G_FILE_TYPE_DIRECTORY;
211 file->details->size = 0;
213 file->details->file_info_is_up_to_date = TRUE;
215 file->details->custom_icon = NULL;
216 file->details->activation_location = NULL;
217 file->details->got_link_info = TRUE;
218 file->details->link_info_is_up_to_date = TRUE;
220 file->details->directory_count = 0;
221 file->details->got_directory_count = TRUE;
222 file->details->directory_count_is_up_to_date = TRUE;
224 nautilus_file_set_display_name (file, _("Search"), NULL, FALSE);
227 static void
228 nautilus_search_directory_file_class_init (NautilusSearchDirectoryFileClass *klass)
230 GObjectClass *object_class;
231 NautilusFileClass *file_class;
233 object_class = G_OBJECT_CLASS (klass);
234 file_class = NAUTILUS_FILE_CLASS (klass);
236 file_class->default_file_type = G_FILE_TYPE_DIRECTORY;
238 file_class->monitor_add = search_directory_file_monitor_add;
239 file_class->monitor_remove = search_directory_file_monitor_remove;
240 file_class->call_when_ready = search_directory_file_call_when_ready;
241 file_class->cancel_call_when_ready = search_directory_file_cancel_call_when_ready;
242 file_class->check_if_ready = search_directory_file_check_if_ready;
243 file_class->get_item_count = search_directory_file_get_item_count;
244 file_class->get_deep_counts = search_directory_file_get_deep_counts;
245 file_class->get_where_string = search_directory_file_get_where_string;