4 * Copyright (C) 2008 - 2012 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
15 #include "collect-io.h"
19 #include "layout_util.h"
21 #include "secure_save.h"
23 #include "ui_fileops.h"
25 #define GQ_COLLECTION_MARKER "#" GQ_APPNAME
27 #define GQ_COLLECTION_FAIL_MIN 300
28 #define GQ_COLLECTION_FAIL_PERCENT 98
29 #define GQ_COLLECTION_READ_BUFSIZE 4096
31 typedef struct _CollectManagerEntry CollectManagerEntry;
33 static void collection_load_thumb_step(CollectionData *cd);
34 static gboolean collection_save_private(CollectionData *cd, const gchar *path);
36 static CollectManagerEntry *collect_manager_get_entry(const gchar *path);
37 static void collect_manager_entry_reset(CollectManagerEntry *entry);
38 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr);
41 static gboolean scan_geometry(gchar *buffer, gint *x, gint *y, gint *w, gint *h)
45 if (sscanf(buffer, "%d %d %d %d", &nx, &ny, &nw, &nh) != 4) return FALSE;
55 static gboolean collection_load_private(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
57 gchar s_buf[GQ_COLLECTION_READ_BUFSIZE];
60 gboolean limit_failures = TRUE;
61 gboolean success = TRUE;
62 gboolean has_official_header = FALSE;
63 gboolean has_geometry_header = FALSE;
64 gboolean has_gqview_header = FALSE;
65 gboolean need_header = TRUE;
68 gboolean changed = FALSE;
69 CollectManagerEntry *entry = NULL;
70 guint flush = !!(flags & COLLECTION_LOAD_FLUSH);
71 guint append = !!(flags & COLLECTION_LOAD_APPEND);
72 guint only_geometry = !!(flags & COLLECTION_LOAD_GEOMETRY);
76 collection_load_stop(cd);
79 collect_manager_flush();
81 entry = collect_manager_get_entry(path);
85 collection_list_free(cd->list);
90 if (!path && !cd->path) return FALSE;
92 if (!path) path = cd->path;
94 pathl = path_from_utf8(path);
96 DEBUG_1("collection load: append=%d flush=%d only_geometry=%d path=%s",
97 append, flush, only_geometry, pathl);
100 f = fopen(pathl, "r");
104 log_printf("Failed to open collection file: \"%s\"\n", path);
108 while (fgets(s_buf, sizeof(s_buf), f))
113 /* Skip whitespaces and empty lines */
114 while (*p && g_ascii_isspace(*p)) p++;
115 if (*p == '\n' || *p == '\r') continue;
120 if (!need_header) continue;
121 if (g_ascii_strncasecmp(p, GQ_COLLECTION_MARKER, strlen(GQ_COLLECTION_MARKER)) == 0)
123 /* Looks like an official collection, allow unchecked input.
124 * All this does is allow adding files that may not exist,
125 * which is needed for the collection manager to work.
126 * Also unofficial files abort after too many invalid entries.
128 has_official_header = TRUE;
129 limit_failures = FALSE;
131 else if (strncmp(p, "#geometry:", 10 ) == 0 &&
132 scan_geometry(p + 10, &cd->window_x, &cd->window_y, &cd->window_w, &cd->window_h))
134 has_geometry_header = TRUE;
135 cd->window_read = TRUE;
136 if (only_geometry) break;
138 else if (g_ascii_strncasecmp(p, "#GQview collection", strlen("#GQview collection")) == 0)
140 /* As 2008/04/15 there is no difference between our collection file format
141 * and GQview 2.1.5 collection file format so ignore failures as well. */
142 has_gqview_header = TRUE;
143 limit_failures = FALSE;
145 need_header = (!has_official_header && !has_gqview_header) || !has_geometry_header;
149 if (only_geometry) continue;
152 /* TODO: This is not safe! */
153 while (*p && *p != '"') p++;
156 while (*p && *p != '"') p++;
163 changed |= collect_manager_process_action(entry, &buf);
165 valid = (buf[0] == G_DIR_SEPARATOR && collection_add_check(cd, file_data_new_group(buf), FALSE, TRUE));
166 if (!valid) DEBUG_1("collection invalid file: %s", buf);
172 if (limit_failures &&
173 fail > GQ_COLLECTION_FAIL_MIN &&
174 fail * 100 / total > GQ_COLLECTION_FAIL_PERCENT)
176 log_printf("%d invalid filenames in unofficial collection file, closing: %s\n", fail, path);
184 DEBUG_1("collection files: total = %d fail = %d official=%d gqview=%d geometry=%d",
185 total, fail, has_official_header, has_gqview_header, has_geometry_header);
188 if (only_geometry) return has_geometry_header;
193 while (collect_manager_process_action(entry, &buf))
195 collection_add_check(cd, file_data_new_group(buf), FALSE, TRUE);
202 cd->list = collection_list_sort(cd->list, cd->sort_method);
204 if (!flush && changed && success)
205 collection_save_private(cd, path);
208 collect_manager_entry_reset(entry);
210 if (!append) cd->changed = FALSE;
215 gboolean collection_load(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
217 if (collection_load_private(cd, path, flags | COLLECTION_LOAD_FLUSH))
219 layout_recent_add_path(cd->path);
226 static void collection_load_thumb_do(CollectionData *cd)
230 if (!cd->thumb_loader || !g_list_find(cd->list, cd->thumb_info)) return;
232 pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader);
233 collection_info_set_thumb(cd->thumb_info, pixbuf);
234 g_object_unref(pixbuf);
236 if (cd->info_updated_func) cd->info_updated_func(cd, cd->thumb_info, cd->info_updated_data);
239 static void collection_load_thumb_error_cb(ThumbLoader *tl, gpointer data)
241 CollectionData *cd = data;
243 collection_load_thumb_do(cd);
244 collection_load_thumb_step(cd);
247 static void collection_load_thumb_done_cb(ThumbLoader *tl, gpointer data)
249 CollectionData *cd = data;
251 collection_load_thumb_do(cd);
252 collection_load_thumb_step(cd);
255 static void collection_load_thumb_step(CollectionData *cd)
262 collection_load_stop(cd);
269 /* find first unloaded thumb */
270 while (work && ci->pixbuf)
276 if (!ci || ci->pixbuf)
279 collection_load_stop(cd);
281 /* send a NULL CollectInfo to notify end */
282 if (cd->info_updated_func) cd->info_updated_func(cd, NULL, cd->info_updated_data);
287 /* setup loader and call it */
289 thumb_loader_free(cd->thumb_loader);
290 cd->thumb_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
291 thumb_loader_set_callbacks(cd->thumb_loader,
292 collection_load_thumb_done_cb,
293 collection_load_thumb_error_cb,
298 if (!thumb_loader_start(cd->thumb_loader, ci->fd))
300 /* error, handle it, do next */
301 DEBUG_1("error loading thumb for %s", ci->fd->path);
302 collection_load_thumb_do(cd);
303 collection_load_thumb_step(cd);
307 void collection_load_thumb_idle(CollectionData *cd)
309 if (!cd->thumb_loader) collection_load_thumb_step(cd);
312 gboolean collection_load_begin(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
314 if (!collection_load(cd, path, flags)) return FALSE;
316 collection_load_thumb_idle(cd);
321 void collection_load_stop(CollectionData *cd)
323 if (!cd->thumb_loader) return;
325 thumb_loader_free(cd->thumb_loader);
326 cd->thumb_loader = NULL;
329 static gboolean collection_save_private(CollectionData *cd, const gchar *path)
335 if (!path && !cd->path) return FALSE;
343 pathl = path_from_utf8(path);
344 ssi = secure_open(pathl);
348 log_printf(_("failed to open collection (write) \"%s\"\n"), path);
352 secure_fprintf(ssi, "%s collection\n", GQ_COLLECTION_MARKER);
353 secure_fprintf(ssi, "#created with %s version %s\n", GQ_APPNAME, VERSION);
355 collection_update_geometry(cd);
358 secure_fprintf(ssi, "#geometry: %d %d %d %d\n", cd->window_x, cd->window_y, cd->window_w, cd->window_h);
362 while (work && secsave_errno == SS_ERR_NONE)
364 CollectInfo *ci = work->data;
365 secure_fprintf(ssi, "\"%s\"\n", ci->fd->path);
369 secure_fprintf(ssi, "#end\n");
371 if (secure_close(ssi))
373 log_printf(_("error saving collection file: %s\nerror: %s\n"), path,
374 secsave_strerror(secsave_errno));
378 if (!cd->path || strcmp(path, cd->path) != 0)
380 gchar *buf = cd->path;
381 cd->path = g_strdup(path);
386 cd->name = g_strdup(filename_from_path(cd->path));
388 collection_path_changed(cd);
396 gboolean collection_save(CollectionData *cd, const gchar *path)
398 if (collection_save_private(cd, path))
400 layout_recent_add_path(cd->path);
407 gboolean collection_load_only_geometry(CollectionData *cd, const gchar *path)
409 return collection_load(cd, path, COLLECTION_LOAD_GEOMETRY);
414 *-------------------------------------------------------------------
416 *-------------------------------------------------------------------
419 #define COLLECT_MANAGER_ACTIONS_PER_IDLE 1000
420 #define COLLECT_MANAGER_FLUSH_DELAY 10000
422 struct _CollectManagerEntry
426 GHashTable *oldpath_hash;
427 GHashTable *newpath_hash;
432 COLLECTION_MANAGER_UPDATE,
433 COLLECTION_MANAGER_ADD,
434 COLLECTION_MANAGER_REMOVE
435 } CollectManagerType;
437 typedef struct _CollectManagerAction CollectManagerAction;
438 struct _CollectManagerAction
443 CollectManagerType type;
449 static GList *collection_manager_entry_list = NULL;
450 static GList *collection_manager_action_list = NULL;
451 static GList *collection_manager_action_tail = NULL;
452 static guint collection_manager_timer_id = 0; /* event source id */
455 static CollectManagerAction *collect_manager_action_new(const gchar *oldpath, const gchar *newpath,
456 CollectManagerType type)
458 CollectManagerAction *action;
460 action = g_new0(CollectManagerAction, 1);
463 action->oldpath = g_strdup(oldpath);
464 action->newpath = g_strdup(newpath);
471 static void collect_manager_action_ref(CollectManagerAction *action)
476 static void collect_manager_action_unref(CollectManagerAction *action)
480 if (action->ref > 0) return;
482 g_free(action->oldpath);
483 g_free(action->newpath);
487 static void collect_manager_entry_free_data(CollectManagerEntry *entry)
491 work = entry->add_list;
494 CollectManagerAction *action;
499 collect_manager_action_unref(action);
501 g_list_free(entry->add_list);
502 if (g_hash_table_size(entry->oldpath_hash) > 0)
503 g_hash_table_destroy(entry->oldpath_hash);
505 g_hash_table_unref(entry->oldpath_hash);
506 if (g_hash_table_size(entry->newpath_hash) > 0)
507 g_hash_table_destroy(entry->newpath_hash);
509 g_hash_table_unref(entry->newpath_hash);
512 static void collect_manager_entry_init_data(CollectManagerEntry *entry)
514 entry->add_list = NULL;
515 entry->oldpath_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify) collect_manager_action_unref);
516 entry->newpath_hash = g_hash_table_new(g_str_hash, g_str_equal);
521 static CollectManagerEntry *collect_manager_entry_new(const gchar *path)
523 CollectManagerEntry *entry;
525 entry = g_new0(CollectManagerEntry, 1);
526 entry->path = g_strdup(path);
527 collect_manager_entry_init_data(entry);
529 collection_manager_entry_list = g_list_append(collection_manager_entry_list, entry);
535 static void collect_manager_entry_free(CollectManagerEntry *entry)
537 collection_manager_entry_list = g_list_remove(collection_manager_entry_list, entry);
539 collect_manager_entry_free_data(entry);
545 static void collect_manager_entry_reset(CollectManagerEntry *entry)
547 collect_manager_entry_free_data(entry);
548 collect_manager_entry_init_data(entry);
551 static CollectManagerEntry *collect_manager_get_entry(const gchar *path)
555 work = collection_manager_entry_list;
558 CollectManagerEntry *entry;
562 if (strcmp(entry->path, path) == 0)
571 static void collect_manager_entry_add_action(CollectManagerEntry *entry, CollectManagerAction *action)
574 CollectManagerAction *orig_action;
576 entry->empty = FALSE;
578 if (action->oldpath == NULL)
581 if (action->newpath == NULL)
586 orig_action = g_hash_table_lookup(entry->newpath_hash, action->newpath);
589 /* target already exists */
590 log_printf("collection manager failed to add another action for target %s in collection %s\n",
591 action->newpath, entry->path);
594 entry->add_list = g_list_append(entry->add_list, action);
595 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
596 collect_manager_action_ref(action);
600 orig_action = g_hash_table_lookup(entry->newpath_hash, action->oldpath);
603 /* new action with the same file */
604 CollectManagerAction *new_action = collect_manager_action_new(orig_action->oldpath, action->newpath, action->type);
606 if (new_action->oldpath)
608 g_hash_table_steal(entry->oldpath_hash, orig_action->oldpath);
609 g_hash_table_insert(entry->oldpath_hash, new_action->oldpath, new_action);
613 GList *work = g_list_find(entry->add_list, orig_action);
614 work->data = new_action;
617 g_hash_table_steal(entry->newpath_hash, orig_action->newpath);
618 if (new_action->newpath)
620 g_hash_table_insert(entry->newpath_hash, new_action->newpath, new_action);
622 collect_manager_action_unref(orig_action);
627 orig_action = g_hash_table_lookup(entry->oldpath_hash, action->oldpath);
630 /* another action for the same source, ignore */
631 log_printf("collection manager failed to add another action for source %s in collection %s\n",
632 action->oldpath, entry->path);
636 g_hash_table_insert(entry->oldpath_hash, action->oldpath, action);
639 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
641 collect_manager_action_ref(action);
644 static gboolean collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr)
646 gchar *path = *path_ptr;
647 CollectManagerAction *action;
654 action = entry->add_list->data;
655 g_assert(action->oldpath == NULL);
656 entry->add_list = g_list_remove(entry->add_list, action);
657 path = g_strdup(action->newpath);
658 g_hash_table_remove(entry->newpath_hash, path);
659 collect_manager_action_unref(action);
662 return (path != NULL);
665 action = g_hash_table_lookup(entry->oldpath_hash, path);
670 path = g_strdup(action->newpath);
675 return FALSE; /* no change */
678 static void collect_manager_refresh(void)
684 dir_fd = file_data_new_dir(get_collections_dir());
685 filelist_read(dir_fd, &list, NULL);
686 file_data_unref(dir_fd);
688 work = collection_manager_entry_list;
691 CollectManagerEntry *entry;
698 while (list_step && entry)
702 fd = list_step->data;
703 list_step = list_step->next;
705 if (strcmp(fd->path, entry->path) == 0)
707 list = g_list_remove(list, fd);
714 collect_manager_entry_free(entry);
729 collect_manager_entry_new(fd->path);
735 static void collect_manager_process_actions(gint max)
737 if (collection_manager_action_list) DEBUG_1("collection manager processing actions");
739 while (collection_manager_action_list != NULL && max > 0)
741 CollectManagerAction *action;
744 action = collection_manager_action_list->data;
745 work = collection_manager_entry_list;
748 CollectManagerEntry *entry;
753 if (action->type == COLLECTION_MANAGER_UPDATE)
755 collect_manager_entry_add_action(entry, action);
757 else if (action->oldpath && action->newpath &&
758 strcmp(action->newpath, entry->path) == 0)
760 /* convert action to standard add format */
761 g_free(action->newpath);
762 if (action->type == COLLECTION_MANAGER_ADD)
764 action->newpath = action->oldpath;
765 action->oldpath = NULL;
767 else if (action->type == COLLECTION_MANAGER_REMOVE)
769 action->newpath = NULL;
771 collect_manager_entry_add_action(entry, action);
777 if (action->type != COLLECTION_MANAGER_UPDATE &&
778 action->oldpath && action->newpath)
780 log_printf("collection manager failed to %s %s for collection %s\n",
781 (action->type == COLLECTION_MANAGER_ADD) ? "add" : "remove",
782 action->oldpath, action->newpath);
785 if (collection_manager_action_tail == collection_manager_action_list)
787 collection_manager_action_tail = NULL;
789 collection_manager_action_list = g_list_remove(collection_manager_action_list, action);
790 collect_manager_action_unref(action);
794 static gboolean collect_manager_process_entry(CollectManagerEntry *entry)
798 if (entry->empty) return FALSE;
800 cd = collection_new(entry->path);
801 (void) collection_load_private(cd, entry->path, COLLECTION_LOAD_NONE);
803 collection_unref(cd);
808 static gboolean collect_manager_process_entry_list(void)
812 work = collection_manager_entry_list;
815 CollectManagerEntry *entry;
819 if (collect_manager_process_entry(entry)) return TRUE;
827 static gboolean collect_manager_process_cb(gpointer data)
829 if (collection_manager_action_list) collect_manager_refresh();
830 collect_manager_process_actions(COLLECT_MANAGER_ACTIONS_PER_IDLE);
831 if (collection_manager_action_list) return TRUE;
833 if (collect_manager_process_entry_list()) return TRUE;
835 DEBUG_1("collection manager is up to date");
839 static gboolean collect_manager_timer_cb(gpointer data)
841 DEBUG_1("collection manager timer expired");
843 g_idle_add_full(G_PRIORITY_LOW, collect_manager_process_cb, NULL, NULL);
845 collection_manager_timer_id = 0;
849 static void collect_manager_timer_push(gint stop)
851 if (collection_manager_timer_id)
855 g_source_remove(collection_manager_timer_id);
856 collection_manager_timer_id = 0;
861 collection_manager_timer_id = g_timeout_add(COLLECT_MANAGER_FLUSH_DELAY,
862 collect_manager_timer_cb, NULL);
863 DEBUG_1("collection manager timer started");
867 static void collect_manager_add_action(CollectManagerAction *action)
871 /* we keep track of the list's tail to keep this a n(1) operation */
873 if (collection_manager_action_tail)
875 collection_manager_action_tail = g_list_append(collection_manager_action_tail, action);
876 collection_manager_action_tail = collection_manager_action_tail->next;
880 collection_manager_action_list = g_list_append(collection_manager_action_list, action);
881 collection_manager_action_tail = collection_manager_action_list;
884 collect_manager_timer_push(FALSE);
887 void collect_manager_moved(FileData *fd)
889 CollectManagerAction *action;
890 const gchar *oldpath = fd->change->source;
891 const gchar *newpath = fd->change->dest;
893 action = collect_manager_action_new(oldpath, newpath, COLLECTION_MANAGER_UPDATE);
894 collect_manager_add_action(action);
897 void collect_manager_add(FileData *fd, const gchar *collection)
899 CollectManagerAction *action;
902 if (!fd || !collection) return;
904 cw = collection_window_find_by_path(collection);
907 if (collection_list_find_fd(cw->cd->list, fd) == NULL)
909 collection_add(cw->cd, fd, FALSE);
914 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_ADD);
915 collect_manager_add_action(action);
918 void collect_manager_remove(FileData *fd, const gchar *collection)
920 CollectManagerAction *action;
923 if (!fd || !collection) return;
925 cw = collection_window_find_by_path(collection);
928 while (collection_remove(cw->cd, fd));
932 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_REMOVE);
933 collect_manager_add_action(action);
936 void collect_manager_flush(void)
938 collect_manager_timer_push(TRUE);
940 DEBUG_1("collection manager flushing");
941 while (collect_manager_process_cb(NULL));
944 void collect_manager_notify_cb(FileData *fd, NotifyType type, gpointer data)
946 if (!(type & NOTIFY_CHANGE) || !fd->change) return;
948 DEBUG_1("Notify collect_manager: %s %04x", fd->path, type);
949 switch (fd->change->type)
951 case FILEDATA_CHANGE_MOVE:
952 collect_manager_moved(fd);
954 case FILEDATA_CHANGE_COPY:
956 case FILEDATA_CHANGE_RENAME:
957 collect_manager_moved(fd);
959 case FILEDATA_CHANGE_DELETE:
960 case FILEDATA_CHANGE_UNSPECIFIED:
961 case FILEDATA_CHANGE_WRITE_METADATA:
966 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */