From 841295eb955285d1bb64430ec5e8708da3372617 Mon Sep 17 00:00:00 2001 From: Martchus Date: Sun, 10 Jun 2018 23:01:17 +0200 Subject: [PATCH] Improve SerializedRole in EntryModel::setData() * Prevent copy * Handle and log errors * Improve coding style --- model/entrymodel.cpp | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/model/entrymodel.cpp b/model/entrymodel.cpp index 7706ad6..2208d2d 100644 --- a/model/entrymodel.cpp +++ b/model/entrymodel.cpp @@ -5,6 +5,7 @@ #endif #include +#include #include @@ -13,6 +14,7 @@ #include #include +#include #include #include @@ -289,21 +291,22 @@ bool EntryModel::setData(const QModelIndex &index, const QVariant &value, int ro } break; case SerializedRole: { - NodeEntry *parent = entry->parent(); - QModelIndex parentIndex = index.parent(); + NodeEntry *const parent = entry->parent(); + const QModelIndex parentIndex = index.parent(); if (!parent || !parentIndex.isValid()) { - break; + return false; } - stringstream ss(stringstream::in | stringstream::out | stringstream::binary); - ss.exceptions(std::stringstream::failbit | std::stringstream::badbit); - QByteArray array = value.toByteArray(); + QByteArray array(value.toByteArray()); if (array.isEmpty()) { - break; + return false; } try { - ss.write(array.data(), array.size()); - Entry *newEntry = Entry::parse(ss); - int row = entry->index(); + stringstream ss(stringstream::in | stringstream::out | stringstream::binary); + ss.exceptions(std::stringstream::failbit | std::stringstream::badbit); + ss.rdbuf()->pubsetbuf(array.data(), array.size()); + + Entry *const newEntry = Entry::parse(ss); + const int row = entry->index(); beginRemoveRows(parentIndex, row, row); delete entry; endRemoveRows(); @@ -311,10 +314,15 @@ bool EntryModel::setData(const QModelIndex &index, const QVariant &value, int ro newEntry->setParent(parent, row); endInsertRows(); return true; + } catch (const Io::ParsingException &parsingError) { + cerr << "EntryModel::setData: parsing exception: " << parsingError.what() << endl; + return false; } catch (...) { - IoUtilities::catchIoFailure(); + const char *const errorMessage(IoUtilities::catchIoFailure()); + cerr << "EntryModel::setData: IO exception: " << errorMessage << endl; + return false; } - } break; + } case DefaultExpandedRole: switch (entry->type()) { case EntryType::Account: