Trim trailing white spaces.
[geeqie.git] / src / filedata.c
index 17b3711..105d016 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Geeqie
  * (C) 2006 John Ellis
- * Copyright (C) 2008 - 2009 The Geeqie Team
+ * Copyright (C) 2008 - 2012 The Geeqie Team
  *
  * Author: John Ellis
  *
 #include "trash.h"
 #include "histogram.h"
 
+#include "exif.h"
+
+#include <errno.h>
 
 static GHashTable *file_data_pool = NULL;
 static GHashTable *file_data_planned_change_hash = NULL;
 
-static gint sidecar_file_priority(const gchar *path);
-static FileData *file_data_new_local(const gchar *path, struct stat *st, gboolean check_sidecars, GHashTable *basename_hash);
+static gint sidecar_file_priority(const gchar *extension);
+static void file_data_check_sidecars(const GList *basename_list);
+static void file_data_disconnect_sidecar_file(FileData *target, FileData *sfd);
+
 
+static SortType filelist_sort_method = SORT_NONE;
+static gboolean filelist_sort_ascend = TRUE;
 
 /*
  *-----------------------------------------------------------------------------
@@ -115,7 +122,7 @@ const gchar *text_from_time(time_t t)
        btime = localtime(&t);
 
        /* the %x warning about 2 digit years is not an error */
-       buflen = strftime(buf, sizeof(buf), "%x %H:%M", btime);
+       buflen = strftime(buf, sizeof(buf), "%x %X", btime);
        if (buflen < 1) return "";
 
        g_free(ret);
@@ -132,119 +139,145 @@ const gchar *text_from_time(time_t t)
 
 /*
  *-----------------------------------------------------------------------------
- * file info struct
+ * changed files detection and notification
  *-----------------------------------------------------------------------------
  */
 
-FileData *file_data_merge_sidecar_files(FileData *target, FileData *source);
-static void file_data_check_sidecars(FileData *fd, GHashTable *basename_hash);
-FileData *file_data_disconnect_sidecar_file(FileData *target, FileData *sfd);
-
-
 void file_data_increment_version(FileData *fd)
 {
        fd->version++;
        fd->valid_marks = 0;
-       if (fd->parent) 
+       if (fd->parent)
                {
                fd->parent->version++;
                fd->parent->valid_marks = 0;
                }
 }
 
-static gint file_data_sort_by_ext(gconstpointer a, gconstpointer b)
+static gboolean file_data_check_changed_single_file(FileData *fd, struct stat *st)
 {
-       const FileData *fda = a;
-       const FileData *fdb = b;
-       
-       return strcmp(fdb->extension, fda->extension);
-}
-
-static GHashTable *file_data_basename_hash_new(void)
-{
-       return g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
+       if (fd->size != st->st_size ||
+           fd->date != st->st_mtime)
+               {
+               fd->size = st->st_size;
+               fd->date = st->st_mtime;
+               fd->mode = st->st_mode;
+               if (fd->thumb_pixbuf) g_object_unref(fd->thumb_pixbuf);
+               fd->thumb_pixbuf = NULL;
+               file_data_increment_version(fd);
+               file_data_send_notification(fd, NOTIFY_REREAD);
+               return TRUE;
+               }
+       return FALSE;
 }
 
-static void file_data_basename_hash_insert(GHashTable *basename_hash, FileData *fd)
+static gboolean file_data_check_changed_files_recursive(FileData *fd, struct stat *st)
 {
-       GList *list;
-       const gchar *ext = extension_from_path(fd->path);
-       gchar *basename = ext ? g_strndup(fd->path, ext - fd->path) : g_strdup(fd->path);
-
-       list = g_hash_table_lookup(basename_hash, basename);
+       gboolean ret = FALSE;
+       GList *work;
        
-       if (!g_list_find(list, fd))
-               {
-               list = g_list_insert_sorted(list, file_data_ref(fd), file_data_sort_by_ext);
-               g_hash_table_insert(basename_hash, basename, list);
-               }
-       else 
+       ret = file_data_check_changed_single_file(fd, st);
+
+       work = fd->sidecar_files;
+       while (work)
                {
-               g_free(basename);
+               FileData *sfd = work->data;
+               struct stat st;
+               work = work->next;
+
+               if (!stat_utf8(sfd->path, &st))
+                       {
+                       fd->size = 0;
+                       fd->date = 0;
+                       file_data_ref(sfd);
+                       file_data_disconnect_sidecar_file(fd, sfd);
+                       ret = TRUE;
+                       file_data_increment_version(sfd);
+                       file_data_send_notification(sfd, NOTIFY_REREAD);
+                       file_data_unref(sfd);
+                       continue;
+                       }
+
+               ret |= file_data_check_changed_files_recursive(sfd, &st);
                }
+       return ret;
 }
 
-static void file_data_basename_hash_remove(GHashTable *basename_hash, FileData *fd)
+
+gboolean file_data_check_changed_files(FileData *fd)
 {
-       GList *list;
-       const gchar *ext = extension_from_path(fd->path);
-       gchar *basename = ext ? g_strndup(fd->path, ext - fd->path) : g_strdup(fd->path);
-       
-       list = g_hash_table_lookup(basename_hash, basename);
-       
-       if (!g_list_find(list, fd)) return;
-       
-       list = g_list_remove(list, fd);
-       file_data_unref(fd);
+       gboolean ret = FALSE;
+       struct stat st;
        
-       if (list)
+       if (fd->parent) fd = fd->parent;
+
+       if (!stat_utf8(fd->path, &st))
                {
-               g_hash_table_insert(basename_hash, basename, list);
+               GList *sidecars;
+               GList *work;
+               FileData *sfd = NULL;
+
+               /* parent is missing, we have to rebuild whole group */
+               ret = TRUE;
+               fd->size = 0;
+               fd->date = 0;
+               
+               /* file_data_disconnect_sidecar_file might delete the file,
+                  we have to keep the reference to prevent this */
+               sidecars = filelist_copy(fd->sidecar_files);
+               file_data_ref(fd);
+               work = sidecars;
+               while (work)
+                       {
+                       sfd = work->data;
+                       work = work->next;
+               
+                       file_data_disconnect_sidecar_file(fd, sfd);
+                       }
+               file_data_check_sidecars(sidecars); /* this will group the sidecars back together */
+               /* now we can release the sidecars */
+               filelist_free(sidecars);
+               file_data_increment_version(fd);
+               file_data_send_notification(fd, NOTIFY_REREAD);
+               file_data_unref(fd);
                }
-       else 
+       else
                {
-               g_hash_table_remove(basename_hash, basename);
-               g_free(basename);
+               ret |= file_data_check_changed_files_recursive(fd, &st);
                }
-}
 
-static void file_data_basename_hash_remove_list(gpointer key, gpointer value, gpointer data)
-{
-       filelist_free((GList *)value);
+       return ret;
 }
 
-static void file_data_basename_hash_free(GHashTable *basename_hash)
-{
-       g_hash_table_foreach(basename_hash, file_data_basename_hash_remove_list, NULL); 
-       g_hash_table_destroy(basename_hash);
-}
+/*
+ *-----------------------------------------------------------------------------
+ * file name, extension, sorting, ...
+ *-----------------------------------------------------------------------------
+ */
 
 static void file_data_set_collate_keys(FileData *fd)
 {
        gchar *caseless_name;
+       gchar *valid_name;
 
-       caseless_name = g_utf8_casefold(fd->name, -1);
+       valid_name = g_filename_display_name(fd->name);
+       caseless_name = g_utf8_casefold(valid_name, -1);
 
        g_free(fd->collate_key_name);
        g_free(fd->collate_key_name_nocase);
 
-#if GLIB_CHECK_VERSION(2, 8, 0)
-       fd->collate_key_name = g_utf8_collate_key_for_filename(fd->name, -1);
-       fd->collate_key_name_nocase = g_utf8_collate_key_for_filename(caseless_name, -1);
-#else
-       fd->collate_key_name = g_utf8_collate_key(fd->name, -1);
+       fd->collate_key_name = g_utf8_collate_key(valid_name, -1);
        fd->collate_key_name_nocase = g_utf8_collate_key(caseless_name, -1);
-#endif
+       
+       g_free(valid_name);
        g_free(caseless_name);
 }
 
-static void file_data_set_path(FileData *fd, const gchar *path, GHashTable *basename_hash)
+static void file_data_set_path(FileData *fd, const gchar *path)
 {
        g_assert(path /* && *path*/); /* view_dir_tree uses FileData with zero length path */
        g_assert(file_data_pool);
 
-       if (basename_hash && fd->path) file_data_basename_hash_remove(basename_hash, fd);
-       
        g_free(fd->path);
 
        if (fd->original_path)
@@ -291,98 +324,29 @@ static void file_data_set_path(FileData *fd, const gchar *path, GHashTable *base
                return;
                }
 
-       fd->extension = extension_from_path(fd->path);
+       fd->extension = registered_extension_from_path(fd->path);
        if (fd->extension == NULL)
                {
                fd->extension = fd->name + strlen(fd->name);
                }
-
-       if (basename_hash) file_data_basename_hash_insert(basename_hash, fd); /* we can ignore the special cases above - they don't have extensions */
-
+               
+       fd->sidecar_priority = sidecar_file_priority(fd->extension);
        file_data_set_collate_keys(fd);
 }
 
-static gboolean file_data_check_changed_files_recursive(FileData *fd, struct stat *st)
-{
-       gboolean ret = FALSE;
-       GList *work;
-       
-       if (fd->size != st->st_size ||
-           fd->date != st->st_mtime)
-               {
-               fd->size = st->st_size;
-               fd->date = st->st_mtime;
-               fd->mode = st->st_mode;
-               if (fd->thumb_pixbuf) g_object_unref(fd->thumb_pixbuf);
-               fd->thumb_pixbuf = NULL;
-               file_data_increment_version(fd);
-               file_data_send_notification(fd, NOTIFY_REREAD);
-               ret = TRUE;
-               }
-
-       work = fd->sidecar_files;
-       while (work)
-               {
-               FileData *sfd = work->data;
-               struct stat st;
-               work = work->next;
-
-               if (!stat_utf8(sfd->path, &st))
-                       {
-                       fd->size = 0;
-                       fd->date = 0;
-                       file_data_disconnect_sidecar_file(fd, sfd);
-                       ret = TRUE;
-                       continue;
-                       }
-
-               ret |= file_data_check_changed_files_recursive(sfd, &st);
-               }
-       return ret;
-}
-
-
-gboolean file_data_check_changed_files(FileData *fd)
-{
-       gboolean ret = FALSE;
-       struct stat st;
-       
-       if (fd->parent) fd = fd->parent;
-
-       if (!stat_utf8(fd->path, &st))
-               {
-               GList *work;
-               FileData *sfd = NULL;
-
-               /* parent is missing, we have to rebuild whole group */
-               ret = TRUE;
-               fd->size = 0;
-               fd->date = 0;
-               
-               work = fd->sidecar_files;
-               while (work)
-                       {
-                       sfd = work->data;
-                       work = work->next;
-               
-                       file_data_disconnect_sidecar_file(fd, sfd);
-                       }
-               if (sfd) file_data_check_sidecars(sfd, NULL); /* this will group the sidecars back together */
-               file_data_send_notification(fd, NOTIFY_REREAD);
-               }
-       else
-               {
-               ret |= file_data_check_changed_files_recursive(fd, &st);
-               }
-
-       return ret;
-}
+/*
+ *-----------------------------------------------------------------------------
+ * create or reuse Filedata
+ *-----------------------------------------------------------------------------
+ */
 
-static FileData *file_data_new(const gchar *path_utf8, struct stat *st, gboolean check_sidecars, GHashTable *basename_hash)
+static FileData *file_data_new(const gchar *path_utf8, struct stat *st, gboolean disable_sidecars)
 {
        FileData *fd;
 
-       DEBUG_2("file_data_new: '%s' %d %d", path_utf8, check_sidecars, !!basename_hash);
+       DEBUG_2("file_data_new: '%s' %d", path_utf8, disable_sidecars);
+
+       if (S_ISDIR(st->st_mode)) disable_sidecars = TRUE;
 
        if (!file_data_pool)
                file_data_pool = g_hash_table_new(g_str_hash, g_str_equal);
@@ -408,12 +372,11 @@ static FileData *file_data_new(const gchar *path_utf8, struct stat *st, gboolean
                {
                gboolean changed;
                
-               if (fd->parent)
-                       changed = file_data_check_changed_files(fd);
-               else
-                       changed = file_data_check_changed_files_recursive(fd, st);
-               if (changed && check_sidecars && sidecar_file_priority(fd->extension))
-                       file_data_check_sidecars(fd, basename_hash);
+               if (disable_sidecars) file_data_disable_grouping(fd, TRUE);
+               
+               
+               changed = file_data_check_changed_single_file(fd, st);
+
                DEBUG_2("file_data_pool hit: '%s' %s", fd->path, changed ? "(changed)" : "");
                
                return fd;
@@ -425,162 +388,88 @@ static FileData *file_data_new(const gchar *path_utf8, struct stat *st, gboolean
        fd->date = st->st_mtime;
        fd->mode = st->st_mode;
        fd->ref = 1;
-       fd->magick = 0x12345678;
-
-       file_data_set_path(fd, path_utf8, basename_hash); /* set path, name, collate_key_*, original_path */
+       fd->magick = FD_MAGICK;
+       
+       if (disable_sidecars) fd->disable_grouping = TRUE;
 
-       if (check_sidecars)
-               file_data_check_sidecars(fd, basename_hash);
+       file_data_set_path(fd, path_utf8); /* set path, name, collate_key_*, original_path */
 
        return fd;
 }
 
-/* extension must contain only ASCII characters */
-static GList *check_case_insensitive_ext(gchar *path)
+static FileData *file_data_new_local(const gchar *path, struct stat *st, gboolean disable_sidecars)
 {
-       gchar *sl;
-       gchar *extl;
-       gint ext_len;
-       GList *list = NULL;
-
-       sl = path_from_utf8(path);
-
-       extl = strrchr(sl, '.');
-       if (extl)
-               {
-               gint i, j;
-               extl++; /* the first char after . */
-               ext_len = strlen(extl);
-       
-               for (i = 0; i < (1 << ext_len); i++)
-                       {
-                       struct stat st;
-                       gboolean skip = FALSE;
-                       for (j = 0; j < ext_len; j++)
-                               {
-                               if (i & (1 << (ext_len - 1 - j))) 
-                                       {
-                                       extl[j] = g_ascii_tolower(extl[j]);
-                                       /* make sure the result does not contain duplicates */
-                                       if (extl[j] == g_ascii_toupper(extl[j]))
-                                               {
-                                               /* no change, probably a number, we have already tested this combination */
-                                               skip = TRUE;
-                                               break;
-                                               }
-                                       }
-                               else
-                                       extl[j] = g_ascii_toupper(extl[j]);
-                               }
-                       if (skip) continue;
-
-                       if (stat(sl, &st) == 0)
-                               {
-                               list = g_list_prepend(list, file_data_new_local(sl, &st, FALSE, FALSE));
-                               }
-                       }
-               }
-       g_free(sl);
+       gchar *path_utf8 = path_to_utf8(path);
+       FileData *ret = file_data_new(path_utf8, st, disable_sidecars);
 
-       return list;
+       g_free(path_utf8);
+       return ret;
 }
 
-static void file_data_check_sidecars(FileData *fd, GHashTable *basename_hash)
+void init_exif_time_data(GList *files)
 {
-       gint base_len;
-       GString *fname;
-       FileData *parent_fd = NULL;
-       GList *work;
-       const GList *basename_list = NULL;
-       GList *group_list = NULL;
-       if (fd->disable_grouping || !sidecar_file_priority(fd->extension))
-               return;
-
-       base_len = fd->extension - fd->path;
-       fname = g_string_new_len(fd->path, base_len);
-
-       if (basename_hash)
+       FileData *file;
+       DEBUG_1("%s init_exif_time_data: ...", get_exec_time());
+       while (files)
                {
-               basename_list = g_hash_table_lookup(basename_hash, fname->str);
-               }
+               file = files->data;
 
+               if (file)
+                       file->exifdate = 0;
 
-       /* check for possible sidecar files;
-          the sidecar files created here are referenced only via fd->sidecar_files or fd->parent,
-          they have fd->ref set to 0 and file_data unref must chack and free them all together
-          (using fd->ref would cause loops and leaks)
-       */
+               files = files->next;
+               }
+}
 
-       /* find all possible sidecar files and order them according to sidecar_ext_get_list,
-          for case-only differences put lowercase first,
-          put the result to group_list 
-       */
-       work = sidecar_ext_get_list();
-       while (work)
+void read_exif_time_data(FileData *file)
+{
+       if (file->exifdate > 0)
                {
-               gchar *ext = work->data;
-               work = work->next;
-
-               if (!basename_hash)
-                       {
-                       GList *new_list;
-                       g_string_truncate(fname, base_len);
-                       g_string_append(fname, ext);
-                       new_list = check_case_insensitive_ext(fname->str);
-                       group_list = g_list_concat(group_list, new_list);
-                       }
-               else
-                       {
-                       const GList *work2 = basename_list;
-                       
-                       while (work2)
-                               {
-                               FileData *sfd = work2->data;
-                               
-                               if (g_ascii_strcasecmp(ext, sfd->extension) == 0) 
-                                       {
-                                       group_list = g_list_append(group_list, file_data_ref(sfd));
-                                       }
-                               work2 = work2->next;
-                               }
-                       }
+               DEBUG_1("%s set_exif_time_data: Already exists for %s", get_exec_time(), file->path);
+               return;
                }
-       g_string_free(fname, TRUE);
+       
+       file->exif = exif_read_fd(file);
 
-       /* process the group list - the first one is the parent file, others are sidecars */
-       work = group_list;
-       while (work)
+       if (file->exif)
                {
-               FileData *new_fd = work->data;
-               work = work->next;
+               gchar *tmp = exif_get_data_as_text(file->exif, "Exif.Photo.DateTimeOriginal");
+               DEBUG_2("%s set_exif_time_data: reading %p %s", get_exec_time(), file, file->path);
 
-               if (new_fd->disable_grouping)
+               if (tmp)
                        {
-                       file_data_unref(new_fd);
-                       continue;
+                       struct tm time_str;
+                       uint year, month, day, hour, min, sec;
+       
+                       sscanf(tmp, "%4d:%2d:%2d %2d:%2d:%2d", &year, &month, &day, &hour, &min, &sec);
+                       time_str.tm_year  = year - 1900;
+                       time_str.tm_mon   = month - 1;
+                       time_str.tm_mday  = day;
+                       time_str.tm_hour  = hour;
+                       time_str.tm_min   = min;
+                       time_str.tm_sec   = sec;
+                       time_str.tm_isdst = 0;
+       
+                       file->exifdate = mktime(&time_str);
+                       g_free(tmp);
                        }
-
-               new_fd->ref--; /* do not use ref here */
-
-               if (!parent_fd)
-                       parent_fd = new_fd; /* parent is the one with the highest prio, found first */
-               else
-                       file_data_merge_sidecar_files(parent_fd, new_fd);
                }
-       g_list_free(group_list);
 }
 
-
-static FileData *file_data_new_local(const gchar *path, struct stat *st, gboolean check_sidecars, GHashTable *basename_hash)
+void set_exif_time_data(GList *files)
 {
-       gchar *path_utf8 = path_to_utf8(path);
-       FileData *ret = file_data_new(path_utf8, st, check_sidecars, basename_hash);
-
-       g_free(path_utf8);
-       return ret;
+       DEBUG_1("%s set_exif_time_data: ...", get_exec_time());
+       
+       while (files)
+               {
+               FileData *file = files->data;
+               
+               read_exif_time_data(file);
+               files = files->next;
+               }
 }
 
-FileData *file_data_new_simple(const gchar *path_utf8)
+FileData *file_data_new_no_grouping(const gchar *path_utf8)
 {
        struct stat st;
 
@@ -590,41 +479,31 @@ FileData *file_data_new_simple(const gchar *path_utf8)
                st.st_mtime = 0;
                }
 
-       return file_data_new(path_utf8, &st, TRUE, NULL);
+       return file_data_new(path_utf8, &st, TRUE);
 }
 
-FileData *file_data_add_sidecar_file(FileData *target, FileData *sfd)
+FileData *file_data_new_dir(const gchar *path_utf8)
 {
-       sfd->parent = target;
-       if (!g_list_find(target->sidecar_files, sfd))
-               target->sidecar_files = g_list_prepend(target->sidecar_files, sfd);
-       file_data_increment_version(sfd); /* increments both sfd and target */
-       return target;
-}
-
-
-FileData *file_data_merge_sidecar_files(FileData *target, FileData *source)
-{
-       GList *work;
-       
-       file_data_add_sidecar_file(target, source);
+       struct stat st;
 
-       work = source->sidecar_files;
-       while (work)
+       if (!stat_utf8(path_utf8, &st))
                {
-               FileData *sfd = work->data;
-               file_data_add_sidecar_file(target, sfd);
-               work = work->next;
+               st.st_size = 0;
+               st.st_mtime = 0;
                }
-
-       g_list_free(source->sidecar_files);
-       source->sidecar_files = NULL;
-
-       target->sidecar_files = filelist_sort(target->sidecar_files, SORT_NAME, TRUE);
-       
-       return target;
+       else
+               /* dir or non-existing yet */
+               g_assert(S_ISDIR(st.st_mode));
+               
+       return file_data_new(path_utf8, &st, TRUE);
 }
 
+/*
+ *-----------------------------------------------------------------------------
+ * reference counting
+ *-----------------------------------------------------------------------------
+ */
+
 #ifdef DEBUG_FILEDATA
 FileData *file_data_ref_debug(const gchar *file, gint line, FileData *fd)
 #else
@@ -632,26 +511,29 @@ FileData *file_data_ref(FileData *fd)
 #endif
 {
        if (fd == NULL) return NULL;
+       if (fd->magick != FD_MAGICK)
 #ifdef DEBUG_FILEDATA
-       if (fd->magick != 0x12345678)
-               DEBUG_0("fd magick mismatch at %s:%d", file, line);
+               DEBUG_0("fd magick mismatch @ %s:%d  fd=%p", file, line, fd);
+#else
+               DEBUG_0("fd magick mismatch fd=%p", fd);
 #endif
-       g_assert(fd->magick == 0x12345678);
+       g_assert(fd->magick == FD_MAGICK);
        fd->ref++;
 
 #ifdef DEBUG_FILEDATA
-       DEBUG_2("file_data_ref (%d): '%s' @ %s:%d", fd->ref, fd->path, file, line);
+       DEBUG_2("file_data_ref fd=%p (%d): '%s' @ %s:%d", fd, fd->ref, fd->path, file, line);
 #else
-       DEBUG_2("file_data_ref (%d): '%s'", fd->ref, fd->path);
+       DEBUG_2("file_data_ref fd=%p (%d): '%s'", fd, fd->ref, fd->path);
 #endif
        return fd;
 }
 
 static void file_data_free(FileData *fd)
 {
-       g_assert(fd->magick == 0x12345678);
+       g_assert(fd->magick == FD_MAGICK);
        g_assert(fd->ref == 0);
 
+       metadata_cache_free(fd);
        g_hash_table_remove(file_data_pool, fd->original_path);
 
        g_free(fd->path);
@@ -674,17 +556,19 @@ void file_data_unref(FileData *fd)
 #endif
 {
        if (fd == NULL) return;
+       if (fd->magick != FD_MAGICK)
 #ifdef DEBUG_FILEDATA
-       if (fd->magick != 0x12345678)
-               DEBUG_0("fd magick mismatch @ %s:%d", file, line);
+               DEBUG_0("fd magick mismatch @ %s:%d  fd=%p", file, line, fd);
+#else
+               DEBUG_0("fd magick mismatch fd=%p", fd);
 #endif
-       g_assert(fd->magick == 0x12345678);
+       g_assert(fd->magick == FD_MAGICK);
        
        fd->ref--;
 #ifdef DEBUG_FILEDATA
-       DEBUG_2("file_data_unref (%d): '%s' @ %s:%d", fd->ref, fd->path, file, line);
+       DEBUG_2("file_data_unref fd=%p (%d): '%s' @ %s:%d", fd, fd->ref, fd->path, file, line);
 #else
-       DEBUG_2("file_data_unref (%d): '%s'", fd->ref, fd->path);
+       DEBUG_2("file_data_unref fd=%p (%d): '%s'", fd, fd->ref, fd->path);
 #endif
        if (fd->ref == 0)
                {
@@ -720,191 +604,245 @@ void file_data_unref(FileData *fd)
                }
 }
 
-FileData *file_data_disconnect_sidecar_file(FileData *target, FileData *sfd)
+
+
+/*
+ *-----------------------------------------------------------------------------
+ * sidecar file info struct
+ *-----------------------------------------------------------------------------
+ */
+
+static gint file_data_sort_by_ext(gconstpointer a, gconstpointer b)
 {
-       sfd->parent = target;
-       g_assert(g_list_find(target->sidecar_files, sfd));
+       const FileData *fda = a;
+       const FileData *fdb = b;
        
-       file_data_increment_version(sfd); /* increments both sfd and target */
+       if (fda->sidecar_priority < fdb->sidecar_priority) return -1;
+       if (fda->sidecar_priority > fdb->sidecar_priority) return 1;
+       
+       return strcmp(fdb->extension, fda->extension);
+}
 
-       target->sidecar_files = g_list_remove(target->sidecar_files, sfd);
-       sfd->parent = NULL;
 
-       if (sfd->ref == 0)
-               {
-               file_data_free(sfd);
-               return NULL;
-               }
+static gint sidecar_file_priority(const gchar *extension)
+{
+       gint i = 1;
+       GList *work;
+
+       if (extension == NULL)
+               return 0;
 
-       return sfd;
+       work = sidecar_ext_get_list();
+
+       while (work) {
+               gchar *ext = work->data;
+               
+               work = work->next;
+               if (g_ascii_strcasecmp(extension, ext) == 0) return i;
+               i++;
+       }
+       return 0;
 }
 
-/* disables / enables grouping for particular file, sends UPDATE notification */
-void file_data_disable_grouping(FileData *fd, gboolean disable)
+static void file_data_check_sidecars(const GList *basename_list)
 {
-       if (!fd->disable_grouping == !disable) return;
-       
-       fd->disable_grouping = !!disable;
-       
-       if (disable)
+       /* basename_list contains the new group - first is the parent, then sorted sidecars */
+       /* all files in the list have ref count > 0 */
+
+       const GList *work;
+       GList *s_work, *new_sidecars;
+       FileData *parent_fd;
+
+       if (!basename_list) return;
+
+
+       DEBUG_2("basename start");
+       work = basename_list;
+       while (work)
                {
+               FileData *fd = work->data;
+               work = work->next;
+               g_assert(fd->magick == FD_MAGICK);
+               DEBUG_2("basename: %p %s", fd, fd->name);
                if (fd->parent)
                        {
-                       FileData *parent = file_data_ref(fd->parent);
-                       file_data_disconnect_sidecar_file(parent, fd);
-                       file_data_send_notification(parent, NOTIFY_GROUPING);
-                       file_data_unref(parent);
-                       }
-               else if (fd->sidecar_files)
-                       {
-                       GList *sidecar_files = filelist_copy(fd->sidecar_files);
-                       GList *work = sidecar_files;
-                       while (work)
-                               {
-                               FileData *sfd = work->data;
-                               work = work->next;
-                               file_data_disconnect_sidecar_file(fd, sfd);
-                               file_data_send_notification(sfd, NOTIFY_GROUPING);
-                               }
-                       file_data_check_sidecars((FileData *)sidecar_files->data, FALSE); /* this will group the sidecars back together */
-                       filelist_free(sidecar_files);
+                       g_assert(fd->parent->magick == FD_MAGICK);
+                       DEBUG_2("                  parent: %p", fd->parent);
                        }
-               else
+               s_work = fd->sidecar_files;
+               while (s_work)
                        {
-                       file_data_increment_version(fd); /* the functions called in the cases above increments the version too */
+                       FileData *sfd = s_work->data;
+                       s_work = s_work->next;
+                       g_assert(sfd->magick == FD_MAGICK);
+                       DEBUG_2("                  sidecar: %p %s", sfd, sfd->name);
                        }
+               
+               g_assert(fd->parent == NULL || fd->sidecar_files == NULL);
                }
-       else
+
+       parent_fd = basename_list->data;
+
+       /* check if the second and next entries of basename_list are already connected
+          as sidecars of the first entry (parent_fd) */
+       work = basename_list->next;
+       s_work = parent_fd->sidecar_files;
+       
+       while (work && s_work)
                {
-               file_data_increment_version(fd);
-               file_data_check_sidecars(fd, FALSE);
+               if (work->data != s_work->data) break;
+               work = work->next;
+               s_work = s_work->next;
+               }
+               
+       if (!work && !s_work)
+               {
+               DEBUG_2("basename no change");
+               return; /* no change in grouping */
                }
-       file_data_send_notification(fd, NOTIFY_GROUPING);
-}
-
-void file_data_disable_grouping_list(GList *fd_list, gboolean disable)
-{
-       GList *work;
        
-       work = fd_list;
+       /* we have to regroup it */
+       
+       /* first, disconnect everything and send notification*/
+
+       work = basename_list;
        while (work)
                {
                FileData *fd = work->data;
+               work = work->next;
+               g_assert(fd->parent == NULL || fd->sidecar_files == NULL);
                
-               file_data_disable_grouping(fd, disable);
+               if (fd->parent)
+                       {
+                       FileData *old_parent = fd->parent;
+                       g_assert(old_parent->parent == NULL || old_parent->sidecar_files == NULL);
+                       file_data_ref(old_parent);
+                       file_data_disconnect_sidecar_file(old_parent, fd);
+                       file_data_send_notification(old_parent, NOTIFY_REREAD);
+                       file_data_unref(old_parent);
+                       }
+               
+               while (fd->sidecar_files)
+                       {
+                       FileData *sfd = fd->sidecar_files->data;
+                       g_assert(sfd->parent == NULL || sfd->sidecar_files == NULL);
+                       file_data_ref(sfd);
+                       file_data_disconnect_sidecar_file(fd, sfd);
+                       file_data_send_notification(sfd, NOTIFY_REREAD);
+                       file_data_unref(sfd);
+                       }
+               file_data_send_notification(fd, NOTIFY_GROUPING);
+               
+               g_assert(fd->parent == NULL && fd->sidecar_files == NULL);
+               }
+
+       /* now we can form the new group */
+       work = basename_list->next;
+       new_sidecars = NULL;
+       while (work)
+               {
+               FileData *sfd = work->data;
+               g_assert(sfd->magick == FD_MAGICK);
+               g_assert(sfd->parent == NULL && sfd->sidecar_files == NULL);
+               sfd->parent = parent_fd;
+               new_sidecars = g_list_prepend(new_sidecars, sfd);
                work = work->next;
                }
+       g_assert(parent_fd->sidecar_files == NULL);
+       parent_fd->sidecar_files = g_list_reverse(new_sidecars);
+       DEBUG_1("basename group changed for %s", parent_fd->path);
 }
 
 
-/* compare name without extension */
-gint file_data_compare_name_without_ext(FileData *fd1, FileData *fd2)
-{
-       size_t len1 = fd1->extension - fd1->name;
-       size_t len2 = fd2->extension - fd2->name;
-
-       if (len1 < len2) return -1;
-       if (len1 > len2) return 1;
-
-       return strncmp(fd1->name, fd2->name, len1); /* FIXME: utf8 */
-}
-
-void file_data_change_info_free(FileDataChangeInfo *fdci, FileData *fd)
+static void file_data_disconnect_sidecar_file(FileData *target, FileData *sfd)
 {
-       if (!fdci && fd) fdci = fd->change;
-
-       if (!fdci) return;
-
-       g_free(fdci->source);
-       g_free(fdci->dest);
+       g_assert(target->magick == FD_MAGICK);
+       g_assert(sfd->magick == FD_MAGICK);
+       g_assert(g_list_find(target->sidecar_files, sfd));
 
-       g_free(fdci);
+       file_data_ref(target);
+       file_data_ref(sfd);
 
-       if (fd) fd->change = NULL;
-}
+       g_assert(sfd->parent == target);
+       
+       file_data_increment_version(sfd); /* increments both sfd and target */
 
-static gboolean file_data_can_write_directly(FileData *fd)
-{
-       return filter_name_is_writable(fd->extension);
-}
+       target->sidecar_files = g_list_remove(target->sidecar_files, sfd);
+       sfd->parent = NULL;
 
-static gboolean file_data_can_write_sidecar(FileData *fd)
-{
-       return filter_name_allow_sidecar(fd->extension) && !filter_name_is_writable(fd->extension);
+       file_data_unref(target);
+       file_data_unref(sfd);
 }
 
-gchar *file_data_get_sidecar_path(FileData *fd, gboolean existing_only)
+/* disables / enables grouping for particular file, sends UPDATE notification */
+void file_data_disable_grouping(FileData *fd, gboolean disable)
 {
-       gchar *sidecar_path = NULL;
-       GList *work;
+       if (!fd->disable_grouping == !disable) return;
        
-       if (!file_data_can_write_sidecar(fd)) return NULL;
+       fd->disable_grouping = !!disable;
        
-       work = fd->parent ? fd->parent->sidecar_files : fd->sidecar_files;
-       while (work)
+       if (disable)
                {
-               FileData *sfd = work->data;
-               work = work->next;
-               if (g_ascii_strcasecmp(sfd->extension, ".xmp") == 0)
+               if (fd->parent)
                        {
-                       sidecar_path = g_strdup(sfd->path);
-                       break;
+                       FileData *parent = file_data_ref(fd->parent);
+                       file_data_disconnect_sidecar_file(parent, fd);
+                       file_data_send_notification(parent, NOTIFY_GROUPING);
+                       file_data_unref(parent);
+                       }
+               else if (fd->sidecar_files)
+                       {
+                       GList *sidecar_files = filelist_copy(fd->sidecar_files);
+                       GList *work = sidecar_files;
+                       while (work)
+                               {
+                               FileData *sfd = work->data;
+                               work = work->next;
+                               file_data_disconnect_sidecar_file(fd, sfd);
+                               file_data_send_notification(sfd, NOTIFY_GROUPING);
+                               }
+                       file_data_check_sidecars(sidecar_files); /* this will group the sidecars back together */
+                       filelist_free(sidecar_files);
+                       }
+               else
+                       {
+                       file_data_increment_version(fd); /* the functions called in the cases above increments the version too */
                        }
                }
-       
-       if (!existing_only && !sidecar_path)
+       else
                {
-               gchar *base = remove_extension_from_path(fd->path);
-               sidecar_path = g_strconcat(base, ".xmp", NULL);
-               g_free(base);
+               file_data_increment_version(fd);
+               /* file_data_check_sidecars call is not necessary - the file will be re-grouped on next dir read */
                }
-
-       return sidecar_path;
+       file_data_send_notification(fd, NOTIFY_GROUPING);
 }
 
-
-/*
- *-----------------------------------------------------------------------------
- * sidecar file info struct
- *-----------------------------------------------------------------------------
- */
-
-
-
-static gint sidecar_file_priority(const gchar *path)
+void file_data_disable_grouping_list(GList *fd_list, gboolean disable)
 {
-       const gchar *extension = extension_from_path(path);
-       gint i = 1;
        GList *work;
-
-       if (extension == NULL)
-               return 0;
-
-       work = sidecar_ext_get_list();
-
-       while (work) {
-               gchar *ext = work->data;
+       
+       work = fd_list;
+       while (work)
+               {
+               FileData *fd = work->data;
                
+               file_data_disable_grouping(fd, disable);
                work = work->next;
-               if (g_ascii_strcasecmp(extension, ext) == 0) return i;
-               i++;
-       }
-       return 0;
+               }
 }
 
 
+
 /*
  *-----------------------------------------------------------------------------
- * load file list
+ * filelist sorting
  *-----------------------------------------------------------------------------
  */
 
-static SortType filelist_sort_method = SORT_NONE;
-static gboolean filelist_sort_ascend = TRUE;
-
 
 gint filelist_sort_compare_filedata(FileData *fa, FileData *fb)
 {
+       gint ret;
        if (!filelist_sort_ascend)
                {
                FileData *tmp = fa;
@@ -926,9 +864,15 @@ gint filelist_sort_compare_filedata(FileData *fa, FileData *fb)
                        if (fa->date > fb->date) return 1;
                        /* fall back to name */
                        break;
+               case SORT_EXIFTIME:
+                       if (fa->exifdate < fb->exifdate) return -1;
+                       if (fa->exifdate > fb->exifdate) return 1;
+                       /* fall back to name */
+                       break;
 #ifdef HAVE_STRVERSCMP
                case SORT_NUMBER:
-                       return strverscmp(fa->name, fb->name);
+                       ret = strverscmp(fa->name, fb->name);
+                       if (ret != 0) return ret;
                        break;
 #endif
                default:
@@ -936,9 +880,16 @@ gint filelist_sort_compare_filedata(FileData *fa, FileData *fb)
                }
 
        if (options->file_sort.case_sensitive)
-               return strcmp(fa->collate_key_name, fb->collate_key_name);
+               ret = strcmp(fa->collate_key_name, fb->collate_key_name);
        else
-               return strcmp(fa->collate_key_name_nocase, fb->collate_key_name_nocase);
+               ret = strcmp(fa->collate_key_name_nocase, fb->collate_key_name_nocase);
+
+       if (ret != 0) return ret;
+       
+       /* do not return 0 unless the files are really the same
+          file_data_pool ensures that original_path is unique
+       */
+       return strcmp(fa->original_path, fb->original_path);
 }
 
 gint filelist_sort_compare_filedata_full(FileData *fa, FileData *fb, SortType method, gboolean ascend)
@@ -969,6 +920,10 @@ GList *filelist_insert_sort_full(GList *list, gpointer data, SortType method, gb
 
 GList *filelist_sort(GList *list, SortType method, gboolean ascend)
 {
+       if (method == SORT_EXIFTIME)
+               {
+               set_exif_time_data(list);
+               }
        return filelist_sort_full(list, method, ascend, (GCompareFunc) filelist_sort_file_cb);
 }
 
@@ -977,6 +932,53 @@ GList *filelist_insert_sort(GList *list, FileData *fd, SortType method, gboolean
        return filelist_insert_sort_full(list, fd, method, ascend, (GCompareFunc) filelist_sort_file_cb);
 }
 
+/*
+ *-----------------------------------------------------------------------------
+ * basename hash - grouping of sidecars in filelist
+ *-----------------------------------------------------------------------------
+ */
+
+
+static GHashTable *file_data_basename_hash_new(void)
+{
+       return g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
+}
+
+static GList * file_data_basename_hash_insert(GHashTable *basename_hash, FileData *fd)
+{
+       GList *list;
+       gchar *basename = g_strndup(fd->path, fd->extension - fd->path);
+
+       list = g_hash_table_lookup(basename_hash, basename);
+       
+       if (!g_list_find(list, fd))
+               {
+               list = g_list_insert_sorted(list, file_data_ref(fd), file_data_sort_by_ext);
+               g_hash_table_insert(basename_hash, basename, list);
+               }
+       else
+               {
+               g_free(basename);
+               }
+       return list;
+}
+
+static void file_data_basename_hash_remove_list(gpointer key, gpointer value, gpointer data)
+{
+       filelist_free((GList *)value);
+}
+
+static void file_data_basename_hash_free(GHashTable *basename_hash)
+{
+       g_hash_table_foreach(basename_hash, file_data_basename_hash_remove_list, NULL);
+       g_hash_table_destroy(basename_hash);
+}
+
+/*
+ *-----------------------------------------------------------------------------
+ * handling sidecars in filelist
+ *-----------------------------------------------------------------------------
+ */
 
 static GList *filelist_filter_out_sidecars(GList *flist)
 {
@@ -998,6 +1000,13 @@ static GList *filelist_filter_out_sidecars(GList *flist)
        return flist_filtered;
 }
 
+static void file_data_basename_hash_to_sidecars(gpointer key, gpointer value, gpointer data)
+{
+       GList *basename_list = (GList *)value;
+       file_data_check_sidecars(basename_list);
+}
+
+
 static gboolean is_hidden_file(const gchar *name)
 {
        if (name[0] != '.') return FALSE;
@@ -1005,7 +1014,13 @@ static gboolean is_hidden_file(const gchar *name)
        return TRUE;
 }
 
-static gboolean filelist_read_real(FileData *dir_fd, GList **files, GList **dirs, gboolean follow_symlinks)
+/*
+ *-----------------------------------------------------------------------------
+ * the main filelist function
+ *-----------------------------------------------------------------------------
+ */
+
+static gboolean filelist_read_real(const gchar *dir_path, GList **files, GList **dirs, gboolean follow_symlinks)
 {
        DIR *dp;
        struct dirent *dir;
@@ -1020,7 +1035,7 @@ static gboolean filelist_read_real(FileData *dir_fd, GList **files, GList **dirs
        if (files) *files = NULL;
        if (dirs) *dirs = NULL;
 
-       pathl = path_from_utf8(dir_fd->path);
+       pathl = path_from_utf8(dir_path);
        if (!pathl) return FALSE;
 
        dp = opendir(pathl);
@@ -1058,41 +1073,92 @@ static gboolean filelist_read_real(FileData *dir_fd, GList **files, GList **dirs
                                    strcmp(name, GQ_CACHE_LOCAL_METADATA) != 0 &&
                                    strcmp(name, THUMB_FOLDER_LOCAL) != 0)
                                        {
-                                       dlist = g_list_prepend(dlist, file_data_new_local(filepath, &ent_sbuf, FALSE, NULL));
+                                       dlist = g_list_prepend(dlist, file_data_new_local(filepath, &ent_sbuf, TRUE));
                                        }
                                }
                        else
                                {
                                if (files && filter_name_exists(name))
                                        {
-                                       flist = g_list_prepend(flist, file_data_new_local(filepath, &ent_sbuf, TRUE, basename_hash));
+                                       FileData *fd = file_data_new_local(filepath, &ent_sbuf, FALSE);
+                                       flist = g_list_prepend(flist, fd);
+                                       if (fd->sidecar_priority && !fd->disable_grouping)
+                                               {
+                                               file_data_basename_hash_insert(basename_hash, fd);
+                                               }
                                        }
                                }
                        }
+               else
+                       {
+                       if (errno == EOVERFLOW)
+                               {
+                               log_printf("stat(): EOVERFLOW, skip '%s'", filepath);
+                               }
+                       }
                g_free(filepath);
                }
 
        closedir(dp);
        
        g_free(pathl);
-       if (basename_hash) file_data_basename_hash_free(basename_hash);
 
        if (dirs) *dirs = dlist;
-       if (files) *files = filelist_filter_out_sidecars(flist);
+
+       if (files)
+               {
+               g_hash_table_foreach(basename_hash, file_data_basename_hash_to_sidecars, NULL);
+
+               *files = filelist_filter_out_sidecars(flist);
+               }
+       if (basename_hash) file_data_basename_hash_free(basename_hash);
+
+       // Call a separate function to initialize the exif datestamps for the found files..
+       if (files) init_exif_time_data(*files);
 
        return TRUE;
 }
 
 gboolean filelist_read(FileData *dir_fd, GList **files, GList **dirs)
 {
-       return filelist_read_real(dir_fd, files, dirs, TRUE);
+       return filelist_read_real(dir_fd->path, files, dirs, TRUE);
 }
 
 gboolean filelist_read_lstat(FileData *dir_fd, GList **files, GList **dirs)
 {
-       return filelist_read_real(dir_fd, files, dirs, FALSE);
+       return filelist_read_real(dir_fd->path, files, dirs, FALSE);
+}
+
+FileData *file_data_new_group(const gchar *path_utf8)
+{
+       gchar *dir;
+       struct stat st;
+       FileData *fd;
+       GList *files;
+
+       if (!stat_utf8(path_utf8, &st))
+               {
+               st.st_size = 0;
+               st.st_mtime = 0;
+               }
+
+       if (S_ISDIR(st.st_mode))
+               return file_data_new(path_utf8, &st, TRUE);
+       
+       dir = remove_level_from_path(path_utf8);
+       
+       filelist_read_real(dir, &files, NULL, TRUE);
+       
+       fd = g_hash_table_lookup(file_data_pool, path_utf8);
+       g_assert(fd);
+       file_data_ref(fd);
+       
+       filelist_free(files);
+       g_free(dir);
+       return fd;
 }
 
+
 void filelist_free(GList *list)
 {
        GList *work;
@@ -1140,7 +1206,7 @@ GList *filelist_from_path_list(GList *list)
                path = work->data;
                work = work->next;
 
-               new_list = g_list_prepend(new_list, file_data_new_simple(path));
+               new_list = g_list_prepend(new_list, file_data_new_group(path));
                }
 
        return g_list_reverse(new_list);
@@ -1255,6 +1321,65 @@ GList *filelist_recursive(FileData *dir_fd)
        return list;
 }
 
+/*
+ *-----------------------------------------------------------------------------
+ * file modification support
+ *-----------------------------------------------------------------------------
+ */
+
+
+void file_data_change_info_free(FileDataChangeInfo *fdci, FileData *fd)
+{
+       if (!fdci && fd) fdci = fd->change;
+
+       if (!fdci) return;
+
+       g_free(fdci->source);
+       g_free(fdci->dest);
+
+       g_free(fdci);
+
+       if (fd) fd->change = NULL;
+}
+
+static gboolean file_data_can_write_directly(FileData *fd)
+{
+       return filter_name_is_writable(fd->extension);
+}
+
+static gboolean file_data_can_write_sidecar(FileData *fd)
+{
+       return filter_name_allow_sidecar(fd->extension) && !filter_name_is_writable(fd->extension);
+}
+
+gchar *file_data_get_sidecar_path(FileData *fd, gboolean existing_only)
+{
+       gchar *sidecar_path = NULL;
+       GList *work;
+       
+       if (!file_data_can_write_sidecar(fd)) return NULL;
+       
+       work = fd->parent ? fd->parent->sidecar_files : fd->sidecar_files;
+       while (work)
+               {
+               FileData *sfd = work->data;
+               work = work->next;
+               if (g_ascii_strcasecmp(sfd->extension, ".xmp") == 0)
+                       {
+                       sidecar_path = g_strdup(sfd->path);
+                       break;
+                       }
+               }
+       
+       if (!existing_only && !sidecar_path)
+               {
+               gchar *base = g_strndup(fd->path, fd->extension - fd->path);
+               sidecar_path = g_strconcat(base, ".xmp", NULL);
+               g_free(base);
+               }
+
+       return sidecar_path;
+}
 
 /*
  * marks and orientation
@@ -1269,12 +1394,12 @@ gboolean file_data_get_mark(FileData *fd, gint n)
 {
        gboolean valid = (fd->valid_marks & (1 << n));
        
-       if (file_data_get_mark_func[n] && !valid) 
+       if (file_data_get_mark_func[n] && !valid)
                {
                guint old = fd->marks;
                gboolean value = (file_data_get_mark_func[n])(fd, n, file_data_mark_func_data[n]);
                
-               if (!value != !(fd->marks & (1 << n))) 
+               if (!value != !(fd->marks & (1 << n)))
                        {
                        fd->marks = fd->marks ^ (1 << n);
                        }
@@ -1305,7 +1430,7 @@ void file_data_set_mark(FileData *fd, gint n, gboolean value)
        guint old;
        if (!value == !file_data_get_mark(fd, n)) return;
        
-       if (file_data_set_mark_func[n]) 
+       if (file_data_set_mark_func[n])
                {
                (file_data_set_mark_func[n])(fd, n, value, file_data_mark_func_data[n]);
                }
@@ -1980,7 +2105,7 @@ gint file_data_verify_ci(FileData *fd)
                
                if (options->metadata.save_in_image_file)
                        {
-                       if (file_data_can_write_directly(fd)) 
+                       if (file_data_can_write_directly(fd))
                                {
                                /* we can write the file directly */
                                if (access_file(fd->path, W_OK))
@@ -1996,7 +2121,7 @@ gint file_data_verify_ci(FileData *fd)
                                                }
                                        }
                                }
-                       else if (file_data_can_write_sidecar(fd)) 
+                       else if (file_data_can_write_sidecar(fd))
                                {
                                /* we can write sidecar */
                                gchar *sidecar = file_data_get_sidecar_path(fd, FALSE);
@@ -2101,7 +2226,7 @@ gint file_data_verify_ci(FileData *fd)
                        }
                else if (!access_file(dest_dir, W_OK))
                        {
-                       ret |= CHANGE_NO_WRITE_PERM_DEST_DIR;
+                       ret |= CHANGE_WARN_NO_WRITE_PERM_DEST_DIR;
                        DEBUG_1("Change checked: destination dir is readonly: %s -> %s", fd->path, fd->change->dest);
                        }
                else if (!same)
@@ -2178,7 +2303,7 @@ gchar *file_data_get_error_string(gint error)
                g_string_append(result, _("destination can't be overwritten"));
                }
 
-       if (error & CHANGE_NO_WRITE_PERM_DEST_DIR)
+       if (error & CHANGE_WARN_NO_WRITE_PERM_DEST_DIR)
                {
                if (result->len > 0) g_string_append(result, ", ");
                g_string_append(result, _("destination directory is not writable"));
@@ -2410,7 +2535,7 @@ gboolean file_data_apply_ci(FileData *fd)
                        }
                else
                        {
-                       file_data_set_path(fd, fd->change->dest, NULL);
+                       file_data_set_path(fd, fd->change->dest);
                        }
                }
        file_data_increment_version(fd);
@@ -2455,29 +2580,6 @@ static gboolean file_data_list_contains_whole_group(GList *list, FileData *fd)
        return TRUE;
 }
 
-#if 0
-static gboolean file_data_list_dump(GList *list)
-{
-       GList *work, *work2;
-
-       work = list;
-       while (work)
-               {
-               FileData *fd = work->data;
-               printf("%s\n", fd->name);
-               work2 = fd->sidecar_files;
-               while (work2)
-                       {
-                       FileData *fd = work2->data;
-                       printf("       %s\n", fd->name);
-                       work2 = work2->next;
-                       }
-               work = work->next;
-               }
-       return TRUE;
-}
-#endif
-
 GList *file_data_process_groups_in_selection(GList *list, gboolean ungroup, GList **ungrouped_list)
 {
        GList *out = NULL;
@@ -2491,10 +2593,10 @@ GList *file_data_process_groups_in_selection(GList *list, gboolean ungroup, GLis
                        FileData *fd = work->data;
                        work = work->next;
                
-                       if (!file_data_list_contains_whole_group(list, fd)) 
+                       if (!file_data_list_contains_whole_group(list, fd))
                                {
                                file_data_disable_grouping(fd, TRUE);
-                               if (ungrouped_list) 
+                               if (ungrouped_list)
                                        {
                                        *ungrouped_list = g_list_prepend(*ungrouped_list, file_data_ref(fd));
                                        }
@@ -2502,7 +2604,7 @@ GList *file_data_process_groups_in_selection(GList *list, gboolean ungroup, GLis
                        }
                }
        
-       /* remove sidecars from the list, 
+       /* remove sidecars from the list,
           they can be still acessed via main_fd->sidecar_files */
        work = list;
        while (work)
@@ -2536,6 +2638,12 @@ GList *file_data_process_groups_in_selection(GList *list, gboolean ungroup, GLis
    implementation in view_file_list.c */
 
 
+typedef struct _NotifyIdleData NotifyIdleData;
+
+struct _NotifyIdleData {
+       FileData *fd;
+       NotifyType type;
+};
 
 
 typedef struct _NotifyData NotifyData;
@@ -2609,17 +2717,29 @@ gboolean file_data_unregister_notify_func(FileDataNotifyFunc func, gpointer data
 }
 
 
-void file_data_send_notification(FileData *fd, NotifyType type)
+gboolean file_data_send_notification_idle_cb(gpointer data)
 {
+       NotifyIdleData *nid = (NotifyIdleData *)data;
        GList *work = notify_func_list;
 
        while (work)
                {
                NotifyData *nd = (NotifyData *)work->data;
                
-               nd->func(fd, type, nd->data);
+               nd->func(nid->fd, nid->type, nd->data);
                work = work->next;
                }
+       file_data_unref(nid->fd);
+       g_free(nid);
+       return FALSE;
+}
+
+void file_data_send_notification(FileData *fd, NotifyType type)
+{
+       NotifyIdleData *nid = g_new0(NotifyIdleData, 1);
+       nid->fd = file_data_ref(fd);
+       nid->type = type;
+       g_idle_add_full(G_PRIORITY_HIGH, file_data_send_notification_idle_cb, nid, NULL);
 }
 
 static GHashTable *file_data_monitor_pool = NULL;