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
30 typedef struct _CollectManagerEntry CollectManagerEntry;
32 static void collection_load_thumb_step(CollectionData *cd);
33 static gint collection_save_private(CollectionData *cd, const gchar *path);
35 static CollectManagerEntry *collect_manager_get_entry(const gchar *path);
36 static void collect_manager_entry_reset(CollectManagerEntry *entry);
37 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr);
40 static gint scan_geometry(gchar *buffer, gint *x, gint *y, gint *w, gint *h)
44 if(sscanf(buffer, "%d %d %d %d", &nx, &ny, &nw, &nh) != 4) return FALSE;
54 static gint collection_load_private(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
59 gint official = FALSE;
63 gboolean changed = FALSE;
64 CollectManagerEntry *entry = NULL;
65 guint flush = flags & COLLECTION_LOAD_FLUSH;
66 guint append = flags & COLLECTION_LOAD_APPEND;
68 collection_load_stop(cd);
71 collect_manager_flush();
73 entry = collect_manager_get_entry(path);
77 collection_list_free(cd->list);
81 if (!path && !cd->path) return FALSE;
83 if (!path) path = cd->path;
86 pathl = path_from_utf8(path);
87 f = fopen(pathl, "r");
91 printf("Failed to open collection file: \"%s\"\n", path);
95 while (fgets(s_buf, sizeof(s_buf), f))
100 if (strncasecmp(s_buf, GQ_COLLECTION_MARKER, strlen(GQ_COLLECTION_MARKER)) == 0)
102 /* Looks like an official collection, allow unchecked input.
103 * All this does is allow adding files that may not exist,
104 * which is needed for the collection manager to work.
105 * Also unofficial files abort after too many invalid entries.
109 else if (strncmp(s_buf, "#geometry:", 10 ) == 0 &&
110 scan_geometry(s_buf + 10, &cd->window_x, &cd->window_y, &cd->window_w, &cd->window_h) )
112 cd->window_read = TRUE;
116 if (s_buf[0]=='\n') continue;
118 buf = quoted_value(s_buf, NULL);
124 changed |= collect_manager_process_action(entry, &buf);
126 valid = (buf[0] == '/' && collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE));
130 if (!valid && !official)
133 if (fail > GQ_COLLECTION_FAIL_MIN &&
134 fail * 100 / total > GQ_COLLECTION_FAIL_PERCENT)
136 printf("Too many invalid filenames in unoffical collection file, closing: %s\n", path);
149 while (collect_manager_process_action(entry, &buf))
151 collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE);
157 cd->list = collection_list_sort(cd->list, cd->sort_method);
159 if (!flush && changed && success)
160 collection_save_private(cd, path);
163 collect_manager_entry_reset(entry);
165 if (!append) cd->changed = FALSE;
170 gint collection_load(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
172 if (collection_load_private(cd, path, flags | COLLECTION_LOAD_FLUSH))
174 layout_recent_add_path(cd->path);
181 static void collection_load_thumb_do(CollectionData *cd)
185 if (!cd->thumb_loader || !g_list_find(cd->list, cd->thumb_info)) return;
187 pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader, TRUE);
188 collection_info_set_thumb(cd->thumb_info, pixbuf);
189 g_object_unref(pixbuf);
191 if (cd->info_updated_func) cd->info_updated_func(cd, cd->thumb_info, cd->info_updated_data);
194 static void collection_load_thumb_error_cb(ThumbLoader *tl, gpointer data)
196 CollectionData *cd = data;
198 collection_load_thumb_do(cd);
199 collection_load_thumb_step(cd);
202 static void collection_load_thumb_done_cb(ThumbLoader *tl, gpointer data)
204 CollectionData *cd = data;
206 collection_load_thumb_do(cd);
207 collection_load_thumb_step(cd);
210 static void collection_load_thumb_step(CollectionData *cd)
217 collection_load_stop(cd);
224 /* find first unloaded thumb */
225 while (work && ci->pixbuf)
231 if (!ci || ci->pixbuf)
234 collection_load_stop(cd);
236 /* send a NULL CollectInfo to notify end */
237 if (cd->info_updated_func) cd->info_updated_func(cd, NULL, cd->info_updated_data);
242 /* setup loader and call it */
244 thumb_loader_free(cd->thumb_loader);
245 cd->thumb_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
246 thumb_loader_set_callbacks(cd->thumb_loader,
247 collection_load_thumb_done_cb,
248 collection_load_thumb_error_cb,
253 if (!thumb_loader_start(cd->thumb_loader, ci->fd->path))
255 /* error, handle it, do next */
256 if (debug) printf("error loading thumb for %s\n", ci->fd->path);
257 collection_load_thumb_do(cd);
258 collection_load_thumb_step(cd);
262 void collection_load_thumb_idle(CollectionData *cd)
264 if (!cd->thumb_loader) collection_load_thumb_step(cd);
267 gint collection_load_begin(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
269 if (!collection_load(cd, path, flags)) return FALSE;
271 collection_load_thumb_idle(cd);
276 void collection_load_stop(CollectionData *cd)
278 if (!cd->thumb_loader) return;
280 thumb_loader_free(cd->thumb_loader);
281 cd->thumb_loader = NULL;
284 static gint collection_save_private(CollectionData *cd, const gchar *path)
290 if (!path && !cd->path) return FALSE;
298 pathl = path_from_utf8(path);
299 ssi = secure_open(pathl);
305 buf = g_strdup_printf(_("failed to open collection (write) \"%s\"\n"), path);
311 secure_fprintf(ssi, "%s collection\n", GQ_COLLECTION_MARKER);
312 secure_fprintf(ssi, "#created with %s version %s\n", GQ_APPNAME, VERSION);
314 collection_update_geometry(cd);
317 secure_fprintf(ssi, "#geometry: %d %d %d %d\n", cd->window_x, cd->window_y, cd->window_w, cd->window_h);
321 while (work && secsave_errno == SS_ERR_NONE)
323 CollectInfo *ci = work->data;
324 secure_fprintf(ssi, "\"%s\"\n", ci->fd->path);
328 secure_fprintf(ssi, "#end\n");
330 if (secure_close(ssi))
334 buf = g_strdup_printf(_("error saving collection file: %s\nerror: %s\n"), path,
335 secsave_strerror(secsave_errno));
341 if (!cd->path || strcmp(path, cd->path) != 0)
343 gchar *buf = cd->path;
344 cd->path = g_strdup(path);
349 cd->name = g_strdup(filename_from_path(cd->path));
351 collection_path_changed(cd);
359 gint collection_save(CollectionData *cd, const gchar *path)
361 if (collection_save_private(cd, path))
363 layout_recent_add_path(cd->path);
370 gint collection_load_only_geometry(CollectionData *cd, const gchar *path)
376 if (!path && !cd->path) return FALSE;
378 if (!path) path = cd->path;
381 pathl = path_from_utf8(path);
382 f = fopen(pathl, "r");
384 if (!f) return FALSE;
386 while (fgets(s_buf, sizeof(s_buf), f))
389 strncmp(s_buf, "#geometry:", 10 ) == 0 &&
390 scan_geometry(s_buf + 10, &cd->window_x, &cd->window_y, &cd->window_w, &cd->window_h) )
392 cd->window_read = TRUE;
403 *-------------------------------------------------------------------
405 *-------------------------------------------------------------------
408 #define COLLECT_MANAGER_ACTIONS_PER_IDLE 1000
409 #define COLLECT_MANAGER_FLUSH_DELAY 10000
411 struct _CollectManagerEntry
415 GHashTable *oldpath_hash;
416 GHashTable *newpath_hash;
421 COLLECTION_MANAGER_UPDATE,
422 COLLECTION_MANAGER_ADD,
423 COLLECTION_MANAGER_REMOVE
424 } CollectManagerType;
426 typedef struct _CollectManagerAction CollectManagerAction;
427 struct _CollectManagerAction
432 CollectManagerType type;
438 static GList *collection_manager_entry_list = NULL;
439 static GList *collection_manager_action_list = NULL;
440 static GList *collection_manager_action_tail = NULL;
441 static gint collection_manager_timer_id = -1;
444 static CollectManagerAction *collect_manager_action_new(const gchar *oldpath, const gchar *newpath,
445 CollectManagerType type)
447 CollectManagerAction *action;
449 action = g_new0(CollectManagerAction, 1);
452 action->oldpath = g_strdup(oldpath);
453 action->newpath = g_strdup(newpath);
460 static void collect_manager_action_ref(CollectManagerAction *action)
465 static void collect_manager_action_unref(CollectManagerAction *action)
469 if (action->ref > 0) return;
471 g_free(action->oldpath);
472 g_free(action->newpath);
476 static void collect_manager_entry_free_data(CollectManagerEntry *entry)
480 work = entry->add_list;
483 CollectManagerAction *action;
488 collect_manager_action_unref(action);
490 g_list_free(entry->add_list);
491 g_hash_table_destroy(entry->oldpath_hash);
492 g_hash_table_destroy(entry->newpath_hash);
495 static void collect_manager_entry_init_data(CollectManagerEntry *entry)
497 entry->add_list = NULL;
498 entry->oldpath_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify) collect_manager_action_unref);
499 entry->newpath_hash = g_hash_table_new(g_str_hash, g_str_equal);
504 static CollectManagerEntry *collect_manager_entry_new(const gchar *path)
506 CollectManagerEntry *entry;
508 entry = g_new0(CollectManagerEntry, 1);
509 entry->path = g_strdup(path);
510 collect_manager_entry_init_data(entry);
512 collection_manager_entry_list = g_list_append(collection_manager_entry_list, entry);
518 static void collect_manager_entry_free(CollectManagerEntry *entry)
520 collection_manager_entry_list = g_list_remove(collection_manager_entry_list, entry);
522 collect_manager_entry_free_data(entry);
528 static void collect_manager_entry_reset(CollectManagerEntry *entry)
530 collect_manager_entry_free_data(entry);
531 collect_manager_entry_init_data(entry);
534 static CollectManagerEntry *collect_manager_get_entry(const gchar *path)
538 work = collection_manager_entry_list;
541 CollectManagerEntry *entry;
545 if (strcmp(entry->path, path) == 0)
554 static void collect_manager_entry_add_action(CollectManagerEntry *entry, CollectManagerAction *action)
557 CollectManagerAction *orig_action;
559 entry->empty = FALSE;
561 if (action->oldpath == NULL)
564 if (action->newpath == NULL)
569 orig_action = g_hash_table_lookup(entry->newpath_hash, action->newpath);
572 /* target already exists */
573 printf("collection manager failed to add another action for target %s in collection %s\n",
574 action->newpath, entry->path);
577 entry->add_list = g_list_append(entry->add_list, action);
578 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
579 collect_manager_action_ref(action);
583 orig_action = g_hash_table_lookup(entry->newpath_hash, action->oldpath);
586 /* new action with the same file */
587 CollectManagerAction *new_action = collect_manager_action_new(orig_action->oldpath, action->newpath, action->type);
589 if (new_action->oldpath)
591 g_hash_table_steal(entry->oldpath_hash, orig_action->oldpath);
592 g_hash_table_insert(entry->oldpath_hash, new_action->oldpath, new_action);
596 GList *work = g_list_find(entry->add_list, orig_action);
597 work->data = new_action;
600 g_hash_table_steal(entry->newpath_hash, orig_action->newpath);
601 if (new_action->newpath)
603 g_hash_table_insert(entry->newpath_hash, new_action->newpath, new_action);
605 collect_manager_action_unref(orig_action);
610 orig_action = g_hash_table_lookup(entry->oldpath_hash, action->oldpath);
613 /* another action for the same source, ignore */
614 printf("collection manager failed to add another action for source %s in collection %s\n",
615 action->oldpath, entry->path);
619 g_hash_table_insert(entry->oldpath_hash, action->oldpath, action);
622 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
624 collect_manager_action_ref(action);
627 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr)
629 gchar *path = *path_ptr;
630 CollectManagerAction *action;
637 action = entry->add_list->data;
638 g_assert(action->oldpath == NULL);
639 entry->add_list = g_list_remove(entry->add_list, action);
640 path = g_strdup(action->newpath);
641 g_hash_table_remove(entry->newpath_hash, path);
642 collect_manager_action_unref(action);
645 return (path != NULL);
648 action = g_hash_table_lookup(entry->oldpath_hash, path);
653 path = g_strdup(action->newpath);
658 return FALSE; /* no change */
661 static void collect_manager_refresh(void)
667 base = g_strconcat(homedir(), "/", GQ_RC_DIR_COLLECTIONS, NULL);
668 path_list(base, &list, NULL);
671 work = collection_manager_entry_list;
674 CollectManagerEntry *entry;
681 while (list_step && entry)
685 path = list_step->data;
686 list_step = list_step->next;
688 if (strcmp(path, entry->path) == 0)
690 list = g_list_remove(list, path);
697 collect_manager_entry_free(entry);
710 collect_manager_entry_new(path);
717 static void collect_manager_process_actions(gint max)
719 if (debug && collection_manager_action_list)
721 printf("collection manager processing actions\n");
724 while (collection_manager_action_list != NULL && max > 0)
726 CollectManagerAction *action;
729 action = collection_manager_action_list->data;
730 work = collection_manager_entry_list;
733 CollectManagerEntry *entry;
738 if (action->type == COLLECTION_MANAGER_UPDATE)
740 collect_manager_entry_add_action(entry, action);
742 else if (action->oldpath && action->newpath &&
743 strcmp(action->newpath, entry->path) == 0)
745 /* convert action to standard add format */
746 g_free(action->newpath);
747 if (action->type == COLLECTION_MANAGER_ADD)
749 action->newpath = action->oldpath;
750 action->oldpath = NULL;
752 else if (action->type == COLLECTION_MANAGER_REMOVE)
754 action->newpath = NULL;
756 collect_manager_entry_add_action(entry, action);
762 if (action->type != COLLECTION_MANAGER_UPDATE &&
763 action->oldpath && action->newpath)
765 printf("collection manager failed to %s %s for collection %s\n",
766 (action->type == COLLECTION_MANAGER_ADD) ? "add" : "remove",
767 action->oldpath, action->newpath);
770 if (collection_manager_action_tail == collection_manager_action_list)
772 collection_manager_action_tail = NULL;
774 collection_manager_action_list = g_list_remove(collection_manager_action_list, action);
775 collect_manager_action_unref(action);
779 static gint collect_manager_process_entry(CollectManagerEntry *entry)
784 if (entry->empty) return FALSE;
786 cd = collection_new(entry->path);
787 success = collection_load_private(cd, entry->path, COLLECTION_LOAD_NONE);
789 collection_unref(cd);
794 static gint collect_manager_process_entry_list(void)
798 work = collection_manager_entry_list;
801 CollectManagerEntry *entry;
805 if (collect_manager_process_entry(entry)) return TRUE;
813 static gint collect_manager_process_cb(gpointer data)
815 if (collection_manager_action_list) collect_manager_refresh();
816 collect_manager_process_actions(COLLECT_MANAGER_ACTIONS_PER_IDLE);
817 if (collection_manager_action_list) return TRUE;
819 if (collect_manager_process_entry_list()) return TRUE;
821 if (debug) printf("collection manager is up to date\n");
825 static gint collect_manager_timer_cb(gpointer data)
827 if (debug) printf("collection manager timer expired\n");
829 g_idle_add_full(G_PRIORITY_LOW, collect_manager_process_cb, NULL, NULL);
831 collection_manager_timer_id = -1;
835 static void collect_manager_timer_push(gint stop)
837 if (collection_manager_timer_id != -1)
841 g_source_remove(collection_manager_timer_id);
842 collection_manager_timer_id = -1;
847 collection_manager_timer_id = g_timeout_add(COLLECT_MANAGER_FLUSH_DELAY,
848 collect_manager_timer_cb, NULL);
849 if (debug) printf("collection manager timer started\n");
853 static void collect_manager_add_action(CollectManagerAction *action)
857 /* we keep track of the list's tail to keep this a n(1) operation */
859 if (collection_manager_action_tail)
861 collection_manager_action_tail = g_list_append(collection_manager_action_tail, action);
862 collection_manager_action_tail = collection_manager_action_tail->next;
866 collection_manager_action_list = g_list_append(collection_manager_action_list, action);
867 collection_manager_action_tail = collection_manager_action_list;
870 collect_manager_timer_push(FALSE);
873 void collect_manager_moved(FileData *fd)
875 CollectManagerAction *action;
876 const gchar *oldpath = fd->change->source;
877 const gchar *newpath = fd->change->dest;
879 action = collect_manager_action_new(oldpath, newpath, COLLECTION_MANAGER_UPDATE);
880 collect_manager_add_action(action);
883 void collect_manager_add(FileData *fd, const gchar *collection)
885 CollectManagerAction *action;
888 if (!fd || !collection) return;
890 cw = collection_window_find_by_path(collection);
893 if (collection_list_find(cw->cd->list, fd->path) == NULL)
895 collection_add(cw->cd, fd, FALSE);
900 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_ADD);
901 collect_manager_add_action(action);
904 void collect_manager_remove(FileData *fd, const gchar *collection)
906 CollectManagerAction *action;
909 if (!fd || !collection) return;
911 cw = collection_window_find_by_path(collection);
914 while (collection_remove(cw->cd, fd));
918 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_REMOVE);
919 collect_manager_add_action(action);
922 void collect_manager_flush(void)
924 collect_manager_timer_push(TRUE);
926 if (debug) printf("collection manager flushing\n");
927 while (collect_manager_process_cb(NULL));