diff --git a/src/gpgolkeyadder/gpgolkeyadder.cpp b/src/gpgolkeyadder/gpgolkeyadder.cpp index ea7159e..dd76a82 100644 --- a/src/gpgolkeyadder/gpgolkeyadder.cpp +++ b/src/gpgolkeyadder/gpgolkeyadder.cpp @@ -1,239 +1,248 @@ /* Copyright (C) 2018 by Intevation GmbH * * This file is Free Software under the GNU GPL (v>=2) * and comes with ABSOLUTELY NO WARRANTY! * See LICENSE.txt for details. */ #include "gpgolkeyadder.h" #include "w32-gettext.h" #include "w32-util.h" #include "stdinreader.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include GpgOLKeyAdder::GpgOLKeyAdder(const QCommandLineParser &parser): QDialog(nullptr), mEdit(new QTextEdit) { setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint)); mName = parser.value(QStringLiteral("username")); setWindowTitle(_("GpgOL") + QStringLiteral(" - ") + _("Configure key for:") + QStringLiteral(" ") + mName); setWindowIcon(QIcon(":/gpgol-icon.svg")); const auto hwnd = parser.value(QStringLiteral("hwnd")); if (!hwnd.isEmpty()) { bool ok; WId id = (WId) hwnd.toInt(&ok); if (!ok) { qDebug() << "invalid hwnd value"; } else { W32::setupForeignParent(id, this, true); setModal(true); } } setupGUI(); // Deletes itself auto reader = new StdinReader; connect (reader, &StdinReader::stdinRead, this, [this] (const QByteArray &data) { - if (data.size()) { + if (data.size() > 1) { mEdit->setPlainText(QString::fromUtf8(data)); } }); reader->start(); } void GpgOLKeyAdder::setupGUI() { /* Setup Edit */ auto fixedFont = QFont("Monospace", 10); fixedFont.setStyleHint(QFont::TypeWriter); mEdit->setFont(fixedFont); resize(QFontMetrics(fixedFont).averageCharWidth() * 80, - QFontMetrics(fixedFont).height() * 40); + QFontMetrics(fixedFont).height() * 30); mEdit->setPlaceholderText(QString::fromUtf8(_("Paste a public key export here. It should look like:")) + QStringLiteral("\n\n-----BEGIN PGP PUBLIC KEY BLOCK-----\n\n" "mQENBEzJavsBCADG/guWL6AxGgngUxp/DcmoitJjaJMqcJkBtD3uKrW81Pbnm3LI\n" "...\n" "dCl8hHggB9x2\n" "=oShe\n" "-----END PGP PUBLIC KEY BLOCK-----")); mEdit->setUndoRedoEnabled(true); /* Setup buttons */ QDialogButtonBox *buttonBox = new QDialogButtonBox(); buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok); connect(buttonBox->button(QDialogButtonBox::Ok), &QAbstractButton::clicked, this, [this] () { checkAccept(); }); connect(buttonBox->button(QDialogButtonBox::Cancel), &QAbstractButton::clicked, this, [this] () { qApp->quit(); }); auto okBtn = buttonBox->button(QDialogButtonBox::Ok); okBtn->setEnabled(false); connect(mEdit, &QTextEdit::textChanged, this, [this, okBtn] () { const auto text = mEdit->toPlainText().trimmed().toUtf8(); + if (!text.size()) { + okBtn->setEnabled(true); + return; + } GpgME::Data data(text.constData(), text.size(), false); okBtn->setEnabled(data.type() == GpgME::Data::PGPKey); }); /* Setup layout */ auto layout = new QVBoxLayout; setLayout(layout); layout->addWidget(mEdit); layout->addWidget(buttonBox); } -void GpgOLKeyAdder::save(const QByteArray &data) +static void save(const QByteArray &data) { - std::cout << data.constData() << std::endl; + if (!data.size()) { + /* Empty is a special case which can mean that an + * existing key should be removed. */ + std::cout << "empty" << std::endl; + } else { + std::cout << data.constData() << std::endl; + } qApp->quit(); } static QString prettyNameAndEMail(const QString &id, const QString &name, const QString &email, const QString &comment) { if (name.isEmpty()) { if (email.isEmpty()) { return QString(); } else if (comment.isEmpty()) { return QStringLiteral("<%1>").arg(email); } else { return QStringLiteral("(%2) <%1>").arg(email, comment); } } if (email.isEmpty()) { if (comment.isEmpty()) { return name; } else { return QStringLiteral("%1 (%2)").arg(name, comment); } } if (comment.isEmpty()) { return QStringLiteral("%1 <%2>").arg(name, email); } else { return QStringLiteral("%1 (%3) <%2>").arg(name, email, comment); } return QString(); } static QString prettyNameAndEMail(const char *id, const char *name_, const std::string &email_, const char *comment_) { return prettyNameAndEMail(QString::fromUtf8(id), QString::fromUtf8(name_), QString::fromStdString(email_), QString::fromUtf8(comment_)); } static QString prettyNameAndEMail(const GpgME::UserID &uid) { - qDebug() << "UserID: " << uid.id(); return prettyNameAndEMail(uid.id(), uid.name(), uid.addrSpec(), uid.comment()); } static QString time_t2string(time_t t) { QDateTime dt; dt.setTime_t(t); return QLocale().toString(dt, QLocale::ShortFormat); } void GpgOLKeyAdder::checkAccept() { const auto text = mEdit->toPlainText().trimmed().toUtf8(); if (text.isEmpty()) { save(text); // Save exits return; } GpgME::Data data(text.constData(), text.size(), false); const auto keys = data.toKeys(); if (!keys.size()) { QMessageBox::warning(this, QString::fromUtf8 (_("Error")), QStringLiteral("%1

").arg(QString::fromUtf8(_("Failed to parse any public key.")))); return; } QStringList keyInfos; for (const auto &key: keys) { if (key.isNull() || !key.numSubkeys()) { qDebug() << "Null key?"; continue; } if (key.hasSecret()) { QMessageBox::warning(this, QString::fromUtf8 (_("Error")), QStringLiteral("%1

").arg(QString::fromUtf8(_("Secret key detected."))) + QString::fromUtf8(_("You can only configure public keys in Outlook." " Import secret keys with Kleopatra."))); return; } if (key.isRevoked() || key.isExpired() || key.isInvalid() || key.isDisabled() || !key.canEncrypt()) { QMessageBox::warning(this, QString::fromUtf8 (_("Error")), QStringLiteral("%1

%2

").arg(QString::fromUtf8(_("Invalid key detected."))).arg( key.primaryFingerprint()) + QString::fromUtf8(_("The key is unusable for Outlook." " Please check Kleopatra for more information."))); return; } const auto subkey = key.subkey(0); QString info = QString::fromLatin1(key.primaryFingerprint()) + "
" + QString::fromUtf8(_("Created:")) + " " + time_t2string(subkey.creationTime()) + "
" + QString::fromUtf8(_("User Ids:")) + "
"; for (const auto &uid: key.userIDs()) { if (uid.isNull() || uid.isRevoked() || uid.isInvalid()) { continue; } info += "  " + prettyNameAndEMail(uid).toHtmlEscaped() + "
"; } keyInfos << info; } QString msg = QString::fromUtf8(_("You are about to configure the following %1 for:")).arg( (keyInfos.size() > 1 ? QString::fromUtf8(_("keys")) : QString::fromUtf8(_("key")))) + QStringLiteral("
\t\"%1\"

").arg(mName.toHtmlEscaped()) + keyInfos.join("

"); msg += "

" + QString::fromUtf8 (_("Continue?")); const auto ret = QMessageBox::question(this, QString::fromUtf8(_("Confirm keys")), msg, QMessageBox::Yes | QMessageBox::Abort, QMessageBox::Yes); if (ret == QMessageBox::Yes) { save(text); return; } } diff --git a/src/gpgolkeyadder/gpgolkeyadder.h b/src/gpgolkeyadder/gpgolkeyadder.h index f64401c..22c9d80 100644 --- a/src/gpgolkeyadder/gpgolkeyadder.h +++ b/src/gpgolkeyadder/gpgolkeyadder.h @@ -1,34 +1,33 @@ #ifndef GPGOLKEYADDER_H #define GPGOLKEYADDER_H /* Copyright (C) 2018 by Intevation GmbH * * This file is Free Software under the GNU GPL (v>=2) * and comes with ABSOLUTELY NO WARRANTY! * See LICENSE.txt for details. */ #include #include class QCommandLineParser; class QTextEdit; class GpgOLKeyAdder: public QDialog { Q_OBJECT public: GpgOLKeyAdder(const QCommandLineParser &parser); protected: /** @brief UI setup */ void setupGUI(); private: void checkAccept(); - void save(const QByteArray &data); QTextEdit *mEdit; QString mName; }; #endif // GPGOLKEYADDER_H