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"
17 #include "layout_util.h"
20 #include "ui_fileops.h"
24 #define GQVIEW_COLLECTION_MARKER "#Geeqie"
26 #define GQVIEW_COLLECTION_FAIL_MIN 300
27 #define GQVIEW_COLLECTION_FAIL_PERCENT 98
29 typedef struct _CollectManagerEntry CollectManagerEntry;
31 static void collection_load_thumb_step(CollectionData *cd);
32 static gint collection_save_private(CollectionData *cd, const gchar *path);
34 static CollectManagerEntry *collect_manager_get_entry(const gchar *path);
35 static void collect_manager_entry_reset(CollectManagerEntry *entry);
36 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr);
39 static gint scan_geometry(gchar *buffer, gint *x, gint *y, gint *w, gint *h)
43 if(sscanf(buffer, "%d %d %d %d", &nx, &ny, &nw, &nh) != 4) return FALSE;
53 static gint collection_load_private(CollectionData *cd, const gchar *path, gint append, gint flush)
58 gint official = FALSE;
62 gboolean changed = FALSE;
63 CollectManagerEntry *entry = NULL;
65 collection_load_stop(cd);
68 collect_manager_flush();
70 entry = collect_manager_get_entry(path);
74 collection_list_free(cd->list);
78 if (!path && !cd->path) return FALSE;
80 if (!path) path = cd->path;
83 pathl = path_from_utf8(path);
84 f = fopen(pathl, "r");
88 printf("Failed to open collection file: \"%s\"\n", path);
92 while (fgets(s_buf, sizeof(s_buf), f))
97 if (strncasecmp(s_buf, GQVIEW_COLLECTION_MARKER, strlen(GQVIEW_COLLECTION_MARKER)) == 0)
99 /* Looks like an official collection, allow unchecked input.
100 * All this does is allow adding files that may not exist,
101 * which is needed for the collection manager to work.
102 * Also unofficial files abort after too many invalid entries.
106 else if (strncmp(s_buf, "#geometry:", 10 ) == 0 &&
107 scan_geometry(s_buf + 10, &cd->window_x, &cd->window_y, &cd->window_w, &cd->window_h) )
109 cd->window_read = TRUE;
113 if (s_buf[0]=='\n') continue;
115 buf = quoted_value(s_buf, NULL);
121 changed |= collect_manager_process_action(entry, &buf);
123 valid = (buf[0] == '/' && collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE));
127 if (!valid && !official)
130 if (fail > GQVIEW_COLLECTION_FAIL_MIN &&
131 fail * 100 / total > GQVIEW_COLLECTION_FAIL_PERCENT)
133 printf("Too many invalid filenames in unoffical collection file, closing: %s\n", path);
146 while (collect_manager_process_action(entry, &buf))
148 collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE);
154 cd->list = collection_list_sort(cd->list, cd->sort_method);
156 if (!flush && changed && success)
157 collection_save_private(cd, path);
160 collect_manager_entry_reset(entry);
162 if (!append) cd->changed = FALSE;
167 gint collection_load(CollectionData *cd, const gchar *path, gint append)
169 if (collection_load_private(cd, path, append, TRUE))
171 layout_recent_add_path(cd->path);
178 static void collection_load_thumb_do(CollectionData *cd)
182 if (!cd->thumb_loader || !g_list_find(cd->list, cd->thumb_info)) return;
184 pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader, TRUE);
185 collection_info_set_thumb(cd->thumb_info, pixbuf);
186 g_object_unref(pixbuf);
188 if (cd->info_updated_func) cd->info_updated_func(cd, cd->thumb_info, cd->info_updated_data);
191 static void collection_load_thumb_error_cb(ThumbLoader *tl, gpointer data)
193 CollectionData *cd = data;
195 collection_load_thumb_do(cd);
196 collection_load_thumb_step(cd);
199 static void collection_load_thumb_done_cb(ThumbLoader *tl, gpointer data)
201 CollectionData *cd = data;
203 collection_load_thumb_do(cd);
204 collection_load_thumb_step(cd);
207 static void collection_load_thumb_step(CollectionData *cd)
214 collection_load_stop(cd);
221 /* find first unloaded thumb */
222 while (work && ci->pixbuf)
228 if (!ci || ci->pixbuf)
231 collection_load_stop(cd);
233 /* send a NULL CollectInfo to notify end */
234 if (cd->info_updated_func) cd->info_updated_func(cd, NULL, cd->info_updated_data);
239 /* setup loader and call it */
241 thumb_loader_free(cd->thumb_loader);
242 cd->thumb_loader = thumb_loader_new(thumb_max_width, thumb_max_height);
243 thumb_loader_set_callbacks(cd->thumb_loader,
244 collection_load_thumb_done_cb,
245 collection_load_thumb_error_cb,
250 if (!thumb_loader_start(cd->thumb_loader, ci->fd->path))
252 /* error, handle it, do next */
253 if (debug) printf("error loading thumb for %s\n", ci->fd->path);
254 collection_load_thumb_do(cd);
255 collection_load_thumb_step(cd);
259 void collection_load_thumb_idle(CollectionData *cd)
261 if (!cd->thumb_loader) collection_load_thumb_step(cd);
264 gint collection_load_begin(CollectionData *cd, const gchar *path, gint append)
266 if (!collection_load(cd, path, append)) return FALSE;
268 collection_load_thumb_idle(cd);
273 void collection_load_stop(CollectionData *cd)
275 if (!cd->thumb_loader) return;
277 thumb_loader_free(cd->thumb_loader);
278 cd->thumb_loader = NULL;
281 static gint collection_save_private(CollectionData *cd, const gchar *path)
289 if (!path && !cd->path) return FALSE;
296 tmp_path = unique_filename(path, ".tmp", "_", 3);
297 if (!tmp_path) return FALSE;
299 pathl = path_from_utf8(tmp_path);
300 save_mask = umask(0077);
301 f = fopen(pathl, "w");
307 /* file open failed */
308 printf("failed to open collection (write) \"%s\"\n", tmp_path);
313 fprintf(f, "%s collection\n", GQVIEW_COLLECTION_MARKER);
314 fprintf(f, "#created with Geeqie version %s\n", VERSION);
316 collection_update_geometry(cd);
319 fprintf(f, "#geometry: %d %d %d %d\n", cd->window_x, cd->window_y, cd->window_w, cd->window_h);
325 CollectInfo *ci = work->data;
326 if (fprintf(f, "\"%s\"\n", ci->fd->path) < 0)
329 printf("Error writing to %s\n", tmp_path);
330 unlink_file(tmp_path);
337 fprintf(f, "#end\n");
341 copy_file_attributes(path, tmp_path, TRUE, FALSE);
342 if (!rename_file(tmp_path, path))
344 printf("collection save unable to rename %s to %s\n", tmp_path, path);
345 unlink_file(tmp_path);
352 if (!cd->path || strcmp(path, cd->path) != 0)
354 gchar *buf = cd->path;
355 cd->path = g_strdup(path);
360 cd->name = g_strdup(filename_from_path(cd->path));
362 collection_path_changed(cd);
370 gint collection_save(CollectionData *cd, const gchar *path)
372 if (collection_save_private(cd, path))
374 layout_recent_add_path(cd->path);
381 gint collection_load_only_geometry(CollectionData *cd, const gchar *path)
387 if (!path && !cd->path) return FALSE;
389 if (!path) path = cd->path;
392 pathl = path_from_utf8(path);
393 f = fopen(pathl, "r");
395 if (!f) return FALSE;
397 while (fgets(s_buf, sizeof(s_buf), f))
400 strncmp(s_buf, "#geometry:", 10 ) == 0 &&
401 scan_geometry(s_buf + 10, &cd->window_x, &cd->window_y, &cd->window_w, &cd->window_h) )
403 cd->window_read = TRUE;
414 *-------------------------------------------------------------------
416 *-------------------------------------------------------------------
419 #define COLLECT_MANAGER_ACTIONS_PER_IDLE 1000
420 #define COLLECT_MANAGER_FLUSH_DELAY 10000
422 struct _CollectManagerEntry
426 GHashTable *oldpath_hash;
427 GHashTable *newpath_hash;
432 COLLECTION_MANAGER_UPDATE,
433 COLLECTION_MANAGER_ADD,
434 COLLECTION_MANAGER_REMOVE
435 } CollectManagerType;
437 typedef struct _CollectManagerAction CollectManagerAction;
438 struct _CollectManagerAction
443 CollectManagerType type;
449 static GList *collection_manager_entry_list = NULL;
450 static GList *collection_manager_action_list = NULL;
451 static GList *collection_manager_action_tail = NULL;
452 static gint collection_manager_timer_id = -1;
455 static CollectManagerAction *collect_manager_action_new(const gchar *oldpath, const gchar *newpath,
456 CollectManagerType type)
458 CollectManagerAction *action;
460 action = g_new0(CollectManagerAction, 1);
463 action->oldpath = g_strdup(oldpath);
464 action->newpath = g_strdup(newpath);
471 static void collect_manager_action_ref(CollectManagerAction *action)
476 static void collect_manager_action_unref(CollectManagerAction *action)
480 if (action->ref > 0) return;
482 g_free(action->oldpath);
483 g_free(action->newpath);
487 static void collect_manager_entry_free_data(CollectManagerEntry *entry)
491 work = entry->add_list;
494 CollectManagerAction *action;
499 collect_manager_action_unref(action);
501 g_list_free(entry->add_list);
502 g_hash_table_destroy(entry->oldpath_hash);
503 g_hash_table_destroy(entry->newpath_hash);
506 static void collect_manager_entry_init_data(CollectManagerEntry *entry)
508 entry->add_list = NULL;
509 entry->oldpath_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify) collect_manager_action_unref);
510 entry->newpath_hash = g_hash_table_new(g_str_hash, g_str_equal);
515 static CollectManagerEntry *collect_manager_entry_new(const gchar *path)
517 CollectManagerEntry *entry;
519 entry = g_new0(CollectManagerEntry, 1);
520 entry->path = g_strdup(path);
521 collect_manager_entry_init_data(entry);
523 collection_manager_entry_list = g_list_append(collection_manager_entry_list, entry);
529 static void collect_manager_entry_free(CollectManagerEntry *entry)
533 collection_manager_entry_list = g_list_remove(collection_manager_entry_list, entry);
535 collect_manager_entry_free_data(entry);
541 static void collect_manager_entry_reset(CollectManagerEntry *entry)
543 collect_manager_entry_free_data(entry);
544 collect_manager_entry_init_data(entry);
547 static CollectManagerEntry *collect_manager_get_entry(const gchar *path)
551 work = collection_manager_entry_list;
554 CollectManagerEntry *entry;
558 if (strcmp(entry->path, path) == 0)
567 static void collect_manager_entry_add_action(CollectManagerEntry *entry, CollectManagerAction *action)
570 CollectManagerAction *orig_action;
572 entry->empty = FALSE;
574 if (action->oldpath == NULL)
577 if (action->newpath == NULL)
582 orig_action = g_hash_table_lookup(entry->newpath_hash, action->newpath);
585 /* target already exists */
586 printf("collection manager failed to add another action for target %s in collection %s\n",
587 action->newpath, entry->path);
590 entry->add_list = g_list_append(entry->add_list, action);
591 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
592 collect_manager_action_ref(action);
596 orig_action = g_hash_table_lookup(entry->newpath_hash, action->oldpath);
599 /* new action with the same file */
600 CollectManagerAction *new_action = collect_manager_action_new(orig_action->oldpath, action->newpath, action->type);
602 if (new_action->oldpath)
604 g_hash_table_steal(entry->oldpath_hash, orig_action->oldpath);
605 g_hash_table_insert(entry->oldpath_hash, new_action->oldpath, new_action);
609 GList *work = g_list_find(entry->add_list, orig_action);
610 work->data = new_action;
613 g_hash_table_steal(entry->newpath_hash, orig_action->newpath);
614 if (new_action->newpath)
616 g_hash_table_insert(entry->newpath_hash, new_action->newpath, new_action);
618 collect_manager_action_unref(orig_action);
623 orig_action = g_hash_table_lookup(entry->oldpath_hash, action->oldpath);
626 /* another action for the same source, ignore */
627 printf("collection manager failed to add another action for source %s in collection %s\n",
628 action->oldpath, entry->path);
632 g_hash_table_insert(entry->oldpath_hash, action->oldpath, action);
635 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
637 collect_manager_action_ref(action);
640 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr)
642 gchar *path = *path_ptr;
643 CollectManagerAction *action;
650 action = entry->add_list->data;
651 g_assert(action->oldpath == NULL);
652 entry->add_list = g_list_remove(entry->add_list, action);
653 path = g_strdup(action->newpath);
654 g_hash_table_remove(entry->newpath_hash, path);
655 collect_manager_action_unref(action);
658 return (path != NULL);
661 action = g_hash_table_lookup(entry->oldpath_hash, path);
666 path = g_strdup(action->newpath);
671 return FALSE; /* no change */
674 static void collect_manager_refresh(void)
680 base = g_strconcat(homedir(), "/", GQVIEW_RC_DIR_COLLECTIONS, NULL);
681 path_list(base, &list, NULL);
684 work = collection_manager_entry_list;
687 CollectManagerEntry *entry;
694 while (list_step && entry)
698 path = list_step->data;
699 list_step = list_step->next;
701 if (strcmp(path, entry->path) == 0)
703 list = g_list_remove(list, path);
710 collect_manager_entry_free(entry);
723 collect_manager_entry_new(path);
730 static void collect_manager_process_actions(gint max)
732 if (debug && collection_manager_action_list)
734 printf("collection manager processing actions\n");
737 while (collection_manager_action_list != NULL && max > 0)
739 CollectManagerAction *action;
742 action = collection_manager_action_list->data;
743 work = collection_manager_entry_list;
746 CollectManagerEntry *entry;
751 if (action->type == COLLECTION_MANAGER_UPDATE)
753 collect_manager_entry_add_action(entry, action);
755 else if (action->oldpath && action->newpath &&
756 strcmp(action->newpath, entry->path) == 0)
758 /* convert action to standard add format */
759 g_free(action->newpath);
760 if (action->type == COLLECTION_MANAGER_ADD)
762 action->newpath = action->oldpath;
763 action->oldpath = NULL;
765 else if (action->type == COLLECTION_MANAGER_REMOVE)
767 action->newpath = NULL;
769 collect_manager_entry_add_action(entry, action);
775 if (action->type != COLLECTION_MANAGER_UPDATE &&
776 action->oldpath && action->newpath)
778 printf("collection manager failed to %s %s for collection %s\n",
779 (action->type == COLLECTION_MANAGER_ADD) ? "add" : "remove",
780 action->oldpath, action->newpath);
783 if (collection_manager_action_tail == collection_manager_action_list)
785 collection_manager_action_tail = NULL;
787 collection_manager_action_list = g_list_remove(collection_manager_action_list, action);
788 collect_manager_action_unref(action);
792 static gint collect_manager_process_entry(CollectManagerEntry *entry)
797 if (entry->empty) return FALSE;
799 cd = collection_new(entry->path);
800 success = collection_load_private(cd, entry->path, FALSE, FALSE);
802 collection_unref(cd);
807 static gint collect_manager_process_entry_list(void)
811 work = collection_manager_entry_list;
814 CollectManagerEntry *entry;
818 if (collect_manager_process_entry(entry)) return TRUE;
826 static gint collect_manager_process_cb(gpointer data)
828 if (collection_manager_action_list) collect_manager_refresh();
829 collect_manager_process_actions(COLLECT_MANAGER_ACTIONS_PER_IDLE);
830 if (collection_manager_action_list) return TRUE;
832 if (collect_manager_process_entry_list()) return TRUE;
834 if (debug) printf("collection manager is up to date\n");
838 static gint collect_manager_timer_cb(gpointer data)
840 if (debug) printf("collection manager timer expired\n");
842 g_idle_add_full(G_PRIORITY_LOW, collect_manager_process_cb, NULL, NULL);
844 collection_manager_timer_id = -1;
848 static void collect_manager_timer_push(gint stop)
850 if (collection_manager_timer_id != -1)
854 g_source_remove(collection_manager_timer_id);
855 collection_manager_timer_id = -1;
860 collection_manager_timer_id = g_timeout_add(COLLECT_MANAGER_FLUSH_DELAY,
861 collect_manager_timer_cb, NULL);
862 if (debug) printf("collection manager timer started\n");
866 static void collect_manager_add_action(CollectManagerAction *action)
870 /* we keep track of the list's tail to keep this a n(1) operation */
872 if (collection_manager_action_tail)
874 collection_manager_action_tail = g_list_append(collection_manager_action_tail, action);
875 collection_manager_action_tail = collection_manager_action_tail->next;
879 collection_manager_action_list = g_list_append(collection_manager_action_list, action);
880 collection_manager_action_tail = collection_manager_action_list;
883 collect_manager_timer_push(FALSE);
886 void collect_manager_moved(FileData *fd)
888 CollectManagerAction *action;
889 const gchar *oldpath = fd->change->source;
890 const gchar *newpath = fd->change->dest;
892 action = collect_manager_action_new(oldpath, newpath, COLLECTION_MANAGER_UPDATE);
893 collect_manager_add_action(action);
896 void collect_manager_add(FileData *fd, const gchar *collection)
898 CollectManagerAction *action;
901 if (!fd || !collection) return;
903 cw = collection_window_find_by_path(collection);
906 if (collection_list_find(cw->cd->list, fd->path) == NULL)
908 collection_add(cw->cd, fd, FALSE);
913 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_ADD);
914 collect_manager_add_action(action);
917 void collect_manager_remove(FileData *fd, const gchar *collection)
919 CollectManagerAction *action;
922 if (!fd || !collection) return;
924 cw = collection_window_find_by_path(collection);
927 while (collection_remove(cw->cd, fd));
931 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_REMOVE);
932 collect_manager_add_action(action);
935 void collect_manager_flush(void)
937 collect_manager_timer_push(TRUE);
939 if (debug) printf("collection manager flushing\n");
940 while (collect_manager_process_cb(NULL));