File indexing completed on 2024-05-12 05:22:53
0001 /* 0002 test_keygen.cpp 0003 0004 This file is part of libkleopatra's test suite. 0005 SPDX-FileCopyrightText: 2004 Klarälvdalens Datakonsult AB 0006 0007 SPDX-License-Identifier: GPL-2.0-only 0008 */ 0009 0010 #include "test_keygen.h" 0011 0012 #include <libkleo/formatting.h> 0013 #include <libkleo/progressdialog.h> 0014 0015 #include <qgpgme/keygenerationjob.h> 0016 #include <qgpgme/keylistjob.h> 0017 #include <qgpgme/protocol.h> 0018 0019 #include <gpgme++/keygenerationresult.h> 0020 0021 #include <KAboutData> 0022 0023 #include <KMessageBox> 0024 #include <QDebug> 0025 0026 #include <QGridLayout> 0027 #include <QLabel> 0028 #include <QLineEdit> 0029 0030 #include <KGuiItem> 0031 #include <KLocalizedString> 0032 #include <QApplication> 0033 #include <QCommandLineParser> 0034 #include <QDialogButtonBox> 0035 #include <QPushButton> 0036 #include <QVBoxLayout> 0037 0038 static const char *const keyParams[] = { 0039 "Key-Type", 0040 "Key-Length", 0041 "Subkey-Type", 0042 "Subkey-Length", 0043 "Name-Real", 0044 "Name-Comment", 0045 "Name-Email", 0046 "Name-DN", 0047 "Expire-Date", 0048 "Passphrase", 0049 }; 0050 static const int numKeyParams = sizeof keyParams / sizeof *keyParams; 0051 0052 static const char *protocol = nullptr; 0053 0054 KeyGenerator::KeyGenerator(QWidget *parent) 0055 : QDialog(parent) 0056 { 0057 setModal(true); 0058 setWindowTitle(QStringLiteral("KeyGenerationJob test")); 0059 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close); 0060 QWidget *mainWidget = new QWidget(this); 0061 auto mainLayout = new QVBoxLayout(this); 0062 mainLayout->addWidget(mainWidget); 0063 auto user1Button = new QPushButton; 0064 buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole); 0065 connect(buttonBox, &QDialogButtonBox::rejected, this, &KeyGenerator::reject); 0066 user1Button->setDefault(true); 0067 KGuiItem::assign(user1Button, KGuiItem(QStringLiteral("Create"))); 0068 0069 QWidget *w = new QWidget(this); 0070 mainLayout->addWidget(w); 0071 mainLayout->addWidget(buttonBox); 0072 0073 auto glay = new QGridLayout(w); 0074 0075 int row = -1; 0076 0077 ++row; 0078 glay->addWidget(new QLabel(QStringLiteral("<GnupgKeyParms format=\"internal\">"), w), row, 0, 1, 2); 0079 for (int i = 0; i < numKeyParams; ++i) { 0080 ++row; 0081 glay->addWidget(new QLabel(QString::fromLatin1(keyParams[i]), w), row, 0); 0082 glay->addWidget(mLineEdits[i] = new QLineEdit(w), row, 1); 0083 } 0084 0085 ++row; 0086 glay->addWidget(new QLabel(QStringLiteral("</GnupgKeyParms>"), w), row, 0, 1, 2); 0087 ++row; 0088 glay->setRowStretch(row, 1); 0089 glay->setColumnStretch(1, 1); 0090 0091 connect(user1Button, &QPushButton::clicked, this, &KeyGenerator::slotStartKeyGeneration); 0092 } 0093 0094 KeyGenerator::~KeyGenerator() 0095 { 0096 } 0097 0098 void KeyGenerator::slotStartKeyGeneration() 0099 { 0100 QString params = QStringLiteral("<GnupgKeyParms format=\"internal\">\n"); 0101 for (int i = 0; i < numKeyParams; ++i) { 0102 if (mLineEdits[i] && !mLineEdits[i]->text().trimmed().isEmpty()) { 0103 params += QString::fromLatin1(keyParams[i]) + (QStringLiteral(": ") + mLineEdits[i]->text().trimmed()) + QLatin1Char('\n'); 0104 } 0105 } 0106 params += QStringLiteral("</GnupgKeyParms>\n"); 0107 0108 const QGpgME::Protocol *proto = nullptr; 0109 if (protocol) { 0110 proto = !strcmp(protocol, "openpgp") ? QGpgME::openpgp() : QGpgME::smime(); 0111 } 0112 if (!proto) { 0113 proto = QGpgME::smime(); 0114 } 0115 Q_ASSERT(proto); 0116 0117 qDebug() << "Using protocol" << proto->name(); 0118 0119 QGpgME::KeyGenerationJob *job = proto->keyGenerationJob(); 0120 Q_ASSERT(job); 0121 0122 connect(job, &QGpgME::KeyGenerationJob::result, this, &KeyGenerator::slotResult); 0123 0124 const GpgME::Error err = job->start(params); 0125 if (err) { 0126 showError(err); 0127 } 0128 #ifndef LIBKLEO_NO_PROGRESSDIALOG 0129 else { 0130 (void)new Kleo::ProgressDialog(job, QStringLiteral("Generating key"), this); 0131 } 0132 #endif 0133 } 0134 0135 void KeyGenerator::showError(const GpgME::Error &err) 0136 { 0137 KMessageBox::error(this, 0138 QStringLiteral("Could not start key generation: %1").arg(Kleo::Formatting::errorAsString(err)), 0139 QStringLiteral("Key Generation Error")); 0140 } 0141 0142 void KeyGenerator::slotResult(const GpgME::KeyGenerationResult &res, const QByteArray &keyData) 0143 { 0144 if (res.error()) { 0145 showError(res.error()); 0146 } else { 0147 KMessageBox::information(this, 0148 QStringLiteral("Key generated successfully, %1 bytes long").arg(keyData.size()), 0149 QStringLiteral("Key Generation Finished")); 0150 } 0151 } 0152 0153 int main(int argc, char **argv) 0154 { 0155 if (argc == 2) { 0156 protocol = argv[1]; 0157 argc = 1; // hide from KDE 0158 } 0159 QApplication app(argc, argv); 0160 KAboutData aboutData(QStringLiteral("test_keygen"), i18n("KeyGenerationJob Test"), QStringLiteral("0.1")); 0161 QCommandLineParser parser; 0162 KAboutData::setApplicationData(aboutData); 0163 aboutData.setupCommandLine(&parser); 0164 parser.process(app); 0165 aboutData.processCommandLine(&parser); 0166 0167 auto keygen = new KeyGenerator(nullptr); 0168 keygen->setObjectName(QLatin1StringView("KeyGenerator top-level")); 0169 keygen->show(); 0170 0171 return app.exec(); 0172 } 0173 0174 #include "moc_test_keygen.cpp"