nautilus-3.6.3/libnautilus-private/nautilus-desktop-icon-file.c

No issues found

  1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
  2 
  3    nautilus-desktop-icon-file.c: Subclass of NautilusFile to help implement the
  4    virtual desktop icons.
  5  
  6    Copyright (C) 2003 Red Hat, 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: Alexander Larsson <alexl@redhat.com>
 24 */
 25 
 26 #include <config.h>
 27 #include "nautilus-desktop-icon-file.h"
 28 
 29 #include "nautilus-desktop-metadata.h"
 30 #include "nautilus-desktop-directory-file.h"
 31 #include "nautilus-directory-notify.h"
 32 #include "nautilus-directory-private.h"
 33 #include "nautilus-file-attributes.h"
 34 #include "nautilus-file-private.h"
 35 #include "nautilus-file-utilities.h"
 36 #include "nautilus-file-operations.h"
 37 #include <eel/eel-glib-extensions.h>
 38 #include "nautilus-desktop-directory.h"
 39 #include <glib/gi18n.h>
 40 #include <string.h>
 41 #include <gio/gio.h>
 42 
 43 struct NautilusDesktopIconFileDetails {
 44 	NautilusDesktopLink *link;
 45 };
 46 
 47 G_DEFINE_TYPE(NautilusDesktopIconFile, nautilus_desktop_icon_file, NAUTILUS_TYPE_FILE)
 48 
 49 
 50 static void
 51 desktop_icon_file_monitor_add (NautilusFile *file,
 52 			       gconstpointer client,
 53 			       NautilusFileAttributes attributes)
 54 {
 55 	nautilus_directory_monitor_add_internal
 56 		(file->details->directory, file,
 57 		 client, TRUE, attributes, NULL, NULL);
 58 }
 59 
 60 static void
 61 desktop_icon_file_monitor_remove (NautilusFile *file,
 62 				  gconstpointer client)
 63 {
 64 	nautilus_directory_monitor_remove_internal
 65 		(file->details->directory, file, client);
 66 }
 67 
 68 static void
 69 desktop_icon_file_call_when_ready (NautilusFile *file,
 70 				   NautilusFileAttributes attributes,
 71 				   NautilusFileCallback callback,
 72 				   gpointer callback_data)
 73 {
 74 	nautilus_directory_call_when_ready_internal
 75 		(file->details->directory, file,
 76 		 attributes, FALSE, NULL, callback, callback_data);
 77 }
 78 
 79 static void
 80 desktop_icon_file_cancel_call_when_ready (NautilusFile *file,
 81 					  NautilusFileCallback callback,
 82 					  gpointer callback_data)
 83 {
 84 	nautilus_directory_cancel_callback_internal
 85 		(file->details->directory, file,
 86 		 NULL, callback, callback_data);
 87 }
 88 
 89 static gboolean
 90 desktop_icon_file_check_if_ready (NautilusFile *file,
 91 				  NautilusFileAttributes attributes)
 92 {
 93 	return nautilus_directory_check_if_ready_internal
 94 		(file->details->directory, file,
 95 		 attributes);
 96 }
 97 
 98 static gboolean
 99 desktop_icon_file_get_item_count (NautilusFile *file, 
100 				  guint *count,
101 				  gboolean *count_unreadable)
102 {
103 	if (count != NULL) {
104 		*count = 0;
105 	}
106 	if (count_unreadable != NULL) {
107 		*count_unreadable = FALSE;
108 	}
109 	return TRUE;
110 }
111 
112 static NautilusRequestStatus
113 desktop_icon_file_get_deep_counts (NautilusFile *file,
114 				   guint *directory_count,
115 				   guint *file_count,
116 				   guint *unreadable_directory_count,
117 				   goffset *total_size)
118 {
119 	if (directory_count != NULL) {
120 		*directory_count = 0;
121 	}
122 	if (file_count != NULL) {
123 		*file_count = 0;
124 	}
125 	if (unreadable_directory_count != NULL) {
126 		*unreadable_directory_count = 0;
127 	}
128 	if (total_size != NULL) {
129 		*total_size = 0;
130 	}
131 
132 	return NAUTILUS_REQUEST_DONE;
133 }
134 
135 static gboolean
136 desktop_icon_file_get_date (NautilusFile *file,
137 			    NautilusDateType date_type,
138 			    time_t *date)
139 {
140 	NautilusDesktopIconFile *desktop_file;
141 
142 	desktop_file = NAUTILUS_DESKTOP_ICON_FILE (file);
143 
144 	return nautilus_desktop_link_get_date (desktop_file->details->link,
145 					       date_type, date);
146 }
147 
148 static char *
149 desktop_icon_file_get_where_string (NautilusFile *file)
150 {
151 	return g_strdup (_("on the desktop"));
152 }
153 
154 static void
155 nautilus_desktop_icon_file_init (NautilusDesktopIconFile *desktop_file)
156 {
157 	desktop_file->details =	G_TYPE_INSTANCE_GET_PRIVATE (desktop_file,
158 							     NAUTILUS_TYPE_DESKTOP_ICON_FILE,
159 							     NautilusDesktopIconFileDetails);
160 }
161 
162 static void
163 update_info_from_link (NautilusDesktopIconFile *icon_file)
164 {
165 	NautilusFile *file;
166 	NautilusDesktopLink *link;
167 	char *display_name;
168 	GMount *mount;
169 	
170 	file = NAUTILUS_FILE (icon_file);
171 	
172 	link = icon_file->details->link;
173 
174 	if (link == NULL) {
175 		return;
176 	}
177 
178 	eel_ref_str_unref (file->details->mime_type);
179 	file->details->mime_type = eel_ref_str_get_unique ("application/x-nautilus-link");
180 	file->details->type = G_FILE_TYPE_SHORTCUT;
181 	file->details->size = 0;
182 	file->details->has_permissions = FALSE;
183 	file->details->can_read = TRUE;
184 	file->details->can_write = TRUE;
185 
186 	file->details->can_mount = FALSE;
187 	file->details->can_unmount = FALSE;
188 	file->details->can_eject = FALSE;
189 	if (file->details->mount) {
190 		g_object_unref (file->details->mount);
191 	}
192 	mount = nautilus_desktop_link_get_mount (link);
193 	file->details->mount = mount;
194 	if (mount) {
195 		file->details->can_unmount = g_mount_can_unmount (mount);
196 		file->details->can_eject = g_mount_can_eject (mount);
197 	}
198 	
199 	file->details->file_info_is_up_to_date = TRUE;
200 
201 	display_name = nautilus_desktop_link_get_display_name (link);
202 	nautilus_file_set_display_name (file,
203 					display_name, NULL, TRUE);
204 	g_free (display_name);
205 
206 	if (file->details->icon != NULL) {
207 		g_object_unref (file->details->icon);
208 	}
209 	file->details->icon = nautilus_desktop_link_get_icon (link);
210 	g_free (file->details->activation_uri);
211 	file->details->activation_uri = nautilus_desktop_link_get_activation_uri (link);
212 	file->details->got_link_info = TRUE;
213 	file->details->link_info_is_up_to_date = TRUE;
214 
215 	file->details->directory_count = 0;
216 	file->details->got_directory_count = TRUE;
217 	file->details->directory_count_is_up_to_date = TRUE;
218 }
219 
220 void
221 nautilus_desktop_icon_file_update (NautilusDesktopIconFile *icon_file)
222 {
223 	NautilusFile *file;
224 	
225 	update_info_from_link (icon_file);
226 	file = NAUTILUS_FILE (icon_file);
227 	nautilus_file_changed (file);
228 }
229 
230 void
231 nautilus_desktop_icon_file_remove (NautilusDesktopIconFile *icon_file)
232 {
233 	NautilusFile *file;
234 	GList list;
235 
236 	icon_file->details->link = NULL;
237 
238 	file = NAUTILUS_FILE (icon_file);
239 	
240 	/* ref here because we might be removing the last ref when we
241 	 * mark the file gone below, but we need to keep a ref at
242 	 * least long enough to send the change notification. 
243 	 */
244 	nautilus_file_ref (file);
245 	
246 	file->details->is_gone = TRUE;
247 	
248 	list.data = file;
249 	list.next = NULL;
250 	list.prev = NULL;
251 	
252 	nautilus_directory_remove_file (file->details->directory, file);
253 	nautilus_directory_emit_change_signals (file->details->directory, &list);
254 	
255 	nautilus_file_unref (file);
256 }
257 
258 NautilusDesktopIconFile *
259 nautilus_desktop_icon_file_new (NautilusDesktopLink *link)
260 {
261 	NautilusFile *file;
262 	NautilusDirectory *directory;
263 	NautilusDesktopIconFile *icon_file;
264 	GList list;
265 	char *name;
266 
267 	directory = nautilus_directory_get_by_uri (EEL_DESKTOP_URI);
268 
269 	file = NAUTILUS_FILE (g_object_new (NAUTILUS_TYPE_DESKTOP_ICON_FILE, NULL));
270 
271 #ifdef NAUTILUS_FILE_DEBUG_REF
272 	printf("%10p ref'd\n", file);
273 	eazel_dump_stack_trace ("\t", 10);
274 #endif
275 
276 	file->details->directory = directory;
277 
278 	icon_file = NAUTILUS_DESKTOP_ICON_FILE (file);
279 	icon_file->details->link = link;
280 
281 	name = nautilus_desktop_link_get_file_name (link);
282 	file->details->name = eel_ref_str_new (name);
283 	g_free (name);
284 
285 	update_info_from_link (icon_file);
286 
287 	nautilus_desktop_update_metadata_from_keyfile (file, file->details->name);
288 
289 	nautilus_directory_add_file (directory, file);
290 
291 	list.data = file;
292 	list.next = NULL;
293 	list.prev = NULL;
294 	nautilus_directory_emit_files_added (directory, &list);
295 
296 	return icon_file;
297 }
298 
299 /* Note: This can return NULL if the link was recently removed (i.e. unmounted) */
300 NautilusDesktopLink *
301 nautilus_desktop_icon_file_get_link (NautilusDesktopIconFile *icon_file)
302 {
303 	if (icon_file->details->link)
304 		return g_object_ref (icon_file->details->link);
305 	else
306 		return NULL;
307 }
308 
309 static void
310 nautilus_desktop_icon_file_unmount (NautilusFile                   *file,
311 				    GMountOperation                *mount_op,
312 				    GCancellable                   *cancellable,
313 				    NautilusFileOperationCallback   callback,
314 				    gpointer                        callback_data)
315 {
316 	NautilusDesktopIconFile *desktop_file;
317 	GMount *mount;
318 	
319 	desktop_file = NAUTILUS_DESKTOP_ICON_FILE (file);
320 	if (desktop_file) {
321 		mount = nautilus_desktop_link_get_mount (desktop_file->details->link);
322 		if (mount != NULL) {
323 			nautilus_file_operations_unmount_mount (NULL, mount, FALSE, TRUE);
324 		}
325 	}
326 	
327 }
328 
329 static void
330 nautilus_desktop_icon_file_eject (NautilusFile                   *file,
331 				  GMountOperation                *mount_op,
332 				  GCancellable                   *cancellable,
333 				  NautilusFileOperationCallback   callback,
334 				  gpointer                        callback_data)
335 {
336 	NautilusDesktopIconFile *desktop_file;
337 	GMount *mount;
338 	
339 	desktop_file = NAUTILUS_DESKTOP_ICON_FILE (file);
340 	if (desktop_file) {
341 		mount = nautilus_desktop_link_get_mount (desktop_file->details->link);
342 		if (mount != NULL) {
343 			nautilus_file_operations_unmount_mount (NULL, mount, TRUE, TRUE);
344 		}
345 	}
346 }
347 
348 static void
349 nautilus_desktop_icon_file_set_metadata (NautilusFile           *file,
350 					 const char             *key,
351 					 const char             *value)
352 {
353 	nautilus_desktop_set_metadata_string (file, file->details->name, key, value);
354 }
355 
356 static void
357 nautilus_desktop_icon_file_set_metadata_as_list (NautilusFile           *file,
358 						 const char             *key,
359 						 char                  **value)
360 {
361 	nautilus_desktop_set_metadata_stringv (file, file->details->name, key, (const gchar **) value);
362 }
363 
364 static void
365 nautilus_desktop_icon_file_class_init (NautilusDesktopIconFileClass *klass)
366 {
367 	GObjectClass *object_class;
368 	NautilusFileClass *file_class;
369 
370 	object_class = G_OBJECT_CLASS (klass);
371 	file_class = NAUTILUS_FILE_CLASS (klass);
372 
373 	file_class->default_file_type = G_FILE_TYPE_DIRECTORY;
374 	
375 	file_class->monitor_add = desktop_icon_file_monitor_add;
376 	file_class->monitor_remove = desktop_icon_file_monitor_remove;
377 	file_class->call_when_ready = desktop_icon_file_call_when_ready;
378 	file_class->cancel_call_when_ready = desktop_icon_file_cancel_call_when_ready;
379 	file_class->check_if_ready = desktop_icon_file_check_if_ready;
380 	file_class->get_item_count = desktop_icon_file_get_item_count;
381 	file_class->get_deep_counts = desktop_icon_file_get_deep_counts;
382 	file_class->get_date = desktop_icon_file_get_date;
383 	file_class->get_where_string = desktop_icon_file_get_where_string;
384 	file_class->set_metadata = nautilus_desktop_icon_file_set_metadata;
385 	file_class->set_metadata_as_list = nautilus_desktop_icon_file_set_metadata_as_list;
386 	file_class->unmount = nautilus_desktop_icon_file_unmount;
387 	file_class->eject = nautilus_desktop_icon_file_eject;
388 
389 	g_type_class_add_private (object_class, sizeof(NautilusDesktopIconFileDetails));
390 }