Commit f2e5d1fe authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Refactor CertifyWidget and CertifyCertificateDialog

* Move check for valid input from CertifyCertificateDialog to CertifyWidget
* Add check for null keys
* Add check for empty list of selected user ids
* Disable Certify button if input is not valid

GnuPG-bug-id: 5245
parent f2062056
......@@ -45,39 +45,23 @@ CertifyCertificateDialog::CertifyCertificateDialog(QWidget *p, Qt::WindowFlags f
mCertWidget = new CertifyWidget(this);
mainLay->addWidget(mCertWidget);
auto buttonBox = new QDialogButtonBox();
auto buttonBox = new QDialogButtonBox{this};
buttonBox->setStandardButtons(QDialogButtonBox::Cancel |
QDialogButtonBox::Ok);
KGuiItem::assign(buttonBox->button(QDialogButtonBox::Ok), KStandardGuiItem::ok());
const auto okButton = buttonBox->button(QDialogButtonBox::Ok);
KGuiItem::assign(okButton, KStandardGuiItem::ok());
okButton->setText(i18n("Certify"));
KGuiItem::assign(buttonBox->button(QDialogButtonBox::Cancel), KStandardGuiItem::cancel());
connect(buttonBox, &QDialogButtonBox::accepted, this, &CertifyCertificateDialog::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::close);
buttonBox->button(QDialogButtonBox::Ok)->setText(i18n("Certify"));
connect(buttonBox->button(QDialogButtonBox::Ok), &QAbstractButton::clicked,
this, [this] () {
KConfigGroup conf(KSharedConfig::openConfig(), "CertifySettings");
const auto lastKey = mCertWidget->secKey();
// Do not accept if the keys are the same.
if (!lastKey.isNull() && !mCertWidget->target().isNull() &&
!strcmp(lastKey.primaryFingerprint(),
mCertWidget->target().primaryFingerprint())) {
KMessageBox::error(this, i18n("You cannot certify using the same key."),
i18n("Invalid Selection"), KMessageBox::Notify);
return;
}
if (!lastKey.isNull()) {
conf.writeEntry("LastKey", lastKey.primaryFingerprint());
}
conf.writeEntry("ExportCheckState", mCertWidget->exportableSelected());
conf.writeEntry("PublishCheckState", mCertWidget->publishSelected());
accept();
});
connect(buttonBox->button(QDialogButtonBox::Cancel), &QAbstractButton::clicked,
this, [this] () {
close();
mainLay->addWidget(buttonBox);
okButton->setEnabled(mCertWidget->isValid());
connect(mCertWidget, &CertifyWidget::changed, this, [this, okButton] () {
okButton->setEnabled(mCertWidget->isValid());
});
mainLay->addWidget(buttonBox);
KConfigGroup cfgGroup(KSharedConfig::openStateConfig(), "CertifyDialog");
const QByteArray geom = cfgGroup.readEntry("geometry", QByteArray());
if (!geom.isEmpty()) {
......@@ -148,3 +132,20 @@ QString CertifyCertificateDialog::tags() const
{
return mCertWidget->tags();
}
void CertifyCertificateDialog::accept()
{
if (!mCertWidget->isValid()) {
return;
}
KConfigGroup conf(KSharedConfig::openConfig(), "CertifySettings");
const auto lastKey = mCertWidget->secKey();
if (!lastKey.isNull()) {
conf.writeEntry("LastKey", lastKey.primaryFingerprint());
}
conf.writeEntry("ExportCheckState", mCertWidget->exportableSelected());
conf.writeEntry("PublishCheckState", mCertWidget->publishSelected());
QDialog::accept();
}
......@@ -48,6 +48,9 @@ public:
QString tags() const;
public Q_SLOTS:
void accept() override;
private:
CertifyWidget *mCertWidget;
};
......
......@@ -18,6 +18,7 @@
#include <Libkleo/KeySelectionCombo>
#include <Libkleo/Formatting>
#include <Libkleo/KeyCache>
#include <Libkleo/Predicates>
#include <QCheckBox>
#include <QHBoxLayout>
......@@ -206,15 +207,14 @@ static bool uidEqual(const GpgME::UserID &lhs, const GpgME::UserID &rhs)
&& qstrcmp(lhs.id(), rhs.id()) == 0;
}
} // anonymous namespace
}
// Use of pimpl as this might be moved to libkleo
class CertifyWidget::Private
{
public:
Private(CertifyWidget *qq) : q(qq),
mFprLabel(new QLabel)
Private(CertifyWidget *qq)
: q{qq}
, mFprLabel{new QLabel{q}}
{
auto mainLay = new QVBoxLayout(q);
mainLay->addWidget(mFprLabel);
......@@ -245,8 +245,8 @@ public:
auto advLay = new QVBoxLayout;
mExportCB = new QCheckBox(i18n("Certify for everyone to see. (exportable)"));
mPublishCB = new QCheckBox(i18n("Publish on keyserver afterwards."));
mExportCB = new QCheckBox(i18n("Certify for everyone to see (exportable)"));
mPublishCB = new QCheckBox(i18n("Publish on keyserver afterwards"));
auto publishLay = new QHBoxLayout;
publishLay->addSpacing(20);
publishLay->addWidget(mPublishCB);
......@@ -258,7 +258,7 @@ public:
infoBtn->setIcon(QIcon::fromTheme(QStringLiteral("help-contextual")));
infoBtn->setFlat(true);
connect(infoBtn, &QPushButton::clicked, q, [this, infoBtn] () {
connect(infoBtn, &QPushButton::clicked, q, [infoBtn] () {
const QString msg = i18n("You can use this to add additional info to a "
"certification.") + QStringLiteral("<br/><br/>") +
i18n("Tags created by anyone with full certification trust "
......@@ -285,6 +285,8 @@ public:
mPublishCB->setEnabled(false);
connect(&mUserIDModel, &QStandardItemModel::itemChanged, q, &CertifyWidget::changed);
connect(mExportCB, &QCheckBox::toggled, [this] (bool on) {
mPublishCB->setEnabled(on);
});
......@@ -293,14 +295,13 @@ public:
#ifdef GPGME_HAS_REMARKS
updateTags();
#endif
Q_EMIT q->changed();
});
loadConfig();
}
~Private()
{
}
~Private() = default;
void loadConfig()
{
......@@ -403,6 +404,23 @@ public:
return mTarget;
}
bool isValid() const
{
// do not accept null keys
if (mTarget.isNull() || mSecKeySelect->currentKey().isNull()) {
return false;
}
// do not accept empty list of user ids
if (selectedUserIDs().empty()) {
return false;
}
// do not accept if the keys are the same
if (_detail::ByFingerprint<std::equal_to>()(mTarget, mSecKeySelect->currentKey())) {
return false;
}
return true;
}
private:
CertifyWidget *const q;
QLabel *mFprLabel = nullptr;
......@@ -463,5 +481,10 @@ bool CertifyWidget::publishSelected() const
return d->publishSelected();
}
bool CertifyWidget::isValid() const
{
return d->isValid();
}
// For UserID model
#include "certifywidget.moc"
......@@ -53,6 +53,11 @@ public:
/* Should the signed key be be published */
bool publishSelected() const;
bool isValid() const;
Q_SIGNALS:
void changed() const;
private:
class Private;
std::unique_ptr<Private> d;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment