1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
2
3 caja-desktop-icon-file.c: Subclass of CajaFile 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., 51 Franklin St, Fifth Floor,
21 Boston, MA 02110-1301, USA.
22
23 Author: Alexander Larsson <alexl@redhat.com>
24 */
25
26 #include <config.h>
27 #include <glib/gi18n.h>
28 #include <string.h>
29 #include <gio/gio.h>
30
31 #include <eel/eel-glib-extensions.h>
32
33 #include "caja-desktop-icon-file.h"
34 #include "caja-desktop-metadata.h"
35 #include "caja-desktop-directory-file.h"
36 #include "caja-directory-notify.h"
37 #include "caja-directory-private.h"
38 #include "caja-file-attributes.h"
39 #include "caja-file-private.h"
40 #include "caja-file-utilities.h"
41 #include "caja-file-operations.h"
42 #include "caja-desktop-directory.h"
43
44 struct _CajaDesktopIconFilePrivate
45 {
46 CajaDesktopLink *link;
47 };
48
G_DEFINE_TYPE_WITH_PRIVATE(CajaDesktopIconFile,caja_desktop_icon_file,CAJA_TYPE_FILE)49 G_DEFINE_TYPE_WITH_PRIVATE (CajaDesktopIconFile, caja_desktop_icon_file, CAJA_TYPE_FILE)
50
51
52 static void
53 desktop_icon_file_monitor_add (CajaFile *file,
54 gconstpointer client,
55 CajaFileAttributes attributes)
56 {
57 caja_directory_monitor_add_internal
58 (file->details->directory, file,
59 client, TRUE, attributes, NULL, NULL);
60 }
61
62 static void
desktop_icon_file_monitor_remove(CajaFile * file,gconstpointer client)63 desktop_icon_file_monitor_remove (CajaFile *file,
64 gconstpointer client)
65 {
66 caja_directory_monitor_remove_internal
67 (file->details->directory, file, client);
68 }
69
70 static void
desktop_icon_file_call_when_ready(CajaFile * file,CajaFileAttributes attributes,CajaFileCallback callback,gpointer callback_data)71 desktop_icon_file_call_when_ready (CajaFile *file,
72 CajaFileAttributes attributes,
73 CajaFileCallback callback,
74 gpointer callback_data)
75 {
76 caja_directory_call_when_ready_internal
77 (file->details->directory, file,
78 attributes, FALSE, NULL, callback, callback_data);
79 }
80
81 static void
desktop_icon_file_cancel_call_when_ready(CajaFile * file,CajaFileCallback callback,gpointer callback_data)82 desktop_icon_file_cancel_call_when_ready (CajaFile *file,
83 CajaFileCallback callback,
84 gpointer callback_data)
85 {
86 caja_directory_cancel_callback_internal
87 (file->details->directory, file,
88 NULL, callback, callback_data);
89 }
90
91 static gboolean
desktop_icon_file_check_if_ready(CajaFile * file,CajaFileAttributes attributes)92 desktop_icon_file_check_if_ready (CajaFile *file,
93 CajaFileAttributes attributes)
94 {
95 return caja_directory_check_if_ready_internal
96 (file->details->directory, file,
97 attributes);
98 }
99
100 static gboolean
desktop_icon_file_get_item_count(CajaFile * file,guint * count,gboolean * count_unreadable)101 desktop_icon_file_get_item_count (CajaFile *file,
102 guint *count,
103 gboolean *count_unreadable)
104 {
105 if (count != NULL)
106 {
107 *count = 0;
108 }
109 if (count_unreadable != NULL)
110 {
111 *count_unreadable = FALSE;
112 }
113 return TRUE;
114 }
115
116 static CajaRequestStatus
desktop_icon_file_get_deep_counts(CajaFile * file,guint * directory_count,guint * file_count,guint * unreadable_directory_count,goffset * total_size,goffset * total_size_on_disk)117 desktop_icon_file_get_deep_counts (CajaFile *file,
118 guint *directory_count,
119 guint *file_count,
120 guint *unreadable_directory_count,
121 goffset *total_size,
122 goffset *total_size_on_disk)
123 {
124 if (directory_count != NULL)
125 {
126 *directory_count = 0;
127 }
128 if (file_count != NULL)
129 {
130 *file_count = 0;
131 }
132 if (unreadable_directory_count != NULL)
133 {
134 *unreadable_directory_count = 0;
135 }
136 if (total_size != NULL)
137 {
138 *total_size = 0;
139 }
140 if (total_size_on_disk != NULL)
141 {
142 *total_size_on_disk = 0;
143 }
144
145 return CAJA_REQUEST_DONE;
146 }
147
148 static gboolean
desktop_icon_file_get_date(CajaFile * file,CajaDateType date_type,time_t * date)149 desktop_icon_file_get_date (CajaFile *file,
150 CajaDateType date_type,
151 time_t *date)
152 {
153 CajaDesktopIconFile *desktop_file;
154
155 desktop_file = CAJA_DESKTOP_ICON_FILE (file);
156
157 return caja_desktop_link_get_date (desktop_file->details->link,
158 date_type, date);
159 }
160
161 static char *
desktop_icon_file_get_where_string(CajaFile * file)162 desktop_icon_file_get_where_string (CajaFile *file)
163 {
164 return g_strdup (_("on the desktop"));
165 }
166
167 static void
caja_desktop_icon_file_init(CajaDesktopIconFile * desktop_file)168 caja_desktop_icon_file_init (CajaDesktopIconFile *desktop_file)
169 {
170 desktop_file->details = caja_desktop_icon_file_get_instance_private (desktop_file);
171 }
172
173 static void
update_info_from_link(CajaDesktopIconFile * icon_file)174 update_info_from_link (CajaDesktopIconFile *icon_file)
175 {
176 CajaFile *file;
177 CajaDesktopLink *link;
178 char *display_name;
179 GMount *mount;
180
181 file = CAJA_FILE (icon_file);
182
183 link = icon_file->details->link;
184
185 if (link == NULL)
186 {
187 return;
188 }
189
190 g_clear_pointer (&file->details->mime_type, g_ref_string_release);
191 file->details->mime_type = g_ref_string_new_intern ("application/x-caja-link");
192 file->details->type = G_FILE_TYPE_SHORTCUT;
193 file->details->size = 0;
194 file->details->has_permissions = FALSE;
195 file->details->can_read = TRUE;
196 file->details->can_write = TRUE;
197
198 file->details->can_mount = FALSE;
199 file->details->can_unmount = FALSE;
200 file->details->can_eject = FALSE;
201 if (file->details->mount)
202 {
203 g_object_unref (file->details->mount);
204 }
205 mount = caja_desktop_link_get_mount (link);
206 file->details->mount = mount;
207 if (mount)
208 {
209 file->details->can_unmount = g_mount_can_unmount (mount);
210 file->details->can_eject = g_mount_can_eject (mount);
211 }
212
213 file->details->file_info_is_up_to_date = TRUE;
214
215 display_name = caja_desktop_link_get_display_name (link);
216 caja_file_set_display_name (file,
217 display_name, NULL, TRUE);
218 g_free (display_name);
219
220 if (file->details->icon != NULL)
221 {
222 g_object_unref (file->details->icon);
223 }
224 file->details->icon = caja_desktop_link_get_icon (link);
225 g_free (file->details->activation_uri);
226 file->details->activation_uri = caja_desktop_link_get_activation_uri (link);
227 file->details->got_link_info = TRUE;
228 file->details->link_info_is_up_to_date = TRUE;
229
230 file->details->directory_count = 0;
231 file->details->got_directory_count = TRUE;
232 file->details->directory_count_is_up_to_date = TRUE;
233 }
234
235 void
caja_desktop_icon_file_update(CajaDesktopIconFile * icon_file)236 caja_desktop_icon_file_update (CajaDesktopIconFile *icon_file)
237 {
238 CajaFile *file;
239
240 update_info_from_link (icon_file);
241 file = CAJA_FILE (icon_file);
242 caja_file_changed (file);
243 }
244
245 void
caja_desktop_icon_file_remove(CajaDesktopIconFile * icon_file)246 caja_desktop_icon_file_remove (CajaDesktopIconFile *icon_file)
247 {
248 CajaFile *file;
249 GList list;
250
251 icon_file->details->link = NULL;
252
253 file = CAJA_FILE (icon_file);
254
255 /* ref here because we might be removing the last ref when we
256 * mark the file gone below, but we need to keep a ref at
257 * least long enough to send the change notification.
258 */
259 caja_file_ref (file);
260
261 file->details->is_gone = TRUE;
262
263 list.data = file;
264 list.next = NULL;
265 list.prev = NULL;
266
267 caja_directory_remove_file (file->details->directory, file);
268 caja_directory_emit_change_signals (file->details->directory, &list);
269
270 caja_file_unref (file);
271 }
272
273 CajaDesktopIconFile *
caja_desktop_icon_file_new(CajaDesktopLink * link)274 caja_desktop_icon_file_new (CajaDesktopLink *link)
275 {
276 CajaFile *file;
277 CajaDirectory *directory;
278 CajaDesktopIconFile *icon_file;
279 GList list;
280 char *name;
281
282 directory = caja_directory_get_by_uri (EEL_DESKTOP_URI);
283
284 file = CAJA_FILE (g_object_new (CAJA_TYPE_DESKTOP_ICON_FILE, NULL));
285
286 #ifdef CAJA_FILE_DEBUG_REF
287 printf("%10p ref'd\n", file);
288 eazel_dump_stack_trace ("\t", 10);
289 #endif
290
291 file->details->directory = directory;
292
293 icon_file = CAJA_DESKTOP_ICON_FILE (file);
294 icon_file->details->link = link;
295
296 name = caja_desktop_link_get_file_name (link);
297 file->details->name = g_ref_string_new (name);
298 g_free (name);
299
300 update_info_from_link (icon_file);
301
302 caja_desktop_update_metadata_from_keyfile (file, file->details->name);
303
304 caja_directory_add_file (directory, file);
305
306 list.data = file;
307 list.next = NULL;
308 list.prev = NULL;
309 caja_directory_emit_files_added (directory, &list);
310
311 return icon_file;
312 }
313
314 /* Note: This can return NULL if the link was recently removed (i.e. unmounted) */
315 CajaDesktopLink *
caja_desktop_icon_file_get_link(CajaDesktopIconFile * icon_file)316 caja_desktop_icon_file_get_link (CajaDesktopIconFile *icon_file)
317 {
318 if (icon_file->details->link)
319 return g_object_ref (icon_file->details->link);
320 else
321 return NULL;
322 }
323
324 static void
caja_desktop_icon_file_unmount(CajaFile * file,GMountOperation * mount_op,GCancellable * cancellable,CajaFileOperationCallback callback,gpointer callback_data)325 caja_desktop_icon_file_unmount (CajaFile *file,
326 GMountOperation *mount_op,
327 GCancellable *cancellable,
328 CajaFileOperationCallback callback,
329 gpointer callback_data)
330 {
331 CajaDesktopIconFile *desktop_file;
332
333 desktop_file = CAJA_DESKTOP_ICON_FILE (file);
334 if (desktop_file)
335 {
336 GMount *mount;
337
338 mount = caja_desktop_link_get_mount (desktop_file->details->link);
339 if (mount != NULL)
340 {
341 caja_file_operations_unmount_mount (NULL, mount, FALSE, TRUE);
342 }
343 }
344
345 }
346
347 static void
caja_desktop_icon_file_eject(CajaFile * file,GMountOperation * mount_op,GCancellable * cancellable,CajaFileOperationCallback callback,gpointer callback_data)348 caja_desktop_icon_file_eject (CajaFile *file,
349 GMountOperation *mount_op,
350 GCancellable *cancellable,
351 CajaFileOperationCallback callback,
352 gpointer callback_data)
353 {
354 CajaDesktopIconFile *desktop_file;
355
356 desktop_file = CAJA_DESKTOP_ICON_FILE (file);
357 if (desktop_file)
358 {
359 GMount *mount;
360
361 mount = caja_desktop_link_get_mount (desktop_file->details->link);
362 if (mount != NULL)
363 {
364 caja_file_operations_unmount_mount (NULL, mount, TRUE, TRUE);
365 }
366 }
367 }
368
369 static void
caja_desktop_icon_file_set_metadata(CajaFile * file,const char * key,const char * value)370 caja_desktop_icon_file_set_metadata (CajaFile *file,
371 const char *key,
372 const char *value)
373 {
374 caja_desktop_set_metadata_string (file, file->details->name, key, value);
375 }
376
377 static void
caja_desktop_icon_file_set_metadata_as_list(CajaFile * file,const char * key,char ** value)378 caja_desktop_icon_file_set_metadata_as_list (CajaFile *file,
379 const char *key,
380 char **value)
381 {
382 caja_desktop_set_metadata_stringv (file, file->details->name, key, (const gchar **) value);
383 }
384
385 static void
caja_desktop_icon_file_class_init(CajaDesktopIconFileClass * klass)386 caja_desktop_icon_file_class_init (CajaDesktopIconFileClass *klass)
387 {
388 CajaFileClass *file_class;
389
390 file_class = CAJA_FILE_CLASS (klass);
391
392 file_class->default_file_type = G_FILE_TYPE_DIRECTORY;
393
394 file_class->monitor_add = desktop_icon_file_monitor_add;
395 file_class->monitor_remove = desktop_icon_file_monitor_remove;
396 file_class->call_when_ready = desktop_icon_file_call_when_ready;
397 file_class->cancel_call_when_ready = desktop_icon_file_cancel_call_when_ready;
398 file_class->check_if_ready = desktop_icon_file_check_if_ready;
399 file_class->get_item_count = desktop_icon_file_get_item_count;
400 file_class->get_deep_counts = desktop_icon_file_get_deep_counts;
401 file_class->get_date = desktop_icon_file_get_date;
402 file_class->get_where_string = desktop_icon_file_get_where_string;
403 file_class->set_metadata = caja_desktop_icon_file_set_metadata;
404 file_class->set_metadata_as_list = caja_desktop_icon_file_set_metadata_as_list;
405 file_class->unmount = caja_desktop_icon_file_unmount;
406 file_class->eject = caja_desktop_icon_file_eject;
407 }
408