diff --git a/src/ct/ct_storage_multifile.cc b/src/ct/ct_storage_multifile.cc index 4a26a0a6d..32b3f6abd 100644 --- a/src/ct/ct_storage_multifile.cc +++ b/src/ct/ct_storage_multifile.cc @@ -413,14 +413,19 @@ bool CtStorageMultiFile::_nodes_to_multifile(const CtTreeIter* ct_tree_iter, const std::string& sha256sum, std::string& rawBlob) { - Glib::Dir gdir{dir_path}; - std::list dir_entries{gdir.begin(), gdir.end()}; - for (const std::string& filename : dir_entries) { - if (str::startswith(filename, sha256sum)) { - rawBlob = Glib::file_get_contents(Glib::build_filename(dir_path, filename)); - return true; + try { + Glib::Dir gdir{dir_path}; + std::list dir_entries{gdir.begin(), gdir.end()}; + for (const std::string& filename : dir_entries) { + if (str::startswith(filename, sha256sum)) { + rawBlob = Glib::file_get_contents(Glib::build_filename(dir_path, filename)); + return true; + } } } + catch (Glib::Error& error) { + spdlog::error("{} {}", __FUNCTION__, error.what()); + } return false; } @@ -527,9 +532,12 @@ Glib::RefPtr CtStorageMultiFile::get_delayed_text_buffer(const gint spdlog::error("!! {} node_id {}", __FUNCTION__, node_id); return Glib::RefPtr{}; } - auto node_buffer = _delayed_text_buffers[node_id]; - _delayed_text_buffers.erase(node_id); + std::shared_ptr node_buffer = _delayed_text_buffers[node_id]; auto xml_element = dynamic_cast(node_buffer->get_root_node()->get_first_child()); const fs::path multifile_dir = _get_node_dirpath(_pCtMainWin->get_tree_store().get_node_from_node_id(node_id)); - return CtStorageXmlHelper{_pCtMainWin}.create_buffer_and_widgets_from_xml(xml_element, syntax, widgets, nullptr, -1, multifile_dir.string()); + auto ret_buffer = CtStorageXmlHelper{_pCtMainWin}.create_buffer_and_widgets_from_xml(xml_element, syntax, widgets, nullptr, -1, multifile_dir.string()); + if (ret_buffer) { + _delayed_text_buffers.erase(node_id); + } + return ret_buffer; } diff --git a/src/ct/ct_storage_xml.cc b/src/ct/ct_storage_xml.cc index 607b47405..f8a71ad85 100644 --- a/src/ct/ct_storage_xml.cc +++ b/src/ct/ct_storage_xml.cc @@ -165,10 +165,13 @@ Glib::RefPtr CtStorageXml::get_delayed_text_buffer(const gint64& no spdlog::error("!! {} node_id {}", __FUNCTION__, node_id); return Glib::RefPtr{}; } - auto node_buffer = _delayed_text_buffers[node_id]; - _delayed_text_buffers.erase(node_id); + std::shared_ptr node_buffer = _delayed_text_buffers[node_id]; auto xml_element = dynamic_cast(node_buffer->get_root_node()->get_first_child()); - return CtStorageXmlHelper{_pCtMainWin}.create_buffer_and_widgets_from_xml(xml_element, syntax, widgets, nullptr, -1, ""); + auto ret_buffer = CtStorageXmlHelper{_pCtMainWin}.create_buffer_and_widgets_from_xml(xml_element, syntax, widgets, nullptr, -1, ""); + if (ret_buffer) { + _delayed_text_buffers.erase(node_id); + } + return ret_buffer; } void CtStorageXml::_nodes_to_xml(CtTreeIter* ct_tree_iter, @@ -331,16 +334,20 @@ Glib::RefPtr CtStorageXmlHelper::create_buffer_and_widgets_from_xml const std::string& multifile_dir) { Glib::RefPtr pBuffer = _pCtMainWin->get_new_text_buffer(); + bool error{false}; pBuffer->begin_not_undoable_action(); for (xmlpp::Node* xml_slot : parent_xml_element->get_children()) { - get_text_buffer_one_slot_from_xml(pBuffer, xml_slot, widgets, text_insert_pos, force_offset, multifile_dir); + if (not get_text_buffer_one_slot_from_xml(pBuffer, xml_slot, widgets, text_insert_pos, force_offset, multifile_dir)) { + error = true; + break; + } } pBuffer->end_not_undoable_action(); pBuffer->set_modified(false); - return pBuffer; + return error ? Glib::RefPtr{} : pBuffer; } -void CtStorageXmlHelper::get_text_buffer_one_slot_from_xml(Glib::RefPtr buffer, +bool CtStorageXmlHelper::get_text_buffer_one_slot_from_xml(Glib::RefPtr buffer, xmlpp::Node* slot_node, std::list& widgets, Gtk::TextIter* text_insert_pos, @@ -368,6 +375,9 @@ void CtStorageXmlHelper::get_text_buffer_one_slot_from_xml(Glib::RefPtr CtStorageXmlHelper::create_buffer_no_widgets(const Glib::ustring& syntax, const char* xml_content) @@ -486,7 +497,10 @@ void CtStorageXmlHelper::_add_rich_text_from_xml(Glib::RefPtr buffe buffer->insert(iter, text_content); } -CtAnchoredWidget* CtStorageXmlHelper::_create_image_from_xml(xmlpp::Element* xml_element, int charOffset, const Glib::ustring& justification, const std::string& multifile_dir) +CtAnchoredWidget* CtStorageXmlHelper::_create_image_from_xml(xmlpp::Element* xml_element, + int charOffset, + const Glib::ustring& justification, + const std::string& multifile_dir) { const Glib::ustring anchorName = xml_element->get_attribute_value("anchor"); if (not anchorName.empty()) { @@ -506,6 +520,7 @@ CtAnchoredWidget* CtStorageXmlHelper::_create_image_from_xml(xmlpp::Element* xml const std::string sha256sum = xml_element->get_attribute_value("sha256sum"); if (not CtStorageMultiFile::read_blob(multifile_dir, sha256sum, rawBlob)) { spdlog::warn("!! unexp not found {} in {}", sha256sum, multifile_dir); + return nullptr; } } if (not file_name.empty()) { diff --git a/src/ct/ct_storage_xml.h b/src/ct/ct_storage_xml.h index f9905af4f..9361138df 100644 --- a/src/ct/ct_storage_xml.h +++ b/src/ct/ct_storage_xml.h @@ -111,7 +111,7 @@ class CtStorageXmlHelper const int force_offset, const std::string& multifile_dir); - void get_text_buffer_one_slot_from_xml(Glib::RefPtr buffer, + bool get_text_buffer_one_slot_from_xml(Glib::RefPtr buffer, xmlpp::Node* slot_node, std::list& widgets, Gtk::TextIter* text_insert_pos,