From 01e9d39b63b500944c59adbe163e6d5a8bfa57b0 Mon Sep 17 00:00:00 2001 From: Janek Bevendorff Date: Sat, 28 Jan 2017 14:18:43 +0100 Subject: [PATCH 1/2] Add 'Remember my choice' checkbox Allow user to store preference when asked whether to execute command, resolves #51 --- src/core/Entry.cpp | 6 +++++ src/core/EntryAttributes.cpp | 1 + src/core/EntryAttributes.h | 1 + src/gui/DatabaseWidget.cpp | 44 ++++++++++++++++++++++++++----- src/gui/entry/EditEntryWidget.cpp | 6 ++--- 5 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index ecc275a28..46e2670ac 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -353,6 +353,12 @@ void Entry::setTitle(const QString& title) void Entry::setUrl(const QString& url) { + bool remove = url != m_attributes->value(EntryAttributes::URLKey) && + (m_attributes->value(EntryAttributes::RememberCmdExecAttr) == "1" || + m_attributes->value(EntryAttributes::RememberCmdExecAttr) == "0"); + if (remove) { + m_attributes->remove(EntryAttributes::RememberCmdExecAttr); + } m_attributes->set(EntryAttributes::URLKey, url, m_attributes->isProtected(EntryAttributes::URLKey)); } diff --git a/src/core/EntryAttributes.cpp b/src/core/EntryAttributes.cpp index 195a8f14a..b633cae32 100644 --- a/src/core/EntryAttributes.cpp +++ b/src/core/EntryAttributes.cpp @@ -24,6 +24,7 @@ const QString EntryAttributes::URLKey = "URL"; const QString EntryAttributes::NotesKey = "Notes"; const QStringList EntryAttributes::DefaultAttributes(QStringList() << TitleKey << UserNameKey << PasswordKey << URLKey << NotesKey); +const QString EntryAttributes::RememberCmdExecAttr = "_EXEC_CMD"; EntryAttributes::EntryAttributes(QObject* parent) : QObject(parent) diff --git a/src/core/EntryAttributes.h b/src/core/EntryAttributes.h index 1c0ddaaeb..211b6d483 100644 --- a/src/core/EntryAttributes.h +++ b/src/core/EntryAttributes.h @@ -52,6 +52,7 @@ public: static const QString URLKey; static const QString NotesKey; static const QStringList DefaultAttributes; + static const QString RememberCmdExecAttr; static bool isDefaultAttribute(const QString& key); Q_SIGNALS: diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index 9b19d161a..46b15f5da 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -496,17 +497,46 @@ void DatabaseWidget::openUrlForEntry(Entry* entry) } if (urlString.startsWith("cmd://")) { + // check if decision to execute command was stored + if (entry->attributes()->hasKey(EntryAttributes::RememberCmdExecAttr)) { + if (entry->attributes()->value(EntryAttributes::RememberCmdExecAttr) == "1") { + QProcess::startDetached(urlString.mid(6)); + } + return; + } + + // otherwise ask user if (urlString.length() > 6) { - QMessageBox::StandardButton result; - result = MessageBox::question( - this, tr("Execute command?"), - tr("Do you really want to execute the following command?

%1") - .arg(urlString.left(200).toHtmlEscaped()), - QMessageBox::Yes | QMessageBox::No); - + QString cmdTruncated = urlString; + if (cmdTruncated.length() > 400) + cmdTruncated = cmdTruncated.left(400) + " […]"; + QMessageBox msgbox(QMessageBox::Icon::Question, + tr("Execute command?"), + tr("Do you really want to execute the following command?

%1
") + .arg(cmdTruncated.toHtmlEscaped()), + QMessageBox::Yes | QMessageBox::No, + this + ); + msgbox.setDefaultButton(QMessageBox::No); + + QCheckBox* checkbox = new QCheckBox(tr("Remember my choice"), &msgbox); + msgbox.setCheckBox(checkbox); + bool remember = false; + QObject::connect(checkbox, &QCheckBox::stateChanged, [&](int state) { + if (static_cast(state) == Qt::CheckState::Checked) { + remember = true; + } + }); + + int result = msgbox.exec(); if (result == QMessageBox::Yes) { QProcess::startDetached(urlString.mid(6)); } + + if (remember) { + entry->attributes()->set(EntryAttributes::RememberCmdExecAttr, + result == QMessageBox::Yes ? "1" : "0"); + } } } else { diff --git a/src/gui/entry/EditEntryWidget.cpp b/src/gui/entry/EditEntryWidget.cpp index c823b3de6..f3535f9b0 100644 --- a/src/gui/entry/EditEntryWidget.cpp +++ b/src/gui/entry/EditEntryWidget.cpp @@ -434,6 +434,9 @@ void EditEntryWidget::saveEntry() void EditEntryWidget::updateEntryData(Entry* entry) const { + entry->attributes()->copyCustomKeysFrom(m_entryAttributes); + entry->attachments()->copyDataFrom(m_entryAttachments); + entry->setTitle(m_mainUi->titleEdit->text()); entry->setUsername(m_mainUi->usernameEdit->text()); entry->setUrl(m_mainUi->urlEdit->text()); @@ -443,9 +446,6 @@ void EditEntryWidget::updateEntryData(Entry* entry) const entry->setNotes(m_mainUi->notesEdit->toPlainText()); - entry->attributes()->copyCustomKeysFrom(m_entryAttributes); - entry->attachments()->copyDataFrom(m_entryAttachments); - IconStruct iconStruct = m_iconsWidget->state(); if (iconStruct.number < 0) { From 3e6f76826b4207dfac13eeff375b30ca0149374d Mon Sep 17 00:00:00 2001 From: Janek Bevendorff Date: Sat, 28 Jan 2017 14:24:33 +0100 Subject: [PATCH 2/2] Don't show cmd:// prefix in confirmation dialog --- src/gui/DatabaseWidget.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index 46b15f5da..ec65ad90c 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -507,7 +507,7 @@ void DatabaseWidget::openUrlForEntry(Entry* entry) // otherwise ask user if (urlString.length() > 6) { - QString cmdTruncated = urlString; + QString cmdTruncated = urlString.mid(6); if (cmdTruncated.length() > 400) cmdTruncated = cmdTruncated.left(400) + " […]"; QMessageBox msgbox(QMessageBox::Icon::Question,