diff --git a/src/runtime/kwalletd/knewwalletdialog.cpp b/src/runtime/kwalletd/knewwalletdialog.cpp index 7a20177..60b7791 100644 --- a/src/runtime/kwalletd/knewwalletdialog.cpp +++ b/src/runtime/kwalletd/knewwalletdialog.cpp @@ -1,187 +1,187 @@ /** * This file is part of the KDE project * Copyright (C) 2013 Valentin Rusu * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License version 2 as published by the Free Software Foundation. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "knewwalletdialog.h" #include "kwalletd_debug.h" #include #include #include #include #include #include #include #include #include #include #include Q_DECLARE_METATYPE(GpgME::Key) namespace KWallet { KNewWalletDialog::KNewWalletDialog(const QString &appName, const QString &walletName, QWidget *parent): QWizard(parent) { setOption(HaveFinishButtonOnEarlyPages); _intro = new KNewWalletDialogIntro(appName, walletName, this); _introId = addPage(_intro); _gpg = new KNewWalletDialogGpg(appName, walletName, this); _gpgId = addPage(_gpg); } bool KNewWalletDialog::isBlowfish() const { return _intro->isBlowfish(); } GpgME::Key KNewWalletDialog::gpgKey() const { QVariant varKey = field(QStringLiteral("key")); return varKey.value< GpgME::Key >(); } KNewWalletDialogIntro::KNewWalletDialogIntro(const QString &appName, const QString &walletName, QWidget *parent): QWizardPage(parent) { _ui.setupUi(this); if (appName.isEmpty()) { _ui.labelIntro->setText(i18n("KDE has requested to create a new wallet named '%1'. This is used to store sensitive data in a secure fashion. Please choose the new wallet's type below or click cancel to deny the application's request.", walletName.toHtmlEscaped())); } else { _ui.labelIntro->setText(i18n("The application '%1' has requested to create a new wallet named '%2'. This is used to store sensitive data in a secure fashion. Please choose the new wallet's type below or click cancel to deny the application's request.", appName.toHtmlEscaped(), walletName.toHtmlEscaped())); } } void KNewWalletDialogIntro::onBlowfishToggled(bool blowfish) { setFinalPage(blowfish); } bool KNewWalletDialogIntro::isBlowfish() const { return _ui.radioBlowfish->isChecked(); } int KNewWalletDialogIntro::nextId() const { if (isBlowfish()) { return -1; } else { return qobject_cast< const KNewWalletDialog * >(wizard())->gpgId(); } } KNewWalletDialogGpg::KNewWalletDialogGpg(const QString &appName, const QString &walletName, QWidget *parent): QWizardPage(parent), _alreadyInitialized(false), _complete(false) { _ui.setupUi(this); } struct AddKeyToList { QTableWidget *_list; int _row; AddKeyToList(QTableWidget *list) : _list(list), _row(0) {} void operator()(const GpgME::Key &k) { GpgME::UserID uid = k.userID(0); QString name(uid.name()); if (uid.comment()) { - name = QStringLiteral("%1 (%2)").arg(name).arg(uid.comment()); + name = QStringLiteral("%1 (%2)").arg(name, uid.comment()); } _list->setItem(_row, 0, new QTableWidgetItem(name)); _list->setItem(_row, 1, new QTableWidgetItem(uid.email())); _list->setItem(_row, 2, new QTableWidgetItem(k.shortKeyID())); QVariant varKey; varKey.setValue(k); _list->item(_row, 0)->setData(Qt::UserRole, varKey); ++_row; } }; void KNewWalletDialogGpg::initializePage() { if (_alreadyInitialized) { return; } registerField(QStringLiteral("key"), this); GpgME::initializeLibrary(); GpgME::Error err = GpgME::checkEngine(GpgME::OpenPGP); if (err) { qCDebug(KWALLETD_LOG) << "OpenPGP not supported on your system!"; KMessageBox::error(this, i18n("The GpgME library failed to initialize for the OpenPGP protocol. Please check your system's configuration then try again.")); emit completeChanged(); return; } std::shared_ptr< GpgME::Context > _ctx(GpgME::Context::createForProtocol(GpgME::OpenPGP)); if (!_ctx) { KMessageBox::error(this, i18n("The GpgME library failed to initialize for the OpenPGP protocol. Please check your system's configuration then try again.")); emit completeChanged(); return; } _ctx->setKeyListMode(GpgME::Local); std::vector< GpgME::Key > keys; err = _ctx->startKeyListing(); while (!err) { GpgME::Key k = _ctx->nextKey(err); if (err) { break; } if (!k.isInvalid() && k.canEncrypt() && (k.ownerTrust() == GpgME::Key::Ultimate)) { keys.push_back(k); } } _ctx->endKeyListing(); if (keys.size() == 0) { KMessageBox::error(this, i18n("Seems that your system has no keys suitable for encryption. Please set-up at least one encryption key, then try again.")); emit completeChanged(); return; } _ui.listCertificates->setRowCount(keys.size()); std::for_each(keys.begin(), keys.end(), AddKeyToList(_ui.listCertificates)); _ui.listCertificates->resizeColumnsToContents(); _ui.listCertificates->setCurrentCell(0, 0); _alreadyInitialized = true; } void KNewWalletDialogGpg::onItemSelectionChanged() { _complete = _ui.listCertificates->currentRow() >= 0; QVariant varKey = _ui.listCertificates->item(_ui.listCertificates->currentRow(), 0)->data(Qt::UserRole); setField(QStringLiteral("key"), varKey); emit completeChanged(); } bool KNewWalletDialogGpg::isComplete() const { return _complete; } bool KNewWalletDialogGpg::validateCurrentPage() { return false; } } // namespace #include "moc_knewwalletdialog.cpp" diff --git a/src/runtime/kwalletd/kwalletwizard.cpp b/src/runtime/kwalletd/kwalletwizard.cpp index 3b2a2ea..f2c3e63 100644 --- a/src/runtime/kwalletd/kwalletwizard.cpp +++ b/src/runtime/kwalletd/kwalletwizard.cpp @@ -1,323 +1,323 @@ /* This file is part of the KDE libraries Copyright (C) 2004 George Staikos This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include "kwalletwizard.h" #include "ui_kwalletwizardpageexplanation.h" #include "ui_kwalletwizardpageintro.h" #include "ui_kwalletwizardpageoptions.h" #include "ui_kwalletwizardpagepassword.h" #ifdef HAVE_GPGMEPP #include "ui_kwalletwizardpagepasswordgpg.h" #include "ui_kwalletwizardpagegpgkey.h" #endif #include #include #include #include #ifdef HAVE_GPGMEPP #include #include #include #include #include #include #endif class PageIntro : public QWizardPage { public: explicit PageIntro(QWidget *parent) : QWizardPage(parent) { ui.setupUi(this); ui.ktitlewidget->setText("

" + i18n("KWallet") + "

"); int iconSize = 3 * fontMetrics().height(); // round to multiple of 16 iconSize = (iconSize + 8) & ~15; QPixmap pix = KIconLoader::global()->loadIcon(QStringLiteral("kwalletmanager"), KIconLoader::Dialog, iconSize); ui.ktitlewidget->setPixmap(pix); bg = new QButtonGroup(this); bg->setExclusive(true); bg->addButton(ui._basic, 0); bg->addButton(ui._advanced, 1); // force the "basic" button to be selected ui._basic->setChecked(true); } QButtonGroup *bg; private: Ui::KWalletWizardPageIntro ui; }; class PagePassword : public QWizardPage { public: explicit PagePassword(QWidget *parent) : QWizardPage(parent) { ui.setupUi(this); registerField(QStringLiteral("useWallet"), ui._useWallet); registerField(QStringLiteral("pass1"), ui._pass1); registerField(QStringLiteral("pass2"), ui._pass2); #ifdef HAVE_GPGMEPP registerField(QStringLiteral("useGPG"), ui._radioGpg); registerField(QStringLiteral("useBlowfish"), ui._radioBlowfish); connect(ui._radioBlowfish, SIGNAL(toggled(bool)), parent, SLOT(passwordPageUpdate())); #endif connect(ui._useWallet, SIGNAL(clicked()), parent, SLOT(passwordPageUpdate())); connect(ui._pass1, SIGNAL(textChanged(QString)), parent, SLOT(passwordPageUpdate())); connect(ui._pass2, SIGNAL(textChanged(QString)), parent, SLOT(passwordPageUpdate())); ui._useWallet->setChecked(true); } int nextId() const override { #ifdef HAVE_GPGMEPP int nextId = -1; if (field(QStringLiteral("useWallet")).toBool()) { if (field(QStringLiteral("useBlowfish")).toBool()) { nextId = static_cast(wizard())->wizardType() == KWalletWizard::Basic ? -1 : KWalletWizard::PageOptionsId; // same as non GPGMEPP case } else { nextId = KWalletWizard::PageGpgKeyId; } } return nextId; #else return static_cast(wizard())->wizardType() == KWalletWizard::Basic ? -1 : KWalletWizard::PageOptionsId; #endif } void setMatchLabelText(const QString &text) { ui._matchLabel->setText(text); } private: #ifdef HAVE_GPGMEPP Ui::KWalletWizardPagePasswordGpg ui; #else Ui::KWalletWizardPagePassword ui; #endif }; #ifdef HAVE_GPGMEPP typedef std::vector< GpgME::Key > KeysVector; Q_DECLARE_METATYPE(GpgME::Key) struct AddKeyToCombo { QComboBox *_list; AddKeyToCombo(QComboBox *list) : _list(list) {} void operator()(const GpgME::Key &k) { - QString text = QStringLiteral("%1 (%2)").arg(k.shortKeyID()).arg(k.userID(0).email()); + QString text = QStringLiteral("%1 (%2)").arg(k.shortKeyID(), k.userID(0).email()); QVariant varKey; varKey.setValue(k); _list->addItem(text, varKey); } }; class PageGpgKey : public QWizardPage { public: explicit PageGpgKey(QWidget *parent) : QWizardPage(parent) , userHasGpgKeys(false) { ui.setupUi(this); registerField(QStringLiteral("gpgKey"), ui._gpgKey); KeysVector keys; GpgME::initializeLibrary(); GpgME::Error err = GpgME::checkEngine(GpgME::OpenPGP); if (err) { qDebug() << "OpenPGP not supported on your system!"; KMessageBox::error(this, i18n("The GpgME library failed to initialize for the OpenPGP protocol. Please check your system's configuration then try again.")); } else { std::shared_ptr< GpgME::Context > ctx(GpgME::Context::createForProtocol(GpgME::OpenPGP)); if (nullptr == ctx) { KMessageBox::error(this, i18n("The GpgME library failed to initialize for the OpenPGP protocol. Please check your system's configuration then try again.")); } else { ctx->setKeyListMode(GPGME_KEYLIST_MODE_LOCAL); err = ctx->startKeyListing(); while (!err) { GpgME::Key k = ctx->nextKey(err); if (err) { break; } if (!k.isInvalid() && k.canEncrypt() && (k.ownerTrust() == GpgME::Key::Ultimate)) { keys.push_back(k); } } ctx->endKeyListing(); } } std::for_each(keys.begin(), keys.end(), AddKeyToCombo(ui._gpgKey)); userHasGpgKeys = keys.size() > 0; if (userHasGpgKeys) { ui.stackedWidget->setCurrentWidget(ui._pageWhenHasKeys); } else { ui.stackedWidget->setCurrentWidget(ui._pageNoKeys); setFinalPage(true); } emit completeChanged(); } int nextId() const override { return static_cast(wizard())->wizardType() == KWalletWizard::Basic ? -1 : KWalletWizard::PageOptionsId; } bool isComplete() const override { return userHasGpgKeys; } bool hasGpgKeys() const { return userHasGpgKeys; } GpgME::Key gpgKey() const { QVariant varKey = ui._gpgKey->itemData(field(QStringLiteral("gpgKey")).toInt()); return varKey.value< GpgME::Key >(); } private: Ui::KWalletWizardPageGpgKey ui; bool userHasGpgKeys; }; #endif class PageOptions : public QWizardPage { public: explicit PageOptions(QWidget *parent) : QWizardPage(parent) { ui.setupUi(this); registerField(QStringLiteral("closeWhenIdle"), ui._closeIdle); registerField(QStringLiteral("networkWallet"), ui._networkWallet); } private: Ui::KWalletWizardPageOptions ui; }; class PageExplanation : public QWizardPage { public: PageExplanation(QWidget *parent) : QWizardPage(parent) { ui.setupUi(this); setFinalPage(true); } private: Ui::KWalletWizardPageExplanation ui; }; KWalletWizard::KWalletWizard(QWidget *parent) : QWizard(parent) { setOption(HaveFinishButtonOnEarlyPages); m_pageIntro = new PageIntro(this); setPage(PageIntroId, m_pageIntro); m_pagePasswd = new PagePassword(this); setPage(PagePasswordId, m_pagePasswd); #ifdef HAVE_GPGMEPP m_pageGpgKey = new PageGpgKey(this); setPage(PageGpgKeyId, m_pageGpgKey); #endif setPage(PageOptionsId, new PageOptions(this)); setPage(PageExplanationId, new PageExplanation(this)); resize(500, 420); } void KWalletWizard::passwordPageUpdate() { bool complete = true; if (field(QStringLiteral("useWallet")).toBool()) { #ifdef HAVE_GPGMEPP if (field(QStringLiteral("useBlowfish")).toBool()) { m_pagePasswd->setFinalPage(wizardType() == Basic); button(NextButton)->setVisible(wizardType() != Basic); #endif if (field(QStringLiteral("pass1")).toString() == field(QStringLiteral("pass2")).toString()) { if (field(QStringLiteral("pass1")).toString().isEmpty()) { m_pagePasswd->setMatchLabelText(i18n("Password is empty. (WARNING: Insecure)")); } else { m_pagePasswd->setMatchLabelText(i18n("Passwords match.")); } } else { m_pagePasswd->setMatchLabelText(i18n("Passwords do not match.")); complete = false; } #ifdef HAVE_GPGMEPP } else { m_pagePasswd->setFinalPage(false); button(NextButton)->setEnabled(true); return; } #endif } else { m_pagePasswd->setMatchLabelText(QString()); } button(wizardType() == Basic ? FinishButton : NextButton)->setEnabled(complete); } KWalletWizard::WizardType KWalletWizard::wizardType() const { return (KWalletWizard::WizardType)m_pageIntro->bg->checkedId(); } void KWalletWizard::initializePage(int id) { switch (id) { case PagePasswordId: { bool islast = m_pageIntro->bg->checkedId() == 0; m_pagePasswd->setFinalPage(islast); button(NextButton)->setVisible(!islast); break; } } } #ifdef HAVE_GPGMEPP GpgME::Key KWalletWizard::gpgKey() const { return m_pageGpgKey->gpgKey(); } #endif