1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: 8; c-basic-offset: 8 -*- */
3 /* vfolder-desktop-method.c
5 Copyright (C) 2001 Red Hat, Inc.
6 Copyright (C) 2001 The Dark Prince
8 The Gnome Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library 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 The Gnome Library 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 Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with the Gnome Library; see the file COPYING.LIB. If not,
20 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
24 /* URI scheme for reading the "applications:" vfolder and other
25 * vfolder schemes. Lots of code stolen from the original desktop
38 #include <sys/types.h>
49 #include <libxml/parser.h>
50 #include <libxml/tree.h>
51 #include <libxml/xmlmemory.h>
53 #include <libgnomevfs/gnome-vfs-mime.h>
55 #include <libgnomevfs/gnome-vfs-module.h>
56 #include <libgnomevfs/gnome-vfs-method.h>
57 #include <libgnomevfs/gnome-vfs-utils.h>
58 #include <libgnomevfs/gnome-vfs-ops.h>
59 #include <libgnomevfs/gnome-vfs-module-shared.h>
60 #include <libgnomevfs/gnome-vfs-monitor-private.h>
62 #define DOT_GNOME ".gnome2"
64 typedef struct _VFolderInfo VFolderInfo;
65 typedef struct _Query Query;
66 typedef struct _QueryKeyword QueryKeyword;
67 typedef struct _QueryFilename QueryFilename;
68 typedef struct _Entry Entry;
69 typedef struct _Folder Folder;
70 typedef struct _EntryFile EntryFile;
71 typedef struct _Keyword Keyword;
72 typedef struct _FileMonitorHandle FileMonitorHandle;
73 typedef struct _StatLoc StatLoc;
74 typedef struct _VFolderURI VFolderURI;
76 /* TODO before 2.0: */
77 /* FIXME: also check/monitor desktop_dirs like we do the vfolder
78 * file and the item dirs */
79 /* FIXME: check if thread locks are not completely on crack which
80 * is likely given my experience with threads */
81 /* FIXME: use filename locking, currently we are full of races if
82 * multiple processes write to this filesystem */
83 /* FIXME: implement monitors */
85 /* TODO for later (star trek future): */
86 /* FIXME: Maybe when chaining to file:, we should call the gnome-vfs wrapper
87 * functions, instead of the file: methods directly. */
88 /* FIXME: related to the above: we should support things being on non
89 * file: filesystems. Such as having the vfolder info file on http
90 * somewhere or some such nonsense :) */
92 static GnomeVFSMethod *parent_method = NULL;
94 static GHashTable *infos = NULL;
96 /* Note: I have no clue about how to write thread safe code and this
97 * is my first attempt, so it's probably wrong
99 G_LOCK_DEFINE_STATIC (vfolder_lock);
101 /* Note: all keywords are quarks */
102 /* Note: basenames are unique */
104 #define UNSUPPORTED_INFO_FIELDS (GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS | \
105 GNOME_VFS_FILE_INFO_FIELDS_DEVICE | \
106 GNOME_VFS_FILE_INFO_FIELDS_INODE | \
107 GNOME_VFS_FILE_INFO_FIELDS_LINK_COUNT | \
108 GNOME_VFS_FILE_INFO_FIELDS_ATIME)
124 struct _QueryKeyword {
130 struct _QueryFilename {
144 int alloc; /* not really useful for folders,
145 but oh well, whatever. It's the number
146 of times this is queried in some directory,
147 used for the Unallocated query type */
160 gboolean implicit_keywords; /* the keywords were added by us */
168 char *desktop_file; /* the .directory file */
172 /* The following is for per file
174 /* excluded by filename */
175 GHashTable *excludes;
176 /* included by filename */
178 GHashTable *includes_ht;
184 gboolean dont_show_if_empty;
185 gboolean only_unallocated; /* include only unallocated items */
187 /* lazily done, will run query only when it
197 gboolean trigger_next; /* if true, next check will fail */
198 char name[1]; /* the structure will be long enough to
202 struct _VFolderInfo {
207 time_t user_filename_last_write;
208 char *desktop_dir; /* directory with .directorys */
209 char *user_desktop_dir; /* directory with .directorys */
210 gboolean user_file_active; /* if using user_filename and
214 char *user_item_dir; /* dir where user changes to
217 /* old style dirs to merge in */
220 /* if entries are valid, else
221 * they need to be (re)read */
222 gboolean entries_valid;
226 /* entry hash by basename */
227 GHashTable *entries_ht;
229 /* The root folder */
232 /* The unallocated folders, the folder which only
233 * include unallocated items */
234 GSList *unallocated_folders;
243 /* change monitoring stuff */
244 GnomeVFSMonitorHandle *filename_monitor;
245 GnomeVFSMonitorHandle *user_filename_monitor;
247 /* stat locations (in case we aren't monitoring) */
248 StatLoc *filename_statloc;
249 StatLoc *user_filename_statloc;
251 /* for .directory dirs */
252 /* FIXME: */GnomeVFSMonitorHandle *desktop_dir_monitor;
253 /* FIXME: */GnomeVFSMonitorHandle *user_desktop_dir_monitor;
255 /* stat locations (in case we aren't monitoring) */
256 /* FIXME: */StatLoc *desktop_dir_statloc;
257 /* FIXME: */StatLoc *user_desktop_dir_statloc;
259 /* FIXME: */GSList *file_monitors; /* FileMonitorHandle */
260 /* FIXME: */GSList *free_file_monitors; /* FileMonitorHandle */
261 GSList *folder_monitors; /* FileMonitorHandle */
262 GSList *free_folder_monitors; /* FileMonitorHandle */
264 GSList *item_dir_monitors; /* GnomeVFSMonitorHandle */
266 /* item dirs to stat */
269 /* ctime for folders */
270 time_t modification_time;
275 struct _FileMonitorHandle {
278 gboolean dir_monitor; /* TRUE if dir, FALSE if file */
280 GnomeVFSMonitorHandle *handle; /* A real handle if we're monitoring
281 an actual file here, or NULL */
282 char *filename; /* Just the basename, used in the free_file_list */
283 gboolean is_directory_file;
288 gboolean is_all_scheme;
289 gboolean ends_in_slash;
296 static Entry * entry_ref (Entry *entry);
297 static Entry * entry_ref_alloc (Entry *entry);
298 static void entry_unref (Entry *entry);
299 static void entry_unref_dealloc (Entry *entry);
300 static void query_destroy (Query *query);
301 static void ensure_folder (VFolderInfo *info,
305 gboolean ignore_unallocated);
306 static void ensure_folder_unlocked (VFolderInfo *info,
310 gboolean ignore_unallocated);
311 /* An EVIL function for quick reading of .desktop files,
312 * only reads in one or two keys, but that's ALL we need */
313 static void readitem_entry (const char *filename,
318 static gboolean vfolder_info_reload (VFolderInfo *info,
319 GnomeVFSResult *result,
320 GnomeVFSContext *context,
321 gboolean force_read_items);
322 static gboolean vfolder_info_reload_unlocked (VFolderInfo *info,
323 GnomeVFSResult *result,
324 GnomeVFSContext *context,
325 gboolean force_read_items);
326 static void invalidate_folder_subfolders (Folder *folder,
327 gboolean lock_taken);
328 static Folder * resolve_folder (VFolderInfo *info,
330 gboolean ignore_basename,
331 GnomeVFSResult *result,
332 GnomeVFSContext *context);
333 static gboolean vfolder_info_read_items (VFolderInfo *info,
334 GnomeVFSResult *result,
335 GnomeVFSContext *context);
337 /* assumes vuri->path already set */
339 vfolder_uri_parse_internal (GnomeVFSURI *uri, VFolderURI *vuri)
341 vuri->scheme = (gchar *) gnome_vfs_uri_get_scheme (uri);
343 vuri->ends_in_slash = FALSE;
345 if (strncmp (vuri->scheme, "all-", strlen ("all-")) == 0) {
346 vuri->scheme += strlen ("all-");
347 vuri->is_all_scheme = TRUE;
349 vuri->is_all_scheme = FALSE;
351 if (vuri->path != NULL) {
352 int last_slash = strlen (vuri->path) - 1;
355 /* Note: This handling of paths is somewhat evil, may need a
358 /* kill leading slashes, that is make sure there is
360 for (first = vuri->path; *first == '/'; first++)
362 if (first != vuri->path) {
367 /* kill trailing slashes (leave first if all slashes) */
368 while (last_slash > 0 && vuri->path [last_slash] == '/') {
369 vuri->path [last_slash--] = '\0';
370 vuri->ends_in_slash = TRUE;
373 /* get basename start */
374 while (last_slash >= 0 && vuri->path [last_slash] != '/')
378 vuri->file = vuri->path + last_slash + 1;
380 vuri->file = vuri->path;
382 if (vuri->file[0] == '\0' &&
383 strcmp (vuri->path, "/") == 0) {
387 vuri->ends_in_slash = TRUE;
397 #define VFOLDER_URI_PARSE(_uri, _vuri) { \
399 path = gnome_vfs_unescape_string ((_uri)->text, G_DIR_SEPARATOR_S); \
400 if (path != NULL) { \
401 (_vuri)->path = g_alloca (strlen (path) + 1); \
402 strcpy ((_vuri)->path, path); \
405 (_vuri)->path = NULL; \
407 vfolder_uri_parse_internal ((_uri), (_vuri)); \
410 static FileMonitorHandle *
411 file_monitor_handle_ref_unlocked (FileMonitorHandle *h)
418 file_monitor_handle_unref_unlocked (FileMonitorHandle *h)
421 if (h->refcount == 0) {
422 gnome_vfs_uri_unref (h->uri);
425 g_free (h->filename);
428 if (h->handle != NULL) {
429 gnome_vfs_monitor_cancel (h->handle);
435 /* This includes the .directory files */
437 emit_monitor (Folder *folder, int type)
440 for (li = ((Entry *)folder)->monitors;
443 FileMonitorHandle *handle = li->data;
444 gnome_vfs_monitor_callback ((GnomeVFSMethodHandle *) handle,
450 emit_file_deleted_monitor (VFolderInfo *info, Entry *entry, Folder *folder)
453 for (li = entry->monitors;
458 GnomeVFSResult result;
459 FileMonitorHandle *handle = li->data;
461 /* Evil! EVIL URI PARSING. this will eat a lot of
462 * stack if we have lots of monitors */
464 VFOLDER_URI_PARSE (handle->uri, &vuri);
466 f = resolve_folder (info,
468 TRUE /* ignore_basename */,
473 gnome_vfs_monitor_callback
474 ((GnomeVFSMethodHandle *) handle,
476 GNOME_VFS_MONITOR_EVENT_DELETED);
481 emit_and_delete_monitor (VFolderInfo *info, Folder *folder)
484 for (li = ((Entry *)folder)->monitors;
487 FileMonitorHandle *handle = li->data;
490 gnome_vfs_monitor_callback ((GnomeVFSMethodHandle *) handle,
492 GNOME_VFS_MONITOR_EVENT_DELETED);
494 if (handle->dir_monitor)
495 info->free_folder_monitors =
496 g_slist_prepend (info->free_folder_monitors,
499 info->free_file_monitors =
500 g_slist_prepend (info->free_file_monitors,
503 g_slist_free (((Entry *)folder)->monitors);
504 ((Entry *)folder)->monitors = NULL;
508 check_ext (const char *name, const char *ext_check)
512 ext = strrchr (name, '.');
514 strcmp (ext, ext_check) != 0)
521 bake_statloc (const char *name,
526 if (stat (name, &s) != 0) {
527 if (errno == ENOENT) {
528 sl = g_malloc0 (sizeof (StatLoc) +
530 sl->last_stat = curtime;
532 sl->trigger_next = FALSE;
533 strcpy (sl->name, name);
538 sl = g_malloc0 (sizeof (StatLoc) +
540 sl->last_stat = curtime;
541 sl->ctime = s.st_ctime;
542 sl->trigger_next = FALSE;
543 strcpy (sl->name, name);
548 /* returns FALSE if we must reread */
550 check_statloc (StatLoc *sl,
555 if (sl->trigger_next) {
556 sl->trigger_next = FALSE;
560 /* don't stat more then once every 3 seconds */
561 if (curtime <= sl->last_stat + 3)
564 sl->last_stat = curtime;
566 if (stat (sl->name, &s) != 0) {
567 if (errno == ENOENT &&
574 if (sl->ctime == s.st_ctime)
581 ensure_dir (const char *dirname, gboolean ignore_basename)
589 parsed = g_path_get_dirname (dirname);
591 parsed = g_strdup (dirname);
593 if (g_file_test (parsed, G_FILE_TEST_IS_DIR)) {
598 p = strchr (parsed, '/');
600 p = strchr (p+1, '/');
604 if (mkdir (parsed, 0700) != 0 &&
610 p = strchr (p+1, '/');
613 if (mkdir (parsed, 0700) != 0 &&
623 /* check for any directory name other then root */
625 any_subdir (const char *dirname)
631 for (p = dirname; *p != '\0'; p++) {
640 destroy_entry_file (EntryFile *efile)
645 g_free (efile->filename);
646 efile->filename = NULL;
648 g_slist_free (efile->keywords);
649 efile->keywords = NULL;
655 destroy_folder (Folder *folder)
662 if (folder->parent != NULL) {
663 folder->parent->subfolders =
664 g_slist_remove (folder->parent->subfolders, folder);
665 folder->parent->up_to_date = FALSE;
666 folder->parent = NULL;
669 g_free (folder->desktop_file);
670 folder->desktop_file = NULL;
672 query_destroy (folder->query);
673 folder->query = NULL;
675 if (folder->excludes != NULL) {
676 g_hash_table_destroy (folder->excludes);
677 folder->excludes = NULL;
680 g_slist_foreach (folder->includes, (GFunc)g_free, NULL);
681 g_slist_free (folder->includes);
682 folder->includes = NULL;
683 if (folder->includes_ht != NULL) {
684 g_hash_table_destroy (folder->includes_ht);
685 folder->includes_ht = NULL;
688 list = folder->subfolders;
689 folder->subfolders = NULL;
690 g_slist_foreach (list, (GFunc)entry_unref, NULL);
693 list = folder->entries;
694 folder->entries = NULL;
695 g_slist_foreach (list, (GFunc)entry_unref, NULL);
702 entry_ref (Entry *entry)
710 entry_ref_alloc (Entry *entry)
721 entry_unref (Entry *entry)
728 if (entry->refcount == 0) {
729 g_free (entry->name);
732 g_slist_foreach (entry->monitors,
733 (GFunc)file_monitor_handle_unref_unlocked,
735 g_slist_free (entry->monitors);
736 entry->monitors = NULL;
738 if (entry->type == ENTRY_FILE)
739 destroy_entry_file ((EntryFile *)entry);
740 else /* ENTRY_FOLDER */
741 destroy_folder ((Folder *)entry);
746 entry_unref_dealloc (Entry *entry)
754 /* Handles ONLY files, not dirs */
755 /* Also allocates the entries as well as refs them */
757 alloc_entries_from_files (VFolderInfo *info, GSList *filenames)
763 for (li = filenames; li != NULL; li = li->next) {
764 char *filename = li->data;
765 GSList *entry_list = g_hash_table_lookup (info->entries_ht, filename);
766 if (entry_list != NULL)
767 files = g_slist_prepend (files,
768 entry_ref_alloc (entry_list->data));
775 matches_query (VFolderInfo *info,
785 #define INVERT_IF_NEEDED(val) (query->not ? !(val) : (val))
786 switch (query->type) {
788 for (li = query->queries; li != NULL; li = li->next) {
789 Query *subquery = li->data;
790 if (matches_query (info, folder, efile, subquery))
791 return INVERT_IF_NEEDED (TRUE);
793 return INVERT_IF_NEEDED (FALSE);
795 for (li = query->queries; li != NULL; li = li->next) {
796 Query *subquery = li->data;
797 if ( ! matches_query (info, folder, efile, subquery))
798 return INVERT_IF_NEEDED (FALSE);
800 return INVERT_IF_NEEDED (TRUE);
803 QueryKeyword *qkeyword = (QueryKeyword *)query;
804 for (li = efile->keywords; li != NULL; li = li->next) {
805 GQuark keyword = GPOINTER_TO_INT (li->data);
806 if (keyword == qkeyword->keyword)
807 return INVERT_IF_NEEDED (TRUE);
809 return INVERT_IF_NEEDED (FALSE);
813 QueryFilename *qfilename = (QueryFilename *)query;
814 if (strcmp (qfilename->filename, ((Entry *)efile)->name) == 0) {
815 return INVERT_IF_NEEDED (TRUE);
817 return INVERT_IF_NEEDED (FALSE);
821 #undef INVERT_IF_NEEDED
822 g_assert_not_reached ();
828 dump_unallocated_folders (Folder *folder)
831 for (li = folder->subfolders; li != NULL; li = li->next)
832 dump_unallocated_folders (li->data);
834 if (folder->only_unallocated &&
835 folder->entries != NULL) {
836 g_slist_foreach (folder->entries,
837 (GFunc)entry_unref_dealloc, NULL);
838 g_slist_free (folder->entries);
839 folder->entries = NULL;
843 /* Run query, allocs and refs the entries */
845 append_query (VFolderInfo *info, Folder *folder)
849 if (folder->query == NULL &&
850 ! folder->only_unallocated)
853 if (folder->only_unallocated) {
854 /* dump all folders that use unallocated
855 * items only. This sucks if you keep
856 * reading one and then another such
857 * folder, but oh well, life sucks for
858 * you then, but at least you get
859 * consistent results */
860 dump_unallocated_folders (info->root);
862 /* ensure all other folders, so that
863 * after this we know which ones are
865 ensure_folder_unlocked (info,
867 TRUE /* subfolders */,
869 /* avoid infinite loops */
870 TRUE /* ignore_unallocated */);
873 for (li = info->entries; li != NULL; li = li->next) {
874 Entry *entry = li->data;
876 if (/* if not file */
877 entry->type != ENTRY_FILE ||
878 /* if already included */
879 (folder->includes_ht != NULL &&
880 g_hash_table_lookup (folder->includes_ht,
881 entry->name) != NULL))
884 if (folder->only_unallocated &&
888 if (matches_query (info, folder, (EntryFile *)entry,
890 folder->entries = g_slist_prepend
891 (folder->entries, entry_ref_alloc (entry));
895 /* get entries in folder */
896 /* FIXME: support cancellation here */
898 ensure_folder_unlocked (VFolderInfo *info,
902 gboolean ignore_unallocated)
906 for (li = folder->subfolders; li != NULL; li = li->next)
907 ensure_folder_unlocked (info, li->data, subfolders,
908 except, ignore_unallocated);
911 if (except == folder)
914 if (ignore_unallocated &&
915 folder->only_unallocated)
918 if (folder->up_to_date)
921 if (folder->entries != NULL) {
922 g_slist_foreach (folder->entries,
923 (GFunc)entry_unref_dealloc, NULL);
924 g_slist_free (folder->entries);
925 folder->entries = NULL;
928 /* Include includes */
929 folder->entries = alloc_entries_from_files (info, folder->includes);
932 append_query (info, folder);
934 /* We were prepending all this time */
935 folder->entries = g_slist_reverse (folder->entries);
937 /* Include subfolders */
938 /* we always whack them onto the beginning */
939 if (folder->subfolders != NULL) {
940 GSList *subfolders = g_slist_copy (folder->subfolders);
941 g_slist_foreach (subfolders, (GFunc)entry_ref_alloc, NULL);
942 folder->entries = g_slist_concat (subfolders, folder->entries);
945 /* Exclude excludes */
946 if (folder->excludes != NULL) {
948 GSList *entries = folder->entries;
949 folder->entries = NULL;
950 for (li = entries; li != NULL; li = li->next) {
951 Entry *entry = li->data;
952 if (g_hash_table_lookup (folder->excludes, entry->name) == NULL)
953 folder->entries = g_slist_prepend (folder->entries, entry);
955 entry_unref_dealloc (entry);
958 g_slist_free (entries);
960 /* to preserve the Folders then everything else order */
961 folder->entries = g_slist_reverse (folder->entries);
964 folder->up_to_date = TRUE;
966 folder->sorted = FALSE;
970 ensure_folder (VFolderInfo *info,
974 gboolean ignore_unallocated)
976 G_LOCK (vfolder_lock);
977 ensure_folder_unlocked (info, folder, subfolders, except, ignore_unallocated);
978 G_UNLOCK (vfolder_lock);
982 get_directory_file_unlocked (VFolderInfo *info, Folder *folder)
986 /* FIXME: cache dir_files */
988 if (folder->desktop_file == NULL)
991 if (folder->desktop_file[0] == G_DIR_SEPARATOR)
992 return g_strdup (folder->desktop_file);
994 /* Now try the user directory */
995 if (info->user_desktop_dir != NULL) {
996 filename = g_build_filename (info->user_desktop_dir,
997 folder->desktop_file,
999 if (access (filename, F_OK) == 0) {
1006 filename = g_build_filename (info->desktop_dir, folder->desktop_file, NULL);
1007 if (access (filename, F_OK) == 0) {
1016 get_directory_file (VFolderInfo *info, Folder *folder)
1020 G_LOCK (vfolder_lock);
1021 ret = get_directory_file_unlocked (info, folder);
1022 G_UNLOCK (vfolder_lock);
1028 get_sort_order (VFolderInfo *info, Folder *folder)
1036 filename = get_directory_file_unlocked (info, folder);
1037 if (filename == NULL)
1041 readitem_entry (filename,
1051 parsed = g_strsplit (order, ":", -1);
1056 for (i = 0; parsed[i] != NULL; i++) {
1057 char *word = parsed[i];
1061 if (word[0] == '\0') {
1065 list = g_slist_prepend (list, word);
1067 /* we've stolen all strings from it */
1070 return g_slist_reverse (list);
1073 /* get entries in folder */
1075 ensure_folder_sort (VFolderInfo *info, Folder *folder)
1077 GSList *li, *sort_order;
1079 GHashTable *entry_hash;
1081 ensure_folder (info, folder,
1082 FALSE /* subfolders */,
1084 FALSE /* ignore_unallocated */);
1088 G_LOCK (vfolder_lock);
1090 sort_order = get_sort_order (info, folder);
1091 if (sort_order == NULL) {
1092 folder->sorted = TRUE;
1093 G_UNLOCK (vfolder_lock);
1097 entries = folder->entries;
1098 folder->entries = NULL;
1100 entry_hash = g_hash_table_new (g_str_hash, g_str_equal);
1101 for (li = entries; li != NULL; li = li->next) {
1102 Entry *entry = li->data;
1103 g_hash_table_insert (entry_hash, entry->name, li);
1106 for (li = sort_order; li != NULL; li = li->next) {
1107 char *word = li->data;
1111 /* we kill the words here */
1114 entry_list = g_hash_table_lookup (entry_hash, word);
1117 if (entry_list == NULL)
1120 entry = entry_list->data;
1122 entries = g_slist_delete_link (entries, entry_list);
1124 folder->entries = g_slist_prepend (folder->entries,
1128 /* put on those that weren't mentioned in the sort */
1129 for (li = entries; li != NULL; li = li->next) {
1130 Entry *entry = li->data;
1132 folder->entries = g_slist_prepend (folder->entries,
1136 g_hash_table_destroy (entry_hash);
1137 g_slist_free (entries);
1138 g_slist_free (sort_order);
1140 folder->sorted = TRUE;
1142 G_UNLOCK (vfolder_lock);
1146 file_new (const char *name)
1148 EntryFile *efile = g_new0 (EntryFile, 1);
1150 efile->entry.type = ENTRY_FILE;
1151 efile->entry.name = g_strdup (name);
1152 efile->entry.refcount = 1;
1158 folder_new (const char *name)
1160 Folder *folder = g_new0 (Folder, 1);
1162 folder->entry.type = ENTRY_FOLDER;
1163 folder->entry.name = g_strdup (name);
1164 folder->entry.refcount = 1;
1170 query_new (int type)
1174 if (type == QUERY_KEYWORD)
1175 query = (Query *)g_new0 (QueryKeyword, 1);
1176 else if (type == QUERY_FILENAME)
1177 query = (Query *)g_new0 (QueryFilename, 1);
1179 query = g_new0 (Query, 1);
1187 query_destroy (Query *query)
1192 if (query->type == QUERY_FILENAME) {
1193 QueryFilename *qfile = (QueryFilename *)query;
1194 g_free (qfile->filename);
1195 qfile->filename = NULL;
1196 } else if (query->type == QUERY_OR ||
1197 query->type == QUERY_AND) {
1198 g_slist_foreach (query->queries, (GFunc)query_destroy, NULL);
1199 g_slist_free (query->queries);
1200 query->queries = NULL;
1207 add_folder_monitor_unlocked (VFolderInfo *info,
1208 FileMonitorHandle *handle)
1211 GnomeVFSResult result;
1214 VFOLDER_URI_PARSE (handle->uri, &vuri);
1216 file_monitor_handle_ref_unlocked (handle);
1218 info->folder_monitors =
1219 g_slist_prepend (info->folder_monitors, handle);
1221 folder = resolve_folder (info,
1223 FALSE /* ignore_basename */,
1227 if (folder == NULL) {
1228 file_monitor_handle_ref_unlocked (handle);
1230 info->free_folder_monitors =
1231 g_slist_prepend (info->free_folder_monitors, handle);
1233 if (handle->exists) {
1234 handle->exists = FALSE;
1235 gnome_vfs_monitor_callback
1236 ((GnomeVFSMethodHandle *)handle,
1238 GNOME_VFS_MONITOR_EVENT_DELETED);
1241 file_monitor_handle_ref_unlocked (handle);
1243 ((Entry *)folder)->monitors =
1244 g_slist_prepend (((Entry *)folder)->monitors, handle);
1246 if ( ! handle->exists) {
1247 handle->exists = TRUE;
1248 gnome_vfs_monitor_callback
1249 ((GnomeVFSMethodHandle *)handle,
1251 GNOME_VFS_MONITOR_EVENT_CREATED);
1258 invalidate_folder_T (Folder *folder)
1260 folder->up_to_date = FALSE;
1262 invalidate_folder_subfolders (folder, TRUE);
1266 invalidate_folder (Folder *folder)
1268 G_LOCK (vfolder_lock);
1269 folder->up_to_date = FALSE;
1270 G_UNLOCK (vfolder_lock);
1272 invalidate_folder_subfolders (folder, FALSE);
1276 invalidate_folder_subfolders (Folder *folder,
1277 gboolean lock_taken)
1281 for (li = folder->subfolders; li != NULL; li = li->next) {
1282 Folder *subfolder = li->data;
1285 invalidate_folder (subfolder);
1287 invalidate_folder_T (subfolder);
1290 emit_monitor (folder, GNOME_VFS_MONITOR_EVENT_CHANGED);
1293 /* FIXME: this is UGLY!, we need to figure out when the file
1294 * got finished changing! */
1296 reread_timeout (gpointer data)
1298 VFolderInfo *info = data;
1299 gboolean force_read_items = info->file_monitors != NULL;
1300 vfolder_info_reload (info, NULL, NULL, force_read_items);
1305 queue_reread_in (VFolderInfo *info, int msec)
1307 G_LOCK (vfolder_lock);
1308 if (info->reread_queue != 0)
1309 g_source_remove (info->reread_queue);
1310 info->reread_queue = g_timeout_add (msec, reread_timeout, info);
1311 G_UNLOCK (vfolder_lock);
1315 vfolder_desktop_dir_monitor (GnomeVFSMonitorHandle *handle,
1316 const gchar *monitor_uri,
1317 const gchar *info_uri,
1318 GnomeVFSMonitorEventType event_type,
1321 /* FIXME: implement */
1325 vfolder_user_desktop_dir_monitor (GnomeVFSMonitorHandle *handle,
1326 const gchar *monitor_uri,
1327 const gchar *info_uri,
1328 GnomeVFSMonitorEventType event_type,
1331 /* FIXME: implement */
1335 vfolder_filename_monitor (GnomeVFSMonitorHandle *handle,
1336 const gchar *monitor_uri,
1337 const gchar *info_uri,
1338 GnomeVFSMonitorEventType event_type,
1341 VFolderInfo *info = user_data;
1343 if ((event_type == GNOME_VFS_MONITOR_EVENT_CREATED ||
1344 event_type == GNOME_VFS_MONITOR_EVENT_CHANGED) &&
1345 ! info->user_file_active) {
1346 queue_reread_in (info, 200);
1347 } else if (event_type == GNOME_VFS_MONITOR_EVENT_DELETED &&
1348 ! info->user_file_active) {
1349 /* FIXME: is this correct? I mean now
1350 * there probably isn't ANY vfolder file, so we
1351 * init to default values really. I have no clue what's
1353 vfolder_info_reload (info, NULL, NULL,
1354 TRUE /* force read items */);
1359 vfolder_user_filename_monitor (GnomeVFSMonitorHandle *handle,
1360 const gchar *monitor_uri,
1361 const gchar *info_uri,
1362 GnomeVFSMonitorEventType event_type,
1365 VFolderInfo *info = user_data;
1367 if ((event_type == GNOME_VFS_MONITOR_EVENT_CREATED ||
1368 event_type == GNOME_VFS_MONITOR_EVENT_CHANGED) &&
1369 info->user_file_active) {
1372 /* see if this was really our own change */
1373 if (info->user_filename_last_write == time (NULL))
1375 /* anal retentive */
1376 if (stat (info->user_filename, &s) == 0 &&
1377 info->user_filename_last_write == s.st_ctime)
1380 queue_reread_in (info, 200);
1381 } else if ((event_type == GNOME_VFS_MONITOR_EVENT_CREATED ||
1382 event_type == GNOME_VFS_MONITOR_EVENT_CHANGED) &&
1383 ! info->user_file_active) {
1384 queue_reread_in (info, 200);
1385 } else if (event_type == GNOME_VFS_MONITOR_EVENT_DELETED &&
1386 info->user_file_active) {
1387 gboolean force_read_items = info->file_monitors != NULL;
1388 vfolder_info_reload (info, NULL, NULL, force_read_items);
1393 item_dir_monitor (GnomeVFSMonitorHandle *handle,
1394 const gchar *monitor_uri,
1395 const gchar *info_uri,
1396 GnomeVFSMonitorEventType event_type,
1399 VFolderInfo *info = user_data;
1401 if (event_type == GNOME_VFS_MONITOR_EVENT_CREATED ||
1402 event_type == GNOME_VFS_MONITOR_EVENT_CHANGED) {
1403 /* first invalidate all folders */
1404 invalidate_folder (info->root);
1405 /* second invalidate all entries */
1406 info->entries_valid = FALSE;
1408 if (info->file_monitors != NULL) {
1409 GnomeVFSResult result;
1412 /* Whack all monitors here! */
1413 for (li = info->file_monitors;
1416 FileMonitorHandle *h = li->data;
1417 if (h->handle != NULL)
1418 gnome_vfs_monitor_cancel (h->handle);
1422 if (vfolder_info_read_items (info, &result, NULL)) {
1423 info->entries_valid = TRUE;
1430 setup_dir_monitor (VFolderInfo *info, const char *dir, gboolean subdirs,
1431 GnomeVFSResult *result,
1432 GnomeVFSContext *context)
1434 GnomeVFSMonitorHandle *handle;
1439 uri = gnome_vfs_get_uri_from_local_path (dir);
1441 if (gnome_vfs_monitor_add (&handle,
1443 GNOME_VFS_MONITOR_DIRECTORY,
1445 info) != GNOME_VFS_OK) {
1446 StatLoc *sl = bake_statloc (dir, time (NULL));
1448 info->stat_dirs = g_slist_prepend (info->stat_dirs, sl);
1454 if (gnome_vfs_context_check_cancellation (context)) {
1455 gnome_vfs_monitor_cancel (handle);
1456 *result = GNOME_VFS_ERROR_CANCELLED;
1460 info->item_dir_monitors =
1461 g_slist_prepend (info->item_dir_monitors, handle);
1470 while ((de = readdir (dh)) != NULL) {
1473 if (gnome_vfs_context_check_cancellation (context)) {
1474 *result = GNOME_VFS_ERROR_CANCELLED;
1479 if (de->d_name[0] == '.')
1482 full_path = g_build_filename (dir, de->d_name, NULL);
1483 if (g_file_test (full_path, G_FILE_TEST_IS_DIR)) {
1484 if ( ! setup_dir_monitor (info, full_path,
1500 monitor_setup (VFolderInfo *info,
1501 gboolean setup_filenames,
1502 gboolean setup_itemdirs,
1503 gboolean setup_desktop_dirs,
1504 GnomeVFSResult *result,
1505 GnomeVFSContext *context)
1510 if (setup_filenames) {
1511 uri = gnome_vfs_get_uri_from_local_path
1514 if (gnome_vfs_monitor_add (&info->filename_monitor,
1516 GNOME_VFS_MONITOR_FILE,
1517 vfolder_filename_monitor,
1518 info) != GNOME_VFS_OK) {
1519 info->filename_monitor = NULL;
1520 info->filename_statloc = bake_statloc (info->filename,
1525 if (setup_filenames &&
1526 info->user_filename != NULL) {
1527 uri = gnome_vfs_get_uri_from_local_path
1528 (info->user_filename);
1529 if (gnome_vfs_monitor_add (&info->user_filename_monitor,
1531 GNOME_VFS_MONITOR_FILE,
1532 vfolder_user_filename_monitor,
1533 info) != GNOME_VFS_OK) {
1534 info->user_filename_monitor = NULL;
1535 info->user_filename_statloc =
1536 bake_statloc (info->user_filename,
1543 if (gnome_vfs_context_check_cancellation (context)) {
1544 *result = GNOME_VFS_ERROR_CANCELLED;
1548 if (setup_itemdirs) {
1549 for (li = info->item_dirs; li != NULL; li = li->next) {
1550 const char *dir = li->data;
1551 if ( ! setup_dir_monitor (info, dir,
1552 FALSE /* subdirs */,
1556 if (info->user_item_dir != NULL) {
1557 if ( ! setup_dir_monitor (info, info->user_item_dir,
1558 FALSE /* subdirs */,
1562 for (li = info->merge_dirs; li != NULL; li = li->next) {
1563 const char *dir = li->data;
1564 if ( ! setup_dir_monitor (info, dir,
1571 if (setup_desktop_dirs) {
1572 uri = gnome_vfs_get_uri_from_local_path
1573 (info->desktop_dir);
1575 if (gnome_vfs_monitor_add (&info->desktop_dir_monitor,
1577 GNOME_VFS_MONITOR_FILE,
1578 vfolder_desktop_dir_monitor,
1579 info) != GNOME_VFS_OK) {
1580 info->desktop_dir_monitor = NULL;
1581 info->desktop_dir_statloc =
1582 bake_statloc (info->desktop_dir,
1587 if (setup_desktop_dirs &&
1588 info->user_desktop_dir != NULL) {
1589 uri = gnome_vfs_get_uri_from_local_path
1590 (info->user_desktop_dir);
1591 if (gnome_vfs_monitor_add (&info->user_desktop_dir_monitor,
1593 GNOME_VFS_MONITOR_DIRECTORY,
1594 vfolder_user_desktop_dir_monitor,
1595 info) != GNOME_VFS_OK) {
1596 info->user_desktop_dir_monitor = NULL;
1597 info->user_desktop_dir_statloc =
1598 bake_statloc (info->user_desktop_dir,
1609 vfolder_info_init (VFolderInfo *info, const char *scheme)
1614 info->scheme = g_strdup (scheme);
1616 info->filename = g_strconcat (SYSCONFDIR,
1617 "/gnome-vfs-2.0/vfolders/",
1618 scheme, ".vfolder-info",
1620 info->user_filename = g_strconcat (g_get_home_dir (),
1621 "/" DOT_GNOME "/vfolders/",
1622 scheme, ".vfolder-info",
1624 info->desktop_dir = g_strconcat (SYSCONFDIR,
1625 "/gnome-vfs-2.0/vfolders/",
1627 info->user_desktop_dir = g_strconcat (g_get_home_dir (),
1628 "/" DOT_GNOME "/vfolders/",
1631 /* Init the desktop paths */
1633 list = g_slist_prepend (list, g_strdup ("/usr/share/applications/"));
1634 if (strcmp ("/usr/share/applications/", DATADIR "/applications/") != 0)
1635 list = g_slist_prepend (list, g_strdup (DATADIR "/applications/"));
1636 path = g_getenv ("DESKTOP_FILE_PATH");
1639 char **ppath = g_strsplit (path, ":", -1);
1640 for (i = 0; ppath[i] != NULL; i++) {
1641 const char *dir = ppath[i];
1642 list = g_slist_prepend (list, g_strdup (dir));
1646 info->item_dirs = g_slist_reverse (list);
1648 info->user_item_dir = g_strconcat (g_get_home_dir (),
1649 "/" DOT_GNOME "/vfolders/",
1653 info->entries_ht = g_hash_table_new (g_str_hash, g_str_equal);
1655 info->root = folder_new ("Root");
1657 info->modification_time = time (NULL);
1661 vfolder_info_free_internals_unlocked (VFolderInfo *info)
1666 if (info->filename_monitor != NULL) {
1667 gnome_vfs_monitor_cancel (info->filename_monitor);
1668 info->filename_monitor = NULL;
1671 if (info->user_filename_monitor != NULL) {
1672 gnome_vfs_monitor_cancel (info->user_filename_monitor);
1673 info->user_filename_monitor = NULL;
1676 g_free (info->filename_statloc);
1677 info->filename_statloc = NULL;
1679 g_free (info->user_filename_statloc);
1680 info->user_filename_statloc = NULL;
1683 if (info->desktop_dir_monitor != NULL) {
1684 gnome_vfs_monitor_cancel (info->desktop_dir_monitor);
1685 info->desktop_dir_monitor = NULL;
1688 if (info->user_desktop_dir_monitor != NULL) {
1689 gnome_vfs_monitor_cancel (info->user_desktop_dir_monitor);
1690 info->user_desktop_dir_monitor = NULL;
1693 g_free (info->desktop_dir_statloc);
1694 info->desktop_dir_statloc = NULL;
1696 g_free (info->user_desktop_dir_statloc);
1697 info->user_desktop_dir_statloc = NULL;
1700 g_slist_foreach (info->item_dir_monitors,
1701 (GFunc)gnome_vfs_monitor_cancel, NULL);
1702 g_slist_free (info->item_dir_monitors);
1703 info->item_dir_monitors = NULL;
1705 g_free (info->scheme);
1706 info->scheme = NULL;
1708 g_free (info->filename);
1709 info->filename = NULL;
1711 g_free (info->user_filename);
1712 info->user_filename = NULL;
1714 g_free (info->desktop_dir);
1715 info->desktop_dir = NULL;
1717 g_free (info->user_desktop_dir);
1718 info->user_desktop_dir = NULL;
1720 g_slist_foreach (info->item_dirs, (GFunc)g_free, NULL);
1721 g_slist_free (info->item_dirs);
1722 info->item_dirs = NULL;
1724 g_free (info->user_item_dir);
1725 info->user_item_dir = NULL;
1727 g_slist_foreach (info->merge_dirs, (GFunc)g_free, NULL);
1728 g_slist_free (info->merge_dirs);
1729 info->merge_dirs = NULL;
1731 g_slist_foreach (info->entries, (GFunc)entry_unref, NULL);
1732 g_slist_free (info->entries);
1733 info->entries = NULL;
1735 if (info->entries_ht != NULL)
1736 g_hash_table_destroy (info->entries_ht);
1737 info->entries_ht = NULL;
1739 g_slist_foreach (info->unallocated_folders,
1742 g_slist_free (info->unallocated_folders);
1743 info->unallocated_folders = NULL;
1745 entry_unref ((Entry *)info->root);
1748 g_slist_foreach (info->stat_dirs, (GFunc)g_free, NULL);
1749 g_slist_free (info->stat_dirs);
1750 info->stat_dirs = NULL;
1752 g_slist_foreach (info->folder_monitors,
1753 (GFunc)file_monitor_handle_unref_unlocked, NULL);
1754 g_slist_free (info->folder_monitors);
1755 info->folder_monitors = NULL;
1757 g_slist_foreach (info->free_folder_monitors,
1758 (GFunc)file_monitor_handle_unref_unlocked, NULL);
1759 g_slist_free (info->free_folder_monitors);
1760 info->free_folder_monitors = NULL;
1762 g_slist_foreach (info->file_monitors,
1763 (GFunc)file_monitor_handle_unref_unlocked, NULL);
1764 g_slist_free (info->file_monitors);
1765 info->file_monitors = NULL;
1767 g_slist_foreach (info->free_file_monitors,
1768 (GFunc)file_monitor_handle_unref_unlocked, NULL);
1769 g_slist_free (info->free_file_monitors);
1770 info->free_file_monitors = NULL;
1772 if (info->reread_queue != 0)
1773 g_source_remove (info->reread_queue);
1774 info->reread_queue = 0;
1778 vfolder_info_free_internals (VFolderInfo *info)
1780 G_LOCK (vfolder_lock);
1781 vfolder_info_free_internals_unlocked (info);
1782 G_UNLOCK (vfolder_lock);
1786 vfolder_info_destroy (VFolderInfo *info)
1788 vfolder_info_free_internals (info);
1793 single_query_read (xmlNode *qnode)
1798 if (qnode->type != XML_ELEMENT_NODE ||
1799 qnode->name == NULL)
1804 if (g_ascii_strcasecmp (qnode->name, "Not") == 0 &&
1805 qnode->xmlChildrenNode != NULL) {
1808 for (iter = qnode->xmlChildrenNode;
1809 iter != NULL && query == NULL;
1811 query = single_query_read (iter);
1812 if (query != NULL) {
1813 query->not = ! query->not;
1816 } else if (g_ascii_strcasecmp (qnode->name, "Keyword") == 0) {
1817 xmlChar *word = xmlNodeGetContent (qnode);
1819 query = query_new (QUERY_KEYWORD);
1820 ((QueryKeyword *)query)->keyword =
1821 g_quark_from_string (word);
1826 } else if (g_ascii_strcasecmp (qnode->name, "Filename") == 0) {
1827 xmlChar *file = xmlNodeGetContent (qnode);
1829 query = query_new (QUERY_FILENAME);
1830 ((QueryFilename *)query)->filename =
1836 } else if (g_ascii_strcasecmp (qnode->name, "And") == 0) {
1837 query = query_new (QUERY_AND);
1838 } else if (g_ascii_strcasecmp (qnode->name, "Or") == 0) {
1839 query = query_new (QUERY_OR);
1841 /* We don't understand */
1845 /* This must be OR or AND */
1846 g_assert (query != NULL);
1848 for (node = qnode->xmlChildrenNode; node != NULL; node = node->next) {
1849 Query *new_query = single_query_read (node);
1851 if (new_query != NULL)
1852 query->queries = g_slist_prepend
1853 (query->queries, new_query);
1856 query->queries = g_slist_reverse (query->queries);
1862 add_or_set_query (Query **query, Query *new_query)
1864 if (*query == NULL) {
1867 Query *old_query = *query;
1868 *query = query_new (QUERY_OR);
1870 g_slist_append ((*query)->queries, old_query);
1872 g_slist_append ((*query)->queries, new_query);
1877 query_read (xmlNode *qnode)
1884 for (node = qnode->xmlChildrenNode; node != NULL; node = node->next) {
1885 if (node->type != XML_ELEMENT_NODE ||
1889 if (g_ascii_strcasecmp (node->name, "Not") == 0 &&
1890 node->xmlChildrenNode != NULL) {
1892 Query *new_query = NULL;
1894 for (iter = node->xmlChildrenNode;
1895 iter != NULL && new_query == NULL;
1897 new_query = single_query_read (iter);
1898 if (new_query != NULL) {
1899 new_query->not = ! new_query->not;
1900 add_or_set_query (&query, new_query);
1903 Query *new_query = single_query_read (node);
1904 if (new_query != NULL)
1905 add_or_set_query (&query, new_query);
1913 folder_read (VFolderInfo *info, xmlNode *fnode)
1918 folder = folder_new (NULL);
1920 for (node = fnode->xmlChildrenNode; node != NULL; node = node->next) {
1921 if (node->type != XML_ELEMENT_NODE ||
1925 if (g_ascii_strcasecmp (node->name, "Name") == 0) {
1926 xmlChar *name = xmlNodeGetContent (node);
1928 g_free (folder->entry.name);
1929 folder->entry.name = g_strdup (name);
1932 } else if (g_ascii_strcasecmp (node->name, "Desktop") == 0) {
1933 xmlChar *desktop = xmlNodeGetContent (node);
1934 if (desktop != NULL) {
1935 g_free (folder->desktop_file);
1936 folder->desktop_file = g_strdup (desktop);
1939 } else if (g_ascii_strcasecmp (node->name, "Include") == 0) {
1940 xmlChar *file = xmlNodeGetContent (node);
1943 char *str = g_strdup (file);
1944 folder->includes = g_slist_prepend
1945 (folder->includes, str);
1946 if (folder->includes_ht == NULL) {
1947 folder->includes_ht =
1948 g_hash_table_new_full
1954 li = g_hash_table_lookup (folder->includes_ht,
1958 /* Note: this will NOT change folder->includes
1960 folder->includes = g_slist_delete_link
1961 (folder->includes, li);
1963 g_hash_table_replace (folder->includes_ht,
1964 file, folder->includes);
1967 } else if (g_ascii_strcasecmp (node->name, "Exclude") == 0) {
1968 xmlChar *file = xmlNodeGetContent (node);
1971 if (folder->excludes == NULL) {
1972 folder->excludes = g_hash_table_new_full
1975 (GDestroyNotify)g_free,
1978 s = g_strdup (file);
1979 g_hash_table_replace (folder->excludes, s, s);
1982 } else if (g_ascii_strcasecmp (node->name, "Query") == 0) {
1985 query = query_read (node);
1987 if (query != NULL) {
1988 if (folder->query != NULL)
1989 query_destroy (folder->query);
1990 folder->query = query;
1992 } else if (g_ascii_strcasecmp (node->name, "OnlyUnallocated") == 0) {
1993 info->unallocated_folders =
1994 g_slist_prepend (info->unallocated_folders,
1995 (Folder *)entry_ref ((Entry *)folder));
1996 folder->only_unallocated = TRUE;
1997 } else if (g_ascii_strcasecmp (node->name, "Folder") == 0) {
1998 Folder *new_folder = folder_read (info, node);
1999 if (new_folder != NULL) {
2000 folder->subfolders =
2001 g_slist_append (folder->subfolders,
2003 new_folder->parent = folder;
2005 } else if (g_ascii_strcasecmp (node->name, "ReadOnly") == 0) {
2006 folder->read_only = TRUE;
2007 } else if (g_ascii_strcasecmp (node->name,
2008 "DontShowIfEmpty") == 0) {
2009 folder->dont_show_if_empty = TRUE;
2013 /* Name is required */
2014 if (folder->entry.name == NULL) {
2015 entry_unref ((Entry *)folder);
2019 folder->includes = g_slist_reverse (folder->includes);
2025 subst_home (const char *dir)
2028 return g_strconcat (g_get_home_dir (),
2032 return g_strdup (dir);
2035 /* FORMAT looks like:
2037 * <!-- Merge dirs optional -->
2038 * <MergeDir>/etc/X11/applnk</MergeDir>
2039 * <!-- Only specify if it should override standard location -->
2040 * <ItemDir>/usr/share/applications</ItemDir>
2041 * <!-- This is where the .directories are -->
2042 * <DesktopDir>/etc/X11/gnome/vfolders</DesktopDir>
2043 * <!-- Root folder -->
2047 * <Include>important.desktop</Include>
2049 * <!-- Other folders -->
2051 * <Name>SomeFolder</Name>
2054 * <Name>Test_Folder</Name>
2055 * <!-- could also be absolute -->
2056 * <Desktop>Test_Folder.directory</Desktop>
2060 * <Keyword>Application</Keyword>
2061 * <Keyword>Game</Keyword>
2063 * <Keyword>Clock</Keyword>
2066 * <Include>somefile.desktop</Include>
2067 * <Include>someotherfile.desktop</Include>
2068 * <Exclude>yetanother.desktop</Exclude>
2075 vfolder_info_read_info (VFolderInfo *info,
2076 GnomeVFSResult *result,
2077 GnomeVFSContext *context)
2081 gboolean got_a_vfolder_dir = FALSE;
2084 if (info->user_filename != NULL &&
2085 access (info->user_filename, F_OK) == 0) {
2086 doc = xmlParseFile (info->user_filename);
2088 info->user_file_active = TRUE;
2091 access (info->filename, F_OK) == 0)
2092 doc = xmlParseFile (info->filename);
2094 if (gnome_vfs_context_check_cancellation (context)) {
2096 *result = GNOME_VFS_ERROR_CANCELLED;
2101 || doc->xmlRootNode == NULL
2102 || doc->xmlRootNode->name == NULL
2103 || g_ascii_strcasecmp (doc->xmlRootNode->name, "VFolderInfo") != 0) {
2105 return TRUE; /* FIXME: really, shouldn't we error out? */
2108 for (node = doc->xmlRootNode->xmlChildrenNode; node != NULL; node = node->next) {
2109 if (node->type != XML_ELEMENT_NODE ||
2113 if (gnome_vfs_context_check_cancellation (context)) {
2115 *result = GNOME_VFS_ERROR_CANCELLED;
2119 if (g_ascii_strcasecmp (node->name, "MergeDir") == 0) {
2120 xmlChar *dir = xmlNodeGetContent (node);
2122 info->merge_dirs = g_slist_append (info->merge_dirs,
2126 } else if (g_ascii_strcasecmp (node->name, "ItemDir") == 0) {
2127 xmlChar *dir = xmlNodeGetContent (node);
2129 if ( ! got_a_vfolder_dir) {
2130 g_slist_foreach (info->item_dirs,
2131 (GFunc)g_free, NULL);
2132 g_slist_free (info->item_dirs);
2133 info->item_dirs = NULL;
2135 got_a_vfolder_dir = TRUE;
2136 info->item_dirs = g_slist_append (info->item_dirs,
2140 } else if (g_ascii_strcasecmp (node->name, "UserItemDir") == 0) {
2141 xmlChar *dir = xmlNodeGetContent (node);
2143 g_free (info->user_item_dir);
2144 info->user_item_dir = subst_home (dir);
2147 } else if (g_ascii_strcasecmp (node->name, "DesktopDir") == 0) {
2148 xmlChar *dir = xmlNodeGetContent (node);
2150 g_free (info->desktop_dir);
2151 info->desktop_dir = g_strdup (dir);
2154 } else if (g_ascii_strcasecmp (node->name, "UserDesktopDir") == 0) {
2155 xmlChar *dir = xmlNodeGetContent (node);
2157 g_free (info->user_desktop_dir);
2158 info->user_desktop_dir = subst_home (dir);
2161 } else if (g_ascii_strcasecmp (node->name, "Folder") == 0) {
2162 Folder *folder = folder_read (info, node);
2163 if (folder != NULL) {
2164 if (info->root != NULL)
2165 entry_unref ((Entry *)info->root);
2166 info->root = folder;
2168 } else if (g_ascii_strcasecmp (node->name, "ReadOnly") == 0) {
2169 info->read_only = TRUE;
2179 add_xml_tree_from_query (xmlNode *parent, Query *query)
2181 xmlNode *real_parent;
2184 real_parent = xmlNewChild (parent /* parent */,
2187 NULL /* content */);
2189 real_parent = parent;
2191 if (query->type == QUERY_KEYWORD) {
2192 QueryKeyword *qkeyword = (QueryKeyword *)query;
2193 const char *string = g_quark_to_string (qkeyword->keyword);
2195 xmlNewChild (real_parent /* parent */,
2197 "Keyword" /* name */,
2198 string /* content */);
2199 } else if (query->type == QUERY_FILENAME) {
2200 QueryFilename *qfilename = (QueryFilename *)query;
2202 xmlNewChild (real_parent /* parent */,
2204 "Filename" /* name */,
2205 qfilename->filename /* content */);
2206 } else if (query->type == QUERY_OR ||
2207 query->type == QUERY_AND) {
2212 if (query->type == QUERY_OR)
2214 else /* QUERY_AND */
2217 node = xmlNewChild (real_parent /* parent */,
2220 NULL /* content */);
2222 for (li = query->queries; li != NULL; li = li->next) {
2223 Query *subquery = li->data;
2224 add_xml_tree_from_query (node, subquery);
2227 g_assert_not_reached ();
2232 add_excludes_to_xml (gpointer key, gpointer value, gpointer user_data)
2234 const char *filename = key;
2235 xmlNode *folder_node = user_data;
2237 xmlNewChild (folder_node /* parent */,
2239 "Exclude" /* name */,
2240 filename /* content */);
2244 add_xml_tree_from_folder (xmlNode *parent, Folder *folder)
2247 xmlNode *folder_node;
2250 folder_node = xmlNewChild (parent /* parent */,
2252 "Folder" /* name */,
2253 NULL /* content */);
2255 xmlNewChild (folder_node /* parent */,
2258 folder->entry.name /* content */);
2260 if (folder->desktop_file != NULL) {
2261 xmlNewChild (folder_node /* parent */,
2263 "Desktop" /* name */,
2264 folder->desktop_file /* content */);
2267 if (folder->read_only)
2268 xmlNewChild (folder_node /* parent */,
2270 "ReadOnly" /* name */,
2271 NULL /* content */);
2272 if (folder->dont_show_if_empty)
2273 xmlNewChild (folder_node /* parent */,
2275 "DontShowIfEmpty" /* name */,
2276 NULL /* content */);
2277 if (folder->only_unallocated)
2278 xmlNewChild (folder_node /* parent */,
2280 "OnlyUnallocated" /* name */,
2281 NULL /* content */);
2283 for (li = folder->subfolders; li != NULL; li = li->next) {
2284 Folder *subfolder = li->data;
2285 add_xml_tree_from_folder (folder_node, subfolder);
2288 for (li = folder->includes; li != NULL; li = li->next) {
2289 const char *include = li->data;
2290 xmlNewChild (folder_node /* parent */,
2292 "Include" /* name */,
2293 include /* content */);
2296 if (folder->excludes) {
2297 g_hash_table_foreach (folder->excludes,
2298 add_excludes_to_xml,
2302 if (folder->query != NULL) {
2303 xmlNode *query_node;
2304 query_node = xmlNewChild (folder_node /* parent */,
2307 NULL /* content */);
2309 add_xml_tree_from_query (query_node, folder->query);
2314 xml_tree_from_vfolder (VFolderInfo *info)
2320 doc = xmlNewDoc ("1.0");
2322 topnode = xmlNewDocNode (doc /* doc */,
2324 "VFolderInfo" /* name */,
2325 NULL /* content */);
2326 doc->xmlRootNode = topnode;
2328 for (li = info->merge_dirs; li != NULL; li = li->next) {
2329 const char *merge_dir = li->data;
2330 xmlNewChild (topnode /* parent */,
2332 "MergeDir" /* name */,
2333 merge_dir /* content */);
2336 for (li = info->item_dirs; li != NULL; li = li->next) {
2337 const char *item_dir = li->data;
2338 xmlNewChild (topnode /* parent */,
2340 "ItemDir" /* name */,
2341 item_dir /* content */);
2344 if (info->user_item_dir != NULL) {
2345 xmlNewChild (topnode /* parent */,
2347 "UserItemDir" /* name */,
2348 info->user_item_dir /* content */);
2351 if (info->desktop_dir != NULL) {
2352 xmlNewChild (topnode /* parent */,
2354 "DesktopDir" /* name */,
2355 info->desktop_dir /* content */);
2358 if (info->user_desktop_dir != NULL) {
2359 xmlNewChild (topnode /* parent */,
2361 "UserDesktopDir" /* name */,
2362 info->user_desktop_dir /* content */);
2365 if (info->root != NULL)
2366 add_xml_tree_from_folder (topnode, info->root);
2371 /* FIXME: what to do about errors */
2373 vfolder_info_write_user (VFolderInfo *info)
2377 if (info->inhibit_write > 0)
2380 if (info->user_filename == NULL)
2383 doc = xml_tree_from_vfolder (info);
2387 /* FIXME: errors, anyone? */
2388 ensure_dir (info->user_filename,
2389 TRUE /* ignore_basename */);
2391 xmlSaveFormatFile (info->user_filename, doc, TRUE /* format */);
2392 /* not as good as a stat, but cheaper ... hmmm what is
2393 * the likelyhood of this not being the same as ctime */
2394 info->user_filename_last_write = time (NULL);
2398 info->user_file_active = TRUE;
2399 info->dirty = FALSE;
2401 info->modification_time = time (NULL);
2404 /* An EVIL function for quick reading of .desktop files,
2405 * only reads in one or two keys, but that's ALL we need */
2407 readitem_entry (const char *filename,
2415 int keylen1, keylen2;
2418 if (result2 != NULL)
2421 fp = fopen (filename, "r");
2426 keylen1 = strlen (key1);
2428 keylen2 = strlen (key2);
2432 /* This is slightly wrong, it should only look
2433 * at the correct section */
2434 while (fgets (buf, sizeof (buf), fp) != NULL) {
2438 char **result = NULL;
2440 /* check if it's one of the keys */
2441 if (strncmp (buf, key1, keylen1) == 0) {
2444 } else if (keylen2 >= 0 &&
2445 strncmp (buf, key2, keylen2) == 0) {
2454 /* still not our key */
2455 if (!(*p == '=' || *p == ' ')) {
2460 while (*p == ' ' || *p == '=');
2462 /* get rid of trailing \n */
2464 if (p[len-1] == '\n' ||
2468 *result = g_strdup (p);
2470 if (*result1 == NULL ||
2471 (result2 != NULL && *result2 == NULL))
2479 vfolder_info_insert_entry (VFolderInfo *info, EntryFile *efile)
2483 entry_ref ((Entry *)efile);
2485 entry_list = g_hash_table_lookup (info->entries_ht, efile->entry.name);
2487 info->entries = g_slist_prepend (info->entries, efile);
2488 /* The hash table contains the GSList pointer */
2489 g_hash_table_replace (info->entries_ht, efile->entry.name,
2492 if (entry_list != NULL) {
2493 Entry *entry = entry_list->data;
2494 info->entries = g_slist_delete_link (info->entries,
2496 entry_unref (entry);
2501 set_keywords (EntryFile *efile, const char *keywords)
2503 if (keywords != NULL) {
2505 char **parsed = g_strsplit (keywords, ";", -1);
2506 for (i = 0; parsed[i] != NULL; i++) {
2508 const char *word = parsed[i];
2509 /* ignore empties (including end of list) */
2510 if (word[0] == '\0')
2512 quark = g_quark_from_string (word);
2513 efile->keywords = g_slist_prepend
2515 GINT_TO_POINTER (quark));
2517 g_strfreev (parsed);
2522 make_entry_file (const char *dir, const char *name)
2530 filename = g_build_filename (dir, name, NULL);
2532 readitem_entry (filename,
2538 if (only_show_in != NULL) {
2539 gboolean show = FALSE;
2540 char **parsed = g_strsplit (only_show_in, ";", -1);
2541 for (i = 0; parsed[i] != NULL; i++) {
2542 if (strcmp (parsed[i], "GNOME") == 0) {
2547 g_strfreev (parsed);
2550 g_free (only_show_in);
2551 g_free (categories);
2556 efile = file_new (name);
2557 efile->filename = filename;
2559 set_keywords (efile, categories);
2561 g_free (only_show_in);
2562 g_free (categories);
2568 vfolder_info_read_items_from (VFolderInfo *info,
2569 const char *item_dir,
2571 GnomeVFSResult *result,
2572 GnomeVFSContext *context)
2577 dir = opendir (item_dir);
2581 while ((de = readdir (dir)) != NULL) {
2584 if (gnome_vfs_context_check_cancellation (context)) {
2586 *result = GNOME_VFS_ERROR_CANCELLED;
2590 /* files MUST be called .desktop */
2591 if (de->d_name[0] == '.' ||
2592 ! check_ext (de->d_name, ".desktop"))
2595 efile = make_entry_file (item_dir, de->d_name);
2599 efile->per_user = per_user;
2601 vfolder_info_insert_entry (info, efile);
2602 entry_unref ((Entry *)efile);
2611 vfolder_info_read_items_merge (VFolderInfo *info,
2612 const char *merge_dir,
2614 GQuark inherited_keyword,
2615 GnomeVFSResult *result,
2616 GnomeVFSContext *context)
2620 GQuark extra_keyword;
2624 gboolean pass_down_extra_keyword = TRUE;
2626 dir = opendir (merge_dir);
2630 Application = g_quark_from_static_string ("Application");
2631 Merged = g_quark_from_static_string ("Merged");
2633 /* FIXME: this should be a hash or something */
2635 if (subdir == NULL) {
2636 extra_keyword = g_quark_from_static_string ("Core");
2637 pass_down_extra_keyword = FALSE;
2638 } else if (g_ascii_strcasecmp (subdir, "Development") == 0)
2639 extra_keyword = g_quark_from_static_string ("Development");
2640 else if (g_ascii_strcasecmp (subdir, "Editors") == 0)
2641 extra_keyword = g_quark_from_static_string ("TextEditor");
2642 else if (g_ascii_strcasecmp (subdir, "Games") == 0)
2643 extra_keyword = g_quark_from_static_string ("Game");
2644 else if (g_ascii_strcasecmp (subdir, "Graphics") == 0)
2645 extra_keyword = g_quark_from_static_string ("Graphics");
2646 else if (g_ascii_strcasecmp (subdir, "Internet") == 0)
2647 extra_keyword = g_quark_from_static_string ("Network");
2648 else if (g_ascii_strcasecmp (subdir, "Multimedia") == 0)
2649 extra_keyword = g_quark_from_static_string ("AudioVideo");
2650 else if (g_ascii_strcasecmp (subdir, "Office") == 0)
2651 extra_keyword = g_quark_from_static_string ("Office");
2652 else if (g_ascii_strcasecmp (subdir, "Settings") == 0)
2653 extra_keyword = g_quark_from_static_string ("Settings");
2654 else if (g_ascii_strcasecmp (subdir, "System") == 0)
2655 extra_keyword = g_quark_from_static_string ("System");
2656 else if (g_ascii_strcasecmp (subdir, "Utilities") == 0)
2657 extra_keyword = g_quark_from_static_string ("Utility");
2659 while ((de = readdir (dir)) != NULL) {
2662 if (gnome_vfs_context_check_cancellation (context)) {
2664 *result = GNOME_VFS_ERROR_CANCELLED;
2669 if (de->d_name[0] == '.')
2672 /* files MUST be called .desktop, so
2673 * treat all others as dirs. If we're wrong,
2674 * the open will fail, which is ok */
2675 if ( ! check_ext (de->d_name, ".desktop")) {
2676 /* if this is a directory recurse */
2677 char *fullname = g_build_filename (merge_dir, de->d_name, NULL);
2678 if ((pass_down_extra_keyword == TRUE) && (extra_keyword != 0)) {
2679 inheritance = extra_keyword;
2681 inheritance = inherited_keyword;
2684 if ( ! vfolder_info_read_items_merge (info,
2697 /* FIXME: add some keywords about some known apps
2698 * like gimp and whatnot, perhaps take these from the vfolder
2699 * file or some such */
2701 efile = make_entry_file (merge_dir, de->d_name);
2705 /* If no keywords set, then add the standard ones */
2706 if (efile->keywords == NULL) {
2707 efile->keywords = g_slist_prepend
2709 GINT_TO_POINTER (Application));
2711 efile->keywords = g_slist_prepend
2713 GINT_TO_POINTER (Merged));
2715 if (inherited_keyword != 0) {
2716 efile->keywords = g_slist_prepend
2718 GINT_TO_POINTER (inherited_keyword));
2721 if (extra_keyword != 0) {
2722 efile->keywords = g_slist_prepend
2724 GINT_TO_POINTER (extra_keyword));
2726 efile->implicit_keywords = TRUE;
2729 vfolder_info_insert_entry (info, efile);
2730 entry_unref ((Entry *)efile);
2739 find_entry (GSList *list, const char *name)
2743 for (li = list; li != NULL; li = li->next) {
2744 Entry *entry = li->data;
2745 if (strcmp (name, entry->name) == 0)
2752 file_monitor (GnomeVFSMonitorHandle *handle,
2753 const gchar *monitor_uri,
2754 const gchar *info_uri,
2755 GnomeVFSMonitorEventType event_type,
2758 FileMonitorHandle *h = user_data;
2760 /* proxy the event through if it is a changed event
2763 if (event_type == GNOME_VFS_MONITOR_EVENT_CHANGED &&
2765 gnome_vfs_monitor_callback ((GnomeVFSMethodHandle *) h,
2766 h->uri, event_type);
2770 try_free_file_monitors_create_files_unlocked (VFolderInfo *info)
2774 list = g_slist_copy (info->free_file_monitors);
2776 for (li = list; li != NULL; li = li->next) {
2777 FileMonitorHandle *handle = li->data;
2779 GnomeVFSResult result;
2780 char *dirfile = NULL;
2782 if (handle->is_directory_file) {
2786 /* Evil! EVIL URI PARSING. this will eat a lot of
2787 * stack if we have lots of free monitors */
2789 VFOLDER_URI_PARSE (handle->uri, &vuri);
2791 folder = resolve_folder (info,
2793 TRUE /* ignore_basename */,
2800 dirfile = get_directory_file_unlocked (info, folder);
2801 if (dirfile == NULL)
2804 entry = (Entry *)folder;
2808 GnomeVFSResult result;
2812 /* Evil! EVIL URI PARSING. this will eat a lot of
2813 * stack if we have lots of monitors */
2815 VFOLDER_URI_PARSE (handle->uri, &vuri);
2817 f = resolve_folder (info,
2819 TRUE /* ignore_basename */,
2824 ensure_folder_unlocked (
2826 FALSE /* subfolders */,
2828 FALSE /* ignore_unallocated */);
2829 entry = find_entry (f->entries, vuri.file);
2836 info->free_file_monitors =
2837 g_slist_remove (info->free_file_monitors, handle);
2839 g_slist_prepend (entry->monitors, handle);
2841 handle->exists = TRUE;
2842 gnome_vfs_monitor_callback ((GnomeVFSMethodHandle *)handle,
2844 GNOME_VFS_MONITOR_EVENT_CREATED);
2846 /* recreate a handle */
2847 if (handle->handle == NULL &&
2848 entry->type == ENTRY_FILE) {
2849 EntryFile *efile = (EntryFile *)entry;
2850 char *uri = gnome_vfs_get_uri_from_local_path
2853 gnome_vfs_monitor_add (&(handle->handle),
2855 GNOME_VFS_MONITOR_FILE,
2860 } else if (handle->handle == NULL &&
2862 char *uri = gnome_vfs_get_uri_from_local_path (dirfile);
2864 gnome_vfs_monitor_add (&(handle->handle),
2866 GNOME_VFS_MONITOR_FILE,
2876 g_slist_free (list);
2879 static void /* unlocked */
2880 rescan_monitors (VFolderInfo *info)
2884 if (info->file_monitors == NULL)
2887 for (li = info->file_monitors; li != NULL; li = li->next) {
2888 FileMonitorHandle *h = li->data;
2889 GnomeVFSResult result;
2891 char *dirfile = NULL;
2893 /* these are handled below */
2897 if (h->is_directory_file) {
2901 /* Evil! EVIL URI PARSING. this will eat a lot of
2902 * stack if we have lots of monitors */
2904 VFOLDER_URI_PARSE (h->uri, &vuri);
2906 folder = resolve_folder (info,
2908 TRUE /* ignore_basename */,
2912 dirfile = get_directory_file_unlocked (info,
2915 if (dirfile == NULL) {
2917 gnome_vfs_monitor_callback
2918 ((GnomeVFSMethodHandle *)h,
2920 GNOME_VFS_MONITOR_EVENT_DELETED);
2921 info->free_file_monitors = g_slist_prepend
2922 (info->free_file_monitors, h);
2923 file_monitor_handle_ref_unlocked (h);
2924 /* it has been unreffed when the entry was
2929 entry = (Entry *)folder;
2933 GnomeVFSResult result;
2937 /* Evil! EVIL URI PARSING. this will eat a lot of
2938 * stack if we have lots of monitors */
2940 VFOLDER_URI_PARSE (h->uri, &vuri);
2942 f = resolve_folder (info,
2944 TRUE /* ignore_basename */,
2949 ensure_folder_unlocked (
2951 FALSE /* subfolders */,
2953 FALSE /* ignore_unallocated */);
2954 entry = find_entry (f->entries, vuri.file);
2957 if (entry == NULL) {
2959 gnome_vfs_monitor_callback
2960 ((GnomeVFSMethodHandle *)h,
2962 GNOME_VFS_MONITOR_EVENT_DELETED);
2963 info->free_file_monitors = g_slist_prepend
2964 (info->free_file_monitors, h);
2965 file_monitor_handle_ref_unlocked (h);
2966 /* it has been unreffed when the entry was
2972 /* recreate a handle */
2973 if (h->handle == NULL &&
2974 entry->type == ENTRY_FILE) {
2975 EntryFile *efile = (EntryFile *)entry;
2976 char *uri = gnome_vfs_get_uri_from_local_path
2979 gnome_vfs_monitor_add (&(h->handle),
2981 GNOME_VFS_MONITOR_FILE,
2986 } else if (h->handle == NULL &&
2988 char *uri = gnome_vfs_get_uri_from_local_path (dirfile);
2990 gnome_vfs_monitor_add (&(h->handle),
2992 GNOME_VFS_MONITOR_FILE,
3002 try_free_file_monitors_create_files_unlocked (info);
3005 static gboolean /* unlocked */
3006 vfolder_info_read_items (VFolderInfo *info,
3007 GnomeVFSResult *result,
3008 GnomeVFSContext *context)
3013 for (li = info->merge_dirs; li != NULL; li = li->next) {
3014 const char *merge_dir = li->data;
3016 if ( ! vfolder_info_read_items_merge (info, merge_dir, NULL, FALSE,
3021 /* Then read the real thing (later overrides) */
3022 for (li = info->item_dirs; li != NULL; li = li->next) {
3023 const char *item_dir = li->data;
3025 if ( ! vfolder_info_read_items_from (info, item_dir,
3026 FALSE /* per_user */,
3031 if (info->user_item_dir != NULL) {
3032 if ( ! vfolder_info_read_items_from (info,
3033 info->user_item_dir,
3034 TRUE /* per_user */,
3039 rescan_monitors (info);
3045 string_slist_equal (GSList *list1, GSList *list2)
3049 for (li1 = list1, li2 = list2;
3050 li1 != NULL && li2 != NULL;
3051 li1 = li1->next, li2 = li2->next) {
3052 const char *s1 = li1->data;
3053 const char *s2 = li2->data;
3054 if (strcmp (s1, s2) != 0)
3057 /* if both are not NULL, then lengths are
3065 safe_string_same (const char *string1, const char *string2)
3067 if (string1 == string2 &&
3071 if (string1 != NULL && string2 != NULL &&
3072 strcmp (string1, string2) == 0)
3079 vfolder_info_item_dirs_same (VFolderInfo *info1, VFolderInfo *info2)
3081 if ( ! string_slist_equal (info1->item_dirs,
3085 if ( ! string_slist_equal (info1->merge_dirs,
3089 if ( ! safe_string_same (info1->user_item_dir,
3090 info2->user_item_dir))
3097 vfolder_info_reload_unlocked (VFolderInfo *info,
3098 GnomeVFSResult *result,
3099 GnomeVFSContext *context,
3100 gboolean force_read_items)
3102 VFolderInfo *newinfo;
3103 gboolean setup_filenames;
3104 gboolean setup_itemdirs;
3107 /* FIXME: Hmmm, race, there is no locking YAIKES,
3108 * we need filename locking for changes. eek, eek, eek */
3113 newinfo = g_new0 (VFolderInfo, 1);
3114 vfolder_info_init (newinfo, info->scheme);
3116 g_free (newinfo->filename);
3117 g_free (newinfo->user_filename);
3118 newinfo->filename = g_strdup (info->filename);
3119 newinfo->user_filename = g_strdup (info->user_filename);
3121 if (gnome_vfs_context_check_cancellation (context)) {
3122 vfolder_info_destroy (newinfo);
3123 *result = GNOME_VFS_ERROR_CANCELLED;
3127 if ( ! vfolder_info_read_info (newinfo, result, context)) {
3128 vfolder_info_destroy (newinfo);
3132 /* FIXME: reload logic for 'desktop_dir' and
3133 * 'user_desktop_dir' */
3135 setup_itemdirs = TRUE;
3137 /* Validity of entries and item dirs and all that is unchanged */
3138 if (vfolder_info_item_dirs_same (info, newinfo)) {
3139 newinfo->entries = info->entries;
3140 info->entries = NULL;
3141 newinfo->entries_ht = info->entries_ht;
3142 info->entries_ht = NULL /* some places assume this
3143 non-null, but we're only
3144 going to destroy this */;
3145 newinfo->entries_valid = info->entries_valid;
3147 /* move over the monitors/statlocs since those are valid */
3148 newinfo->item_dir_monitors = info->item_dir_monitors;
3149 info->item_dir_monitors = NULL;
3150 newinfo->stat_dirs = info->stat_dirs;
3151 info->stat_dirs = NULL;
3153 /* No need to resetup dir monitors */
3154 setup_itemdirs = FALSE;
3156 /* No need to do anything with file monitors */
3158 /* Whack all monitors here! */
3159 for (li = info->file_monitors; li != NULL; li = li->next) {
3160 FileMonitorHandle *h = li->data;
3161 if (h->handle != NULL)
3162 gnome_vfs_monitor_cancel (h->handle);
3167 setup_filenames = TRUE;
3169 if (safe_string_same (info->filename, newinfo->filename) &&
3170 safe_string_same (info->user_filename, newinfo->user_filename)) {
3171 newinfo->user_filename_last_write =
3172 info->user_filename_last_write;
3174 /* move over the monitors/statlocs since those are valid */
3175 newinfo->filename_monitor = info->filename_monitor;
3176 info->filename_monitor = NULL;
3177 newinfo->user_filename_monitor = info->user_filename_monitor;
3178 info->user_filename_monitor = NULL;
3180 if (info->filename_statloc != NULL &&
3181 info->filename != NULL)
3182 newinfo->filename_statloc =
3183 bake_statloc (info->filename,
3185 if (info->user_filename_statloc != NULL &&
3186 info->user_filename != NULL)
3187 newinfo->user_filename_statloc =
3188 bake_statloc (info->user_filename,
3191 /* No need to resetup filename monitors */
3192 setup_filenames = FALSE;
3195 /* Note: not cancellable anymore, since we've
3196 * already started nibbling on the info structure,
3197 * so we'd need to back things out or some such,
3198 * too complex, so screw that */
3199 monitor_setup (info,
3202 /* FIXME: setup_desktop_dirs */ TRUE,
3205 for (li = info->folder_monitors;
3208 FileMonitorHandle *handle = li->data;
3211 add_folder_monitor_unlocked (newinfo, handle);
3213 file_monitor_handle_unref_unlocked (handle);
3215 g_slist_free (info->folder_monitors);
3216 info->folder_monitors = NULL;
3218 g_slist_foreach (info->free_folder_monitors,
3219 (GFunc)file_monitor_handle_unref_unlocked, NULL);
3220 g_slist_free (info->free_folder_monitors);
3221 info->folder_monitors = NULL;
3223 /* we can just copy these for now, they will be readded
3224 * and all the fun stuff will be done with them later */
3225 newinfo->file_monitors = info->file_monitors;
3226 info->file_monitors = NULL;
3227 newinfo->free_file_monitors = info->free_file_monitors;
3228 info->free_file_monitors = NULL;
3230 /* emit changed on all folders, a bit drastic, but oh well,
3231 * we also invalidate all folders at the same time, but that is
3232 * irrelevant since they should all just be invalid to begin with */
3233 invalidate_folder_T (info->root);
3235 /* FIXME: make sure if this was enough, I think it was */
3237 vfolder_info_free_internals_unlocked (info);
3238 memcpy (info, newinfo, sizeof (VFolderInfo));
3241 /* must rescan the monitors here */
3242 if (info->entries_valid) {
3243 rescan_monitors (info);
3246 if ( ! info->entries_valid &&
3249 /* FIXME: I bet cancelation plays havoc with monitors,
3250 * I'm not sure however */
3251 if (info->file_monitors != NULL) {
3252 vfolder_info_read_items (info, &res, NULL);
3254 if ( ! vfolder_info_read_items (info, result, context))
3257 info->entries_valid = TRUE;
3264 vfolder_info_reload (VFolderInfo *info,
3265 GnomeVFSResult *result,
3266 GnomeVFSContext *context,
3267 gboolean force_read_items)
3269 G_LOCK (vfolder_lock);
3270 if (vfolder_info_reload_unlocked (info, result, context,
3271 force_read_items)) {
3272 G_UNLOCK (vfolder_lock);
3275 G_UNLOCK (vfolder_lock);
3281 vfolder_info_recheck (VFolderInfo *info,
3282 GnomeVFSResult *result,
3283 GnomeVFSContext *context)
3286 time_t curtime = time (NULL);
3287 gboolean reread = FALSE;
3289 if (info->filename_statloc != NULL &&
3290 ! check_statloc (info->filename_statloc, curtime)) {
3291 if ( ! vfolder_info_reload_unlocked (info, result, context,
3292 FALSE /* force read items */)) {
3293 /* we have failed, make sure we fail
3295 info->filename_statloc->trigger_next = TRUE;
3301 info->user_filename_statloc != NULL &&
3302 ! check_statloc (info->user_filename_statloc, curtime)) {
3303 if ( ! vfolder_info_reload_unlocked (info, result, context,
3304 FALSE /* force read items */)) {
3305 /* we have failed, make sure we fail
3307 info->user_filename_statloc->trigger_next = TRUE;
3313 if (info->entries_valid) {
3314 for (li = info->stat_dirs; li != NULL; li = li->next) {
3315 StatLoc *sl = li->data;
3316 if ( ! check_statloc (sl, curtime)) {
3317 info->entries_valid = FALSE;
3325 static VFolderInfo *
3326 get_vfolder_info_unlocked (const char *scheme,
3327 GnomeVFSResult *result,
3328 GnomeVFSContext *context)
3332 if (infos != NULL &&
3333 (info = g_hash_table_lookup (infos, scheme)) != NULL) {
3334 if ( ! vfolder_info_recheck (info, result, context)) {
3337 if ( ! info->entries_valid) {
3338 g_slist_foreach (info->entries,
3339 (GFunc)entry_unref, NULL);
3340 g_slist_free (info->entries);
3341 info->entries = NULL;
3343 if (info->entries_ht != NULL)
3344 g_hash_table_destroy (info->entries_ht);
3345 info->entries_ht = g_hash_table_new (g_str_hash,
3348 if ( ! vfolder_info_read_items (info,
3350 info->entries_valid = FALSE;
3354 invalidate_folder_T (info->root);
3356 info->entries_valid = TRUE;
3361 if (gnome_vfs_context_check_cancellation (context)) {
3362 *result = GNOME_VFS_ERROR_CANCELLED;
3367 infos = g_hash_table_new_full
3368 (g_str_hash, g_str_equal,
3369 (GDestroyNotify)g_free,
3370 (GDestroyNotify)vfolder_info_destroy);
3372 info = g_new0 (VFolderInfo, 1);
3373 vfolder_info_init (info, scheme);
3375 if (gnome_vfs_context_check_cancellation (context)) {
3376 vfolder_info_destroy (info);
3377 *result = GNOME_VFS_ERROR_CANCELLED;
3381 if ( ! vfolder_info_read_info (info, result, context)) {
3382 vfolder_info_destroy (info);
3386 if ( ! monitor_setup (info,
3387 TRUE /* setup_filenames */,
3388 TRUE /* setup_itemdirs */,
3389 TRUE /* setup_desktop_dirs */,
3391 vfolder_info_destroy (info);
3395 g_hash_table_insert (infos, g_strdup (scheme), info);
3397 if ( ! vfolder_info_read_items (info, result, context)) {
3398 info->entries_valid = FALSE;
3401 info->entries_valid = TRUE;
3406 static VFolderInfo *
3407 get_vfolder_info (const char *scheme,
3408 GnomeVFSResult *result,
3409 GnomeVFSContext *context)
3412 G_LOCK (vfolder_lock);
3413 info = get_vfolder_info_unlocked (scheme, result, context);
3414 G_UNLOCK (vfolder_lock);
3420 keywords_to_string (GSList *keywords)
3423 GString *str = g_string_new (NULL);
3425 for (li = keywords; li != NULL; li = li->next) {
3426 GQuark word = GPOINTER_TO_INT (li->data);
3427 g_string_append (str, g_quark_to_string (word));
3428 g_string_append_c (str, ';');
3431 return g_string_free (str, FALSE);
3434 /* copy file and add keywords line */
3436 copy_file_with_keywords (const char *from, const char *to, GSList *keywords)
3442 char *keyword_string;
3444 if ( ! ensure_dir (to,
3445 TRUE /* ignore_basename */))
3448 wfd = open (to, O_CREAT | O_WRONLY | O_TRUNC, 0600);
3453 keyword_string = keywords_to_string (keywords);
3455 wfp = fdopen (wfd, "w");
3457 fp = fopen (from, "r");
3459 gboolean wrote_keywords = FALSE;
3460 while (fgets (buf, sizeof (buf), fp) != NULL) {
3461 fprintf (wfp, "%s", buf);
3462 if ( ! wrote_keywords &&
3463 (strncmp (buf, "[Desktop Entry]",
3464 strlen ("[Desktop Entry]")) == 0 ||
3465 strncmp (buf, "[KDE Desktop Entry]",
3466 strlen ("[KDE Desktop Entry]")) == 0)) {
3467 fprintf (wfp, "Categories=%s\n",
3469 wrote_keywords = TRUE;
3475 fprintf (wfp, "[Desktop Entry]\nCategories=%s\n",
3479 /* FIXME: does this close wfd???? */
3484 g_free (keyword_string);
3490 copy_file (const char *from, const char *to)
3495 if ( ! ensure_dir (to,
3496 TRUE /* ignore_basename */))
3499 wfd = open (to, O_CREAT | O_WRONLY | O_TRUNC, 0600);
3504 fd = open (from, O_RDONLY);
3509 while ((n = read (fd, buf, sizeof(buf))) > 0) {
3510 write (wfd, buf, n);
3522 make_file_private (VFolderInfo *info, EntryFile *efile)
3525 Entry *entry = (Entry *)efile;
3527 if (efile->per_user)
3530 /* this file already exists so whack its monitors */
3531 if (efile->filename != NULL) {
3534 for (li = entry->monitors; li != NULL; li = li->next) {
3535 FileMonitorHandle *h = li->data;
3536 if (h->handle != NULL)
3537 gnome_vfs_monitor_cancel (h->handle);
3542 newfname = g_build_filename (g_get_home_dir (),
3549 if (efile->implicit_keywords) {
3550 if (efile->filename != NULL &&
3551 ! copy_file_with_keywords (efile->filename,
3554 /* FIXME: what to do with monitors here, they
3555 * have already been whacked, a corner case
3561 if (efile->filename != NULL &&
3562 ! copy_file (efile->filename, newfname)) {
3563 /* FIXME: what to do with monitors here, they
3564 * have already been whacked, a corner case
3571 /* we didn't copy but ensure path anyway */
3572 if (efile->filename == NULL &&
3573 ! ensure_dir (newfname,
3574 TRUE /* ignore_basename */)) {
3579 /* this file already exists so re-add monitors at the new location */
3580 if (efile->filename != NULL) {
3582 char *uri = gnome_vfs_get_uri_from_local_path (newfname);
3584 for (li = entry->monitors; li != NULL; li = li->next) {
3585 FileMonitorHandle *h = li->data;
3587 gnome_vfs_monitor_add (&(h->handle),
3589 GNOME_VFS_MONITOR_FILE,
3597 g_free (efile->filename);
3598 efile->filename = newfname;
3599 efile->per_user = TRUE;
3605 try_free_file_monitors_create_dirfile_unlocked (VFolderInfo *info,
3610 list = g_slist_copy (info->free_file_monitors);
3612 for (li = list; li != NULL; li = li->next) {
3613 FileMonitorHandle *handle = li->data;
3616 GnomeVFSResult result;
3618 if ( ! handle->is_directory_file)
3621 /* Evil! EVIL URI PARSING. this will eat a lot of stack if we
3622 * have lots of free monitors */
3624 VFOLDER_URI_PARSE (handle->uri, &vuri);
3626 f = resolve_folder (info,
3628 TRUE /* ignore_basename */,
3635 info->free_file_monitors =
3636 g_slist_remove (info->free_file_monitors, handle);
3637 ((Entry *)folder)->monitors =
3638 g_slist_prepend (((Entry *)folder)->monitors, handle);
3640 handle->exists = TRUE;
3641 gnome_vfs_monitor_callback ((GnomeVFSMethodHandle *)handle,
3643 GNOME_VFS_MONITOR_EVENT_CREATED);
3646 g_slist_free (list);
3650 make_new_dirfile (VFolderInfo *info, Folder *folder)
3652 char *name = g_strdup (folder->entry.name);
3658 for (p = name; *p != '\0'; p++) {
3659 if ( ! ( (*p >= 'a' && *p <= 'z') ||
3660 (*p >= 'A' && *p <= 'Z') ||
3661 (*p >= '0' && *p <= '9') ||
3675 fname = g_strdup_printf ("%s-%d.directory", name, i);
3677 fname = g_strdup_printf ("%s.directory", name);
3680 fullname = g_build_filename
3681 (info->user_desktop_dir, fname, NULL);
3682 fd = open (fullname, O_CREAT | O_WRONLY | O_EXCL, 0600);
3688 folder->desktop_file = fname;
3691 try_free_file_monitors_create_dirfile_unlocked (info, folder);
3695 make_dirfile_private (VFolderInfo *info, Folder *folder)
3703 if (info->user_desktop_dir == NULL)
3706 if ( ! ensure_dir (info->user_desktop_dir,
3707 FALSE /* ignore_basename */))
3711 if (folder->desktop_file == NULL) {
3712 make_new_dirfile (info, folder);
3716 /* FIXME: this is broken! What if the desktop file exists
3717 * in the local but there is a different (but with a same name)
3718 * .directory in the system. */
3719 fname = g_build_filename (info->user_desktop_dir,
3720 folder->desktop_file,
3723 if (access (fname, F_OK) == 0) {
3728 desktop_file = get_directory_file (info, folder);
3730 if (desktop_file == NULL) {
3731 int fd = open (fname, O_CREAT | O_EXCL | O_WRONLY, 0600);
3740 for (li = ((Entry *)folder)->monitors; li != NULL; li = li->next) {
3741 FileMonitorHandle *h = li->data;
3742 if (h->is_directory_file) {
3743 if (h->handle != NULL)
3744 gnome_vfs_monitor_cancel (h->handle);
3751 if ( ! copy_file (desktop_file, fname)) {
3754 fname = desktop_file;
3755 desktop_file = NULL;
3758 uri = gnome_vfs_get_uri_from_local_path (fname);
3760 for (li = ((Entry *)folder)->monitors; li != NULL; li = li->next) {
3761 FileMonitorHandle *h = li->data;
3763 if (h->is_directory_file) {
3764 gnome_vfs_monitor_add (&(h->handle),
3766 GNOME_VFS_MONITOR_FILE,
3774 g_free (desktop_file);
3781 resolve_folder (VFolderInfo *info,
3783 gboolean ignore_basename,
3784 GnomeVFSResult *result,
3785 GnomeVFSContext *context)
3789 Folder *folder = info->root;
3791 ppath = g_strsplit (path, "/", -1);
3793 if (ppath == NULL ||
3796 *result = GNOME_VFS_ERROR_INVALID_URI;
3800 for (i = 0; ppath [i] != NULL; i++) {
3801 const char *segment = ppath[i];
3803 if (*segment == '\0')
3806 if (ignore_basename && ppath [i + 1] == NULL)
3809 folder = (Folder *) find_entry (folder->subfolders,
3817 if (gnome_vfs_context_check_cancellation (context)) {
3818 *result = GNOME_VFS_ERROR_CANCELLED;
3823 *result = GNOME_VFS_ERROR_NOT_FOUND;
3829 resolve_path (VFolderInfo *info,
3831 const char *basename,
3832 Folder **return_folder,
3833 GnomeVFSResult *result,
3834 GnomeVFSContext *context)
3839 if (strcmp (path, "/") == 0)
3840 return (Entry *)info->root;
3842 folder = resolve_folder (info, path,
3843 TRUE /* ignore_basename */,
3846 if (return_folder != NULL)
3847 *return_folder = folder;
3849 if (folder == NULL) {
3853 /* Make sure we have the entries here */
3854 ensure_folder_unlocked (info, folder,
3855 FALSE /* subfolders */,
3857 FALSE /* ignore_unallocated */);
3859 entry = find_entry (folder->entries, basename);
3862 *result = GNOME_VFS_ERROR_NOT_FOUND;
3868 get_entry_unlocked (VFolderURI *vuri,
3870 gboolean *is_directory_file,
3871 GnomeVFSResult *result,
3872 GnomeVFSContext *context)
3877 if (is_directory_file != NULL)
3878 *is_directory_file = FALSE;
3882 info = get_vfolder_info_unlocked (vuri->scheme, result, context);
3886 if (gnome_vfs_context_check_cancellation (context)) {
3887 *result = GNOME_VFS_ERROR_CANCELLED;
3891 if (vuri->is_all_scheme) {
3894 if (vuri->file == NULL) {
3895 entry = resolve_path (info,
3904 efile_list = g_hash_table_lookup (info->entries_ht, vuri->file);
3906 if (efile_list == NULL) {
3907 *result = GNOME_VFS_ERROR_NOT_FOUND;
3910 return efile_list->data;
3914 if (vuri->file != NULL &&
3915 check_ext (vuri->file, ".directory") == TRUE) {
3918 folder = resolve_folder (info, vuri->path,
3919 TRUE /* ignore_basename */,
3921 if (folder == NULL) {
3925 if (is_directory_file != NULL)
3926 *is_directory_file = TRUE;
3931 return (Entry *)folder;
3933 entry = resolve_path (info, vuri->path, vuri->file, parent,
3940 get_entry (VFolderURI *vuri,
3942 gboolean *is_directory_file,
3943 GnomeVFSResult *result,
3944 GnomeVFSContext *context)
3948 G_LOCK (vfolder_lock);
3949 entry = get_entry_unlocked (vuri,
3953 G_UNLOCK (vfolder_lock);
3958 /* only works for files and only those that exist */
3959 /* unlocked function */
3960 static GnomeVFSURI *
3961 desktop_uri_to_file_uri (VFolderInfo *info,
3962 VFolderURI *desktop_vuri,
3964 gboolean *the_is_directory_file,
3965 Folder **the_folder,
3967 GnomeVFSResult *result,
3968 GnomeVFSContext *context)
3970 gboolean is_directory_file;
3971 GnomeVFSURI *ret_uri;
3972 Folder *folder = NULL;
3975 entry = get_entry_unlocked (desktop_vuri,
3983 if (gnome_vfs_context_check_cancellation (context)) {
3984 *result = GNOME_VFS_ERROR_CANCELLED;
3988 if (the_folder != NULL)
3989 *the_folder = folder;
3991 if (the_entry != NULL)
3993 if (the_is_directory_file != NULL)
3994 *the_is_directory_file = is_directory_file;
3996 if (is_directory_file &&
3997 entry->type == ENTRY_FOLDER) {
4000 folder = (Folder *)entry;
4002 if (the_folder != NULL)
4003 *the_folder = folder;
4005 /* we'll be doing something write like */
4006 if (folder->read_only &&
4008 *result = GNOME_VFS_ERROR_READ_ONLY;
4015 if (gnome_vfs_context_check_cancellation (context)) {
4016 *result = GNOME_VFS_ERROR_CANCELLED;
4020 if ( ! make_dirfile_private (info, folder)) {
4021 *result = GNOME_VFS_ERROR_GENERIC;
4024 fname = g_build_filename (g_get_home_dir (),
4025 folder->desktop_file,
4027 ret_uri = gnome_vfs_uri_new (fname);
4032 desktop_file = get_directory_file_unlocked (info, folder);
4033 if (desktop_file != NULL) {
4034 char *s = gnome_vfs_get_uri_from_local_path
4037 g_free (desktop_file);
4039 ret_uri = gnome_vfs_uri_new (s);
4044 *result = GNOME_VFS_ERROR_NOT_FOUND;
4047 } else if (entry->type == ENTRY_FILE) {
4048 EntryFile *efile = (EntryFile *)entry;
4051 /* we'll be doing something write like */
4052 if (folder != NULL &&
4053 folder->read_only &&
4055 *result = GNOME_VFS_ERROR_READ_ONLY;
4059 if (gnome_vfs_context_check_cancellation (context)) {
4060 *result = GNOME_VFS_ERROR_CANCELLED;
4065 ! make_file_private (info, efile)) {
4066 *result = GNOME_VFS_ERROR_GENERIC;
4070 s = gnome_vfs_get_uri_from_local_path (efile->filename);
4071 ret_uri = gnome_vfs_uri_new (s);
4076 if (the_folder != NULL)
4077 *the_folder = (Folder *)entry;
4078 *result = GNOME_VFS_ERROR_IS_DIRECTORY;
4084 remove_file (Folder *folder, const char *basename)
4089 if (folder->includes_ht != NULL) {
4090 li = g_hash_table_lookup (folder->includes_ht, basename);
4092 char *name = li->data;
4093 folder->includes = g_slist_delete_link
4094 (folder->includes, li);
4095 g_hash_table_remove (folder->includes_ht, basename);
4100 if (folder->excludes == NULL) {
4101 folder->excludes = g_hash_table_new_full
4102 (g_str_hash, g_str_equal,
4103 (GDestroyNotify)g_free,
4106 s = g_strdup (basename);
4107 g_hash_table_replace (folder->excludes, s, s);
4111 add_file (Folder *folder, const char *basename)
4115 if (folder->includes_ht != NULL) {
4116 li = g_hash_table_lookup (folder->includes_ht, basename);
4121 char *str = g_strdup (basename);
4123 g_slist_prepend (folder->includes, str);
4124 if (folder->includes_ht == NULL) {
4125 folder->includes_ht =
4126 g_hash_table_new_full (g_str_hash,
4131 g_hash_table_replace (folder->includes_ht,
4132 str, folder->includes);
4134 if (folder->excludes != NULL)
4135 g_hash_table_remove (folder->excludes, basename);
4138 typedef struct _FileHandle FileHandle;
4139 struct _FileHandle {
4141 GnomeVFSMethodHandle *handle;
4144 gboolean is_directory_file;
4148 make_handle (GnomeVFSMethodHandle **method_handle,
4149 GnomeVFSMethodHandle *file_handle,
4152 gboolean is_directory_file,
4155 if (file_handle != NULL) {
4156 FileHandle *handle = g_new0 (FileHandle, 1);
4158 handle->info = info;
4159 handle->handle = file_handle;
4160 handle->entry = entry_ref (entry);
4161 handle->is_directory_file = is_directory_file;
4162 handle->write = write;
4164 *method_handle = (GnomeVFSMethodHandle *) handle;
4166 *method_handle = NULL;
4171 whack_handle (FileHandle *handle)
4173 entry_unref (handle->entry);
4174 handle->entry = NULL;
4176 handle->handle = NULL;
4177 handle->info = NULL;
4182 static GnomeVFSResult
4183 do_open (GnomeVFSMethod *method,
4184 GnomeVFSMethodHandle **method_handle,
4186 GnomeVFSOpenMode mode,
4187 GnomeVFSContext *context)
4189 GnomeVFSURI *file_uri;
4190 GnomeVFSResult result = GNOME_VFS_OK;
4193 gboolean is_directory_file;
4194 GnomeVFSMethodHandle *file_handle = NULL;
4197 VFOLDER_URI_PARSE (uri, &vuri);
4199 /* These can't be very nice FILE names */
4200 if (vuri.file == NULL ||
4202 return GNOME_VFS_ERROR_INVALID_URI;
4204 info = get_vfolder_info (vuri.scheme, &result, context);
4208 if (mode & GNOME_VFS_OPEN_WRITE &&
4209 (info->read_only || vuri.is_all_scheme))
4210 return GNOME_VFS_ERROR_READ_ONLY;
4212 G_LOCK (vfolder_lock);
4213 file_uri = desktop_uri_to_file_uri (info,
4217 NULL /* the_folder */,
4218 mode & GNOME_VFS_OPEN_WRITE,
4222 if (file_uri == NULL) {
4223 G_UNLOCK (vfolder_lock);
4227 result = (* parent_method->open) (parent_method,
4233 if (result == GNOME_VFS_ERROR_CANCELLED) {
4234 G_UNLOCK (vfolder_lock);
4235 gnome_vfs_uri_unref (file_uri);
4239 make_handle (method_handle,
4244 mode & GNOME_VFS_OPEN_WRITE);
4246 gnome_vfs_uri_unref (file_uri);
4249 vfolder_info_write_user (info);
4252 G_UNLOCK (vfolder_lock);
4258 remove_from_all_except (Folder *root,
4264 if (root != except) {
4265 remove_file (root, name);
4266 if (root->up_to_date) {
4267 for (li = root->entries; li != NULL; li = li->next) {
4268 Entry *entry = li->data;
4269 if (strcmp (name, entry->name) == 0) {
4272 (root->entries, li);
4279 for (li = root->subfolders; li != NULL; li = li->next) {
4280 Folder *subfolder = li->data;
4282 remove_from_all_except (subfolder, name, except);
4286 static GnomeVFSResult
4287 do_create (GnomeVFSMethod *method,
4288 GnomeVFSMethodHandle **method_handle,
4290 GnomeVFSOpenMode mode,
4293 GnomeVFSContext *context)
4295 GnomeVFSResult result = GNOME_VFS_OK;
4296 GnomeVFSMethodHandle *file_handle;
4297 GnomeVFSURI *file_uri;
4306 VFOLDER_URI_PARSE (uri, &vuri);
4308 /* These can't be very nice FILE names */
4309 if (vuri.file == NULL ||
4311 return GNOME_VFS_ERROR_INVALID_URI;
4313 if ( ! check_ext (vuri.file, ".desktop") &&
4314 ! strcmp (vuri.file, ".directory") == 0) {
4315 return GNOME_VFS_ERROR_INVALID_URI;
4318 /* all scheme is read only */
4319 if (vuri.is_all_scheme)
4320 return GNOME_VFS_ERROR_READ_ONLY;
4322 info = get_vfolder_info (vuri.scheme, &result, context);
4326 if (info->user_filename == NULL ||
4328 return GNOME_VFS_ERROR_READ_ONLY;
4330 parent = resolve_folder (info, vuri.path,
4331 TRUE /* ignore_basename */,
4336 if (parent->read_only)
4337 return GNOME_VFS_ERROR_READ_ONLY;
4339 if (strcmp (vuri.file, ".directory") == 0) {
4342 G_LOCK (vfolder_lock);
4346 desktop_file = get_directory_file_unlocked (info, parent);
4347 if (desktop_file != NULL) {
4348 g_free (desktop_file);
4349 G_UNLOCK (vfolder_lock);
4350 return GNOME_VFS_ERROR_FILE_EXISTS;
4354 if ( ! make_dirfile_private (info, parent)) {
4355 G_UNLOCK (vfolder_lock);
4356 return GNOME_VFS_ERROR_GENERIC;
4358 fname = g_build_filename (g_get_home_dir (),
4359 parent->desktop_file,
4361 s = gnome_vfs_get_uri_from_local_path (fname);
4362 file_uri = gnome_vfs_uri_new (s);
4366 if (file_uri == NULL) {
4367 G_UNLOCK (vfolder_lock);
4368 return GNOME_VFS_ERROR_GENERIC;
4371 result = (* parent_method->create) (parent_method,
4378 gnome_vfs_uri_unref (file_uri);
4380 make_handle (method_handle,
4384 TRUE /* is_directory_file */,
4388 vfolder_info_write_user (info);
4390 G_UNLOCK (vfolder_lock);
4395 ensure_folder (info, parent,
4396 FALSE /* subfolders */,
4398 FALSE /* ignore_unallocated */);
4400 entry = find_entry (parent->entries, vuri.file);
4402 if (entry != NULL &&
4403 entry->type == ENTRY_FOLDER)
4404 return GNOME_VFS_ERROR_IS_DIRECTORY;
4406 efile = (EntryFile *)entry;
4408 if (efile != NULL) {
4410 return GNOME_VFS_ERROR_FILE_EXISTS;
4412 G_LOCK (vfolder_lock);
4413 if ( ! make_file_private (info, efile)) {
4414 G_UNLOCK (vfolder_lock);
4415 return GNOME_VFS_ERROR_GENERIC;
4418 s = gnome_vfs_get_uri_from_local_path (efile->filename);
4419 file_uri = gnome_vfs_uri_new (s);
4422 if (file_uri == NULL) {
4423 G_UNLOCK (vfolder_lock);
4424 return GNOME_VFS_ERROR_GENERIC;
4427 result = (* parent_method->create) (parent_method,
4434 gnome_vfs_uri_unref (file_uri);
4436 make_handle (method_handle,
4440 FALSE /* is_directory_file */,
4443 G_UNLOCK (vfolder_lock);
4448 G_LOCK (vfolder_lock);
4450 li = g_hash_table_lookup (info->entries_ht, vuri.file);
4452 if (exclusive && li != NULL) {
4453 G_UNLOCK (vfolder_lock);
4454 return GNOME_VFS_ERROR_FILE_EXISTS;
4458 efile = file_new (vuri.file);
4459 vfolder_info_insert_entry (info, efile);
4460 entry_unref ((Entry *)efile);
4465 /* this will make a private name for this */
4466 if ( ! make_file_private (info, efile)) {
4467 G_UNLOCK (vfolder_lock);
4468 return GNOME_VFS_ERROR_GENERIC;
4471 add_file (parent, vuri.file);
4472 parent->sorted = FALSE;
4474 if (parent->up_to_date)
4475 parent->entries = g_slist_prepend (parent->entries, efile);
4477 /* if we created a brand new name, then we exclude it
4478 * from everywhere else to ensure overall sanity */
4480 remove_from_all_except (info->root, vuri.file, parent);
4482 s = gnome_vfs_get_uri_from_local_path (efile->filename);
4483 file_uri = gnome_vfs_uri_new (s);
4486 result = (* parent_method->create) (parent_method,
4493 gnome_vfs_uri_unref (file_uri);
4495 make_handle (method_handle,
4499 FALSE /* is_directory_file */,
4502 vfolder_info_write_user (info);
4504 G_UNLOCK (vfolder_lock);
4509 static GnomeVFSResult
4510 do_close (GnomeVFSMethod *method,
4511 GnomeVFSMethodHandle *method_handle,
4512 GnomeVFSContext *context)
4514 GnomeVFSResult result;
4515 FileHandle *handle = (FileHandle *)method_handle;
4516 if (method_handle == (GnomeVFSMethodHandle *)method)
4517 return GNOME_VFS_OK;
4519 G_LOCK (vfolder_lock);
4521 result = (* parent_method->close) (parent_method,
4524 handle->handle = NULL;
4526 /* we reread the Categories keyword */
4527 if (handle->write &&
4528 handle->entry != NULL &&
4529 handle->entry->type == ENTRY_FILE) {
4530 EntryFile *efile = (EntryFile *)handle->entry;
4532 readitem_entry (efile->filename,
4537 set_keywords (efile, categories);
4538 g_free (categories);
4539 /* FIXME: what about OnlyShowIn */
4541 /* FIXME: check if the keywords changed, if not, do
4544 /* Perhaps a bit drastic */
4545 /* also this emits the CHANGED monitor signal */
4546 invalidate_folder_T (handle->info->root);
4548 /* the file changed monitor will happen by itself
4549 * as the underlying file is changed */
4550 } else if (handle->write &&
4551 handle->entry != NULL &&
4552 handle->entry->type == ENTRY_FOLDER &&
4553 handle->is_directory_file) {
4554 /* if we're monitoring this directory, emit the CHANGED
4555 * monitor thing, it will also emit a changed on
4556 * the file itself. It is better to emit changed
4558 emit_monitor ((Folder *)(handle->entry),
4559 GNOME_VFS_MONITOR_EVENT_CHANGED);
4562 whack_handle (handle);
4564 G_UNLOCK (vfolder_lock);
4570 fill_buffer (gpointer buffer,
4571 GnomeVFSFileSize num_bytes,
4572 GnomeVFSFileSize *bytes_read)
4576 for (i = 0; i < num_bytes; i++) {
4577 if (rand () % 32 == 0 ||
4581 buf[i] = ((rand()>>4) % 94) + 32;
4583 if (bytes_read != 0)
4587 static GnomeVFSResult
4588 do_read (GnomeVFSMethod *method,
4589 GnomeVFSMethodHandle *method_handle,
4591 GnomeVFSFileSize num_bytes,
4592 GnomeVFSFileSize *bytes_read,
4593 GnomeVFSContext *context)
4595 GnomeVFSResult result;
4596 FileHandle *handle = (FileHandle *)method_handle;
4598 if (method_handle == (GnomeVFSMethodHandle *)method) {
4599 if ((rand () >> 4) & 0x3) {
4600 fill_buffer (buffer, num_bytes, bytes_read);
4601 return GNOME_VFS_OK;
4603 return GNOME_VFS_ERROR_EOF;
4607 result = (* parent_method->read) (parent_method,
4616 static GnomeVFSResult
4617 do_write (GnomeVFSMethod *method,
4618 GnomeVFSMethodHandle *method_handle,
4619 gconstpointer buffer,
4620 GnomeVFSFileSize num_bytes,
4621 GnomeVFSFileSize *bytes_written,
4622 GnomeVFSContext *context)
4624 GnomeVFSResult result;
4625 FileHandle *handle = (FileHandle *)method_handle;
4627 if (method_handle == (GnomeVFSMethodHandle *)method)
4628 return GNOME_VFS_OK;
4630 result = (* parent_method->write) (parent_method,
4640 static GnomeVFSResult
4641 do_seek (GnomeVFSMethod *method,
4642 GnomeVFSMethodHandle *method_handle,
4643 GnomeVFSSeekPosition whence,
4644 GnomeVFSFileOffset offset,
4645 GnomeVFSContext *context)
4647 GnomeVFSResult result;
4648 FileHandle *handle = (FileHandle *)method_handle;
4650 if (method_handle == (GnomeVFSMethodHandle *)method)
4651 return GNOME_VFS_OK;
4653 result = (* parent_method->seek) (parent_method,
4661 static GnomeVFSResult
4662 do_tell (GnomeVFSMethod *method,
4663 GnomeVFSMethodHandle *method_handle,
4664 GnomeVFSFileOffset *offset_return)
4666 GnomeVFSResult result;
4667 FileHandle *handle = (FileHandle *)method_handle;
4669 result = (* parent_method->tell) (parent_method,
4677 static GnomeVFSResult
4678 do_truncate_handle (GnomeVFSMethod *method,
4679 GnomeVFSMethodHandle *method_handle,
4680 GnomeVFSFileSize where,
4681 GnomeVFSContext *context)
4683 GnomeVFSResult result;
4684 FileHandle *handle = (FileHandle *)method_handle;
4686 if (method_handle == (GnomeVFSMethodHandle *)method)
4687 return GNOME_VFS_OK;
4689 result = (* parent_method->truncate_handle) (parent_method,
4697 static GnomeVFSResult
4698 do_truncate (GnomeVFSMethod *method,
4700 GnomeVFSFileSize where,
4701 GnomeVFSContext *context)
4703 GnomeVFSURI *file_uri;
4704 GnomeVFSResult result = GNOME_VFS_OK;
4709 VFOLDER_URI_PARSE (uri, &vuri);
4711 /* These can't be very nice FILE names */
4712 if (vuri.file == NULL ||
4714 return GNOME_VFS_ERROR_INVALID_URI;
4716 if (vuri.is_all_scheme)
4717 return GNOME_VFS_ERROR_READ_ONLY;
4719 info = get_vfolder_info (vuri.scheme, &result, context);
4723 if (info->read_only)
4724 return GNOME_VFS_ERROR_READ_ONLY;
4726 G_LOCK (vfolder_lock);
4727 file_uri = desktop_uri_to_file_uri (info,
4730 NULL /* the_is_directory_file */,
4731 NULL /* the_folder */,
4732 TRUE /* privatize */,
4735 G_UNLOCK (vfolder_lock);
4737 if (file_uri == NULL)
4740 result = (* parent_method->truncate) (parent_method,
4745 gnome_vfs_uri_unref (file_uri);
4748 G_LOCK (vfolder_lock);
4749 vfolder_info_write_user (info);
4750 G_UNLOCK (vfolder_lock);
4753 if (entry->type == ENTRY_FILE) {
4754 EntryFile *efile = (EntryFile *)entry;
4756 G_LOCK (vfolder_lock);
4757 g_slist_free (efile->keywords);
4758 efile->keywords = NULL;
4759 G_UNLOCK (vfolder_lock);
4762 /* Perhaps a bit drastic, but oh well */
4763 invalidate_folder (info->root);
4768 typedef struct _DirHandle DirHandle;
4773 GnomeVFSFileInfoOptions options;
4775 /* List of Entries */
4780 static GnomeVFSResult
4781 do_open_directory (GnomeVFSMethod *method,
4782 GnomeVFSMethodHandle **method_handle,
4784 GnomeVFSFileInfoOptions options,
4785 GnomeVFSContext *context)
4787 GnomeVFSResult result = GNOME_VFS_OK;
4794 VFOLDER_URI_PARSE (uri, &vuri);
4796 info = get_vfolder_info (vuri.scheme, &result, context);
4800 /* In the all- scheme just list all filenames */
4801 if (vuri.is_all_scheme) {
4802 if (any_subdir (vuri.path))
4803 return GNOME_VFS_ERROR_NOT_FOUND;
4805 dh = g_new0 (DirHandle, 1);
4807 dh->options = options;
4810 G_LOCK (vfolder_lock);
4811 dh->list = g_slist_copy (info->entries);
4812 g_slist_foreach (dh->list, (GFunc)entry_ref, NULL);
4813 dh->current = dh->list;
4814 G_UNLOCK (vfolder_lock);
4816 *method_handle = (GnomeVFSMethodHandle*) dh;
4817 return GNOME_VFS_OK;
4820 folder = resolve_folder (info, vuri.path,
4821 FALSE /* ignore_basename */,
4826 /* Make sure we have the entries and sorted here */
4827 ensure_folder_sort (info, folder);
4829 dh = g_new0 (DirHandle, 1);
4831 dh->options = options;
4833 G_LOCK (vfolder_lock);
4834 dh->folder = (Folder *)entry_ref ((Entry *)folder);
4835 dh->list = g_slist_copy (folder->entries);
4836 g_slist_foreach (folder->entries, (GFunc)entry_ref, NULL);
4837 G_UNLOCK (vfolder_lock);
4839 desktop_file = get_directory_file (info, folder);
4840 if (desktop_file != NULL) {
4841 EntryFile *efile = file_new (".directory");
4842 dh->list = g_slist_prepend (dh->list, efile);
4843 g_free (desktop_file);
4846 dh->current = dh->list;
4848 *method_handle = (GnomeVFSMethodHandle*) dh;
4850 return GNOME_VFS_OK;
4853 static GnomeVFSResult
4854 do_close_directory (GnomeVFSMethod *method,
4855 GnomeVFSMethodHandle *method_handle,
4856 GnomeVFSContext *context)
4860 dh = (DirHandle*) method_handle;
4862 G_LOCK (vfolder_lock);
4864 g_slist_foreach (dh->list, (GFunc)entry_unref, NULL);
4865 g_slist_free (dh->list);
4870 if (dh->folder != NULL)
4871 entry_unref ((Entry *)dh->folder);
4878 G_UNLOCK (vfolder_lock);
4880 return GNOME_VFS_OK;
4883 static GnomeVFSResult
4884 do_read_directory (GnomeVFSMethod *method,
4885 GnomeVFSMethodHandle *method_handle,
4886 GnomeVFSFileInfo *file_info,
4887 GnomeVFSContext *context)
4891 GnomeVFSFileInfoOptions options;
4893 dh = (DirHandle*) method_handle;
4895 read_directory_again:
4897 if (dh->current == NULL) {
4898 return GNOME_VFS_ERROR_EOF;
4901 entry = dh->current->data;
4902 dh->current = dh->current->next;
4904 options = dh->options;
4906 if (entry->type == ENTRY_FILE &&
4907 ((EntryFile *)entry)->filename != NULL) {
4908 EntryFile *efile = (EntryFile *)entry;
4909 char *furi = gnome_vfs_get_uri_from_local_path (efile->filename);
4910 GnomeVFSURI *uri = gnome_vfs_uri_new (furi);
4912 /* we always get mime-type by forcing it below */
4913 if (options & GNOME_VFS_FILE_INFO_GET_MIME_TYPE)
4914 options &= ~GNOME_VFS_FILE_INFO_GET_MIME_TYPE;
4916 file_info->valid_fields = GNOME_VFS_FILE_INFO_FIELDS_NONE;
4918 /* Get the file info for this */
4919 (* parent_method->get_file_info) (parent_method,
4925 /* we ignore errors from this since the file_info just
4926 * won't be filled completely if there's an error, that's all */
4928 g_free (file_info->mime_type);
4929 file_info->mime_type = g_strdup ("application/x-gnome-app-info");
4930 file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
4932 /* Now we wipe those fields we don't support */
4933 file_info->valid_fields &= ~(UNSUPPORTED_INFO_FIELDS);
4935 gnome_vfs_uri_unref (uri);
4937 } else if (entry->type == ENTRY_FILE) {
4938 file_info->valid_fields = GNOME_VFS_FILE_INFO_FIELDS_NONE;
4940 file_info->name = g_strdup (entry->name);
4941 GNOME_VFS_FILE_INFO_SET_LOCAL (file_info, TRUE);
4943 file_info->type = GNOME_VFS_FILE_TYPE_REGULAR;
4944 file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_TYPE;
4946 /* FIXME: Is this correct? isn't there an xdg mime type? */
4947 file_info->mime_type = g_strdup ("application/x-gnome-app-info");
4948 file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
4950 /* FIXME: get some ctime/mtime */
4951 } else /* ENTRY_FOLDER */ {
4952 Folder *folder = (Folder *)entry;
4954 /* Skip empty folders if they have
4956 if (folder->dont_show_if_empty) {
4957 /* Make sure we have the entries */
4958 ensure_folder (dh->info, folder,
4959 FALSE /* subfolders */,
4961 FALSE /* ignore_unallocated */);
4963 if (folder->entries == NULL) {
4964 /* start this function over on the
4966 goto read_directory_again;
4970 file_info->valid_fields = GNOME_VFS_FILE_INFO_FIELDS_NONE;
4972 file_info->name = g_strdup (entry->name);
4973 GNOME_VFS_FILE_INFO_SET_LOCAL (file_info, TRUE);
4975 file_info->type = GNOME_VFS_FILE_TYPE_DIRECTORY;
4976 file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_TYPE;
4978 file_info->mime_type = g_strdup ("x-directory/vfolder-desktop");
4979 file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
4981 file_info->ctime = dh->info->modification_time;
4982 file_info->mtime = dh->info->modification_time;
4983 file_info->valid_fields |= (GNOME_VFS_FILE_INFO_FIELDS_CTIME |
4984 GNOME_VFS_FILE_INFO_FIELDS_MTIME);
4987 return GNOME_VFS_OK;
4990 static GnomeVFSResult
4991 do_get_file_info (GnomeVFSMethod *method,
4993 GnomeVFSFileInfo *file_info,
4994 GnomeVFSFileInfoOptions options,
4995 GnomeVFSContext *context)
4997 GnomeVFSURI *file_uri;
4998 GnomeVFSResult result = GNOME_VFS_OK;
5003 VFOLDER_URI_PARSE (uri, &vuri);
5005 info = get_vfolder_info (vuri.scheme, &result, context);
5009 G_LOCK (vfolder_lock);
5010 file_uri = desktop_uri_to_file_uri (info,
5012 NULL /* the_entry */,
5013 NULL /* the_is_directory_file */,
5015 FALSE /* privatize */,
5018 G_UNLOCK (vfolder_lock);
5020 if (file_uri == NULL &&
5021 result != GNOME_VFS_ERROR_IS_DIRECTORY)
5024 if (file_uri != NULL) {
5025 /* we always get mime-type by forcing it below */
5026 if (options & GNOME_VFS_FILE_INFO_GET_MIME_TYPE)
5027 options &= ~GNOME_VFS_FILE_INFO_GET_MIME_TYPE;
5029 result = (* parent_method->get_file_info) (parent_method,
5035 g_free (file_info->mime_type);
5036 file_info->mime_type = g_strdup ("application/x-gnome-app-info");
5037 file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
5039 /* Now we wipe those fields we don't support */
5040 file_info->valid_fields &= ~(UNSUPPORTED_INFO_FIELDS);
5042 gnome_vfs_uri_unref (file_uri);
5045 } else if (folder != NULL) {
5046 file_info->valid_fields = GNOME_VFS_FILE_INFO_FIELDS_NONE;
5048 file_info->name = g_strdup (folder->entry.name);
5049 GNOME_VFS_FILE_INFO_SET_LOCAL (file_info, TRUE);
5051 file_info->type = GNOME_VFS_FILE_TYPE_DIRECTORY;
5052 file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_TYPE;
5054 file_info->mime_type = g_strdup ("x-directory/vfolder-desktop");
5055 file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
5057 file_info->ctime = info->modification_time;
5058 file_info->mtime = info->modification_time;
5059 file_info->valid_fields |= (GNOME_VFS_FILE_INFO_FIELDS_CTIME |
5060 GNOME_VFS_FILE_INFO_FIELDS_MTIME);
5062 return GNOME_VFS_OK;
5064 return GNOME_VFS_ERROR_NOT_FOUND;
5068 static GnomeVFSResult
5069 do_get_file_info_from_handle (GnomeVFSMethod *method,
5070 GnomeVFSMethodHandle *method_handle,
5071 GnomeVFSFileInfo *file_info,
5072 GnomeVFSFileInfoOptions options,
5073 GnomeVFSContext *context)
5075 GnomeVFSResult result;
5076 FileHandle *handle = (FileHandle *)method_handle;
5078 if (method_handle == (GnomeVFSMethodHandle *)method) {
5079 g_free (file_info->mime_type);
5080 file_info->mime_type = g_strdup ("text/plain");
5081 file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
5082 return GNOME_VFS_OK;
5085 /* we always get mime-type by forcing it below */
5086 if (options & GNOME_VFS_FILE_INFO_GET_MIME_TYPE)
5087 options &= ~GNOME_VFS_FILE_INFO_GET_MIME_TYPE;
5089 result = (* parent_method->get_file_info_from_handle) (parent_method,
5095 /* any file is of the .desktop type */
5096 g_free (file_info->mime_type);
5097 file_info->mime_type = g_strdup ("application/x-gnome-app-info");
5098 file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
5100 /* Now we wipe those fields we don't support */
5101 file_info->valid_fields &= ~(UNSUPPORTED_INFO_FIELDS);
5108 do_is_local (GnomeVFSMethod *method,
5109 const GnomeVFSURI *uri)
5115 try_free_folder_monitors_create_unlocked (VFolderInfo *info,
5120 list = g_slist_copy (info->free_folder_monitors);
5122 for (li = list; li != NULL; li = li->next) {
5123 FileMonitorHandle *handle = li->data;
5126 GnomeVFSResult result;
5128 /* Evil! EVIL URI PARSING. this will eat a lot of stack if we
5129 * have lots of free monitors */
5131 VFOLDER_URI_PARSE (handle->uri, &vuri);
5133 f = resolve_folder (info,
5135 FALSE /* ignore_basename */,
5142 info->free_folder_monitors =
5143 g_slist_remove (info->free_folder_monitors, handle);
5144 ((Entry *)folder)->monitors =
5145 g_slist_prepend (((Entry *)folder)->monitors, handle);
5147 handle->exists = TRUE;
5148 gnome_vfs_monitor_callback ((GnomeVFSMethodHandle *)handle,
5150 GNOME_VFS_MONITOR_EVENT_CREATED);
5155 static GnomeVFSResult
5156 do_make_directory (GnomeVFSMethod *method,
5159 GnomeVFSContext *context)
5161 GnomeVFSResult result = GNOME_VFS_OK;
5163 Folder *parent, *folder;
5166 VFOLDER_URI_PARSE (uri, &vuri);
5168 if (vuri.is_all_scheme)
5169 return GNOME_VFS_ERROR_READ_ONLY;
5171 info = get_vfolder_info (vuri.scheme, &result, context);
5175 if (info->user_filename == NULL ||
5177 return GNOME_VFS_ERROR_READ_ONLY;
5179 parent = resolve_folder (info, vuri.path,
5180 TRUE /* ignore_basename */,
5184 else if (parent->read_only)
5185 return GNOME_VFS_ERROR_READ_ONLY;
5187 G_LOCK (vfolder_lock);
5189 folder = (Folder *)find_entry (parent->subfolders,
5191 if (folder != NULL) {
5192 G_UNLOCK (vfolder_lock);
5193 return GNOME_VFS_ERROR_FILE_EXISTS;
5196 folder = folder_new (vuri.file);
5197 parent->subfolders = g_slist_append (parent->subfolders, folder);
5198 folder->parent = parent;
5199 parent->up_to_date = FALSE;
5201 try_free_folder_monitors_create_unlocked (info, folder);
5203 /* parent changed */
5204 emit_monitor (parent, GNOME_VFS_MONITOR_EVENT_CHANGED);
5206 vfolder_info_write_user (info);
5207 G_UNLOCK (vfolder_lock);
5209 return GNOME_VFS_OK;
5212 static GnomeVFSResult
5213 do_remove_directory (GnomeVFSMethod *method,
5215 GnomeVFSContext *context)
5217 GnomeVFSResult result = GNOME_VFS_OK;
5222 VFOLDER_URI_PARSE (uri, &vuri);
5224 if (vuri.is_all_scheme)
5225 return GNOME_VFS_ERROR_READ_ONLY;
5227 info = get_vfolder_info (vuri.scheme, &result, context);
5231 if (info->user_filename == NULL ||
5233 return GNOME_VFS_ERROR_READ_ONLY;
5235 G_LOCK (vfolder_lock);
5237 folder = resolve_folder (info, vuri.path,
5238 FALSE /* ignore_basename */,
5240 if (folder == NULL) {
5241 G_UNLOCK (vfolder_lock);
5245 if (folder->read_only ||
5246 (folder->parent != NULL &&
5247 folder->parent->read_only)) {
5248 G_UNLOCK (vfolder_lock);
5249 return GNOME_VFS_ERROR_READ_ONLY;
5252 /* don't make removing directories easy */
5253 if (folder->desktop_file != NULL) {
5254 G_UNLOCK (vfolder_lock);
5255 return GNOME_VFS_ERROR_DIRECTORY_NOT_EMPTY;
5258 /* Make sure we have the entries */
5259 ensure_folder_unlocked (info, folder,
5260 FALSE /* subfolders */,
5262 FALSE /* ignore_unallocated */);
5264 /* don't make removing directories easy */
5265 if (folder->entries != NULL) {
5266 G_UNLOCK (vfolder_lock);
5267 return GNOME_VFS_ERROR_DIRECTORY_NOT_EMPTY;
5270 emit_and_delete_monitor (info, folder);
5272 if (folder->only_unallocated) {
5273 GSList *li = g_slist_find (info->unallocated_folders,
5276 info->unallocated_folders = g_slist_delete_link
5277 (info->unallocated_folders, li);
5278 entry_unref ((Entry *)folder);
5282 if (folder == info->root) {
5284 entry_unref ((Entry *)folder);
5285 info->root = folder_new ("Root");
5287 Folder *parent = folder->parent;
5289 g_assert (parent != NULL);
5291 parent->subfolders =
5292 g_slist_remove (parent->subfolders, folder);
5294 parent->up_to_date = FALSE;
5296 entry_unref ((Entry *)folder);
5298 /* parent changed */
5299 emit_monitor (parent, GNOME_VFS_MONITOR_EVENT_CHANGED);
5302 vfolder_info_write_user (info);
5304 G_UNLOCK (vfolder_lock);
5306 return GNOME_VFS_OK;
5309 /* a fairly evil function that does the whole move bit by copy and
5311 static GnomeVFSResult
5312 long_move (GnomeVFSMethod *method,
5313 VFolderURI *old_vuri,
5314 VFolderURI *new_vuri,
5315 gboolean force_replace,
5316 GnomeVFSContext *context)
5318 GnomeVFSResult result;
5319 GnomeVFSMethodHandle *handle;
5320 GnomeVFSURI *file_uri;
5327 info = get_vfolder_info (old_vuri->scheme, &result, context);
5331 G_LOCK (vfolder_lock);
5332 file_uri = desktop_uri_to_file_uri (info,
5334 NULL /* the_entry */,
5335 NULL /* the_is_directory_file */,
5336 NULL /* the_folder */,
5337 FALSE /* privatize */,
5340 G_UNLOCK (vfolder_lock);
5342 if (file_uri == NULL)
5345 path = gnome_vfs_uri_get_path (file_uri);
5347 gnome_vfs_uri_unref (file_uri);
5348 return GNOME_VFS_ERROR_INVALID_URI;
5351 fd = open (path, O_RDONLY);
5353 gnome_vfs_uri_unref (file_uri);
5354 return gnome_vfs_result_from_errno ();
5357 gnome_vfs_uri_unref (file_uri);
5359 info->inhibit_write++;
5361 result = method->create (method,
5364 GNOME_VFS_OPEN_WRITE,
5365 force_replace /* exclusive */,
5368 if (result != GNOME_VFS_OK) {
5370 info->inhibit_write--;
5374 while ((bytes = read (fd, buf, BUFSIZ)) > 0) {
5375 GnomeVFSFileSize bytes_written = 0;
5376 result = method->write (method,
5382 if (result == GNOME_VFS_OK &&
5383 bytes_written != bytes)
5384 result = GNOME_VFS_ERROR_NO_SPACE;
5385 if (result != GNOME_VFS_OK) {
5387 method->close (method, handle, context);
5388 /* FIXME: is this completely correct ? */
5389 method->unlink (method,
5392 G_LOCK (vfolder_lock);
5393 info->inhibit_write--;
5394 vfolder_info_write_user (info);
5395 G_UNLOCK (vfolder_lock);
5402 result = method->close (method, handle, context);
5403 if (result != GNOME_VFS_OK) {
5404 G_LOCK (vfolder_lock);
5405 info->inhibit_write--;
5406 vfolder_info_write_user (info);
5407 G_UNLOCK (vfolder_lock);
5411 result = method->unlink (method, old_vuri->uri, context);
5413 G_LOCK (vfolder_lock);
5414 info->inhibit_write--;
5415 vfolder_info_write_user (info);
5416 G_UNLOCK (vfolder_lock);
5421 static GnomeVFSResult
5422 move_directory_file (VFolderInfo *info,
5426 if (old_folder->desktop_file == NULL)
5427 return GNOME_VFS_ERROR_NOT_FOUND;
5429 /* "move" the desktop file */
5430 g_free (new_folder->desktop_file);
5431 new_folder->desktop_file = old_folder->desktop_file;
5432 old_folder->desktop_file = NULL;
5434 /* is this too drastic, it will requery the folder? */
5435 new_folder->up_to_date = FALSE;
5436 old_folder->up_to_date = FALSE;
5438 emit_monitor (new_folder, GNOME_VFS_MONITOR_EVENT_CHANGED);
5439 emit_monitor (old_folder, GNOME_VFS_MONITOR_EVENT_CHANGED);
5441 vfolder_info_write_user (info);
5443 return GNOME_VFS_OK;
5447 is_sub (Folder *master, Folder *sub)
5451 for (li = master->subfolders; li != NULL; li = li->next) {
5452 Folder *subfolder = li->data;
5454 if (subfolder == sub ||
5455 is_sub (subfolder, sub))
5462 static GnomeVFSResult
5463 move_folder (VFolderInfo *info,
5464 Folder *old_folder, Entry *old_entry,
5465 Folder *new_folder, Entry *new_entry)
5467 Folder *source = (Folder *)old_entry;
5470 if (new_entry != NULL &&
5471 new_entry->type != ENTRY_FOLDER)
5472 return GNOME_VFS_ERROR_NOT_A_DIRECTORY;
5474 if (new_entry != NULL) {
5475 target = (Folder *)new_entry;
5477 target = new_folder;
5480 /* move to where we are, yay, we're done :) */
5481 if (source->parent == target)
5482 return GNOME_VFS_OK;
5484 if (source == target ||
5485 is_sub (source, target))
5486 return GNOME_VFS_ERROR_LOOP;
5488 /* this will never happen, but we're paranoid */
5489 if (source->parent == NULL)
5490 return GNOME_VFS_ERROR_LOOP;
5492 source->parent->subfolders = g_slist_remove (source->parent->subfolders,
5494 target->subfolders = g_slist_append (target->subfolders,
5497 source->parent = target;
5499 source->up_to_date = FALSE;
5500 target->up_to_date = FALSE;
5502 emit_monitor (source, GNOME_VFS_MONITOR_EVENT_CHANGED);
5503 emit_monitor (target, GNOME_VFS_MONITOR_EVENT_CHANGED);
5505 vfolder_info_write_user (info);
5507 return GNOME_VFS_OK;
5510 static GnomeVFSResult
5511 do_move (GnomeVFSMethod *method,
5512 GnomeVFSURI *old_uri,
5513 GnomeVFSURI *new_uri,
5514 gboolean force_replace,
5515 GnomeVFSContext *context)
5517 GnomeVFSResult result = GNOME_VFS_OK;
5519 Folder *old_folder, *new_folder;
5520 Entry *old_entry, *new_entry;
5521 gboolean old_is_directory_file, new_is_directory_file;
5522 VFolderURI old_vuri, new_vuri;
5524 VFOLDER_URI_PARSE (old_uri, &old_vuri);
5525 VFOLDER_URI_PARSE (new_uri, &new_vuri);
5527 if (old_vuri.file == NULL)
5528 return GNOME_VFS_ERROR_INVALID_URI;
5530 if (old_vuri.is_all_scheme)
5531 return GNOME_VFS_ERROR_READ_ONLY;
5533 if (strcmp (old_vuri.scheme, new_vuri.scheme) != 0)
5534 return GNOME_VFS_ERROR_NOT_SAME_FILE_SYSTEM;
5536 info = get_vfolder_info (old_vuri.scheme, &result, context);
5540 if (info->read_only)
5541 return GNOME_VFS_ERROR_READ_ONLY;
5543 old_entry = get_entry (&old_vuri,
5545 &old_is_directory_file,
5548 if (old_entry == NULL)
5551 if (old_folder != NULL && old_folder->read_only)
5552 return GNOME_VFS_ERROR_READ_ONLY;
5554 new_entry = get_entry (&new_vuri,
5556 &new_is_directory_file,
5559 if (new_entry == NULL && new_folder == NULL)
5562 if (new_folder != NULL && new_folder->read_only)
5563 return GNOME_VFS_ERROR_READ_ONLY;
5565 if (new_is_directory_file != old_is_directory_file) {
5566 /* this will do another set of lookups
5567 * perhaps this can be done in a nicer way,
5568 * but is this the common case? I don't think so */
5569 return long_move (method, &old_vuri, &new_vuri,
5570 force_replace, context);
5573 if (new_is_directory_file) {
5574 g_assert (old_entry != NULL);
5575 g_assert (new_entry != NULL);
5576 G_LOCK (vfolder_lock);
5577 result = move_directory_file (info,
5578 (Folder *)old_entry,
5579 (Folder *)new_entry);
5580 G_UNLOCK (vfolder_lock);
5584 if (old_entry->type == ENTRY_FOLDER) {
5585 G_LOCK (vfolder_lock);
5586 result = move_folder (info,
5587 old_folder, old_entry,
5588 new_folder, new_entry);
5589 G_UNLOCK (vfolder_lock);
5593 /* move into self, just whack the old one */
5594 if (old_entry == new_entry) {
5596 if (new_folder == old_folder)
5597 return GNOME_VFS_OK;
5599 if ( ! force_replace)
5600 return GNOME_VFS_ERROR_FILE_EXISTS;
5602 G_LOCK (vfolder_lock);
5604 remove_file (old_folder, old_vuri.file);
5606 old_folder->entries = g_slist_remove (old_folder->entries,
5608 entry_unref (old_entry);
5610 emit_monitor (old_folder, GNOME_VFS_MONITOR_EVENT_CHANGED);
5612 vfolder_info_write_user (info);
5614 G_UNLOCK (vfolder_lock);
5616 return GNOME_VFS_OK;
5619 /* this is a simple move */
5620 if (new_entry == NULL ||
5621 new_entry->type == ENTRY_FOLDER) {
5622 if (new_entry != NULL) {
5623 new_folder = (Folder *)new_entry;
5625 /* a file and a totally different one */
5626 if (strcmp (new_vuri.file, old_entry->name) != 0) {
5627 /* yay, a long move */
5628 /* this will do another set of lookups
5629 * perhaps this can be done in a nicer way,
5630 * but is this the common case? I don't think
5632 return long_move (method, &old_vuri, &new_vuri,
5633 force_replace, context);
5638 if (new_folder == old_folder)
5639 return GNOME_VFS_OK;
5641 G_LOCK (vfolder_lock);
5643 remove_file (old_folder, old_entry->name);
5644 add_file (new_folder, old_entry->name);
5646 new_folder->entries = g_slist_prepend (new_folder->entries,
5648 entry_ref (old_entry);
5649 new_folder->sorted = FALSE;
5651 old_folder->entries = g_slist_remove (old_folder->entries,
5653 entry_unref (old_entry);
5655 emit_monitor (new_folder, GNOME_VFS_MONITOR_EVENT_CHANGED);
5656 emit_monitor (old_folder, GNOME_VFS_MONITOR_EVENT_CHANGED);
5658 vfolder_info_write_user (info);
5660 G_UNLOCK (vfolder_lock);
5662 return GNOME_VFS_OK;
5665 /* do we EVER get here? */
5667 /* this will do another set of lookups
5668 * perhaps this can be done in a nicer way,
5669 * but is this the common case? I don't think so */
5670 return long_move (method, &old_vuri, &new_vuri,
5671 force_replace, context);
5674 static GnomeVFSResult
5675 do_unlink (GnomeVFSMethod *method,
5677 GnomeVFSContext *context)
5679 GnomeVFSResult result = GNOME_VFS_OK;
5682 gboolean is_directory_file;
5687 VFOLDER_URI_PARSE (uri, &vuri);
5689 if (vuri.file == NULL)
5690 return GNOME_VFS_ERROR_INVALID_URI;
5692 if (vuri.is_all_scheme == TRUE)
5693 return GNOME_VFS_ERROR_READ_ONLY;
5695 info = get_vfolder_info (vuri.scheme, &result, context);
5698 else if (info->read_only)
5699 return GNOME_VFS_ERROR_READ_ONLY;
5701 entry = get_entry (&vuri,
5707 else if (the_folder != NULL &&
5708 the_folder->read_only)
5709 return GNOME_VFS_ERROR_READ_ONLY;
5711 if (entry->type == ENTRY_FOLDER &&
5712 is_directory_file) {
5713 Folder *folder = (Folder *)entry;
5715 if (folder->desktop_file == NULL)
5716 return GNOME_VFS_ERROR_NOT_FOUND;
5718 G_LOCK (vfolder_lock);
5720 g_free (folder->desktop_file);
5721 folder->desktop_file = NULL;
5723 emit_monitor (folder, GNOME_VFS_MONITOR_EVENT_CHANGED);
5725 vfolder_info_write_user (info);
5727 G_UNLOCK (vfolder_lock);
5729 return GNOME_VFS_OK;
5730 } else if (entry->type == ENTRY_FOLDER) {
5731 return GNOME_VFS_ERROR_IS_DIRECTORY;
5732 } else if (the_folder == NULL) {
5733 return GNOME_VFS_ERROR_NOT_FOUND;
5736 G_LOCK (vfolder_lock);
5738 the_folder->entries = g_slist_remove (the_folder->entries,
5740 entry_unref (entry);
5742 remove_file (the_folder, vuri.file);
5744 emit_monitor (the_folder, GNOME_VFS_MONITOR_EVENT_CHANGED);
5746 /* evil, we must remove this from the unallocated folders as well
5747 * so that it magically doesn't appear there. But it's not so simple.
5748 * We only want to remove it if it isn't in that folder already. */
5749 for (li = info->unallocated_folders;
5752 Folder *folder = li->data;
5755 /* This is actually really evil since ensuring
5756 * an unallocated folder clears all other unallocated
5757 * folders in it's wake. I'm not sure it's worth
5758 * optimizing however */
5759 ensure_folder_unlocked (info, folder,
5760 FALSE /* subfolders */,
5762 FALSE /* ignore_unallocated */);
5763 l = g_slist_find (folder->entries, entry);
5765 remove_file (folder, vuri.file);
5769 emit_file_deleted_monitor (info, entry, the_folder);
5771 /* FIXME: if this was a user file and this is the only
5772 * reference to it, unlink it. */
5774 vfolder_info_write_user (info);
5776 G_UNLOCK (vfolder_lock);
5778 return GNOME_VFS_OK;
5781 static GnomeVFSResult
5782 do_check_same_fs (GnomeVFSMethod *method,
5783 GnomeVFSURI *source_uri,
5784 GnomeVFSURI *target_uri,
5785 gboolean *same_fs_return,
5786 GnomeVFSContext *context)
5788 VFolderURI source_vuri, target_vuri;
5790 *same_fs_return = FALSE;
5792 VFOLDER_URI_PARSE (source_uri, &source_vuri);
5793 VFOLDER_URI_PARSE (target_uri, &target_vuri);
5795 if (strcmp (source_vuri.scheme, target_vuri.scheme) != 0 ||
5796 source_vuri.is_all_scheme != target_vuri.is_all_scheme)
5797 *same_fs_return = FALSE;
5799 *same_fs_return = TRUE;
5801 return GNOME_VFS_OK;
5804 static GnomeVFSResult
5805 do_set_file_info (GnomeVFSMethod *method,
5807 const GnomeVFSFileInfo *info,
5808 GnomeVFSSetFileInfoMask mask,
5809 GnomeVFSContext *context)
5813 VFOLDER_URI_PARSE (uri, &vuri);
5815 if (vuri.file == NULL)
5816 return GNOME_VFS_ERROR_INVALID_URI;
5818 if (mask & GNOME_VFS_SET_FILE_INFO_NAME) {
5819 GnomeVFSResult result = GNOME_VFS_OK;
5820 char *dirname = gnome_vfs_uri_extract_dirname (uri);
5821 GnomeVFSURI *new_uri = gnome_vfs_uri_dup (uri);
5823 G_LOCK (vfolder_lock);
5824 g_free (new_uri->text);
5825 new_uri->text = g_build_path ("/", dirname, info->name, NULL);
5826 G_UNLOCK (vfolder_lock);
5828 result = do_move (method,
5831 FALSE /* force_replace */,
5835 gnome_vfs_uri_unref (new_uri);
5838 /* We don't support setting any of this other permission,
5839 * times and all that voodoo */
5840 return GNOME_VFS_ERROR_NOT_SUPPORTED;
5844 static GnomeVFSResult
5845 do_monitor_add (GnomeVFSMethod *method,
5846 GnomeVFSMethodHandle **method_handle_return,
5848 GnomeVFSMonitorType monitor_type)
5852 GnomeVFSResult result;
5855 GnomeVFSURI *file_uri;
5856 FileMonitorHandle *handle;
5857 gboolean is_directory_file;
5859 VFOLDER_URI_PARSE (uri, &vuri);
5861 info = get_vfolder_info (vuri.scheme, &result, NULL);
5865 if (monitor_type == GNOME_VFS_MONITOR_DIRECTORY) {
5866 G_LOCK (vfolder_lock);
5868 folder = resolve_folder (info,
5870 FALSE /* ignore_basename */,
5874 handle = g_new0 (FileMonitorHandle, 1);
5875 handle->refcount = 2;
5876 handle->uri = gnome_vfs_uri_dup (uri);
5877 handle->dir_monitor = TRUE;
5878 handle->handle = NULL;
5879 handle->filename = NULL;
5881 if (folder == NULL) {
5882 handle->exists = FALSE;
5883 info->free_folder_monitors =
5884 g_slist_prepend (info->free_folder_monitors,
5887 handle->exists = TRUE;
5888 ((Entry *)folder)->monitors =
5889 g_slist_prepend (((Entry *)folder)->monitors,
5893 info->folder_monitors =
5894 g_slist_prepend (info->folder_monitors, handle);
5896 G_UNLOCK (vfolder_lock);
5898 *method_handle_return = (GnomeVFSMethodHandle *) handle;
5900 return GNOME_VFS_OK;
5902 /* These can't be very nice FILE names */
5903 if (vuri.file == NULL ||
5905 return GNOME_VFS_ERROR_INVALID_URI;
5907 G_LOCK (vfolder_lock);
5908 file_uri = desktop_uri_to_file_uri (info,
5912 NULL /* the_folder */,
5917 handle = g_new0 (FileMonitorHandle, 1);
5918 handle->refcount = 2;
5919 handle->uri = gnome_vfs_uri_dup (uri);
5920 handle->dir_monitor = FALSE;
5921 handle->handle = NULL;
5922 handle->filename = g_strdup (vuri.file);
5923 handle->is_directory_file = is_directory_file;
5925 info->file_monitors =
5926 g_slist_prepend (info->file_monitors, handle);
5929 if (file_uri == NULL) {
5930 handle->exists = FALSE;
5931 info->free_file_monitors =
5932 g_slist_prepend (info->free_file_monitors,
5935 char *uri_string = gnome_vfs_uri_to_string (file_uri, 0);
5936 handle->exists = TRUE;
5937 gnome_vfs_monitor_add (&(handle->handle),
5939 GNOME_VFS_MONITOR_FILE,
5942 g_free (uri_string);
5944 entry->monitors = g_slist_prepend (entry->monitors,
5946 gnome_vfs_uri_unref (file_uri);
5949 *method_handle_return = (GnomeVFSMethodHandle *) handle;
5951 G_UNLOCK (vfolder_lock);
5953 return GNOME_VFS_OK;
5957 static GnomeVFSResult
5958 do_monitor_cancel (GnomeVFSMethod *method,
5959 GnomeVFSMethodHandle *method_handle)
5961 FileMonitorHandle *handle;
5964 GnomeVFSResult result;
5968 handle = (FileMonitorHandle *)method_handle;
5970 /* FIXME: is this correct? */
5971 if (method_handle == NULL)
5972 return GNOME_VFS_OK;
5974 VFOLDER_URI_PARSE (handle->uri, &vuri);
5976 info = get_vfolder_info (vuri.scheme, &result, NULL);
5980 if (handle->dir_monitor) {
5981 G_LOCK (vfolder_lock);
5983 folder = resolve_folder (info,
5985 FALSE /* ignore_basename */,
5989 for (li = info->folder_monitors; li != NULL; li = li->next) {
5990 FileMonitorHandle *h = li->data;
5993 info->folder_monitors = g_slist_delete_link
5994 (info->folder_monitors, li);
5995 file_monitor_handle_unref_unlocked (h);
5999 if (folder == NULL) {
6000 for (li = info->free_folder_monitors;
6003 FileMonitorHandle *h = li->data;
6006 info->free_folder_monitors = g_slist_delete_link
6007 (info->free_folder_monitors, li);
6008 file_monitor_handle_unref_unlocked (h);
6012 for (li = ((Entry *)folder)->monitors;
6015 FileMonitorHandle *h = li->data;
6018 ((Entry *)folder)->monitors =
6020 (((Entry *)folder)->monitors, li);
6021 file_monitor_handle_unref_unlocked (h);
6026 G_UNLOCK (vfolder_lock);
6028 return GNOME_VFS_OK;
6030 G_LOCK (vfolder_lock);
6032 for (li = info->file_monitors; li != NULL; li = li->next) {
6033 FileMonitorHandle *h = li->data;
6036 info->file_monitors = g_slist_delete_link
6037 (info->file_monitors, li);
6038 file_monitor_handle_unref_unlocked (h);
6042 for (li = info->free_file_monitors;
6045 FileMonitorHandle *h = li->data;
6048 info->free_file_monitors = g_slist_delete_link
6049 (info->free_file_monitors, li);
6050 file_monitor_handle_unref_unlocked (h);
6054 for (li = info->entries; li != NULL; li = li->next) {
6055 Entry *e = li->data;
6056 GSList *link = g_slist_find (e->monitors, handle);
6061 e->monitors = g_slist_delete_link (e->monitors, link);
6063 file_monitor_handle_unref_unlocked (handle);
6067 G_UNLOCK (vfolder_lock);
6069 /* Note: last unref of our monitor will cancel the
6070 * underlying handle */
6072 return GNOME_VFS_OK;
6077 /* gnome-vfs bureaucracy */
6079 static GnomeVFSMethod method = {
6080 sizeof (GnomeVFSMethod),
6093 do_get_file_info_from_handle,
6096 do_remove_directory,
6102 NULL /* find_directory */,
6103 NULL /* create_symbolic_link */,
6109 vfs_module_init (const char *method_name,
6112 parent_method = gnome_vfs_method_get ("file");
6114 if (parent_method == NULL) {
6115 g_error ("Could not find 'file' method for gnome-vfs");
6123 vfs_module_shutdown (GnomeVFSMethod *method)
6128 g_hash_table_destroy (infos);