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 gint 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 DEBUG_1("collection load: append=%d flush=%d only_geometry=%d path=%s",
95 append, flush, only_geometry, path);
98 pathl = path_from_utf8(path);
99 f = fopen(pathl, "r");
103 log_printf("Failed to open collection file: \"%s\"\n", path);
107 while (fgets(s_buf, sizeof(s_buf), f))
112 /* Skip whitespaces and empty lines */
113 while (*p && g_ascii_isspace(*p)) p++;
114 if (*p == '\n' || *p == '\r') continue;
119 if (!need_header) continue;
120 if (g_ascii_strncasecmp(p, GQ_COLLECTION_MARKER, strlen(GQ_COLLECTION_MARKER)) == 0)
122 /* Looks like an official collection, allow unchecked input.
123 * All this does is allow adding files that may not exist,
124 * which is needed for the collection manager to work.
125 * Also unofficial files abort after too many invalid entries.
127 has_official_header = TRUE;
128 limit_failures = FALSE;
130 else if (strncmp(p, "#geometry:", 10 ) == 0 &&
131 scan_geometry(p + 10, &cd->window_x, &cd->window_y, &cd->window_w, &cd->window_h))
133 has_geometry_header = TRUE;
134 cd->window_read = TRUE;
135 if (only_geometry) break;
137 else if (g_ascii_strncasecmp(p, "#GQview collection", strlen("#GQview collection")) == 0)
139 /* As 2008/04/15 there is no difference between our collection file format
140 * and GQview 2.1.5 collection file format so ignore failures as well. */
141 has_gqview_header = TRUE;
142 limit_failures = FALSE;
144 need_header = (!has_official_header && !has_gqview_header) || !has_geometry_header;
148 if (only_geometry) continue;
151 while (*p && *p != '"') p++;
154 while (*p && *p != '"') p++;
161 changed |= collect_manager_process_action(entry, &buf);
163 valid = (buf[0] == G_DIR_SEPARATOR && collection_add_check(cd, file_data_new_group(buf), FALSE, TRUE));
164 if (!valid) DEBUG_1("collection invalid file: %s", buf);
170 if (limit_failures &&
171 fail > GQ_COLLECTION_FAIL_MIN &&
172 fail * 100 / total > GQ_COLLECTION_FAIL_PERCENT)
174 log_printf("%d invalid filenames in unofficial collection file, closing: %s\n", fail, path);
182 DEBUG_1("collection files: total = %d fail = %d official=%d gqview=%d geometry=%d",
183 total, fail, has_official_header, has_gqview_header, has_geometry_header);
186 if (only_geometry) return has_geometry_header;
191 while (collect_manager_process_action(entry, &buf))
193 collection_add_check(cd, file_data_new_group(buf), FALSE, TRUE);
200 cd->list = collection_list_sort(cd->list, cd->sort_method);
202 if (!flush && changed && success)
203 collection_save_private(cd, path);
206 collect_manager_entry_reset(entry);
208 if (!append) cd->changed = FALSE;
213 gboolean collection_load(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
215 if (collection_load_private(cd, path, flags | COLLECTION_LOAD_FLUSH))
217 layout_recent_add_path(cd->path);
224 static void collection_load_thumb_do(CollectionData *cd)
228 if (!cd->thumb_loader || !g_list_find(cd->list, cd->thumb_info)) return;
230 pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader);
231 collection_info_set_thumb(cd->thumb_info, pixbuf);
232 g_object_unref(pixbuf);
234 if (cd->info_updated_func) cd->info_updated_func(cd, cd->thumb_info, cd->info_updated_data);
237 static void collection_load_thumb_error_cb(ThumbLoader *tl, gpointer data)
239 CollectionData *cd = data;
241 collection_load_thumb_do(cd);
242 collection_load_thumb_step(cd);
245 static void collection_load_thumb_done_cb(ThumbLoader *tl, gpointer data)
247 CollectionData *cd = data;
249 collection_load_thumb_do(cd);
250 collection_load_thumb_step(cd);
253 static void collection_load_thumb_step(CollectionData *cd)
260 collection_load_stop(cd);
267 /* find first unloaded thumb */
268 while (work && ci->pixbuf)
274 if (!ci || ci->pixbuf)
277 collection_load_stop(cd);
279 /* send a NULL CollectInfo to notify end */
280 if (cd->info_updated_func) cd->info_updated_func(cd, NULL, cd->info_updated_data);
285 /* setup loader and call it */
287 thumb_loader_free(cd->thumb_loader);
288 cd->thumb_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
289 thumb_loader_set_callbacks(cd->thumb_loader,
290 collection_load_thumb_done_cb,
291 collection_load_thumb_error_cb,
296 if (!thumb_loader_start(cd->thumb_loader, ci->fd))
298 /* error, handle it, do next */
299 DEBUG_1("error loading thumb for %s", ci->fd->path);
300 collection_load_thumb_do(cd);
301 collection_load_thumb_step(cd);
305 void collection_load_thumb_idle(CollectionData *cd)
307 if (!cd->thumb_loader) collection_load_thumb_step(cd);
310 gboolean collection_load_begin(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
312 if (!collection_load(cd, path, flags)) return FALSE;
314 collection_load_thumb_idle(cd);
319 void collection_load_stop(CollectionData *cd)
321 if (!cd->thumb_loader) return;
323 thumb_loader_free(cd->thumb_loader);
324 cd->thumb_loader = NULL;
327 static gboolean collection_save_private(CollectionData *cd, const gchar *path)
333 if (!path && !cd->path) return FALSE;
341 pathl = path_from_utf8(path);
342 ssi = secure_open(pathl);
346 log_printf(_("failed to open collection (write) \"%s\"\n"), path);
350 secure_fprintf(ssi, "%s collection\n", GQ_COLLECTION_MARKER);
351 secure_fprintf(ssi, "#created with %s version %s\n", GQ_APPNAME, VERSION);
353 collection_update_geometry(cd);
356 secure_fprintf(ssi, "#geometry: %d %d %d %d\n", cd->window_x, cd->window_y, cd->window_w, cd->window_h);
360 while (work && secsave_errno == SS_ERR_NONE)
362 CollectInfo *ci = work->data;
363 secure_fprintf(ssi, "\"%s\"\n", ci->fd->path);
367 secure_fprintf(ssi, "#end\n");
369 if (secure_close(ssi))
371 log_printf(_("error saving collection file: %s\nerror: %s\n"), path,
372 secsave_strerror(secsave_errno));
376 if (!cd->path || strcmp(path, cd->path) != 0)
378 gchar *buf = cd->path;
379 cd->path = g_strdup(path);
384 cd->name = g_strdup(filename_from_path(cd->path));
386 collection_path_changed(cd);
394 gboolean collection_save(CollectionData *cd, const gchar *path)
396 if (collection_save_private(cd, path))
398 layout_recent_add_path(cd->path);
405 gboolean collection_load_only_geometry(CollectionData *cd, const gchar *path)
407 return collection_load(cd, path, COLLECTION_LOAD_GEOMETRY);
412 *-------------------------------------------------------------------
414 *-------------------------------------------------------------------
417 #define COLLECT_MANAGER_ACTIONS_PER_IDLE 1000
418 #define COLLECT_MANAGER_FLUSH_DELAY 10000
420 struct _CollectManagerEntry
424 GHashTable *oldpath_hash;
425 GHashTable *newpath_hash;
430 COLLECTION_MANAGER_UPDATE,
431 COLLECTION_MANAGER_ADD,
432 COLLECTION_MANAGER_REMOVE
433 } CollectManagerType;
435 typedef struct _CollectManagerAction CollectManagerAction;
436 struct _CollectManagerAction
441 CollectManagerType type;
447 static GList *collection_manager_entry_list = NULL;
448 static GList *collection_manager_action_list = NULL;
449 static GList *collection_manager_action_tail = NULL;
450 static guint collection_manager_timer_id = 0; /* event source id */
453 static CollectManagerAction *collect_manager_action_new(const gchar *oldpath, const gchar *newpath,
454 CollectManagerType type)
456 CollectManagerAction *action;
458 action = g_new0(CollectManagerAction, 1);
461 action->oldpath = g_strdup(oldpath);
462 action->newpath = g_strdup(newpath);
469 static void collect_manager_action_ref(CollectManagerAction *action)
474 static void collect_manager_action_unref(CollectManagerAction *action)
478 if (action->ref > 0) return;
480 g_free(action->oldpath);
481 g_free(action->newpath);
485 static void collect_manager_entry_free_data(CollectManagerEntry *entry)
489 work = entry->add_list;
492 CollectManagerAction *action;
497 collect_manager_action_unref(action);
499 g_list_free(entry->add_list);
500 g_hash_table_destroy(entry->oldpath_hash);
501 g_hash_table_destroy(entry->newpath_hash);
504 static void collect_manager_entry_init_data(CollectManagerEntry *entry)
506 entry->add_list = NULL;
507 entry->oldpath_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify) collect_manager_action_unref);
508 entry->newpath_hash = g_hash_table_new(g_str_hash, g_str_equal);
513 static CollectManagerEntry *collect_manager_entry_new(const gchar *path)
515 CollectManagerEntry *entry;
517 entry = g_new0(CollectManagerEntry, 1);
518 entry->path = g_strdup(path);
519 collect_manager_entry_init_data(entry);
521 collection_manager_entry_list = g_list_append(collection_manager_entry_list, entry);
527 static void collect_manager_entry_free(CollectManagerEntry *entry)
529 collection_manager_entry_list = g_list_remove(collection_manager_entry_list, entry);
531 collect_manager_entry_free_data(entry);
537 static void collect_manager_entry_reset(CollectManagerEntry *entry)
539 collect_manager_entry_free_data(entry);
540 collect_manager_entry_init_data(entry);
543 static CollectManagerEntry *collect_manager_get_entry(const gchar *path)
547 work = collection_manager_entry_list;
550 CollectManagerEntry *entry;
554 if (strcmp(entry->path, path) == 0)
563 static void collect_manager_entry_add_action(CollectManagerEntry *entry, CollectManagerAction *action)
566 CollectManagerAction *orig_action;
568 entry->empty = FALSE;
570 if (action->oldpath == NULL)
573 if (action->newpath == NULL)
578 orig_action = g_hash_table_lookup(entry->newpath_hash, action->newpath);
581 /* target already exists */
582 log_printf("collection manager failed to add another action for target %s in collection %s\n",
583 action->newpath, entry->path);
586 entry->add_list = g_list_append(entry->add_list, action);
587 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
588 collect_manager_action_ref(action);
592 orig_action = g_hash_table_lookup(entry->newpath_hash, action->oldpath);
595 /* new action with the same file */
596 CollectManagerAction *new_action = collect_manager_action_new(orig_action->oldpath, action->newpath, action->type);
598 if (new_action->oldpath)
600 g_hash_table_steal(entry->oldpath_hash, orig_action->oldpath);
601 g_hash_table_insert(entry->oldpath_hash, new_action->oldpath, new_action);
605 GList *work = g_list_find(entry->add_list, orig_action);
606 work->data = new_action;
609 g_hash_table_steal(entry->newpath_hash, orig_action->newpath);
610 if (new_action->newpath)
612 g_hash_table_insert(entry->newpath_hash, new_action->newpath, new_action);
614 collect_manager_action_unref(orig_action);
619 orig_action = g_hash_table_lookup(entry->oldpath_hash, action->oldpath);
622 /* another action for the same source, ignore */
623 log_printf("collection manager failed to add another action for source %s in collection %s\n",
624 action->oldpath, entry->path);
628 g_hash_table_insert(entry->oldpath_hash, action->oldpath, action);
631 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
633 collect_manager_action_ref(action);
636 static gboolean collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr)
638 gchar *path = *path_ptr;
639 CollectManagerAction *action;
646 action = entry->add_list->data;
647 g_assert(action->oldpath == NULL);
648 entry->add_list = g_list_remove(entry->add_list, action);
649 path = g_strdup(action->newpath);
650 g_hash_table_remove(entry->newpath_hash, path);
651 collect_manager_action_unref(action);
654 return (path != NULL);
657 action = g_hash_table_lookup(entry->oldpath_hash, path);
662 path = g_strdup(action->newpath);
667 return FALSE; /* no change */
670 static void collect_manager_refresh(void)
676 dir_fd = file_data_new_dir(get_collections_dir());
677 filelist_read(dir_fd, &list, NULL);
678 file_data_unref(dir_fd);
680 work = collection_manager_entry_list;
683 CollectManagerEntry *entry;
690 while (list_step && entry)
694 fd = list_step->data;
695 list_step = list_step->next;
697 if (strcmp(fd->path, entry->path) == 0)
699 list = g_list_remove(list, fd);
706 collect_manager_entry_free(entry);
719 collect_manager_entry_new(fd->path);
725 static void collect_manager_process_actions(gint max)
727 if (collection_manager_action_list) DEBUG_1("collection manager processing actions");
729 while (collection_manager_action_list != NULL && max > 0)
731 CollectManagerAction *action;
734 action = collection_manager_action_list->data;
735 work = collection_manager_entry_list;
738 CollectManagerEntry *entry;
743 if (action->type == COLLECTION_MANAGER_UPDATE)
745 collect_manager_entry_add_action(entry, action);
747 else if (action->oldpath && action->newpath &&
748 strcmp(action->newpath, entry->path) == 0)
750 /* convert action to standard add format */
751 g_free(action->newpath);
752 if (action->type == COLLECTION_MANAGER_ADD)
754 action->newpath = action->oldpath;
755 action->oldpath = NULL;
757 else if (action->type == COLLECTION_MANAGER_REMOVE)
759 action->newpath = NULL;
761 collect_manager_entry_add_action(entry, action);
767 if (action->type != COLLECTION_MANAGER_UPDATE &&
768 action->oldpath && action->newpath)
770 log_printf("collection manager failed to %s %s for collection %s\n",
771 (action->type == COLLECTION_MANAGER_ADD) ? "add" : "remove",
772 action->oldpath, action->newpath);
775 if (collection_manager_action_tail == collection_manager_action_list)
777 collection_manager_action_tail = NULL;
779 collection_manager_action_list = g_list_remove(collection_manager_action_list, action);
780 collect_manager_action_unref(action);
784 static gboolean collect_manager_process_entry(CollectManagerEntry *entry)
789 if (entry->empty) return FALSE;
791 cd = collection_new(entry->path);
792 success = collection_load_private(cd, entry->path, COLLECTION_LOAD_NONE);
794 collection_unref(cd);
799 static gboolean collect_manager_process_entry_list(void)
803 work = collection_manager_entry_list;
806 CollectManagerEntry *entry;
810 if (collect_manager_process_entry(entry)) return TRUE;
818 static gboolean collect_manager_process_cb(gpointer data)
820 if (collection_manager_action_list) collect_manager_refresh();
821 collect_manager_process_actions(COLLECT_MANAGER_ACTIONS_PER_IDLE);
822 if (collection_manager_action_list) return TRUE;
824 if (collect_manager_process_entry_list()) return TRUE;
826 DEBUG_1("collection manager is up to date");
830 static gboolean collect_manager_timer_cb(gpointer data)
832 DEBUG_1("collection manager timer expired");
834 g_idle_add_full(G_PRIORITY_LOW, collect_manager_process_cb, NULL, NULL);
836 collection_manager_timer_id = 0;
840 static void collect_manager_timer_push(gint stop)
842 if (collection_manager_timer_id)
846 g_source_remove(collection_manager_timer_id);
847 collection_manager_timer_id = 0;
852 collection_manager_timer_id = g_timeout_add(COLLECT_MANAGER_FLUSH_DELAY,
853 collect_manager_timer_cb, NULL);
854 DEBUG_1("collection manager timer started");
858 static void collect_manager_add_action(CollectManagerAction *action)
862 /* we keep track of the list's tail to keep this a n(1) operation */
864 if (collection_manager_action_tail)
866 collection_manager_action_tail = g_list_append(collection_manager_action_tail, action);
867 collection_manager_action_tail = collection_manager_action_tail->next;
871 collection_manager_action_list = g_list_append(collection_manager_action_list, action);
872 collection_manager_action_tail = collection_manager_action_list;
875 collect_manager_timer_push(FALSE);
878 void collect_manager_moved(FileData *fd)
880 CollectManagerAction *action;
881 const gchar *oldpath = fd->change->source;
882 const gchar *newpath = fd->change->dest;
884 action = collect_manager_action_new(oldpath, newpath, COLLECTION_MANAGER_UPDATE);
885 collect_manager_add_action(action);
888 void collect_manager_add(FileData *fd, const gchar *collection)
890 CollectManagerAction *action;
893 if (!fd || !collection) return;
895 cw = collection_window_find_by_path(collection);
898 if (collection_list_find_fd(cw->cd->list, fd) == NULL)
900 collection_add(cw->cd, fd, FALSE);
905 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_ADD);
906 collect_manager_add_action(action);
909 void collect_manager_remove(FileData *fd, const gchar *collection)
911 CollectManagerAction *action;
914 if (!fd || !collection) return;
916 cw = collection_window_find_by_path(collection);
919 while (collection_remove(cw->cd, fd));
923 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_REMOVE);
924 collect_manager_add_action(action);
927 void collect_manager_flush(void)
929 collect_manager_timer_push(TRUE);
931 DEBUG_1("collection manager flushing");
932 while (collect_manager_process_cb(NULL));
935 void collect_manager_notify_cb(FileData *fd, NotifyType type, gpointer data)
937 if (!(type & NOTIFY_CHANGE) || !fd->change) return;
939 DEBUG_1("Notify collect_manager: %s %04x", fd->path, type);
940 switch (fd->change->type)
942 case FILEDATA_CHANGE_MOVE:
943 collect_manager_moved(fd);
945 case FILEDATA_CHANGE_COPY:
947 case FILEDATA_CHANGE_RENAME:
948 collect_manager_moved(fd);
950 case FILEDATA_CHANGE_DELETE:
951 case FILEDATA_CHANGE_UNSPECIFIED:
952 case FILEDATA_CHANGE_WRITE_METADATA:
957 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */