1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
2 
3    nemo-search-directory-file.c: Subclass of NemoFile 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., 51 Franklin Street - Suite 500,
21    Boston, MA 02110-1335, USA.
22 
23    Author: Anders Carlsson <andersca@imendio.com>
24 */
25 
26 #include <config.h>
27 #include "nemo-search-directory-file.h"
28 
29 #include "nemo-directory-notify.h"
30 #include "nemo-directory-private.h"
31 #include "nemo-file-attributes.h"
32 #include "nemo-file-private.h"
33 #include "nemo-file-utilities.h"
34 #include <eel/eel-glib-extensions.h>
35 #include "nemo-search-directory.h"
36 #include <gtk/gtk.h>
37 #include <glib/gi18n.h>
38 #include <string.h>
39 
40 struct NemoSearchDirectoryFileDetails {
41 	NemoSearchDirectory *search_directory;
42 };
43 
44 G_DEFINE_TYPE(NemoSearchDirectoryFile, nemo_search_directory_file, NEMO_TYPE_FILE);
45 
46 
47 static void
search_directory_file_monitor_add(NemoFile * file,gconstpointer client,NemoFileAttributes attributes)48 search_directory_file_monitor_add (NemoFile *file,
49 				   gconstpointer client,
50 				   NemoFileAttributes 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 	nemo_search_directory_file_update_display_name (NEMO_SEARCH_DIRECTORY_FILE (file));
57 }
58 
59 static void
search_directory_file_monitor_remove(NemoFile * file,gconstpointer client)60 search_directory_file_monitor_remove (NemoFile *file,
61 				      gconstpointer client)
62 {
63 	/* Do nothing here, we don't have any monitors */
64 }
65 
66 static void
search_directory_file_call_when_ready(NemoFile * file,NemoFileAttributes file_attributes,NemoFileCallback callback,gpointer callback_data)67 search_directory_file_call_when_ready (NemoFile *file,
68 				       NemoFileAttributes file_attributes,
69 				       NemoFileCallback callback,
70 				       gpointer callback_data)
71 
72 {
73 	/* Update display name, in case this didn't happen yet */
74 	nemo_search_directory_file_update_display_name (NEMO_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
search_directory_file_cancel_call_when_ready(NemoFile * file,NemoFileCallback callback,gpointer callback_data)81 search_directory_file_cancel_call_when_ready (NemoFile *file,
82 					       NemoFileCallback callback,
83 					       gpointer callback_data)
84 {
85 	/* Do nothing here, we don't have any pending calls */
86 }
87 
88 static gboolean
search_directory_file_check_if_ready(NemoFile * file,NemoFileAttributes attributes)89 search_directory_file_check_if_ready (NemoFile *file,
90 				      NemoFileAttributes attributes)
91 {
92 	return TRUE;
93 }
94 
95 static gboolean
search_directory_file_get_item_count(NemoFile * file,guint * count,gboolean * count_unreadable)96 search_directory_file_get_item_count (NemoFile *file,
97 				      guint *count,
98 				      gboolean *count_unreadable)
99 {
100 	GList *file_list;
101 
102 	if (count) {
103 		file_list = nemo_directory_get_file_list (file->details->directory);
104 
105 		*count = g_list_length (file_list);
106 
107 		nemo_file_list_free (file_list);
108 	}
109 
110 	return TRUE;
111 }
112 
113 static NemoRequestStatus
search_directory_file_get_deep_counts(NemoFile * file,guint * directory_count,guint * file_count,guint * unreadable_directory_count,guint * hidden_count,goffset * total_size)114 search_directory_file_get_deep_counts (NemoFile *file,
115 				       guint *directory_count,
116 				       guint *file_count,
117 				       guint *unreadable_directory_count,
118                        guint *hidden_count,
119 				       goffset *total_size)
120 {
121 	NemoFile *dir_file;
122 	GList *file_list, *l;
123 	guint dirs, files;
124 	GFileType type;
125 
126 	file_list = nemo_directory_get_file_list (file->details->directory);
127 
128 	dirs = files = 0;
129 	for (l = file_list; l != NULL; l = l->next) {
130 		dir_file = NEMO_FILE (l->data);
131 		type = nemo_file_get_file_type (dir_file);
132 		if (type == G_FILE_TYPE_DIRECTORY) {
133 			dirs++;
134 		} else {
135 			files++;
136 		}
137 	}
138 
139 	if (directory_count != NULL) {
140 		*directory_count = dirs;
141 	}
142 	if (file_count != NULL) {
143 		*file_count = files;
144 	}
145 	if (unreadable_directory_count != NULL) {
146 		*unreadable_directory_count = 0;
147 	}
148 	if (total_size != NULL) {
149 		/* FIXME: Maybe we want to calculate this? */
150 		*total_size = 0;
151 	}
152     if (hidden_count != NULL) {
153         *hidden_count = 0;
154     }
155 
156 	nemo_file_list_free (file_list);
157 
158 	return NEMO_REQUEST_DONE;
159 }
160 
161 static char *
search_directory_file_get_where_string(NemoFile * file)162 search_directory_file_get_where_string (NemoFile *file)
163 {
164 	return g_strdup (_("Search"));
165 }
166 
167 void
nemo_search_directory_file_update_display_name(NemoSearchDirectoryFile * search_file)168 nemo_search_directory_file_update_display_name (NemoSearchDirectoryFile *search_file)
169 {
170 	NemoFile *file;
171 	NemoSearchDirectory *search_dir;
172 	NemoQuery *query;
173 	char *display_name;
174 	gboolean changed;
175 
176 
177 	display_name = NULL;
178 	file = NEMO_FILE (search_file);
179 	if (file->details->directory) {
180 		search_dir = NEMO_SEARCH_DIRECTORY (file->details->directory);
181 		query = nemo_search_directory_get_query (search_dir);
182 
183 		if (query != NULL) {
184 			display_name = nemo_query_to_readable_string (query);
185 			g_object_unref (query);
186 		}
187 	}
188 
189 	if (display_name == NULL) {
190 		display_name = g_strdup (_("Search"));
191 	}
192 
193 	changed = nemo_file_set_display_name (file, display_name, NULL, TRUE);
194 	if (changed) {
195 		nemo_file_emit_changed (file);
196 	}
197 
198 	g_free (display_name);
199 }
200 
201 static void
nemo_search_directory_file_init(NemoSearchDirectoryFile * search_file)202 nemo_search_directory_file_init (NemoSearchDirectoryFile *search_file)
203 {
204 	NemoFile *file;
205 
206 	file = NEMO_FILE (search_file);
207 
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;
212 
213 	file->details->file_info_is_up_to_date = TRUE;
214 
215 	file->details->custom_icon = NULL;
216 	file->details->activation_uri = NULL;
217 	file->details->got_link_info = TRUE;
218 	file->details->link_info_is_up_to_date = TRUE;
219 
220 	file->details->directory_count = 0;
221 	file->details->got_directory_count = TRUE;
222 	file->details->directory_count_is_up_to_date = TRUE;
223 
224 	nemo_file_set_display_name (file, _("Search"), NULL, TRUE);
225 }
226 
227 static void
nemo_search_directory_file_class_init(NemoSearchDirectoryFileClass * klass)228 nemo_search_directory_file_class_init (NemoSearchDirectoryFileClass *klass)
229 {
230 	NemoFileClass *file_class;
231 
232 	file_class = NEMO_FILE_CLASS (klass);
233 
234 	file_class->default_file_type = G_FILE_TYPE_DIRECTORY;
235 
236 	file_class->monitor_add = search_directory_file_monitor_add;
237 	file_class->monitor_remove = search_directory_file_monitor_remove;
238 	file_class->call_when_ready = search_directory_file_call_when_ready;
239 	file_class->cancel_call_when_ready = search_directory_file_cancel_call_when_ready;
240 	file_class->check_if_ready = search_directory_file_check_if_ready;
241 	file_class->get_item_count = search_directory_file_get_item_count;
242 	file_class->get_deep_counts = search_directory_file_get_deep_counts;
243 	file_class->get_where_string = search_directory_file_get_where_string;
244 }
245