4 * Copyright (C) 2008 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 gint 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 gint collection_load_private(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
57 gchar s_buf[GQ_COLLECTION_READ_BUFSIZE];
60 gint limit_failures = TRUE;
62 gint has_official_header = FALSE;
63 gint has_geometry_header = FALSE;
64 gint has_gqview_header = FALSE;
65 gint 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 buf = quoted_value(p, NULL);
157 changed |= collect_manager_process_action(entry, &buf);
159 valid = (buf[0] == G_DIR_SEPARATOR && collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE));
160 if (!valid) DEBUG_1("collection invalid file: %s", buf);
167 if (limit_failures &&
168 fail > GQ_COLLECTION_FAIL_MIN &&
169 fail * 100 / total > GQ_COLLECTION_FAIL_PERCENT)
171 log_printf("%d invalid filenames in unofficial collection file, closing: %s\n", fail, path);
179 DEBUG_1("collection files: total = %d fail = %d official=%d gqview=%d geometry=%d",
180 total, fail, has_official_header, has_gqview_header, has_geometry_header);
183 if (only_geometry) return has_geometry_header;
188 while (collect_manager_process_action(entry, &buf))
190 collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE);
197 cd->list = collection_list_sort(cd->list, cd->sort_method);
199 if (!flush && changed && success)
200 collection_save_private(cd, path);
203 collect_manager_entry_reset(entry);
205 if (!append) cd->changed = FALSE;
210 gint collection_load(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
212 if (collection_load_private(cd, path, flags | COLLECTION_LOAD_FLUSH))
214 layout_recent_add_path(cd->path);
221 static void collection_load_thumb_do(CollectionData *cd)
225 if (!cd->thumb_loader || !g_list_find(cd->list, cd->thumb_info)) return;
227 pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader);
228 collection_info_set_thumb(cd->thumb_info, pixbuf);
229 g_object_unref(pixbuf);
231 if (cd->info_updated_func) cd->info_updated_func(cd, cd->thumb_info, cd->info_updated_data);
234 static void collection_load_thumb_error_cb(ThumbLoader *tl, gpointer data)
236 CollectionData *cd = data;
238 collection_load_thumb_do(cd);
239 collection_load_thumb_step(cd);
242 static void collection_load_thumb_done_cb(ThumbLoader *tl, gpointer data)
244 CollectionData *cd = data;
246 collection_load_thumb_do(cd);
247 collection_load_thumb_step(cd);
250 static void collection_load_thumb_step(CollectionData *cd)
257 collection_load_stop(cd);
264 /* find first unloaded thumb */
265 while (work && ci->pixbuf)
271 if (!ci || ci->pixbuf)
274 collection_load_stop(cd);
276 /* send a NULL CollectInfo to notify end */
277 if (cd->info_updated_func) cd->info_updated_func(cd, NULL, cd->info_updated_data);
282 /* setup loader and call it */
284 thumb_loader_free(cd->thumb_loader);
285 cd->thumb_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
286 thumb_loader_set_callbacks(cd->thumb_loader,
287 collection_load_thumb_done_cb,
288 collection_load_thumb_error_cb,
293 if (!thumb_loader_start(cd->thumb_loader, ci->fd))
295 /* error, handle it, do next */
296 DEBUG_1("error loading thumb for %s", ci->fd->path);
297 collection_load_thumb_do(cd);
298 collection_load_thumb_step(cd);
302 void collection_load_thumb_idle(CollectionData *cd)
304 if (!cd->thumb_loader) collection_load_thumb_step(cd);
307 gint collection_load_begin(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
309 if (!collection_load(cd, path, flags)) return FALSE;
311 collection_load_thumb_idle(cd);
316 void collection_load_stop(CollectionData *cd)
318 if (!cd->thumb_loader) return;
320 thumb_loader_free(cd->thumb_loader);
321 cd->thumb_loader = NULL;
324 static gint collection_save_private(CollectionData *cd, const gchar *path)
330 if (!path && !cd->path) return FALSE;
338 pathl = path_from_utf8(path);
339 ssi = secure_open(pathl);
343 log_printf(_("failed to open collection (write) \"%s\"\n"), path);
347 secure_fprintf(ssi, "%s collection\n", GQ_COLLECTION_MARKER);
348 secure_fprintf(ssi, "#created with %s version %s\n", GQ_APPNAME, VERSION);
350 collection_update_geometry(cd);
353 secure_fprintf(ssi, "#geometry: %d %d %d %d\n", cd->window_x, cd->window_y, cd->window_w, cd->window_h);
357 while (work && secsave_errno == SS_ERR_NONE)
359 CollectInfo *ci = work->data;
360 secure_fprintf(ssi, "\"%s\"\n", ci->fd->path);
364 secure_fprintf(ssi, "#end\n");
366 if (secure_close(ssi))
368 log_printf(_("error saving collection file: %s\nerror: %s\n"), path,
369 secsave_strerror(secsave_errno));
373 if (!cd->path || strcmp(path, cd->path) != 0)
375 gchar *buf = cd->path;
376 cd->path = g_strdup(path);
381 cd->name = g_strdup(filename_from_path(cd->path));
383 collection_path_changed(cd);
391 gint collection_save(CollectionData *cd, const gchar *path)
393 if (collection_save_private(cd, path))
395 layout_recent_add_path(cd->path);
402 gint collection_load_only_geometry(CollectionData *cd, const gchar *path)
404 return collection_load(cd, path, COLLECTION_LOAD_GEOMETRY);
409 *-------------------------------------------------------------------
411 *-------------------------------------------------------------------
414 #define COLLECT_MANAGER_ACTIONS_PER_IDLE 1000
415 #define COLLECT_MANAGER_FLUSH_DELAY 10000
417 struct _CollectManagerEntry
421 GHashTable *oldpath_hash;
422 GHashTable *newpath_hash;
427 COLLECTION_MANAGER_UPDATE,
428 COLLECTION_MANAGER_ADD,
429 COLLECTION_MANAGER_REMOVE
430 } CollectManagerType;
432 typedef struct _CollectManagerAction CollectManagerAction;
433 struct _CollectManagerAction
438 CollectManagerType type;
444 static GList *collection_manager_entry_list = NULL;
445 static GList *collection_manager_action_list = NULL;
446 static GList *collection_manager_action_tail = NULL;
447 static gint collection_manager_timer_id = -1;
450 static CollectManagerAction *collect_manager_action_new(const gchar *oldpath, const gchar *newpath,
451 CollectManagerType type)
453 CollectManagerAction *action;
455 action = g_new0(CollectManagerAction, 1);
458 action->oldpath = g_strdup(oldpath);
459 action->newpath = g_strdup(newpath);
466 static void collect_manager_action_ref(CollectManagerAction *action)
471 static void collect_manager_action_unref(CollectManagerAction *action)
475 if (action->ref > 0) return;
477 g_free(action->oldpath);
478 g_free(action->newpath);
482 static void collect_manager_entry_free_data(CollectManagerEntry *entry)
486 work = entry->add_list;
489 CollectManagerAction *action;
494 collect_manager_action_unref(action);
496 g_list_free(entry->add_list);
497 g_hash_table_destroy(entry->oldpath_hash);
498 g_hash_table_destroy(entry->newpath_hash);
501 static void collect_manager_entry_init_data(CollectManagerEntry *entry)
503 entry->add_list = NULL;
504 entry->oldpath_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify) collect_manager_action_unref);
505 entry->newpath_hash = g_hash_table_new(g_str_hash, g_str_equal);
510 static CollectManagerEntry *collect_manager_entry_new(const gchar *path)
512 CollectManagerEntry *entry;
514 entry = g_new0(CollectManagerEntry, 1);
515 entry->path = g_strdup(path);
516 collect_manager_entry_init_data(entry);
518 collection_manager_entry_list = g_list_append(collection_manager_entry_list, entry);
524 static void collect_manager_entry_free(CollectManagerEntry *entry)
526 collection_manager_entry_list = g_list_remove(collection_manager_entry_list, entry);
528 collect_manager_entry_free_data(entry);
534 static void collect_manager_entry_reset(CollectManagerEntry *entry)
536 collect_manager_entry_free_data(entry);
537 collect_manager_entry_init_data(entry);
540 static CollectManagerEntry *collect_manager_get_entry(const gchar *path)
544 work = collection_manager_entry_list;
547 CollectManagerEntry *entry;
551 if (strcmp(entry->path, path) == 0)
560 static void collect_manager_entry_add_action(CollectManagerEntry *entry, CollectManagerAction *action)
563 CollectManagerAction *orig_action;
565 entry->empty = FALSE;
567 if (action->oldpath == NULL)
570 if (action->newpath == NULL)
575 orig_action = g_hash_table_lookup(entry->newpath_hash, action->newpath);
578 /* target already exists */
579 log_printf("collection manager failed to add another action for target %s in collection %s\n",
580 action->newpath, entry->path);
583 entry->add_list = g_list_append(entry->add_list, action);
584 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
585 collect_manager_action_ref(action);
589 orig_action = g_hash_table_lookup(entry->newpath_hash, action->oldpath);
592 /* new action with the same file */
593 CollectManagerAction *new_action = collect_manager_action_new(orig_action->oldpath, action->newpath, action->type);
595 if (new_action->oldpath)
597 g_hash_table_steal(entry->oldpath_hash, orig_action->oldpath);
598 g_hash_table_insert(entry->oldpath_hash, new_action->oldpath, new_action);
602 GList *work = g_list_find(entry->add_list, orig_action);
603 work->data = new_action;
606 g_hash_table_steal(entry->newpath_hash, orig_action->newpath);
607 if (new_action->newpath)
609 g_hash_table_insert(entry->newpath_hash, new_action->newpath, new_action);
611 collect_manager_action_unref(orig_action);
616 orig_action = g_hash_table_lookup(entry->oldpath_hash, action->oldpath);
619 /* another action for the same source, ignore */
620 log_printf("collection manager failed to add another action for source %s in collection %s\n",
621 action->oldpath, entry->path);
625 g_hash_table_insert(entry->oldpath_hash, action->oldpath, action);
628 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
630 collect_manager_action_ref(action);
633 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr)
635 gchar *path = *path_ptr;
636 CollectManagerAction *action;
643 action = entry->add_list->data;
644 g_assert(action->oldpath == NULL);
645 entry->add_list = g_list_remove(entry->add_list, action);
646 path = g_strdup(action->newpath);
647 g_hash_table_remove(entry->newpath_hash, path);
648 collect_manager_action_unref(action);
651 return (path != NULL);
654 action = g_hash_table_lookup(entry->oldpath_hash, path);
659 path = g_strdup(action->newpath);
664 return FALSE; /* no change */
667 static void collect_manager_refresh(void)
673 dir_fd = file_data_new_simple(get_collections_dir());
674 filelist_read(dir_fd, &list, NULL);
675 file_data_unref(dir_fd);
677 work = collection_manager_entry_list;
680 CollectManagerEntry *entry;
687 while (list_step && entry)
691 fd = list_step->data;
692 list_step = list_step->next;
694 if (strcmp(fd->path, entry->path) == 0)
696 list = g_list_remove(list, fd);
703 collect_manager_entry_free(entry);
716 collect_manager_entry_new(fd->path);
722 static void collect_manager_process_actions(gint max)
724 if (collection_manager_action_list) DEBUG_1("collection manager processing actions");
726 while (collection_manager_action_list != NULL && max > 0)
728 CollectManagerAction *action;
731 action = collection_manager_action_list->data;
732 work = collection_manager_entry_list;
735 CollectManagerEntry *entry;
740 if (action->type == COLLECTION_MANAGER_UPDATE)
742 collect_manager_entry_add_action(entry, action);
744 else if (action->oldpath && action->newpath &&
745 strcmp(action->newpath, entry->path) == 0)
747 /* convert action to standard add format */
748 g_free(action->newpath);
749 if (action->type == COLLECTION_MANAGER_ADD)
751 action->newpath = action->oldpath;
752 action->oldpath = NULL;
754 else if (action->type == COLLECTION_MANAGER_REMOVE)
756 action->newpath = NULL;
758 collect_manager_entry_add_action(entry, action);
764 if (action->type != COLLECTION_MANAGER_UPDATE &&
765 action->oldpath && action->newpath)
767 log_printf("collection manager failed to %s %s for collection %s\n",
768 (action->type == COLLECTION_MANAGER_ADD) ? "add" : "remove",
769 action->oldpath, action->newpath);
772 if (collection_manager_action_tail == collection_manager_action_list)
774 collection_manager_action_tail = NULL;
776 collection_manager_action_list = g_list_remove(collection_manager_action_list, action);
777 collect_manager_action_unref(action);
781 static gint collect_manager_process_entry(CollectManagerEntry *entry)
786 if (entry->empty) return FALSE;
788 cd = collection_new(entry->path);
789 success = collection_load_private(cd, entry->path, COLLECTION_LOAD_NONE);
791 collection_unref(cd);
796 static gint collect_manager_process_entry_list(void)
800 work = collection_manager_entry_list;
803 CollectManagerEntry *entry;
807 if (collect_manager_process_entry(entry)) return TRUE;
815 static gint collect_manager_process_cb(gpointer data)
817 if (collection_manager_action_list) collect_manager_refresh();
818 collect_manager_process_actions(COLLECT_MANAGER_ACTIONS_PER_IDLE);
819 if (collection_manager_action_list) return TRUE;
821 if (collect_manager_process_entry_list()) return TRUE;
823 DEBUG_1("collection manager is up to date");
827 static gint collect_manager_timer_cb(gpointer data)
829 DEBUG_1("collection manager timer expired");
831 g_idle_add_full(G_PRIORITY_LOW, collect_manager_process_cb, NULL, NULL);
833 collection_manager_timer_id = -1;
837 static void collect_manager_timer_push(gint stop)
839 if (collection_manager_timer_id != -1)
843 g_source_remove(collection_manager_timer_id);
844 collection_manager_timer_id = -1;
849 collection_manager_timer_id = g_timeout_add(COLLECT_MANAGER_FLUSH_DELAY,
850 collect_manager_timer_cb, NULL);
851 DEBUG_1("collection manager timer started");
855 static void collect_manager_add_action(CollectManagerAction *action)
859 /* we keep track of the list's tail to keep this a n(1) operation */
861 if (collection_manager_action_tail)
863 collection_manager_action_tail = g_list_append(collection_manager_action_tail, action);
864 collection_manager_action_tail = collection_manager_action_tail->next;
868 collection_manager_action_list = g_list_append(collection_manager_action_list, action);
869 collection_manager_action_tail = collection_manager_action_list;
872 collect_manager_timer_push(FALSE);
875 void collect_manager_moved(FileData *fd)
877 CollectManagerAction *action;
878 const gchar *oldpath = fd->change->source;
879 const gchar *newpath = fd->change->dest;
881 action = collect_manager_action_new(oldpath, newpath, COLLECTION_MANAGER_UPDATE);
882 collect_manager_add_action(action);
885 void collect_manager_add(FileData *fd, const gchar *collection)
887 CollectManagerAction *action;
890 if (!fd || !collection) return;
892 cw = collection_window_find_by_path(collection);
895 if (collection_list_find_fd(cw->cd->list, fd) == NULL)
897 collection_add(cw->cd, fd, FALSE);
902 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_ADD);
903 collect_manager_add_action(action);
906 void collect_manager_remove(FileData *fd, const gchar *collection)
908 CollectManagerAction *action;
911 if (!fd || !collection) return;
913 cw = collection_window_find_by_path(collection);
916 while (collection_remove(cw->cd, fd));
920 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_REMOVE);
921 collect_manager_add_action(action);
924 void collect_manager_flush(void)
926 collect_manager_timer_push(TRUE);
928 DEBUG_1("collection manager flushing");
929 while (collect_manager_process_cb(NULL));
932 void collect_manager_notify_cb(FileData *fd, NotifyType type, gpointer data)
935 if (type != NOTIFY_TYPE_CHANGE || !fd->change) return;
937 switch(fd->change->type)
939 case FILEDATA_CHANGE_MOVE:
940 collect_manager_moved(fd);
942 case FILEDATA_CHANGE_COPY:
944 case FILEDATA_CHANGE_RENAME:
945 collect_manager_moved(fd);
947 case FILEDATA_CHANGE_DELETE:
949 case FILEDATA_CHANGE_UNSPECIFIED:
954 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */