Add 'Remember my choice' checkbox

Allow user to store preference when asked whether to execute command, resolves #51
This commit is contained in:
Janek Bevendorff 2017-01-28 14:18:43 +01:00
parent 0116d4176a
commit 01e9d39b63
No known key found for this signature in database
GPG Key ID: CFEC2F6850BFFA53
5 changed files with 48 additions and 10 deletions

View File

@ -353,6 +353,12 @@ void Entry::setTitle(const QString& title)
void Entry::setUrl(const QString& url) 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)); m_attributes->set(EntryAttributes::URLKey, url, m_attributes->isProtected(EntryAttributes::URLKey));
} }

View File

@ -24,6 +24,7 @@ const QString EntryAttributes::URLKey = "URL";
const QString EntryAttributes::NotesKey = "Notes"; const QString EntryAttributes::NotesKey = "Notes";
const QStringList EntryAttributes::DefaultAttributes(QStringList() << TitleKey << UserNameKey const QStringList EntryAttributes::DefaultAttributes(QStringList() << TitleKey << UserNameKey
<< PasswordKey << URLKey << NotesKey); << PasswordKey << URLKey << NotesKey);
const QString EntryAttributes::RememberCmdExecAttr = "_EXEC_CMD";
EntryAttributes::EntryAttributes(QObject* parent) EntryAttributes::EntryAttributes(QObject* parent)
: QObject(parent) : QObject(parent)

View File

@ -52,6 +52,7 @@ public:
static const QString URLKey; static const QString URLKey;
static const QString NotesKey; static const QString NotesKey;
static const QStringList DefaultAttributes; static const QStringList DefaultAttributes;
static const QString RememberCmdExecAttr;
static bool isDefaultAttribute(const QString& key); static bool isDefaultAttribute(const QString& key);
Q_SIGNALS: Q_SIGNALS:

View File

@ -19,6 +19,7 @@
#include <QAction> #include <QAction>
#include <QDesktopServices> #include <QDesktopServices>
#include <QCheckBox>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QLabel> #include <QLabel>
#include <QFile> #include <QFile>
@ -496,17 +497,46 @@ void DatabaseWidget::openUrlForEntry(Entry* entry)
} }
if (urlString.startsWith("cmd://")) { 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) { if (urlString.length() > 6) {
QMessageBox::StandardButton result; QString cmdTruncated = urlString;
result = MessageBox::question( if (cmdTruncated.length() > 400)
this, tr("Execute command?"), cmdTruncated = cmdTruncated.left(400) + " […]";
tr("Do you really want to execute the following command?<br><br>%1") QMessageBox msgbox(QMessageBox::Icon::Question,
.arg(urlString.left(200).toHtmlEscaped()), tr("Execute command?"),
QMessageBox::Yes | QMessageBox::No); tr("Do you really want to execute the following command?<br><br>%1<br>")
.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<Qt::CheckState>(state) == Qt::CheckState::Checked) {
remember = true;
}
});
int result = msgbox.exec();
if (result == QMessageBox::Yes) { if (result == QMessageBox::Yes) {
QProcess::startDetached(urlString.mid(6)); QProcess::startDetached(urlString.mid(6));
} }
if (remember) {
entry->attributes()->set(EntryAttributes::RememberCmdExecAttr,
result == QMessageBox::Yes ? "1" : "0");
}
} }
} }
else { else {

View File

@ -434,6 +434,9 @@ void EditEntryWidget::saveEntry()
void EditEntryWidget::updateEntryData(Entry* entry) const void EditEntryWidget::updateEntryData(Entry* entry) const
{ {
entry->attributes()->copyCustomKeysFrom(m_entryAttributes);
entry->attachments()->copyDataFrom(m_entryAttachments);
entry->setTitle(m_mainUi->titleEdit->text()); entry->setTitle(m_mainUi->titleEdit->text());
entry->setUsername(m_mainUi->usernameEdit->text()); entry->setUsername(m_mainUi->usernameEdit->text());
entry->setUrl(m_mainUi->urlEdit->text()); entry->setUrl(m_mainUi->urlEdit->text());
@ -443,9 +446,6 @@ void EditEntryWidget::updateEntryData(Entry* entry) const
entry->setNotes(m_mainUi->notesEdit->toPlainText()); entry->setNotes(m_mainUi->notesEdit->toPlainText());
entry->attributes()->copyCustomKeysFrom(m_entryAttributes);
entry->attachments()->copyDataFrom(m_entryAttachments);
IconStruct iconStruct = m_iconsWidget->state(); IconStruct iconStruct = m_iconsWidget->state();
if (iconStruct.number < 0) { if (iconStruct.number < 0) {