improved xmp vs. legacy metadata handling
authorVladimir Nadvornik <nadvornik@suse.cz>
Wed, 26 Mar 2008 08:00:46 +0000 (08:00 +0000)
committerVladimir Nadvornik <nadvornik@suse.cz>
Wed, 26 Mar 2008 08:00:46 +0000 (08:00 +0000)
src/bar_exif.c
src/bar_info.c
src/exif-common.c

index c14b3ed..38b2c51 100644 (file)
@@ -314,8 +314,6 @@ void bar_exif_set(GtkWidget *bar, FileData *fd)
 {
        ExifBar *eb;
 
-       g_assert(fd);
-       
        eb = g_object_get_data(G_OBJECT(bar), "bar_exif_data");
        if (!eb) return;
 
index 4fa099e..9ea1e2a 100644 (file)
@@ -186,6 +186,26 @@ static gint comment_file_read(gchar *path, GList **keywords, gchar **comment)
        return TRUE;
 }
 
+static gint comment_delete_legacy(FileData *fd)
+{
+       gchar *comment_path;
+       gchar *comment_pathl;
+       gint success = FALSE;
+       if (!fd) return FALSE;
+
+       comment_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
+       if (!comment_path) return FALSE;
+
+       comment_pathl = path_from_utf8(comment_path);
+
+       success = !unlink(comment_pathl);
+
+       g_free(comment_pathl);
+       g_free(comment_path);
+
+       return success;
+}
+
 static gint comment_legacy_read(FileData *fd, GList **keywords, gchar **comment)
 {
        gchar *comment_path;
@@ -212,6 +232,7 @@ gchar *keyword_key = "Xmp.dc.subject";
 static gint comment_xmp_read(FileData *fd, GList **keywords, gchar **comment)
 {
        ExifData *exif = exif_read_fd(fd, FALSE);
+       gint success;
        if (!exif) return FALSE;
 
        if (comment)
@@ -237,7 +258,10 @@ static gint comment_xmp_read(FileData *fd, GList **keywords, gchar **comment)
                }
                
        exif_free(exif);
-       return TRUE;
+       
+       success = *comment || *keywords;
+       
+       return success;
 }
 
 static gint comment_xmp_write(FileData *fd, GList *keywords, const gchar *comment)
@@ -287,16 +311,59 @@ static gint comment_xmp_write(FileData *fd, GList *keywords, const gchar *commen
 
 gint comment_write(FileData *fd, GList *keywords, const gchar *comment)
 {
-       if (comment_xmp_write(fd, keywords, comment)) return TRUE;
+       if (!fd) return FALSE;
+
+       if (enable_metadata_dirs && /* FIXME - use dedicated option */
+           comment_xmp_write(fd, keywords, comment))
+               {
+               comment_delete_legacy(fd);
+               return TRUE;
+               }
 
        return comment_legacy_write(fd, keywords, comment);
 }
 
 gint comment_read(FileData *fd, GList **keywords, gchar **comment)
 {
-       if (comment_xmp_read(fd, keywords, comment)) return TRUE;
+       GList *keywords1, *keywords2;
+       gchar *comment1, *comment2;
+       gint res1, res2;
+
+       if (!fd) return FALSE;
+
+       res1 = comment_xmp_read(fd, &keywords1, &comment1);
+       res2 = comment_legacy_read(fd, &keywords2, &comment2);
+        
+       if (!res1 && !res2)
+               {
+               return FALSE;
+               }
+       
+       if (keywords)
+               {
+               if (res1 && res2)
+                       *keywords = g_list_concat(keywords1, keywords2);
+               else
+                       *keywords = res1 ? keywords1 : keywords2;
+               }
+       else
+               {
+               if (res1) string_list_free(keywords1);
+               if (res2) string_list_free(keywords2);
+               }
+               
 
-       return comment_legacy_read(fd, keywords, comment);
+       if (comment)
+               {
+               if (res1 && res2 && comment1 && comment2 && comment1[0] && comment2[0])
+                       *comment = g_strdup_printf("%s\n%s", comment1, comment2);
+               else
+                       *comment = res1 ? comment1 : comment2;
+               }
+       if (res1 && (!comment || *comment != comment1)) g_free(comment1);
+       if (res2 && (!comment || *comment != comment2)) g_free(comment2);
+       
+       return TRUE;
 }
 
 
index 9087267..4fb9b0d 100644 (file)
@@ -329,9 +329,13 @@ gchar *exif_get_data_as_text(ExifData *exif, const gchar *key)
 
 ExifData *exif_read_fd(FileData *fd, gint parse_color_profile)
 {
-       GList *work = fd->parent ? fd->parent->sidecar_files : fd->sidecar_files;
+       GList *work;
        gchar *sidecar_path = NULL;
 
+       if (!fd) return NULL;
+
+       work = fd->parent ? fd->parent->sidecar_files : fd->sidecar_files;
+
        if (strcasecmp(fd->extension, ".cr2") == 0 || // FIXME: list of formats that can have xmp sidecar, make it configurable
            strcasecmp(fd->extension, ".nef") == 0)
                {