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;
150 buf = quoted_value(p, NULL);
156 changed |= collect_manager_process_action(entry, &buf);
158 valid = (buf[0] == '/' && collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE));
159 if (!valid) DEBUG_1("collection invalid file: %s", buf);
166 if (limit_failures &&
167 fail > GQ_COLLECTION_FAIL_MIN &&
168 fail * 100 / total > GQ_COLLECTION_FAIL_PERCENT)
170 log_printf("%d invalid filenames in unofficial collection file, closing: %s\n", fail, path);
178 DEBUG_1("collection files: total = %d fail = %d official=%d gqview=%d geometry=%d",
179 total, fail, has_official_header, has_gqview_header, has_geometry_header);
182 if (only_geometry) return has_geometry_header;
187 while (collect_manager_process_action(entry, &buf))
189 collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE);
196 cd->list = collection_list_sort(cd->list, cd->sort_method);
198 if (!flush && changed && success)
199 collection_save_private(cd, path);
202 collect_manager_entry_reset(entry);
204 if (!append) cd->changed = FALSE;
209 gint collection_load(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
211 if (collection_load_private(cd, path, flags | COLLECTION_LOAD_FLUSH))
213 layout_recent_add_path(cd->path);
220 static void collection_load_thumb_do(CollectionData *cd)
224 if (!cd->thumb_loader || !g_list_find(cd->list, cd->thumb_info)) return;
226 pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader, TRUE);
227 collection_info_set_thumb(cd->thumb_info, pixbuf);
228 g_object_unref(pixbuf);
230 if (cd->info_updated_func) cd->info_updated_func(cd, cd->thumb_info, cd->info_updated_data);
233 static void collection_load_thumb_error_cb(ThumbLoader *tl, gpointer data)
235 CollectionData *cd = data;
237 collection_load_thumb_do(cd);
238 collection_load_thumb_step(cd);
241 static void collection_load_thumb_done_cb(ThumbLoader *tl, gpointer data)
243 CollectionData *cd = data;
245 collection_load_thumb_do(cd);
246 collection_load_thumb_step(cd);
249 static void collection_load_thumb_step(CollectionData *cd)
256 collection_load_stop(cd);
263 /* find first unloaded thumb */
264 while (work && ci->pixbuf)
270 if (!ci || ci->pixbuf)
273 collection_load_stop(cd);
275 /* send a NULL CollectInfo to notify end */
276 if (cd->info_updated_func) cd->info_updated_func(cd, NULL, cd->info_updated_data);
281 /* setup loader and call it */
283 thumb_loader_free(cd->thumb_loader);
284 cd->thumb_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
285 thumb_loader_set_callbacks(cd->thumb_loader,
286 collection_load_thumb_done_cb,
287 collection_load_thumb_error_cb,
292 if (!thumb_loader_start(cd->thumb_loader, ci->fd->path))
294 /* error, handle it, do next */
295 DEBUG_1("error loading thumb for %s", ci->fd->path);
296 collection_load_thumb_do(cd);
297 collection_load_thumb_step(cd);
301 void collection_load_thumb_idle(CollectionData *cd)
303 if (!cd->thumb_loader) collection_load_thumb_step(cd);
306 gint collection_load_begin(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
308 if (!collection_load(cd, path, flags)) return FALSE;
310 collection_load_thumb_idle(cd);
315 void collection_load_stop(CollectionData *cd)
317 if (!cd->thumb_loader) return;
319 thumb_loader_free(cd->thumb_loader);
320 cd->thumb_loader = NULL;
323 static gint collection_save_private(CollectionData *cd, const gchar *path)
329 if (!path && !cd->path) return FALSE;
337 pathl = path_from_utf8(path);
338 ssi = secure_open(pathl);
342 log_printf(_("failed to open collection (write) \"%s\"\n"), path);
346 secure_fprintf(ssi, "%s collection\n", GQ_COLLECTION_MARKER);
347 secure_fprintf(ssi, "#created with %s version %s\n", GQ_APPNAME, VERSION);
349 collection_update_geometry(cd);
352 secure_fprintf(ssi, "#geometry: %d %d %d %d\n", cd->window_x, cd->window_y, cd->window_w, cd->window_h);
356 while (work && secsave_errno == SS_ERR_NONE)
358 CollectInfo *ci = work->data;
359 secure_fprintf(ssi, "\"%s\"\n", ci->fd->path);
363 secure_fprintf(ssi, "#end\n");
365 if (secure_close(ssi))
367 log_printf(_("error saving collection file: %s\nerror: %s\n"), path,
368 secsave_strerror(secsave_errno));
372 if (!cd->path || strcmp(path, cd->path) != 0)
374 gchar *buf = cd->path;
375 cd->path = g_strdup(path);
380 cd->name = g_strdup(filename_from_path(cd->path));
382 collection_path_changed(cd);
390 gint collection_save(CollectionData *cd, const gchar *path)
392 if (collection_save_private(cd, path))
394 layout_recent_add_path(cd->path);
401 gint collection_load_only_geometry(CollectionData *cd, const gchar *path)
403 return collection_load(cd, path, COLLECTION_LOAD_GEOMETRY);
408 *-------------------------------------------------------------------
410 *-------------------------------------------------------------------
413 #define COLLECT_MANAGER_ACTIONS_PER_IDLE 1000
414 #define COLLECT_MANAGER_FLUSH_DELAY 10000
416 struct _CollectManagerEntry
420 GHashTable *oldpath_hash;
421 GHashTable *newpath_hash;
426 COLLECTION_MANAGER_UPDATE,
427 COLLECTION_MANAGER_ADD,
428 COLLECTION_MANAGER_REMOVE
429 } CollectManagerType;
431 typedef struct _CollectManagerAction CollectManagerAction;
432 struct _CollectManagerAction
437 CollectManagerType type;
443 static GList *collection_manager_entry_list = NULL;
444 static GList *collection_manager_action_list = NULL;
445 static GList *collection_manager_action_tail = NULL;
446 static gint collection_manager_timer_id = -1;
449 static CollectManagerAction *collect_manager_action_new(const gchar *oldpath, const gchar *newpath,
450 CollectManagerType type)
452 CollectManagerAction *action;
454 action = g_new0(CollectManagerAction, 1);
457 action->oldpath = g_strdup(oldpath);
458 action->newpath = g_strdup(newpath);
465 static void collect_manager_action_ref(CollectManagerAction *action)
470 static void collect_manager_action_unref(CollectManagerAction *action)
474 if (action->ref > 0) return;
476 g_free(action->oldpath);
477 g_free(action->newpath);
481 static void collect_manager_entry_free_data(CollectManagerEntry *entry)
485 work = entry->add_list;
488 CollectManagerAction *action;
493 collect_manager_action_unref(action);
495 g_list_free(entry->add_list);
496 g_hash_table_destroy(entry->oldpath_hash);
497 g_hash_table_destroy(entry->newpath_hash);
500 static void collect_manager_entry_init_data(CollectManagerEntry *entry)
502 entry->add_list = NULL;
503 entry->oldpath_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify) collect_manager_action_unref);
504 entry->newpath_hash = g_hash_table_new(g_str_hash, g_str_equal);
509 static CollectManagerEntry *collect_manager_entry_new(const gchar *path)
511 CollectManagerEntry *entry;
513 entry = g_new0(CollectManagerEntry, 1);
514 entry->path = g_strdup(path);
515 collect_manager_entry_init_data(entry);
517 collection_manager_entry_list = g_list_append(collection_manager_entry_list, entry);
523 static void collect_manager_entry_free(CollectManagerEntry *entry)
525 collection_manager_entry_list = g_list_remove(collection_manager_entry_list, entry);
527 collect_manager_entry_free_data(entry);
533 static void collect_manager_entry_reset(CollectManagerEntry *entry)
535 collect_manager_entry_free_data(entry);
536 collect_manager_entry_init_data(entry);
539 static CollectManagerEntry *collect_manager_get_entry(const gchar *path)
543 work = collection_manager_entry_list;
546 CollectManagerEntry *entry;
550 if (strcmp(entry->path, path) == 0)
559 static void collect_manager_entry_add_action(CollectManagerEntry *entry, CollectManagerAction *action)
562 CollectManagerAction *orig_action;
564 entry->empty = FALSE;
566 if (action->oldpath == NULL)
569 if (action->newpath == NULL)
574 orig_action = g_hash_table_lookup(entry->newpath_hash, action->newpath);
577 /* target already exists */
578 log_printf("collection manager failed to add another action for target %s in collection %s\n",
579 action->newpath, entry->path);
582 entry->add_list = g_list_append(entry->add_list, action);
583 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
584 collect_manager_action_ref(action);
588 orig_action = g_hash_table_lookup(entry->newpath_hash, action->oldpath);
591 /* new action with the same file */
592 CollectManagerAction *new_action = collect_manager_action_new(orig_action->oldpath, action->newpath, action->type);
594 if (new_action->oldpath)
596 g_hash_table_steal(entry->oldpath_hash, orig_action->oldpath);
597 g_hash_table_insert(entry->oldpath_hash, new_action->oldpath, new_action);
601 GList *work = g_list_find(entry->add_list, orig_action);
602 work->data = new_action;
605 g_hash_table_steal(entry->newpath_hash, orig_action->newpath);
606 if (new_action->newpath)
608 g_hash_table_insert(entry->newpath_hash, new_action->newpath, new_action);
610 collect_manager_action_unref(orig_action);
615 orig_action = g_hash_table_lookup(entry->oldpath_hash, action->oldpath);
618 /* another action for the same source, ignore */
619 log_printf("collection manager failed to add another action for source %s in collection %s\n",
620 action->oldpath, entry->path);
624 g_hash_table_insert(entry->oldpath_hash, action->oldpath, action);
627 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
629 collect_manager_action_ref(action);
632 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr)
634 gchar *path = *path_ptr;
635 CollectManagerAction *action;
642 action = entry->add_list->data;
643 g_assert(action->oldpath == NULL);
644 entry->add_list = g_list_remove(entry->add_list, action);
645 path = g_strdup(action->newpath);
646 g_hash_table_remove(entry->newpath_hash, path);
647 collect_manager_action_unref(action);
650 return (path != NULL);
653 action = g_hash_table_lookup(entry->oldpath_hash, path);
658 path = g_strdup(action->newpath);
663 return FALSE; /* no change */
666 static void collect_manager_refresh(void)
672 base = g_build_filename(homedir(), GQ_RC_DIR_COLLECTIONS, NULL);
673 filelist_read(base, &list, NULL);
676 work = collection_manager_entry_list;
679 CollectManagerEntry *entry;
686 while (list_step && entry)
690 fd = list_step->data;
691 list_step = list_step->next;
693 if (strcmp(fd->path, entry->path) == 0)
695 list = g_list_remove(list, fd);
702 collect_manager_entry_free(entry);
715 collect_manager_entry_new(fd->path);
721 static void collect_manager_process_actions(gint max)
723 if (collection_manager_action_list) DEBUG_1("collection manager processing actions");
725 while (collection_manager_action_list != NULL && max > 0)
727 CollectManagerAction *action;
730 action = collection_manager_action_list->data;
731 work = collection_manager_entry_list;
734 CollectManagerEntry *entry;
739 if (action->type == COLLECTION_MANAGER_UPDATE)
741 collect_manager_entry_add_action(entry, action);
743 else if (action->oldpath && action->newpath &&
744 strcmp(action->newpath, entry->path) == 0)
746 /* convert action to standard add format */
747 g_free(action->newpath);
748 if (action->type == COLLECTION_MANAGER_ADD)
750 action->newpath = action->oldpath;
751 action->oldpath = NULL;
753 else if (action->type == COLLECTION_MANAGER_REMOVE)
755 action->newpath = NULL;
757 collect_manager_entry_add_action(entry, action);
763 if (action->type != COLLECTION_MANAGER_UPDATE &&
764 action->oldpath && action->newpath)
766 log_printf("collection manager failed to %s %s for collection %s\n",
767 (action->type == COLLECTION_MANAGER_ADD) ? "add" : "remove",
768 action->oldpath, action->newpath);
771 if (collection_manager_action_tail == collection_manager_action_list)
773 collection_manager_action_tail = NULL;
775 collection_manager_action_list = g_list_remove(collection_manager_action_list, action);
776 collect_manager_action_unref(action);
780 static gint collect_manager_process_entry(CollectManagerEntry *entry)
785 if (entry->empty) return FALSE;
787 cd = collection_new(entry->path);
788 success = collection_load_private(cd, entry->path, COLLECTION_LOAD_NONE);
790 collection_unref(cd);
795 static gint collect_manager_process_entry_list(void)
799 work = collection_manager_entry_list;
802 CollectManagerEntry *entry;
806 if (collect_manager_process_entry(entry)) return TRUE;
814 static gint collect_manager_process_cb(gpointer data)
816 if (collection_manager_action_list) collect_manager_refresh();
817 collect_manager_process_actions(COLLECT_MANAGER_ACTIONS_PER_IDLE);
818 if (collection_manager_action_list) return TRUE;
820 if (collect_manager_process_entry_list()) return TRUE;
822 DEBUG_1("collection manager is up to date");
826 static gint collect_manager_timer_cb(gpointer data)
828 DEBUG_1("collection manager timer expired");
830 g_idle_add_full(G_PRIORITY_LOW, collect_manager_process_cb, NULL, NULL);
832 collection_manager_timer_id = -1;
836 static void collect_manager_timer_push(gint stop)
838 if (collection_manager_timer_id != -1)
842 g_source_remove(collection_manager_timer_id);
843 collection_manager_timer_id = -1;
848 collection_manager_timer_id = g_timeout_add(COLLECT_MANAGER_FLUSH_DELAY,
849 collect_manager_timer_cb, NULL);
850 DEBUG_1("collection manager timer started");
854 static void collect_manager_add_action(CollectManagerAction *action)
858 /* we keep track of the list's tail to keep this a n(1) operation */
860 collection_manager_action_tail = g_list_append(collection_manager_action_tail, action);
861 if (!collection_manager_action_list)
862 collection_manager_action_list = collection_manager_action_tail;
863 collection_manager_action_tail = collection_manager_action_tail->next;
865 collect_manager_timer_push(FALSE);
868 void collect_manager_moved(FileData *fd)
870 CollectManagerAction *action;
871 const gchar *oldpath = fd->change->source;
872 const gchar *newpath = fd->change->dest;
874 action = collect_manager_action_new(oldpath, newpath, COLLECTION_MANAGER_UPDATE);
875 collect_manager_add_action(action);
878 void collect_manager_add(FileData *fd, const gchar *collection)
880 CollectManagerAction *action;
883 if (!fd || !collection) return;
885 cw = collection_window_find_by_path(collection);
888 if (collection_list_find(cw->cd->list, fd->path) == NULL)
890 collection_add(cw->cd, fd, FALSE);
895 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_ADD);
896 collect_manager_add_action(action);
899 void collect_manager_remove(FileData *fd, const gchar *collection)
901 CollectManagerAction *action;
904 if (!fd || !collection) return;
906 cw = collection_window_find_by_path(collection);
909 while (collection_remove(cw->cd, fd));
913 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_REMOVE);
914 collect_manager_add_action(action);
917 void collect_manager_flush(void)
919 collect_manager_timer_push(TRUE);
921 DEBUG_1("collection manager flushing");
922 while (collect_manager_process_cb(NULL));