diff --git a/src/ui/keyselectioncombo.cpp b/src/ui/keyselectioncombo.cpp index 3974e1d29..c2d09ef04 100644 --- a/src/ui/keyselectioncombo.cpp +++ b/src/ui/keyselectioncombo.cpp @@ -1,543 +1,556 @@ /* This file is part of Kleopatra, the KDE keymanager Copyright (c) 2016 Klarälvdalens Datakonsult AB Kleopatra is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Kleopatra is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "keyselectioncombo.h" #include #include "kleo/dn.h" #include "models/keylistmodel.h" #include "models/keylistsortfilterproxymodel.h" #include "models/keycache.h" #include "utils/formatting.h" #include "progressbar.h" #include "kleo/defaultkeyfilter.h" #include #include #include #include #include Q_DECLARE_METATYPE(GpgME::Key) namespace { class ProxyModel : public QSortFilterProxyModel { Q_OBJECT private: struct CustomItem { QIcon icon; QString text; QVariant data; + QString toolTip; }; public: ProxyModel(QObject *parent = nullptr) : QSortFilterProxyModel(parent) { } ~ProxyModel() override { qDeleteAll(mFrontItems); qDeleteAll(mBackItems); } bool lessThan(const QModelIndex &left, const QModelIndex &right) const override { const auto leftKey = sourceModel()->data(left, Kleo::KeyListModelInterface::KeyRole).value(); const auto rightKey = sourceModel()->data(right, Kleo::KeyListModelInterface::KeyRole).value(); if (leftKey.isNull()) { return false; } if (rightKey.isNull()) { return true; } // As we display UID(0) this is ok. We probably need a get Best UID at some point. const auto lUid = leftKey.userID(0); const auto rUid = rightKey.userID(0); if (lUid.isNull()) { return false; } if (rUid.isNull()) { return true; } int cmp = strcmp (lUid.id(), rUid.id()); if (cmp) { return cmp < 0; } if (lUid.validity() == rUid.validity()) { /* Both are the same check which one is newer. */ time_t oldTime = 0; for (const GpgME::Subkey &s: leftKey.subkeys()) { if (s.isRevoked() || s.isInvalid() || s.isDisabled()) { continue; } if (s.creationTime() > oldTime) { oldTime= s.creationTime(); } } time_t newTime = 0; for (const GpgME::Subkey &s: rightKey.subkeys()) { if (s.isRevoked() || s.isInvalid() || s.isDisabled()) { continue; } if (s.creationTime() > newTime) { newTime = s.creationTime(); } } return newTime < oldTime; } return lUid.validity() > rUid.validity(); } bool isCustomItem(const int row) const { return row < mFrontItems.count() || row >= mFrontItems.count() + QSortFilterProxyModel::rowCount(); } - void prependItem(const QIcon &icon, const QString &text, const QVariant &data) + void prependItem(const QIcon &icon, const QString &text, const QVariant &data, const QString &toolTip) { beginInsertRows(QModelIndex(), 0, 0); - mFrontItems.push_front(new CustomItem{ icon, text, data }); + mFrontItems.push_front(new CustomItem{ icon, text, data, toolTip }); endInsertRows(); } - void appendItem(const QIcon &icon, const QString &text, const QVariant &data) + void appendItem(const QIcon &icon, const QString &text, const QVariant &data, const QString &toolTip) { beginInsertRows(QModelIndex(), rowCount(), rowCount()); - mBackItems.push_back(new CustomItem{ icon, text, data }); + mBackItems.push_back(new CustomItem{ icon, text, data, toolTip }); endInsertRows(); } void removeCustomItem(const QVariant &data) { for (int i = 0; i < mFrontItems.count(); ++i) { if (mFrontItems[i]->data == data) { beginRemoveRows(QModelIndex(), i, i); delete mFrontItems.takeAt(i); endRemoveRows(); return; } } for (int i = 0; i < mBackItems.count(); ++i) { if (mBackItems[i]->data == data) { const int index = mFrontItems.count() + QSortFilterProxyModel::rowCount() + i; beginRemoveRows(QModelIndex(), index, index); delete mBackItems.takeAt(i); endRemoveRows(); return; } } } int rowCount(const QModelIndex &parent = QModelIndex()) const override { return mFrontItems.count() + QSortFilterProxyModel::rowCount(parent) + mBackItems.count(); } QModelIndex mapToSource(const QModelIndex &index) const override { if (!isCustomItem(index.row())) { const int row = index.row() - mFrontItems.count(); return sourceModel()->index(row, index.column()); } else { return QModelIndex(); } } QModelIndex mapFromSource(const QModelIndex &source_index) const override { const QModelIndex idx = QSortFilterProxyModel::mapFromSource(source_index); return createIndex(mFrontItems.count() + idx.row(), idx.column(), idx.internalPointer()); } QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override { if (row < 0 || row >= rowCount()) { return QModelIndex(); } if (row < mFrontItems.count()) { return createIndex(row, column, mFrontItems[row]); } else if (row >= mFrontItems.count() + QSortFilterProxyModel::rowCount()) { return createIndex(row, column, mBackItems[row - mFrontItems.count() - QSortFilterProxyModel::rowCount()]); } else { const QModelIndex mi = QSortFilterProxyModel::index(row - mFrontItems.count(), column, parent); return createIndex(row, column, mi.internalPointer()); } } Qt::ItemFlags flags(const QModelIndex &index) const override { Q_UNUSED(index); return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemNeverHasChildren; } QModelIndex parent(const QModelIndex &) const override { // Flat list return QModelIndex(); } QVariant data(const QModelIndex &index, int role) const override { if (!index.isValid()) { return QVariant(); } if (isCustomItem(index.row())) { Q_ASSERT(!mFrontItems.isEmpty() || !mBackItems.isEmpty()); CustomItem *ci = static_cast(index.internalPointer()); switch (role) { case Qt::DisplayRole: return ci->text; case Qt::DecorationRole: return ci->icon; case Qt::UserRole: return ci->data; + case Qt::ToolTipRole: + return ci->toolTip; default: return QVariant(); } } const auto key = QSortFilterProxyModel::data(index, Kleo::KeyListModelInterface::KeyRole).value(); Q_ASSERT(!key.isNull()); if (key.isNull()) { return QVariant(); } switch (role) { case Qt::DisplayRole: { const auto userID = key.userID(0); QString name, email; if (key.protocol() == GpgME::OpenPGP) { name = QString::fromUtf8(userID.name()); email = QString::fromUtf8(userID.email()); } else { const Kleo::DN dn(userID.id()); name = dn[QStringLiteral("CN")]; email = dn[QStringLiteral("EMAIL")]; } return i18nc("Name (validity, type, created: date)", "%1 (%2, %3 created: %4)", email.isEmpty() ? name : name.isEmpty() ? email : i18nc("Name ", "%1 <%2>", name, email), Kleo::Formatting::complianceStringShort(key), Kleo::KeyCache::instance()->pgpOnly() ? QString() : key.protocol() == GpgME::OpenPGP ? i18n("OpenPGP") + QLatin1Char(',') : i18n("S/MIME") + QLatin1Char(','), Kleo::Formatting::creationDateString(key)); } case Qt::ToolTipRole: return Kleo::Formatting::toolTip(key, Kleo::Formatting::Validity | Kleo::Formatting::Issuer | Kleo::Formatting::Subject | Kleo::Formatting::Fingerprint | Kleo::Formatting::ExpiryDates | Kleo::Formatting::UserIDs); case Qt::DecorationRole: return Kleo::Formatting::iconForUid(key.userID(0)); default: return QSortFilterProxyModel::data(index, role); } } private: QVector mFrontItems; QVector mBackItems; }; } // anonymous namespace namespace Kleo { class KeySelectionComboPrivate { public: KeySelectionComboPrivate(KeySelectionCombo *parent) : wasEnabled(true) , q(parent) { } /* Selects the first key with a UID addrSpec that matches * the mPerfectMatchMbox variable. * * The idea here is that if there are keys like: * * tom-store@abc.com * susi-store@abc.com * store@abc.com * * And the user wants to send a mail to "store@abc.com" * the filter should still show tom and susi (because they * both are part of store) but the key for "store" should * be preselected. * * Returns true if one was selected. False otherwise. */ bool selectPerfectIdMatch() const { if (mPerfectMatchMbox.isEmpty()) { return false; } for (int i = 0; i < proxyModel->rowCount(); ++i) { const auto idx = proxyModel->index(i, 0, QModelIndex()); const auto key = proxyModel->data(idx, Kleo::KeyListModelInterface::KeyRole).value(); if (key.isNull()) { // WTF? continue; } for (const auto &uid: key.userIDs()) { if (QString::fromStdString(uid.addrSpec()) == mPerfectMatchMbox) { q->setCurrentIndex(i); return true; } } } return false; } /* Updates the current key with the default key if the key matches * the current key filter. */ void updateWithDefaultKey() const { GpgME::Protocol filterProto = GpgME::UnknownProtocol; const auto filter = dynamic_cast (sortFilterProxy->keyFilter().get()); if (filter && filter->isOpenPGP() == DefaultKeyFilter::Set) { filterProto = GpgME::OpenPGP; } else if (filter && filter->isOpenPGP() == DefaultKeyFilter::NotSet) { filterProto = GpgME::CMS; } QString defaultKey = defaultKeys.value (filterProto); if (defaultKey.isEmpty()) { // Fallback to unknown protocol defaultKey = defaultKeys.value (GpgME::UnknownProtocol); } q->setCurrentKey(defaultKey); } Kleo::AbstractKeyListModel *model = nullptr; Kleo::KeyListSortFilterProxyModel *sortFilterProxy = nullptr; ProxyModel *proxyModel = nullptr; std::shared_ptr cache; QMap defaultKeys; bool wasEnabled = false; bool useWasEnabled = false; bool secretOnly; QString mPerfectMatchMbox; private: KeySelectionCombo * const q; }; } using namespace Kleo; KeySelectionCombo::KeySelectionCombo(QWidget* parent) : KeySelectionCombo(true, parent) {} KeySelectionCombo::KeySelectionCombo(bool secretOnly, QWidget* parent) : QComboBox(parent) , d(new KeySelectionComboPrivate(this)) { d->model = Kleo::AbstractKeyListModel::createFlatKeyListModel(this); d->secretOnly = secretOnly; d->sortFilterProxy = new Kleo::KeyListSortFilterProxyModel(this); d->sortFilterProxy->setSourceModel(d->model); d->proxyModel = new ProxyModel(this); d->proxyModel->setSourceModel(d->sortFilterProxy); setModel(d->proxyModel); connect(this, static_cast(&KeySelectionCombo::currentIndexChanged), this, [this](int row) { if (row >= 0 && row < d->proxyModel->rowCount()) { if (d->proxyModel->isCustomItem(row)) { Q_EMIT customItemSelected(d->proxyModel->index(row, 0).data(Qt::UserRole)); } else { Q_EMIT currentKeyChanged(currentKey()); } } }); d->cache = Kleo::KeyCache::mutableInstance(); QTimer::singleShot(0, this, &KeySelectionCombo::init); } KeySelectionCombo::~KeySelectionCombo() { delete d; } void KeySelectionCombo::init() { connect(d->cache.get(), &Kleo::KeyCache::keyListingDone, this, [this]() { // Set useKeyCache ensures that the cache is populated // so this can be a blocking call if the cache is not initialized d->model->useKeyCache(true, d->secretOnly); d->proxyModel->removeCustomItem(QStringLiteral("-libkleo-loading-keys")); // We use the useWasEnabled state variable to decide if we should // change the enable / disable state based on the keylist done signal. // If we triggered the refresh useWasEnabled is true and we want to // enable / disable again after our refresh, as the refresh disabled it. // // But if a keyListingDone signal comes from just a generic refresh // triggered by someone else we don't want to change the enable / disable // state. if (d->useWasEnabled) { setEnabled(d->wasEnabled); d->useWasEnabled = false; } Q_EMIT keyListingFinished(); }); connect(this, &KeySelectionCombo::keyListingFinished, this, [this]() { d->updateWithDefaultKey(); }); if (!d->cache->initialized()) { refreshKeys(); } else { d->model->useKeyCache(true, d->secretOnly); Q_EMIT keyListingFinished(); } connect(this, QOverload::of(&QComboBox::currentIndexChanged), this, [this] () { setToolTip(currentData(Qt::ToolTipRole).toString()); }); } void KeySelectionCombo::setKeyFilter(const std::shared_ptr &kf) { d->sortFilterProxy->setKeyFilter(kf); d->proxyModel->sort(0); d->updateWithDefaultKey(); } std::shared_ptr KeySelectionCombo::keyFilter() const { return d->sortFilterProxy->keyFilter(); } void KeySelectionCombo::setIdFilter(const QString &id) { d->sortFilterProxy->setFilterRegExp(id); d->mPerfectMatchMbox = id; d->updateWithDefaultKey(); } QString KeySelectionCombo::idFilter() const { return d->sortFilterProxy->filterRegExp().pattern(); } GpgME::Key Kleo::KeySelectionCombo::currentKey() const { return currentData(Kleo::KeyListModelInterface::KeyRole).value(); } void Kleo::KeySelectionCombo::setCurrentKey(const GpgME::Key &key) { const int idx = findData(QVariant::fromValue(key), Kleo::KeyListModelInterface::KeyRole, Qt::MatchExactly); if (idx > -1) { setCurrentIndex(idx); } else { d->selectPerfectIdMatch(); } setToolTip(currentData(Qt::ToolTipRole).toString()); } void Kleo::KeySelectionCombo::setCurrentKey(const QString &fingerprint) { const auto cur = currentKey(); if (!cur.isNull() && !fingerprint.isEmpty() && fingerprint == QLatin1String(cur.primaryFingerprint())) { // Already set return; } for (int i = 0; i < d->proxyModel->rowCount(); ++i) { const auto idx = d->proxyModel->index(i, 0, QModelIndex()); const auto key = d->proxyModel->data(idx, Kleo::KeyListModelInterface::KeyRole).value(); if (!key.isNull() && fingerprint == QString::fromLatin1(key.primaryFingerprint())) { setCurrentIndex(i); setToolTip(currentData(Qt::ToolTipRole).toString()); return; } } if (!d->selectPerfectIdMatch()) { setCurrentIndex(0); } setToolTip(currentData(Qt::ToolTipRole).toString()); } void KeySelectionCombo::refreshKeys() { d->wasEnabled = isEnabled(); d->useWasEnabled = true; setEnabled(false); const bool wasBlocked = blockSignals(true); prependCustomItem(QIcon(), i18n("Loading keys ..."), QStringLiteral("-libkleo-loading-keys")); setCurrentIndex(0); blockSignals(wasBlocked); d->cache->startKeyListing(); } +void KeySelectionCombo::appendCustomItem(const QIcon &icon, const QString &text, const QVariant &data, const QString &toolTip) +{ + d->proxyModel->appendItem(icon, text, data, toolTip); +} + void KeySelectionCombo::appendCustomItem(const QIcon &icon, const QString &text, const QVariant &data) { - d->proxyModel->appendItem(icon, text, data); + appendCustomItem(icon, text, data, QString()); +} + +void KeySelectionCombo::prependCustomItem(const QIcon &icon, const QString &text, const QVariant &data, const QString &toolTip) +{ + d->proxyModel->prependItem(icon, text, data, toolTip); } void KeySelectionCombo::prependCustomItem(const QIcon &icon, const QString &text, const QVariant &data) { - d->proxyModel->prependItem(icon, text, data); + prependCustomItem(icon, text, data, QString()); } void Kleo::KeySelectionCombo::setDefaultKey(const QString &fingerprint, GpgME::Protocol proto) { d->defaultKeys.insert(proto, fingerprint); d->updateWithDefaultKey(); } void Kleo::KeySelectionCombo::setDefaultKey(const QString &fingerprint) { setDefaultKey(fingerprint, GpgME::UnknownProtocol); } QString Kleo::KeySelectionCombo::defaultKey(GpgME::Protocol proto) const { return d->defaultKeys.value(proto); } QString Kleo::KeySelectionCombo::defaultKey() const { return defaultKey(GpgME::UnknownProtocol); } #include "keyselectioncombo.moc" diff --git a/src/ui/keyselectioncombo.h b/src/ui/keyselectioncombo.h index 3f3d8ab2f..e298f6db5 100644 --- a/src/ui/keyselectioncombo.h +++ b/src/ui/keyselectioncombo.h @@ -1,83 +1,85 @@ /* This file is part of Kleopatra, the KDE keymanager Copyright (c) 2016 Klarälvdalens Datakonsult AB Kleopatra is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Kleopatra is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef KLEO_KEYSELECTIONCOMBO_H #define KLEO_KEYSELECTIONCOMBO_H #include #include #include #include #include namespace GpgME { class Key; } namespace Kleo { class KeyFilter; class KeySelectionComboPrivate; class KLEO_EXPORT KeySelectionCombo : public QComboBox { Q_OBJECT public: explicit KeySelectionCombo(QWidget *parent = nullptr); explicit KeySelectionCombo(bool secretOnly, QWidget *parent = nullptr); ~KeySelectionCombo() override; void setKeyFilter(const std::shared_ptr &kf); std::shared_ptr keyFilter() const; void setIdFilter(const QString &id); QString idFilter() const; void refreshKeys(); GpgME::Key currentKey() const; void setCurrentKey(const GpgME::Key &key); void setCurrentKey(const QString &fingerprint); void setDefaultKey(const QString &fingerprint); void setDefaultKey(const QString &fingerprint, GpgME::Protocol proto); QString defaultKey() const; QString defaultKey(GpgME::Protocol proto) const; void prependCustomItem(const QIcon &icon, const QString &text, const QVariant &data); void appendCustomItem(const QIcon &icon, const QString &text, const QVariant &data); + void prependCustomItem(const QIcon &icon, const QString &text, const QVariant &data, const QString &toolTip); + void appendCustomItem(const QIcon &icon, const QString &text, const QVariant &data, const QString &toolTip); Q_SIGNALS: void customItemSelected(const QVariant &data); void currentKeyChanged(const GpgME::Key &key); void keyListingFinished(); protected: virtual void init(); private: KeySelectionComboPrivate * const d; }; } #endif diff --git a/src/ui/newkeyapprovaldialog.cpp b/src/ui/newkeyapprovaldialog.cpp index 27497438e..999b823a9 100644 --- a/src/ui/newkeyapprovaldialog.cpp +++ b/src/ui/newkeyapprovaldialog.cpp @@ -1,719 +1,735 @@ /* -*- c++ -*- newkeyapprovaldialog.cpp This file is part of libkleopatra, the KDE keymanagement library Copyright (c) 2018 Intevation GmbH Libkleopatra is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Libkleopatra is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, the copyright holders give permission to link the code of this program with any edition of the Qt library by Trolltech AS, Norway (or with modified versions of Qt that use the same license as Qt), and distribute linked combinations including the two. You must obey the GNU General Public License in all respects for all of the code used other than Qt. If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #include "newkeyapprovaldialog.h" #include "kleo/defaultkeyfilter.h" #include "keyselectioncombo.h" #include "progressdialog.h" #include "utils/formatting.h" #include "libkleo_debug.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace Kleo; namespace { class OpenPGPFilter: public DefaultKeyFilter { public: OpenPGPFilter() : DefaultKeyFilter() { setIsOpenPGP(DefaultKeyFilter::Set); setCanEncrypt(DefaultKeyFilter::Set); } }; static std::shared_ptr s_pgpFilter = std::shared_ptr (new OpenPGPFilter); class OpenPGPSignFilter: public DefaultKeyFilter { public: OpenPGPSignFilter() : DefaultKeyFilter() { /* Also list unusable keys to make it transparent why they are unusable */ setDisabled(DefaultKeyFilter::NotSet); setRevoked(DefaultKeyFilter::NotSet); setExpired(DefaultKeyFilter::NotSet); setCanSign(DefaultKeyFilter::Set); setHasSecret(DefaultKeyFilter::Set); setIsOpenPGP(DefaultKeyFilter::Set); } }; static std::shared_ptr s_pgpSignFilter = std::shared_ptr (new OpenPGPSignFilter); class SMIMEFilter: public DefaultKeyFilter { public: SMIMEFilter(): DefaultKeyFilter() { setIsOpenPGP(DefaultKeyFilter::NotSet); setCanEncrypt(DefaultKeyFilter::Set); } }; static std::shared_ptr s_smimeFilter = std::shared_ptr (new SMIMEFilter); class SMIMESignFilter: public DefaultKeyFilter { public: SMIMESignFilter(): DefaultKeyFilter() { setDisabled(DefaultKeyFilter::NotSet); setRevoked(DefaultKeyFilter::NotSet); setExpired(DefaultKeyFilter::NotSet); setCanSign(DefaultKeyFilter::Set); setIsOpenPGP(DefaultKeyFilter::NotSet); setHasSecret(DefaultKeyFilter::Set); } }; static std::shared_ptr s_smimeSignFilter = std::shared_ptr (new SMIMESignFilter); static std::shared_ptr s_defaultFilter= std::shared_ptr (new DefaultKeyFilter); class SignFilter: public DefaultKeyFilter { public: SignFilter(): DefaultKeyFilter() { setHasSecret(DefaultKeyFilter::Set); } }; static std::shared_ptr s_signFilter = std::shared_ptr (new SignFilter); /* Some decoration and a button to remove the filter for a keyselectioncombo */ class ComboWidget: public QWidget { Q_OBJECT public: explicit ComboWidget(KeySelectionCombo *combo): mCombo(combo), mFilterBtn(new QPushButton), mFromOverride(GpgME::UnknownProtocol) { auto hLay = new QHBoxLayout(this); hLay->addWidget(combo, 1); hLay->addWidget(mFilterBtn, 0); // Assume that combos start out with a filter mFilterBtn->setIcon(QIcon::fromTheme(QStringLiteral("kt-remove-filters"))); mFilterBtn->setToolTip(i18n("Remove Filter")); // FIXME: This is ugly to enforce but otherwise the // icon is broken. combo->setMinimumHeight(22); mFilterBtn->setMinimumHeight(23); connect(mFilterBtn, &QPushButton::clicked, this, [this] () { const QString curFilter = mCombo->idFilter(); if (curFilter.isEmpty()) { mCombo->setIdFilter(mLastIdFilter); mLastIdFilter = QString(); mFilterBtn->setIcon(QIcon::fromTheme(QStringLiteral("kt-remove-filters"))); mFilterBtn->setToolTip(i18n("Remove Filter")); } else { mLastIdFilter = curFilter; mFilterBtn->setIcon(QIcon::fromTheme(QStringLiteral("kt-add-filters"))); mFilterBtn->setToolTip(i18n("Add Filter")); mCombo->setIdFilter(QString()); } }); } KeySelectionCombo *combo() { return mCombo; } GpgME::Protocol fromOverride() const { return mFromOverride; } void setFromOverride(GpgME::Protocol proto) { mFromOverride = proto; } private: KeySelectionCombo *mCombo; QPushButton *mFilterBtn; QString mLastIdFilter; GpgME::Protocol mFromOverride; }; static enum GpgME::UserID::Validity keyValidity(const GpgME::Key &key) { enum GpgME::UserID::Validity validity = GpgME::UserID::Validity::Unknown; for (const auto &uid: key.userIDs()) { if (validity == GpgME::UserID::Validity::Unknown || validity > uid.validity()) { validity = uid.validity(); } } return validity; } static bool key_has_addr(const GpgME::Key &key, const QString &addr) { for (const auto &uid: key.userIDs()) { if (QString::fromStdString(uid.addrSpec()).toLower() == addr.toLower()) { return true; } } return false; } } // namespace class NewKeyApprovalDialog::Private { private: enum Action { Unset, GenerateKey, IgnoreKey, }; public: Private(NewKeyApprovalDialog *pub, GpgME::Protocol forcedProtocol, GpgME::Protocol presetProtocol, const QString &sender, bool allowMixed): mProto(forcedProtocol), mSender(sender), mAllowMixed(allowMixed), q(pub) { + // We do the translation here to avoid having the same string multiple times. + mGenerateTooltip = i18nc("@info:tooltip for a 'Generate new key pair' action " + "in a combobox when a user does not yet have an OpenPGP or S/MIME key.", + "Generate a new key using your E-Mail address.

" + "The key is necessary to decrypt and sign E-Mails. " + "You will be asked for a passphrase to protect this key and the protected key " + "will be stored in your home directory."); mMainLay = new QVBoxLayout; QDialogButtonBox *btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); mOkButton = btnBox->button(QDialogButtonBox::Ok); QObject::connect (btnBox, &QDialogButtonBox::accepted, q, [this] () { accepted(); }); QObject::connect (btnBox, &QDialogButtonBox::rejected, q, &QDialog::reject); mScrollArea = new QScrollArea; mScrollArea->setWidget(new QWidget); mScrollLayout = new QVBoxLayout; mScrollArea->widget()->setLayout(mScrollLayout); mScrollArea->setWidgetResizable(true); mScrollArea->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContentsOnFirstShow); mScrollArea->setFrameStyle(QFrame::NoFrame); mScrollLayout->setContentsMargins(0, 0, 0, 0); q->setWindowTitle(i18n("Security approval")); auto fmtLayout = new QHBoxLayout; mFormatBtns = new QButtonGroup; auto pgpBtn = new QRadioButton(i18n("OpenPGP")); auto smimeBtn = new QRadioButton(i18n("S/MIME")); mFormatBtns->addButton(pgpBtn, 1); mFormatBtns->addButton(smimeBtn, 2); mFormatBtns->setExclusive(true); fmtLayout->addStretch(-1); fmtLayout->addWidget(pgpBtn); fmtLayout->addWidget(smimeBtn); mMainLay->addLayout(fmtLayout); // Handle force / preset if (forcedProtocol == GpgME::OpenPGP) { pgpBtn->setChecked(true); pgpBtn->setVisible(false); smimeBtn->setVisible(false); } else if (forcedProtocol == GpgME::CMS) { smimeBtn->setChecked(true); pgpBtn->setVisible(false); smimeBtn->setVisible(false); } else if (presetProtocol == GpgME::CMS) { smimeBtn->setChecked(true); } else if (!mAllowMixed) { pgpBtn->setChecked(true); } else if (mAllowMixed) { smimeBtn->setVisible(false); pgpBtn->setVisible(false); } updateFilter(); QObject::connect (mFormatBtns, static_cast (&QButtonGroup::buttonToggled), q, [this](int, bool) { updateFilter(); }); mMainLay->addWidget(mScrollArea); mComplianceLbl = new QLabel; mComplianceLbl->setVisible(false); auto btnLayout = new QHBoxLayout; btnLayout->addWidget(mComplianceLbl); btnLayout->addWidget(btnBox); mMainLay->addLayout(btnLayout); q->setLayout(mMainLay); } void generateKey(KeySelectionCombo *combo) { const auto &addr = combo->property("address").toString(); auto job = new QGpgME::DefaultKeyGenerationJob(q); auto progress = new Kleo::ProgressDialog(job, i18n("Generating key for '%1'...", addr) + QStringLiteral("\n\n") + i18n("This can take several minutes."), q); progress->setWindowFlags(progress->windowFlags() & ~Qt::WindowContextHelpButtonHint); progress->setWindowTitle(i18n("Key generation")); progress->setModal(true); progress->setAutoClose(true); progress->setMinimumDuration(0); progress->setValue(0); mRunningJobs << job; connect (job, &QGpgME::DefaultKeyGenerationJob::result, q, [this, job, combo] (const GpgME::KeyGenerationResult &result) { handleKeyGenResult(result, job, combo); }); job->start(addr, QString()); return; } void handleKeyGenResult(const GpgME::KeyGenerationResult &result, QGpgME::Job *job, KeySelectionCombo *combo) { mLastError = result.error(); if (!mLastError || mLastError.isCanceled()) { combo->setDefaultKey(QString::fromLatin1(result.fingerprint()), GpgME::OpenPGP); connect (combo, &KeySelectionCombo::keyListingFinished, q, [this, job] () { mRunningJobs.removeAll(job); }); combo->refreshKeys(); } else { mRunningJobs.removeAll(job); } } void checkAccepted() { if (mLastError || mLastError.isCanceled()) { KMessageBox::error(q, QString::fromLocal8Bit(mLastError.asString()), i18n("Operation Failed")); mRunningJobs.clear(); return; } if (!mRunningJobs.empty()) { return; } /* Save the keys */ bool isPGP = mFormatBtns->checkedId() == 1; bool isSMIME = mFormatBtns->checkedId() == 2; mAcceptedEnc.clear(); mAcceptedSig.clear(); for (const auto combo: mEncCombos) { const auto &addr = combo->property("address").toString(); const auto &key = combo->currentKey(); if (!combo->isVisible()) { continue; } if (isSMIME && key.protocol() != GpgME::CMS) { continue; } if (isPGP && key.protocol() != GpgME::OpenPGP) { continue; } if (mAcceptedEnc.contains(addr)) { mAcceptedEnc[addr].push_back(key); } else { std::vector vec; vec.push_back(key); mAcceptedEnc.insert(addr, vec); } } for (const auto combo: mSigningCombos) { const auto key = combo->currentKey(); if (!combo->isVisible()) { continue; } if (isSMIME && key.protocol() != GpgME::CMS) { continue; } if (isPGP && key.protocol() != GpgME::OpenPGP) { continue; } mAcceptedSig.push_back(combo->currentKey()); } q->accept(); } void accepted() { // We can assume everything was validly resolved, otherwise // the OK button would have been disabled. // Handle custom items now. for (auto combo: mAllCombos) { auto act = combo->currentData(Qt::UserRole).toInt(); if (act == GenerateKey) { generateKey(combo); // Only generate once return; } } checkAccepted(); } void updateFilter() { bool isPGP = mFormatBtns->checkedId() == 1; bool isSMIME = mFormatBtns->checkedId() == 2; if (isSMIME) { mCurEncFilter = s_smimeFilter; mCurSigFilter = s_smimeSignFilter; } else if (isPGP) { mCurEncFilter = s_pgpFilter; mCurSigFilter = s_pgpSignFilter; } else { mCurEncFilter = s_defaultFilter; mCurSigFilter = s_signFilter; } for (auto combo: mSigningCombos) { combo->setKeyFilter(mCurSigFilter); auto widget = qobject_cast (combo->parentWidget()); if (!widget) { qCDebug(LIBKLEO_LOG) << "Failed to find signature combo widget"; continue; } widget->setVisible(widget->fromOverride() == GpgME::UnknownProtocol || ((isSMIME && widget->fromOverride() == GpgME::CMS) || (isPGP && widget->fromOverride() == GpgME::OpenPGP))); } for (auto combo: mEncCombos) { combo->setKeyFilter(mCurEncFilter); auto widget = qobject_cast (combo->parentWidget()); if (!widget) { qCDebug(LIBKLEO_LOG) << "Failed to find combo widget"; continue; } widget->setVisible(widget->fromOverride() == GpgME::UnknownProtocol || ((isSMIME && widget->fromOverride() == GpgME::CMS) || (isPGP && widget->fromOverride() == GpgME::OpenPGP))); } } ComboWidget *createSigningCombo(const QString &addr, const GpgME::Key &key) { auto combo = new KeySelectionCombo(); combo->setKeyFilter(mCurSigFilter); if (!key.isNull()) { combo->setDefaultKey(QString::fromLatin1(key.primaryFingerprint()), key.protocol()); } if (key.isNull() && mProto != GpgME::CMS) { combo->appendCustomItem(QIcon::fromTheme(QStringLiteral("document-new")), - i18n("Generate a new key pair"), GenerateKey); + i18n("Generate a new key pair"), GenerateKey, + mGenerateTooltip); } combo->appendCustomItem(QIcon::fromTheme(QStringLiteral("emblem-unavailable")), - i18n("Don't confirm identity and integrity"), IgnoreKey); + i18n("Don't confirm identity and integrity"), IgnoreKey, + i18nc("@info:tooltip for not selecting a key for signing.", + "The E-Mail will not be cryptographically signed.")); mSigningCombos << combo; mAllCombos << combo; combo->setProperty("address", addr); connect(combo, &KeySelectionCombo::currentKeyChanged, q, [this] () { updateOkButton(); }); connect(combo, QOverload::of(&QComboBox::currentIndexChanged), q, [this] () { updateOkButton(); }); return new ComboWidget(combo); } void addSigningKeys(const QMap > &resolved, const QStringList &unresolved) { if (resolved.empty() && unresolved.empty()) { return; } for (const QString &addr: resolved.keys()) { auto group = new QGroupBox(i18nc("Caption for signing key selection", "Confirm identity '%1' as:", addr)); group->setAlignment(Qt::AlignLeft); mScrollLayout->addWidget(group); auto sigLayout = new QVBoxLayout; group->setLayout(sigLayout); for (const auto &key: resolved[addr]) { auto comboWidget = createSigningCombo(addr, key); if (key_has_addr (key, addr)) { comboWidget->combo()->setIdFilter(addr); } if (resolved[addr].size() > 1) { comboWidget->setFromOverride(key.protocol()); } sigLayout->addWidget(comboWidget); } } for (const QString &addr: unresolved) { auto group = new QGroupBox(i18nc("Caption for signing key selection, no key found", "No key found for the address '%1':", addr)); group->setAlignment(Qt::AlignLeft); mScrollLayout->addWidget(group); auto sigLayout = new QHBoxLayout; group->setLayout(sigLayout); auto comboWidget = createSigningCombo(addr, GpgME::Key()); comboWidget->combo()->setIdFilter(addr); sigLayout->addWidget(comboWidget); } } void addEncryptionAddr(const QString &addr, const std::vector &keys, QGridLayout *encGrid) { encGrid->addWidget(new QLabel(addr), encGrid->rowCount(), 0); for (const auto &key: keys) { auto combo = new KeySelectionCombo(false); combo->setKeyFilter(mCurEncFilter); if (!key.isNull()) { combo->setDefaultKey(QString::fromLatin1(key.primaryFingerprint()), key.protocol()); } if (mSender == addr && key.isNull()) { combo->appendCustomItem(QIcon::fromTheme(QStringLiteral("document-new")), - i18n("Generate a new key pair"), GenerateKey); + i18n("Generate a new key pair"), GenerateKey, + mGenerateTooltip); } combo->appendCustomItem(QIcon::fromTheme(QStringLiteral("emblem-unavailable")), - i18n("Ignore recipient"), IgnoreKey); + i18n("No Key"), IgnoreKey, + i18nc("@info:tooltip for No Key selected for a specific recipient.", + "Do not select a key for this recipient.

" + "The recipient will receive the encrypted E-Mail, but it can only " + "be decrypted with the other keys selected in this dialog.")); if (key.isNull() || key_has_addr (key, addr)) { combo->setIdFilter(addr); } connect(combo, &KeySelectionCombo::currentKeyChanged, q, [this] () { updateOkButton(); }); connect(combo, QOverload::of(&QComboBox::currentIndexChanged), q, [this] () { updateOkButton(); }); mEncCombos << combo; mAllCombos << combo; combo->setProperty("address", addr); auto comboWidget = new ComboWidget(combo); if (keys.size() > 1) { comboWidget->setFromOverride(key.protocol()); } encGrid->addWidget(comboWidget, encGrid->rowCount(), 0, 1, 2); } } void addEncryptionKeys(const QMap > &resolved, const QStringList &unresolved) { if (resolved.empty() && unresolved.empty()) { return; } auto group = new QGroupBox(i18n("Encrypt to:")); group->setAlignment(Qt::AlignLeft); auto encGrid = new QGridLayout; group->setLayout(encGrid); mScrollLayout->addWidget(group); for (const QString &addr: resolved.keys()) { addEncryptionAddr(addr, resolved[addr], encGrid); } std::vector dummy; dummy.push_back(GpgME::Key()); for (const QString &addr: unresolved) { addEncryptionAddr(addr, dummy, encGrid); } encGrid->setColumnStretch(1, -1); mScrollLayout->addStretch(-1); } void updateOkButton() { static QString origOkText = mOkButton->text(); bool isGenerate = false; bool isAllIgnored = true; // Check if generate is selected. for (auto combo: mAllCombos) { auto act = combo->currentData(Qt::UserRole).toInt(); if (act == GenerateKey) { mOkButton->setText(i18n("Generate")); isGenerate = true; } if (act != IgnoreKey) { isAllIgnored = false; } } mOkButton->setEnabled(!isAllIgnored); if (!isGenerate) { mOkButton->setText(origOkText); } if (Formatting::complianceMode() != QStringLiteral("de-vs")) { return; } // Handle compliance bool de_vs = true; for (const auto &key: q->signingKeys()) { if (!Formatting::isKeyDeVs(key) || keyValidity(key) < GpgME::UserID::Validity::Full) { de_vs = false; break; } } if (de_vs) { for (const auto &keys: q->encryptionKeys().values()) { for (const auto &key: keys) { if (!Formatting::isKeyDeVs(key) || keyValidity(key) < GpgME::UserID::Validity::Full) { de_vs = false; break; } } if (!de_vs) { break; } } } mOkButton->setIcon(QIcon::fromTheme(de_vs ? QStringLiteral("security-high") : QStringLiteral("security-medium"))); mOkButton->setStyleSheet(QStringLiteral("background-color: ") + (de_vs ? QStringLiteral("#D5FAE2") // KColorScheme(QPalette::Active, KColorScheme::View).background(KColorScheme::PositiveBackground).color().name() : QStringLiteral("#FAE9EB"))); //KColorScheme(QPalette::Active, KColorScheme::View).background(KColorScheme::NegativeBackground).color().name())); mComplianceLbl->setText(de_vs ? i18nc("VS-NfD-conforming is a German standard for restricted documents for which special restrictions about algorithms apply. The string states that all cryptographic operations necessary for the communication are compliant with that.", "VS-NfD-compliant communication possible.") : i18nc("VS-NfD-conforming is a German standard for restricted documents for which special restrictions about algorithms apply. The string states that all cryptographic operations necessary for the communication are compliant with that.", "VS-NfD-compliant communication not possible.")); mComplianceLbl->setVisible(true); } void selectionChanged() { bool isPGP = false; bool isCMS = false; for (const auto combo: mEncCombos) { isPGP |= combo->currentKey().protocol() == GpgME::OpenPGP; isCMS |= combo->currentKey().protocol() == GpgME::CMS; if (isPGP && isCMS) { break; } } } ~Private() {} GpgME::Protocol mProto; QList mSigningCombos; QList mEncCombos; QList mAllCombos; QScrollArea *mScrollArea; QVBoxLayout *mScrollLayout; QPushButton *mOkButton; QVBoxLayout *mMainLay; QButtonGroup *mFormatBtns; std::shared_ptr mCurSigFilter; std::shared_ptr mCurEncFilter; QString mSender; bool mAllowMixed; NewKeyApprovalDialog *q; QList mRunningJobs; GpgME::Error mLastError; QLabel *mComplianceLbl; QMap > mAcceptedEnc; std::vector mAcceptedSig; + QString mGenerateTooltip; }; NewKeyApprovalDialog::NewKeyApprovalDialog(const QMap > &resolvedSigningKeys, const QMap > &resolvedRecp, const QStringList &unresolvedSigKeys, const QStringList &unresolvedRecp, const QString &sender, bool allowMixed, GpgME::Protocol forcedProtocol, GpgME::Protocol presetProtocol, QWidget *parent, Qt::WindowFlags f): QDialog(parent, f), d(new Private(this, forcedProtocol, presetProtocol, sender, allowMixed)) { d->addSigningKeys(resolvedSigningKeys, unresolvedSigKeys); d->addEncryptionKeys(resolvedRecp, unresolvedRecp); d->updateFilter(); d->updateOkButton(); const auto size = sizeHint(); const auto desk = QApplication::desktop()->screenGeometry(this); resize(QSize(desk.width() / 3, qMin(size.height(), desk.height() / 2))); } std::vector NewKeyApprovalDialog::signingKeys() { return d->mAcceptedSig; } QMap > NewKeyApprovalDialog::encryptionKeys() { return d->mAcceptedEnc; } #include "newkeyapprovaldialog.moc"