Improve SerializedRole in EntryModel::setData()
* Prevent copy * Handle and log errors * Improve coding style
This commit is contained in:
parent
2496c495c7
commit
841295eb95
|
@ -5,6 +5,7 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <passwordfile/io/entry.h>
|
#include <passwordfile/io/entry.h>
|
||||||
|
#include <passwordfile/io/parsingexception.h>
|
||||||
|
|
||||||
#include <c++utilities/io/catchiofailure.h>
|
#include <c++utilities/io/catchiofailure.h>
|
||||||
|
|
||||||
|
@ -13,6 +14,7 @@
|
||||||
#include <QIcon>
|
#include <QIcon>
|
||||||
#include <QMimeData>
|
#include <QMimeData>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
@ -289,21 +291,22 @@ bool EntryModel::setData(const QModelIndex &index, const QVariant &value, int ro
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SerializedRole: {
|
case SerializedRole: {
|
||||||
NodeEntry *parent = entry->parent();
|
NodeEntry *const parent = entry->parent();
|
||||||
QModelIndex parentIndex = index.parent();
|
const QModelIndex parentIndex = index.parent();
|
||||||
if (!parent || !parentIndex.isValid()) {
|
if (!parent || !parentIndex.isValid()) {
|
||||||
break;
|
return false;
|
||||||
}
|
}
|
||||||
stringstream ss(stringstream::in | stringstream::out | stringstream::binary);
|
QByteArray array(value.toByteArray());
|
||||||
ss.exceptions(std::stringstream::failbit | std::stringstream::badbit);
|
|
||||||
QByteArray array = value.toByteArray();
|
|
||||||
if (array.isEmpty()) {
|
if (array.isEmpty()) {
|
||||||
break;
|
return false;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
ss.write(array.data(), array.size());
|
stringstream ss(stringstream::in | stringstream::out | stringstream::binary);
|
||||||
Entry *newEntry = Entry::parse(ss);
|
ss.exceptions(std::stringstream::failbit | std::stringstream::badbit);
|
||||||
int row = entry->index();
|
ss.rdbuf()->pubsetbuf(array.data(), array.size());
|
||||||
|
|
||||||
|
Entry *const newEntry = Entry::parse(ss);
|
||||||
|
const int row = entry->index();
|
||||||
beginRemoveRows(parentIndex, row, row);
|
beginRemoveRows(parentIndex, row, row);
|
||||||
delete entry;
|
delete entry;
|
||||||
endRemoveRows();
|
endRemoveRows();
|
||||||
|
@ -311,10 +314,15 @@ bool EntryModel::setData(const QModelIndex &index, const QVariant &value, int ro
|
||||||
newEntry->setParent(parent, row);
|
newEntry->setParent(parent, row);
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
return true;
|
return true;
|
||||||
|
} catch (const Io::ParsingException &parsingError) {
|
||||||
|
cerr << "EntryModel::setData: parsing exception: " << parsingError.what() << endl;
|
||||||
|
return false;
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
IoUtilities::catchIoFailure();
|
const char *const errorMessage(IoUtilities::catchIoFailure());
|
||||||
|
cerr << "EntryModel::setData: IO exception: " << errorMessage << endl;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
} break;
|
}
|
||||||
case DefaultExpandedRole:
|
case DefaultExpandedRole:
|
||||||
switch (entry->type()) {
|
switch (entry->type()) {
|
||||||
case EntryType::Account:
|
case EntryType::Account:
|
||||||
|
|
Loading…
Reference in New Issue