Use QStringLiteral
[kdepim.git] / kleopatra / tests / test_keylistmodels.cpp
blob02cc470b314f30b930cf7de4aec3cb4bb393c474
1 /* -*- mode: c++; c-basic-offset:4 -*-
2 test_keylistmodels.cpp
4 This file is part of Kleopatra's test suite.
5 Copyright (c) 2007 Klarälvdalens Datakonsult AB
7 Kleopatra is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Kleopatra is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 In addition, as a special exception, the copyright holders give
22 permission to link the code of this program with any edition of
23 the Qt library by Trolltech AS, Norway (or with modified versions
24 of Qt that use the same license as Qt), and distribute linked
25 combinations including the two. You must obey the GNU General
26 Public License in all respects for all of the code used other than
27 Qt. If you modify this file, you may extend this exception to
28 your version of the file, but you are not obligated to do so. If
29 you do not wish to do so, delete this exception statement from
30 your version.
33 #include <config-kleopatra.h>
35 #include <models/keylistmodel.h>
36 #include <models/keylistsortfilterproxymodel.h>
38 #include <utils/formatting.h>
40 #include <KAboutData>
42 #include <QTreeView>
43 #include <QLineEdit>
44 #include <QTimer>
45 #include <QEventLoop>
46 #include <QDateTime>
47 #include "kleopatra_debug.h"
49 #include <qgpgme/eventloopinteractor.h>
51 #include <gpgme++/context.h>
52 #include <gpgme++/error.h>
53 #include <gpgme++/key.h>
55 #include <memory>
56 #include <vector>
57 #include <string>
58 #include <cassert>
59 #include <QApplication>
60 #include <KLocalizedString>
61 #include <QCommandLineParser>
62 #include <QCommandLineOption>
64 class Relay : public QObject
66 Q_OBJECT
67 public:
68 explicit Relay(QObject *p = 0) : QObject(p) {}
70 public Q_SLOTS:
71 void slotNextKeyEvent(GpgME::Context *, const GpgME::Key &key)
73 qDebug("next key");
74 mKeys.push_back(key);
75 // push out keys in chunks of 1..16 keys
76 if (mKeys.size() > qrand() % 16U) {
77 emit nextKeys(mKeys);
78 mKeys.clear();
82 void slotOperationDoneEvent(GpgME::Context *, const GpgME::Error &error)
84 qDebug("listing done error: %d", error.encodedError());
87 Q_SIGNALS:
88 void nextKeys(const std::vector<GpgME::Key> &keys);
90 private:
91 std::vector<GpgME::Key> mKeys;
94 int main(int argc, char *argv[])
97 if (const GpgME::Error initError = GpgME::initializeLibrary(0)) {
98 qCDebug(KLEOPATRA_LOG) << "Error initializing gpgme:" << QString::fromLocal8Bit(initError.asString()) ;
99 return 1;
102 KAboutData aboutData("test_flatkeylistmodel", 0, i18n("FlatKeyListModel Test"), "0.2");
103 QApplication app(argc, argv);
104 QCommandLineParser parser;
105 KAboutData::setApplicationData(aboutData);
106 parser.addVersionOption();
107 parser.addHelpOption();
108 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("flat"), i18n("Perform flat certificate listing")));
109 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("hierarchical"), i18n("Perform hierarchical certificate listing")));
110 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("disable-smime"), i18n("Do not list SMIME certificates")));
111 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("secret"), i18n("List secret keys only")));
113 aboutData.setupCommandLine(&parser);
114 parser.process(app);
115 aboutData.processCommandLine(&parser);
117 const bool showFlat = parser.isSet(QStringLiteral("flat")) || !parser.isSet(QStringLiteral("hierarchical"));
118 const bool showHier = parser.isSet(QStringLiteral("hierarchical")) || !parser.isSet(QStringLiteral("flat"));
119 const bool disablesmime = parser.isSet(QStringLiteral("disable-smime"));
120 const bool secretOnly = parser.isSet(QStringLiteral("secret"));
122 qsrand(QDateTime::currentDateTime().toTime_t());
124 QWidget flatWidget, hierarchicalWidget;
125 QVBoxLayout flatLay(&flatWidget), hierarchicalLay(&hierarchicalWidget);
126 QLineEdit flatLE(&flatWidget), hierarchicalLE(&hierarchicalWidget);
127 QTreeView flat(&flatWidget), hierarchical(&hierarchicalWidget);
129 flat.setSortingEnabled(true);
130 flat.sortByColumn(Kleo::AbstractKeyListModel::Fingerprint, Qt::AscendingOrder);
131 hierarchical.setSortingEnabled(true);
132 hierarchical.sortByColumn(Kleo::AbstractKeyListModel::Fingerprint, Qt::AscendingOrder);
134 flatLay.addWidget(&flatLE);
135 flatLay.addWidget(&flat);
137 hierarchicalLay.addWidget(&hierarchicalLE);
138 hierarchicalLay.addWidget(&hierarchical);
140 flatWidget.setWindowTitle(QStringLiteral("Flat Key Listing"));
141 hierarchicalWidget.setWindowTitle(QStringLiteral("Hierarchical Key Listing"));
143 Kleo::KeyListSortFilterProxyModel flatProxy, hierarchicalProxy;
145 QObject::connect(&flatLE, SIGNAL(textChanged(QString)), &flatProxy, SLOT(setFilterFixedString(QString)));
146 QObject::connect(&hierarchicalLE, SIGNAL(textChanged(QString)), &hierarchicalProxy, SLOT(setFilterFixedString(QString)));
148 Relay relay;
149 QObject::connect(QGpgME::EventLoopInteractor::instance(), SIGNAL(nextKeyEventSignal(GpgME::Context*,GpgME::Key)),
150 &relay, SLOT(slotNextKeyEvent(GpgME::Context*,GpgME::Key)));
151 QObject::connect(QGpgME::EventLoopInteractor::instance(), SIGNAL(operationDoneEventSignal(GpgME::Context*,GpgME::Error)),
152 &relay, SLOT(slotOperationDoneEvent(GpgME::Context*,GpgME::Error)));
154 if (showFlat)
155 if (Kleo::AbstractKeyListModel *const model = Kleo::AbstractKeyListModel::createFlatKeyListModel(&flat)) {
156 QObject::connect(&relay, SIGNAL(nextKeys(std::vector<GpgME::Key>)), model, SLOT(addKeys(std::vector<GpgME::Key>)));
157 model->setToolTipOptions(Kleo::Formatting::AllOptions);
158 flatProxy.setSourceModel(model);
159 flat.setModel(&flatProxy);
161 flatWidget.show();
164 if (showHier)
165 if (Kleo::AbstractKeyListModel *const model = Kleo::AbstractKeyListModel::createHierarchicalKeyListModel(&hierarchical)) {
166 QObject::connect(&relay, SIGNAL(nextKeys(std::vector<GpgME::Key>)), model, SLOT(addKeys(std::vector<GpgME::Key>)));
167 model->setToolTipOptions(Kleo::Formatting::AllOptions);
168 hierarchicalProxy.setSourceModel(model);
169 hierarchical.setModel(&hierarchicalProxy);
171 hierarchicalWidget.show();
174 const char *pattern[] = { 0 };
176 const std::auto_ptr<GpgME::Context> pgp(GpgME::Context::createForProtocol(GpgME::OpenPGP));
177 pgp->setManagedByEventLoopInteractor(true);
178 pgp->setKeyListMode(GpgME::Local);
180 if (const GpgME::Error e = pgp->startKeyListing(pattern, secretOnly)) {
181 qCDebug(KLEOPATRA_LOG) << "pgp->startKeyListing() ->" << e.asString();
184 if (!disablesmime) {
185 const std::auto_ptr<GpgME::Context> cms(GpgME::Context::createForProtocol(GpgME::CMS));
186 cms->setManagedByEventLoopInteractor(true);
187 cms->setKeyListMode(GpgME::Local);
189 if (const GpgME::Error e = cms->startKeyListing(pattern, secretOnly)) {
190 qCDebug(KLEOPATRA_LOG) << "cms" << e.asString();
193 QEventLoop loop;
194 QTimer::singleShot(2000, &loop, SLOT(quit()));
195 loop.exec();
197 const std::auto_ptr<GpgME::Context> cms2(GpgME::Context::createForProtocol(GpgME::CMS));
198 cms2->setManagedByEventLoopInteractor(true);
199 cms2->setKeyListMode(GpgME::Local);
201 if (const GpgME::Error e = cms2->startKeyListing(pattern, secretOnly)) {
202 qCDebug(KLEOPATRA_LOG) << "cms2" << e.asString();
206 return app.exec();
209 #include "test_flatkeylistmodel.moc"