2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "collect-io.h"
27 #include <gdk-pixbuf/gdk-pixbuf.h>
29 #include <glib-object.h>
31 #if defined(__linux__)
34 #include <sys/mount.h>
43 #include "layout-util.h"
44 #include "main-defines.h"
46 #include "secure-save.h"
48 #include "ui-fileops.h"
49 #include "ui-utildlg.h"
51 #define GQ_COLLECTION_MARKER "#" GQ_APPNAME
54 GQ_COLLECTION_FAIL_MIN = 300,
55 GQ_COLLECTION_FAIL_PERCENT = 98,
56 GQ_COLLECTION_READ_BUFSIZE = 4096
59 struct CollectManagerEntry;
61 static void collection_load_thumb_step(CollectionData *cd);
62 static gboolean collection_save_private(CollectionData *cd, const gchar *path);
64 static CollectManagerEntry *collect_manager_get_entry(const gchar *path);
65 static void collect_manager_entry_reset(CollectManagerEntry *entry);
66 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr);
71 gboolean scan_geometry(gchar *buffer, GdkRectangle &window)
78 if (sscanf(buffer, "%d %d %d %d", &nx, &ny, &nw, &nh) != 4) return FALSE;
90 static gboolean collection_load_private(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
92 gchar s_buf[GQ_COLLECTION_READ_BUFSIZE];
95 gboolean limit_failures = TRUE;
96 gboolean success = TRUE;
97 gboolean has_official_header = FALSE;
98 gboolean has_geometry_header = FALSE;
99 gboolean has_gqview_header = FALSE;
100 gboolean need_header = TRUE;
103 gboolean changed = FALSE;
104 CollectManagerEntry *entry = nullptr;
105 guint flush = !!(flags & COLLECTION_LOAD_FLUSH);
106 guint append = !!(flags & COLLECTION_LOAD_APPEND);
107 guint only_geometry = !!(flags & COLLECTION_LOAD_GEOMETRY);
108 gboolean reading_extended_filename = FALSE;
113 collection_load_stop(cd);
116 collect_manager_flush();
118 entry = collect_manager_get_entry(path);
122 g_list_free_full(cd->list, reinterpret_cast<GDestroyNotify>(collection_info_free));
127 if (!path && !cd->path) return FALSE;
129 if (!path) path = cd->path;
131 pathl = path_from_utf8(path);
133 DEBUG_1("collection load: append=%d flush=%d only_geometry=%d path=%s", append, flush, only_geometry, pathl);
136 f = fopen(pathl, "r");
140 log_printf("Failed to open collection file: \"%s\"\n", path);
144 GString *extended_filename_buffer = g_string_new(nullptr);
145 while (fgets(s_buf, sizeof(s_buf), f))
150 if (!reading_extended_filename)
152 /* Skip whitespaces and empty lines */
153 while (*p && g_ascii_isspace(*p)) p++;
154 if (*p == '\n' || *p == '\r') continue;
159 if (!need_header) continue;
160 if (g_ascii_strncasecmp(p, GQ_COLLECTION_MARKER, strlen(GQ_COLLECTION_MARKER)) == 0)
162 /* Looks like an official collection, allow unchecked input.
163 * All this does is allow adding files that may not exist,
164 * which is needed for the collection manager to work.
165 * Also unofficial files abort after too many invalid entries.
167 has_official_header = TRUE;
168 limit_failures = FALSE;
170 else if (strncmp(p, "#geometry:", 10 ) == 0 && scan_geometry(p + 10, cd->window))
172 has_geometry_header = TRUE;
173 cd->window_read = TRUE;
174 if (only_geometry) break;
176 else if (g_ascii_strncasecmp(p, "#GQview collection", strlen("#GQview collection")) == 0)
178 /* As 2008/04/15 there is no difference between our collection file format
179 * and GQview 2.1.5 collection file format so ignore failures as well. */
180 has_gqview_header = TRUE;
181 limit_failures = FALSE;
183 need_header = (!has_official_header && !has_gqview_header) || !has_geometry_header;
187 if (only_geometry) continue;
191 /** @todo This is not safe! */
192 /* Updated: anything within double quotes is considered a filename */
193 if (!reading_extended_filename)
195 /* not yet reading filename */
196 while (*p && *p != '"') p++;
198 /* start of filename read */
200 while (*p && *p != '"') p++;
203 /* first part of extended filename */
204 g_string_append(extended_filename_buffer, buf);
205 reading_extended_filename = TRUE;
212 while (*p && *p != '"') p++;
215 /* end of extended filename still not found */
216 g_string_append(extended_filename_buffer, buf);
220 /* end of extended filename found */
221 g_string_append_len(extended_filename_buffer, buf, p - buf);
222 reading_extended_filename = FALSE;
225 if (extended_filename_buffer->len > 0)
227 buffer2 = g_strdup(extended_filename_buffer->str);
228 g_string_erase(extended_filename_buffer, 0, -1);
233 buffer2 = g_strdup(buf);
239 gboolean found = FALSE;
242 changed |= collect_manager_process_action(entry, &buffer2);
244 valid = (buffer2[0] == G_DIR_SEPARATOR && collection_add_check(cd, file_data_new_simple(buffer2), FALSE, TRUE));
247 log_printf("Warning: Collection: %s Invalid file: %s", cd->name, buffer2);
248 DEBUG_1("collection invalid file: %s", buffer2);
254 /* If the file path has the prefix home, tmp or usr it was on the local file system and has been deleted. Ignore it. */
255 if (!g_str_has_prefix(buffer2, "/home") && !g_str_has_prefix(buffer2, "/tmp") && !g_str_has_prefix(buffer2, "/usr"))
257 /* The file was on a mounted drive and either has been deleted or the drive is not mounted */
258 #if defined(__linux__)
259 struct mntent *mount_entry;
262 mount_entries = setmntent("/proc/mounts", "r");
263 if (mount_entries == nullptr)
265 /* It is assumed this will never fail */
270 while (nullptr != (mount_entry = getmntent(mount_entries)))
272 if (g_strcmp0(mount_entry->mnt_dir, G_DIR_SEPARATOR_S) != 0)
274 if (g_str_has_prefix(buffer2, mount_entry->mnt_dir))
276 log_printf("%s was a file on a mounted filesystem but has been deleted: %s", buffer2, cd->name);
282 endmntent(mount_entries);
284 struct statfs* mounts;
285 int num_mounts = getmntinfo(&mounts, MNT_NOWAIT);
289 /* It is assumed this will never fail */
294 for (int i = 0; i < num_mounts; i++)
296 if (g_strcmp0(mounts[i].f_mntonname, G_DIR_SEPARATOR_S) != 0)
298 if (g_str_has_prefix(buffer2, mounts[i].f_mntonname))
300 log_printf("%s was a file on a mounted filesystem but has been deleted: %s", buffer2, cd->name);
310 log_printf("%s is a file on an unmounted filesystem: %s", buffer2, cd->path);
311 gchar *text = g_strdup_printf(_("This Collection cannot be opened because it contains a link to a file on a drive which is not yet mounted.\n\nCollection: %s\nFile: %s\n"), cd->path, buffer2);
312 warning_dialog(_("Cannot open Collection"), text, GQ_ICON_DIALOG_WARNING, nullptr);
315 collection_window_close_by_collection(cd);
322 log_printf("%s was a file on local filesystem but has been deleted: %s", buffer2, cd->name);
326 if (limit_failures && fail > GQ_COLLECTION_FAIL_MIN && fail * 100 / total > GQ_COLLECTION_FAIL_PERCENT)
328 log_printf("%d invalid filenames in unofficial collection file, closing: %s\n", fail, path);
337 g_string_free(extended_filename_buffer, TRUE);
339 DEBUG_1("collection files: total = %d fail = %d official=%d gqview=%d geometry=%d", total, fail, has_official_header, has_gqview_header, has_geometry_header);
342 if (only_geometry) return has_geometry_header;
346 gchar *buf = nullptr;
347 while (collect_manager_process_action(entry, &buf))
349 collection_add_check(cd, file_data_new_group(buf), FALSE, TRUE);
356 cd->list = collection_list_sort(cd->list, cd->sort_method);
358 if (!flush && changed && success)
359 collection_save_private(cd, path);
362 collect_manager_entry_reset(entry);
364 if (!append) cd->changed = FALSE;
369 gboolean collection_load(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
371 if (collection_load_private(cd, path, static_cast<CollectionLoadFlags>(flags | COLLECTION_LOAD_FLUSH)))
373 layout_recent_add_path(cd->path);
380 static void collection_load_thumb_do(CollectionData *cd)
384 if (!cd->thumb_loader || !g_list_find(cd->list, cd->thumb_info)) return;
386 pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader);
387 collection_info_set_thumb(cd->thumb_info, pixbuf);
388 g_object_unref(pixbuf);
390 if (cd->info_updated_func) cd->info_updated_func(cd, cd->thumb_info, cd->info_updated_data);
393 static void collection_load_thumb_error_cb(ThumbLoader *, gpointer data)
395 auto cd = static_cast<CollectionData *>(data);
397 collection_load_thumb_do(cd);
398 collection_load_thumb_step(cd);
401 static void collection_load_thumb_done_cb(ThumbLoader *, gpointer data)
403 auto cd = static_cast<CollectionData *>(data);
405 collection_load_thumb_do(cd);
406 collection_load_thumb_step(cd);
409 static void collection_load_thumb_step(CollectionData *cd)
416 collection_load_stop(cd);
421 ci = static_cast<CollectInfo *>(work->data);
423 /* find first unloaded thumb */
424 while (work && ci->pixbuf)
426 ci = static_cast<CollectInfo *>(work->data);
430 if (!ci || ci->pixbuf)
433 collection_load_stop(cd);
435 /* send a NULL CollectInfo to notify end */
436 if (cd->info_updated_func) cd->info_updated_func(cd, nullptr, cd->info_updated_data);
441 /* setup loader and call it */
443 thumb_loader_free(cd->thumb_loader);
444 cd->thumb_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
445 thumb_loader_set_callbacks(cd->thumb_loader,
446 collection_load_thumb_done_cb,
447 collection_load_thumb_error_cb,
452 if (!thumb_loader_start(cd->thumb_loader, ci->fd))
454 /* error, handle it, do next */
455 DEBUG_1("error loading thumb for %s", ci->fd->path);
456 collection_load_thumb_do(cd);
457 collection_load_thumb_step(cd);
461 void collection_load_thumb_idle(CollectionData *cd)
463 if (!cd->thumb_loader) collection_load_thumb_step(cd);
466 gboolean collection_load_begin(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
468 if (!collection_load(cd, path, flags)) return FALSE;
470 collection_load_thumb_idle(cd);
475 void collection_load_stop(CollectionData *cd)
477 if (!cd->thumb_loader) return;
479 thumb_loader_free(cd->thumb_loader);
480 cd->thumb_loader = nullptr;
483 static gboolean collection_save_private(CollectionData *cd, const gchar *path)
489 if (!path && !cd->path) return FALSE;
497 pathl = path_from_utf8(path);
498 ssi = secure_open(pathl);
502 log_printf(_("failed to open collection (write) \"%s\"\n"), path);
506 secure_fprintf(ssi, "%s collection\n", GQ_COLLECTION_MARKER);
507 secure_fprintf(ssi, "#created with %s version %s\n", GQ_APPNAME, VERSION);
509 collection_update_geometry(cd);
512 secure_fprintf(ssi, "#geometry: %d %d %d %d\n", cd->window.x, cd->window.y, cd->window.width, cd->window.height);
516 while (work && secsave_errno == SS_ERR_NONE)
518 auto ci = static_cast<CollectInfo *>(work->data);
519 secure_fprintf(ssi, "\"%s\"\n", ci->fd->path);
523 secure_fprintf(ssi, "#end\n");
525 if (secure_close(ssi))
527 log_printf(_("error saving collection file: %s\nerror: %s\n"), path,
528 secsave_strerror(secsave_errno));
532 if (!cd->path || strcmp(path, cd->path) != 0)
534 gchar *buf = cd->path;
535 cd->path = g_strdup(path);
540 cd->name = g_strdup(filename_from_path(cd->path));
542 collection_path_changed(cd);
550 gboolean collection_save(CollectionData *cd, const gchar *path)
552 if (collection_save_private(cd, path))
554 layout_recent_add_path(cd->path);
561 gboolean collection_load_only_geometry(CollectionData *cd, const gchar *path)
563 return collection_load(cd, path, COLLECTION_LOAD_GEOMETRY);
568 *-------------------------------------------------------------------
570 *-------------------------------------------------------------------
574 COLLECT_MANAGER_ACTIONS_PER_IDLE = 1000,
575 COLLECT_MANAGER_FLUSH_DELAY = 10000
578 struct CollectManagerEntry
582 GHashTable *oldpath_hash;
583 GHashTable *newpath_hash;
587 enum CollectManagerType {
588 COLLECTION_MANAGER_UPDATE,
589 COLLECTION_MANAGER_ADD,
590 COLLECTION_MANAGER_REMOVE
593 struct CollectManagerAction
598 CollectManagerType type;
604 static GList *collection_manager_entry_list = nullptr;
605 static GList *collection_manager_action_list = nullptr;
606 static GList *collection_manager_action_tail = nullptr;
607 static guint collection_manager_timer_id = 0; /* event source id */
610 static CollectManagerAction *collect_manager_action_new(const gchar *oldpath, const gchar *newpath,
611 CollectManagerType type)
613 CollectManagerAction *action;
615 action = g_new0(CollectManagerAction, 1);
618 action->oldpath = g_strdup(oldpath);
619 action->newpath = g_strdup(newpath);
626 static void collect_manager_action_ref(CollectManagerAction *action)
631 static void collect_manager_action_unref(CollectManagerAction *action)
635 if (action->ref > 0) return;
637 g_free(action->oldpath);
638 g_free(action->newpath);
642 static void collect_manager_entry_free_data(CollectManagerEntry *entry)
644 g_list_free_full(entry->add_list, reinterpret_cast<GDestroyNotify>(collect_manager_action_unref));
645 if (g_hash_table_size(entry->oldpath_hash) > 0)
646 g_hash_table_destroy(entry->oldpath_hash);
648 g_hash_table_unref(entry->oldpath_hash);
649 if (g_hash_table_size(entry->newpath_hash) > 0)
650 g_hash_table_destroy(entry->newpath_hash);
652 g_hash_table_unref(entry->newpath_hash);
655 static void collect_manager_entry_init_data(CollectManagerEntry *entry)
657 entry->add_list = nullptr;
658 entry->oldpath_hash = g_hash_table_new_full(g_str_hash, g_str_equal, nullptr, reinterpret_cast<GDestroyNotify>(collect_manager_action_unref));
659 entry->newpath_hash = g_hash_table_new(g_str_hash, g_str_equal);
664 static CollectManagerEntry *collect_manager_entry_new(const gchar *path)
666 CollectManagerEntry *entry;
668 entry = g_new0(CollectManagerEntry, 1);
669 entry->path = g_strdup(path);
670 collect_manager_entry_init_data(entry);
672 collection_manager_entry_list = g_list_append(collection_manager_entry_list, entry);
678 static void collect_manager_entry_free(CollectManagerEntry *entry)
680 collection_manager_entry_list = g_list_remove(collection_manager_entry_list, entry);
682 collect_manager_entry_free_data(entry);
688 static void collect_manager_entry_reset(CollectManagerEntry *entry)
690 collect_manager_entry_free_data(entry);
691 collect_manager_entry_init_data(entry);
694 static CollectManagerEntry *collect_manager_get_entry(const gchar *path)
698 work = collection_manager_entry_list;
701 CollectManagerEntry *entry;
703 entry = static_cast<CollectManagerEntry *>(work->data);
705 if (strcmp(entry->path, path) == 0)
714 static void collect_manager_entry_add_action(CollectManagerEntry *entry, CollectManagerAction *action)
717 CollectManagerAction *orig_action;
719 entry->empty = FALSE;
721 if (action->oldpath == nullptr)
724 if (action->newpath == nullptr)
729 orig_action = static_cast<CollectManagerAction *>(g_hash_table_lookup(entry->newpath_hash, action->newpath));
732 /* target already exists */
733 log_printf("collection manager failed to add another action for target %s in collection %s\n",
734 action->newpath, entry->path);
737 entry->add_list = g_list_append(entry->add_list, action);
738 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
739 collect_manager_action_ref(action);
743 orig_action = static_cast<CollectManagerAction *>(g_hash_table_lookup(entry->newpath_hash, action->oldpath));
746 /* new action with the same file */
747 CollectManagerAction *new_action = collect_manager_action_new(orig_action->oldpath, action->newpath, action->type);
749 if (new_action->oldpath)
751 g_hash_table_steal(entry->oldpath_hash, orig_action->oldpath);
752 g_hash_table_insert(entry->oldpath_hash, new_action->oldpath, new_action);
756 GList *work = g_list_find(entry->add_list, orig_action);
757 work->data = new_action;
760 g_hash_table_steal(entry->newpath_hash, orig_action->newpath);
761 if (new_action->newpath)
763 g_hash_table_insert(entry->newpath_hash, new_action->newpath, new_action);
765 collect_manager_action_unref(orig_action);
770 orig_action = static_cast<CollectManagerAction *>(g_hash_table_lookup(entry->oldpath_hash, action->oldpath));
773 /* another action for the same source, ignore */
774 log_printf("collection manager failed to add another action for source %s in collection %s\n",
775 action->oldpath, entry->path);
779 g_hash_table_insert(entry->oldpath_hash, action->oldpath, action);
782 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
784 collect_manager_action_ref(action);
787 static gboolean collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr)
789 gchar *path = *path_ptr;
790 CollectManagerAction *action;
797 action = static_cast<CollectManagerAction *>(entry->add_list->data);
798 g_assert(action->oldpath == nullptr);
799 entry->add_list = g_list_remove(entry->add_list, action);
800 path = g_strdup(action->newpath);
801 g_hash_table_remove(entry->newpath_hash, path);
802 collect_manager_action_unref(action);
805 return (path != nullptr);
808 action = static_cast<CollectManagerAction *>(g_hash_table_lookup(entry->oldpath_hash, path));
812 strcpy(*path_ptr, action->newpath);
816 return FALSE; /* no change */
819 static void collect_manager_refresh()
825 dir_fd = file_data_new_dir(get_collections_dir());
826 filelist_read(dir_fd, &list, nullptr);
827 file_data_unref(dir_fd);
829 work = collection_manager_entry_list;
832 CollectManagerEntry *entry;
835 entry = static_cast<CollectManagerEntry *>(work->data);
839 while (list_step && entry)
843 fd = static_cast<FileData *>(list_step->data);
844 list_step = list_step->next;
846 if (strcmp(fd->path, entry->path) == 0)
848 list = g_list_remove(list, fd);
855 collect_manager_entry_free(entry);
867 fd = static_cast<FileData *>(work->data);
870 collect_manager_entry_new(fd->path);
876 static void collect_manager_process_actions(gint max)
878 if (collection_manager_action_list) DEBUG_1("collection manager processing actions");
880 while (collection_manager_action_list != nullptr && max > 0)
882 CollectManagerAction *action;
885 action = static_cast<CollectManagerAction *>(collection_manager_action_list->data);
886 work = collection_manager_entry_list;
889 CollectManagerEntry *entry;
891 entry = static_cast<CollectManagerEntry *>(work->data);
894 if (action->type == COLLECTION_MANAGER_UPDATE)
896 collect_manager_entry_add_action(entry, action);
898 else if (action->oldpath && action->newpath &&
899 strcmp(action->newpath, entry->path) == 0)
901 /* convert action to standard add format */
902 g_free(action->newpath);
903 if (action->type == COLLECTION_MANAGER_ADD)
905 action->newpath = action->oldpath;
906 action->oldpath = nullptr;
908 else if (action->type == COLLECTION_MANAGER_REMOVE)
910 action->newpath = nullptr;
912 collect_manager_entry_add_action(entry, action);
918 if (action->type != COLLECTION_MANAGER_UPDATE &&
919 action->oldpath && action->newpath)
921 log_printf("collection manager failed to %s %s for collection %s\n",
922 (action->type == COLLECTION_MANAGER_ADD) ? "add" : "remove",
923 action->oldpath, action->newpath);
926 if (collection_manager_action_tail == collection_manager_action_list)
928 collection_manager_action_tail = nullptr;
930 collection_manager_action_list = g_list_remove(collection_manager_action_list, action);
931 collect_manager_action_unref(action);
935 static gboolean collect_manager_process_entry(CollectManagerEntry *entry)
939 if (entry->empty) return FALSE;
941 cd = collection_new(entry->path);
942 (void) collection_load_private(cd, entry->path, COLLECTION_LOAD_NONE);
944 collection_unref(cd);
949 static gboolean collect_manager_process_entry_list()
953 work = collection_manager_entry_list;
956 CollectManagerEntry *entry;
958 entry = static_cast<CollectManagerEntry *>(work->data);
960 if (collect_manager_process_entry(entry)) return TRUE;
968 static gboolean collect_manager_process_cb(gpointer)
970 if (collection_manager_action_list) collect_manager_refresh();
971 collect_manager_process_actions(COLLECT_MANAGER_ACTIONS_PER_IDLE);
972 if (collection_manager_action_list) return G_SOURCE_CONTINUE;
974 if (collect_manager_process_entry_list()) return G_SOURCE_CONTINUE;
976 DEBUG_1("collection manager is up to date");
977 return G_SOURCE_REMOVE;
980 static gboolean collect_manager_timer_cb(gpointer)
982 DEBUG_1("collection manager timer expired");
984 g_idle_add_full(G_PRIORITY_LOW, collect_manager_process_cb, nullptr, nullptr);
986 collection_manager_timer_id = 0;
990 static void collect_manager_timer_push(gint stop)
992 if (collection_manager_timer_id)
996 g_source_remove(collection_manager_timer_id);
997 collection_manager_timer_id = 0;
1002 collection_manager_timer_id = g_timeout_add(COLLECT_MANAGER_FLUSH_DELAY,
1003 collect_manager_timer_cb, nullptr);
1004 DEBUG_1("collection manager timer started");
1008 static void collect_manager_add_action(CollectManagerAction *action)
1010 if (!action) return;
1012 /* we keep track of the list's tail to keep this a n(1) operation */
1014 if (collection_manager_action_tail)
1016 collection_manager_action_tail = g_list_append(collection_manager_action_tail, action);
1017 collection_manager_action_tail = collection_manager_action_tail->next;
1021 collection_manager_action_list = g_list_append(collection_manager_action_list, action);
1022 collection_manager_action_tail = collection_manager_action_list;
1025 collect_manager_timer_push(FALSE);
1028 void collect_manager_moved(FileData *fd)
1030 CollectManagerAction *action;
1031 const gchar *oldpath = fd->change->source;
1032 const gchar *newpath = fd->change->dest;
1034 action = collect_manager_action_new(oldpath, newpath, COLLECTION_MANAGER_UPDATE);
1035 collect_manager_add_action(action);
1038 void collect_manager_add(FileData *fd, const gchar *collection)
1040 CollectManagerAction *action;
1043 if (!fd || !collection) return;
1045 cw = collection_window_find_by_path(collection);
1048 if (collection_list_find_fd(cw->cd->list, fd) == nullptr)
1050 collection_add(cw->cd, fd, FALSE);
1055 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_ADD);
1056 collect_manager_add_action(action);
1059 void collect_manager_remove(FileData *fd, const gchar *collection)
1061 CollectManagerAction *action;
1064 if (!fd || !collection) return;
1066 cw = collection_window_find_by_path(collection);
1069 while (collection_remove(cw->cd, fd));
1073 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_REMOVE);
1074 collect_manager_add_action(action);
1077 void collect_manager_flush()
1079 collect_manager_timer_push(TRUE);
1081 DEBUG_1("collection manager flushing");
1082 while (collect_manager_process_cb(nullptr));
1085 void collect_manager_notify_cb(FileData *fd, NotifyType type, gpointer)
1087 if (!(type & NOTIFY_CHANGE) || !fd->change) return;
1089 DEBUG_1("Notify collect_manager: %s %04x", fd->path, type);
1090 switch (fd->change->type)
1092 case FILEDATA_CHANGE_MOVE:
1093 collect_manager_moved(fd);
1095 case FILEDATA_CHANGE_COPY:
1097 case FILEDATA_CHANGE_RENAME:
1098 collect_manager_moved(fd);
1100 case FILEDATA_CHANGE_DELETE:
1101 case FILEDATA_CHANGE_UNSPECIFIED:
1102 case FILEDATA_CHANGE_WRITE_METADATA:
1107 static gint collection_manager_sort_cb(gconstpointer a, gconstpointer b)
1109 auto char_a = static_cast<const gchar *>(a);
1110 auto char_b = static_cast<const gchar *>(b);
1112 return g_strcmp0(char_a, char_b);
1116 * @brief Creates sorted list of collections
1117 * @param[out] names_exc sorted list of collections names excluding extension
1118 * @param[out] names_inc sorted list of collections names including extension
1119 * @param[out] paths sorted list of collection paths
1121 * Lists of type gchar.
1122 * Used lists must be freed with string_list_free()
1124 void collect_manager_list(GList **names_exc, GList **names_inc, GList **paths)
1127 GList *list = nullptr;
1132 if (names_exc == nullptr && names_inc == nullptr && paths == nullptr)
1137 dir_fd = file_data_new_dir((get_collections_dir()));
1139 filelist_read(dir_fd, &list, nullptr);
1143 fd = static_cast<FileData *>(list->data);
1144 filename = g_strdup(filename_from_path(fd->path));
1146 if (file_extension_match(filename, GQ_COLLECTION_EXT))
1148 name = remove_extension_from_path(filename);
1150 if (names_exc != nullptr)
1152 *names_exc = g_list_insert_sorted(*names_exc, g_strdup(name),
1153 collection_manager_sort_cb);
1154 *names_exc = g_list_first(*names_exc);
1156 if (names_inc != nullptr)
1158 *names_inc = g_list_insert_sorted(*names_inc,filename,
1159 collection_manager_sort_cb);
1160 *names_inc = g_list_first(*names_inc);
1162 if (paths != nullptr)
1164 *paths = g_list_insert_sorted(*paths, g_strdup(fd->path),
1165 collection_manager_sort_cb);
1166 *paths = g_list_first(*paths);
1174 filelist_free(list);
1176 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */