1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
2
3 caja-desktop-directory-file.c: Subclass of CajaFile to help implement the
4 virtual desktop.
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 <gtk/gtk.h>
28 #include <glib/gi18n.h>
29 #include <string.h>
30
31 #include <eel/eel-glib-extensions.h>
32 #include <eel/eel-gtk-macros.h>
33
34 #include "caja-desktop-directory-file.h"
35 #include "caja-desktop-metadata.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-desktop-directory.h"
42 #include "caja-metadata.h"
43
44 struct CajaDesktopDirectoryFileDetails
45 {
46 CajaDesktopDirectory *desktop_directory;
47
48 CajaFile *real_dir_file;
49
50 GHashTable *callbacks;
51 GHashTable *monitors;
52 };
53
54 typedef struct
55 {
56 CajaDesktopDirectoryFile *desktop_file;
57 CajaFileCallback callback;
58 gpointer callback_data;
59
60 CajaFileAttributes delegated_attributes;
61 CajaFileAttributes non_delegated_attributes;
62
63 GList *non_ready_files;
64
65 gboolean initializing;
66 } DesktopCallback;
67
68 typedef struct
69 {
70 CajaDesktopDirectoryFile *desktop_file;
71
72 CajaFileAttributes delegated_attributes;
73 CajaFileAttributes non_delegated_attributes;
74 } DesktopMonitor;
75
76
77 static void caja_desktop_directory_file_init (gpointer object,
78 gpointer klass);
79 static void caja_desktop_directory_file_class_init (gpointer klass);
80
EEL_CLASS_BOILERPLATE(CajaDesktopDirectoryFile,caja_desktop_directory_file,CAJA_TYPE_FILE)81 EEL_CLASS_BOILERPLATE (CajaDesktopDirectoryFile,
82 caja_desktop_directory_file,
83 CAJA_TYPE_FILE)
84
85 static guint
86 desktop_callback_hash (gconstpointer desktop_callback_as_pointer)
87 {
88 const DesktopCallback *desktop_callback;
89
90 desktop_callback = desktop_callback_as_pointer;
91 return GPOINTER_TO_UINT (desktop_callback->callback)
92 ^ GPOINTER_TO_UINT (desktop_callback->callback_data);
93 }
94
95 static gboolean
desktop_callback_equal(gconstpointer desktop_callback_as_pointer,gconstpointer desktop_callback_as_pointer_2)96 desktop_callback_equal (gconstpointer desktop_callback_as_pointer,
97 gconstpointer desktop_callback_as_pointer_2)
98 {
99 const DesktopCallback *desktop_callback, *desktop_callback_2;
100
101 desktop_callback = desktop_callback_as_pointer;
102 desktop_callback_2 = desktop_callback_as_pointer_2;
103
104 return desktop_callback->callback == desktop_callback_2->callback
105 && desktop_callback->callback_data == desktop_callback_2->callback_data;
106 }
107
108
109 static void
real_file_changed_callback(CajaFile * real_file,gpointer callback_data)110 real_file_changed_callback (CajaFile *real_file,
111 gpointer callback_data)
112 {
113 CajaDesktopDirectoryFile *desktop_file;
114
115 desktop_file = CAJA_DESKTOP_DIRECTORY_FILE (callback_data);
116 caja_file_changed (CAJA_FILE (desktop_file));
117 }
118
119 static CajaFileAttributes
get_delegated_attributes_mask(void)120 get_delegated_attributes_mask (void)
121 {
122 return CAJA_FILE_ATTRIBUTE_DEEP_COUNTS |
123 CAJA_FILE_ATTRIBUTE_DIRECTORY_ITEM_COUNT |
124 CAJA_FILE_ATTRIBUTE_DIRECTORY_ITEM_MIME_TYPES;
125 }
126
127 static void
partition_attributes(CajaFileAttributes attributes,CajaFileAttributes * delegated_attributes,CajaFileAttributes * non_delegated_attributes)128 partition_attributes (CajaFileAttributes attributes,
129 CajaFileAttributes *delegated_attributes,
130 CajaFileAttributes *non_delegated_attributes)
131 {
132 CajaFileAttributes mask;
133
134 mask = get_delegated_attributes_mask ();
135
136 *delegated_attributes = attributes & mask;
137 *non_delegated_attributes = attributes & ~mask;
138 }
139
140 static void
desktop_directory_file_monitor_add(CajaFile * file,gconstpointer client,CajaFileAttributes attributes)141 desktop_directory_file_monitor_add (CajaFile *file,
142 gconstpointer client,
143 CajaFileAttributes attributes)
144 {
145 CajaDesktopDirectoryFile *desktop_file;
146 DesktopMonitor *monitor;
147
148 desktop_file = CAJA_DESKTOP_DIRECTORY_FILE (file);
149
150 /* Map the client to a unique value so this doesn't interfere
151 * with direct monitoring of the file by the same client.
152 */
153 monitor = g_hash_table_lookup (desktop_file->details->monitors, client);
154 if (monitor != NULL)
155 {
156 g_assert (monitor->desktop_file == desktop_file);
157 }
158 else
159 {
160 monitor = g_new0 (DesktopMonitor, 1);
161 monitor->desktop_file = desktop_file;
162 g_hash_table_insert (desktop_file->details->monitors,
163 (gpointer) client, monitor);
164 }
165
166 partition_attributes (attributes,
167 &monitor->delegated_attributes,
168 &monitor->non_delegated_attributes);
169
170 /* Pawn off partioned attributes to real dir file */
171 caja_file_monitor_add (desktop_file->details->real_dir_file,
172 monitor, monitor->delegated_attributes);
173
174 /* Do the rest ourself */
175 caja_directory_monitor_add_internal
176 (file->details->directory, file,
177 client, TRUE,
178 monitor->non_delegated_attributes,
179 NULL, NULL);
180 }
181
182 static void
desktop_directory_file_monitor_remove(CajaFile * file,gconstpointer client)183 desktop_directory_file_monitor_remove (CajaFile *file,
184 gconstpointer client)
185 {
186 CajaDesktopDirectoryFile *desktop_file;
187 DesktopMonitor *monitor;
188
189 desktop_file = CAJA_DESKTOP_DIRECTORY_FILE (file);
190
191 /* Map the client to the value used by the earlier add call. */
192 monitor = g_hash_table_lookup (desktop_file->details->monitors, client);
193 if (monitor == NULL)
194 {
195 return;
196 }
197
198 /* Call through to the real file remove calls. */
199 g_hash_table_remove (desktop_file->details->monitors, client);
200
201 /* Remove the locally handled parts */
202 caja_directory_monitor_remove_internal
203 (file->details->directory, file, client);
204 }
205
206 static void
desktop_callback_destroy(DesktopCallback * desktop_callback)207 desktop_callback_destroy (DesktopCallback *desktop_callback)
208 {
209 g_assert (desktop_callback != NULL);
210 g_assert (CAJA_IS_DESKTOP_DIRECTORY_FILE (desktop_callback->desktop_file));
211
212 caja_file_unref (CAJA_FILE (desktop_callback->desktop_file));
213 g_list_free (desktop_callback->non_ready_files);
214 g_free (desktop_callback);
215 }
216
217 static void
desktop_callback_check_done(DesktopCallback * desktop_callback)218 desktop_callback_check_done (DesktopCallback *desktop_callback)
219 {
220 /* Check if we are ready. */
221 if (desktop_callback->initializing ||
222 desktop_callback->non_ready_files != NULL)
223 {
224 return;
225 }
226
227 /* Ensure our metadata is updated before calling back. */
228 caja_desktop_update_metadata_from_keyfile(CAJA_FILE (desktop_callback->desktop_file),
229 "directory");
230
231 /* Remove from the hash table before sending it. */
232 g_hash_table_remove (desktop_callback->desktop_file->details->callbacks,
233 desktop_callback);
234
235 /* We are ready, so do the real callback. */
236 (* desktop_callback->callback) (CAJA_FILE (desktop_callback->desktop_file),
237 desktop_callback->callback_data);
238
239 /* And we are done. */
240 desktop_callback_destroy (desktop_callback);
241 }
242
243 static void
desktop_callback_remove_file(DesktopCallback * desktop_callback,CajaFile * file)244 desktop_callback_remove_file (DesktopCallback *desktop_callback,
245 CajaFile *file)
246 {
247 desktop_callback->non_ready_files = g_list_remove
248 (desktop_callback->non_ready_files, file);
249 desktop_callback_check_done (desktop_callback);
250 }
251
252 static void
ready_callback(CajaFile * file,gpointer callback_data)253 ready_callback (CajaFile *file,
254 gpointer callback_data)
255 {
256 DesktopCallback *desktop_callback;
257
258 g_assert (CAJA_IS_FILE (file));
259 g_assert (callback_data != NULL);
260
261 desktop_callback = callback_data;
262 g_assert (g_list_find (desktop_callback->non_ready_files, file) != NULL);
263
264 desktop_callback_remove_file (desktop_callback, file);
265 }
266
267 static void
desktop_directory_file_call_when_ready(CajaFile * file,CajaFileAttributes attributes,CajaFileCallback callback,gpointer callback_data)268 desktop_directory_file_call_when_ready (CajaFile *file,
269 CajaFileAttributes attributes,
270 CajaFileCallback callback,
271 gpointer callback_data)
272
273 {
274 CajaDesktopDirectoryFile *desktop_file;
275 DesktopCallback search_key, *desktop_callback;
276
277 desktop_file = CAJA_DESKTOP_DIRECTORY_FILE (file);
278
279 /* Check to be sure we aren't overwriting. */
280 search_key.callback = callback;
281 search_key.callback_data = callback_data;
282 if (g_hash_table_lookup (desktop_file->details->callbacks, &search_key) != NULL)
283 {
284 g_warning ("tried to add a new callback while an old one was pending");
285 return;
286 }
287
288 /* Create a desktop_callback record. */
289 desktop_callback = g_new0 (DesktopCallback, 1);
290 caja_file_ref (file);
291 desktop_callback->desktop_file = desktop_file;
292 desktop_callback->callback = callback;
293 desktop_callback->callback_data = callback_data;
294 desktop_callback->initializing = TRUE;
295
296 partition_attributes (attributes,
297 &desktop_callback->delegated_attributes,
298 &desktop_callback->non_delegated_attributes);
299
300 desktop_callback->non_ready_files = g_list_prepend
301 (desktop_callback->non_ready_files, file);
302 desktop_callback->non_ready_files = g_list_prepend
303 (desktop_callback->non_ready_files, desktop_file->details->real_dir_file);
304
305 /* Put it in the hash table. */
306 g_hash_table_insert (desktop_file->details->callbacks,
307 desktop_callback, desktop_callback);
308
309 /* Now connect to each file's call_when_ready. */
310 caja_directory_call_when_ready_internal
311 (file->details->directory, file,
312 desktop_callback->non_delegated_attributes,
313 FALSE, NULL, ready_callback, desktop_callback);
314 caja_file_call_when_ready
315 (desktop_file->details->real_dir_file,
316 desktop_callback->delegated_attributes,
317 ready_callback, desktop_callback);
318
319 desktop_callback->initializing = FALSE;
320
321 /* Check if any files became read while we were connecting up
322 * the call_when_ready callbacks (also handles the pathological
323 * case where there are no files at all).
324 */
325 desktop_callback_check_done (desktop_callback);
326
327 }
328
329 static void
desktop_directory_file_cancel_call_when_ready(CajaFile * file,CajaFileCallback callback,gpointer callback_data)330 desktop_directory_file_cancel_call_when_ready (CajaFile *file,
331 CajaFileCallback callback,
332 gpointer callback_data)
333 {
334 CajaDesktopDirectoryFile *desktop_file;
335 DesktopCallback search_key, *desktop_callback;
336
337 desktop_file = CAJA_DESKTOP_DIRECTORY_FILE (file);
338
339 /* Find the entry in the table. */
340 search_key.callback = callback;
341 search_key.callback_data = callback_data;
342 desktop_callback = g_hash_table_lookup (desktop_file->details->callbacks, &search_key);
343 if (desktop_callback == NULL)
344 {
345 return;
346 }
347
348 /* Remove from the hash table before working with it. */
349 g_hash_table_remove (desktop_callback->desktop_file->details->callbacks, desktop_callback);
350
351 /* Tell the real directory to cancel the call. */
352 caja_directory_cancel_callback_internal
353 (file->details->directory, file,
354 NULL, ready_callback, desktop_callback);
355
356 caja_file_cancel_call_when_ready
357 (desktop_file->details->real_dir_file,
358 ready_callback, desktop_callback);
359
360 desktop_callback_destroy (desktop_callback);
361 }
362
363 static gboolean
real_check_if_ready(CajaFile * file,CajaFileAttributes attributes)364 real_check_if_ready (CajaFile *file,
365 CajaFileAttributes attributes)
366 {
367 return caja_directory_check_if_ready_internal
368 (file->details->directory, file,
369 attributes);
370 }
371
372 static gboolean
desktop_directory_file_check_if_ready(CajaFile * file,CajaFileAttributes attributes)373 desktop_directory_file_check_if_ready (CajaFile *file,
374 CajaFileAttributes attributes)
375 {
376 CajaFileAttributes delegated_attributes, non_delegated_attributes;
377 CajaDesktopDirectoryFile *desktop_file;
378
379 desktop_file = CAJA_DESKTOP_DIRECTORY_FILE (file);
380
381 partition_attributes (attributes,
382 &delegated_attributes,
383 &non_delegated_attributes);
384
385 return real_check_if_ready (file, non_delegated_attributes) &&
386 caja_file_check_if_ready (desktop_file->details->real_dir_file,
387 delegated_attributes);
388 }
389
390 static gboolean
desktop_directory_file_get_item_count(CajaFile * file,guint * count,gboolean * count_unreadable)391 desktop_directory_file_get_item_count (CajaFile *file,
392 guint *count,
393 gboolean *count_unreadable)
394 {
395 CajaDesktopDirectoryFile *desktop_file;
396 gboolean got_count;
397
398 desktop_file = CAJA_DESKTOP_DIRECTORY_FILE (file);
399
400 got_count = caja_file_get_directory_item_count (desktop_file->details->real_dir_file,
401 count,
402 count_unreadable);
403
404 if (count)
405 {
406 *count += g_list_length (file->details->directory->details->file_list);
407 }
408
409 return got_count;
410 }
411
412 static CajaRequestStatus
desktop_directory_file_get_deep_counts(CajaFile * file,guint * directory_count,guint * file_count,guint * unreadable_directory_count,goffset * total_size,goffset * total_size_on_disk)413 desktop_directory_file_get_deep_counts (CajaFile *file,
414 guint *directory_count,
415 guint *file_count,
416 guint *unreadable_directory_count,
417 goffset *total_size,
418 goffset *total_size_on_disk)
419 {
420 CajaDesktopDirectoryFile *desktop_file;
421 CajaRequestStatus status;
422
423 desktop_file = CAJA_DESKTOP_DIRECTORY_FILE (file);
424
425 status = caja_file_get_deep_counts (desktop_file->details->real_dir_file,
426 directory_count,
427 file_count,
428 unreadable_directory_count,
429 total_size,
430 total_size_on_disk,
431 TRUE);
432
433 if (file_count)
434 {
435 *file_count += g_list_length (file->details->directory->details->file_list);
436 }
437
438 return status;
439 }
440
441 static gboolean
desktop_directory_file_get_date(CajaFile * file,CajaDateType date_type,time_t * date)442 desktop_directory_file_get_date (CajaFile *file,
443 CajaDateType date_type,
444 time_t *date)
445 {
446 CajaDesktopDirectoryFile *desktop_file;
447
448 desktop_file = CAJA_DESKTOP_DIRECTORY_FILE (file);
449
450 return caja_file_get_date (desktop_file->details->real_dir_file,
451 date_type,
452 date);
453 }
454
455 static char *
desktop_directory_file_get_where_string(CajaFile * file)456 desktop_directory_file_get_where_string (CajaFile *file)
457 {
458 return g_strdup (_("on the desktop"));
459 }
460
461
462 static void
monitor_destroy(gpointer data)463 monitor_destroy (gpointer data)
464 {
465 DesktopMonitor *monitor = data;
466
467 caja_file_monitor_remove
468 (CAJA_FILE (monitor->desktop_file->details->real_dir_file), monitor);
469 g_free (monitor);
470 }
471
472 static void
caja_desktop_directory_file_set_metadata(CajaFile * file,const char * key,const char * value)473 caja_desktop_directory_file_set_metadata (CajaFile *file,
474 const char *key,
475 const char *value)
476 {
477 caja_desktop_set_metadata_string (file, "directory", key, value);
478 }
479
480 static void
caja_desktop_directory_file_set_metadata_as_list(CajaFile * file,const char * key,char ** value)481 caja_desktop_directory_file_set_metadata_as_list (CajaFile *file,
482 const char *key,
483 char **value)
484 {
485 caja_desktop_set_metadata_stringv (file, "directory", key, (const gchar **) value);
486 }
487
488 static void
caja_desktop_directory_file_init(gpointer object,gpointer klass)489 caja_desktop_directory_file_init (gpointer object, gpointer klass)
490 {
491 CajaDesktopDirectoryFile *desktop_file;
492 CajaDesktopDirectory *desktop_directory;
493 CajaDirectory *real_dir;
494 CajaFile *real_dir_file;
495
496 desktop_file = CAJA_DESKTOP_DIRECTORY_FILE (object);
497
498 desktop_directory = CAJA_DESKTOP_DIRECTORY (caja_directory_get_by_uri (EEL_DESKTOP_URI));
499
500 desktop_file->details = g_new0 (CajaDesktopDirectoryFileDetails, 1);
501 desktop_file->details->desktop_directory = desktop_directory;
502
503 desktop_file->details->callbacks = g_hash_table_new
504 (desktop_callback_hash, desktop_callback_equal);
505 desktop_file->details->monitors = g_hash_table_new_full (NULL, NULL,
506 NULL, monitor_destroy);
507
508 real_dir = caja_desktop_directory_get_real_directory (desktop_directory);
509 real_dir_file = caja_directory_get_corresponding_file (real_dir);
510 caja_directory_unref (real_dir);
511
512 desktop_file->details->real_dir_file = real_dir_file;
513
514 g_signal_connect_object (real_dir_file, "changed",
515 G_CALLBACK (real_file_changed_callback), desktop_file, 0);
516 }
517
518
519 static void
desktop_callback_remove_file_cover(gpointer key,gpointer value,gpointer callback_data)520 desktop_callback_remove_file_cover (gpointer key,
521 gpointer value,
522 gpointer callback_data)
523 {
524 desktop_callback_remove_file
525 (value, CAJA_FILE (callback_data));
526 }
527
528
529 static void
desktop_finalize(GObject * object)530 desktop_finalize (GObject *object)
531 {
532 CajaDesktopDirectoryFile *desktop_file;
533 CajaDesktopDirectory *desktop_directory;
534
535 desktop_file = CAJA_DESKTOP_DIRECTORY_FILE (object);
536 desktop_directory = desktop_file->details->desktop_directory;
537
538 /* Todo: ghash now safe? */
539 eel_g_hash_table_safe_for_each
540 (desktop_file->details->callbacks,
541 desktop_callback_remove_file_cover,
542 desktop_file->details->real_dir_file);
543
544 if (g_hash_table_size (desktop_file->details->callbacks) != 0)
545 {
546 g_warning ("call_when_ready still pending when desktop virtual file is destroyed");
547 }
548
549 g_hash_table_destroy (desktop_file->details->callbacks);
550 g_hash_table_destroy (desktop_file->details->monitors);
551
552 caja_file_unref (desktop_file->details->real_dir_file);
553
554 g_free (desktop_file->details);
555
556 caja_directory_unref (CAJA_DIRECTORY (desktop_directory));
557
558 EEL_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
559 }
560
561 static void
caja_desktop_directory_file_class_init(gpointer klass)562 caja_desktop_directory_file_class_init (gpointer klass)
563 {
564 GObjectClass *object_class;
565 CajaFileClass *file_class;
566
567 object_class = G_OBJECT_CLASS (klass);
568 file_class = CAJA_FILE_CLASS (klass);
569
570 object_class->finalize = desktop_finalize;
571
572 file_class->default_file_type = G_FILE_TYPE_DIRECTORY;
573
574 file_class->monitor_add = desktop_directory_file_monitor_add;
575 file_class->monitor_remove = desktop_directory_file_monitor_remove;
576 file_class->call_when_ready = desktop_directory_file_call_when_ready;
577 file_class->cancel_call_when_ready = desktop_directory_file_cancel_call_when_ready;
578 file_class->check_if_ready = desktop_directory_file_check_if_ready;
579 file_class->get_item_count = desktop_directory_file_get_item_count;
580 file_class->get_deep_counts = desktop_directory_file_get_deep_counts;
581 file_class->get_date = desktop_directory_file_get_date;
582 file_class->get_where_string = desktop_directory_file_get_where_string;
583 file_class->set_metadata = caja_desktop_directory_file_set_metadata;
584 file_class->set_metadata_as_list = caja_desktop_directory_file_set_metadata_as_list;
585 }
586