2 * Copyright (C) 2006 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.
25 #include "filefilter.h"
27 #include "thumb-standard.h"
28 #include "ui-fileops.h"
31 #include "histogram.h"
32 #include "secure-save.h"
40 gint global_file_data_count = 0;
43 static GHashTable *file_data_pool = nullptr;
44 static GHashTable *file_data_planned_change_hash = nullptr;
46 static gint sidecar_file_priority(const gchar *extension);
47 static void file_data_check_sidecars(const GList *basename_list);
48 static void file_data_disconnect_sidecar_file(FileData *target, FileData *sfd);
51 static SortType filelist_sort_method = SORT_NONE;
52 static gboolean filelist_sort_ascend = TRUE;
53 static gboolean filelist_sort_case = TRUE;
56 *-----------------------------------------------------------------------------
57 * text conversion utils
58 *-----------------------------------------------------------------------------
61 gchar *text_from_size(gint64 size)
67 /* what I would like to use is printf("%'d", size)
68 * BUT: not supported on every libc :(
72 /* the %lld conversion is not valid in all libcs, so use a simple work-around */
73 a = g_strdup_printf("%d%09d", static_cast<guint>(size / 1000000000), static_cast<guint>(size % 1000000000));
77 a = g_strdup_printf("%d", static_cast<guint>(size));
83 b = g_new(gchar, l + n + 1);
108 gchar *text_from_size_abrev(gint64 size)
110 if (size < static_cast<gint64>(1024))
112 return g_strdup_printf(_("%d bytes"), static_cast<gint>(size));
114 if (size < static_cast<gint64>(1048576))
116 return g_strdup_printf(_("%.1f KiB"), static_cast<gdouble>(size) / 1024.0);
118 if (size < static_cast<gint64>(1073741824))
120 return g_strdup_printf(_("%.1f MiB"), static_cast<gdouble>(size) / 1048576.0);
123 /* to avoid overflowing the gdouble, do division in two steps */
125 return g_strdup_printf(_("%.1f GiB"), static_cast<gdouble>(size) / 1024.0);
128 /* note: returned string is valid until next call to text_from_time() */
129 const gchar *text_from_time(time_t t)
131 static gchar *ret = nullptr;
135 GError *error = nullptr;
137 btime = localtime(&t);
139 /* the %x warning about 2 digit years is not an error */
140 buflen = strftime(buf, sizeof(buf), "%x %X", btime);
141 if (buflen < 1) return "";
144 ret = g_locale_to_utf8(buf, buflen, nullptr, nullptr, &error);
147 log_printf("Error converting locale strftime to UTF-8: %s\n", error->message);
156 *-----------------------------------------------------------------------------
157 * changed files detection and notification
158 *-----------------------------------------------------------------------------
161 void file_data_increment_version(FileData *fd)
167 fd->parent->version++;
168 fd->parent->valid_marks = 0;
172 static gboolean file_data_check_changed_single_file(FileData *fd, struct stat *st)
174 if (fd->size != st->st_size ||
175 fd->date != st->st_mtime)
177 fd->size = st->st_size;
178 fd->date = st->st_mtime;
179 fd->cdate = st->st_ctime;
180 fd->mode = st->st_mode;
181 if (fd->thumb_pixbuf) g_object_unref(fd->thumb_pixbuf);
182 fd->thumb_pixbuf = nullptr;
183 file_data_increment_version(fd);
184 file_data_send_notification(fd, NOTIFY_REREAD);
190 static gboolean file_data_check_changed_files_recursive(FileData *fd, struct stat *st)
192 gboolean ret = FALSE;
195 ret = file_data_check_changed_single_file(fd, st);
197 work = fd->sidecar_files;
200 auto sfd = static_cast<FileData *>(work->data);
204 if (!stat_utf8(sfd->path, &st))
209 file_data_disconnect_sidecar_file(fd, sfd);
211 file_data_increment_version(sfd);
212 file_data_send_notification(sfd, NOTIFY_REREAD);
213 file_data_unref(sfd);
217 ret |= file_data_check_changed_files_recursive(sfd, &st);
223 gboolean file_data_check_changed_files(FileData *fd)
225 gboolean ret = FALSE;
228 if (fd->parent) fd = fd->parent;
230 if (!stat_utf8(fd->path, &st))
234 FileData *sfd = nullptr;
236 /* parent is missing, we have to rebuild whole group */
241 /* file_data_disconnect_sidecar_file might delete the file,
242 we have to keep the reference to prevent this */
243 sidecars = filelist_copy(fd->sidecar_files);
248 sfd = static_cast<FileData *>(work->data);
251 file_data_disconnect_sidecar_file(fd, sfd);
253 file_data_check_sidecars(sidecars); /* this will group the sidecars back together */
254 /* now we can release the sidecars */
255 filelist_free(sidecars);
256 file_data_increment_version(fd);
257 file_data_send_notification(fd, NOTIFY_REREAD);
262 ret |= file_data_check_changed_files_recursive(fd, &st);
269 *-----------------------------------------------------------------------------
270 * file name, extension, sorting, ...
271 *-----------------------------------------------------------------------------
274 static void file_data_set_collate_keys(FileData *fd)
276 gchar *caseless_name;
279 valid_name = g_filename_display_name(fd->name);
280 caseless_name = g_utf8_casefold(valid_name, -1);
282 g_free(fd->collate_key_name);
283 g_free(fd->collate_key_name_nocase);
285 fd->collate_key_name_natural = g_utf8_collate_key_for_filename(fd->name, -1);
286 fd->collate_key_name_nocase_natural = g_utf8_collate_key_for_filename(caseless_name, -1);
287 fd->collate_key_name = g_utf8_collate_key(valid_name, -1);
288 fd->collate_key_name_nocase = g_utf8_collate_key(caseless_name, -1);
291 g_free(caseless_name);
294 static void file_data_set_path(FileData *fd, const gchar *path)
296 g_assert(path /* && *path*/); /* view_dir_tree uses FileData with zero length path */
297 g_assert(file_data_pool);
301 if (fd->original_path)
303 g_hash_table_remove(file_data_pool, fd->original_path);
304 g_free(fd->original_path);
307 g_assert(!g_hash_table_lookup(file_data_pool, path));
309 fd->original_path = g_strdup(path);
310 g_hash_table_insert(file_data_pool, fd->original_path, fd);
312 if (strcmp(path, G_DIR_SEPARATOR_S) == 0)
314 fd->path = g_strdup(path);
316 fd->extension = fd->name + 1;
317 file_data_set_collate_keys(fd);
321 fd->path = g_strdup(path);
322 fd->name = filename_from_path(fd->path);
324 if (strcmp(fd->name, "..") == 0)
326 gchar *dir = remove_level_from_path(path);
328 fd->path = remove_level_from_path(dir);
331 fd->extension = fd->name + 2;
332 file_data_set_collate_keys(fd);
335 else if (strcmp(fd->name, ".") == 0)
338 fd->path = remove_level_from_path(path);
340 fd->extension = fd->name + 1;
341 file_data_set_collate_keys(fd);
345 fd->extension = registered_extension_from_path(fd->path);
346 if (fd->extension == nullptr)
348 fd->extension = fd->name + strlen(fd->name);
351 fd->sidecar_priority = sidecar_file_priority(fd->extension);
352 file_data_set_collate_keys(fd);
356 *-----------------------------------------------------------------------------
357 * create or reuse Filedata
358 *-----------------------------------------------------------------------------
361 static FileData *file_data_new(const gchar *path_utf8, struct stat *st, gboolean disable_sidecars)
367 DEBUG_2("file_data_new: '%s' %d", path_utf8, disable_sidecars);
369 if (S_ISDIR(st->st_mode)) disable_sidecars = TRUE;
372 file_data_pool = g_hash_table_new(g_str_hash, g_str_equal);
374 fd = static_cast<FileData *>(g_hash_table_lookup(file_data_pool, path_utf8));
380 if (!fd && file_data_planned_change_hash)
382 fd = static_cast<FileData *>(g_hash_table_lookup(file_data_planned_change_hash, path_utf8));
385 DEBUG_1("planned change: using %s -> %s", path_utf8, fd->path);
386 if (!isfile(fd->path))
389 file_data_apply_ci(fd);
402 if (disable_sidecars) file_data_disable_grouping(fd, TRUE);
405 changed = file_data_check_changed_single_file(fd, st);
407 DEBUG_2("file_data_pool hit: '%s' %s", fd->path, changed ? "(changed)" : "");
412 fd = g_new0(FileData, 1);
413 #ifdef DEBUG_FILEDATA
414 global_file_data_count++;
415 DEBUG_2("file data count++: %d", global_file_data_count);
418 fd->size = st->st_size;
419 fd->date = st->st_mtime;
420 fd->cdate = st->st_ctime;
421 fd->mode = st->st_mode;
423 fd->magick = FD_MAGICK;
425 fd->rating = STAR_RATING_NOT_READ;
426 fd->format_class = filter_file_get_class(path_utf8);
430 user = getpwuid(st->st_uid);
433 fd->owner = g_strdup_printf("%u", st->st_uid);
437 fd->owner = g_strdup(user->pw_name);
440 group = getgrgid(st->st_gid);
443 fd->group = g_strdup_printf("%u", st->st_gid);
447 fd->group = g_strdup(group->gr_name);
450 fd->sym_link = get_symbolic_link(path_utf8);
452 if (disable_sidecars) fd->disable_grouping = TRUE;
454 file_data_set_path(fd, path_utf8); /* set path, name, collate_key_*, original_path */
459 static FileData *file_data_new_local(const gchar *path, struct stat *st, gboolean disable_sidecars)
461 gchar *path_utf8 = path_to_utf8(path);
462 FileData *ret = file_data_new(path_utf8, st, disable_sidecars);
468 FileData *file_data_new_simple(const gchar *path_utf8)
473 if (!stat_utf8(path_utf8, &st))
479 fd = static_cast<FileData *>(g_hash_table_lookup(file_data_pool, path_utf8));
480 if (!fd) fd = file_data_new(path_utf8, &st, TRUE);
489 void read_exif_time_data(FileData *file)
491 if (file->exifdate > 0)
493 DEBUG_1("%s set_exif_time_data: Already exists for %s", get_exec_time(), file->path);
504 gchar *tmp = exif_get_data_as_text(file->exif, "Exif.Photo.DateTimeOriginal");
505 DEBUG_2("%s set_exif_time_data: reading %p %s", get_exec_time(), (void *)file, file->path);
510 uint year, month, day, hour, min, sec;
512 sscanf(tmp, "%4u:%2u:%2u %2u:%2u:%2u", &year, &month, &day, &hour, &min, &sec);
513 time_str.tm_year = year - 1900;
514 time_str.tm_mon = month - 1;
515 time_str.tm_mday = day;
516 time_str.tm_hour = hour;
517 time_str.tm_min = min;
518 time_str.tm_sec = sec;
519 time_str.tm_isdst = 0;
521 file->exifdate = mktime(&time_str);
527 void read_exif_time_digitized_data(FileData *file)
529 if (file->exifdate_digitized > 0)
531 DEBUG_1("%s set_exif_time_digitized_data: Already exists for %s", get_exec_time(), file->path);
542 gchar *tmp = exif_get_data_as_text(file->exif, "Exif.Photo.DateTimeDigitized");
543 DEBUG_2("%s set_exif_time_digitized_data: reading %p %s", get_exec_time(), (void *)file, file->path);
548 uint year, month, day, hour, min, sec;
550 sscanf(tmp, "%4u:%2u:%2u %2u:%2u:%2u", &year, &month, &day, &hour, &min, &sec);
551 time_str.tm_year = year - 1900;
552 time_str.tm_mon = month - 1;
553 time_str.tm_mday = day;
554 time_str.tm_hour = hour;
555 time_str.tm_min = min;
556 time_str.tm_sec = sec;
557 time_str.tm_isdst = 0;
559 file->exifdate_digitized = mktime(&time_str);
565 void read_rating_data(FileData *file)
569 rating_str = metadata_read_string(file, RATING_KEY, METADATA_PLAIN);
572 file->rating = atoi(rating_str);
581 #pragma GCC diagnostic push
582 #pragma GCC diagnostic ignored "-Wunused-function"
583 void set_exif_time_data_unused(GList *files)
585 DEBUG_1("%s set_exif_time_data: ...", get_exec_time());
589 FileData *file = static_cast<FileData *>(files->data);
591 read_exif_time_data(file);
596 void set_exif_time_digitized_data_unused(GList *files)
598 DEBUG_1("%s set_exif_time_digitized_data: ...", get_exec_time());
602 FileData *file = static_cast<FileData *>(files->data);
604 read_exif_time_digitized_data(file);
609 void set_rating_data_unused(GList *files)
612 DEBUG_1("%s set_rating_data: ...", get_exec_time());
616 FileData *file = static_cast<FileData *>(files->data);
617 rating_str = metadata_read_string(file, RATING_KEY, METADATA_PLAIN);
620 file->rating = atoi(rating_str);
626 #pragma GCC diagnostic pop
628 FileData *file_data_new_no_grouping(const gchar *path_utf8)
632 if (!stat_utf8(path_utf8, &st))
638 return file_data_new(path_utf8, &st, TRUE);
641 FileData *file_data_new_dir(const gchar *path_utf8)
645 if (!stat_utf8(path_utf8, &st))
651 /* dir or non-existing yet */
652 g_assert(S_ISDIR(st.st_mode));
654 return file_data_new(path_utf8, &st, TRUE);
658 *-----------------------------------------------------------------------------
660 *-----------------------------------------------------------------------------
663 #ifdef DEBUG_FILEDATA
664 FileData *file_data_ref_debug(const gchar *file, gint line, FileData *fd)
666 FileData *file_data_ref(FileData *fd)
669 if (fd == nullptr) return nullptr;
670 if (fd->magick != FD_MAGICK)
671 #ifdef DEBUG_FILEDATA
672 log_printf("Error: fd magick mismatch @ %s:%d fd=%p", file, line, (void *)fd);
674 log_printf("Error: fd magick mismatch fd=%p", fd);
676 g_assert(fd->magick == FD_MAGICK);
679 #ifdef DEBUG_FILEDATA
680 DEBUG_2("file_data_ref fd=%p (%d): '%s' @ %s:%d", (void *)fd, fd->ref, fd->path, file, line);
682 DEBUG_2("file_data_ref fd=%p (%d): '%s'", fd, fd->ref, fd->path);
688 * @brief Print ref. count and image name
691 * Print image ref. count and full path name of all images in
692 * the file_data_pool.
694 * Used only by DEBUG_FD()
696 void file_data_dump()
698 #ifdef DEBUG_FILEDATA
704 list = g_hash_table_get_values(file_data_pool);
706 log_printf("%d", global_file_data_count);
707 log_printf("%d", g_list_length(list));
712 fd = static_cast<FileData *>(work->data);
713 log_printf("%-4d %s", fd->ref, fd->path);
722 static void file_data_free(FileData *fd)
724 g_assert(fd->magick == FD_MAGICK);
725 g_assert(fd->ref == 0);
726 g_assert(!fd->locked);
728 #ifdef DEBUG_FILEDATA
729 global_file_data_count--;
730 DEBUG_2("file data count--: %d", global_file_data_count);
733 metadata_cache_free(fd);
734 g_hash_table_remove(file_data_pool, fd->original_path);
737 g_free(fd->original_path);
738 g_free(fd->collate_key_name);
739 g_free(fd->collate_key_name_nocase);
740 g_free(fd->extended_extension);
741 if (fd->thumb_pixbuf) g_object_unref(fd->thumb_pixbuf);
742 histmap_free(fd->histmap);
745 g_free(fd->sym_link);
746 g_free(fd->format_name);
747 g_assert(fd->sidecar_files == nullptr); /* sidecar files must be freed before calling this */
749 file_data_change_info_free(nullptr, fd);
754 * @brief Checks if the FileData is referenced
756 * Checks the refcount and whether the FileData is locked.
758 static gboolean file_data_check_has_ref(FileData *fd)
760 return fd->ref > 0 || fd->locked;
764 * @brief Consider freeing a FileData.
766 * This function will free a FileData and its children provided that neither its parent nor it has
767 * a positive refcount, and provided that neither is locked.
769 static void file_data_consider_free(FileData *fd)
772 FileData *parent = fd->parent ? fd->parent : fd;
774 g_assert(fd->magick == FD_MAGICK);
775 if (file_data_check_has_ref(fd)) return;
776 if (file_data_check_has_ref(parent)) return;
778 work = parent->sidecar_files;
781 auto sfd = static_cast<FileData *>(work->data);
782 if (file_data_check_has_ref(sfd)) return;
786 /* Neither the parent nor the siblings are referenced, so we can free everything */
787 DEBUG_2("file_data_consider_free: deleting '%s', parent '%s'",
788 fd->path, fd->parent ? parent->path : "-");
790 g_list_free_full(parent->sidecar_files, reinterpret_cast<GDestroyNotify>(file_data_free));
791 parent->sidecar_files = nullptr;
793 file_data_free(parent);
796 #ifdef DEBUG_FILEDATA
797 void file_data_unref_debug(const gchar *file, gint line, FileData *fd)
799 void file_data_unref(FileData *fd)
802 if (fd == nullptr) return;
803 if (fd->magick != FD_MAGICK)
804 #ifdef DEBUG_FILEDATA
805 log_printf("Error: fd magick mismatch @ %s:%d fd=%p", file, line, (void *)fd);
807 log_printf("Error: fd magick mismatch fd=%p", fd);
809 g_assert(fd->magick == FD_MAGICK);
812 #ifdef DEBUG_FILEDATA
813 DEBUG_2("file_data_unref fd=%p (%d:%d): '%s' @ %s:%d", (void *)fd, fd->ref, fd->locked, fd->path,
816 DEBUG_2("file_data_unref fd=%p (%d:%d): '%s'", fd, fd->ref, fd->locked, fd->path);
819 // Free FileData if it's no longer ref'd
820 file_data_consider_free(fd);
824 * @brief Lock the FileData in memory.
826 * This allows the caller to prevent a FileData from being freed, even after its refcount is zero.
827 * This is intended to be used in cases where a FileData _should_ stay in memory as an optimization,
828 * even if the code would continue to function properly even if the FileData were freed. Code that
829 * _requires_ the FileData to remain in memory should continue to use file_data_(un)ref.
831 * Note: This differs from file_data_ref in that the behavior is reentrant -- after N calls to
832 * file_data_lock, a single call to file_data_unlock will unlock the FileData.
834 void file_data_lock(FileData *fd)
836 if (fd == nullptr) return;
837 if (fd->magick != FD_MAGICK) log_printf("Error: fd magick mismatch fd=%p", (void *)fd);
839 g_assert(fd->magick == FD_MAGICK);
842 DEBUG_2("file_data_ref fd=%p (%d): '%s'", (void *)fd, fd->ref, fd->path);
846 * @brief Reset the maintain-FileData-in-memory lock
848 * This again allows the FileData to be freed when its refcount drops to zero. Automatically frees
849 * the FileData if its refcount is already zero (which will happen if the lock is the only thing
850 * keeping it from being freed.
852 void file_data_unlock(FileData *fd)
854 if (fd == nullptr) return;
855 if (fd->magick != FD_MAGICK) log_printf("Error: fd magick mismatch fd=%p", (void *)fd);
857 g_assert(fd->magick == FD_MAGICK);
860 // Free FileData if it's no longer ref'd
861 file_data_consider_free(fd);
865 * @brief Lock all of the FileDatas in the provided list
867 * @see file_data_lock(#FileData)
869 void file_data_lock_list(GList *list)
876 auto fd = static_cast<FileData *>(work->data);
883 * @brief Unlock all of the FileDatas in the provided list
885 * @see #file_data_unlock(#FileData)
887 void file_data_unlock_list(GList *list)
894 auto fd = static_cast<FileData *>(work->data);
896 file_data_unlock(fd);
901 *-----------------------------------------------------------------------------
902 * sidecar file info struct
903 *-----------------------------------------------------------------------------
906 static gint file_data_sort_by_ext(gconstpointer a, gconstpointer b)
908 auto fda = static_cast<const FileData *>(a);
909 auto fdb = static_cast<const FileData *>(b);
911 if (fda->sidecar_priority < fdb->sidecar_priority) return -1;
912 if (fda->sidecar_priority > fdb->sidecar_priority) return 1;
914 return strcmp(fdb->extension, fda->extension);
918 static gint sidecar_file_priority(const gchar *extension)
923 if (extension == nullptr)
926 work = sidecar_ext_get_list();
929 auto ext = static_cast<gchar *>(work->data);
932 if (g_ascii_strcasecmp(extension, ext) == 0) return i;
938 static void file_data_check_sidecars(const GList *basename_list)
940 /* basename_list contains the new group - first is the parent, then sorted sidecars */
941 /* all files in the list have ref count > 0 */
944 GList *s_work, *new_sidecars;
947 if (!basename_list) return;
950 DEBUG_2("basename start");
951 work = basename_list;
954 auto fd = static_cast<FileData *>(work->data);
956 g_assert(fd->magick == FD_MAGICK);
957 DEBUG_2("basename: %p %s", (void *)fd, fd->name);
960 g_assert(fd->parent->magick == FD_MAGICK);
961 DEBUG_2(" parent: %p", (void *)fd->parent);
963 s_work = fd->sidecar_files;
966 auto sfd = static_cast<FileData *>(s_work->data);
967 s_work = s_work->next;
968 g_assert(sfd->magick == FD_MAGICK);
969 DEBUG_2(" sidecar: %p %s", (void *)sfd, sfd->name);
972 g_assert(fd->parent == nullptr || fd->sidecar_files == nullptr);
975 parent_fd = static_cast<FileData *>(basename_list->data);
977 /* check if the second and next entries of basename_list are already connected
978 as sidecars of the first entry (parent_fd) */
979 work = basename_list->next;
980 s_work = parent_fd->sidecar_files;
982 while (work && s_work)
984 if (work->data != s_work->data) break;
986 s_work = s_work->next;
989 if (!work && !s_work)
991 DEBUG_2("basename no change");
992 return; /* no change in grouping */
995 /* we have to regroup it */
997 /* first, disconnect everything and send notification*/
999 work = basename_list;
1002 auto fd = static_cast<FileData *>(work->data);
1004 g_assert(fd->parent == nullptr || fd->sidecar_files == nullptr);
1008 FileData *old_parent = fd->parent;
1009 g_assert(old_parent->parent == nullptr || old_parent->sidecar_files == nullptr);
1010 file_data_ref(old_parent);
1011 file_data_disconnect_sidecar_file(old_parent, fd);
1012 file_data_send_notification(old_parent, NOTIFY_REREAD);
1013 file_data_unref(old_parent);
1016 while (fd->sidecar_files)
1018 auto sfd = static_cast<FileData *>(fd->sidecar_files->data);
1019 g_assert(sfd->parent == nullptr || sfd->sidecar_files == nullptr);
1021 file_data_disconnect_sidecar_file(fd, sfd);
1022 file_data_send_notification(sfd, NOTIFY_REREAD);
1023 file_data_unref(sfd);
1025 file_data_send_notification(fd, NOTIFY_GROUPING);
1027 g_assert(fd->parent == nullptr && fd->sidecar_files == nullptr);
1030 /* now we can form the new group */
1031 work = basename_list->next;
1032 new_sidecars = nullptr;
1035 auto sfd = static_cast<FileData *>(work->data);
1036 g_assert(sfd->magick == FD_MAGICK);
1037 g_assert(sfd->parent == nullptr && sfd->sidecar_files == nullptr);
1038 sfd->parent = parent_fd;
1039 new_sidecars = g_list_prepend(new_sidecars, sfd);
1042 g_assert(parent_fd->sidecar_files == nullptr);
1043 parent_fd->sidecar_files = g_list_reverse(new_sidecars);
1044 DEBUG_1("basename group changed for %s", parent_fd->path);
1048 static void file_data_disconnect_sidecar_file(FileData *target, FileData *sfd)
1050 g_assert(target->magick == FD_MAGICK);
1051 g_assert(sfd->magick == FD_MAGICK);
1052 g_assert(g_list_find(target->sidecar_files, sfd));
1054 file_data_ref(target);
1057 g_assert(sfd->parent == target);
1059 file_data_increment_version(sfd); /* increments both sfd and target */
1061 target->sidecar_files = g_list_remove(target->sidecar_files, sfd);
1062 sfd->parent = nullptr;
1063 g_free(sfd->extended_extension);
1064 sfd->extended_extension = nullptr;
1066 file_data_unref(target);
1067 file_data_unref(sfd);
1070 /* disables / enables grouping for particular file, sends UPDATE notification */
1071 void file_data_disable_grouping(FileData *fd, gboolean disable)
1073 if (!fd->disable_grouping == !disable) return;
1075 fd->disable_grouping = !!disable;
1081 FileData *parent = file_data_ref(fd->parent);
1082 file_data_disconnect_sidecar_file(parent, fd);
1083 file_data_send_notification(parent, NOTIFY_GROUPING);
1084 file_data_unref(parent);
1086 else if (fd->sidecar_files)
1088 GList *sidecar_files = filelist_copy(fd->sidecar_files);
1089 GList *work = sidecar_files;
1092 auto sfd = static_cast<FileData *>(work->data);
1094 file_data_disconnect_sidecar_file(fd, sfd);
1095 file_data_send_notification(sfd, NOTIFY_GROUPING);
1097 file_data_check_sidecars(sidecar_files); /* this will group the sidecars back together */
1098 filelist_free(sidecar_files);
1102 file_data_increment_version(fd); /* the functions called in the cases above increments the version too */
1107 file_data_increment_version(fd);
1108 /* file_data_check_sidecars call is not necessary - the file will be re-grouped on next dir read */
1110 file_data_send_notification(fd, NOTIFY_GROUPING);
1113 void file_data_disable_grouping_list(GList *fd_list, gboolean disable)
1120 auto fd = static_cast<FileData *>(work->data);
1122 file_data_disable_grouping(fd, disable);
1130 *-----------------------------------------------------------------------------
1132 *-----------------------------------------------------------------------------
1136 gint filelist_sort_compare_filedata(FileData *fa, FileData *fb)
1139 if (!filelist_sort_ascend)
1146 switch (filelist_sort_method)
1151 if (fa->size < fb->size) return -1;
1152 if (fa->size > fb->size) return 1;
1153 /* fall back to name */
1156 if (fa->date < fb->date) return -1;
1157 if (fa->date > fb->date) return 1;
1158 /* fall back to name */
1161 if (fa->cdate < fb->cdate) return -1;
1162 if (fa->cdate > fb->cdate) return 1;
1163 /* fall back to name */
1166 if (fa->exifdate < fb->exifdate) return -1;
1167 if (fa->exifdate > fb->exifdate) return 1;
1168 /* fall back to name */
1170 case SORT_EXIFTIMEDIGITIZED:
1171 if (fa->exifdate_digitized < fb->exifdate_digitized) return -1;
1172 if (fa->exifdate_digitized > fb->exifdate_digitized) return 1;
1173 /* fall back to name */
1176 if (fa->rating < fb->rating) return -1;
1177 if (fa->rating > fb->rating) return 1;
1178 /* fall back to name */
1181 if (fa->format_class < fb->format_class) return -1;
1182 if (fa->format_class > fb->format_class) return 1;
1183 /* fall back to name */
1186 ret = strcmp(fa->collate_key_name_natural, fb->collate_key_name_natural);
1187 if (ret != 0) return ret;
1188 /* fall back to name */
1194 if (filelist_sort_case)
1195 ret = strcmp(fa->collate_key_name, fb->collate_key_name);
1197 ret = strcmp(fa->collate_key_name_nocase, fb->collate_key_name_nocase);
1199 if (ret != 0) return ret;
1201 /* do not return 0 unless the files are really the same
1202 file_data_pool ensures that original_path is unique
1204 return strcmp(fa->original_path, fb->original_path);
1207 gint filelist_sort_compare_filedata_full(FileData *fa, FileData *fb, SortType method, gboolean ascend)
1209 filelist_sort_method = method;
1210 filelist_sort_ascend = ascend;
1211 return filelist_sort_compare_filedata(fa, fb);
1214 static gint filelist_sort_file_cb(gpointer a, gpointer b)
1216 return filelist_sort_compare_filedata(static_cast<FileData *>(a), static_cast<FileData *>(b));
1219 GList *filelist_sort_full(GList *list, SortType method, gboolean ascend, gboolean case_sensitive, GCompareFunc cb)
1221 filelist_sort_method = method;
1222 filelist_sort_ascend = ascend;
1223 filelist_sort_case = case_sensitive;
1224 return g_list_sort(list, cb);
1227 GList *filelist_insert_sort_full(GList *list, gpointer data, SortType method, gboolean ascend, gboolean case_sensitive, GCompareFunc cb)
1229 filelist_sort_method = method;
1230 filelist_sort_ascend = ascend;
1231 filelist_sort_case = case_sensitive;
1232 return g_list_insert_sorted(list, data, cb);
1235 GList *filelist_sort(GList *list, SortType method, gboolean ascend, gboolean case_sensitive)
1237 return filelist_sort_full(list, method, ascend, case_sensitive, reinterpret_cast<GCompareFunc>(filelist_sort_file_cb));
1240 #pragma GCC diagnostic push
1241 #pragma GCC diagnostic ignored "-Wunused-function"
1242 GList *filelist_insert_sort_unused(GList *list, FileData *fd, SortType method, gboolean ascend)
1244 return filelist_insert_sort_full(list, fd, method, ascend, ascend, (GCompareFunc) filelist_sort_file_cb);
1246 #pragma GCC diagnostic pop
1249 *-----------------------------------------------------------------------------
1250 * basename hash - grouping of sidecars in filelist
1251 *-----------------------------------------------------------------------------
1255 static GHashTable *file_data_basename_hash_new()
1257 return g_hash_table_new_full(g_str_hash, g_str_equal, g_free, nullptr);
1260 static GList * file_data_basename_hash_insert(GHashTable *basename_hash, FileData *fd)
1263 gchar *basename = g_strndup(fd->path, fd->extension - fd->path);
1265 list = static_cast<GList *>(g_hash_table_lookup(basename_hash, basename));
1269 DEBUG_1("TG: basename_hash not found for %s",fd->path);
1270 const gchar *parent_extension = registered_extension_from_path(basename);
1272 if (parent_extension)
1274 DEBUG_1("TG: parent extension %s",parent_extension);
1275 gchar *parent_basename = g_strndup(basename, parent_extension - basename);
1276 DEBUG_1("TG: parent basename %s",parent_basename);
1277 auto parent_fd = static_cast<FileData *>(g_hash_table_lookup(file_data_pool, basename));
1280 DEBUG_1("TG: parent fd found");
1281 list = static_cast<GList *>(g_hash_table_lookup(basename_hash, parent_basename));
1282 if (!g_list_find(list, parent_fd))
1284 DEBUG_1("TG: parent fd doesn't fit");
1285 g_free(parent_basename);
1291 basename = parent_basename;
1292 fd->extended_extension = g_strconcat(parent_extension, fd->extension, NULL);
1298 if (!g_list_find(list, fd))
1300 list = g_list_insert_sorted(list, file_data_ref(fd), file_data_sort_by_ext);
1301 g_hash_table_insert(basename_hash, basename, list);
1310 static void file_data_basename_hash_insert_cb(gpointer fd, gpointer basename_hash)
1312 file_data_basename_hash_insert(static_cast<GHashTable *>(basename_hash), static_cast<FileData *>(fd));
1315 static void file_data_basename_hash_remove_list(gpointer, gpointer value, gpointer)
1317 filelist_free(static_cast<GList *>(value));
1320 static void file_data_basename_hash_free(GHashTable *basename_hash)
1322 g_hash_table_foreach(basename_hash, file_data_basename_hash_remove_list, nullptr);
1323 g_hash_table_destroy(basename_hash);
1327 *-----------------------------------------------------------------------------
1328 * handling sidecars in filelist
1329 *-----------------------------------------------------------------------------
1332 static GList *filelist_filter_out_sidecars(GList *flist)
1334 GList *work = flist;
1335 GList *flist_filtered = nullptr;
1339 auto fd = static_cast<FileData *>(work->data);
1342 if (fd->parent) /* remove fd's that are children */
1343 file_data_unref(fd);
1345 flist_filtered = g_list_prepend(flist_filtered, fd);
1349 return flist_filtered;
1352 static void file_data_basename_hash_to_sidecars(gpointer, gpointer value, gpointer)
1354 auto basename_list = static_cast<GList *>(value);
1355 file_data_check_sidecars(basename_list);
1359 static gboolean is_hidden_file(const gchar *name)
1361 if (name[0] != '.') return FALSE;
1362 if (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) return FALSE;
1367 *-----------------------------------------------------------------------------
1368 * the main filelist function
1369 *-----------------------------------------------------------------------------
1372 static gboolean filelist_read_real(const gchar *dir_path, GList **files, GList **dirs, gboolean follow_symlinks)
1377 GList *dlist = nullptr;
1378 GList *flist = nullptr;
1379 GList *xmp_files = nullptr;
1380 gint (*stat_func)(const gchar *path, struct stat *buf);
1381 GHashTable *basename_hash = nullptr;
1383 g_assert(files || dirs);
1385 if (files) *files = nullptr;
1386 if (dirs) *dirs = nullptr;
1388 pathl = path_from_utf8(dir_path);
1389 if (!pathl) return FALSE;
1391 dp = opendir(pathl);
1398 if (files) basename_hash = file_data_basename_hash_new();
1400 if (follow_symlinks)
1405 while ((dir = readdir(dp)) != nullptr)
1407 struct stat ent_sbuf;
1408 const gchar *name = dir->d_name;
1411 if (!options->file_filter.show_hidden_files && is_hidden_file(name))
1414 filepath = g_build_filename(pathl, name, NULL);
1415 if (stat_func(filepath, &ent_sbuf) >= 0)
1417 if (S_ISDIR(ent_sbuf.st_mode))
1419 /* we ignore the .thumbnails dir for cleanliness */
1421 (name[0] != '.' || (name[1] != '\0' && (name[1] != '.' || name[2] != '\0'))) &&
1422 strcmp(name, GQ_CACHE_LOCAL_THUMB) != 0 &&
1423 strcmp(name, GQ_CACHE_LOCAL_METADATA) != 0 &&
1424 strcmp(name, THUMB_FOLDER_LOCAL) != 0)
1426 dlist = g_list_prepend(dlist, file_data_new_local(filepath, &ent_sbuf, TRUE));
1431 if (files && filter_name_exists(name))
1433 FileData *fd = file_data_new_local(filepath, &ent_sbuf, FALSE);
1434 flist = g_list_prepend(flist, fd);
1435 if (fd->sidecar_priority && !fd->disable_grouping)
1437 if (strcmp(fd->extension, ".xmp") != 0)
1438 file_data_basename_hash_insert(basename_hash, fd);
1440 xmp_files = g_list_append(xmp_files, fd);
1447 if (errno == EOVERFLOW)
1449 log_printf("stat(): EOVERFLOW, skip '%s'", filepath);
1461 g_list_foreach(xmp_files,file_data_basename_hash_insert_cb,basename_hash);
1462 g_list_free(xmp_files);
1465 if (dirs) *dirs = dlist;
1469 g_hash_table_foreach(basename_hash, file_data_basename_hash_to_sidecars, nullptr);
1471 *files = filelist_filter_out_sidecars(flist);
1473 if (basename_hash) file_data_basename_hash_free(basename_hash);
1478 gboolean filelist_read(FileData *dir_fd, GList **files, GList **dirs)
1480 return filelist_read_real(dir_fd->path, files, dirs, TRUE);
1483 gboolean filelist_read_lstat(FileData *dir_fd, GList **files, GList **dirs)
1485 return filelist_read_real(dir_fd->path, files, dirs, FALSE);
1488 FileData *file_data_new_group(const gchar *path_utf8)
1495 if (!file_data_pool)
1497 file_data_pool = g_hash_table_new(g_str_hash, g_str_equal);
1500 if (!stat_utf8(path_utf8, &st))
1506 if (S_ISDIR(st.st_mode))
1507 return file_data_new(path_utf8, &st, TRUE);
1509 dir = remove_level_from_path(path_utf8);
1511 filelist_read_real(dir, &files, nullptr, TRUE);
1513 fd = static_cast<FileData *>(g_hash_table_lookup(file_data_pool, path_utf8));
1514 if (!fd) fd = file_data_new(path_utf8, &st, TRUE);
1520 filelist_free(files);
1526 void filelist_free(GList *list)
1533 file_data_unref((FileData *)work->data);
1541 GList *filelist_copy(GList *list)
1543 GList *new_list = nullptr;
1551 fd = static_cast<FileData *>(work->data);
1554 new_list = g_list_prepend(new_list, file_data_ref(fd));
1557 return g_list_reverse(new_list);
1560 GList *filelist_from_path_list(GList *list)
1562 GList *new_list = nullptr;
1570 path = static_cast<gchar *>(work->data);
1573 new_list = g_list_prepend(new_list, file_data_new_group(path));
1576 return g_list_reverse(new_list);
1579 GList *filelist_to_path_list(GList *list)
1581 GList *new_list = nullptr;
1589 fd = static_cast<FileData *>(work->data);
1592 new_list = g_list_prepend(new_list, g_strdup(fd->path));
1595 return g_list_reverse(new_list);
1598 GList *filelist_filter(GList *list, gboolean is_dir_list)
1602 if (!is_dir_list && options->file_filter.disable && options->file_filter.show_hidden_files) return list;
1607 auto fd = static_cast<FileData *>(work->data);
1608 const gchar *name = fd->name;
1612 if ((!options->file_filter.show_hidden_files && is_hidden_file(name)) ||
1613 (!is_dir_list && !filter_name_exists(name)) ||
1614 (is_dir_list && name[0] == '.' && (strcmp(name, GQ_CACHE_LOCAL_THUMB) == 0 ||
1615 strcmp(name, GQ_CACHE_LOCAL_METADATA) == 0)) )
1617 list = g_list_remove_link(list, link);
1618 file_data_unref(fd);
1627 *-----------------------------------------------------------------------------
1628 * filelist recursive
1629 *-----------------------------------------------------------------------------
1632 static gint filelist_sort_path_cb(gconstpointer a, gconstpointer b)
1634 return CASE_SORT(((FileData *)a)->path, ((FileData *)b)->path);
1637 GList *filelist_sort_path(GList *list)
1639 return g_list_sort(list, filelist_sort_path_cb);
1642 static void filelist_recursive_append(GList **list, GList *dirs)
1649 auto fd = static_cast<FileData *>(work->data);
1653 if (filelist_read(fd, &f, &d))
1655 f = filelist_filter(f, FALSE);
1656 f = filelist_sort_path(f);
1657 *list = g_list_concat(*list, f);
1659 d = filelist_filter(d, TRUE);
1660 d = filelist_sort_path(d);
1661 filelist_recursive_append(list, d);
1669 static void filelist_recursive_append_full(GList **list, GList *dirs, SortType method, gboolean ascend, gboolean case_sensitive)
1676 auto fd = static_cast<FileData *>(work->data);
1680 if (filelist_read(fd, &f, &d))
1682 f = filelist_filter(f, FALSE);
1683 f = filelist_sort_full(f, method, ascend, case_sensitive, reinterpret_cast<GCompareFunc>(filelist_sort_file_cb));
1684 *list = g_list_concat(*list, f);
1686 d = filelist_filter(d, TRUE);
1687 d = filelist_sort_path(d);
1688 filelist_recursive_append_full(list, d, method, ascend, case_sensitive);
1696 GList *filelist_recursive(FileData *dir_fd)
1701 if (!filelist_read(dir_fd, &list, &d)) return nullptr;
1702 list = filelist_filter(list, FALSE);
1703 list = filelist_sort_path(list);
1705 d = filelist_filter(d, TRUE);
1706 d = filelist_sort_path(d);
1707 filelist_recursive_append(&list, d);
1713 GList *filelist_recursive_full(FileData *dir_fd, SortType method, gboolean ascend, gboolean case_sensitive)
1718 if (!filelist_read(dir_fd, &list, &d)) return nullptr;
1719 list = filelist_filter(list, FALSE);
1720 list = filelist_sort_full(list, method, ascend, case_sensitive, reinterpret_cast<GCompareFunc>(filelist_sort_file_cb));
1722 d = filelist_filter(d, TRUE);
1723 d = filelist_sort_path(d);
1724 filelist_recursive_append_full(&list, d, method, ascend, case_sensitive);
1731 *-----------------------------------------------------------------------------
1732 * file modification support
1733 *-----------------------------------------------------------------------------
1737 void file_data_change_info_free(FileDataChangeInfo *fdci, FileData *fd)
1739 if (!fdci && fd) fdci = fd->change;
1743 g_free(fdci->source);
1748 if (fd) fd->change = nullptr;
1751 static gboolean file_data_can_write_directly(FileData *fd)
1753 return filter_name_is_writable(fd->extension);
1756 static gboolean file_data_can_write_sidecar(FileData *fd)
1758 return filter_name_allow_sidecar(fd->extension) && !filter_name_is_writable(fd->extension);
1761 gchar *file_data_get_sidecar_path(FileData *fd, gboolean existing_only)
1763 gchar *sidecar_path = nullptr;
1766 if (!file_data_can_write_sidecar(fd)) return nullptr;
1768 work = fd->parent ? fd->parent->sidecar_files : fd->sidecar_files;
1769 gchar *extended_extension = g_strconcat(fd->parent ? fd->parent->extension : fd->extension, ".xmp", NULL);
1772 auto sfd = static_cast<FileData *>(work->data);
1774 if (g_ascii_strcasecmp(sfd->extension, ".xmp") == 0 || g_ascii_strcasecmp(sfd->extension, extended_extension) == 0)
1776 sidecar_path = g_strdup(sfd->path);
1780 g_free(extended_extension);
1782 if (!existing_only && !sidecar_path)
1784 if (options->metadata.sidecar_extended_name)
1785 sidecar_path = g_strconcat(fd->path, ".xmp", NULL);
1788 gchar *base = g_strndup(fd->path, fd->extension - fd->path);
1789 sidecar_path = g_strconcat(base, ".xmp", NULL);
1794 return sidecar_path;
1798 * marks and orientation
1801 static FileDataGetMarkFunc file_data_get_mark_func[FILEDATA_MARKS_SIZE];
1802 static FileDataSetMarkFunc file_data_set_mark_func[FILEDATA_MARKS_SIZE];
1803 static gpointer file_data_mark_func_data[FILEDATA_MARKS_SIZE];
1804 static GDestroyNotify file_data_destroy_mark_func[FILEDATA_MARKS_SIZE];
1806 gboolean file_data_get_mark(FileData *fd, gint n)
1808 gboolean valid = (fd->valid_marks & (1 << n));
1810 if (file_data_get_mark_func[n] && !valid)
1812 guint old = fd->marks;
1813 gboolean value = (file_data_get_mark_func[n])(fd, n, file_data_mark_func_data[n]);
1815 if (!value != !(fd->marks & (1 << n)))
1817 fd->marks = fd->marks ^ (1 << n);
1820 fd->valid_marks |= (1 << n);
1821 if (old && !fd->marks) /* keep files with non-zero marks in memory */
1823 file_data_unref(fd);
1825 else if (!old && fd->marks)
1831 return !!(fd->marks & (1 << n));
1834 guint file_data_get_marks(FileData *fd)
1837 for (i = 0; i < FILEDATA_MARKS_SIZE; i++) file_data_get_mark(fd, i);
1841 void file_data_set_mark(FileData *fd, gint n, gboolean value)
1844 if (!value == !file_data_get_mark(fd, n)) return;
1846 if (file_data_set_mark_func[n])
1848 (file_data_set_mark_func[n])(fd, n, value, file_data_mark_func_data[n]);
1853 fd->marks = fd->marks ^ (1 << n);
1855 if (old && !fd->marks) /* keep files with non-zero marks in memory */
1857 file_data_unref(fd);
1859 else if (!old && fd->marks)
1864 file_data_increment_version(fd);
1865 file_data_send_notification(fd, NOTIFY_MARKS);
1868 gboolean file_data_filter_marks(FileData *fd, guint filter)
1871 for (i = 0; i < FILEDATA_MARKS_SIZE; i++) if (filter & (1 << i)) file_data_get_mark(fd, i);
1872 return ((fd->marks & filter) == filter);
1875 GList *file_data_filter_marks_list(GList *list, guint filter)
1882 auto fd = static_cast<FileData *>(work->data);
1886 if (!file_data_filter_marks(fd, filter))
1888 list = g_list_remove_link(list, link);
1889 file_data_unref(fd);
1897 gboolean file_data_filter_file_filter(FileData *fd, GRegex *filter)
1899 return g_regex_match(filter, fd->name, static_cast<GRegexMatchFlags>(0), nullptr);
1902 GList *file_data_filter_file_filter_list(GList *list, GRegex *filter)
1909 auto fd = static_cast<FileData *>(work->data);
1913 if (!file_data_filter_file_filter(fd, filter))
1915 list = g_list_remove_link(list, link);
1916 file_data_unref(fd);
1924 static gboolean file_data_filter_class(FileData *fd, guint filter)
1928 for (i = 0; i < FILE_FORMAT_CLASSES; i++)
1930 if (filter & (1 << i))
1932 if (static_cast<FileFormatClass>(i) == filter_file_get_class(fd->path))
1942 GList *file_data_filter_class_list(GList *list, guint filter)
1949 auto fd = static_cast<FileData *>(work->data);
1953 if (!file_data_filter_class(fd, filter))
1955 list = g_list_remove_link(list, link);
1956 file_data_unref(fd);
1964 static void file_data_notify_mark_func(gpointer, gpointer value, gpointer)
1966 auto fd = static_cast<FileData *>(value);
1967 file_data_increment_version(fd);
1968 file_data_send_notification(fd, NOTIFY_MARKS);
1971 gboolean file_data_register_mark_func(gint n, FileDataGetMarkFunc get_mark_func, FileDataSetMarkFunc set_mark_func, gpointer data, GDestroyNotify notify)
1973 if (n < 0 || n >= FILEDATA_MARKS_SIZE) return FALSE;
1975 if (file_data_destroy_mark_func[n]) (file_data_destroy_mark_func[n])(file_data_mark_func_data[n]);
1977 file_data_get_mark_func[n] = get_mark_func;
1978 file_data_set_mark_func[n] = set_mark_func;
1979 file_data_mark_func_data[n] = data;
1980 file_data_destroy_mark_func[n] = notify;
1982 if (get_mark_func && file_data_pool)
1984 /* this effectively changes all known files */
1985 g_hash_table_foreach(file_data_pool, file_data_notify_mark_func, nullptr);
1991 void file_data_get_registered_mark_func(gint n, FileDataGetMarkFunc *get_mark_func, FileDataSetMarkFunc *set_mark_func, gpointer *data)
1993 if (get_mark_func) *get_mark_func = file_data_get_mark_func[n];
1994 if (set_mark_func) *set_mark_func = file_data_set_mark_func[n];
1995 if (data) *data = file_data_mark_func_data[n];
1998 #pragma GCC diagnostic push
1999 #pragma GCC diagnostic ignored "-Wunused-function"
2000 gint file_data_get_user_orientation_unused(FileData *fd)
2002 return fd->user_orientation;
2005 void file_data_set_user_orientation_unused(FileData *fd, gint value)
2007 if (fd->user_orientation == value) return;
2009 fd->user_orientation = value;
2010 file_data_increment_version(fd);
2011 file_data_send_notification(fd, NOTIFY_ORIENTATION);
2013 #pragma GCC diagnostic pop
2017 * file_data - operates on the given fd
2018 * file_data_sc - operates on the given fd + sidecars - all fds linked via fd->sidecar_files or fd->parent
2022 /* return list of sidecar file extensions in a string */
2023 gchar *file_data_sc_list_to_string(FileData *fd)
2026 GString *result = g_string_new("");
2028 work = fd->sidecar_files;
2031 auto sfd = static_cast<FileData *>(work->data);
2033 result = g_string_append(result, "+ ");
2034 result = g_string_append(result, sfd->extension);
2036 if (work) result = g_string_append_c(result, ' ');
2039 return g_string_free(result, FALSE);
2045 * add FileDataChangeInfo (see typedefs.h) for the given operation
2046 * uses file_data_add_change_info
2048 * fails if the fd->change already exists - change operations can't run in parallel
2049 * fd->change_info works as a lock
2051 * dest can be NULL - in this case the current name is used for now, it will
2056 FileDataChangeInfo types:
2058 MOVE - path is changed, name may be changed too
2059 RENAME - path remains unchanged, name is changed
2060 extension should remain (FIXME should we allow editing extension? it will make problems with grouping)
2061 sidecar names are changed too, extensions are not changed
2063 UPDATE - file size, date or grouping has been changed
2066 gboolean file_data_add_ci(FileData *fd, FileDataChangeType type, const gchar *src, const gchar *dest)
2068 FileDataChangeInfo *fdci;
2070 if (fd->change) return FALSE;
2072 fdci = g_new0(FileDataChangeInfo, 1);
2077 fdci->source = g_strdup(src);
2079 fdci->source = g_strdup(fd->path);
2082 fdci->dest = g_strdup(dest);
2089 static void file_data_planned_change_remove(FileData *fd)
2091 if (file_data_planned_change_hash &&
2092 (fd->change->type == FILEDATA_CHANGE_MOVE || fd->change->type == FILEDATA_CHANGE_RENAME))
2094 if (g_hash_table_lookup(file_data_planned_change_hash, fd->change->dest) == fd)
2096 DEBUG_1("planned change: removing %s -> %s", fd->change->dest, fd->path);
2097 g_hash_table_remove(file_data_planned_change_hash, fd->change->dest);
2098 file_data_unref(fd);
2099 if (g_hash_table_size(file_data_planned_change_hash) == 0)
2101 g_hash_table_destroy(file_data_planned_change_hash);
2102 file_data_planned_change_hash = nullptr;
2103 DEBUG_1("planned change: empty");
2110 void file_data_free_ci(FileData *fd)
2112 FileDataChangeInfo *fdci = fd->change;
2116 file_data_planned_change_remove(fd);
2118 if (fdci->regroup_when_finished) file_data_disable_grouping(fd, FALSE);
2120 g_free(fdci->source);
2125 fd->change = nullptr;
2128 void file_data_set_regroup_when_finished(FileData *fd, gboolean enable)
2130 FileDataChangeInfo *fdci = fd->change;
2132 fdci->regroup_when_finished = enable;
2135 static gboolean file_data_sc_add_ci(FileData *fd, FileDataChangeType type)
2139 if (fd->parent) fd = fd->parent;
2141 if (fd->change) return FALSE;
2143 work = fd->sidecar_files;
2146 auto sfd = static_cast<FileData *>(work->data);
2148 if (sfd->change) return FALSE;
2152 file_data_add_ci(fd, type, nullptr, nullptr);
2154 work = fd->sidecar_files;
2157 auto sfd = static_cast<FileData *>(work->data);
2159 file_data_add_ci(sfd, type, nullptr, nullptr);
2166 static gboolean file_data_sc_check_ci(FileData *fd, FileDataChangeType type)
2170 if (fd->parent) fd = fd->parent;
2172 if (!fd->change || fd->change->type != type) return FALSE;
2174 work = fd->sidecar_files;
2177 auto sfd = static_cast<FileData *>(work->data);
2179 if (!sfd->change || sfd->change->type != type) return FALSE;
2187 gboolean file_data_sc_add_ci_copy(FileData *fd, const gchar *dest_path)
2189 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_COPY)) return FALSE;
2190 file_data_sc_update_ci_copy(fd, dest_path);
2194 gboolean file_data_sc_add_ci_move(FileData *fd, const gchar *dest_path)
2196 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_MOVE)) return FALSE;
2197 file_data_sc_update_ci_move(fd, dest_path);
2201 gboolean file_data_sc_add_ci_rename(FileData *fd, const gchar *dest_path)
2203 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_RENAME)) return FALSE;
2204 file_data_sc_update_ci_rename(fd, dest_path);
2208 gboolean file_data_sc_add_ci_delete(FileData *fd)
2210 return file_data_sc_add_ci(fd, FILEDATA_CHANGE_DELETE);
2213 gboolean file_data_sc_add_ci_unspecified(FileData *fd, const gchar *dest_path)
2215 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_UNSPECIFIED)) return FALSE;
2216 file_data_sc_update_ci_unspecified(fd, dest_path);
2220 gboolean file_data_add_ci_write_metadata(FileData *fd)
2222 return file_data_add_ci(fd, FILEDATA_CHANGE_WRITE_METADATA, nullptr, nullptr);
2225 void file_data_sc_free_ci(FileData *fd)
2229 if (fd->parent) fd = fd->parent;
2231 file_data_free_ci(fd);
2233 work = fd->sidecar_files;
2236 auto sfd = static_cast<FileData *>(work->data);
2238 file_data_free_ci(sfd);
2243 gboolean file_data_sc_add_ci_delete_list(GList *fd_list)
2246 gboolean ret = TRUE;
2251 auto fd = static_cast<FileData *>(work->data);
2253 if (!file_data_sc_add_ci_delete(fd)) ret = FALSE;
2260 static void file_data_sc_revert_ci_list(GList *fd_list)
2267 auto fd = static_cast<FileData *>(work->data);
2269 file_data_sc_free_ci(fd);
2274 static gboolean file_data_sc_add_ci_list_call_func(GList *fd_list, const gchar *dest, gboolean (*func)(FileData *, const gchar *))
2281 auto fd = static_cast<FileData *>(work->data);
2283 if (!func(fd, dest))
2285 file_data_sc_revert_ci_list(work->prev);
2294 gboolean file_data_sc_add_ci_copy_list(GList *fd_list, const gchar *dest)
2296 return file_data_sc_add_ci_list_call_func(fd_list, dest, file_data_sc_add_ci_copy);
2299 gboolean file_data_sc_add_ci_move_list(GList *fd_list, const gchar *dest)
2301 return file_data_sc_add_ci_list_call_func(fd_list, dest, file_data_sc_add_ci_move);
2304 gboolean file_data_sc_add_ci_rename_list(GList *fd_list, const gchar *dest)
2306 return file_data_sc_add_ci_list_call_func(fd_list, dest, file_data_sc_add_ci_rename);
2309 gboolean file_data_sc_add_ci_unspecified_list(GList *fd_list, const gchar *dest)
2311 return file_data_sc_add_ci_list_call_func(fd_list, dest, file_data_sc_add_ci_unspecified);
2314 gboolean file_data_add_ci_write_metadata_list(GList *fd_list)
2317 gboolean ret = TRUE;
2322 auto fd = static_cast<FileData *>(work->data);
2324 if (!file_data_add_ci_write_metadata(fd)) ret = FALSE;
2331 void file_data_free_ci_list(GList *fd_list)
2338 auto fd = static_cast<FileData *>(work->data);
2340 file_data_free_ci(fd);
2345 void file_data_sc_free_ci_list(GList *fd_list)
2352 auto fd = static_cast<FileData *>(work->data);
2354 file_data_sc_free_ci(fd);
2360 * update existing fd->change, it will be used from dialog callbacks for interactive editing
2361 * fails if fd->change does not exist or the change type does not match
2364 static void file_data_update_planned_change_hash(FileData *fd, const gchar *old_path, gchar *new_path)
2366 FileDataChangeType type = fd->change->type;
2368 if (type == FILEDATA_CHANGE_MOVE || type == FILEDATA_CHANGE_RENAME)
2372 if (!file_data_planned_change_hash)
2373 file_data_planned_change_hash = g_hash_table_new(g_str_hash, g_str_equal);
2375 if (old_path && g_hash_table_lookup(file_data_planned_change_hash, old_path) == fd)
2377 DEBUG_1("planned change: removing %s -> %s", old_path, fd->path);
2378 g_hash_table_remove(file_data_planned_change_hash, old_path);
2379 file_data_unref(fd);
2382 ofd = static_cast<FileData *>(g_hash_table_lookup(file_data_planned_change_hash, new_path));
2387 DEBUG_1("planned change: replacing %s -> %s", new_path, ofd->path);
2388 g_hash_table_remove(file_data_planned_change_hash, new_path);
2389 file_data_unref(ofd);
2392 DEBUG_1("planned change: inserting %s -> %s", new_path, fd->path);
2394 g_hash_table_insert(file_data_planned_change_hash, new_path, fd);
2399 static void file_data_update_ci_dest(FileData *fd, const gchar *dest_path)
2401 gchar *old_path = fd->change->dest;
2403 fd->change->dest = g_strdup(dest_path);
2404 file_data_update_planned_change_hash(fd, old_path, fd->change->dest);
2408 static void file_data_update_ci_dest_preserve_ext(FileData *fd, const gchar *dest_path)
2410 const gchar *extension = registered_extension_from_path(fd->change->source);
2411 gchar *base = remove_extension_from_path(dest_path);
2412 gchar *old_path = fd->change->dest;
2414 fd->change->dest = g_strconcat(base, fd->extended_extension ? fd->extended_extension : extension, NULL);
2415 file_data_update_planned_change_hash(fd, old_path, fd->change->dest);
2421 static void file_data_sc_update_ci(FileData *fd, const gchar *dest_path)
2424 gchar *dest_path_full = nullptr;
2426 if (fd->parent) fd = fd->parent;
2430 dest_path = fd->path;
2432 else if (!strchr(dest_path, G_DIR_SEPARATOR)) /* we got only filename, not a full path */
2434 gchar *dir = remove_level_from_path(fd->path);
2436 dest_path_full = g_build_filename(dir, dest_path, NULL);
2438 dest_path = dest_path_full;
2440 else if (fd->change->type != FILEDATA_CHANGE_RENAME && isdir(dest_path)) /* rename should not move files between directories */
2442 dest_path_full = g_build_filename(dest_path, fd->name, NULL);
2443 dest_path = dest_path_full;
2446 file_data_update_ci_dest(fd, dest_path);
2448 work = fd->sidecar_files;
2451 auto sfd = static_cast<FileData *>(work->data);
2453 file_data_update_ci_dest_preserve_ext(sfd, dest_path);
2457 g_free(dest_path_full);
2460 static gboolean file_data_sc_check_update_ci(FileData *fd, const gchar *dest_path, FileDataChangeType type)
2462 if (!file_data_sc_check_ci(fd, type)) return FALSE;
2463 file_data_sc_update_ci(fd, dest_path);
2467 gboolean file_data_sc_update_ci_copy(FileData *fd, const gchar *dest_path)
2469 return file_data_sc_check_update_ci(fd, dest_path, FILEDATA_CHANGE_COPY);
2472 gboolean file_data_sc_update_ci_move(FileData *fd, const gchar *dest_path)
2474 return file_data_sc_check_update_ci(fd, dest_path, FILEDATA_CHANGE_MOVE);
2477 gboolean file_data_sc_update_ci_rename(FileData *fd, const gchar *dest_path)
2479 return file_data_sc_check_update_ci(fd, dest_path, FILEDATA_CHANGE_RENAME);
2482 gboolean file_data_sc_update_ci_unspecified(FileData *fd, const gchar *dest_path)
2484 return file_data_sc_check_update_ci(fd, dest_path, FILEDATA_CHANGE_UNSPECIFIED);
2487 static gboolean file_data_sc_update_ci_list_call_func(GList *fd_list,
2489 gboolean (*func)(FileData *, const gchar *))
2492 gboolean ret = TRUE;
2497 auto fd = static_cast<FileData *>(work->data);
2499 if (!func(fd, dest)) ret = FALSE;
2506 gboolean file_data_sc_update_ci_move_list(GList *fd_list, const gchar *dest)
2508 return file_data_sc_update_ci_list_call_func(fd_list, dest, file_data_sc_update_ci_move);
2511 gboolean file_data_sc_update_ci_copy_list(GList *fd_list, const gchar *dest)
2513 return file_data_sc_update_ci_list_call_func(fd_list, dest, file_data_sc_update_ci_copy);
2516 gboolean file_data_sc_update_ci_unspecified_list(GList *fd_list, const gchar *dest)
2518 return file_data_sc_update_ci_list_call_func(fd_list, dest, file_data_sc_update_ci_unspecified);
2523 * verify source and dest paths - dest image exists, etc.
2524 * it should detect all possible problems with the planned operation
2527 gint file_data_verify_ci(FileData *fd, GList *list)
2529 gint ret = CHANGE_OK;
2531 GList *work = nullptr;
2532 FileData *fd1 = nullptr;
2536 DEBUG_1("Change checked: no change info: %s", fd->path);
2540 if (!isname(fd->path))
2542 /* this probably should not happen */
2543 ret |= CHANGE_NO_SRC;
2544 DEBUG_1("Change checked: file does not exist: %s", fd->path);
2548 dir = remove_level_from_path(fd->path);
2550 if (fd->change->type != FILEDATA_CHANGE_DELETE &&
2551 fd->change->type != FILEDATA_CHANGE_MOVE && /* the unsaved metadata should survive move and rename operations */
2552 fd->change->type != FILEDATA_CHANGE_RENAME &&
2553 fd->change->type != FILEDATA_CHANGE_WRITE_METADATA &&
2556 ret |= CHANGE_WARN_UNSAVED_META;
2557 DEBUG_1("Change checked: unsaved metadata: %s", fd->path);
2560 if (fd->change->type != FILEDATA_CHANGE_DELETE &&
2561 fd->change->type != FILEDATA_CHANGE_WRITE_METADATA &&
2562 !access_file(fd->path, R_OK))
2564 ret |= CHANGE_NO_READ_PERM;
2565 DEBUG_1("Change checked: no read permission: %s", fd->path);
2567 else if ((fd->change->type == FILEDATA_CHANGE_DELETE || fd->change->type == FILEDATA_CHANGE_MOVE) &&
2568 !access_file(dir, W_OK))
2570 ret |= CHANGE_NO_WRITE_PERM_DIR;
2571 DEBUG_1("Change checked: source dir is readonly: %s", fd->path);
2573 else if (fd->change->type != FILEDATA_CHANGE_COPY &&
2574 fd->change->type != FILEDATA_CHANGE_UNSPECIFIED &&
2575 fd->change->type != FILEDATA_CHANGE_WRITE_METADATA &&
2576 !access_file(fd->path, W_OK))
2578 ret |= CHANGE_WARN_NO_WRITE_PERM;
2579 DEBUG_1("Change checked: no write permission: %s", fd->path);
2581 /* WRITE_METADATA is special because it can be configured to silently write to ~/.geeqie/...
2582 - that means that there are no hard errors and warnings can be disabled
2583 - the destination is determined during the check
2585 else if (fd->change->type == FILEDATA_CHANGE_WRITE_METADATA)
2587 /* determine destination file */
2588 gboolean have_dest = FALSE;
2589 gchar *dest_dir = nullptr;
2591 if (options->metadata.save_in_image_file)
2593 if (file_data_can_write_directly(fd))
2595 /* we can write the file directly */
2596 if (access_file(fd->path, W_OK))
2602 if (options->metadata.warn_on_write_problems)
2604 ret |= CHANGE_WARN_NO_WRITE_PERM;
2605 DEBUG_1("Change checked: file is not writable: %s", fd->path);
2609 else if (file_data_can_write_sidecar(fd))
2611 /* we can write sidecar */
2612 gchar *sidecar = file_data_get_sidecar_path(fd, FALSE);
2613 if (access_file(sidecar, W_OK) || (!isname(sidecar) && access_file(dir, W_OK)))
2615 file_data_update_ci_dest(fd, sidecar);
2620 if (options->metadata.warn_on_write_problems)
2622 ret |= CHANGE_WARN_NO_WRITE_PERM;
2623 DEBUG_1("Change checked: file is not writable: %s", sidecar);
2632 /* write private metadata file under ~/.geeqie */
2634 /* If an existing metadata file exists, we will try writing to
2635 * it's location regardless of the user's preference.
2637 gchar *metadata_path = nullptr;
2639 /* but ignore XMP if we are not able to write it */
2640 metadata_path = cache_find_location(CACHE_TYPE_XMP_METADATA, fd->path);
2642 if (!metadata_path) metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
2644 if (metadata_path && !access_file(metadata_path, W_OK))
2646 g_free(metadata_path);
2647 metadata_path = nullptr;
2654 dest_dir = cache_get_location(CACHE_TYPE_METADATA, fd->path, FALSE, &mode);
2655 if (recursive_mkdir_if_not_exists(dest_dir, mode))
2657 gchar *filename = g_strconcat(fd->name, options->metadata.save_legacy_format ? GQ_CACHE_EXT_METADATA : GQ_CACHE_EXT_XMP_METADATA, NULL);
2659 metadata_path = g_build_filename(dest_dir, filename, NULL);
2663 if (access_file(metadata_path, W_OK) || (!isname(metadata_path) && access_file(dest_dir, W_OK)))
2665 file_data_update_ci_dest(fd, metadata_path);
2670 ret |= CHANGE_NO_WRITE_PERM_DEST;
2671 DEBUG_1("Change checked: file is not writable: %s", metadata_path);
2673 g_free(metadata_path);
2678 if (fd->change->dest && fd->change->type != FILEDATA_CHANGE_WRITE_METADATA)
2683 same = (strcmp(fd->path, fd->change->dest) == 0);
2687 const gchar *dest_ext = registered_extension_from_path(fd->change->dest);
2688 if (!dest_ext) dest_ext = "";
2689 if (!options->file_filter.disable_file_extension_checks)
2691 if (g_ascii_strcasecmp(fd->extension, dest_ext) != 0)
2693 ret |= CHANGE_WARN_CHANGED_EXT;
2694 DEBUG_1("Change checked: source and destination have different extensions: %s -> %s", fd->path, fd->change->dest);
2700 if (fd->change->type != FILEDATA_CHANGE_UNSPECIFIED) /** @FIXME this is now needed for running editors */
2702 ret |= CHANGE_WARN_SAME;
2703 DEBUG_1("Change checked: source and destination are the same: %s -> %s", fd->path, fd->change->dest);
2707 dest_dir = remove_level_from_path(fd->change->dest);
2709 if (!isdir(dest_dir))
2711 ret |= CHANGE_NO_DEST_DIR;
2712 DEBUG_1("Change checked: destination dir does not exist: %s -> %s", fd->path, fd->change->dest);
2714 else if (!access_file(dest_dir, W_OK))
2716 ret |= CHANGE_WARN_NO_WRITE_PERM_DEST_DIR;
2717 DEBUG_1("Change checked: destination dir is readonly: %s -> %s", fd->path, fd->change->dest);
2721 if (isfile(fd->change->dest))
2723 if (!access_file(fd->change->dest, W_OK))
2725 ret |= CHANGE_NO_WRITE_PERM_DEST;
2726 DEBUG_1("Change checked: destination file exists and is readonly: %s -> %s", fd->path, fd->change->dest);
2730 ret |= CHANGE_WARN_DEST_EXISTS;
2731 DEBUG_1("Change checked: destination exists: %s -> %s", fd->path, fd->change->dest);
2734 else if (isdir(fd->change->dest))
2736 ret |= CHANGE_DEST_EXISTS;
2737 DEBUG_1("Change checked: destination exists: %s -> %s", fd->path, fd->change->dest);
2744 /* During a rename operation, check if another planned destination file has
2747 if(fd->change->type == FILEDATA_CHANGE_RENAME ||
2748 fd->change->type == FILEDATA_CHANGE_COPY ||
2749 fd->change->type == FILEDATA_CHANGE_MOVE)
2754 fd1 = static_cast<FileData *>(work->data);
2756 if (fd1 != nullptr && fd != fd1 )
2758 if (!strcmp(fd->change->dest, fd1->change->dest))
2760 ret |= CHANGE_DUPLICATE_DEST;
2766 fd->change->error = ret;
2767 if (ret == 0) DEBUG_1("Change checked: OK: %s", fd->path);
2774 gint file_data_sc_verify_ci(FileData *fd, GList *list)
2779 ret = file_data_verify_ci(fd, list);
2781 work = fd->sidecar_files;
2784 auto sfd = static_cast<FileData *>(work->data);
2786 ret |= file_data_verify_ci(sfd, list);
2793 gchar *file_data_get_error_string(gint error)
2795 GString *result = g_string_new("");
2797 if (error & CHANGE_NO_SRC)
2799 if (result->len > 0) g_string_append(result, ", ");
2800 g_string_append(result, _("file or directory does not exist"));
2803 if (error & CHANGE_DEST_EXISTS)
2805 if (result->len > 0) g_string_append(result, ", ");
2806 g_string_append(result, _("destination already exists"));
2809 if (error & CHANGE_NO_WRITE_PERM_DEST)
2811 if (result->len > 0) g_string_append(result, ", ");
2812 g_string_append(result, _("destination can't be overwritten"));
2815 if (error & CHANGE_WARN_NO_WRITE_PERM_DEST_DIR)
2817 if (result->len > 0) g_string_append(result, ", ");
2818 g_string_append(result, _("destination directory is not writable"));
2821 if (error & CHANGE_NO_DEST_DIR)
2823 if (result->len > 0) g_string_append(result, ", ");
2824 g_string_append(result, _("destination directory does not exist"));
2827 if (error & CHANGE_NO_WRITE_PERM_DIR)
2829 if (result->len > 0) g_string_append(result, ", ");
2830 g_string_append(result, _("source directory is not writable"));
2833 if (error & CHANGE_NO_READ_PERM)
2835 if (result->len > 0) g_string_append(result, ", ");
2836 g_string_append(result, _("no read permission"));
2839 if (error & CHANGE_WARN_NO_WRITE_PERM)
2841 if (result->len > 0) g_string_append(result, ", ");
2842 g_string_append(result, _("file is readonly"));
2845 if (error & CHANGE_WARN_DEST_EXISTS)
2847 if (result->len > 0) g_string_append(result, ", ");
2848 g_string_append(result, _("destination already exists and will be overwritten"));
2851 if (error & CHANGE_WARN_SAME)
2853 if (result->len > 0) g_string_append(result, ", ");
2854 g_string_append(result, _("source and destination are the same"));
2857 if (error & CHANGE_WARN_CHANGED_EXT)
2859 if (result->len > 0) g_string_append(result, ", ");
2860 g_string_append(result, _("source and destination have different extension"));
2863 if (error & CHANGE_WARN_UNSAVED_META)
2865 if (result->len > 0) g_string_append(result, ", ");
2866 g_string_append(result, _("there are unsaved metadata changes for the file"));
2869 if (error & CHANGE_DUPLICATE_DEST)
2871 if (result->len > 0) g_string_append(result, ", ");
2872 g_string_append(result, _("another destination file has the same filename"));
2875 return g_string_free(result, FALSE);
2878 gint file_data_verify_ci_list(GList *list, gchar **desc, gboolean with_sidecars)
2881 gint all_errors = 0;
2882 gint common_errors = ~0;
2887 if (!list) return 0;
2889 num = g_list_length(list);
2890 errors = g_new(int, num);
2898 fd = static_cast<FileData *>(work->data);
2901 error = with_sidecars ? file_data_sc_verify_ci(fd, list) : file_data_verify_ci(fd, list);
2902 all_errors |= error;
2903 common_errors &= error;
2910 if (desc && all_errors)
2913 GString *result = g_string_new("");
2917 gchar *str = file_data_get_error_string(common_errors);
2918 g_string_append(result, str);
2919 g_string_append(result, "\n");
2930 fd = static_cast<FileData *>(work->data);
2933 error = errors[i] & ~common_errors;
2937 gchar *str = file_data_get_error_string(error);
2938 g_string_append_printf(result, "%s: %s\n", fd->name, str);
2943 *desc = g_string_free(result, FALSE);
2952 * perform the change described by FileFataChangeInfo
2953 * it is used for internal operations,
2954 * this function actually operates with files on the filesystem
2955 * it should implement safe delete
2958 static gboolean file_data_perform_move(FileData *fd)
2960 g_assert(!strcmp(fd->change->source, fd->path));
2961 return move_file(fd->change->source, fd->change->dest);
2964 static gboolean file_data_perform_copy(FileData *fd)
2966 g_assert(!strcmp(fd->change->source, fd->path));
2967 return copy_file(fd->change->source, fd->change->dest);
2970 static gboolean file_data_perform_delete(FileData *fd)
2972 if (isdir(fd->path) && !islink(fd->path))
2973 return rmdir_utf8(fd->path);
2975 if (options->file_ops.safe_delete_enable)
2976 return file_util_safe_unlink(fd->path);
2978 return unlink_file(fd->path);
2981 gboolean file_data_perform_ci(FileData *fd)
2983 /** @FIXME When a directory that is a symbolic link is deleted,
2984 * at this point fd->change is null because no FileDataChangeInfo
2985 * has been set up. Therefore there is a seg. fault.
2986 * This code simply aborts the delete.
2993 FileDataChangeType type = fd->change->type;
2997 case FILEDATA_CHANGE_MOVE:
2998 return file_data_perform_move(fd);
2999 case FILEDATA_CHANGE_COPY:
3000 return file_data_perform_copy(fd);
3001 case FILEDATA_CHANGE_RENAME:
3002 return file_data_perform_move(fd); /* the same as move */
3003 case FILEDATA_CHANGE_DELETE:
3004 return file_data_perform_delete(fd);
3005 case FILEDATA_CHANGE_WRITE_METADATA:
3006 return metadata_write_perform(fd);
3007 case FILEDATA_CHANGE_UNSPECIFIED:
3008 /* nothing to do here */
3016 gboolean file_data_sc_perform_ci(FileData *fd)
3019 gboolean ret = TRUE;
3020 FileDataChangeType type = fd->change->type;
3022 if (!file_data_sc_check_ci(fd, type)) return FALSE;
3024 work = fd->sidecar_files;
3027 auto sfd = static_cast<FileData *>(work->data);
3029 if (!file_data_perform_ci(sfd)) ret = FALSE;
3033 if (!file_data_perform_ci(fd)) ret = FALSE;
3039 * updates FileData structure according to FileDataChangeInfo
3042 gboolean file_data_apply_ci(FileData *fd)
3044 FileDataChangeType type = fd->change->type;
3046 /** @FIXME delete ?*/
3047 if (type == FILEDATA_CHANGE_MOVE || type == FILEDATA_CHANGE_RENAME)
3049 DEBUG_1("planned change: applying %s -> %s", fd->change->dest, fd->path);
3050 file_data_planned_change_remove(fd);
3052 if (g_hash_table_lookup(file_data_pool, fd->change->dest))
3054 /* this change overwrites another file which is already known to other modules
3055 renaming fd would create duplicate FileData structure
3056 the best thing we can do is nothing
3058 /** @FIXME maybe we could copy stuff like marks
3060 DEBUG_1("can't rename fd, target exists %s -> %s", fd->change->dest, fd->path);
3064 file_data_set_path(fd, fd->change->dest);
3067 file_data_increment_version(fd);
3068 file_data_send_notification(fd, NOTIFY_CHANGE);
3073 gboolean file_data_sc_apply_ci(FileData *fd)
3076 FileDataChangeType type = fd->change->type;
3078 if (!file_data_sc_check_ci(fd, type)) return FALSE;
3080 work = fd->sidecar_files;
3083 auto sfd = static_cast<FileData *>(work->data);
3085 file_data_apply_ci(sfd);
3089 file_data_apply_ci(fd);
3094 static gboolean file_data_list_contains_whole_group(GList *list, FileData *fd)
3097 if (fd->parent) fd = fd->parent;
3098 if (!g_list_find(list, fd)) return FALSE;
3100 work = fd->sidecar_files;
3103 if (!g_list_find(list, work->data)) return FALSE;
3109 GList *file_data_process_groups_in_selection(GList *list, gboolean ungroup, GList **ungrouped_list)
3111 GList *out = nullptr;
3114 /* change partial groups to independent files */
3119 auto fd = static_cast<FileData *>(work->data);
3122 if (!file_data_list_contains_whole_group(list, fd))
3124 file_data_disable_grouping(fd, TRUE);
3127 *ungrouped_list = g_list_prepend(*ungrouped_list, file_data_ref(fd));
3133 /* remove sidecars from the list,
3134 they can be still accessed via main_fd->sidecar_files */
3138 auto fd = static_cast<FileData *>(work->data);
3142 (!ungroup && !file_data_list_contains_whole_group(list, fd)))
3144 out = g_list_prepend(out, file_data_ref(fd));
3148 filelist_free(list);
3149 out = g_list_reverse(out);
3159 * notify other modules about the change described by FileDataChangeInfo
3162 /* might use file_maint_ functions for now, later it should be changed to a system of callbacks */
3163 /** @FIXME do we need the ignore_list? It looks like a workaround for ineffective
3164 implementation in view-file-list.cc */
3167 struct NotifyIdleData {
3174 FileDataNotifyFunc func;
3176 NotifyPriority priority;
3179 static GList *notify_func_list = nullptr;
3181 static gint file_data_notify_sort(gconstpointer a, gconstpointer b)
3183 auto nda = static_cast<const NotifyData *>(a);
3184 auto ndb = static_cast<const NotifyData *>(b);
3186 if (nda->priority < ndb->priority) return -1;
3187 if (nda->priority > ndb->priority) return 1;
3191 gboolean file_data_register_notify_func(FileDataNotifyFunc func, gpointer data, NotifyPriority priority)
3194 GList *work = notify_func_list;
3198 auto nd = static_cast<NotifyData *>(work->data);
3200 if (nd->func == func && nd->data == data)
3202 g_warning("Notify func already registered");
3208 nd = g_new(NotifyData, 1);
3211 nd->priority = priority;
3213 notify_func_list = g_list_insert_sorted(notify_func_list, nd, file_data_notify_sort);
3214 DEBUG_2("Notify func registered: %p", (void *)nd);
3219 gboolean file_data_unregister_notify_func(FileDataNotifyFunc func, gpointer data)
3221 GList *work = notify_func_list;
3225 auto nd = static_cast<NotifyData *>(work->data);
3227 if (nd->func == func && nd->data == data)
3229 notify_func_list = g_list_delete_link(notify_func_list, work);
3230 DEBUG_2("Notify func unregistered: %p", (void *)nd);
3237 g_warning("Notify func not found");
3241 #pragma GCC diagnostic push
3242 #pragma GCC diagnostic ignored "-Wunused-function"
3243 gboolean file_data_send_notification_idle_cb_unused(gpointer data)
3245 NotifyIdleData *nid = (NotifyIdleData *)data;
3246 GList *work = notify_func_list;
3250 NotifyData *nd = (NotifyData *)work->data;
3252 nd->func(nid->fd, nid->type, nd->data);
3255 file_data_unref(nid->fd);
3259 #pragma GCC diagnostic pop
3261 void file_data_send_notification(FileData *fd, NotifyType type)
3263 GList *work = notify_func_list;
3267 auto nd = static_cast<NotifyData *>(work->data);
3269 nd->func(fd, type, nd->data);
3273 NotifyIdleData *nid = g_new0(NotifyIdleData, 1);
3274 nid->fd = file_data_ref(fd);
3276 g_idle_add_full(G_PRIORITY_HIGH, file_data_send_notification_idle_cb, nid, NULL);
3280 static GHashTable *file_data_monitor_pool = nullptr;
3281 static guint realtime_monitor_id = 0; /* event source id */
3283 static void realtime_monitor_check_cb(gpointer key, gpointer, gpointer)
3285 auto fd = static_cast<FileData *>(key);
3287 file_data_check_changed_files(fd);
3289 DEBUG_1("monitor %s", fd->path);
3292 static gboolean realtime_monitor_cb(gpointer)
3294 if (!options->update_on_time_change) return TRUE;
3295 g_hash_table_foreach(file_data_monitor_pool, realtime_monitor_check_cb, nullptr);
3299 gboolean file_data_register_real_time_monitor(FileData *fd)
3305 if (!file_data_monitor_pool)
3306 file_data_monitor_pool = g_hash_table_new(g_direct_hash, g_direct_equal);
3308 count = GPOINTER_TO_INT(g_hash_table_lookup(file_data_monitor_pool, fd));
3310 DEBUG_1("Register realtime %d %s", count, fd->path);
3313 g_hash_table_insert(file_data_monitor_pool, fd, GINT_TO_POINTER(count));
3315 if (!realtime_monitor_id)
3317 realtime_monitor_id = g_timeout_add(5000, realtime_monitor_cb, nullptr);
3323 gboolean file_data_unregister_real_time_monitor(FileData *fd)
3327 g_assert(file_data_monitor_pool);
3329 count = GPOINTER_TO_INT(g_hash_table_lookup(file_data_monitor_pool, fd));
3331 DEBUG_1("Unregister realtime %d %s", count, fd->path);
3333 g_assert(count > 0);
3338 g_hash_table_remove(file_data_monitor_pool, fd);
3340 g_hash_table_insert(file_data_monitor_pool, fd, GINT_TO_POINTER(count));
3342 file_data_unref(fd);
3344 if (g_hash_table_size(file_data_monitor_pool) == 0)
3346 g_source_remove(realtime_monitor_id);
3347 realtime_monitor_id = 0;
3355 *-----------------------------------------------------------------------------
3356 * Saving marks list, clearing marks
3357 * Uses file_data_pool
3358 *-----------------------------------------------------------------------------
3361 static void marks_get_files(gpointer key, gpointer value, gpointer userdata)
3363 auto file_name = static_cast<gchar *>(key);
3364 auto result = static_cast<GString *>(userdata);
3367 if (isfile(file_name))
3369 fd = static_cast<FileData *>(value);
3370 if (fd && fd->marks > 0)
3372 g_string_append_printf(result, "%s,%i\n", fd->path, fd->marks);
3377 gboolean marks_list_load(const gchar *path)
3385 pathl = path_from_utf8(path);
3386 f = fopen(pathl, "r");
3388 if (!f) return FALSE;
3390 /* first line must start with Marks comment */
3391 if (!fgets(s_buf, sizeof(s_buf), f) ||
3392 strncmp(s_buf, "#Marks", 6) != 0)
3398 while (fgets(s_buf, sizeof(s_buf), f))
3400 if (s_buf[0]=='#') continue;
3401 file_path = strtok(s_buf, ",");
3402 marks_value = strtok(nullptr, ",");
3403 if (isfile(file_path))
3405 FileData *fd = file_data_new_no_grouping(file_path);
3410 gint mark_no = 1 << n;
3411 if (atoi(marks_value) & mark_no)
3413 file_data_set_mark(fd, n , 1);
3424 gboolean marks_list_save(gchar *path, gboolean save)
3426 SecureSaveInfo *ssi;
3429 pathl = path_from_utf8(path);
3430 ssi = secure_open(pathl);
3434 log_printf(_("Error: Unable to write marks lists to: %s\n"), path);
3438 secure_fprintf(ssi, "#Marks lists\n");
3440 GString *marks = g_string_new("");
3443 g_hash_table_foreach(file_data_pool, marks_get_files, marks);
3445 secure_fprintf(ssi, "%s", marks->str);
3446 g_string_free(marks, TRUE);
3448 secure_fprintf(ssi, "#end\n");
3449 return (secure_close(ssi) == 0);
3452 static void marks_clear(gpointer key, gpointer value, gpointer)
3454 auto file_name = static_cast<gchar *>(key);
3459 if (isfile(file_name))
3461 fd = static_cast<FileData *>(value);
3462 if (fd && fd->marks > 0)
3468 if (fd->marks & mark_no)
3470 file_data_set_mark(fd, n , 0);
3478 void marks_clear_all()
3480 g_hash_table_foreach(file_data_pool, marks_clear, nullptr);
3483 void file_data_set_page_num(FileData *fd, gint page_num)
3485 if (fd->page_total > 1 && page_num < 0)
3487 fd->page_num = fd->page_total - 1;
3489 else if (fd->page_total > 1 && page_num <= fd->page_total)
3491 fd->page_num = page_num - 1;
3497 file_data_send_notification(fd, NOTIFY_REREAD);
3500 void file_data_inc_page_num(FileData *fd)
3502 if (fd->page_total > 0 && fd->page_num < fd->page_total - 1)
3504 fd->page_num = fd->page_num + 1;
3506 else if (fd->page_total == 0)
3508 fd->page_num = fd->page_num + 1;
3510 file_data_send_notification(fd, NOTIFY_REREAD);
3513 void file_data_dec_page_num(FileData *fd)
3515 if (fd->page_num > 0)
3517 fd->page_num = fd->page_num - 1;
3519 file_data_send_notification(fd, NOTIFY_REREAD);
3522 void file_data_set_page_total(FileData *fd, gint page_total)
3524 fd->page_total = page_total;
3527 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */