Fix #323: Rating system
[geeqie.git] / src / filedata.c
index 3126a4e..b690e81 100644 (file)
@@ -427,6 +427,8 @@ static FileData *file_data_new(const gchar *path_utf8, struct stat *st, gboolean
        fd->mode = st->st_mode;
        fd->ref = 1;
        fd->magick = FD_MAGICK;
+       fd->exifdate = 0;
+       fd->rating = 0;
 
        if (disable_sidecars) fd->disable_grouping = TRUE;
 
@@ -465,21 +467,6 @@ FileData *file_data_new_simple(const gchar *path_utf8)
        return fd;
 }
 
-void init_exif_time_data(GList *files)
-{
-       FileData *file;
-       DEBUG_1("%s init_exif_time_data: ...", get_exec_time());
-       while (files)
-               {
-               file = files->data;
-
-               if (file)
-                       file->exifdate = 0;
-
-               files = files->next;
-               }
-}
-
 void read_exif_time_data(FileData *file)
 {
        if (file->exifdate > 0)
@@ -528,6 +515,24 @@ void set_exif_time_data(GList *files)
                }
 }
 
+void set_rating_data(GList *files)
+{
+       gchar *rating_str;
+       DEBUG_1("%s set_rating_data: ...", get_exec_time());
+
+       while (files)
+               {
+               FileData *file = files->data;
+               rating_str = metadata_read_string(file, RATING_KEY, METADATA_PLAIN);
+               if (rating_str )
+                       {
+                       file->rating = atoi(rating_str);
+                       g_free(rating_str);
+                       }
+               files = files->next;
+               }
+}
+
 FileData *file_data_new_no_grouping(const gchar *path_utf8)
 {
        struct stat st;
@@ -1040,6 +1045,11 @@ gint filelist_sort_compare_filedata(FileData *fa, FileData *fb)
                        if (fa->exifdate > fb->exifdate) return 1;
                        /* fall back to name */
                        break;
+               case SORT_RATING:
+                       if (fa->rating < fb->rating) return -1;
+                       if (fa->rating > fb->rating) return 1;
+                       /* fall back to name */
+                       break;
 #ifdef HAVE_STRVERSCMP
                case SORT_NUMBER:
                        ret = strverscmp(fa->name, fb->name);
@@ -1095,6 +1105,10 @@ GList *filelist_sort(GList *list, SortType method, gboolean ascend)
                {
                set_exif_time_data(list);
                }
+       if (method == SORT_RATING)
+               {
+               set_rating_data(list);
+               }
        return filelist_sort_full(list, method, ascend, (GCompareFunc) filelist_sort_file_cb);
 }
 
@@ -1330,9 +1344,6 @@ static gboolean filelist_read_real(const gchar *dir_path, GList **files, GList *
                }
        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;
 }
 
@@ -1726,7 +1737,7 @@ gboolean file_data_register_mark_func(gint n, FileDataGetMarkFunc get_mark_func,
         file_data_mark_func_data[n] = data;
         file_data_destroy_mark_func[n] = notify;
 
-        if (get_mark_func)
+       if (get_mark_func && file_data_pool)
                {
                /* this effectively changes all known files */
                g_hash_table_foreach(file_data_pool, file_data_notify_mark_func, NULL);