File indexing completed on 2024-05-12 05:22:53

0001 /*
0002     This file is part of libkleopatra's test suite.
0003     SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB
0004 
0005     SPDX-License-Identifier: GPL-2.0-only
0006 */
0007 
0008 #include <kleo/defaultkeyfilter.h>
0009 #include <ui/keyselectioncombo.h>
0010 
0011 #include <gpgme++/key.h>
0012 
0013 #include <KAboutData>
0014 #include <QDebug>
0015 
0016 #include <KLocalizedString>
0017 #include <QApplication>
0018 #include <QCommandLineParser>
0019 #include <QVBoxLayout>
0020 #include <vector>
0021 
0022 int main(int argc, char **argv)
0023 {
0024     QApplication app(argc, argv);
0025     KAboutData aboutData(QStringLiteral("test_keyselectioncombo"), i18n("KeySelectionCombo Test"), QStringLiteral("0.1"));
0026     QCommandLineParser parser;
0027     QCommandLineOption openpgpOption(QStringLiteral("openpgp"), i18n("Show OpenPGP keys"));
0028     parser.addOption(openpgpOption);
0029     QCommandLineOption smimeOption(QStringLiteral("smime"), i18n("Show S/MIME keys"));
0030     parser.addOption(smimeOption);
0031     QCommandLineOption encryptOption(QStringLiteral("encryption"), i18n("Show keys for encryption"));
0032     parser.addOption(encryptOption);
0033     QCommandLineOption signingOption(QStringLiteral("signing"), i18n("Show keys for signing"));
0034     parser.addOption(signingOption);
0035 
0036     KAboutData::setApplicationData(aboutData);
0037     aboutData.setupCommandLine(&parser);
0038     parser.process(app);
0039     aboutData.processCommandLine(&parser);
0040 
0041     QWidget window;
0042     QVBoxLayout layout(&window);
0043 
0044     Kleo::KeySelectionCombo combo;
0045     layout.addWidget(&combo);
0046 
0047     std::shared_ptr<Kleo::DefaultKeyFilter> filter(new Kleo::DefaultKeyFilter);
0048     filter->setCanSign(parser.isSet(signingOption) ? Kleo::DefaultKeyFilter::Set : Kleo::DefaultKeyFilter::DoesNotMatter);
0049     filter->setCanEncrypt(parser.isSet(encryptOption) ? Kleo::DefaultKeyFilter::Set : Kleo::DefaultKeyFilter::DoesNotMatter);
0050     filter->setIsOpenPGP(parser.isSet(openpgpOption) ? Kleo::DefaultKeyFilter::Set : Kleo::DefaultKeyFilter::NotSet);
0051     filter->setHasSecret(Kleo::DefaultKeyFilter::Set);
0052     // filter->setOwnerTrust(Kleo::DefaultKeyFilter::IsAtLeast);
0053     // filter->setOwnerTrustReferenceLevel(GpgME::Key::Ultimate);
0054     combo.setKeyFilter(filter);
0055 
0056     combo.prependCustomItem(QIcon(), i18n("No key"), QStringLiteral("no-key"));
0057     QObject::connect(&combo, &Kleo::KeySelectionCombo::currentKeyChanged, [](const GpgME::Key &key) {
0058         qDebug() << "Current key changed:" << key.keyID();
0059     });
0060     QObject::connect(&combo, &Kleo::KeySelectionCombo::customItemSelected, [](const QVariant &data) {
0061         qDebug() << "Custom item selected:" << data.toString();
0062     });
0063 
0064     window.show();
0065 
0066     /*
0067     if (dlg.exec() == QDialog::Accepted) {
0068         qDebug() << "accepted; selected key:" << (dlg.selectedKey().userID(0).id() ? dlg.selectedKey().userID(0).id() : "<null>") << "\nselected _keys_:";
0069         for (std::vector<GpgME::Key>::const_iterator it = dlg.selectedKeys().begin(); it != dlg.selectedKeys().end(); ++it) {
0070             qDebug() << (it->userID(0).id() ? it->userID(0).id() : "<null>");
0071         }
0072     } else {
0073         qDebug() << "rejected";
0074     }
0075     */
0076 
0077     return app.exec();
0078 }