Improve coding style

This commit is contained in:
Martchus 2018-03-14 00:15:12 +01:00
parent 3590a5150c
commit 836e6bbcb2
11 changed files with 536 additions and 533 deletions

View File

@ -9,7 +9,7 @@ class FieldDelegate : public QStyledItemDelegate {
public:
FieldDelegate(QObject *parent = nullptr);
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
};
} // namespace QtGui

View File

@ -440,14 +440,12 @@ bool MainWindow::openFile(const QString &path, bool readOnly)
}
// warn before loading a very big file
if (m_file.size() > 10485760) {
if (QMessageBox::warning(this, QApplication::applicationName(),
if (m_file.size() > 10485760 && QMessageBox::warning(this, QApplication::applicationName(),
tr("The file you want to load seems to be very big. Do you really want to open it?"), QMessageBox::Yes, QMessageBox::No)
== QMessageBox::No) {
m_file.clear();
return false;
}
}
// ask for a password if required
if (m_file.isEncryptionUsed()) {
@ -488,7 +486,10 @@ bool MainWindow::openFile(const QString &path, bool readOnly)
}
// show a message in the error case
if (!msg.isEmpty()) {
if (msg.isEmpty()) {
// show contents
return showFile();
}
m_file.clear();
m_ui->statusBar->showMessage(msg, 5000);
if (QMessageBox::critical(this, QApplication::applicationName(), msg, QMessageBox::Cancel, QMessageBox::Retry) == QMessageBox::Retry) {
@ -496,10 +497,6 @@ bool MainWindow::openFile(const QString &path, bool readOnly)
} else {
return false;
}
} else {
// show contents
return showFile();
}
}
/*!
@ -618,7 +615,7 @@ void MainWindow::updateWindowTitle()
void MainWindow::applyDefaultExpanding(const QModelIndex &parent)
{
for (int row = 0, rows = m_entryFilterModel->rowCount(parent); row < rows; ++row) {
QModelIndex index = m_entryFilterModel->index(row, 0, parent);
const QModelIndex index = m_entryFilterModel->index(row, 0, parent);
if (!index.isValid()) {
return;
}
@ -735,7 +732,7 @@ bool MainWindow::askForCreatingFile()
/*!
* \brief Shows an warning if no file is opened.
* \retruns Returns whether the warning has been shown.
* \returns Returns whether the warning has been shown.
*/
bool MainWindow::showNoFileOpened()
{
@ -748,7 +745,7 @@ bool MainWindow::showNoFileOpened()
/*!
* \brief Shows an warning if no account is selected.
* \retruns Returns whether the warning has been shown.
* \returns Returns whether the warning has been shown.
*/
bool MainWindow::showNoAccount()
{
@ -821,11 +818,9 @@ bool MainWindow::saveFile()
return false;
}
}
} else {
if (!askForCreatingFile()) {
} else if (!askForCreatingFile()) {
return false;
}
}
// ask for a password if none is set
if (m_file.password()[0] == 0) {
@ -858,7 +853,7 @@ bool MainWindow::saveFile()
m_ui->statusBar->showMessage(msg, 5000);
QMessageBox::critical(this, QApplication::applicationName(), msg);
return false;
} else {
}
if (m_readOnly || m_somethingChanged) {
m_readOnly = false;
m_somethingChanged = false;
@ -867,7 +862,6 @@ bool MainWindow::saveFile()
m_recentMgr->addEntry(QString::fromStdString(m_file.path()));
m_ui->statusBar->showMessage(tr("The password list has been saved."), 5000);
return true;
}
}
/*!
@ -904,14 +898,15 @@ void MainWindow::showContainingDirectory()
{
if (showNoFileOpened()) {
return;
} else if (m_file.path().empty()) {
}
if (m_file.path().empty()) {
QMessageBox::warning(this, QApplication::applicationName(), tr("The currently opened file hasn't been saved yet."));
} else {
return;
}
const QFileInfo file(QString::fromStdString(m_file.path()));
if (file.dir().exists()) {
DesktopUtils::openLocalFileOrDir(file.dir().absolutePath());
}
}
}
/*!
@ -941,30 +936,29 @@ void MainWindow::addEntry(EntryType type)
return;
}
const QModelIndexList selectedIndexes = m_ui->treeView->selectionModel()->selectedRows(0);
if (selectedIndexes.size() == 1) {
const QModelIndex selected = m_entryFilterModel->mapToSource(selectedIndexes.at(0));
if (m_entryModel->isNode(selected)) {
const QModelIndex selected = selectedIndexes.size() == 1 ? m_entryFilterModel->mapToSource(selectedIndexes.at(0)) : QModelIndex();
if (!selected.isValid() || !m_entryModel->isNode(selected)) {
QMessageBox::warning(this, QApplication::applicationName(), tr("No node element selected."));
return;
}
bool result;
const QString text = QInputDialog::getText(this, type == EntryType::Account ? tr("Add account") : tr("Add category"),
tr("Enter the entry name"), QLineEdit::Normal, tr("new entry"), &result);
if (result) {
if (!text.isEmpty()) {
int row = m_entryModel->rowCount(selected);
m_entryModel->setInsertType(type);
if (m_entryModel->insertRow(row, selected)) {
m_entryModel->setData(m_entryModel->index(row, 0, selected), text, Qt::DisplayRole);
setSomethingChanged(true);
} else {
QMessageBox::warning(this, QApplication::applicationName(), tr("Unable to create new entry."));
}
} else {
QMessageBox::warning(this, QApplication::applicationName(), tr("You didn't enter text."));
}
}
if (!result) {
return;
}
if (text.isEmpty()) {
QMessageBox::warning(this, QApplication::applicationName(), tr("You didn't enter text."));
return;
}
QMessageBox::warning(this, QApplication::applicationName(), tr("No node element selected."));
int row = m_entryModel->rowCount(selected);
m_entryModel->setInsertType(type);
if (!m_entryModel->insertRow(row, selected)) {
QMessageBox::warning(this, QApplication::applicationName(), tr("Unable to create new entry."));
return;
}
m_entryModel->setData(m_entryModel->index(row, 0, selected), text, Qt::DisplayRole);
setSomethingChanged(true);
}
/*!
@ -976,14 +970,14 @@ void MainWindow::removeEntry()
return;
}
const QModelIndexList selectedIndexes = m_ui->treeView->selectionModel()->selectedRows(0);
if (selectedIndexes.size() == 1) {
if (selectedIndexes.size() != 1) {
QMessageBox::warning(this, QApplication::applicationName(), tr("No entry selected."));
return;
}
const QModelIndex selected = m_entryFilterModel->mapToSource(selectedIndexes.at(0));
if (!m_entryModel->removeRow(selected.row(), selected.parent())) {
QMessageBox::warning(this, QApplication::applicationName(), tr("Unable to remove the entry."));
}
} else {
QMessageBox::warning(this, QApplication::applicationName(), tr("No entry selected."));
}
}
/*!
@ -1023,7 +1017,11 @@ void MainWindow::insertRow()
return;
}
const QModelIndexList selectedIndexes = m_ui->tableView->selectionModel()->selectedIndexes();
if (selectedIndexes.size()) {
if (selectedIndexes.empty()) {
QMessageBox::warning(
this, windowTitle(), tr("A field has to be selected since new fields are always inserted before the currently selected field."));
return;
}
int row = m_fieldModel->rowCount();
for (const QModelIndex &index : selectedIndexes) {
if (index.row() < row) {
@ -1033,10 +1031,6 @@ void MainWindow::insertRow()
if (row < m_fieldModel->rowCount() - 1) {
m_fieldModel->insertRow(row);
}
} else {
QMessageBox::warning(
this, windowTitle(), tr("A field has to be selected since new fields are always inserted before the currently selected field."));
}
}
/*!
@ -1052,15 +1046,15 @@ void MainWindow::removeRows()
for (const QModelIndex &index : selectedIndexes) {
rows << index.row();
}
if (rows.size()) {
if (rows.empty()) {
QMessageBox::warning(this, windowTitle(), tr("No fields have been removed since there are currently no fields selected."));
return;
}
for (int i = m_fieldModel->rowCount() - 1; i >= 0; --i) {
if (rows.contains(i)) {
m_fieldModel->removeRow(i);
}
}
} else {
QMessageBox::warning(this, windowTitle(), tr("No fields have been removed since there are currently no fields selected."));
}
}
/*!
@ -1088,14 +1082,14 @@ void MainWindow::setFieldType(FieldType fieldType)
return;
}
const QModelIndexList selectedIndexes = m_ui->tableView->selectionModel()->selectedIndexes();
if (!selectedIndexes.isEmpty()) {
if (selectedIndexes.isEmpty()) {
QMessageBox::warning(this, windowTitle(), tr("No fields have been changed since there are currently no fields selected."));
return;
}
const QVariant typeVariant(static_cast<int>(fieldType));
for (const QModelIndex &index : selectedIndexes) {
m_fieldModel->setData(index, typeVariant, FieldTypeRole);
}
} else {
QMessageBox::warning(this, windowTitle(), tr("No fields have been changed since there are currently no fields selected."));
}
}
/*!
@ -1154,19 +1148,21 @@ void MainWindow::showTreeViewContextMenu()
return;
}
const QModelIndexList selectedIndexes = m_ui->treeView->selectionModel()->selectedRows(0);
if (selectedIndexes.size() == 1) {
if (selectedIndexes.size() != 1) {
return;
}
QMenu contextMenu(this);
QModelIndex selected = m_entryFilterModel->mapToSource(selectedIndexes.at(0));
Entry *entry = m_entryModel->entry(selected);
const QModelIndex selected(m_entryFilterModel->mapToSource(selectedIndexes.at(0)));
const Entry *const entry = m_entryModel->entry(selected);
if (entry->type() == EntryType::Node) {
contextMenu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), tr("Add account"), this, &MainWindow::addAccount);
contextMenu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), tr("Add category"), this, &MainWindow::addCategory);
}
contextMenu.addAction(QIcon::fromTheme(QStringLiteral("list-remove")), tr("Remove entry"), this, &MainWindow::removeEntry);
if (entry->type() == EntryType::Node) {
auto *nodeEntry = static_cast<NodeEntry *>(entry);
const auto *const nodeEntry = static_cast<const NodeEntry *>(entry);
contextMenu.addSeparator();
auto *action = new QAction(&contextMenu);
auto *const action = new QAction(&contextMenu);
action->setCheckable(true);
action->setText(tr("Expanded by default"));
action->setChecked(nodeEntry->isExpandedByDefault());
@ -1175,7 +1171,6 @@ void MainWindow::showTreeViewContextMenu()
contextMenu.addAction(action);
}
contextMenu.exec(QCursor::pos());
}
}
/*!

View File

@ -49,7 +49,7 @@ class MainWindow : public QMainWindow {
public:
explicit MainWindow(QSettings &settings, Dialogs::QtSettings *qtSettings = nullptr, QWidget *parent = nullptr);
~MainWindow();
~MainWindow() override;
public slots:
// file management
@ -71,9 +71,9 @@ public slots:
void showUndoView();
protected:
bool eventFilter(QObject *obj, QEvent *event);
void closeEvent(QCloseEvent *event);
void timerEvent(QTimerEvent *event);
bool eventFilter(QObject *obj, QEvent *event) override;
void closeEvent(QCloseEvent *event) override;
void timerEvent(QTimerEvent *event) override;
private slots:
// file management

View File

@ -70,7 +70,6 @@ PasswordGeneratorDialog::PasswordGeneratorDialog(QWidget *parent)
*/
PasswordGeneratorDialog::~PasswordGeneratorDialog()
{
delete m_ui;
}
/*!

View File

@ -6,6 +6,7 @@
#include <QDialog>
#include <vector>
#include <memory>
namespace QtGui {
@ -18,7 +19,7 @@ class PasswordGeneratorDialog : public QDialog {
public:
explicit PasswordGeneratorDialog(QWidget *parent = nullptr);
~PasswordGeneratorDialog();
~PasswordGeneratorDialog() override;
private Q_SLOTS:
void generateNewPassword();
@ -27,7 +28,7 @@ private Q_SLOTS:
void copyPassword();
private:
Ui::PasswordGeneratorDialog *m_ui;
std::unique_ptr<Ui::PasswordGeneratorDialog> m_ui;
std::vector<char> m_charset;
Util::OpenSslRandomDevice m_random;
};

View File

@ -37,15 +37,14 @@ inline QUndoStack *StackSupport::undoStack()
*/
inline bool StackSupport::push(CustomUndoCommand *command)
{
if (m_undoStack) {
if (!m_undoStack) {
return false;
}
if (command->isNoop()) {
return true; // doing nothing can never fail
} else {
}
m_undoStack->push(command);
return command->redoResult();
}
}
return false;
}
/*!

View File

@ -132,7 +132,7 @@ FieldModelInsertRowsCommand::FieldModelInsertRowsCommand(FieldModel *model, int
, m_row(row)
, m_count(count)
{
setText(QApplication::translate("undocommands", "insertion of %1 row(s) before row %2", 0, count).arg(count).arg(row + 1));
setText(QApplication::translate("undocommands", "insertion of %1 row(s) before row %2", nullptr, count).arg(count).arg(row + 1));
}
bool FieldModelInsertRowsCommand::internalRedo()
@ -163,9 +163,9 @@ FieldModelRemoveRowsCommand::FieldModelRemoveRowsCommand(FieldModel *model, int
, m_count(count)
{
if (count == 1) {
setText(QApplication::translate("undocommands", "removal of row %1", 0, count).arg(row + 1));
setText(QApplication::translate("undocommands", "removal of row %1", nullptr, count).arg(row + 1));
} else {
setText(QApplication::translate("undocommands", "removal of the rows %1 to %2", 0, count).arg(row + 1).arg(row + count));
setText(QApplication::translate("undocommands", "removal of the rows %1 to %2", nullptr, count).arg(row + 1).arg(row + count));
}
}

View File

@ -27,8 +27,8 @@ public:
bool redoResult() const;
bool undoResult() const;
bool isNoop() const;
void redo();
void undo();
void redo() override;
void undo() override;
protected:
void setNoop(bool noop);
@ -81,8 +81,8 @@ public:
explicit FieldModelSetValueCommand(FieldModel *model, const QModelIndex &index, const QVariant &value, int role);
protected:
bool internalRedo();
bool internalUndo();
bool internalRedo() override;
bool internalUndo() override;
private:
Io::AccountEntry *m_account;
@ -99,8 +99,8 @@ public:
explicit FieldModelInsertRowsCommand(FieldModel *model, int row, int count);
protected:
bool internalRedo();
bool internalUndo();
bool internalRedo() override;
bool internalUndo() override;
private:
Io::AccountEntry *m_account;
@ -114,8 +114,8 @@ public:
explicit FieldModelRemoveRowsCommand(FieldModel *model, int row, int count);
protected:
bool internalRedo();
bool internalUndo();
bool internalRedo() override;
bool internalUndo() override;
private:
Io::AccountEntry *m_account;
@ -130,8 +130,8 @@ public:
explicit EntryModelSetValueCommand(EntryModel *model, const QModelIndex &index, const QVariant &value, int role);
protected:
bool internalRedo();
bool internalUndo();
bool internalRedo() override;
bool internalUndo() override;
private:
EntryModel *m_model;
@ -143,12 +143,10 @@ private:
class EntryModelModifyRowsCommand : public CustomUndoCommand {
public:
~EntryModelModifyRowsCommand();
~EntryModelModifyRowsCommand() override;
protected:
explicit EntryModelModifyRowsCommand(EntryModel *model, int row, int count, const QModelIndex &parent);
bool internalRedo() = 0;
bool internalUndo() = 0;
bool insert();
bool remove();
EntryModel *m_model;
@ -163,8 +161,8 @@ public:
explicit EntryModelInsertRowsCommand(EntryModel *model, int row, int count, const QModelIndex &parent);
protected:
bool internalRedo();
bool internalUndo();
bool internalRedo() override;
bool internalUndo() override;
};
class EntryModelRemoveRowsCommand : public EntryModelModifyRowsCommand {
@ -172,8 +170,8 @@ public:
explicit EntryModelRemoveRowsCommand(EntryModel *model, int row, int count, const QModelIndex &parent);
protected:
bool internalRedo();
bool internalUndo();
bool internalRedo() override;
bool internalUndo() override;
};
class EntryModelMoveRowsCommand : public CustomUndoCommand {
@ -182,8 +180,8 @@ public:
EntryModel *model, const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild);
protected:
bool internalRedo();
bool internalUndo();
bool internalRedo() override;
bool internalUndo() override;
private:
EntryModel *m_model;

View File

@ -61,11 +61,7 @@ EntryModel::EntryModel(QUndoStack *undoStack, QObject *parent) :
*/
Entry *EntryModel::entry(const QModelIndex &index)
{
if(index.isValid()) {
return static_cast<Entry *>(index.internalPointer());
} else {
return nullptr;
}
return index.isValid() ? static_cast<Entry *>(index.internalPointer()) : nullptr;
}
/*!
@ -78,10 +74,12 @@ Entry *EntryModel::entry(const QModelIndex &index)
*/
QList<Entry *> EntryModel::takeEntries(int row, int count, const QModelIndex &parent)
{
Entry *const parentEntry = entry(parent);
if(!parentEntry || parentEntry->type() != EntryType::Node) {
return QList<Entry *>();
}
QList<Entry *> res;
if(Entry *parentEntry = entry(parent)) {
if(parentEntry->type() == EntryType::Node) {
NodeEntry *parentNodeEntry = static_cast<NodeEntry *>(parentEntry);
NodeEntry *const parentNodeEntry = static_cast<NodeEntry *>(parentEntry);
int lastIndex = row + count - 1;
const vector<Entry *> &children = parentNodeEntry->children();
if(lastIndex < 0 || static_cast<size_t>(lastIndex) >= children.size()) {
@ -89,13 +87,11 @@ QList<Entry *> EntryModel::takeEntries(int row, int count, const QModelIndex &pa
}
beginRemoveRows(parent, row, lastIndex);
for(int index = lastIndex; index >= row; --index) {
Entry *child = children.at(index);
Entry *const child = children[index];
child->setParent(nullptr);
res << child;
}
endRemoveRows();
}
}
return res;
}
@ -111,43 +107,46 @@ bool EntryModel::insertEntries(int row, const QModelIndex &parent, const QList<E
if(entries.isEmpty()) {
return true;
}
if(Entry *parentEntry = entry(parent)) {
if(parentEntry->type() == EntryType::Node) {
NodeEntry *parentNodeEntry = static_cast<NodeEntry *>(parentEntry);
Entry *const parentEntry = entry(parent);
if(!parentEntry || parentEntry->type() != EntryType::Node) {
return false;
}
NodeEntry *const parentNodeEntry = static_cast<NodeEntry *>(parentEntry);
const vector<Entry *> &children = parentNodeEntry->children();
if(row < 0 || static_cast<size_t>(row) > children.size()) {
row = children.size();
}
beginInsertRows(parent, row, row + entries.size() - 1);
foreach(Entry *entry, entries) {
for(Entry *const entry : entries) {
entry->setParent(parentNodeEntry, row);
++row;
}
endInsertRows();
return true;
}
}
return false;
}
QModelIndex EntryModel::index(int row, int column, const QModelIndex &parent) const
{
if(parent.isValid()) {
if(Entry *parentEntry = static_cast<Entry *>(parent.internalPointer())) {
if(!parent.isValid()) {
if(m_rootEntry && row == 0) {
return createIndex(row, column, m_rootEntry);
}
return QModelIndex();
}
const auto *const parentEntry = static_cast<const Entry *>(parent.internalPointer());
if(!parentEntry) {
return QModelIndex();
}
switch(parentEntry->type()) {
case EntryType::Node: {
const std::vector<Entry *> &children = static_cast<NodeEntry *>(parentEntry)->children();
const std::vector<Entry *> &children = static_cast<const NodeEntry *>(parentEntry)->children();
if(row >= 0 && static_cast<size_t>(row) < children.size()) {
return createIndex(row, column, children.at(row));
return createIndex(row, column, children[static_cast<size_t>(row)]);
}
break;
} case EntryType::Account:
;
}
}
} else if(m_rootEntry && row == 0) {
return createIndex(row, column, m_rootEntry);
}
return QModelIndex();
}
@ -166,29 +165,27 @@ QModelIndex EntryModel::index(Entry *entry) const
QModelIndex EntryModel::parent(const QModelIndex &child) const
{
if(child.isValid()) {
if(Entry *entry = static_cast<Entry *>(child.internalPointer())) {
NodeEntry *parent = entry->parent();
if(!child.isValid()) {
return QModelIndex();
}
const auto *const entry = static_cast<Entry *>(child.internalPointer());
if(!entry) {
return QModelIndex();
}
NodeEntry *const parent = entry->parent();
if(parent && (child.row() >= 0 && static_cast<size_t>(child.row()) < parent->children().size())) {
return createIndex(parent->index() > 0 ? parent->index() : 0, 0, parent);
}
}
}
return QModelIndex();
}
bool EntryModel::hasChildren(const QModelIndex &parent) const
{
if(parent.isValid()) {
if(Entry *entry = static_cast<Entry *>(parent.internalPointer())) {
if(entry->type() == EntryType::Node) {
return static_cast<NodeEntry *>(entry)->children().size();
}
}
} else {
if(!parent.isValid()) {
return true;
}
return false;
const auto *const entry = static_cast<Entry *>(parent.internalPointer());
return entry && entry->type() == EntryType::Node && !static_cast<const NodeEntry *>(entry)->children().empty();
}
/*!
@ -197,18 +194,22 @@ bool EntryModel::hasChildren(const QModelIndex &parent) const
*/
bool EntryModel::isNode(const QModelIndex &parent) const
{
if(parent.isValid()) {
if(Entry *entry = static_cast<Entry *>(parent.internalPointer())) {
return entry->type() == EntryType::Node;
}
}
if(!parent.isValid()) {
return false;
}
const auto *const entry = static_cast<const Entry *>(parent.internalPointer());
return entry && entry->type() == EntryType::Node;
}
QVariant EntryModel::data(const QModelIndex &index, int role) const
{
if(index.isValid()) {
if(Entry *entry = static_cast<Entry *>(index.internalPointer())) {
if(!index.isValid()) {
return QVariant();
}
const auto *const entry = static_cast<const Entry *>(index.internalPointer());
if(!entry) {
return QVariant();
}
switch(role) {
case Qt::DisplayRole:
case Qt::EditRole:
@ -230,8 +231,8 @@ QVariant EntryModel::data(const QModelIndex &index, int role) const
ss.exceptions(std::stringstream::failbit | std::stringstream::badbit);
try {
entry->make(ss);
string string = ss.str();
return QByteArray(string.data(), string.size());
const auto str(ss.str());
return QByteArray(str.data(), str.size());
} catch(...) {
IoUtilities::catchIoFailure();
return false;
@ -239,21 +240,19 @@ QVariant EntryModel::data(const QModelIndex &index, int role) const
}
break;
case DefaultExpandedRole:
return entry->type() == EntryType::Node && static_cast<NodeEntry *>(entry)->isExpandedByDefault();
return entry->type() == EntryType::Node && static_cast<const NodeEntry *>(entry)->isExpandedByDefault();
default:
;
}
}
}
return QVariant();
}
QMap<int, QVariant> EntryModel::itemData(const QModelIndex &index) const
{
QMap<int, QVariant> roles;
roles.insert(Qt::DisplayRole, data(index, Qt::DisplayRole));
roles.insert(SerializedRole, data(index, SerializedRole));
return roles;
return QMap<int, QVariant>{
{Qt::DisplayRole, data(index, Qt::DisplayRole)},
{SerializedRole, data(index, SerializedRole)},
};
}
bool EntryModel::setData(const QModelIndex &index, const QVariant &value, int role)
@ -263,8 +262,13 @@ bool EntryModel::setData(const QModelIndex &index, const QVariant &value, int ro
return push(new EntryModelSetValueCommand(this, index, value, role));
}
#endif
if(index.isValid()) {
if(Entry *entry = static_cast<Entry *>(index.internalPointer())) {
if(!index.isValid()) {
return false;
}
auto *const entry = static_cast<Entry *>(index.internalPointer());
if(!entry) {
return false;
}
switch(role) {
case Qt::DisplayRole:
case Qt::EditRole:
@ -280,11 +284,15 @@ bool EntryModel::setData(const QModelIndex &index, const QVariant &value, int ro
case SerializedRole: {
NodeEntry *parent = entry->parent();
QModelIndex parentIndex = index.parent();
if(parent && parentIndex.isValid()) {
if(!parent || !parentIndex.isValid()) {
break;
}
stringstream ss(stringstream::in | stringstream::out | stringstream::binary);
ss.exceptions(std::stringstream::failbit | std::stringstream::badbit);
QByteArray array = value.toByteArray();
if(array.size()) {
if(array.isEmpty()) {
break;
}
try {
ss.write(array.data(), array.size());
Entry *newEntry = Entry::parse(ss);
@ -300,8 +308,6 @@ bool EntryModel::setData(const QModelIndex &index, const QVariant &value, int ro
IoUtilities::catchIoFailure();
}
}
}
}
break;
case DefaultExpandedRole:
switch(entry->type()) {
@ -316,8 +322,6 @@ bool EntryModel::setData(const QModelIndex &index, const QVariant &value, int ro
default:
;
}
}
}
return false;
}
@ -388,9 +392,13 @@ bool EntryModel::insertRows(int row, int count, const QModelIndex &parent)
return push(new EntryModelInsertRowsCommand(this, row, count, parent));
}
#endif
if(parent.isValid()) {
if(Entry *parentEntry = static_cast<Entry *>(parent.internalPointer())) {
if(parentEntry->type() == EntryType::Node) {
if(!parent.isValid()) {
return false;
}
auto *const parentEntry = static_cast<Entry *>(parent.internalPointer());
if(!parentEntry || parentEntry->type() != EntryType::Node) {
return false;
}
beginInsertRows(parent, row, row + count - 1);
for(int end = row + count; row < end; ++row) {
Entry *newEntry;
@ -408,10 +416,6 @@ bool EntryModel::insertRows(int row, int count, const QModelIndex &parent)
}
endInsertRows();
return true;
}
}
}
return false;
}
bool EntryModel::removeRows(int row, int count, const QModelIndex &parent)
@ -421,17 +425,17 @@ bool EntryModel::removeRows(int row, int count, const QModelIndex &parent)
return push(new EntryModelRemoveRowsCommand(this, row, count, parent));
}
#endif
if(parent.isValid() && count > 0) {
if(Entry *parentEntry = static_cast<Entry *>(parent.internalPointer())) {
if(parentEntry->type() == EntryType::Node) {
if(!parent.isValid() || count <= 0) {
return false;
}
auto *const parentEntry = static_cast<Entry *>(parent.internalPointer());
if(!parentEntry || parentEntry->type() != EntryType::Node) {
return false;
}
beginRemoveRows(parent, row, row + count - 1);
static_cast<NodeEntry *>(parentEntry)->deleteChildren(row, row + count);
endRemoveRows();
return true;
}
}
}
return false;
}
bool EntryModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
@ -442,20 +446,24 @@ bool EntryModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int co
}
#endif
// check validation of specified arguments
if(sourceParent.isValid() && destinationParent.isValid()
&& sourceRow >= 0 && count > 0
&& entry(sourceParent)->type() == EntryType::Node // source and destination parent entries
&& entry(destinationParent)->type() == EntryType::Node) { // need to be node entries
if(!sourceParent.isValid() || !destinationParent.isValid()
|| sourceRow < 0 || count <= 0
|| entry(sourceParent)->type() != EntryType::Node // source and destination parent entries
|| entry(destinationParent)->type() != EntryType::Node) { // need to be node entries
return false;
}
// determine the source parent entry and dest parent entry as node entries
NodeEntry *srcParentEntry = static_cast<NodeEntry *>(sourceParent.internalPointer());
NodeEntry *destParentEntry = static_cast<NodeEntry *>(destinationParent.internalPointer());
auto *const srcParentEntry = static_cast<NodeEntry *>(sourceParent.internalPointer());
auto *const destParentEntry = static_cast<NodeEntry *>(destinationParent.internalPointer());
// source rows must be within the valid range
if(static_cast<size_t>(sourceRow + count) <= srcParentEntry->children().size()
if(static_cast<size_t>(sourceRow + count) > srcParentEntry->children().size()
// if source and destination parent are the same the destination child mustn't be in the source range
&& (srcParentEntry != destParentEntry || (destinationChild < sourceRow || (sourceRow + count) < destinationChild))) {
|| !(srcParentEntry != destParentEntry || (destinationChild < sourceRow || (sourceRow + count) < destinationChild))) {
return false;
}
// do not move a row to one of its own children! -> check before
for(int index = 0; index < count; ++index) {
Entry *toMove = srcParentEntry->children().at(sourceRow + index);
Entry *toMove = srcParentEntry->children()[static_cast<size_t>(sourceRow + index)];
if(toMove->type() == EntryType::Node) {
if(destParentEntry->isIndirectChildOf(static_cast<NodeEntry *>(toMove))) {
return false;
@ -465,7 +473,7 @@ bool EntryModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int co
// actually perform the move operation
beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, destinationChild);
for(int index = 0; index < count; ++index) {
Entry *toMove = srcParentEntry->children().at(sourceRow + index);
Entry *toMove = srcParentEntry->children()[static_cast<size_t>(sourceRow + index)];
if(srcParentEntry == destParentEntry && sourceRow < destinationChild) {
toMove->setParent(destParentEntry, destinationChild + index - 1);
} else {
@ -474,9 +482,6 @@ bool EntryModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int co
}
endMoveRows();
return true;
}
}
return false;
}
QStringList EntryModel::mimeTypes() const
@ -497,17 +502,18 @@ QMimeData *EntryModel::mimeData(const QModelIndexList &indexes) const
QStringList plainTextParts;
QByteArray encoded;
QDataStream dataStream(&encoded, QIODevice::WriteOnly);
foreach(const QModelIndex &index, indexes) {
if(index.isValid()) {
Entry *entry = static_cast<Entry *>(index.internalPointer());
list<string> path = entry->path();
for(const QModelIndex &index : indexes) {
if(!index.isValid()) {
continue;
}
const auto *const entry = static_cast<const Entry *>(index.internalPointer());
const auto path(entry->path());
dataStream << static_cast<quint32>(path.size());
for(const string &part : path) {
dataStream << QString::fromStdString(part);
}
plainTextParts << QString::fromStdString(entry->label());
}
}
data->setData(types.at(0), encoded);
data->setText(plainTextParts.join(QStringLiteral(", ")));
return data;
@ -533,7 +539,7 @@ bool EntryModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int
column = 0;
}
// decode and insert
QByteArray encoded = data->data(format);
QByteArray encoded(data->data(format));
QDataStream stream(&encoded, QIODevice::ReadOnly);
int moved = 0;
while(!stream.atEnd()) {
@ -545,14 +551,15 @@ bool EntryModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int
stream >> part;
path.push_back(part.toStdString());
}
if(Entry *entry = m_rootEntry->entryByPath(path, true)) {
if(NodeEntry *srcParentEntry = entry->parent()) {
if(moveRows(index(srcParentEntry), entry->index(), 1, parent, row)) {
auto *const entry = m_rootEntry->entryByPath(path, true);
if(!entry) {
continue;
}
auto *const srcParentEntry = entry->parent();
if(srcParentEntry && moveRows(index(srcParentEntry), entry->index(), 1, parent, row)) {
++moved;
}
}
}
}
return false;
}

View File

@ -55,7 +55,9 @@ FieldModel::FieldModel(QUndoStack *undoStack, QObject *parent) :
*/
void FieldModel::setAccountEntry(AccountEntry *entry)
{
if(entry != m_accountEntry) {
if(entry == m_accountEntry) {
return;
}
beginResetModel();
if((m_accountEntry = entry)) {
m_fields = &entry->fields();
@ -63,12 +65,13 @@ void FieldModel::setAccountEntry(AccountEntry *entry)
m_fields = nullptr;
}
endResetModel();
}
}
QVariant FieldModel::data(const QModelIndex &index, int role) const
{
if(index.isValid() && m_fields && index.row() >= 0) {
if(!index.isValid() || !m_fields || index.row() < 0) {
return QVariant();
}
// return data for existent field
if(static_cast<size_t>(index.row()) < m_fields->size()) {
switch(role) {
@ -76,19 +79,19 @@ QVariant FieldModel::data(const QModelIndex &index, int role) const
case Qt::EditRole:
switch(index.column()) {
case 0:
return QString::fromStdString(m_fields->at(index.row()).name());
return QString::fromStdString((*m_fields)[static_cast<size_t>(index.row())].name());
case 1:
return (m_passwordVisibility == PasswordVisibility::Always
|| role == Qt::EditRole
|| m_fields->at(index.row()).type() != FieldType::Password)
? QString::fromStdString(m_fields->at(index.row()).value())
: QString(m_fields->at(index.row()).value().size(), QChar(0x2022));
|| (*m_fields)[static_cast<size_t>(index.row())].type() != FieldType::Password)
? QString::fromStdString((*m_fields)[static_cast<size_t>(index.row())].value())
: QString((*m_fields)[static_cast<size_t>(index.row())].value().size(), QChar(0x2022));
default:
;
}
break;
case FieldTypeRole:
return static_cast<int>(m_fields->at(index.row()).type());
return static_cast<int>((*m_fields)[static_cast<size_t>(index.row())].type());
default:
;
}
@ -110,21 +113,22 @@ QVariant FieldModel::data(const QModelIndex &index, int role) const
;
}
}
}
return QVariant();
}
QMap<int, QVariant> FieldModel::itemData(const QModelIndex &index) const
{
static int roles[] = {Qt::EditRole, FieldTypeRole};
static const auto roleMap = [this, index] {
QMap<int, QVariant> roleMap;
for(int role : roles) {
QVariant variantData = data(index, role);
for(const auto role : initializer_list<int>{Qt::EditRole, FieldTypeRole}) {
const auto variantData(data(index, role));
if (variantData.isValid()) {
roleMap.insert(role, variantData);
}
}
return roleMap;
}();
return roleMap;
}
bool FieldModel::setData(const QModelIndex &index, const QVariant &value, int role)
@ -134,10 +138,12 @@ bool FieldModel::setData(const QModelIndex &index, const QVariant &value, int ro
return push(new FieldModelSetValueCommand(this, index, value, role));
}
#endif
if(!index.isValid() || !m_fields || index.row() < 0) {
return false;
}
QVector<int> roles;
if(index.isValid() && m_fields && index.row() >= 0) {
// set data for existing field
if(static_cast<size_t>(index.row()) < m_fields->size()) {
// set data for existing field
switch(role) {
case Qt::EditRole:
switch(index.column()) {
@ -170,8 +176,8 @@ bool FieldModel::setData(const QModelIndex &index, const QVariant &value, int ro
m_fields->pop_back();
endRemoveRows();
}
// set data for a new field emplaced at the end of the field list
} else if(static_cast<size_t>(index.row()) == m_fields->size() && !value.toString().isEmpty()) {
// set data for a new field emplaced at the end of the field list
switch(role) {
case Qt::DisplayRole:
case Qt::EditRole:
@ -198,11 +204,10 @@ bool FieldModel::setData(const QModelIndex &index, const QVariant &value, int ro
;
}
}
}
// return false if nothing could be changed
if(roles.isEmpty()) {
return false;
} else {
}
// some roles affect other roles
switch(role) {
case Qt::EditRole:
@ -214,7 +219,6 @@ bool FieldModel::setData(const QModelIndex &index, const QVariant &value, int ro
default:
;
}
}
// emit data changed signal on sucess
emit dataChanged(index, index, roles);
return true;
@ -267,13 +271,13 @@ bool FieldModel::insertRows(int row, int count, const QModelIndex &parent)
return push(new FieldModelInsertRowsCommand(this, row, count));
}
#endif
if(!parent.isValid() && row >= 0 && count > 0 && static_cast<size_t>(row) <= m_fields->size()) {
if(parent.isValid() || row < 0 || count <= 0 || static_cast<size_t>(row + count) > m_fields->size()) {
return false;
}
beginInsertRows(parent, row, row + count - 1);
m_fields->insert(m_fields->begin() + row, count, Field(m_accountEntry));
m_fields->insert(m_fields->begin() + row, static_cast<size_t>(count), Field(m_accountEntry));
endInsertRows();
return true;
}
return false;
}
bool FieldModel::removeRows(int row, int count, const QModelIndex &parent)
@ -283,22 +287,19 @@ bool FieldModel::removeRows(int row, int count, const QModelIndex &parent)
return push(new FieldModelRemoveRowsCommand(this, row, count));
}
#endif
if(!parent.isValid() && row >= 0 && count > 0 && static_cast<size_t>(row + count) <= m_fields->size()) {
if(parent.isValid() || row < 0 || count <= 0 || static_cast<size_t>(row + count) > m_fields->size()) {
return false;
}
beginRemoveRows(parent, row, row + count - 1);
m_fields->erase(m_fields->begin() + row, m_fields->begin() + row + count);
endRemoveRows();
return true;
}
return false;
}
bool FieldModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
if(!QAbstractTableModel::dropMimeData(data, action, row, column, parent)) {
if(data->hasText()) {
if(!QAbstractTableModel::dropMimeData(data, action, row, column, parent) && data->hasText()) {
return setData(parent, data->text(), Qt::EditRole);
}
}
return false;
}
@ -310,13 +311,14 @@ QStringList FieldModel::mimeTypes() const
QMimeData *FieldModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *data = QAbstractTableModel::mimeData(indexes);
if(!indexes.isEmpty()) {
if(indexes.isEmpty()) {
return data;
}
QStringList result;
foreach(const QModelIndex &index, indexes) {
for(const QModelIndex &index : indexes) {
result << index.data(Qt::EditRole).toString();
}
data->setText(result.join(QChar('\n')));
}
return data;
}
@ -328,7 +330,7 @@ QMimeData *FieldModel::mimeData(const QModelIndexList &indexes) const
const Field *FieldModel::field(size_t row) const
{
if(m_fields && row < m_fields->size()) {
return &m_fields->at(row);
return &(*m_fields)[row];
}
return nullptr;
}

View File

@ -30,10 +30,12 @@ inline QString ApplicationPaths::path(QStandardPaths::StandardLocation location)
{
QString path = QStandardPaths::standardLocations(location).value(0);
QDir dir(path);
if (!dir.exists())
if (!dir.exists()) {
dir.mkpath(path);
if (!path.isEmpty() && !path.endsWith("/"))
}
if (!path.isEmpty() && !path.endsWith("/")) {
path += "/";
}
return path;
}
} // namespace QtGui