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));
149 if (!valid && limit_failures)
152 if (fail > GQ_COLLECTION_FAIL_MIN &&
153 fail * 100 / total > GQ_COLLECTION_FAIL_PERCENT)
155 printf("Too many invalid filenames in unoffical collection file, closing: %s\n", path);
164 if (only_geometry) return FALSE;
169 while (collect_manager_process_action(entry, &buf))
171 collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE);
177 cd->list = collection_list_sort(cd->list, cd->sort_method);
179 if (!flush && changed && success)
180 collection_save_private(cd, path);
183 collect_manager_entry_reset(entry);
185 if (!append) cd->changed = FALSE;
190 gint collection_load(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
192 if (collection_load_private(cd, path, flags | COLLECTION_LOAD_FLUSH))
194 layout_recent_add_path(cd->path);
201 static void collection_load_thumb_do(CollectionData *cd)
205 if (!cd->thumb_loader || !g_list_find(cd->list, cd->thumb_info)) return;
207 pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader, TRUE);
208 collection_info_set_thumb(cd->thumb_info, pixbuf);
209 g_object_unref(pixbuf);
211 if (cd->info_updated_func) cd->info_updated_func(cd, cd->thumb_info, cd->info_updated_data);
214 static void collection_load_thumb_error_cb(ThumbLoader *tl, gpointer data)
216 CollectionData *cd = data;
218 collection_load_thumb_do(cd);
219 collection_load_thumb_step(cd);
222 static void collection_load_thumb_done_cb(ThumbLoader *tl, gpointer data)
224 CollectionData *cd = data;
226 collection_load_thumb_do(cd);
227 collection_load_thumb_step(cd);
230 static void collection_load_thumb_step(CollectionData *cd)
237 collection_load_stop(cd);
244 /* find first unloaded thumb */
245 while (work && ci->pixbuf)
251 if (!ci || ci->pixbuf)
254 collection_load_stop(cd);
256 /* send a NULL CollectInfo to notify end */
257 if (cd->info_updated_func) cd->info_updated_func(cd, NULL, cd->info_updated_data);
262 /* setup loader and call it */
264 thumb_loader_free(cd->thumb_loader);
265 cd->thumb_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
266 thumb_loader_set_callbacks(cd->thumb_loader,
267 collection_load_thumb_done_cb,
268 collection_load_thumb_error_cb,
273 if (!thumb_loader_start(cd->thumb_loader, ci->fd->path))
275 /* error, handle it, do next */
276 if (debug) printf("error loading thumb for %s\n", ci->fd->path);
277 collection_load_thumb_do(cd);
278 collection_load_thumb_step(cd);
282 void collection_load_thumb_idle(CollectionData *cd)
284 if (!cd->thumb_loader) collection_load_thumb_step(cd);
287 gint collection_load_begin(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
289 if (!collection_load(cd, path, flags)) return FALSE;
291 collection_load_thumb_idle(cd);
296 void collection_load_stop(CollectionData *cd)
298 if (!cd->thumb_loader) return;
300 thumb_loader_free(cd->thumb_loader);
301 cd->thumb_loader = NULL;
304 static gint collection_save_private(CollectionData *cd, const gchar *path)
310 if (!path && !cd->path) return FALSE;
318 pathl = path_from_utf8(path);
319 ssi = secure_open(pathl);
325 buf = g_strdup_printf(_("failed to open collection (write) \"%s\"\n"), path);
331 secure_fprintf(ssi, "%s collection\n", GQ_COLLECTION_MARKER);
332 secure_fprintf(ssi, "#created with %s version %s\n", GQ_APPNAME, VERSION);
334 collection_update_geometry(cd);
337 secure_fprintf(ssi, "#geometry: %d %d %d %d\n", cd->window_x, cd->window_y, cd->window_w, cd->window_h);
341 while (work && secsave_errno == SS_ERR_NONE)
343 CollectInfo *ci = work->data;
344 secure_fprintf(ssi, "\"%s\"\n", ci->fd->path);
348 secure_fprintf(ssi, "#end\n");
350 if (secure_close(ssi))
354 buf = g_strdup_printf(_("error saving collection file: %s\nerror: %s\n"), path,
355 secsave_strerror(secsave_errno));
361 if (!cd->path || strcmp(path, cd->path) != 0)
363 gchar *buf = cd->path;
364 cd->path = g_strdup(path);
369 cd->name = g_strdup(filename_from_path(cd->path));
371 collection_path_changed(cd);
379 gint collection_save(CollectionData *cd, const gchar *path)
381 if (collection_save_private(cd, path))
383 layout_recent_add_path(cd->path);
390 gint collection_load_only_geometry(CollectionData *cd, const gchar *path)
392 return collection_load(cd, path, COLLECTION_LOAD_GEOMETRY);
397 *-------------------------------------------------------------------
399 *-------------------------------------------------------------------
402 #define COLLECT_MANAGER_ACTIONS_PER_IDLE 1000
403 #define COLLECT_MANAGER_FLUSH_DELAY 10000
405 struct _CollectManagerEntry
409 GHashTable *oldpath_hash;
410 GHashTable *newpath_hash;
415 COLLECTION_MANAGER_UPDATE,
416 COLLECTION_MANAGER_ADD,
417 COLLECTION_MANAGER_REMOVE
418 } CollectManagerType;
420 typedef struct _CollectManagerAction CollectManagerAction;
421 struct _CollectManagerAction
426 CollectManagerType type;
432 static GList *collection_manager_entry_list = NULL;
433 static GList *collection_manager_action_list = NULL;
434 static GList *collection_manager_action_tail = NULL;
435 static gint collection_manager_timer_id = -1;
438 static CollectManagerAction *collect_manager_action_new(const gchar *oldpath, const gchar *newpath,
439 CollectManagerType type)
441 CollectManagerAction *action;
443 action = g_new0(CollectManagerAction, 1);
446 action->oldpath = g_strdup(oldpath);
447 action->newpath = g_strdup(newpath);
454 static void collect_manager_action_ref(CollectManagerAction *action)
459 static void collect_manager_action_unref(CollectManagerAction *action)
463 if (action->ref > 0) return;
465 g_free(action->oldpath);
466 g_free(action->newpath);
470 static void collect_manager_entry_free_data(CollectManagerEntry *entry)
474 work = entry->add_list;
477 CollectManagerAction *action;
482 collect_manager_action_unref(action);
484 g_list_free(entry->add_list);
485 g_hash_table_destroy(entry->oldpath_hash);
486 g_hash_table_destroy(entry->newpath_hash);
489 static void collect_manager_entry_init_data(CollectManagerEntry *entry)
491 entry->add_list = NULL;
492 entry->oldpath_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify) collect_manager_action_unref);
493 entry->newpath_hash = g_hash_table_new(g_str_hash, g_str_equal);
498 static CollectManagerEntry *collect_manager_entry_new(const gchar *path)
500 CollectManagerEntry *entry;
502 entry = g_new0(CollectManagerEntry, 1);
503 entry->path = g_strdup(path);
504 collect_manager_entry_init_data(entry);
506 collection_manager_entry_list = g_list_append(collection_manager_entry_list, entry);
512 static void collect_manager_entry_free(CollectManagerEntry *entry)
514 collection_manager_entry_list = g_list_remove(collection_manager_entry_list, entry);
516 collect_manager_entry_free_data(entry);
522 static void collect_manager_entry_reset(CollectManagerEntry *entry)
524 collect_manager_entry_free_data(entry);
525 collect_manager_entry_init_data(entry);
528 static CollectManagerEntry *collect_manager_get_entry(const gchar *path)
532 work = collection_manager_entry_list;
535 CollectManagerEntry *entry;
539 if (strcmp(entry->path, path) == 0)
548 static void collect_manager_entry_add_action(CollectManagerEntry *entry, CollectManagerAction *action)
551 CollectManagerAction *orig_action;
553 entry->empty = FALSE;
555 if (action->oldpath == NULL)
558 if (action->newpath == NULL)
563 orig_action = g_hash_table_lookup(entry->newpath_hash, action->newpath);
566 /* target already exists */
567 printf("collection manager failed to add another action for target %s in collection %s\n",
568 action->newpath, entry->path);
571 entry->add_list = g_list_append(entry->add_list, action);
572 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
573 collect_manager_action_ref(action);
577 orig_action = g_hash_table_lookup(entry->newpath_hash, action->oldpath);
580 /* new action with the same file */
581 CollectManagerAction *new_action = collect_manager_action_new(orig_action->oldpath, action->newpath, action->type);
583 if (new_action->oldpath)
585 g_hash_table_steal(entry->oldpath_hash, orig_action->oldpath);
586 g_hash_table_insert(entry->oldpath_hash, new_action->oldpath, new_action);
590 GList *work = g_list_find(entry->add_list, orig_action);
591 work->data = new_action;
594 g_hash_table_steal(entry->newpath_hash, orig_action->newpath);
595 if (new_action->newpath)
597 g_hash_table_insert(entry->newpath_hash, new_action->newpath, new_action);
599 collect_manager_action_unref(orig_action);
604 orig_action = g_hash_table_lookup(entry->oldpath_hash, action->oldpath);
607 /* another action for the same source, ignore */
608 printf("collection manager failed to add another action for source %s in collection %s\n",
609 action->oldpath, entry->path);
613 g_hash_table_insert(entry->oldpath_hash, action->oldpath, action);
616 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
618 collect_manager_action_ref(action);
621 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr)
623 gchar *path = *path_ptr;
624 CollectManagerAction *action;
631 action = entry->add_list->data;
632 g_assert(action->oldpath == NULL);
633 entry->add_list = g_list_remove(entry->add_list, action);
634 path = g_strdup(action->newpath);
635 g_hash_table_remove(entry->newpath_hash, path);
636 collect_manager_action_unref(action);
639 return (path != NULL);
642 action = g_hash_table_lookup(entry->oldpath_hash, path);
647 path = g_strdup(action->newpath);
652 return FALSE; /* no change */
655 static void collect_manager_refresh(void)
661 base = g_strconcat(homedir(), "/", GQ_RC_DIR_COLLECTIONS, NULL);
662 path_list(base, &list, NULL);
665 work = collection_manager_entry_list;
668 CollectManagerEntry *entry;
675 while (list_step && entry)
679 path = list_step->data;
680 list_step = list_step->next;
682 if (strcmp(path, entry->path) == 0)
684 list = g_list_remove(list, path);
691 collect_manager_entry_free(entry);
704 collect_manager_entry_new(path);
711 static void collect_manager_process_actions(gint max)
713 if (debug && collection_manager_action_list)
715 printf("collection manager processing actions\n");
718 while (collection_manager_action_list != NULL && max > 0)
720 CollectManagerAction *action;
723 action = collection_manager_action_list->data;
724 work = collection_manager_entry_list;
727 CollectManagerEntry *entry;
732 if (action->type == COLLECTION_MANAGER_UPDATE)
734 collect_manager_entry_add_action(entry, action);
736 else if (action->oldpath && action->newpath &&
737 strcmp(action->newpath, entry->path) == 0)
739 /* convert action to standard add format */
740 g_free(action->newpath);
741 if (action->type == COLLECTION_MANAGER_ADD)
743 action->newpath = action->oldpath;
744 action->oldpath = NULL;
746 else if (action->type == COLLECTION_MANAGER_REMOVE)
748 action->newpath = NULL;
750 collect_manager_entry_add_action(entry, action);
756 if (action->type != COLLECTION_MANAGER_UPDATE &&
757 action->oldpath && action->newpath)
759 printf("collection manager failed to %s %s for collection %s\n",
760 (action->type == COLLECTION_MANAGER_ADD) ? "add" : "remove",
761 action->oldpath, action->newpath);
764 if (collection_manager_action_tail == collection_manager_action_list)
766 collection_manager_action_tail = NULL;
768 collection_manager_action_list = g_list_remove(collection_manager_action_list, action);
769 collect_manager_action_unref(action);
773 static gint collect_manager_process_entry(CollectManagerEntry *entry)
778 if (entry->empty) return FALSE;
780 cd = collection_new(entry->path);
781 success = collection_load_private(cd, entry->path, COLLECTION_LOAD_NONE);
783 collection_unref(cd);
788 static gint collect_manager_process_entry_list(void)
792 work = collection_manager_entry_list;
795 CollectManagerEntry *entry;
799 if (collect_manager_process_entry(entry)) return TRUE;
807 static gint collect_manager_process_cb(gpointer data)
809 if (collection_manager_action_list) collect_manager_refresh();
810 collect_manager_process_actions(COLLECT_MANAGER_ACTIONS_PER_IDLE);
811 if (collection_manager_action_list) return TRUE;
813 if (collect_manager_process_entry_list()) return TRUE;
815 if (debug) printf("collection manager is up to date\n");
819 static gint collect_manager_timer_cb(gpointer data)
821 if (debug) printf("collection manager timer expired\n");
823 g_idle_add_full(G_PRIORITY_LOW, collect_manager_process_cb, NULL, NULL);
825 collection_manager_timer_id = -1;
829 static void collect_manager_timer_push(gint stop)
831 if (collection_manager_timer_id != -1)
835 g_source_remove(collection_manager_timer_id);
836 collection_manager_timer_id = -1;
841 collection_manager_timer_id = g_timeout_add(COLLECT_MANAGER_FLUSH_DELAY,
842 collect_manager_timer_cb, NULL);
843 if (debug) printf("collection manager timer started\n");
847 static void collect_manager_add_action(CollectManagerAction *action)
851 /* we keep track of the list's tail to keep this a n(1) operation */
853 if (collection_manager_action_tail)
855 collection_manager_action_tail = g_list_append(collection_manager_action_tail, action);
856 collection_manager_action_tail = collection_manager_action_tail->next;
860 collection_manager_action_list = g_list_append(collection_manager_action_list, action);
861 collection_manager_action_tail = collection_manager_action_list;
864 collect_manager_timer_push(FALSE);
867 void collect_manager_moved(FileData *fd)
869 CollectManagerAction *action;
870 const gchar *oldpath = fd->change->source;
871 const gchar *newpath = fd->change->dest;
873 action = collect_manager_action_new(oldpath, newpath, COLLECTION_MANAGER_UPDATE);
874 collect_manager_add_action(action);
877 void collect_manager_add(FileData *fd, const gchar *collection)
879 CollectManagerAction *action;
882 if (!fd || !collection) return;
884 cw = collection_window_find_by_path(collection);
887 if (collection_list_find(cw->cd->list, fd->path) == NULL)
889 collection_add(cw->cd, fd, FALSE);
894 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_ADD);
895 collect_manager_add_action(action);
898 void collect_manager_remove(FileData *fd, const gchar *collection)
900 CollectManagerAction *action;
903 if (!fd || !collection) return;
905 cw = collection_window_find_by_path(collection);
908 while (collection_remove(cw->cd, fd));
912 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_REMOVE);
913 collect_manager_add_action(action);
916 void collect_manager_flush(void)
918 collect_manager_timer_push(TRUE);
920 if (debug) printf("collection manager flushing\n");
921 while (collect_manager_process_cb(NULL));