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;
62 gint has_official_header = FALSE;
63 gint has_geometry_header = FALSE;
64 gint has_gqview_header = FALSE;
65 gint need_header = TRUE;
68 gboolean changed = FALSE;
69 CollectManagerEntry *entry = NULL;
70 guint flush = flags & COLLECTION_LOAD_FLUSH;
71 guint append = flags & COLLECTION_LOAD_APPEND;
72 guint only_geometry = flags & COLLECTION_LOAD_GEOMETRY;
76 collection_load_stop(cd);
79 collect_manager_flush();
81 entry = collect_manager_get_entry(path);
85 collection_list_free(cd->list);
90 if (!path && !cd->path) return FALSE;
92 if (!path) path = cd->path;
94 if (debug) printf("collection load: append=%d flush=%d only_geometry=%d path=%s\n",
95 append, flush, only_geometry, path);
98 pathl = path_from_utf8(path);
99 f = fopen(pathl, "r");
103 printf("Failed to open collection file: \"%s\"\n", path);
107 while (fgets(s_buf, sizeof(s_buf), f))
112 /* Skip whitespaces and empty lines */
113 while (*p && g_ascii_isspace(*p)) p++;
114 if (*p == '\n' || *p == '\r') continue;
119 if (!need_header) continue;
120 if (strncasecmp(p, GQ_COLLECTION_MARKER, strlen(GQ_COLLECTION_MARKER)) == 0)
122 /* Looks like an official collection, allow unchecked input.
123 * All this does is allow adding files that may not exist,
124 * which is needed for the collection manager to work.
125 * Also unofficial files abort after too many invalid entries.
127 has_official_header = TRUE;
128 limit_failures = FALSE;
130 else if (strncmp(p, "#geometry:", 10 ) == 0 &&
131 scan_geometry(p + 10, &cd->window_x, &cd->window_y, &cd->window_w, &cd->window_h))
133 has_geometry_header = TRUE;
134 cd->window_read = TRUE;
135 if (only_geometry) break;
137 else if (strncasecmp(p, "#GQview collection", strlen("#GQview collection")) == 0)
139 /* As 2008/04/15 there is no difference between our collection file format
140 * and GQview 2.1.5 collection file format so ignore failures as well. */
141 has_gqview_header = TRUE;
142 limit_failures = FALSE;
144 need_header = (!has_official_header && !has_gqview_header) || !has_geometry_header;
149 buf = quoted_value(p, NULL);
155 changed |= collect_manager_process_action(entry, &buf);
157 valid = (buf[0] == '/' && collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE));
158 if (debug && !valid) printf("collection invalid file: %s\n", buf);
162 if (!valid && limit_failures)
165 if (fail > GQ_COLLECTION_FAIL_MIN &&
166 fail * 100 / total > GQ_COLLECTION_FAIL_PERCENT)
168 printf("%d invalid filenames in unoffical collection file, closing: %s\n", fail, path);
176 if (debug) printf("collection files: total = %d fail = %d official=%d gqview=%d geometry=%d\n",
177 total, fail, has_official_header, has_gqview_header, has_geometry_header);
180 if (only_geometry) return has_geometry_header;
185 while (collect_manager_process_action(entry, &buf))
187 collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE);
193 cd->list = collection_list_sort(cd->list, cd->sort_method);
195 if (!flush && changed && success)
196 collection_save_private(cd, path);
199 collect_manager_entry_reset(entry);
201 if (!append) cd->changed = FALSE;
206 gint collection_load(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
208 if (collection_load_private(cd, path, flags | COLLECTION_LOAD_FLUSH))
210 layout_recent_add_path(cd->path);
217 static void collection_load_thumb_do(CollectionData *cd)
221 if (!cd->thumb_loader || !g_list_find(cd->list, cd->thumb_info)) return;
223 pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader, TRUE);
224 collection_info_set_thumb(cd->thumb_info, pixbuf);
225 g_object_unref(pixbuf);
227 if (cd->info_updated_func) cd->info_updated_func(cd, cd->thumb_info, cd->info_updated_data);
230 static void collection_load_thumb_error_cb(ThumbLoader *tl, gpointer data)
232 CollectionData *cd = data;
234 collection_load_thumb_do(cd);
235 collection_load_thumb_step(cd);
238 static void collection_load_thumb_done_cb(ThumbLoader *tl, gpointer data)
240 CollectionData *cd = data;
242 collection_load_thumb_do(cd);
243 collection_load_thumb_step(cd);
246 static void collection_load_thumb_step(CollectionData *cd)
253 collection_load_stop(cd);
260 /* find first unloaded thumb */
261 while (work && ci->pixbuf)
267 if (!ci || ci->pixbuf)
270 collection_load_stop(cd);
272 /* send a NULL CollectInfo to notify end */
273 if (cd->info_updated_func) cd->info_updated_func(cd, NULL, cd->info_updated_data);
278 /* setup loader and call it */
280 thumb_loader_free(cd->thumb_loader);
281 cd->thumb_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
282 thumb_loader_set_callbacks(cd->thumb_loader,
283 collection_load_thumb_done_cb,
284 collection_load_thumb_error_cb,
289 if (!thumb_loader_start(cd->thumb_loader, ci->fd->path))
291 /* error, handle it, do next */
292 if (debug) printf("error loading thumb for %s\n", ci->fd->path);
293 collection_load_thumb_do(cd);
294 collection_load_thumb_step(cd);
298 void collection_load_thumb_idle(CollectionData *cd)
300 if (!cd->thumb_loader) collection_load_thumb_step(cd);
303 gint collection_load_begin(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
305 if (!collection_load(cd, path, flags)) return FALSE;
307 collection_load_thumb_idle(cd);
312 void collection_load_stop(CollectionData *cd)
314 if (!cd->thumb_loader) return;
316 thumb_loader_free(cd->thumb_loader);
317 cd->thumb_loader = NULL;
320 static gint collection_save_private(CollectionData *cd, const gchar *path)
326 if (!path && !cd->path) return FALSE;
334 pathl = path_from_utf8(path);
335 ssi = secure_open(pathl);
341 buf = g_strdup_printf(_("failed to open collection (write) \"%s\"\n"), path);
347 secure_fprintf(ssi, "%s collection\n", GQ_COLLECTION_MARKER);
348 secure_fprintf(ssi, "#created with %s version %s\n", GQ_APPNAME, VERSION);
350 collection_update_geometry(cd);
353 secure_fprintf(ssi, "#geometry: %d %d %d %d\n", cd->window_x, cd->window_y, cd->window_w, cd->window_h);
357 while (work && secsave_errno == SS_ERR_NONE)
359 CollectInfo *ci = work->data;
360 secure_fprintf(ssi, "\"%s\"\n", ci->fd->path);
364 secure_fprintf(ssi, "#end\n");
366 if (secure_close(ssi))
370 buf = g_strdup_printf(_("error saving collection file: %s\nerror: %s\n"), path,
371 secsave_strerror(secsave_errno));
377 if (!cd->path || strcmp(path, cd->path) != 0)
379 gchar *buf = cd->path;
380 cd->path = g_strdup(path);
385 cd->name = g_strdup(filename_from_path(cd->path));
387 collection_path_changed(cd);
395 gint collection_save(CollectionData *cd, const gchar *path)
397 if (collection_save_private(cd, path))
399 layout_recent_add_path(cd->path);
406 gint collection_load_only_geometry(CollectionData *cd, const gchar *path)
408 return collection_load(cd, path, COLLECTION_LOAD_GEOMETRY);
413 *-------------------------------------------------------------------
415 *-------------------------------------------------------------------
418 #define COLLECT_MANAGER_ACTIONS_PER_IDLE 1000
419 #define COLLECT_MANAGER_FLUSH_DELAY 10000
421 struct _CollectManagerEntry
425 GHashTable *oldpath_hash;
426 GHashTable *newpath_hash;
431 COLLECTION_MANAGER_UPDATE,
432 COLLECTION_MANAGER_ADD,
433 COLLECTION_MANAGER_REMOVE
434 } CollectManagerType;
436 typedef struct _CollectManagerAction CollectManagerAction;
437 struct _CollectManagerAction
442 CollectManagerType type;
448 static GList *collection_manager_entry_list = NULL;
449 static GList *collection_manager_action_list = NULL;
450 static GList *collection_manager_action_tail = NULL;
451 static gint collection_manager_timer_id = -1;
454 static CollectManagerAction *collect_manager_action_new(const gchar *oldpath, const gchar *newpath,
455 CollectManagerType type)
457 CollectManagerAction *action;
459 action = g_new0(CollectManagerAction, 1);
462 action->oldpath = g_strdup(oldpath);
463 action->newpath = g_strdup(newpath);
470 static void collect_manager_action_ref(CollectManagerAction *action)
475 static void collect_manager_action_unref(CollectManagerAction *action)
479 if (action->ref > 0) return;
481 g_free(action->oldpath);
482 g_free(action->newpath);
486 static void collect_manager_entry_free_data(CollectManagerEntry *entry)
490 work = entry->add_list;
493 CollectManagerAction *action;
498 collect_manager_action_unref(action);
500 g_list_free(entry->add_list);
501 g_hash_table_destroy(entry->oldpath_hash);
502 g_hash_table_destroy(entry->newpath_hash);
505 static void collect_manager_entry_init_data(CollectManagerEntry *entry)
507 entry->add_list = NULL;
508 entry->oldpath_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify) collect_manager_action_unref);
509 entry->newpath_hash = g_hash_table_new(g_str_hash, g_str_equal);
514 static CollectManagerEntry *collect_manager_entry_new(const gchar *path)
516 CollectManagerEntry *entry;
518 entry = g_new0(CollectManagerEntry, 1);
519 entry->path = g_strdup(path);
520 collect_manager_entry_init_data(entry);
522 collection_manager_entry_list = g_list_append(collection_manager_entry_list, entry);
528 static void collect_manager_entry_free(CollectManagerEntry *entry)
530 collection_manager_entry_list = g_list_remove(collection_manager_entry_list, entry);
532 collect_manager_entry_free_data(entry);
538 static void collect_manager_entry_reset(CollectManagerEntry *entry)
540 collect_manager_entry_free_data(entry);
541 collect_manager_entry_init_data(entry);
544 static CollectManagerEntry *collect_manager_get_entry(const gchar *path)
548 work = collection_manager_entry_list;
551 CollectManagerEntry *entry;
555 if (strcmp(entry->path, path) == 0)
564 static void collect_manager_entry_add_action(CollectManagerEntry *entry, CollectManagerAction *action)
567 CollectManagerAction *orig_action;
569 entry->empty = FALSE;
571 if (action->oldpath == NULL)
574 if (action->newpath == NULL)
579 orig_action = g_hash_table_lookup(entry->newpath_hash, action->newpath);
582 /* target already exists */
583 printf("collection manager failed to add another action for target %s in collection %s\n",
584 action->newpath, entry->path);
587 entry->add_list = g_list_append(entry->add_list, action);
588 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
589 collect_manager_action_ref(action);
593 orig_action = g_hash_table_lookup(entry->newpath_hash, action->oldpath);
596 /* new action with the same file */
597 CollectManagerAction *new_action = collect_manager_action_new(orig_action->oldpath, action->newpath, action->type);
599 if (new_action->oldpath)
601 g_hash_table_steal(entry->oldpath_hash, orig_action->oldpath);
602 g_hash_table_insert(entry->oldpath_hash, new_action->oldpath, new_action);
606 GList *work = g_list_find(entry->add_list, orig_action);
607 work->data = new_action;
610 g_hash_table_steal(entry->newpath_hash, orig_action->newpath);
611 if (new_action->newpath)
613 g_hash_table_insert(entry->newpath_hash, new_action->newpath, new_action);
615 collect_manager_action_unref(orig_action);
620 orig_action = g_hash_table_lookup(entry->oldpath_hash, action->oldpath);
623 /* another action for the same source, ignore */
624 printf("collection manager failed to add another action for source %s in collection %s\n",
625 action->oldpath, entry->path);
629 g_hash_table_insert(entry->oldpath_hash, action->oldpath, action);
632 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
634 collect_manager_action_ref(action);
637 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr)
639 gchar *path = *path_ptr;
640 CollectManagerAction *action;
647 action = entry->add_list->data;
648 g_assert(action->oldpath == NULL);
649 entry->add_list = g_list_remove(entry->add_list, action);
650 path = g_strdup(action->newpath);
651 g_hash_table_remove(entry->newpath_hash, path);
652 collect_manager_action_unref(action);
655 return (path != NULL);
658 action = g_hash_table_lookup(entry->oldpath_hash, path);
663 path = g_strdup(action->newpath);
668 return FALSE; /* no change */
671 static void collect_manager_refresh(void)
677 base = g_strconcat(homedir(), "/", GQ_RC_DIR_COLLECTIONS, NULL);
678 path_list(base, &list, NULL);
681 work = collection_manager_entry_list;
684 CollectManagerEntry *entry;
691 while (list_step && entry)
695 path = list_step->data;
696 list_step = list_step->next;
698 if (strcmp(path, entry->path) == 0)
700 list = g_list_remove(list, path);
707 collect_manager_entry_free(entry);
720 collect_manager_entry_new(path);
727 static void collect_manager_process_actions(gint max)
729 if (debug && collection_manager_action_list)
731 printf("collection manager processing actions\n");
734 while (collection_manager_action_list != NULL && max > 0)
736 CollectManagerAction *action;
739 action = collection_manager_action_list->data;
740 work = collection_manager_entry_list;
743 CollectManagerEntry *entry;
748 if (action->type == COLLECTION_MANAGER_UPDATE)
750 collect_manager_entry_add_action(entry, action);
752 else if (action->oldpath && action->newpath &&
753 strcmp(action->newpath, entry->path) == 0)
755 /* convert action to standard add format */
756 g_free(action->newpath);
757 if (action->type == COLLECTION_MANAGER_ADD)
759 action->newpath = action->oldpath;
760 action->oldpath = NULL;
762 else if (action->type == COLLECTION_MANAGER_REMOVE)
764 action->newpath = NULL;
766 collect_manager_entry_add_action(entry, action);
772 if (action->type != COLLECTION_MANAGER_UPDATE &&
773 action->oldpath && action->newpath)
775 printf("collection manager failed to %s %s for collection %s\n",
776 (action->type == COLLECTION_MANAGER_ADD) ? "add" : "remove",
777 action->oldpath, action->newpath);
780 if (collection_manager_action_tail == collection_manager_action_list)
782 collection_manager_action_tail = NULL;
784 collection_manager_action_list = g_list_remove(collection_manager_action_list, action);
785 collect_manager_action_unref(action);
789 static gint collect_manager_process_entry(CollectManagerEntry *entry)
794 if (entry->empty) return FALSE;
796 cd = collection_new(entry->path);
797 success = collection_load_private(cd, entry->path, COLLECTION_LOAD_NONE);
799 collection_unref(cd);
804 static gint collect_manager_process_entry_list(void)
808 work = collection_manager_entry_list;
811 CollectManagerEntry *entry;
815 if (collect_manager_process_entry(entry)) return TRUE;
823 static gint collect_manager_process_cb(gpointer data)
825 if (collection_manager_action_list) collect_manager_refresh();
826 collect_manager_process_actions(COLLECT_MANAGER_ACTIONS_PER_IDLE);
827 if (collection_manager_action_list) return TRUE;
829 if (collect_manager_process_entry_list()) return TRUE;
831 if (debug) printf("collection manager is up to date\n");
835 static gint collect_manager_timer_cb(gpointer data)
837 if (debug) printf("collection manager timer expired\n");
839 g_idle_add_full(G_PRIORITY_LOW, collect_manager_process_cb, NULL, NULL);
841 collection_manager_timer_id = -1;
845 static void collect_manager_timer_push(gint stop)
847 if (collection_manager_timer_id != -1)
851 g_source_remove(collection_manager_timer_id);
852 collection_manager_timer_id = -1;
857 collection_manager_timer_id = g_timeout_add(COLLECT_MANAGER_FLUSH_DELAY,
858 collect_manager_timer_cb, NULL);
859 if (debug) printf("collection manager timer started\n");
863 static void collect_manager_add_action(CollectManagerAction *action)
867 /* we keep track of the list's tail to keep this a n(1) operation */
869 if (collection_manager_action_tail)
871 collection_manager_action_tail = g_list_append(collection_manager_action_tail, action);
872 collection_manager_action_tail = collection_manager_action_tail->next;
876 collection_manager_action_list = g_list_append(collection_manager_action_list, action);
877 collection_manager_action_tail = collection_manager_action_list;
880 collect_manager_timer_push(FALSE);
883 void collect_manager_moved(FileData *fd)
885 CollectManagerAction *action;
886 const gchar *oldpath = fd->change->source;
887 const gchar *newpath = fd->change->dest;
889 action = collect_manager_action_new(oldpath, newpath, COLLECTION_MANAGER_UPDATE);
890 collect_manager_add_action(action);
893 void collect_manager_add(FileData *fd, const gchar *collection)
895 CollectManagerAction *action;
898 if (!fd || !collection) return;
900 cw = collection_window_find_by_path(collection);
903 if (collection_list_find(cw->cd->list, fd->path) == NULL)
905 collection_add(cw->cd, fd, FALSE);
910 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_ADD);
911 collect_manager_add_action(action);
914 void collect_manager_remove(FileData *fd, const gchar *collection)
916 CollectManagerAction *action;
919 if (!fd || !collection) return;
921 cw = collection_window_find_by_path(collection);
924 while (collection_remove(cw->cd, fd));
928 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_REMOVE);
929 collect_manager_add_action(action);
932 void collect_manager_flush(void)
934 collect_manager_timer_push(TRUE);
936 if (debug) printf("collection manager flushing\n");
937 while (collect_manager_process_cb(NULL));