qt: Allow specifying import options when importing keys

* lang/qt/src/importjob.cpp, lang/qt/src/importjob.h (class ImportJob):
Add member functions setImportOptions and importOptions.
* lang/qt/src/importjob_p.h (struct ImportJobPrivate): Add member
m_importOptions.
* lang/qt/src/qgpgmeimportjob.cpp (import_qba): Add arg importOptions
and adjust the callers. Set import options context flag.

* lang/qt/tests/t-import.cpp (ImportTest): Add member function
testImportWithImportOptions.
--

GnuPG-bug-id: 7152
This commit is contained in:
Ingo Klöcker 2024-06-12 11:34:41 +02:00
parent 5b73b0722a
commit 2187a4de8c
No known key found for this signature in database
GPG Key ID: F5A5D1692277A1E9
5 changed files with 66 additions and 4 deletions

View File

@ -55,6 +55,18 @@ QString QGpgME::ImportJob::importFilter() const
return d->m_importFilter;
}
void QGpgME::ImportJob::setImportOptions(const QString &options)
{
const auto d = jobPrivate<ImportJobPrivate>(this);
d->m_importOptions = options;
}
QString QGpgME::ImportJob::importOptions() const
{
const auto d = jobPrivate<ImportJobPrivate>(this);
return d->m_importOptions;
}
void ImportJob::setKeyOrigin(GpgME::Key::Origin origin, const QString &url)
{
const auto d = jobPrivate<ImportJobPrivate>(this);

View File

@ -79,6 +79,9 @@ public:
void setImportFilter(const QString &filter);
QString importFilter() const;
void setImportOptions(const QString &options);
QString importOptions() const;
void setKeyOrigin(GpgME::Key::Origin origin, const QString &url = {});
GpgME::Key::Origin keyOrigin() const;
QString keyOriginUrl() const;

View File

@ -44,6 +44,7 @@ namespace QGpgME
struct ImportJobPrivate : public JobPrivate
{
QString m_importFilter;
QString m_importOptions;
GpgME::Key::Origin m_keyOrigin = GpgME::Key::OriginUnknown;
QString m_keyOriginUrl;
};

View File

@ -106,11 +106,15 @@ static const char *originToString(Key::Origin origin)
}
static QGpgMEImportJob::result_type import_qba(Context *ctx, const QByteArray &certData, const QString &importFilter,
Key::Origin keyOrigin, const QString &keyOriginUrl)
const QString &importOptions, Key::Origin keyOrigin,
const QString &keyOriginUrl)
{
if (!importFilter.isEmpty()) {
ctx->setFlag("import-filter", importFilter.toStdString().c_str());
}
if (!importOptions.isEmpty()) {
ctx->setFlag("import-options", importOptions.toStdString().c_str());
}
if (keyOrigin != Key::OriginUnknown) {
if (const auto origin = originToString(keyOrigin)) {
std::string value{origin};
@ -149,19 +153,19 @@ static QGpgMEImportJob::result_type import_qba(Context *ctx, const QByteArray &c
Error QGpgMEImportJob::start(const QByteArray &certData)
{
run(std::bind(&import_qba, std::placeholders::_1, certData, importFilter(), keyOrigin(), keyOriginUrl()));
run(std::bind(&import_qba, std::placeholders::_1, certData, importFilter(), importOptions(), keyOrigin(), keyOriginUrl()));
return Error();
}
GpgME::ImportResult QGpgME::QGpgMEImportJob::exec(const QByteArray &keyData)
{
const result_type r = import_qba(context(), keyData, importFilter(), keyOrigin(), keyOriginUrl());
const result_type r = import_qba(context(), keyData, importFilter(), importOptions(), keyOrigin(), keyOriginUrl());
return std::get<0>(r);
}
Error QGpgMEImportJob::startLater(const QByteArray &certData)
{
setWorkerFunction(std::bind(&import_qba, std::placeholders::_1, certData, importFilter(), keyOrigin(), keyOriginUrl()));
setWorkerFunction(std::bind(&import_qba, std::placeholders::_1, certData, importFilter(), importOptions(), keyOrigin(), keyOriginUrl()));
return {};
}

View File

@ -121,6 +121,48 @@ private Q_SLOTS:
QCOMPARE(key.userID(0).id(), "importWithImportFilter@example.net");
}
void testImportWithImportOptions()
{
if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.1.23") {
QSKIP("gpg does not yet support --import-options show-only");
}
// pub ed25519 2024-06-12 [SC]
// A52F4947AF1506F3A7572EFC140278B773CA7C16
// uid importOptions@example.net
static const char keyFpr[] = "A52F4947AF1506F3A7572EFC140278B773CA7C16";
static const char keyData[] =
"-----BEGIN PGP PUBLIC KEY BLOCK-----\n"
"\n"
"mDMEZmlpmBYJKwYBBAHaRw8BAQdAZaSopKwccTwnMlJBVCWMT6et1T1WF9EkXdJi\n"
"gzI74xW0GWltcG9ydE9wdGlvbnNAZXhhbXBsZS5uZXSIkwQTFgoAOxYhBKUvSUev\n"
"FQbzp1cu/BQCeLdzynwWBQJmaWmYAhsDBQsJCAcCAiICBhUKCQgLAgQWAgMBAh4H\n"
"AheAAAoJEBQCeLdzynwWjmQBAP4dQEN/M4/dKIAlxNAbWzIkV+eSoUFLJszOJ/xx\n"
"FwJzAP43gkdXkUsHZt/U3mLZqtiHJFd7JxVm7hKRoAVBhZZYDw==\n"
"=7Z1j\n"
"-----END PGP PUBLIC KEY BLOCK-----\n";
auto *job = openpgp()->importJob();
job->setImportOptions(QLatin1String{"show-only"});
connect(job, &ImportJob::result, this,
[this](ImportResult result, QString, Error)
{
QVERIFY(!result.error());
QCOMPARE(result.numConsidered(), 0);
QCOMPARE(result.numImported(), 0);
QVERIFY(result.imports().empty());
Q_EMIT asyncDone();
});
job->start(QByteArray{keyData});
QSignalSpy spy (this, SIGNAL(asyncDone()));
QVERIFY(spy.wait());
auto ctx = std::unique_ptr<GpgME::Context>(Context::createForProtocol(GpgME::OpenPGP));
GpgME::Error err;
const auto key = ctx->key(keyFpr, err, false);
QVERIFY(key.isNull());
}
void testImportWithKeyOrigin()
{
if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.1.22") {