7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
14 #include "collect-io.h"
18 #include "layout_util.h"
20 #include "secure_save.h"
22 #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;
64 gboolean changed = FALSE;
65 CollectManagerEntry *entry = NULL;
66 guint flush = flags & COLLECTION_LOAD_FLUSH;
67 guint append = flags & COLLECTION_LOAD_APPEND;
68 guint only_geometry = flags & COLLECTION_LOAD_GEOMETRY;
72 collection_load_stop(cd);
75 collect_manager_flush();
77 entry = collect_manager_get_entry(path);
81 collection_list_free(cd->list);
86 if (!path && !cd->path) return FALSE;
88 if (!path) path = cd->path;
90 if (debug) printf("collection load: append=%d flush=%d only_geometry=%d path=%s\n",
91 append, flush, only_geometry, path);
94 pathl = path_from_utf8(path);
95 f = fopen(pathl, "r");
99 printf("Failed to open collection file: \"%s\"\n", path);
103 while (fgets(s_buf, sizeof(s_buf), f))
108 if (strncasecmp(s_buf, GQ_COLLECTION_MARKER, strlen(GQ_COLLECTION_MARKER)) == 0)
110 /* Looks like an official collection, allow unchecked input.
111 * All this does is allow adding files that may not exist,
112 * which is needed for the collection manager to work.
113 * Also unofficial files abort after too many invalid entries.
115 limit_failures = FALSE;
117 else if (strncmp(s_buf, "#geometry:", 10 ) == 0 &&
118 scan_geometry(s_buf + 10, &cd->window_x, &cd->window_y, &cd->window_w, &cd->window_h) )
120 cd->window_read = TRUE;
127 else if (strncasecmp(s_buf, "GQview collection", strlen("GQview collection")) == 0)
129 /* As 2008/04/15 there is no difference between our collection file format
130 * and GQview 2.1.5 collection file format so ignore failures as well. */
131 limit_failures = FALSE;
135 if (s_buf[0]=='\n') continue;
137 buf = quoted_value(s_buf, NULL);
143 changed |= collect_manager_process_action(entry, &buf);
145 valid = (buf[0] == '/' && collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE));
146 if (debug && !valid) printf("collection invalid file: %s\n", buf);
150 if (!valid && limit_failures)
153 if (fail > GQ_COLLECTION_FAIL_MIN &&
154 fail * 100 / total > GQ_COLLECTION_FAIL_PERCENT)
156 printf("%d invalid filenames in unoffical collection file, closing: %s\n", fail, path);
164 if (debug) printf("collection files: total = %d fail = %d\n", total, fail);
167 if (only_geometry) return FALSE;
172 while (collect_manager_process_action(entry, &buf))
174 collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE);
180 cd->list = collection_list_sort(cd->list, cd->sort_method);
182 if (!flush && changed && success)
183 collection_save_private(cd, path);
186 collect_manager_entry_reset(entry);
188 if (!append) cd->changed = FALSE;
193 gint collection_load(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
195 if (collection_load_private(cd, path, flags | COLLECTION_LOAD_FLUSH))
197 layout_recent_add_path(cd->path);
204 static void collection_load_thumb_do(CollectionData *cd)
208 if (!cd->thumb_loader || !g_list_find(cd->list, cd->thumb_info)) return;
210 pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader, TRUE);
211 collection_info_set_thumb(cd->thumb_info, pixbuf);
212 g_object_unref(pixbuf);
214 if (cd->info_updated_func) cd->info_updated_func(cd, cd->thumb_info, cd->info_updated_data);
217 static void collection_load_thumb_error_cb(ThumbLoader *tl, gpointer data)
219 CollectionData *cd = data;
221 collection_load_thumb_do(cd);
222 collection_load_thumb_step(cd);
225 static void collection_load_thumb_done_cb(ThumbLoader *tl, gpointer data)
227 CollectionData *cd = data;
229 collection_load_thumb_do(cd);
230 collection_load_thumb_step(cd);
233 static void collection_load_thumb_step(CollectionData *cd)
240 collection_load_stop(cd);
247 /* find first unloaded thumb */
248 while (work && ci->pixbuf)
254 if (!ci || ci->pixbuf)
257 collection_load_stop(cd);
259 /* send a NULL CollectInfo to notify end */
260 if (cd->info_updated_func) cd->info_updated_func(cd, NULL, cd->info_updated_data);
265 /* setup loader and call it */
267 thumb_loader_free(cd->thumb_loader);
268 cd->thumb_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
269 thumb_loader_set_callbacks(cd->thumb_loader,
270 collection_load_thumb_done_cb,
271 collection_load_thumb_error_cb,
276 if (!thumb_loader_start(cd->thumb_loader, ci->fd->path))
278 /* error, handle it, do next */
279 if (debug) printf("error loading thumb for %s\n", ci->fd->path);
280 collection_load_thumb_do(cd);
281 collection_load_thumb_step(cd);
285 void collection_load_thumb_idle(CollectionData *cd)
287 if (!cd->thumb_loader) collection_load_thumb_step(cd);
290 gint collection_load_begin(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
292 if (!collection_load(cd, path, flags)) return FALSE;
294 collection_load_thumb_idle(cd);
299 void collection_load_stop(CollectionData *cd)
301 if (!cd->thumb_loader) return;
303 thumb_loader_free(cd->thumb_loader);
304 cd->thumb_loader = NULL;
307 static gint collection_save_private(CollectionData *cd, const gchar *path)
313 if (!path && !cd->path) return FALSE;
321 pathl = path_from_utf8(path);
322 ssi = secure_open(pathl);
328 buf = g_strdup_printf(_("failed to open collection (write) \"%s\"\n"), path);
334 secure_fprintf(ssi, "%s collection\n", GQ_COLLECTION_MARKER);
335 secure_fprintf(ssi, "#created with %s version %s\n", GQ_APPNAME, VERSION);
337 collection_update_geometry(cd);
340 secure_fprintf(ssi, "#geometry: %d %d %d %d\n", cd->window_x, cd->window_y, cd->window_w, cd->window_h);
344 while (work && secsave_errno == SS_ERR_NONE)
346 CollectInfo *ci = work->data;
347 secure_fprintf(ssi, "\"%s\"\n", ci->fd->path);
351 secure_fprintf(ssi, "#end\n");
353 if (secure_close(ssi))
357 buf = g_strdup_printf(_("error saving collection file: %s\nerror: %s\n"), path,
358 secsave_strerror(secsave_errno));
364 if (!cd->path || strcmp(path, cd->path) != 0)
366 gchar *buf = cd->path;
367 cd->path = g_strdup(path);
372 cd->name = g_strdup(filename_from_path(cd->path));
374 collection_path_changed(cd);
382 gint collection_save(CollectionData *cd, const gchar *path)
384 if (collection_save_private(cd, path))
386 layout_recent_add_path(cd->path);
393 gint collection_load_only_geometry(CollectionData *cd, const gchar *path)
395 return collection_load(cd, path, COLLECTION_LOAD_GEOMETRY);
400 *-------------------------------------------------------------------
402 *-------------------------------------------------------------------
405 #define COLLECT_MANAGER_ACTIONS_PER_IDLE 1000
406 #define COLLECT_MANAGER_FLUSH_DELAY 10000
408 struct _CollectManagerEntry
412 GHashTable *oldpath_hash;
413 GHashTable *newpath_hash;
418 COLLECTION_MANAGER_UPDATE,
419 COLLECTION_MANAGER_ADD,
420 COLLECTION_MANAGER_REMOVE
421 } CollectManagerType;
423 typedef struct _CollectManagerAction CollectManagerAction;
424 struct _CollectManagerAction
429 CollectManagerType type;
435 static GList *collection_manager_entry_list = NULL;
436 static GList *collection_manager_action_list = NULL;
437 static GList *collection_manager_action_tail = NULL;
438 static gint collection_manager_timer_id = -1;
441 static CollectManagerAction *collect_manager_action_new(const gchar *oldpath, const gchar *newpath,
442 CollectManagerType type)
444 CollectManagerAction *action;
446 action = g_new0(CollectManagerAction, 1);
449 action->oldpath = g_strdup(oldpath);
450 action->newpath = g_strdup(newpath);
457 static void collect_manager_action_ref(CollectManagerAction *action)
462 static void collect_manager_action_unref(CollectManagerAction *action)
466 if (action->ref > 0) return;
468 g_free(action->oldpath);
469 g_free(action->newpath);
473 static void collect_manager_entry_free_data(CollectManagerEntry *entry)
477 work = entry->add_list;
480 CollectManagerAction *action;
485 collect_manager_action_unref(action);
487 g_list_free(entry->add_list);
488 g_hash_table_destroy(entry->oldpath_hash);
489 g_hash_table_destroy(entry->newpath_hash);
492 static void collect_manager_entry_init_data(CollectManagerEntry *entry)
494 entry->add_list = NULL;
495 entry->oldpath_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify) collect_manager_action_unref);
496 entry->newpath_hash = g_hash_table_new(g_str_hash, g_str_equal);
501 static CollectManagerEntry *collect_manager_entry_new(const gchar *path)
503 CollectManagerEntry *entry;
505 entry = g_new0(CollectManagerEntry, 1);
506 entry->path = g_strdup(path);
507 collect_manager_entry_init_data(entry);
509 collection_manager_entry_list = g_list_append(collection_manager_entry_list, entry);
515 static void collect_manager_entry_free(CollectManagerEntry *entry)
517 collection_manager_entry_list = g_list_remove(collection_manager_entry_list, entry);
519 collect_manager_entry_free_data(entry);
525 static void collect_manager_entry_reset(CollectManagerEntry *entry)
527 collect_manager_entry_free_data(entry);
528 collect_manager_entry_init_data(entry);
531 static CollectManagerEntry *collect_manager_get_entry(const gchar *path)
535 work = collection_manager_entry_list;
538 CollectManagerEntry *entry;
542 if (strcmp(entry->path, path) == 0)
551 static void collect_manager_entry_add_action(CollectManagerEntry *entry, CollectManagerAction *action)
554 CollectManagerAction *orig_action;
556 entry->empty = FALSE;
558 if (action->oldpath == NULL)
561 if (action->newpath == NULL)
566 orig_action = g_hash_table_lookup(entry->newpath_hash, action->newpath);
569 /* target already exists */
570 printf("collection manager failed to add another action for target %s in collection %s\n",
571 action->newpath, entry->path);
574 entry->add_list = g_list_append(entry->add_list, action);
575 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
576 collect_manager_action_ref(action);
580 orig_action = g_hash_table_lookup(entry->newpath_hash, action->oldpath);
583 /* new action with the same file */
584 CollectManagerAction *new_action = collect_manager_action_new(orig_action->oldpath, action->newpath, action->type);
586 if (new_action->oldpath)
588 g_hash_table_steal(entry->oldpath_hash, orig_action->oldpath);
589 g_hash_table_insert(entry->oldpath_hash, new_action->oldpath, new_action);
593 GList *work = g_list_find(entry->add_list, orig_action);
594 work->data = new_action;
597 g_hash_table_steal(entry->newpath_hash, orig_action->newpath);
598 if (new_action->newpath)
600 g_hash_table_insert(entry->newpath_hash, new_action->newpath, new_action);
602 collect_manager_action_unref(orig_action);
607 orig_action = g_hash_table_lookup(entry->oldpath_hash, action->oldpath);
610 /* another action for the same source, ignore */
611 printf("collection manager failed to add another action for source %s in collection %s\n",
612 action->oldpath, entry->path);
616 g_hash_table_insert(entry->oldpath_hash, action->oldpath, action);
619 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
621 collect_manager_action_ref(action);
624 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr)
626 gchar *path = *path_ptr;
627 CollectManagerAction *action;
634 action = entry->add_list->data;
635 g_assert(action->oldpath == NULL);
636 entry->add_list = g_list_remove(entry->add_list, action);
637 path = g_strdup(action->newpath);
638 g_hash_table_remove(entry->newpath_hash, path);
639 collect_manager_action_unref(action);
642 return (path != NULL);
645 action = g_hash_table_lookup(entry->oldpath_hash, path);
650 path = g_strdup(action->newpath);
655 return FALSE; /* no change */
658 static void collect_manager_refresh(void)
664 base = g_strconcat(homedir(), "/", GQ_RC_DIR_COLLECTIONS, NULL);
665 path_list(base, &list, NULL);
668 work = collection_manager_entry_list;
671 CollectManagerEntry *entry;
678 while (list_step && entry)
682 path = list_step->data;
683 list_step = list_step->next;
685 if (strcmp(path, entry->path) == 0)
687 list = g_list_remove(list, path);
694 collect_manager_entry_free(entry);
707 collect_manager_entry_new(path);
714 static void collect_manager_process_actions(gint max)
716 if (debug && collection_manager_action_list)
718 printf("collection manager processing actions\n");
721 while (collection_manager_action_list != NULL && max > 0)
723 CollectManagerAction *action;
726 action = collection_manager_action_list->data;
727 work = collection_manager_entry_list;
730 CollectManagerEntry *entry;
735 if (action->type == COLLECTION_MANAGER_UPDATE)
737 collect_manager_entry_add_action(entry, action);
739 else if (action->oldpath && action->newpath &&
740 strcmp(action->newpath, entry->path) == 0)
742 /* convert action to standard add format */
743 g_free(action->newpath);
744 if (action->type == COLLECTION_MANAGER_ADD)
746 action->newpath = action->oldpath;
747 action->oldpath = NULL;
749 else if (action->type == COLLECTION_MANAGER_REMOVE)
751 action->newpath = NULL;
753 collect_manager_entry_add_action(entry, action);
759 if (action->type != COLLECTION_MANAGER_UPDATE &&
760 action->oldpath && action->newpath)
762 printf("collection manager failed to %s %s for collection %s\n",
763 (action->type == COLLECTION_MANAGER_ADD) ? "add" : "remove",
764 action->oldpath, action->newpath);
767 if (collection_manager_action_tail == collection_manager_action_list)
769 collection_manager_action_tail = NULL;
771 collection_manager_action_list = g_list_remove(collection_manager_action_list, action);
772 collect_manager_action_unref(action);
776 static gint collect_manager_process_entry(CollectManagerEntry *entry)
781 if (entry->empty) return FALSE;
783 cd = collection_new(entry->path);
784 success = collection_load_private(cd, entry->path, COLLECTION_LOAD_NONE);
786 collection_unref(cd);
791 static gint collect_manager_process_entry_list(void)
795 work = collection_manager_entry_list;
798 CollectManagerEntry *entry;
802 if (collect_manager_process_entry(entry)) return TRUE;
810 static gint collect_manager_process_cb(gpointer data)
812 if (collection_manager_action_list) collect_manager_refresh();
813 collect_manager_process_actions(COLLECT_MANAGER_ACTIONS_PER_IDLE);
814 if (collection_manager_action_list) return TRUE;
816 if (collect_manager_process_entry_list()) return TRUE;
818 if (debug) printf("collection manager is up to date\n");
822 static gint collect_manager_timer_cb(gpointer data)
824 if (debug) printf("collection manager timer expired\n");
826 g_idle_add_full(G_PRIORITY_LOW, collect_manager_process_cb, NULL, NULL);
828 collection_manager_timer_id = -1;
832 static void collect_manager_timer_push(gint stop)
834 if (collection_manager_timer_id != -1)
838 g_source_remove(collection_manager_timer_id);
839 collection_manager_timer_id = -1;
844 collection_manager_timer_id = g_timeout_add(COLLECT_MANAGER_FLUSH_DELAY,
845 collect_manager_timer_cb, NULL);
846 if (debug) printf("collection manager timer started\n");
850 static void collect_manager_add_action(CollectManagerAction *action)
854 /* we keep track of the list's tail to keep this a n(1) operation */
856 if (collection_manager_action_tail)
858 collection_manager_action_tail = g_list_append(collection_manager_action_tail, action);
859 collection_manager_action_tail = collection_manager_action_tail->next;
863 collection_manager_action_list = g_list_append(collection_manager_action_list, action);
864 collection_manager_action_tail = collection_manager_action_list;
867 collect_manager_timer_push(FALSE);
870 void collect_manager_moved(FileData *fd)
872 CollectManagerAction *action;
873 const gchar *oldpath = fd->change->source;
874 const gchar *newpath = fd->change->dest;
876 action = collect_manager_action_new(oldpath, newpath, COLLECTION_MANAGER_UPDATE);
877 collect_manager_add_action(action);
880 void collect_manager_add(FileData *fd, const gchar *collection)
882 CollectManagerAction *action;
885 if (!fd || !collection) return;
887 cw = collection_window_find_by_path(collection);
890 if (collection_list_find(cw->cd->list, fd->path) == NULL)
892 collection_add(cw->cd, fd, FALSE);
897 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_ADD);
898 collect_manager_add_action(action);
901 void collect_manager_remove(FileData *fd, const gchar *collection)
903 CollectManagerAction *action;
906 if (!fd || !collection) return;
908 cw = collection_window_find_by_path(collection);
911 while (collection_remove(cw->cd, fd));
915 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_REMOVE);
916 collect_manager_add_action(action);
919 void collect_manager_flush(void)
921 collect_manager_timer_push(TRUE);
923 if (debug) printf("collection manager flushing\n");
924 while (collect_manager_process_cb(NULL));