[niepce] toolkit: fix a crash in the metadata widget try to unwrap an empty Option<>



commit 6f14f996d0d9d72375d4b408297d6e6e2411b1e7
Author: Hubert Figuière <hub figuiere net>
Date:   Thu Feb 1 20:26:36 2018 -0500

    toolkit: fix a crash in the metadata widget try to unwrap an empty Option<>

 src/fwk/base/propertybag.cpp       |  9 ++++--
 src/fwk/toolkit/metadatawidget.cpp | 58 ++++++++++++++++++++------------------
 src/fwk/toolkit/metadatawidget.hpp | 18 ++++++------
 3 files changed, 46 insertions(+), 39 deletions(-)
---
diff --git a/src/fwk/base/propertybag.cpp b/src/fwk/base/propertybag.cpp
index 2e2e034..d24df2e 100644
--- a/src/fwk/base/propertybag.cpp
+++ b/src/fwk/base/propertybag.cpp
@@ -18,7 +18,7 @@
  */
 
 
-
+#include "debug.hpp"
 #include "propertybag.hpp"
 
 namespace fwk {
@@ -101,15 +101,18 @@ PropertyValuePtr property_bag_value(const PropertyBagPtr& bag, PropertyIndex idx
 bool set_value_for_property(PropertyBag& bag, PropertyIndex idx,
                             const PropertyValue & value)
 {
+    DBG_ASSERT(&bag, "bag is NULL");
+    DBG_ASSERT(&value, "value is NULL");
     return ffi::fwk_property_bag_set_value(&bag, idx, &value);
 }
 
-/* return an option */
-// XXX fix me
 fwk::Option<PropertyValuePtr> get_value_for_property(const PropertyBag& bag,
                                                      PropertyIndex idx)
 {
     auto value = ffi::fwk_property_bag_value(&bag, idx);
+    if (!value) {
+        return fwk::Option<PropertyValuePtr>();
+    }
     return fwk::Option<PropertyValuePtr>(property_value_wrap(value));
 }
 
diff --git a/src/fwk/toolkit/metadatawidget.cpp b/src/fwk/toolkit/metadatawidget.cpp
index 5fce86f..4e9b9c4 100644
--- a/src/fwk/toolkit/metadatawidget.cpp
+++ b/src/fwk/toolkit/metadatawidget.cpp
@@ -244,8 +244,8 @@ void MetaDataWidget::set_data_source(const fwk::PropertyBagPtr& properties)
     const MetaDataFormat * current = m_fmt->formats;
     while(current && current->label) {
         auto result = get_value_for_property(*properties, current->id);
-        if(!result.empty() || !current->readonly) {
-            add_data(current, *result.unwrap());
+        if (!result.empty() || !current->readonly) {
+            add_data(current, std::move(result));
         }
         else {
             DBG_OUT("get_property failed id = %d, label = %s",
@@ -256,14 +256,14 @@ void MetaDataWidget::set_data_source(const fwk::PropertyBagPtr& properties)
 }
 
 bool MetaDataWidget::set_fraction_dec_data(Gtk::Widget* w,
-                                           const PropertyValue & value)
+                                           const PropertyValuePtr& value)
 {
-    if (!fwk_property_value_is_string(&value)) {
+    if (!fwk_property_value_is_string(value.get())) {
         ERR_OUT("Data not string(fraction)");
         return false;
     }
     try {
-        const std::string str_value = fwk::property_value_get_string(value);
+        const std::string str_value = fwk::property_value_get_string(*value);
         DBG_OUT("set fraction dec %s", str_value.c_str());
         std::string frac = str(boost::format("%.1f")
                                % ffi::fwk_fraction_to_decimal(str_value.c_str()));
@@ -277,14 +277,14 @@ bool MetaDataWidget::set_fraction_dec_data(Gtk::Widget* w,
 }
 
 bool MetaDataWidget::set_fraction_data(Gtk::Widget* w,
-                                       const PropertyValue & value)
+                                       const PropertyValuePtr& value)
 {
-    if (!fwk_property_value_is_string(&value)) {
+    if (!fwk_property_value_is_string(value.get())) {
         ERR_OUT("Data not string(fraction)");
         return false;
     }
     try {
-        const std::string str_value = fwk::property_value_get_string(value);
+        const std::string str_value = fwk::property_value_get_string(*value);
         DBG_OUT("set fraction %s", str_value.c_str());
         boost::rational<int> r
             = boost::lexical_cast<boost::rational<int>>(str_value);
@@ -301,14 +301,14 @@ bool MetaDataWidget::set_fraction_data(Gtk::Widget* w,
 }
 
 bool MetaDataWidget::set_star_rating_data(Gtk::Widget* w,
-                                          const PropertyValue & value)
+                                          const PropertyValuePtr& value)
 {
-    if (!fwk_property_value_is_integer(&value)) {
+    if (!fwk_property_value_is_integer(value.get())) {
         ERR_OUT("Data not integer");
         return false;
     }
     try {
-        int rating = fwk_property_value_get_integer(&value);
+        int rating = fwk_property_value_get_integer(value.get());
         AutoFlag flag(m_update);
         static_cast<fwk::RatingLabel*>(w)->set_rating(rating);
     }
@@ -318,11 +318,11 @@ bool MetaDataWidget::set_star_rating_data(Gtk::Widget* w,
     return true;
 }
 
-bool MetaDataWidget::set_string_array_data(Gtk::Widget* w, const PropertyValue & value)
+bool MetaDataWidget::set_string_array_data(Gtk::Widget* w, const PropertyValuePtr& value)
 {
     try {
         AutoFlag flag(m_update);
-        std::vector<std::string> tokens = fwk::property_value_get_string_array(value);
+        std::vector<std::string> tokens = fwk::property_value_get_string_array(*value);
 
         static_cast<fwk::TokenTextView*>(w)->set_tokens(tokens);
     }
@@ -333,9 +333,9 @@ bool MetaDataWidget::set_string_array_data(Gtk::Widget* w, const PropertyValue &
 }
 
 bool MetaDataWidget::set_text_data(Gtk::Widget* w, bool readonly,
-                                   const PropertyValue & value)
+                                   const PropertyValuePtr& value)
 {
-    if (!fwk_property_value_is_string(&value)) {
+    if (!fwk_property_value_is_string(value.get())) {
         ERR_OUT("Data not string.");
         return false;
     }
@@ -343,10 +343,10 @@ bool MetaDataWidget::set_text_data(Gtk::Widget* w, bool readonly,
         AutoFlag flag(m_update);
         if(readonly) {
             static_cast<Gtk::Label*>(w)->set_text(
-                fwk::property_value_get_string(value));
+                fwk::property_value_get_string(*value));
         } else {
             static_cast<Gtk::TextView*>(w)->get_buffer()->set_text(
-                fwk::property_value_get_string(value));
+                fwk::property_value_get_string(*value));
         }
     }
     catch(...) {
@@ -356,9 +356,9 @@ bool MetaDataWidget::set_text_data(Gtk::Widget* w, bool readonly,
 }
 
 bool MetaDataWidget::set_string_data(Gtk::Widget* w, bool readonly,
-                                     const PropertyValue & value)
+                                     const PropertyValuePtr& value)
 {
-    if (!fwk_property_value_is_string(&value)) {
+    if (!fwk_property_value_is_string(value.get())) {
         ERR_OUT("Data not string.");
         return false;
     }
@@ -366,10 +366,10 @@ bool MetaDataWidget::set_string_data(Gtk::Widget* w, bool readonly,
         AutoFlag flag(m_update);
         if(readonly) {
             static_cast<Gtk::Label*>(w)->set_text(
-                fwk::property_value_get_string(value));
+                fwk::property_value_get_string(*value));
         } else {
             static_cast<Gtk::Entry*>(w)->set_text(
-                fwk::property_value_get_string(value));
+                fwk::property_value_get_string(*value));
         }
     }
     catch(...) {
@@ -378,14 +378,14 @@ bool MetaDataWidget::set_string_data(Gtk::Widget* w, bool readonly,
     return true;
 }
 
-bool MetaDataWidget::set_date_data(Gtk::Widget* w, const PropertyValue & value)
+bool MetaDataWidget::set_date_data(Gtk::Widget* w, const PropertyValuePtr& value)
 {
-    if (!fwk_property_value_is_date(&value)) {
+    if (!fwk_property_value_is_date(value.get())) {
         return false;
     }
     try {
         AutoFlag flag(m_update);
-        const fwk::Date* date = fwk_property_value_get_date(&value);
+        const fwk::Date* date = fwk_property_value_get_date(value.get());
         if (date) {
             static_cast<Gtk::Label*>(w)->set_text(fwk::date_to_string(date));
 
@@ -401,9 +401,13 @@ bool MetaDataWidget::set_date_data(Gtk::Widget* w, const PropertyValue & value)
 }
 
 void MetaDataWidget::add_data(const MetaDataFormat * current,
-                              const PropertyValue & value)
+                              fwk::Option<PropertyValuePtr>&& optional_value)
 {
-    if (fwk_property_value_is_empty(&value)) {
+    if (optional_value.empty()) {
+        return;
+    }
+    auto value = optional_value.unwrap();
+    if (fwk_property_value_is_empty(value.get())) {
         return;
     }
 
@@ -495,7 +499,7 @@ void MetaDataWidget::emit_metadata_changed(fwk::PropertyIndex prop,
     fwk::set_value_for_property(*props, prop, *value);
     auto result = fwk::get_value_for_property(*m_current_data, prop);
     if (!result.empty()) {
-        set_value_for_property(*old_props, prop, *result.unwrap());
+        fwk::set_value_for_property(*old_props, prop, *result.unwrap());
     }
     signal_metadata_changed.emit(props, old_props);
 }
diff --git a/src/fwk/toolkit/metadatawidget.hpp b/src/fwk/toolkit/metadatawidget.hpp
index 78d3483..8a45cd1 100644
--- a/src/fwk/toolkit/metadatawidget.hpp
+++ b/src/fwk/toolkit/metadatawidget.hpp
@@ -69,7 +69,7 @@ public:
     MetaDataWidget(const Glib::ustring & title);
 
     void add_data(const MetaDataFormat * current,
-                  const PropertyValue & value);
+                  fwk::Option<PropertyValuePtr>&& value);
     void set_data_format(const MetaDataSectionFormat * fmt);
     void set_data_source(const fwk::PropertyBagPtr& properties);
 
@@ -93,16 +93,16 @@ private:
 
     // set data
     // Fraction as a decimal
-    bool set_fraction_dec_data(Gtk::Widget* w, const PropertyValue & value);
+    bool set_fraction_dec_data(Gtk::Widget* w, const PropertyValuePtr& value);
     // Fraction as fraction
-    bool set_fraction_data(Gtk::Widget* w, const PropertyValue & value);
-    bool set_star_rating_data(Gtk::Widget* w, const PropertyValue & value);
-    bool set_string_array_data(Gtk::Widget* w, const PropertyValue & value);
-    bool set_text_data(Gtk::Widget* w, bool readonly, 
-                       const PropertyValue & value);
+    bool set_fraction_data(Gtk::Widget* w, const PropertyValuePtr& value);
+    bool set_star_rating_data(Gtk::Widget* w, const PropertyValuePtr& value);
+    bool set_string_array_data(Gtk::Widget* w, const PropertyValuePtr& value);
+    bool set_text_data(Gtk::Widget* w, bool readonly,
+                       const PropertyValuePtr& value);
     bool set_string_data(Gtk::Widget* w, bool readonly,
-                         const PropertyValue & value);
-    bool set_date_data(Gtk::Widget* w, const PropertyValue & value);
+                         const PropertyValuePtr& value);
+    bool set_date_data(Gtk::Widget* w, const PropertyValuePtr& value);
 
     void emit_metadata_changed(fwk::PropertyIndex prop, const fwk::PropertyValuePtr & value);
 


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