[gexiv2/wip/Jehan/issue-60-use-gerror: 4/5] New gexiv2_metadata_try_(g|s)et_tag_string() variants with GError.




commit 52c3a2b1442b52d95abd561a5973862c46f740ea
Author: Jehan <jehan girinstud io>
Date:   Sun Nov 8 19:22:54 2020 +0100

    New gexiv2_metadata_try_(g|s)et_tag_string() variants with GError.
    
    Similarly to my previous commit, gexiv2_metadata_set_tag_string() and
    gexiv2_metadata_get_tag_string() should have variants which just set a
    GError appropriately on tag error, rather than raising a WARNING.

 gexiv2/gexiv2-metadata-exif.cpp  | 10 ++++---
 gexiv2/gexiv2-metadata-gps.cpp   |  6 ++--
 gexiv2/gexiv2-metadata-iptc.cpp  | 10 ++++---
 gexiv2/gexiv2-metadata-private.h | 12 ++++----
 gexiv2/gexiv2-metadata-xmp.cpp   | 10 ++++---
 gexiv2/gexiv2-metadata.cpp       | 62 +++++++++++++++++++++++++++++++---------
 gexiv2/gexiv2-metadata.h         | 33 +++++++++++++++++++++
 7 files changed, 109 insertions(+), 34 deletions(-)
---
diff --git a/gexiv2/gexiv2-metadata-exif.cpp b/gexiv2/gexiv2-metadata-exif.cpp
index 15cf116..81e54b3 100644
--- a/gexiv2/gexiv2-metadata-exif.cpp
+++ b/gexiv2/gexiv2-metadata-exif.cpp
@@ -97,10 +97,11 @@ gchar** gexiv2_metadata_get_exif_tags (GExiv2Metadata *self) {
     return data;
 }
 
-gchar* gexiv2_metadata_get_exif_tag_string (GExiv2Metadata *self, const gchar* tag) {
+gchar* gexiv2_metadata_get_exif_tag_string (GExiv2Metadata *self, const gchar* tag, GError **error) {
     g_return_val_if_fail(GEXIV2_IS_METADATA (self), NULL);
     g_return_val_if_fail(tag != NULL, NULL);
     g_return_val_if_fail(self->priv->image.get() != NULL, NULL);
+    g_return_val_if_fail(error == nullptr || *error == nullptr, FALSE);
     
     Exiv2::ExifData &exif_data = self->priv->image->exifData();
     
@@ -112,7 +113,7 @@ gchar* gexiv2_metadata_get_exif_tag_string (GExiv2Metadata *self, const gchar* t
         if (it != exif_data.end())
             return g_strdup (it->toString ().c_str ());
     } catch (Exiv2::Error& e) {
-        LOG_ERROR(e);
+        g_set_error_literal (error, g_quark_from_string ("GExiv2"), e.code (), e.what ());
     }
     
     return NULL;
@@ -143,18 +144,19 @@ gchar* gexiv2_metadata_get_exif_tag_interpreted_string (GExiv2Metadata *self, co
     return NULL;
 }
 
-gboolean gexiv2_metadata_set_exif_tag_string (GExiv2Metadata *self, const gchar* tag, const gchar* value) {
+gboolean gexiv2_metadata_set_exif_tag_string (GExiv2Metadata *self, const gchar* tag, const gchar* value, 
GError **error) {
     g_return_val_if_fail(GEXIV2_IS_METADATA (self), FALSE);
     g_return_val_if_fail(tag != NULL, FALSE);
     g_return_val_if_fail(value != NULL, FALSE);
     g_return_val_if_fail(self->priv->image.get() != NULL, FALSE);
+    g_return_val_if_fail(error == nullptr || *error == nullptr, FALSE);
     
     try {
         self->priv->image->exifData()[tag] = value;
         
         return TRUE;
     } catch (Exiv2::Error& e) {
-        LOG_ERROR(e);
+        g_set_error_literal (error, g_quark_from_string ("GExiv2"), e.code (), e.what ());
     }
     
     return FALSE;
diff --git a/gexiv2/gexiv2-metadata-gps.cpp b/gexiv2/gexiv2-metadata-gps.cpp
index deeb4ac..7d0d18a 100644
--- a/gexiv2/gexiv2-metadata-gps.cpp
+++ b/gexiv2/gexiv2-metadata-gps.cpp
@@ -56,7 +56,7 @@ gboolean gexiv2_metadata_get_gps_longitude (GExiv2Metadata *self, gdouble *longi
         double min, sec;
         *longitude = 0.0;
 
-        gchar* longitude_ref = gexiv2_metadata_get_exif_tag_string (self, "Exif.GPSInfo.GPSLongitudeRef");
+        gchar* longitude_ref = gexiv2_metadata_get_exif_tag_string (self, "Exif.GPSInfo.GPSLongitudeRef", 
nullptr);
         GPointer longitude_ref_guard(longitude_ref);
 
         if (longitude_ref == NULL || longitude_ref[0] == '\0') {
@@ -105,7 +105,7 @@ gboolean gexiv2_metadata_get_gps_latitude (GExiv2Metadata *self, gdouble *latitu
         double min, sec;
         *latitude = 0.0;
 
-        gchar* latitude_ref = gexiv2_metadata_get_exif_tag_string (self, "Exif.GPSInfo.GPSLatitudeRef");
+        gchar* latitude_ref = gexiv2_metadata_get_exif_tag_string (self, "Exif.GPSInfo.GPSLatitudeRef", 
nullptr);
         GPointer latitude_ref_guard(latitude_ref);
 
         if (latitude_ref == NULL || latitude_ref[0] == '\0') {
@@ -153,7 +153,7 @@ gboolean gexiv2_metadata_get_gps_altitude (GExiv2Metadata *self, gdouble *altitu
     try {
         *altitude = 0.0;
 
-        gchar* altitude_ref = gexiv2_metadata_get_exif_tag_string (self, "Exif.GPSInfo.GPSAltitudeRef");
+        gchar* altitude_ref = gexiv2_metadata_get_exif_tag_string (self, "Exif.GPSInfo.GPSAltitudeRef", 
nullptr);
         GPointer altitude_ref_guard(altitude_ref);
 
         if (altitude_ref == NULL || altitude_ref[0] == '\0') {
diff --git a/gexiv2/gexiv2-metadata-iptc.cpp b/gexiv2/gexiv2-metadata-iptc.cpp
index efb5824..8340d72 100644
--- a/gexiv2/gexiv2-metadata-iptc.cpp
+++ b/gexiv2/gexiv2-metadata-iptc.cpp
@@ -97,10 +97,11 @@ gchar** gexiv2_metadata_get_iptc_tags (GExiv2Metadata *self) {
     return data;
 }
 
-gchar* gexiv2_metadata_get_iptc_tag_string (GExiv2Metadata *self, const gchar* tag) {
+gchar* gexiv2_metadata_get_iptc_tag_string (GExiv2Metadata *self, const gchar* tag, GError **error) {
     g_return_val_if_fail(GEXIV2_IS_METADATA (self), NULL);
     g_return_val_if_fail(tag != NULL, NULL);
     g_return_val_if_fail(self->priv->image.get() != NULL, NULL);
+    g_return_val_if_fail(error == nullptr || *error == nullptr, FALSE);
     
     Exiv2::IptcData& iptc_data = self->priv->image->iptcData();
     
@@ -112,7 +113,7 @@ gchar* gexiv2_metadata_get_iptc_tag_string (GExiv2Metadata *self, const gchar* t
         if (it != iptc_data.end())
             return g_strdup (it->toString ().c_str ());
     } catch (Exiv2::Error& e) {
-        LOG_ERROR(e);
+        g_set_error_literal (error, g_quark_from_string ("GExiv2"), e.code (), e.what ());
     }
     
     return NULL;
@@ -144,18 +145,19 @@ gchar* gexiv2_metadata_get_iptc_tag_interpreted_string (GExiv2Metadata *self, co
 }
 
 gboolean gexiv2_metadata_set_iptc_tag_string (GExiv2Metadata *self, const gchar* tag,
-    const gchar* value) {
+    const gchar* value, GError **error) {
     g_return_val_if_fail (GEXIV2_IS_METADATA (self), FALSE);
     g_return_val_if_fail(tag != NULL, FALSE);
     g_return_val_if_fail(value != NULL, FALSE);
     g_return_val_if_fail(self->priv->image.get() != NULL, FALSE);
+    g_return_val_if_fail(error == nullptr || *error == nullptr, FALSE);
     
     try {
         self->priv->image->iptcData()[tag] = value;
         
         return TRUE;
     } catch (Exiv2::Error& e) {
-        LOG_ERROR(e);
+        g_set_error_literal (error, g_quark_from_string ("GExiv2"), e.code (), e.what ());
     }
     
     return FALSE;
diff --git a/gexiv2/gexiv2-metadata-private.h b/gexiv2/gexiv2-metadata-private.h
index 989af0b..30e3c13 100644
--- a/gexiv2/gexiv2-metadata-private.h
+++ b/gexiv2/gexiv2-metadata-private.h
@@ -42,8 +42,8 @@ struct _GExiv2MetadataPrivate
 
 G_GNUC_INTERNAL gboolean               gexiv2_metadata_has_exif_tag            (GExiv2Metadata *self, const 
gchar* tag);
 G_GNUC_INTERNAL gboolean               gexiv2_metadata_clear_exif_tag          (GExiv2Metadata *self, const 
gchar* tag);
-G_GNUC_INTERNAL gchar*                 gexiv2_metadata_get_exif_tag_string     (GExiv2Metadata *self, const 
gchar* tag);
-G_GNUC_INTERNAL gboolean               gexiv2_metadata_set_exif_tag_string     (GExiv2Metadata *self, const 
gchar* tag, const gchar* value);
+G_GNUC_INTERNAL gchar*                 gexiv2_metadata_get_exif_tag_string     (GExiv2Metadata *self, const 
gchar* tag, GError **error);
+G_GNUC_INTERNAL gboolean               gexiv2_metadata_set_exif_tag_string     (GExiv2Metadata *self, const 
gchar* tag, const gchar* value, GError **error);
 G_GNUC_INTERNAL gchar*                 gexiv2_metadata_get_exif_tag_interpreted_string (GExiv2Metadata 
*self, const gchar* tag);
 G_GNUC_INTERNAL glong                  gexiv2_metadata_get_exif_tag_long       (GExiv2Metadata *self, const 
gchar* tag);
 G_GNUC_INTERNAL gboolean               gexiv2_metadata_set_exif_tag_long       (GExiv2Metadata *self, const 
gchar* tag, glong value);
@@ -57,8 +57,8 @@ G_GNUC_INTERNAL GBytes*                       gexiv2_metadata_get_exif_tag_raw        
(GExiv2Metadata *self
 
 G_GNUC_INTERNAL gboolean               gexiv2_metadata_clear_xmp_tag           (GExiv2Metadata *self, const 
gchar* tag);
 G_GNUC_INTERNAL gboolean               gexiv2_metadata_has_xmp_tag                     (GExiv2Metadata 
*self, const gchar* tag);
-G_GNUC_INTERNAL gchar*                 gexiv2_metadata_get_xmp_tag_string      (GExiv2Metadata *self, const 
gchar* tag);
-G_GNUC_INTERNAL gboolean               gexiv2_metadata_set_xmp_tag_string      (GExiv2Metadata *self, const 
gchar* tag, const gchar* value);
+G_GNUC_INTERNAL gchar*                 gexiv2_metadata_get_xmp_tag_string      (GExiv2Metadata *self, const 
gchar* tag, GError **error);
+G_GNUC_INTERNAL gboolean               gexiv2_metadata_set_xmp_tag_string      (GExiv2Metadata *self, const 
gchar* tag, const gchar* value, GError **error);
 G_GNUC_INTERNAL gchar*                 gexiv2_metadata_get_xmp_tag_interpreted_string (GExiv2Metadata *self, 
const gchar* tag);
 G_GNUC_INTERNAL glong                  gexiv2_metadata_get_xmp_tag_long        (GExiv2Metadata *self, const 
gchar* tag);
 G_GNUC_INTERNAL gboolean               gexiv2_metadata_set_xmp_tag_long        (GExiv2Metadata *self, const 
gchar* tag, glong value);
@@ -75,8 +75,8 @@ G_GNUC_INTERNAL GBytes*                       gexiv2_metadata_get_xmp_tag_raw         
(GExiv2Metadata *self
 
 G_GNUC_INTERNAL gboolean               gexiv2_metadata_clear_iptc_tag          (GExiv2Metadata *self, const 
gchar* tag);
 G_GNUC_INTERNAL gboolean               gexiv2_metadata_has_iptc_tag            (GExiv2Metadata *self, const 
gchar* tag);
-G_GNUC_INTERNAL gchar*                 gexiv2_metadata_get_iptc_tag_string     (GExiv2Metadata *self, const 
gchar* tag);
-G_GNUC_INTERNAL gboolean               gexiv2_metadata_set_iptc_tag_string     (GExiv2Metadata *self, const 
gchar* tag, const gchar* value);
+G_GNUC_INTERNAL gchar*                 gexiv2_metadata_get_iptc_tag_string     (GExiv2Metadata *self, const 
gchar* tag, GError **error);
+G_GNUC_INTERNAL gboolean               gexiv2_metadata_set_iptc_tag_string     (GExiv2Metadata *self, const 
gchar* tag, const gchar* value, GError **error);
 G_GNUC_INTERNAL gchar*                 gexiv2_metadata_get_iptc_tag_interpreted_string (GExiv2Metadata 
*self, const gchar* tag);
 G_GNUC_INTERNAL gchar**                        gexiv2_metadata_get_iptc_tag_multiple   (GExiv2Metadata 
*self, const gchar* tag, GError **error);
 G_GNUC_INTERNAL gboolean               gexiv2_metadata_set_iptc_tag_multiple   (GExiv2Metadata *self, const 
gchar* tag, const gchar** values, GError **error);
diff --git a/gexiv2/gexiv2-metadata-xmp.cpp b/gexiv2/gexiv2-metadata-xmp.cpp
index 4647d36..87795a4 100644
--- a/gexiv2/gexiv2-metadata-xmp.cpp
+++ b/gexiv2/gexiv2-metadata-xmp.cpp
@@ -126,10 +126,11 @@ gchar** gexiv2_metadata_get_xmp_tags (GExiv2Metadata *self) {
     return data;
 }
 
-gchar* gexiv2_metadata_get_xmp_tag_string (GExiv2Metadata *self, const gchar* tag) {
+gchar* gexiv2_metadata_get_xmp_tag_string (GExiv2Metadata *self, const gchar* tag, GError **error) {
     g_return_val_if_fail(GEXIV2_IS_METADATA (self), NULL);
     g_return_val_if_fail(tag != NULL, NULL);
     g_return_val_if_fail(self->priv->image.get() != NULL, NULL);
+    g_return_val_if_fail(error == nullptr || *error == nullptr, FALSE);
     
     Exiv2::XmpData& xmp_data = self->priv->image->xmpData();
     
@@ -141,7 +142,7 @@ gchar* gexiv2_metadata_get_xmp_tag_string (GExiv2Metadata *self, const gchar* ta
         if (it != xmp_data.end())
             return g_strdup (it->toString ().c_str ());
     } catch (Exiv2::Error& e) {
-        LOG_ERROR(e);
+        g_set_error_literal (error, g_quark_from_string ("GExiv2"), e.code (), e.what ());
     }
     
     return NULL;
@@ -215,18 +216,19 @@ gboolean gexiv2_metadata_set_xmp_tag_struct (GExiv2Metadata *self, const gchar*
 }
 
 gboolean gexiv2_metadata_set_xmp_tag_string (GExiv2Metadata *self, const gchar* tag, 
-    const gchar* value) {
+    const gchar* value, GError **error) {
     g_return_val_if_fail(GEXIV2_IS_METADATA (self), FALSE);
     g_return_val_if_fail(tag != NULL, FALSE);
     g_return_val_if_fail(value != NULL, FALSE);
     g_return_val_if_fail(self->priv->image.get() != NULL, FALSE);
+    g_return_val_if_fail(error == nullptr || *error == nullptr, FALSE);
     
     try {
         self->priv->image->xmpData()[tag] = value;
         
         return TRUE;
     } catch (Exiv2::Error& e) {
-        LOG_ERROR(e);
+        g_set_error_literal (error, g_quark_from_string ("GExiv2"), e.code (), e.what ());
     }
     
     return FALSE;
diff --git a/gexiv2/gexiv2-metadata.cpp b/gexiv2/gexiv2-metadata.cpp
index 3b04017..426a55d 100644
--- a/gexiv2/gexiv2-metadata.cpp
+++ b/gexiv2/gexiv2-metadata.cpp
@@ -894,40 +894,76 @@ gboolean gexiv2_metadata_is_iptc_tag(const gchar* tag) {
     return g_ascii_strncasecmp("Iptc.", tag, 5) == 0;
 }
 
-gchar* gexiv2_metadata_get_tag_string (GExiv2Metadata *self, const gchar* tag) {
-    g_return_val_if_fail(GEXIV2_IS_METADATA (self), NULL);
-    g_return_val_if_fail(tag != NULL, NULL);
-    g_return_val_if_fail(self->priv->image.get() != NULL, NULL);
+gchar* gexiv2_metadata_try_get_tag_string (GExiv2Metadata *self, const gchar* tag, GError **error) {
+    g_return_val_if_fail(GEXIV2_IS_METADATA (self), nullptr);
+    g_return_val_if_fail(tag != nullptr, nullptr);
+    g_return_val_if_fail(self->priv->image.get() != nullptr, nullptr);
     
     if (gexiv2_metadata_is_xmp_tag(tag))
-        return gexiv2_metadata_get_xmp_tag_string (self, tag);
+        return gexiv2_metadata_get_xmp_tag_string (self, tag, error);
         
     if (gexiv2_metadata_is_exif_tag(tag))
-        return gexiv2_metadata_get_exif_tag_string (self, tag);
+        return gexiv2_metadata_get_exif_tag_string (self, tag, error);
         
     if (gexiv2_metadata_is_iptc_tag(tag))
-        return gexiv2_metadata_get_iptc_tag_string (self, tag);
+        return gexiv2_metadata_get_iptc_tag_string (self, tag, error);
     
     return NULL;
 }
 
-gboolean gexiv2_metadata_set_tag_string (GExiv2Metadata *self, const gchar* tag, const gchar* value) {
+gboolean gexiv2_metadata_try_set_tag_string (GExiv2Metadata *self, const gchar* tag, const gchar* value, 
GError **error) {
     g_return_val_if_fail(GEXIV2_IS_METADATA(self), FALSE);
-    g_return_val_if_fail(tag != NULL, FALSE);
-    g_return_val_if_fail(self->priv->image.get() != NULL, FALSE);
+    g_return_val_if_fail(tag != nullptr, FALSE);
+    g_return_val_if_fail(self->priv->image.get() != nullptr, FALSE);
     
     if (gexiv2_metadata_is_xmp_tag(tag))
-        return gexiv2_metadata_set_xmp_tag_string(self, tag, value);
+        return gexiv2_metadata_set_xmp_tag_string(self, tag, value, error);
     
     if (gexiv2_metadata_is_exif_tag(tag))
-        return gexiv2_metadata_set_exif_tag_string(self, tag, value);
+        return gexiv2_metadata_set_exif_tag_string(self, tag, value, error);
     
     if (gexiv2_metadata_is_iptc_tag(tag))
-        return gexiv2_metadata_set_iptc_tag_string(self, tag, value);
+        return gexiv2_metadata_set_iptc_tag_string(self, tag, value, error);
     
     return FALSE;
 }
 
+gchar* gexiv2_metadata_get_tag_string (GExiv2Metadata *self, const gchar* tag) {
+    gchar  *value;
+    GError *error = nullptr;
+
+    g_return_val_if_fail(GEXIV2_IS_METADATA (self), nullptr);
+    g_return_val_if_fail(tag != nullptr, nullptr);
+    g_return_val_if_fail(self->priv->image.get() != nullptr, nullptr);
+
+    value = gexiv2_metadata_try_get_tag_string (self, tag, &error);
+
+    if (error) {
+        g_warning("%s", error->message);
+        g_clear_error(&error);
+    }
+
+    return value;
+}
+
+gboolean gexiv2_metadata_set_tag_string (GExiv2Metadata *self, const gchar* tag, const gchar* value) {
+    gboolean  success;
+    GError   *error = nullptr;
+
+    g_return_val_if_fail(GEXIV2_IS_METADATA(self), FALSE);
+    g_return_val_if_fail(tag != nullptr, FALSE);
+    g_return_val_if_fail(self->priv->image.get() != nullptr, FALSE);
+
+    success = gexiv2_metadata_try_set_tag_string(self, tag, value, &error);
+
+    if (error) {
+        g_warning("%s", error->message);
+        g_clear_error(&error);
+    }
+
+    return success;
+}
+
 gchar* gexiv2_metadata_get_tag_interpreted_string (GExiv2Metadata *self, const gchar* tag) {
     g_return_val_if_fail(GEXIV2_IS_METADATA (self), NULL);
     g_return_val_if_fail(tag != NULL, NULL);
diff --git a/gexiv2/gexiv2-metadata.h b/gexiv2/gexiv2-metadata.h
index 89e1495..3315d92 100644
--- a/gexiv2/gexiv2-metadata.h
+++ b/gexiv2/gexiv2-metadata.h
@@ -489,6 +489,31 @@ gint                       gexiv2_metadata_get_pixel_width         (GExiv2Metadata 
*self);
  */
 gint                   gexiv2_metadata_get_pixel_height        (GExiv2Metadata *self);
 
+/**
+ * gexiv2_metadata_try_get_tag_string:
+ * @self: An instance of #GExiv2Metadata
+ * @tag: Exiv2 tag name
+ * @error: (allow-none): A return location for a #GError or %nullptr
+ *
+ * The Exiv2 Tag Reference can be found at <ulink url="http://exiv2.org/metadata.html";></ulink>
+ *
+ * Returns: (transfer full) (allow-none): The tag's value as a string
+ */
+gchar*                 gexiv2_metadata_try_get_tag_string      (GExiv2Metadata *self, const gchar* tag, 
GError **error);
+
+/**
+ * gexiv2_metadata_try_set_tag_string:
+ * @self: An instance of #GExiv2Metadata
+ * @tag: Exiv2 tag name
+ * @value: The value to set or replace the existing value
+ * @error: (allow-none): A return location for a #GError or %nullptr
+ *
+ * The Exiv2 Tag Reference can be found at <ulink url="http://exiv2.org/metadata.html";></ulink>
+ *
+ * Returns: TRUE on success
+ */
+gboolean               gexiv2_metadata_try_set_tag_string      (GExiv2Metadata *self, const gchar* tag, 
const gchar* value, GError **error);
+
 /**
  * gexiv2_metadata_get_tag_string:
  * @self: An instance of #GExiv2Metadata
@@ -496,6 +521,10 @@ gint                       gexiv2_metadata_get_pixel_height        (GExiv2Metadata 
*self);
  *
  * The Exiv2 Tag Reference can be found at <ulink url="http://exiv2.org/metadata.html";></ulink>
  *
+ * In case of error, a GLib warning will be logged. Use instead
+ * gexiv2_metadata_try_get_tag_string() if you want to avoid this and
+ * control if and how the error is outputted.
+ *
  * Returns: (transfer full) (allow-none): The tag's value as a string
  */
 gchar*                 gexiv2_metadata_get_tag_string          (GExiv2Metadata *self, const gchar* tag);
@@ -508,6 +537,10 @@ gchar*                     gexiv2_metadata_get_tag_string          (GExiv2Metadata 
*self, const gchar* tag
  *
  * The Exiv2 Tag Reference can be found at <ulink url="http://exiv2.org/metadata.html";></ulink>
  *
+ * In case of error, a GLib warning will be logged. Use instead
+ * gexiv2_metadata_try_set_tag_string() if you want to avoid this and
+ * control if and how the error is outputted.
+ *
  * Returns: TRUE on success
  */
 gboolean               gexiv2_metadata_set_tag_string          (GExiv2Metadata *self, const gchar* tag, 
const gchar* value);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]