Fix #381: Feature-Request: Make JPEG comment available for overlays
[geeqie.git] / src / exiv2.cc
index d9d66c6..0ad5c61 100644 (file)
@@ -1,12 +1,21 @@
 /*
- * Geeqie
- * Copyright (C) 2008 - 2009 The Geeqie Team
+ * Copyright (C) 2008 - 2016 The Geeqie Team
  *
  * Author: Vladimir Nadvornik
  *
- * This software is released under the GNU General Public License (GNU GPL).
- * Please read the included file COPYING for more information.
- * This software comes with no warranty of any kind, use at your own risk!
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include "config.h"
@@ -16,6 +25,7 @@
 #include <exiv2/image.hpp>
 #include <exiv2/exif.hpp>
 #include <iostream>
+#include <string>
 
 // EXIV2_TEST_VERSION is defined in Exiv2 0.15 and newer.
 #ifndef EXIV2_TEST_VERSION
@@ -76,12 +86,42 @@ struct _AltKey
 
 /* this is a list of keys that should be converted, even with the older Exiv2 which does not support it directly */
 static const AltKey alt_keys[] = {
-       {"Xmp.tiff.Orientation", "Exif.Image.Orientation", NULL},
-       {"Xmp.dc.subject", NULL, "Iptc.Application2.Keywords"},
-       {"Xmp.dc.description", NULL, "Iptc.Application2.Caption"},
+       {"Xmp.tiff.Orientation",                "Exif.Image.Orientation",       NULL},
+       {"Xmp.dc.title",                        NULL,                           "Iptc.Application2.ObjectName"          },
+       {"Xmp.photoshop.Urgency",               NULL,                           "Iptc.Application2.Urgency"             },
+       {"Xmp.photoshop.Category",              NULL,                           "Iptc.Application2.Category"            },
+       {"Xmp.photoshop.SupplementalCategory",  NULL,                           "Iptc.Application2.SuppCategory"        },
+       {"Xmp.dc.subject",                      NULL,                           "Iptc.Application2.Keywords"            },
+       {"Xmp.iptc.Location",                   NULL,                           "Iptc.Application2.LocationName"        },
+       {"Xmp.photoshop.Instruction",           NULL,                           "Iptc.Application2.SpecialInstructions" },
+       {"Xmp.photoshop.DateCreated",           NULL,                           "Iptc.Application2.DateCreated"         },
+       {"Xmp.dc.creator",                      NULL,                           "Iptc.Application2.Byline"              },
+       {"Xmp.photoshop.AuthorsPosition",       NULL,                           "Iptc.Application2.BylineTitle"         },
+       {"Xmp.photoshop.City",                  NULL,                           "Iptc.Application2.City"                },
+       {"Xmp.photoshop.State",                 NULL,                           "Iptc.Application2.ProvinceState"       },
+       {"Xmp.iptc.CountryCode",                NULL,                           "Iptc.Application2.CountryCode"         },
+       {"Xmp.photoshop.Country",               NULL,                           "Iptc.Application2.CountryName"         },
+       {"Xmp.photoshop.TransmissionReference", NULL,                           "Iptc.Application2.TransmissionReference"},
+       {"Xmp.photoshop.Headline",              NULL,                           "Iptc.Application2.Headline"            },
+       {"Xmp.photoshop.Credit",                NULL,                           "Iptc.Application2.Credit"              },
+       {"Xmp.photoshop.Source",                NULL,                           "Iptc.Application2.Source"              },
+       {"Xmp.dc.rights",                       NULL,                           "Iptc.Application2.Copyright"           },
+       {"Xmp.dc.description",                  NULL,                           "Iptc.Application2.Caption"             },
+       {"Xmp.photoshop.CaptionWriter",         NULL,                           "Iptc.Application2.Writer"              },
        {NULL, NULL, NULL}
        };
 
+static void _debug_exception(const char* file,
+                             int line,
+                             const char* func,
+                             Exiv2::AnyError& e)
+{
+       gchar *str = g_locale_from_utf8(e.what(), -1, NULL, NULL, NULL);
+       DEBUG_1("%s:%d:%s:Exiv2: %s", file, line, func, str);
+       g_free(str);
+}
+
+#define debug_exception(e) _debug_exception(__FILE__, __LINE__, __func__, e)
 
 struct _ExifData
 {
@@ -94,7 +134,7 @@ struct _ExifData
        virtual ~_ExifData()
        {
        }
-       
+
        virtual void writeMetadata(gchar *path = NULL)
        {
                g_critical("Unsupported method of writing metadata");
@@ -106,7 +146,7 @@ struct _ExifData
        }
 
        virtual Exiv2::Image *image() = 0;
-       
+
        virtual Exiv2::ExifData &exifData() = 0;
 
        virtual Exiv2::IptcData &iptcData() = 0;
@@ -118,6 +158,10 @@ struct _ExifData
        virtual void add_jpeg_color_profile(unsigned char *cp_data, guint cp_length) = 0;
 
        virtual guchar *get_jpeg_color_profile(guint *data_len) = 0;
+
+       virtual std::string image_comment() const = 0;
+
+       virtual void set_image_comment(const std::string& comment) = 0;
 };
 
 // This allows read-only access to the original metadata
@@ -129,6 +173,14 @@ protected:
        /* the icc profile in jpeg is not technically exif - store it here */
        unsigned char *cp_data_;
        guint cp_length_;
+       gboolean valid_;
+       gchar *pathl_;
+
+       Exiv2::ExifData emptyExifData_;
+       Exiv2::IptcData emptyIptcData_;
+#if EXIV2_TEST_VERSION(0,16,0)
+       Exiv2::XmpData emptyXmpData_;
+#endif
 
 public:
        _ExifDataOriginal(Exiv2::Image::AutoPtr image)
@@ -136,68 +188,82 @@ public:
                cp_data_ = NULL;
                cp_length_ = 0;
                image_ = image;
+               valid_ = TRUE;
        }
 
        _ExifDataOriginal(gchar *path)
        {
                cp_data_ = NULL;
                cp_length_ = 0;
-               gchar *pathl = path_from_utf8(path);
-               image_ = Exiv2::ImageFactory::open(pathl);
-               g_free(pathl);
-//             g_assert (image.get() != 0);
-               image_->readMetadata();
+               valid_ = TRUE;
 
-#if EXIV2_TEST_VERSION(0,16,0)
-               if (image_->mimeType() == "application/rdf+xml")
+               pathl_ = path_from_utf8(path);
+               try
                        {
-                       //Exiv2 sidecar converts xmp to exif and iptc, we don't want it.
-                       image_->clearExifData();
-                       image_->clearIptcData();
-                       }
+                       image_ = Exiv2::ImageFactory::open(pathl_);
+//                     g_assert (image.get() != 0);
+                       image_->readMetadata();
+
+#if EXIV2_TEST_VERSION(0,16,0)
+                       if (image_->mimeType() == "application/rdf+xml")
+                               {
+                               //Exiv2 sidecar converts xmp to exif and iptc, we don't want it.
+                               image_->clearExifData();
+                               image_->clearIptcData();
+                               }
 #endif
 
 #if EXIV2_TEST_VERSION(0,14,0)
-               if (image_->mimeType() == "image/jpeg")
-                       {
-                       /* try to get jpeg color profile */
-                       Exiv2::BasicIo &io = image_->io();
-                       gint open = io.isopen();
-                       if (!open) io.open();
-                       if (io.isopen())
+                       if (image_->mimeType() == "image/jpeg")
                                {
-                               unsigned char *mapped = (unsigned char*)io.mmap();
-                               if (mapped) exif_jpeg_parse_color(this, mapped, io.size());
-                               io.munmap();
+                               /* try to get jpeg color profile */
+                               Exiv2::BasicIo &io = image_->io();
+                               gint open = io.isopen();
+                               if (!open) io.open();
+                               if (io.isopen())
+                                       {
+                                       unsigned char *mapped = (unsigned char*)io.mmap();
+                                       if (mapped) exif_jpeg_parse_color(this, mapped, io.size());
+                                       io.munmap();
+                                       }
+                               if (!open) io.close();
                                }
-                       if (!open) io.close();
-                       }
 #endif
+                       }
+               catch (Exiv2::AnyError& e)
+                       {
+                       valid_ = FALSE;
+                       }
        }
-       
+
        virtual ~_ExifDataOriginal()
        {
                if (cp_data_) g_free(cp_data_);
+               if (pathl_) g_free(pathl_);
        }
-       
+
        virtual Exiv2::Image *image()
        {
+               if (!valid_) return NULL;
                return image_.get();
        }
-       
+
        virtual Exiv2::ExifData &exifData ()
        {
+               if (!valid_) return emptyExifData_;
                return image_->exifData();
        }
 
        virtual Exiv2::IptcData &iptcData ()
        {
+               if (!valid_) return emptyIptcData_;
                return image_->iptcData();
        }
 
 #if EXIV2_TEST_VERSION(0,16,0)
        virtual Exiv2::XmpData &xmpData ()
        {
+               if (!valid_) return emptyXmpData_;
                return image_->xmpData();
        }
 #endif
@@ -218,6 +284,17 @@ public:
                }
                return NULL;
        }
+
+       virtual std::string image_comment() const
+       {
+               return image_.get() ? image_->comment() : "";
+       }
+
+       virtual void set_image_comment(const std::string& comment)
+       {
+               if (image_.get())
+                       image_->setComment(comment);
+       }
 };
 
 extern "C" {
@@ -257,7 +334,14 @@ public:
                exifData_ = imageData_->exifData();
                iptcData_ = imageData_->iptcData();
 #if EXIV2_TEST_VERSION(0,17,0)
-               syncExifWithXmp(exifData_, xmpData_);
+               try
+                       {
+                       syncExifWithXmp(exifData_, xmpData_);
+                       }
+               catch (...)
+                       {
+                       DEBUG_1("Exiv2: Catching bug\n");
+                       }
 #endif
                if (modified_xmp)
                        {
@@ -281,27 +365,30 @@ public:
                if (!path)
                        {
 #if EXIV2_TEST_VERSION(0,17,0)
-                       if (options->metadata.save_legacy_IPTC) 
+                       if (options->metadata.save_legacy_IPTC)
                                copyXmpToIptc(xmpData_, iptcData_);
                        else
                                iptcData_.clear();
 
                        copyXmpToExif(xmpData_, exifData_);
 #endif
-                       imageData_->image()->setExifData(exifData_);
-                       imageData_->image()->setIptcData(iptcData_);
+                       Exiv2::Image *image = imageData_->image();
+
+                       if (!image) Exiv2::Error(21);
+                       image->setExifData(exifData_);
+                       image->setIptcData(iptcData_);
 #if EXIV2_TEST_VERSION(0,16,0)
-                       imageData_->image()->setXmpData(xmpData_);
+                       image->setXmpData(xmpData_);
 #endif
-                       imageData_->image()->writeMetadata();
-                       } 
+                       image->writeMetadata();
+                       }
                else
                        {
 #if EXIV2_TEST_VERSION(0,17,0)
                        gchar *pathl = path_from_utf8(path);;
 
                        Exiv2::Image::AutoPtr sidecar = Exiv2::ImageFactory::create(Exiv2::ImageType::xmp, pathl);
-                               
+
                        g_free(pathl);
 
                        sidecar->setXmpData(xmpData_);
@@ -311,12 +398,12 @@ public:
 #endif
                        }
        }
-       
+
        virtual Exiv2::Image *image()
        {
                return imageData_->image();
        }
-       
+
        virtual Exiv2::ExifData &exifData ()
        {
                return exifData_;
@@ -343,6 +430,16 @@ public:
        {
                return imageData_->get_jpeg_color_profile(data_len);
        }
+
+       virtual std::string image_comment() const
+       {
+               return imageData_->image_comment();
+       }
+
+       virtual void set_image_comment(const std::string& comment)
+       {
+               imageData_->set_image_comment(comment);
+       }
 };
 
 
@@ -350,6 +447,16 @@ public:
 
 extern "C" {
 
+
+void exif_init(void)
+{
+#ifdef EXV_ENABLE_NLS
+       bind_textdomain_codeset (EXV_PACKAGE, "UTF-8");
+#endif
+}
+
+
+
 static void _ExifDataProcessed_update_xmp(gpointer key, gpointer value, gpointer data)
 {
        exif_update_metadata((ExifData *)data, (gchar *)key, (GList *)value);
@@ -362,10 +469,10 @@ ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
                return new _ExifDataProcessed(path, sidecar_path, modified_xmp);
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
                return NULL;
        }
-       
+
 }
 
 gboolean exif_write(ExifData *exif)
@@ -375,7 +482,7 @@ gboolean exif_write(ExifData *exif)
                return TRUE;
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
                return FALSE;
        }
 }
@@ -387,10 +494,10 @@ gboolean exif_write_sidecar(ExifData *exif, gchar *path)
                return TRUE;
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
                return FALSE;
        }
-       
+
 }
 
 
@@ -436,7 +543,7 @@ ExifItem *exif_get_item(ExifData *exif, const gchar *key)
                return (ExifItem *)item;
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
                return NULL;
        }
 }
@@ -473,7 +580,7 @@ ExifItem *exif_add_item(ExifData *exif, const gchar *key)
                return (ExifItem *)item;
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
                return NULL;
        }
 }
@@ -508,10 +615,10 @@ ExifItem *exif_get_first_item(ExifData *exif)
                        }
 #endif
                return NULL;
-                       
+
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
                return NULL;
        }
 }
@@ -542,7 +649,7 @@ ExifItem *exif_get_next_item(ExifData *exif)
                return NULL;
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
                return NULL;
        }
 }
@@ -554,7 +661,7 @@ char *exif_item_get_tag_name(ExifItem *item)
                return g_strdup(((Exiv2::Metadatum *)item)->key().c_str());
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
                return NULL;
        }
 }
@@ -566,7 +673,7 @@ guint exif_item_get_tag_id(ExifItem *item)
                return ((Exiv2::Metadatum *)item)->tag();
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
                return 0;
        }
 }
@@ -578,7 +685,7 @@ guint exif_item_get_elements(ExifItem *item)
                return ((Exiv2::Metadatum *)item)->count();
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
                return 0;
        }
 }
@@ -595,7 +702,7 @@ char *exif_item_get_data(ExifItem *item, guint *data_len)
                return data;
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
                return NULL;
        }
 }
@@ -604,10 +711,10 @@ char *exif_item_get_description(ExifItem *item)
 {
        try {
                if (!item) return NULL;
-               return g_locale_to_utf8(((Exiv2::Metadatum *)item)->tagLabel().c_str(), -1, NULL, NULL, NULL);
+               return utf8_validate_or_convert(((Exiv2::Metadatum *)item)->tagLabel().c_str());
        }
        catch (std::exception& e) {
-//             std::cout << "Caught Exiv2 exception '" << e << "'\n";
+//             debug_exception(e);
                return NULL;
        }
 }
@@ -642,8 +749,8 @@ static guint format_id_trans_tbl [] = {
        EXIF_FORMAT_STRING,
        EXIF_FORMAT_STRING
        };
-       
-       
+
+
 
 guint exif_item_get_format_id(ExifItem *item)
 {
@@ -654,19 +761,19 @@ guint exif_item_get_format_id(ExifItem *item)
                return format_id_trans_tbl[id];
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
                return EXIF_FORMAT_UNKNOWN;
        }
 }
 
-const char *exif_item_get_format_name(ExifItem *item, gint brief)
+const char *exif_item_get_format_name(ExifItem *item, gboolean brief)
 {
        try {
                if (!item) return NULL;
                return ((Exiv2::Metadatum *)item)->typeName();
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
                return NULL;
        }
 }
@@ -678,7 +785,7 @@ gchar *exif_item_get_data_as_text(ExifItem *item)
                if (!item) return NULL;
                Exiv2::Metadatum *metadatum = (Exiv2::Metadatum *)item;
 #if EXIV2_TEST_VERSION(0,17,0)
-               return g_locale_to_utf8(metadatum->print().c_str(), -1, NULL, NULL, NULL);
+               return utf8_validate_or_convert(metadatum->print().c_str());
 #else
                std::stringstream str;
                Exiv2::Exifdatum *exifdatum;
@@ -695,7 +802,7 @@ gchar *exif_item_get_data_as_text(ExifItem *item)
                        str << *xmpdatum;
 #endif
 
-               return g_locale_to_utf8(str.str().c_str(), -1, NULL, NULL, NULL);
+               return utf8_validate_or_convert(str.str().c_str());
 #endif
        }
        catch (Exiv2::AnyError& e) {
@@ -720,8 +827,7 @@ gchar *exif_item_get_string(ExifItem *item, int idx)
                        if (pos != std::string::npos) str = str.substr(pos+1);
                        }
 
-//             return g_locale_to_utf8(str.c_str(), -1, NULL, NULL, NULL); // FIXME
-               return g_strdup(str.c_str());
+               return utf8_validate_or_convert(str.c_str());
        }
        catch (Exiv2::AnyError& e) {
                return NULL;
@@ -732,12 +838,12 @@ gchar *exif_item_get_string(ExifItem *item, int idx)
 gint exif_item_get_integer(ExifItem *item, gint *value)
 {
        try {
-               if (!item) return 0;
+               if (!item || exif_item_get_elements(item) == 0) return 0;
                *value = ((Exiv2::Metadatum *)item)->toLong();
                return 1;
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
                return 0;
        }
 }
@@ -755,7 +861,7 @@ ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
                return &ret;
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
                return NULL;
        }
 }
@@ -764,18 +870,33 @@ gchar *exif_get_tag_description_by_key(const gchar *key)
 {
        try {
                Exiv2::ExifKey ekey(key);
-               return g_locale_to_utf8(Exiv2::ExifTags::tagLabel(ekey.tag(), ekey.ifdId ()), -1, NULL, NULL, NULL);
+               return utf8_validate_or_convert(ekey.tagLabel().c_str());
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
-               return NULL;
+               try {
+                       Exiv2::IptcKey ikey(key);
+                       return utf8_validate_or_convert(ikey.tagLabel().c_str());
+               }
+               catch (Exiv2::AnyError& e) {
+                       try {
+#if EXIV2_TEST_VERSION(0,16,0)
+                               Exiv2::XmpKey xkey(key);
+                               return utf8_validate_or_convert(xkey.tagLabel().c_str());
+#endif
+                       }
+                       catch (Exiv2::AnyError& e) {
+                               debug_exception(e);
+                               return NULL;
+                       }
+               }
        }
+       return NULL;
 }
 
 static const AltKey *find_alt_key(const gchar *xmp_key)
 {
        gint i = 0;
-       
+
        while (alt_keys[i].xmp_key)
                {
                if (strcmp(xmp_key, alt_keys[i].xmp_key) == 0) return &alt_keys[i];
@@ -791,7 +912,7 @@ static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const
 
                try {
                        Exiv2::ExifKey ekey(key);
-                       
+
                        Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
                        while (pos != exif->exifData().end())
                                {
@@ -807,7 +928,7 @@ static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const
                }
                catch (Exiv2::AnyError& e) {
 #if EXIV2_TEST_VERSION(0,16,0)
-                       try 
+                       try
 #endif
                        {
                                Exiv2::IptcKey ekey(key);
@@ -845,7 +966,7 @@ static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const
                return 1;
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
                return 0;
        }
 }
@@ -853,7 +974,7 @@ static gint exif_update_metadata_simple(ExifData *exif, const gchar *key, const
 gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
 {
        gint ret = exif_update_metadata_simple(exif, key, values);
-       
+
        if (
 #if !EXIV2_TEST_VERSION(0,17,0)
            TRUE || /* no conversion support */
@@ -875,11 +996,12 @@ gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
 }
 
 
-static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item)
+static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item, MetadataFormat format, const Exiv2::ExifData *metadata)
 {
 #if EXIV2_TEST_VERSION(0,16,0)
        Exiv2::TypeId id = item.typeId();
-       if (id == Exiv2::asciiString ||
+       if (format == METADATA_FORMATTED ||
+           id == Exiv2::asciiString ||
            id == Exiv2::undefined ||
            id == Exiv2::string ||
            id == Exiv2::date ||
@@ -889,15 +1011,52 @@ static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item)
            id == Exiv2::comment
            )
                {
-#endif 
+#endif
                /* read as a single entry */
-               std::string str = item.toString();
+               std::string str;
+
+               if (format == METADATA_FORMATTED)
+                       {
+#if EXIV2_TEST_VERSION(0,17,0)
+                       str = item.print(
+#if EXIV2_TEST_VERSION(0,18,0)
+                                       metadata
+#endif
+                                       );
+#else
+                       std::stringstream stream;
+                       Exiv2::Exifdatum *exifdatum;
+                       Exiv2::Iptcdatum *iptcdatum;
+#if EXIV2_TEST_VERSION(0,16,0)
+                       Exiv2::Xmpdatum *xmpdatum;
+#endif
+                       if ((exifdatum = dynamic_cast<Exiv2::Exifdatum *>(&item)))
+                               stream << *exifdatum;
+                       else if ((iptcdatum = dynamic_cast<Exiv2::Iptcdatum *>(&item)))
+                               stream << *iptcdatum;
+#if EXIV2_TEST_VERSION(0,16,0)
+                       else if ((xmpdatum = dynamic_cast<Exiv2::Xmpdatum *>(&item)))
+                               stream << *xmpdatum;
+#endif
+                       str = stream.str();
+#endif
+                       if (str.length() > 1024)
+                               {
+                               /* truncate very long strings, they cause problems in gui */
+                               str.erase(1024);
+                               str.append("...");
+                               }
+                       }
+               else
+                       {
+                       str = item.toString();
+                       }
                if (str.length() > 5 && str.substr(0, 5) == "lang=")
                        {
                        std::string::size_type pos = str.find_first_of(' ');
                        if (pos != std::string::npos) str = str.substr(pos+1);
                        }
-               list = g_list_append(list, utf8_validate_or_convert(str.c_str())); 
+               list = g_list_append(list, utf8_validate_or_convert(str.c_str()));
 #if EXIV2_TEST_VERSION(0,16,0)
                }
        else
@@ -905,22 +1064,21 @@ static GList *exif_add_value_to_glist(GList *list, Exiv2::Metadatum &item)
                /* read as a list */
                gint i;
                for (i = 0; i < item.count(); i++)
-                       list = g_list_append(list, utf8_validate_or_convert(item.toString(i).c_str())); 
+                       list = g_list_append(list, utf8_validate_or_convert(item.toString(i).c_str()));
                }
-#endif 
+#endif
        return list;
 }
 
-static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key)
+static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key, MetadataFormat format)
 {
        GList *list = NULL;
        try {
                try {
                        Exiv2::ExifKey ekey(key);
-                       
                        Exiv2::ExifData::iterator pos = exif->exifData().findKey(ekey);
                        if (pos != exif->exifData().end())
-                               list = exif_add_value_to_glist(list, *pos);
+                               list = exif_add_value_to_glist(list, *pos, format, &exif->exifData());
 
                }
                catch (Exiv2::AnyError& e) {
@@ -930,7 +1088,7 @@ static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key)
                                while (pos != exif->iptcData().end())
                                        {
                                        if (pos->key() == key)
-                                               list = exif_add_value_to_glist(list, *pos);
+                                               list = exif_add_value_to_glist(list, *pos, format, NULL);
                                        ++pos;
                                        }
 
@@ -940,34 +1098,44 @@ static GList *exif_get_metadata_simple(ExifData *exif, const gchar *key)
                                Exiv2::XmpKey ekey(key);
                                Exiv2::XmpData::iterator pos = exif->xmpData().findKey(ekey);
                                if (pos != exif->xmpData().end())
-                                       list = exif_add_value_to_glist(list, *pos);
+                                       list = exif_add_value_to_glist(list, *pos, format, NULL);
 #endif
                        }
                }
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
        }
        return list;
 }
 
-GList *exif_get_metadata(ExifData *exif, const gchar *key)
+GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
 {
        GList *list = NULL;
-       
-       list = exif_get_metadata_simple(exif, key);
-       
+
+       if (!key) return NULL;
+
+       if (format == METADATA_FORMATTED)
+               {
+               gchar *text;
+               gint key_valid;
+               text = exif_get_formatted_by_key(exif, key, &key_valid);
+               if (key_valid) return g_list_append(NULL, text);
+               }
+
+       list = exif_get_metadata_simple(exif, key, format);
+
        /* the following code can be ifdefed out as soon as Exiv2 supports it */
        if (!list)
                {
                const AltKey *alt_key = find_alt_key(key);
                if (alt_key && alt_key->iptc_key)
-                       list = exif_get_metadata_simple(exif, alt_key->iptc_key);
+                       list = exif_get_metadata_simple(exif, alt_key->iptc_key, format);
 
-#if !EXIV2_TEST_VERSION(0,17,0)        
+#if !EXIV2_TEST_VERSION(0,17,0)
                /* with older Exiv2 versions exif is not synced */
                if (!list && alt_key && alt_key->exif_key)
-                       list = exif_get_metadata_simple(exif, alt_key->exif_key);
+                       list = exif_get_metadata_simple(exif, alt_key->exif_key, format);
 #endif
                }
        return list;
@@ -990,16 +1158,35 @@ guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
        return ret;
 }
 
+gchar* exif_get_image_comment(FileData* fd)
+{
+       if (!fd || !fd->exif)
+               return g_strdup("");
+
+       return g_strdup(fd->exif->image_comment().c_str());
+}
+
+void exif_set_image_comment(FileData* fd, const gchar* comment)
+{
+       if (!fd || !fd->exif)
+               return;
+
+       fd->exif->set_image_comment(comment ? comment : "");
+}
+
+
 #if EXIV2_TEST_VERSION(0,17,90)
 
 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
 {
        if (!exif) return NULL;
 
-       const char* path = exif->image()->io().path().c_str();
+       if (!exif->image()) return NULL;
+
+       std::string const path = exif->image()->io().path();
        /* given image pathname, first do simple (and fast) file extension test */
-       gboolean is_raw = filter_file_class(path, FORMAT_CLASS_RAWIMAGE);
-       
+       gboolean is_raw = filter_file_class(path.c_str(), FORMAT_CLASS_RAWIMAGE);
+
        if (!is_raw && requested_width == 0) return NULL;
 
        try {
@@ -1012,7 +1199,7 @@ guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width,
                        {
                        Exiv2::PreviewPropertiesList::iterator pos;
                        Exiv2::PreviewPropertiesList::iterator last = --list.end();
-                       
+
                        if (requested_width == 0)
                                {
                                pos = last; // the largest
@@ -1026,11 +1213,11 @@ guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width,
                                            pos->height_ >= (uint32_t)requested_height) break;
                                        ++pos;
                                        }
-                               
+
                                // we are not interested in smaller thumbnails in normal image formats - we can use full image instead
-                               if (!is_raw) 
+                               if (!is_raw)
                                        {
-                                       if (pos->width_ < (uint32_t)requested_width || pos->height_ < (uint32_t)requested_height) return NULL; 
+                                       if (pos->width_ < (uint32_t)requested_width || pos->height_ < (uint32_t)requested_height) return NULL;
                                        }
                                }
 
@@ -1045,7 +1232,7 @@ guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width,
                return NULL;
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
                return NULL;
        }
 }
@@ -1096,10 +1283,12 @@ extern "C" guchar *exif_get_preview(ExifData *exif, guint *data_len, gint reques
        unsigned long offset;
 
        if (!exif) return NULL;
-       const char* path = exif->image()->io().path().c_str();
+       if (!exif->image()) return NULL;
+
+       std::string const path = exif->image()->io().path();
 
        /* given image pathname, first do simple (and fast) file extension test */
-       if (!filter_file_class(path, FORMAT_CLASS_RAWIMAGE)) return NULL;
+       if (!filter_file_class(path.c_str(), FORMAT_CLASS_RAWIMAGE)) return NULL;
 
        try {
                struct stat st;
@@ -1107,12 +1296,12 @@ extern "C" guchar *exif_get_preview(ExifData *exif, guint *data_len, gint reques
                size_t map_len;
                UnmapData *ud;
                int fd;
-               
+
                RawFile rf(exif->image()->io());
                offset = rf.preview_offset();
-               DEBUG_1("%s: offset %lu", path, offset);
-               
-               fd = open(path, O_RDONLY);
+               DEBUG_1("%s: offset %lu", path.c_str(), offset);
+
+               fd = open(path.c_str(), O_RDONLY);
                if (fd == -1)
                        {
                        return NULL;
@@ -1135,13 +1324,13 @@ extern "C" guchar *exif_get_preview(ExifData *exif, guint *data_len, gint reques
                ud->ptr = map_data + offset;
                ud->map_data = map_data;
                ud->map_len = map_len;
-               
+
                exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
                return ud->ptr;
-               
+
        }
        catch (Exiv2::AnyError& e) {
-               std::cout << "Caught Exiv2 exception '" << e << "'\n";
+               debug_exception(e);
        }
        return NULL;
 
@@ -1150,7 +1339,7 @@ extern "C" guchar *exif_get_preview(ExifData *exif, guint *data_len, gint reques
 void exif_free_preview(guchar *buf)
 {
        GList *work = exif_unmap_list;
-       
+
        while (work)
                {
                UnmapData *ud = (UnmapData *)work->data;
@@ -1186,7 +1375,7 @@ RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
         if (io.open() != 0) {
             throw Error(9, io.path(), strError());
         }
-        
+
         map_data = io.mmap();
         map_len = io.size();
 
@@ -1240,7 +1429,7 @@ RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
        if (0 == rootDir.get()) {
                throw Error(1, "No root element defined in TIFF structure");
        }
-       
+
        if (tiffHeader)
                {
                if (!tiffHeader->read(map_data, map_len)) throw Error(3, "TIFF");
@@ -1250,12 +1439,12 @@ RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
                rootDir->setStart(map_data + tiffHeader->ifdOffset());
 #endif
                }
-               
+
        if (cr2Header)
                {
                rootDir->setStart(map_data + cr2Header->offset());
                }
-       
+
        TiffRwState::AutoPtr state(new TiffRwState(tiffHeader ? tiffHeader->byteOrder() : littleEndian, 0, createFct));
 
        TiffReader reader(map_data,
@@ -1264,7 +1453,7 @@ RawFile::RawFile(BasicIo &io) : io_(io), map_data(NULL), map_len(0), offset(0)
                          state);
 
        rootDir->accept(reader);
-       
+
        if (tiffHeader)
                delete tiffHeader;
        if (cr2Header)
@@ -1295,7 +1484,7 @@ unsigned long RawFile::preview_offset(void)
 {
        const Value *val;
        if (offset) return offset;
-       
+
        if (type == Exiv2::ImageType::cr2)
                {
                val = find(0x111, Group::ifd0);
@@ -1303,7 +1492,7 @@ unsigned long RawFile::preview_offset(void)
 
                return 0;
                }
-       
+
        val = find(0x201, Group::sub0_0);
        if (val) return val->toLong();