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 /* Skip whitespaces and empty lines */
109 while (*p && g_ascii_isspace(*p)) p++;
110 if (*p == '\n' || *p == '\r') continue;
115 if (strncasecmp(p, GQ_COLLECTION_MARKER, strlen(GQ_COLLECTION_MARKER)) == 0)
117 /* Looks like an official collection, allow unchecked input.
118 * All this does is allow adding files that may not exist,
119 * which is needed for the collection manager to work.
120 * Also unofficial files abort after too many invalid entries.
122 limit_failures = FALSE;
124 else if (strncmp(p, "#geometry:", 10 ) == 0 &&
125 scan_geometry(p + 10, &cd->window_x, &cd->window_y, &cd->window_w, &cd->window_h) )
127 cd->window_read = TRUE;
134 else if (strncasecmp(p, "#GQview collection", strlen("#GQview collection")) == 0)
136 /* As 2008/04/15 there is no difference between our collection file format
137 * and GQview 2.1.5 collection file format so ignore failures as well. */
138 limit_failures = FALSE;
144 buf = quoted_value(p, NULL);
150 changed |= collect_manager_process_action(entry, &buf);
152 valid = (buf[0] == '/' && collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE));
153 if (debug && !valid) printf("collection invalid file: %s\n", buf);
157 if (!valid && limit_failures)
160 if (fail > GQ_COLLECTION_FAIL_MIN &&
161 fail * 100 / total > GQ_COLLECTION_FAIL_PERCENT)
163 printf("%d invalid filenames in unoffical collection file, closing: %s\n", fail, path);
171 if (debug) printf("collection files: total = %d fail = %d\n", total, fail);
174 if (only_geometry) return FALSE;
179 while (collect_manager_process_action(entry, &buf))
181 collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE);
187 cd->list = collection_list_sort(cd->list, cd->sort_method);
189 if (!flush && changed && success)
190 collection_save_private(cd, path);
193 collect_manager_entry_reset(entry);
195 if (!append) cd->changed = FALSE;
200 gint collection_load(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
202 if (collection_load_private(cd, path, flags | COLLECTION_LOAD_FLUSH))
204 layout_recent_add_path(cd->path);
211 static void collection_load_thumb_do(CollectionData *cd)
215 if (!cd->thumb_loader || !g_list_find(cd->list, cd->thumb_info)) return;
217 pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader, TRUE);
218 collection_info_set_thumb(cd->thumb_info, pixbuf);
219 g_object_unref(pixbuf);
221 if (cd->info_updated_func) cd->info_updated_func(cd, cd->thumb_info, cd->info_updated_data);
224 static void collection_load_thumb_error_cb(ThumbLoader *tl, gpointer data)
226 CollectionData *cd = data;
228 collection_load_thumb_do(cd);
229 collection_load_thumb_step(cd);
232 static void collection_load_thumb_done_cb(ThumbLoader *tl, gpointer data)
234 CollectionData *cd = data;
236 collection_load_thumb_do(cd);
237 collection_load_thumb_step(cd);
240 static void collection_load_thumb_step(CollectionData *cd)
247 collection_load_stop(cd);
254 /* find first unloaded thumb */
255 while (work && ci->pixbuf)
261 if (!ci || ci->pixbuf)
264 collection_load_stop(cd);
266 /* send a NULL CollectInfo to notify end */
267 if (cd->info_updated_func) cd->info_updated_func(cd, NULL, cd->info_updated_data);
272 /* setup loader and call it */
274 thumb_loader_free(cd->thumb_loader);
275 cd->thumb_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
276 thumb_loader_set_callbacks(cd->thumb_loader,
277 collection_load_thumb_done_cb,
278 collection_load_thumb_error_cb,
283 if (!thumb_loader_start(cd->thumb_loader, ci->fd->path))
285 /* error, handle it, do next */
286 if (debug) printf("error loading thumb for %s\n", ci->fd->path);
287 collection_load_thumb_do(cd);
288 collection_load_thumb_step(cd);
292 void collection_load_thumb_idle(CollectionData *cd)
294 if (!cd->thumb_loader) collection_load_thumb_step(cd);
297 gint collection_load_begin(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
299 if (!collection_load(cd, path, flags)) return FALSE;
301 collection_load_thumb_idle(cd);
306 void collection_load_stop(CollectionData *cd)
308 if (!cd->thumb_loader) return;
310 thumb_loader_free(cd->thumb_loader);
311 cd->thumb_loader = NULL;
314 static gint collection_save_private(CollectionData *cd, const gchar *path)
320 if (!path && !cd->path) return FALSE;
328 pathl = path_from_utf8(path);
329 ssi = secure_open(pathl);
335 buf = g_strdup_printf(_("failed to open collection (write) \"%s\"\n"), path);
341 secure_fprintf(ssi, "%s collection\n", GQ_COLLECTION_MARKER);
342 secure_fprintf(ssi, "#created with %s version %s\n", GQ_APPNAME, VERSION);
344 collection_update_geometry(cd);
347 secure_fprintf(ssi, "#geometry: %d %d %d %d\n", cd->window_x, cd->window_y, cd->window_w, cd->window_h);
351 while (work && secsave_errno == SS_ERR_NONE)
353 CollectInfo *ci = work->data;
354 secure_fprintf(ssi, "\"%s\"\n", ci->fd->path);
358 secure_fprintf(ssi, "#end\n");
360 if (secure_close(ssi))
364 buf = g_strdup_printf(_("error saving collection file: %s\nerror: %s\n"), path,
365 secsave_strerror(secsave_errno));
371 if (!cd->path || strcmp(path, cd->path) != 0)
373 gchar *buf = cd->path;
374 cd->path = g_strdup(path);
379 cd->name = g_strdup(filename_from_path(cd->path));
381 collection_path_changed(cd);
389 gint collection_save(CollectionData *cd, const gchar *path)
391 if (collection_save_private(cd, path))
393 layout_recent_add_path(cd->path);
400 gint collection_load_only_geometry(CollectionData *cd, const gchar *path)
402 return collection_load(cd, path, COLLECTION_LOAD_GEOMETRY);
407 *-------------------------------------------------------------------
409 *-------------------------------------------------------------------
412 #define COLLECT_MANAGER_ACTIONS_PER_IDLE 1000
413 #define COLLECT_MANAGER_FLUSH_DELAY 10000
415 struct _CollectManagerEntry
419 GHashTable *oldpath_hash;
420 GHashTable *newpath_hash;
425 COLLECTION_MANAGER_UPDATE,
426 COLLECTION_MANAGER_ADD,
427 COLLECTION_MANAGER_REMOVE
428 } CollectManagerType;
430 typedef struct _CollectManagerAction CollectManagerAction;
431 struct _CollectManagerAction
436 CollectManagerType type;
442 static GList *collection_manager_entry_list = NULL;
443 static GList *collection_manager_action_list = NULL;
444 static GList *collection_manager_action_tail = NULL;
445 static gint collection_manager_timer_id = -1;
448 static CollectManagerAction *collect_manager_action_new(const gchar *oldpath, const gchar *newpath,
449 CollectManagerType type)
451 CollectManagerAction *action;
453 action = g_new0(CollectManagerAction, 1);
456 action->oldpath = g_strdup(oldpath);
457 action->newpath = g_strdup(newpath);
464 static void collect_manager_action_ref(CollectManagerAction *action)
469 static void collect_manager_action_unref(CollectManagerAction *action)
473 if (action->ref > 0) return;
475 g_free(action->oldpath);
476 g_free(action->newpath);
480 static void collect_manager_entry_free_data(CollectManagerEntry *entry)
484 work = entry->add_list;
487 CollectManagerAction *action;
492 collect_manager_action_unref(action);
494 g_list_free(entry->add_list);
495 g_hash_table_destroy(entry->oldpath_hash);
496 g_hash_table_destroy(entry->newpath_hash);
499 static void collect_manager_entry_init_data(CollectManagerEntry *entry)
501 entry->add_list = NULL;
502 entry->oldpath_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify) collect_manager_action_unref);
503 entry->newpath_hash = g_hash_table_new(g_str_hash, g_str_equal);
508 static CollectManagerEntry *collect_manager_entry_new(const gchar *path)
510 CollectManagerEntry *entry;
512 entry = g_new0(CollectManagerEntry, 1);
513 entry->path = g_strdup(path);
514 collect_manager_entry_init_data(entry);
516 collection_manager_entry_list = g_list_append(collection_manager_entry_list, entry);
522 static void collect_manager_entry_free(CollectManagerEntry *entry)
524 collection_manager_entry_list = g_list_remove(collection_manager_entry_list, entry);
526 collect_manager_entry_free_data(entry);
532 static void collect_manager_entry_reset(CollectManagerEntry *entry)
534 collect_manager_entry_free_data(entry);
535 collect_manager_entry_init_data(entry);
538 static CollectManagerEntry *collect_manager_get_entry(const gchar *path)
542 work = collection_manager_entry_list;
545 CollectManagerEntry *entry;
549 if (strcmp(entry->path, path) == 0)
558 static void collect_manager_entry_add_action(CollectManagerEntry *entry, CollectManagerAction *action)
561 CollectManagerAction *orig_action;
563 entry->empty = FALSE;
565 if (action->oldpath == NULL)
568 if (action->newpath == NULL)
573 orig_action = g_hash_table_lookup(entry->newpath_hash, action->newpath);
576 /* target already exists */
577 printf("collection manager failed to add another action for target %s in collection %s\n",
578 action->newpath, entry->path);
581 entry->add_list = g_list_append(entry->add_list, action);
582 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
583 collect_manager_action_ref(action);
587 orig_action = g_hash_table_lookup(entry->newpath_hash, action->oldpath);
590 /* new action with the same file */
591 CollectManagerAction *new_action = collect_manager_action_new(orig_action->oldpath, action->newpath, action->type);
593 if (new_action->oldpath)
595 g_hash_table_steal(entry->oldpath_hash, orig_action->oldpath);
596 g_hash_table_insert(entry->oldpath_hash, new_action->oldpath, new_action);
600 GList *work = g_list_find(entry->add_list, orig_action);
601 work->data = new_action;
604 g_hash_table_steal(entry->newpath_hash, orig_action->newpath);
605 if (new_action->newpath)
607 g_hash_table_insert(entry->newpath_hash, new_action->newpath, new_action);
609 collect_manager_action_unref(orig_action);
614 orig_action = g_hash_table_lookup(entry->oldpath_hash, action->oldpath);
617 /* another action for the same source, ignore */
618 printf("collection manager failed to add another action for source %s in collection %s\n",
619 action->oldpath, entry->path);
623 g_hash_table_insert(entry->oldpath_hash, action->oldpath, action);
626 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
628 collect_manager_action_ref(action);
631 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr)
633 gchar *path = *path_ptr;
634 CollectManagerAction *action;
641 action = entry->add_list->data;
642 g_assert(action->oldpath == NULL);
643 entry->add_list = g_list_remove(entry->add_list, action);
644 path = g_strdup(action->newpath);
645 g_hash_table_remove(entry->newpath_hash, path);
646 collect_manager_action_unref(action);
649 return (path != NULL);
652 action = g_hash_table_lookup(entry->oldpath_hash, path);
657 path = g_strdup(action->newpath);
662 return FALSE; /* no change */
665 static void collect_manager_refresh(void)
671 base = g_strconcat(homedir(), "/", GQ_RC_DIR_COLLECTIONS, NULL);
672 path_list(base, &list, NULL);
675 work = collection_manager_entry_list;
678 CollectManagerEntry *entry;
685 while (list_step && entry)
689 path = list_step->data;
690 list_step = list_step->next;
692 if (strcmp(path, entry->path) == 0)
694 list = g_list_remove(list, path);
701 collect_manager_entry_free(entry);
714 collect_manager_entry_new(path);
721 static void collect_manager_process_actions(gint max)
723 if (debug && collection_manager_action_list)
725 printf("collection manager processing actions\n");
728 while (collection_manager_action_list != NULL && max > 0)
730 CollectManagerAction *action;
733 action = collection_manager_action_list->data;
734 work = collection_manager_entry_list;
737 CollectManagerEntry *entry;
742 if (action->type == COLLECTION_MANAGER_UPDATE)
744 collect_manager_entry_add_action(entry, action);
746 else if (action->oldpath && action->newpath &&
747 strcmp(action->newpath, entry->path) == 0)
749 /* convert action to standard add format */
750 g_free(action->newpath);
751 if (action->type == COLLECTION_MANAGER_ADD)
753 action->newpath = action->oldpath;
754 action->oldpath = NULL;
756 else if (action->type == COLLECTION_MANAGER_REMOVE)
758 action->newpath = NULL;
760 collect_manager_entry_add_action(entry, action);
766 if (action->type != COLLECTION_MANAGER_UPDATE &&
767 action->oldpath && action->newpath)
769 printf("collection manager failed to %s %s for collection %s\n",
770 (action->type == COLLECTION_MANAGER_ADD) ? "add" : "remove",
771 action->oldpath, action->newpath);
774 if (collection_manager_action_tail == collection_manager_action_list)
776 collection_manager_action_tail = NULL;
778 collection_manager_action_list = g_list_remove(collection_manager_action_list, action);
779 collect_manager_action_unref(action);
783 static gint collect_manager_process_entry(CollectManagerEntry *entry)
788 if (entry->empty) return FALSE;
790 cd = collection_new(entry->path);
791 success = collection_load_private(cd, entry->path, COLLECTION_LOAD_NONE);
793 collection_unref(cd);
798 static gint collect_manager_process_entry_list(void)
802 work = collection_manager_entry_list;
805 CollectManagerEntry *entry;
809 if (collect_manager_process_entry(entry)) return TRUE;
817 static gint collect_manager_process_cb(gpointer data)
819 if (collection_manager_action_list) collect_manager_refresh();
820 collect_manager_process_actions(COLLECT_MANAGER_ACTIONS_PER_IDLE);
821 if (collection_manager_action_list) return TRUE;
823 if (collect_manager_process_entry_list()) return TRUE;
825 if (debug) printf("collection manager is up to date\n");
829 static gint collect_manager_timer_cb(gpointer data)
831 if (debug) printf("collection manager timer expired\n");
833 g_idle_add_full(G_PRIORITY_LOW, collect_manager_process_cb, NULL, NULL);
835 collection_manager_timer_id = -1;
839 static void collect_manager_timer_push(gint stop)
841 if (collection_manager_timer_id != -1)
845 g_source_remove(collection_manager_timer_id);
846 collection_manager_timer_id = -1;
851 collection_manager_timer_id = g_timeout_add(COLLECT_MANAGER_FLUSH_DELAY,
852 collect_manager_timer_cb, NULL);
853 if (debug) printf("collection manager timer started\n");
857 static void collect_manager_add_action(CollectManagerAction *action)
861 /* we keep track of the list's tail to keep this a n(1) operation */
863 if (collection_manager_action_tail)
865 collection_manager_action_tail = g_list_append(collection_manager_action_tail, action);
866 collection_manager_action_tail = collection_manager_action_tail->next;
870 collection_manager_action_list = g_list_append(collection_manager_action_list, action);
871 collection_manager_action_tail = collection_manager_action_list;
874 collect_manager_timer_push(FALSE);
877 void collect_manager_moved(FileData *fd)
879 CollectManagerAction *action;
880 const gchar *oldpath = fd->change->source;
881 const gchar *newpath = fd->change->dest;
883 action = collect_manager_action_new(oldpath, newpath, COLLECTION_MANAGER_UPDATE);
884 collect_manager_add_action(action);
887 void collect_manager_add(FileData *fd, const gchar *collection)
889 CollectManagerAction *action;
892 if (!fd || !collection) return;
894 cw = collection_window_find_by_path(collection);
897 if (collection_list_find(cw->cd->list, fd->path) == NULL)
899 collection_add(cw->cd, fd, FALSE);
904 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_ADD);
905 collect_manager_add_action(action);
908 void collect_manager_remove(FileData *fd, const gchar *collection)
910 CollectManagerAction *action;
913 if (!fd || !collection) return;
915 cw = collection_window_find_by_path(collection);
918 while (collection_remove(cw->cd, fd));
922 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_REMOVE);
923 collect_manager_add_action(action);
926 void collect_manager_flush(void)
928 collect_manager_timer_push(TRUE);
930 if (debug) printf("collection manager flushing\n");
931 while (collect_manager_process_cb(NULL));