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"
26 #define GQ_COLLECTION_MARKER "#" GQ_APPNAME
28 #define GQ_COLLECTION_FAIL_MIN 300
29 #define GQ_COLLECTION_FAIL_PERCENT 98
30 #define GQ_COLLECTION_READ_BUFSIZE 4096
32 typedef struct _CollectManagerEntry CollectManagerEntry;
34 static void collection_load_thumb_step(CollectionData *cd);
35 static gint collection_save_private(CollectionData *cd, const gchar *path);
37 static CollectManagerEntry *collect_manager_get_entry(const gchar *path);
38 static void collect_manager_entry_reset(CollectManagerEntry *entry);
39 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr);
42 static gint scan_geometry(gchar *buffer, gint *x, gint *y, gint *w, gint *h)
46 if (sscanf(buffer, "%d %d %d %d", &nx, &ny, &nw, &nh) != 4) return FALSE;
56 static gint collection_load_private(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
58 gchar s_buf[GQ_COLLECTION_READ_BUFSIZE];
61 gint limit_failures = TRUE;
63 gint has_official_header = FALSE;
64 gint has_geometry_header = FALSE;
65 gint has_gqview_header = FALSE;
66 gint need_header = TRUE;
69 gboolean changed = FALSE;
70 CollectManagerEntry *entry = NULL;
71 guint flush = !!(flags & COLLECTION_LOAD_FLUSH);
72 guint append = !!(flags & COLLECTION_LOAD_APPEND);
73 guint only_geometry = !!(flags & COLLECTION_LOAD_GEOMETRY);
77 collection_load_stop(cd);
80 collect_manager_flush();
82 entry = collect_manager_get_entry(path);
86 collection_list_free(cd->list);
91 if (!path && !cd->path) return FALSE;
93 if (!path) path = cd->path;
95 DEBUG_1("collection load: append=%d flush=%d only_geometry=%d path=%s",
96 append, flush, only_geometry, path);
99 pathl = path_from_utf8(path);
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 buf = quoted_value(p, NULL);
158 changed |= collect_manager_process_action(entry, &buf);
160 valid = (buf[0] == G_DIR_SEPARATOR && collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE));
161 if (!valid) DEBUG_1("collection invalid file: %s", buf);
168 if (limit_failures &&
169 fail > GQ_COLLECTION_FAIL_MIN &&
170 fail * 100 / total > GQ_COLLECTION_FAIL_PERCENT)
172 log_printf("%d invalid filenames in unofficial collection file, closing: %s\n", fail, path);
180 DEBUG_1("collection files: total = %d fail = %d official=%d gqview=%d geometry=%d",
181 total, fail, has_official_header, has_gqview_header, has_geometry_header);
184 if (only_geometry) return has_geometry_header;
189 while (collect_manager_process_action(entry, &buf))
191 collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE);
198 cd->list = collection_list_sort(cd->list, cd->sort_method);
200 if (!flush && changed && success)
201 collection_save_private(cd, path);
204 collect_manager_entry_reset(entry);
206 if (!append) cd->changed = FALSE;
211 gint collection_load(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
213 if (collection_load_private(cd, path, flags | COLLECTION_LOAD_FLUSH))
215 layout_recent_add_path(cd->path);
222 static void collection_load_thumb_do(CollectionData *cd)
226 if (!cd->thumb_loader || !g_list_find(cd->list, cd->thumb_info)) return;
228 pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader);
229 collection_info_set_thumb(cd->thumb_info, pixbuf);
230 g_object_unref(pixbuf);
232 if (cd->info_updated_func) cd->info_updated_func(cd, cd->thumb_info, cd->info_updated_data);
235 static void collection_load_thumb_error_cb(ThumbLoader *tl, gpointer data)
237 CollectionData *cd = data;
239 collection_load_thumb_do(cd);
240 collection_load_thumb_step(cd);
243 static void collection_load_thumb_done_cb(ThumbLoader *tl, gpointer data)
245 CollectionData *cd = data;
247 collection_load_thumb_do(cd);
248 collection_load_thumb_step(cd);
251 static void collection_load_thumb_step(CollectionData *cd)
258 collection_load_stop(cd);
265 /* find first unloaded thumb */
266 while (work && ci->pixbuf)
272 if (!ci || ci->pixbuf)
275 collection_load_stop(cd);
277 /* send a NULL CollectInfo to notify end */
278 if (cd->info_updated_func) cd->info_updated_func(cd, NULL, cd->info_updated_data);
283 /* setup loader and call it */
285 thumb_loader_free(cd->thumb_loader);
286 cd->thumb_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
287 thumb_loader_set_callbacks(cd->thumb_loader,
288 collection_load_thumb_done_cb,
289 collection_load_thumb_error_cb,
294 if (!thumb_loader_start(cd->thumb_loader, ci->fd))
296 /* error, handle it, do next */
297 DEBUG_1("error loading thumb for %s", ci->fd->path);
298 collection_load_thumb_do(cd);
299 collection_load_thumb_step(cd);
303 void collection_load_thumb_idle(CollectionData *cd)
305 if (!cd->thumb_loader) collection_load_thumb_step(cd);
308 gint collection_load_begin(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
310 if (!collection_load(cd, path, flags)) return FALSE;
312 collection_load_thumb_idle(cd);
317 void collection_load_stop(CollectionData *cd)
319 if (!cd->thumb_loader) return;
321 thumb_loader_free(cd->thumb_loader);
322 cd->thumb_loader = NULL;
325 static gint collection_save_private(CollectionData *cd, const gchar *path)
331 if (!path && !cd->path) return FALSE;
339 pathl = path_from_utf8(path);
340 ssi = secure_open(pathl);
344 log_printf(_("failed to open collection (write) \"%s\"\n"), path);
348 secure_fprintf(ssi, "%s collection\n", GQ_COLLECTION_MARKER);
349 secure_fprintf(ssi, "#created with %s version %s\n", GQ_APPNAME, VERSION);
351 collection_update_geometry(cd);
354 secure_fprintf(ssi, "#geometry: %d %d %d %d\n", cd->window_x, cd->window_y, cd->window_w, cd->window_h);
358 while (work && secsave_errno == SS_ERR_NONE)
360 CollectInfo *ci = work->data;
361 secure_fprintf(ssi, "\"%s\"\n", ci->fd->path);
365 secure_fprintf(ssi, "#end\n");
367 if (secure_close(ssi))
369 log_printf(_("error saving collection file: %s\nerror: %s\n"), path,
370 secsave_strerror(secsave_errno));
374 if (!cd->path || strcmp(path, cd->path) != 0)
376 gchar *buf = cd->path;
377 cd->path = g_strdup(path);
382 cd->name = g_strdup(filename_from_path(cd->path));
384 collection_path_changed(cd);
392 gint collection_save(CollectionData *cd, const gchar *path)
394 if (collection_save_private(cd, path))
396 layout_recent_add_path(cd->path);
403 gint collection_load_only_geometry(CollectionData *cd, const gchar *path)
405 return collection_load(cd, path, COLLECTION_LOAD_GEOMETRY);
410 *-------------------------------------------------------------------
412 *-------------------------------------------------------------------
415 #define COLLECT_MANAGER_ACTIONS_PER_IDLE 1000
416 #define COLLECT_MANAGER_FLUSH_DELAY 10000
418 struct _CollectManagerEntry
422 GHashTable *oldpath_hash;
423 GHashTable *newpath_hash;
428 COLLECTION_MANAGER_UPDATE,
429 COLLECTION_MANAGER_ADD,
430 COLLECTION_MANAGER_REMOVE
431 } CollectManagerType;
433 typedef struct _CollectManagerAction CollectManagerAction;
434 struct _CollectManagerAction
439 CollectManagerType type;
445 static GList *collection_manager_entry_list = NULL;
446 static GList *collection_manager_action_list = NULL;
447 static GList *collection_manager_action_tail = NULL;
448 static gint collection_manager_timer_id = -1;
451 static CollectManagerAction *collect_manager_action_new(const gchar *oldpath, const gchar *newpath,
452 CollectManagerType type)
454 CollectManagerAction *action;
456 action = g_new0(CollectManagerAction, 1);
459 action->oldpath = g_strdup(oldpath);
460 action->newpath = g_strdup(newpath);
467 static void collect_manager_action_ref(CollectManagerAction *action)
472 static void collect_manager_action_unref(CollectManagerAction *action)
476 if (action->ref > 0) return;
478 g_free(action->oldpath);
479 g_free(action->newpath);
483 static void collect_manager_entry_free_data(CollectManagerEntry *entry)
487 work = entry->add_list;
490 CollectManagerAction *action;
495 collect_manager_action_unref(action);
497 g_list_free(entry->add_list);
498 g_hash_table_destroy(entry->oldpath_hash);
499 g_hash_table_destroy(entry->newpath_hash);
502 static void collect_manager_entry_init_data(CollectManagerEntry *entry)
504 entry->add_list = NULL;
505 entry->oldpath_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify) collect_manager_action_unref);
506 entry->newpath_hash = g_hash_table_new(g_str_hash, g_str_equal);
511 static CollectManagerEntry *collect_manager_entry_new(const gchar *path)
513 CollectManagerEntry *entry;
515 entry = g_new0(CollectManagerEntry, 1);
516 entry->path = g_strdup(path);
517 collect_manager_entry_init_data(entry);
519 collection_manager_entry_list = g_list_append(collection_manager_entry_list, entry);
525 static void collect_manager_entry_free(CollectManagerEntry *entry)
527 collection_manager_entry_list = g_list_remove(collection_manager_entry_list, entry);
529 collect_manager_entry_free_data(entry);
535 static void collect_manager_entry_reset(CollectManagerEntry *entry)
537 collect_manager_entry_free_data(entry);
538 collect_manager_entry_init_data(entry);
541 static CollectManagerEntry *collect_manager_get_entry(const gchar *path)
545 work = collection_manager_entry_list;
548 CollectManagerEntry *entry;
552 if (strcmp(entry->path, path) == 0)
561 static void collect_manager_entry_add_action(CollectManagerEntry *entry, CollectManagerAction *action)
564 CollectManagerAction *orig_action;
566 entry->empty = FALSE;
568 if (action->oldpath == NULL)
571 if (action->newpath == NULL)
576 orig_action = g_hash_table_lookup(entry->newpath_hash, action->newpath);
579 /* target already exists */
580 log_printf("collection manager failed to add another action for target %s in collection %s\n",
581 action->newpath, entry->path);
584 entry->add_list = g_list_append(entry->add_list, action);
585 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
586 collect_manager_action_ref(action);
590 orig_action = g_hash_table_lookup(entry->newpath_hash, action->oldpath);
593 /* new action with the same file */
594 CollectManagerAction *new_action = collect_manager_action_new(orig_action->oldpath, action->newpath, action->type);
596 if (new_action->oldpath)
598 g_hash_table_steal(entry->oldpath_hash, orig_action->oldpath);
599 g_hash_table_insert(entry->oldpath_hash, new_action->oldpath, new_action);
603 GList *work = g_list_find(entry->add_list, orig_action);
604 work->data = new_action;
607 g_hash_table_steal(entry->newpath_hash, orig_action->newpath);
608 if (new_action->newpath)
610 g_hash_table_insert(entry->newpath_hash, new_action->newpath, new_action);
612 collect_manager_action_unref(orig_action);
617 orig_action = g_hash_table_lookup(entry->oldpath_hash, action->oldpath);
620 /* another action for the same source, ignore */
621 log_printf("collection manager failed to add another action for source %s in collection %s\n",
622 action->oldpath, entry->path);
626 g_hash_table_insert(entry->oldpath_hash, action->oldpath, action);
629 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
631 collect_manager_action_ref(action);
634 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr)
636 gchar *path = *path_ptr;
637 CollectManagerAction *action;
644 action = entry->add_list->data;
645 g_assert(action->oldpath == NULL);
646 entry->add_list = g_list_remove(entry->add_list, action);
647 path = g_strdup(action->newpath);
648 g_hash_table_remove(entry->newpath_hash, path);
649 collect_manager_action_unref(action);
652 return (path != NULL);
655 action = g_hash_table_lookup(entry->oldpath_hash, path);
660 path = g_strdup(action->newpath);
665 return FALSE; /* no change */
668 static void collect_manager_refresh(void)
675 base = g_build_filename(homedir(), GQ_RC_DIR_COLLECTIONS, NULL);
676 dir_fd = file_data_new_simple(base);
677 filelist_read(dir_fd, &list, NULL);
678 file_data_unref(dir_fd);
681 work = collection_manager_entry_list;
684 CollectManagerEntry *entry;
691 while (list_step && entry)
695 fd = list_step->data;
696 list_step = list_step->next;
698 if (strcmp(fd->path, entry->path) == 0)
700 list = g_list_remove(list, fd);
707 collect_manager_entry_free(entry);
720 collect_manager_entry_new(fd->path);
726 static void collect_manager_process_actions(gint max)
728 if (collection_manager_action_list) DEBUG_1("collection manager processing actions");
730 while (collection_manager_action_list != NULL && max > 0)
732 CollectManagerAction *action;
735 action = collection_manager_action_list->data;
736 work = collection_manager_entry_list;
739 CollectManagerEntry *entry;
744 if (action->type == COLLECTION_MANAGER_UPDATE)
746 collect_manager_entry_add_action(entry, action);
748 else if (action->oldpath && action->newpath &&
749 strcmp(action->newpath, entry->path) == 0)
751 /* convert action to standard add format */
752 g_free(action->newpath);
753 if (action->type == COLLECTION_MANAGER_ADD)
755 action->newpath = action->oldpath;
756 action->oldpath = NULL;
758 else if (action->type == COLLECTION_MANAGER_REMOVE)
760 action->newpath = NULL;
762 collect_manager_entry_add_action(entry, action);
768 if (action->type != COLLECTION_MANAGER_UPDATE &&
769 action->oldpath && action->newpath)
771 log_printf("collection manager failed to %s %s for collection %s\n",
772 (action->type == COLLECTION_MANAGER_ADD) ? "add" : "remove",
773 action->oldpath, action->newpath);
776 if (collection_manager_action_tail == collection_manager_action_list)
778 collection_manager_action_tail = NULL;
780 collection_manager_action_list = g_list_remove(collection_manager_action_list, action);
781 collect_manager_action_unref(action);
785 static gint collect_manager_process_entry(CollectManagerEntry *entry)
790 if (entry->empty) return FALSE;
792 cd = collection_new(entry->path);
793 success = collection_load_private(cd, entry->path, COLLECTION_LOAD_NONE);
795 collection_unref(cd);
800 static gint collect_manager_process_entry_list(void)
804 work = collection_manager_entry_list;
807 CollectManagerEntry *entry;
811 if (collect_manager_process_entry(entry)) return TRUE;
819 static gint collect_manager_process_cb(gpointer data)
821 if (collection_manager_action_list) collect_manager_refresh();
822 collect_manager_process_actions(COLLECT_MANAGER_ACTIONS_PER_IDLE);
823 if (collection_manager_action_list) return TRUE;
825 if (collect_manager_process_entry_list()) return TRUE;
827 DEBUG_1("collection manager is up to date");
831 static gint collect_manager_timer_cb(gpointer data)
833 DEBUG_1("collection manager timer expired");
835 g_idle_add_full(G_PRIORITY_LOW, collect_manager_process_cb, NULL, NULL);
837 collection_manager_timer_id = -1;
841 static void collect_manager_timer_push(gint stop)
843 if (collection_manager_timer_id != -1)
847 g_source_remove(collection_manager_timer_id);
848 collection_manager_timer_id = -1;
853 collection_manager_timer_id = g_timeout_add(COLLECT_MANAGER_FLUSH_DELAY,
854 collect_manager_timer_cb, NULL);
855 DEBUG_1("collection manager timer started");
859 static void collect_manager_add_action(CollectManagerAction *action)
863 /* we keep track of the list's tail to keep this a n(1) operation */
865 if (collection_manager_action_tail)
867 collection_manager_action_tail = g_list_append(collection_manager_action_tail, action);
868 collection_manager_action_tail = collection_manager_action_tail->next;
872 collection_manager_action_list = g_list_append(collection_manager_action_list, action);
873 collection_manager_action_tail = collection_manager_action_list;
876 collect_manager_timer_push(FALSE);
879 void collect_manager_moved(FileData *fd)
881 CollectManagerAction *action;
882 const gchar *oldpath = fd->change->source;
883 const gchar *newpath = fd->change->dest;
885 action = collect_manager_action_new(oldpath, newpath, COLLECTION_MANAGER_UPDATE);
886 collect_manager_add_action(action);
889 void collect_manager_add(FileData *fd, const gchar *collection)
891 CollectManagerAction *action;
894 if (!fd || !collection) return;
896 cw = collection_window_find_by_path(collection);
899 if (collection_list_find_fd(cw->cd->list, fd) == NULL)
901 collection_add(cw->cd, fd, FALSE);
906 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_ADD);
907 collect_manager_add_action(action);
910 void collect_manager_remove(FileData *fd, const gchar *collection)
912 CollectManagerAction *action;
915 if (!fd || !collection) return;
917 cw = collection_window_find_by_path(collection);
920 while (collection_remove(cw->cd, fd));
924 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_REMOVE);
925 collect_manager_add_action(action);
928 void collect_manager_flush(void)
930 collect_manager_timer_push(TRUE);
932 DEBUG_1("collection manager flushing");
933 while (collect_manager_process_cb(NULL));
936 void collect_manager_notify_cb(FileData *fd, NotifyType type, gpointer data)
939 if (type != NOTIFY_TYPE_CHANGE || !fd->change) return;
941 switch(fd->change->type)
943 case FILEDATA_CHANGE_MOVE:
944 collect_manager_moved(fd);
946 case FILEDATA_CHANGE_COPY:
948 case FILEDATA_CHANGE_RENAME:
949 collect_manager_moved(fd);
951 case FILEDATA_CHANGE_DELETE:
953 case FILEDATA_CHANGE_UNSPECIFIED: