2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "collect-io.h"
27 #include "layout_util.h"
29 #include "secure_save.h"
31 #include "ui_fileops.h"
33 #define GQ_COLLECTION_MARKER "#" GQ_APPNAME
35 #define GQ_COLLECTION_FAIL_MIN 300
36 #define GQ_COLLECTION_FAIL_PERCENT 98
37 #define GQ_COLLECTION_READ_BUFSIZE 4096
39 typedef struct _CollectManagerEntry CollectManagerEntry;
41 static void collection_load_thumb_step(CollectionData *cd);
42 static gboolean collection_save_private(CollectionData *cd, const gchar *path);
44 static CollectManagerEntry *collect_manager_get_entry(const gchar *path);
45 static void collect_manager_entry_reset(CollectManagerEntry *entry);
46 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr);
49 static gboolean scan_geometry(gchar *buffer, gint *x, gint *y, gint *w, gint *h)
53 if (sscanf(buffer, "%d %d %d %d", &nx, &ny, &nw, &nh) != 4) return FALSE;
63 static gboolean collection_load_private(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
65 gchar s_buf[GQ_COLLECTION_READ_BUFSIZE];
68 gboolean limit_failures = TRUE;
69 gboolean success = TRUE;
70 gboolean has_official_header = FALSE;
71 gboolean has_geometry_header = FALSE;
72 gboolean has_gqview_header = FALSE;
73 gboolean need_header = TRUE;
76 gboolean changed = FALSE;
77 CollectManagerEntry *entry = NULL;
78 guint flush = !!(flags & COLLECTION_LOAD_FLUSH);
79 guint append = !!(flags & COLLECTION_LOAD_APPEND);
80 guint only_geometry = !!(flags & COLLECTION_LOAD_GEOMETRY);
84 collection_load_stop(cd);
87 collect_manager_flush();
89 entry = collect_manager_get_entry(path);
93 collection_list_free(cd->list);
98 if (!path && !cd->path) return FALSE;
100 if (!path) path = cd->path;
102 pathl = path_from_utf8(path);
104 DEBUG_1("collection load: append=%d flush=%d only_geometry=%d path=%s",
105 append, flush, only_geometry, pathl);
108 f = fopen(pathl, "r");
112 log_printf("Failed to open collection file: \"%s\"\n", path);
116 while (fgets(s_buf, sizeof(s_buf), f))
121 /* Skip whitespaces and empty lines */
122 while (*p && g_ascii_isspace(*p)) p++;
123 if (*p == '\n' || *p == '\r') continue;
128 if (!need_header) continue;
129 if (g_ascii_strncasecmp(p, GQ_COLLECTION_MARKER, strlen(GQ_COLLECTION_MARKER)) == 0)
131 /* Looks like an official collection, allow unchecked input.
132 * All this does is allow adding files that may not exist,
133 * which is needed for the collection manager to work.
134 * Also unofficial files abort after too many invalid entries.
136 has_official_header = TRUE;
137 limit_failures = FALSE;
139 else if (strncmp(p, "#geometry:", 10 ) == 0 &&
140 scan_geometry(p + 10, &cd->window_x, &cd->window_y, &cd->window_w, &cd->window_h))
142 has_geometry_header = TRUE;
143 cd->window_read = TRUE;
144 if (only_geometry) break;
146 else if (g_ascii_strncasecmp(p, "#GQview collection", strlen("#GQview collection")) == 0)
148 /* As 2008/04/15 there is no difference between our collection file format
149 * and GQview 2.1.5 collection file format so ignore failures as well. */
150 has_gqview_header = TRUE;
151 limit_failures = FALSE;
153 need_header = (!has_official_header && !has_gqview_header) || !has_geometry_header;
157 if (only_geometry) continue;
160 /* TODO: This is not safe! */
161 while (*p && *p != '"') p++;
164 while (*p && *p != '"') p++;
171 changed |= collect_manager_process_action(entry, &buf);
173 valid = (buf[0] == G_DIR_SEPARATOR && collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE));
174 if (!valid) DEBUG_1("collection invalid file: %s", buf);
180 if (limit_failures &&
181 fail > GQ_COLLECTION_FAIL_MIN &&
182 fail * 100 / total > GQ_COLLECTION_FAIL_PERCENT)
184 log_printf("%d invalid filenames in unofficial collection file, closing: %s\n", fail, path);
192 DEBUG_1("collection files: total = %d fail = %d official=%d gqview=%d geometry=%d",
193 total, fail, has_official_header, has_gqview_header, has_geometry_header);
196 if (only_geometry) return has_geometry_header;
201 while (collect_manager_process_action(entry, &buf))
203 collection_add_check(cd, file_data_new_group(buf), FALSE, TRUE);
210 cd->list = collection_list_sort(cd->list, cd->sort_method);
212 if (!flush && changed && success)
213 collection_save_private(cd, path);
216 collect_manager_entry_reset(entry);
218 if (!append) cd->changed = FALSE;
223 gboolean collection_load(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
225 if (collection_load_private(cd, path, flags | COLLECTION_LOAD_FLUSH))
227 layout_recent_add_path(cd->path);
234 static void collection_load_thumb_do(CollectionData *cd)
238 if (!cd->thumb_loader || !g_list_find(cd->list, cd->thumb_info)) return;
240 pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader);
241 collection_info_set_thumb(cd->thumb_info, pixbuf);
242 g_object_unref(pixbuf);
244 if (cd->info_updated_func) cd->info_updated_func(cd, cd->thumb_info, cd->info_updated_data);
247 static void collection_load_thumb_error_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_done_cb(ThumbLoader *tl, gpointer data)
257 CollectionData *cd = data;
259 collection_load_thumb_do(cd);
260 collection_load_thumb_step(cd);
263 static void collection_load_thumb_step(CollectionData *cd)
270 collection_load_stop(cd);
277 /* find first unloaded thumb */
278 while (work && ci->pixbuf)
284 if (!ci || ci->pixbuf)
287 collection_load_stop(cd);
289 /* send a NULL CollectInfo to notify end */
290 if (cd->info_updated_func) cd->info_updated_func(cd, NULL, cd->info_updated_data);
295 /* setup loader and call it */
297 thumb_loader_free(cd->thumb_loader);
298 cd->thumb_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
299 thumb_loader_set_callbacks(cd->thumb_loader,
300 collection_load_thumb_done_cb,
301 collection_load_thumb_error_cb,
306 if (!thumb_loader_start(cd->thumb_loader, ci->fd))
308 /* error, handle it, do next */
309 DEBUG_1("error loading thumb for %s", ci->fd->path);
310 collection_load_thumb_do(cd);
311 collection_load_thumb_step(cd);
315 void collection_load_thumb_idle(CollectionData *cd)
317 if (!cd->thumb_loader) collection_load_thumb_step(cd);
320 gboolean collection_load_begin(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
322 if (!collection_load(cd, path, flags)) return FALSE;
324 collection_load_thumb_idle(cd);
329 void collection_load_stop(CollectionData *cd)
331 if (!cd->thumb_loader) return;
333 thumb_loader_free(cd->thumb_loader);
334 cd->thumb_loader = NULL;
337 static gboolean collection_save_private(CollectionData *cd, const gchar *path)
343 if (!path && !cd->path) return FALSE;
351 pathl = path_from_utf8(path);
352 ssi = secure_open(pathl);
356 log_printf(_("failed to open collection (write) \"%s\"\n"), path);
360 secure_fprintf(ssi, "%s collection\n", GQ_COLLECTION_MARKER);
361 secure_fprintf(ssi, "#created with %s version %s\n", GQ_APPNAME, VERSION);
363 collection_update_geometry(cd);
366 secure_fprintf(ssi, "#geometry: %d %d %d %d\n", cd->window_x, cd->window_y, cd->window_w, cd->window_h);
370 while (work && secsave_errno == SS_ERR_NONE)
372 CollectInfo *ci = work->data;
373 secure_fprintf(ssi, "\"%s\"\n", ci->fd->path);
377 secure_fprintf(ssi, "#end\n");
379 if (secure_close(ssi))
381 log_printf(_("error saving collection file: %s\nerror: %s\n"), path,
382 secsave_strerror(secsave_errno));
386 if (!cd->path || strcmp(path, cd->path) != 0)
388 gchar *buf = cd->path;
389 cd->path = g_strdup(path);
394 cd->name = g_strdup(filename_from_path(cd->path));
396 collection_path_changed(cd);
404 gboolean collection_save(CollectionData *cd, const gchar *path)
406 if (collection_save_private(cd, path))
408 layout_recent_add_path(cd->path);
415 gboolean collection_load_only_geometry(CollectionData *cd, const gchar *path)
417 return collection_load(cd, path, COLLECTION_LOAD_GEOMETRY);
422 *-------------------------------------------------------------------
424 *-------------------------------------------------------------------
427 #define COLLECT_MANAGER_ACTIONS_PER_IDLE 1000
428 #define COLLECT_MANAGER_FLUSH_DELAY 10000
430 struct _CollectManagerEntry
434 GHashTable *oldpath_hash;
435 GHashTable *newpath_hash;
440 COLLECTION_MANAGER_UPDATE,
441 COLLECTION_MANAGER_ADD,
442 COLLECTION_MANAGER_REMOVE
443 } CollectManagerType;
445 typedef struct _CollectManagerAction CollectManagerAction;
446 struct _CollectManagerAction
451 CollectManagerType type;
457 static GList *collection_manager_entry_list = NULL;
458 static GList *collection_manager_action_list = NULL;
459 static GList *collection_manager_action_tail = NULL;
460 static guint collection_manager_timer_id = 0; /* event source id */
463 static CollectManagerAction *collect_manager_action_new(const gchar *oldpath, const gchar *newpath,
464 CollectManagerType type)
466 CollectManagerAction *action;
468 action = g_new0(CollectManagerAction, 1);
471 action->oldpath = g_strdup(oldpath);
472 action->newpath = g_strdup(newpath);
479 static void collect_manager_action_ref(CollectManagerAction *action)
484 static void collect_manager_action_unref(CollectManagerAction *action)
488 if (action->ref > 0) return;
490 g_free(action->oldpath);
491 g_free(action->newpath);
495 static void collect_manager_entry_free_data(CollectManagerEntry *entry)
499 work = entry->add_list;
502 CollectManagerAction *action;
507 collect_manager_action_unref(action);
509 g_list_free(entry->add_list);
510 if (g_hash_table_size(entry->oldpath_hash) > 0)
511 g_hash_table_destroy(entry->oldpath_hash);
513 g_hash_table_unref(entry->oldpath_hash);
514 if (g_hash_table_size(entry->newpath_hash) > 0)
515 g_hash_table_destroy(entry->newpath_hash);
517 g_hash_table_unref(entry->newpath_hash);
520 static void collect_manager_entry_init_data(CollectManagerEntry *entry)
522 entry->add_list = NULL;
523 entry->oldpath_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify) collect_manager_action_unref);
524 entry->newpath_hash = g_hash_table_new(g_str_hash, g_str_equal);
529 static CollectManagerEntry *collect_manager_entry_new(const gchar *path)
531 CollectManagerEntry *entry;
533 entry = g_new0(CollectManagerEntry, 1);
534 entry->path = g_strdup(path);
535 collect_manager_entry_init_data(entry);
537 collection_manager_entry_list = g_list_append(collection_manager_entry_list, entry);
543 static void collect_manager_entry_free(CollectManagerEntry *entry)
545 collection_manager_entry_list = g_list_remove(collection_manager_entry_list, entry);
547 collect_manager_entry_free_data(entry);
553 static void collect_manager_entry_reset(CollectManagerEntry *entry)
555 collect_manager_entry_free_data(entry);
556 collect_manager_entry_init_data(entry);
559 static CollectManagerEntry *collect_manager_get_entry(const gchar *path)
563 work = collection_manager_entry_list;
566 CollectManagerEntry *entry;
570 if (strcmp(entry->path, path) == 0)
579 static void collect_manager_entry_add_action(CollectManagerEntry *entry, CollectManagerAction *action)
582 CollectManagerAction *orig_action;
584 entry->empty = FALSE;
586 if (action->oldpath == NULL)
589 if (action->newpath == NULL)
594 orig_action = g_hash_table_lookup(entry->newpath_hash, action->newpath);
597 /* target already exists */
598 log_printf("collection manager failed to add another action for target %s in collection %s\n",
599 action->newpath, entry->path);
602 entry->add_list = g_list_append(entry->add_list, action);
603 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
604 collect_manager_action_ref(action);
608 orig_action = g_hash_table_lookup(entry->newpath_hash, action->oldpath);
611 /* new action with the same file */
612 CollectManagerAction *new_action = collect_manager_action_new(orig_action->oldpath, action->newpath, action->type);
614 if (new_action->oldpath)
616 g_hash_table_steal(entry->oldpath_hash, orig_action->oldpath);
617 g_hash_table_insert(entry->oldpath_hash, new_action->oldpath, new_action);
621 GList *work = g_list_find(entry->add_list, orig_action);
622 work->data = new_action;
625 g_hash_table_steal(entry->newpath_hash, orig_action->newpath);
626 if (new_action->newpath)
628 g_hash_table_insert(entry->newpath_hash, new_action->newpath, new_action);
630 collect_manager_action_unref(orig_action);
635 orig_action = g_hash_table_lookup(entry->oldpath_hash, action->oldpath);
638 /* another action for the same source, ignore */
639 log_printf("collection manager failed to add another action for source %s in collection %s\n",
640 action->oldpath, entry->path);
644 g_hash_table_insert(entry->oldpath_hash, action->oldpath, action);
647 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
649 collect_manager_action_ref(action);
652 static gboolean collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr)
654 gchar *path = *path_ptr;
655 CollectManagerAction *action;
662 action = entry->add_list->data;
663 g_assert(action->oldpath == NULL);
664 entry->add_list = g_list_remove(entry->add_list, action);
665 path = g_strdup(action->newpath);
666 g_hash_table_remove(entry->newpath_hash, path);
667 collect_manager_action_unref(action);
670 return (path != NULL);
673 action = g_hash_table_lookup(entry->oldpath_hash, path);
678 path = g_strdup(action->newpath);
683 return FALSE; /* no change */
686 static void collect_manager_refresh(void)
692 dir_fd = file_data_new_dir(get_collections_dir());
693 filelist_read(dir_fd, &list, NULL);
694 file_data_unref(dir_fd);
696 work = collection_manager_entry_list;
699 CollectManagerEntry *entry;
706 while (list_step && entry)
710 fd = list_step->data;
711 list_step = list_step->next;
713 if (strcmp(fd->path, entry->path) == 0)
715 list = g_list_remove(list, fd);
722 collect_manager_entry_free(entry);
737 collect_manager_entry_new(fd->path);
743 static void collect_manager_process_actions(gint max)
745 if (collection_manager_action_list) DEBUG_1("collection manager processing actions");
747 while (collection_manager_action_list != NULL && max > 0)
749 CollectManagerAction *action;
752 action = collection_manager_action_list->data;
753 work = collection_manager_entry_list;
756 CollectManagerEntry *entry;
761 if (action->type == COLLECTION_MANAGER_UPDATE)
763 collect_manager_entry_add_action(entry, action);
765 else if (action->oldpath && action->newpath &&
766 strcmp(action->newpath, entry->path) == 0)
768 /* convert action to standard add format */
769 g_free(action->newpath);
770 if (action->type == COLLECTION_MANAGER_ADD)
772 action->newpath = action->oldpath;
773 action->oldpath = NULL;
775 else if (action->type == COLLECTION_MANAGER_REMOVE)
777 action->newpath = NULL;
779 collect_manager_entry_add_action(entry, action);
785 if (action->type != COLLECTION_MANAGER_UPDATE &&
786 action->oldpath && action->newpath)
788 log_printf("collection manager failed to %s %s for collection %s\n",
789 (action->type == COLLECTION_MANAGER_ADD) ? "add" : "remove",
790 action->oldpath, action->newpath);
793 if (collection_manager_action_tail == collection_manager_action_list)
795 collection_manager_action_tail = NULL;
797 collection_manager_action_list = g_list_remove(collection_manager_action_list, action);
798 collect_manager_action_unref(action);
802 static gboolean collect_manager_process_entry(CollectManagerEntry *entry)
806 if (entry->empty) return FALSE;
808 cd = collection_new(entry->path);
809 (void) collection_load_private(cd, entry->path, COLLECTION_LOAD_NONE);
811 collection_unref(cd);
816 static gboolean collect_manager_process_entry_list(void)
820 work = collection_manager_entry_list;
823 CollectManagerEntry *entry;
827 if (collect_manager_process_entry(entry)) return TRUE;
835 static gboolean collect_manager_process_cb(gpointer data)
837 if (collection_manager_action_list) collect_manager_refresh();
838 collect_manager_process_actions(COLLECT_MANAGER_ACTIONS_PER_IDLE);
839 if (collection_manager_action_list) return TRUE;
841 if (collect_manager_process_entry_list()) return TRUE;
843 DEBUG_1("collection manager is up to date");
847 static gboolean collect_manager_timer_cb(gpointer data)
849 DEBUG_1("collection manager timer expired");
851 g_idle_add_full(G_PRIORITY_LOW, collect_manager_process_cb, NULL, NULL);
853 collection_manager_timer_id = 0;
857 static void collect_manager_timer_push(gint stop)
859 if (collection_manager_timer_id)
863 g_source_remove(collection_manager_timer_id);
864 collection_manager_timer_id = 0;
869 collection_manager_timer_id = g_timeout_add(COLLECT_MANAGER_FLUSH_DELAY,
870 collect_manager_timer_cb, NULL);
871 DEBUG_1("collection manager timer started");
875 static void collect_manager_add_action(CollectManagerAction *action)
879 /* we keep track of the list's tail to keep this a n(1) operation */
881 if (collection_manager_action_tail)
883 collection_manager_action_tail = g_list_append(collection_manager_action_tail, action);
884 collection_manager_action_tail = collection_manager_action_tail->next;
888 collection_manager_action_list = g_list_append(collection_manager_action_list, action);
889 collection_manager_action_tail = collection_manager_action_list;
892 collect_manager_timer_push(FALSE);
895 void collect_manager_moved(FileData *fd)
897 CollectManagerAction *action;
898 const gchar *oldpath = fd->change->source;
899 const gchar *newpath = fd->change->dest;
901 action = collect_manager_action_new(oldpath, newpath, COLLECTION_MANAGER_UPDATE);
902 collect_manager_add_action(action);
905 void collect_manager_add(FileData *fd, const gchar *collection)
907 CollectManagerAction *action;
910 if (!fd || !collection) return;
912 cw = collection_window_find_by_path(collection);
915 if (collection_list_find_fd(cw->cd->list, fd) == NULL)
917 collection_add(cw->cd, fd, FALSE);
922 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_ADD);
923 collect_manager_add_action(action);
926 void collect_manager_remove(FileData *fd, const gchar *collection)
928 CollectManagerAction *action;
931 if (!fd || !collection) return;
933 cw = collection_window_find_by_path(collection);
936 while (collection_remove(cw->cd, fd));
940 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_REMOVE);
941 collect_manager_add_action(action);
944 void collect_manager_flush(void)
946 collect_manager_timer_push(TRUE);
948 DEBUG_1("collection manager flushing");
949 while (collect_manager_process_cb(NULL));
952 void collect_manager_notify_cb(FileData *fd, NotifyType type, gpointer data)
954 if (!(type & NOTIFY_CHANGE) || !fd->change) return;
956 DEBUG_1("Notify collect_manager: %s %04x", fd->path, type);
957 switch (fd->change->type)
959 case FILEDATA_CHANGE_MOVE:
960 collect_manager_moved(fd);
962 case FILEDATA_CHANGE_COPY:
964 case FILEDATA_CHANGE_RENAME:
965 collect_manager_moved(fd);
967 case FILEDATA_CHANGE_DELETE:
968 case FILEDATA_CHANGE_UNSPECIFIED:
969 case FILEDATA_CHANGE_WRITE_METADATA:
974 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */