Commit ea8264a3 authored by Volker Krause's avatar Volker Krause
Browse files

Return whether health certificate import was successful

This will be needed to provide proper user feedback.
parent 78c76ed1
......@@ -45,8 +45,8 @@ private Q_SLOTS:
QSignalSpy insertSpy(&mgr, &QAbstractItemModel::rowsInserted);
QCOMPARE(mgr.rowCount(), 0);
const auto rawData = readFile(QLatin1String(SOURCE_DIR "/data/health-certificates/full-vaccination.txt"));
mgr.importCertificate(rawData);
#if HAVE_KHEALTHCERTIFICATE
QVERIFY(mgr.importCertificate(rawData));
QCOMPARE(mgr.rowCount(), 1);
QCOMPARE(insertSpy.size(), 1);
QVERIFY(!mgr.data(mgr.index(0, 0), Qt::DisplayRole).toString().isEmpty());
......@@ -56,6 +56,29 @@ private Q_SLOTS:
#endif
}
{
HealthCertificateManager mgr;
QAbstractItemModelTester modelTester(&mgr);
#if HAVE_KHEALTHCERTIFICATE
QCOMPARE(mgr.rowCount(), 1);
const auto rawData = readFile(QLatin1String(SOURCE_DIR "/data/health-certificates/full-vaccination.txt"));
// no duplicates
QVERIFY(mgr.importCertificate(rawData));
QCOMPARE(mgr.rowCount(), 1);
#endif
}
{
HealthCertificateManager mgr;
QAbstractItemModelTester modelTester(&mgr);
#if HAVE_KHEALTHCERTIFICATE
QCOMPARE(mgr.rowCount(), 1);
// garbage is rejected
QVERIFY(!mgr.importCertificate("not a vaccination certificate"));
QCOMPARE(mgr.rowCount(), 1);
#endif
}
{
HealthCertificateManager mgr;
QAbstractItemModelTester modelTester(&mgr);
......
......@@ -529,7 +529,7 @@ QVector<QString> ApplicationController::importReservationOrHealthCertificate(con
return {};
}
void ApplicationController::importHealthCertificateRecursive(const ExtractorDocumentNode &node)
bool ApplicationController::importHealthCertificateRecursive(const ExtractorDocumentNode &node)
{
if (node.childNodes().size() == 1 && node.mimeType() == QLatin1String("internal/qimage")) {
const auto &child = node.childNodes()[0];
......@@ -541,9 +541,13 @@ void ApplicationController::importHealthCertificateRecursive(const ExtractorDocu
}
}
bool result = false;
for (const auto &child : node.childNodes()) {
importHealthCertificateRecursive(child);
if (importHealthCertificateRecursive(child)) { // no shortcut evaluation, more than one QR code per PDF is possible
result = true;
}
}
return result;
}
void ApplicationController::importPass(const QString &passId)
......
......@@ -87,7 +87,7 @@ private:
void importBundle(const QByteArray &data);
void importBundle(KItinerary::File *file);
QVector<QString> importReservationOrHealthCertificate(const QByteArray &data, const QString &fileName = {});
void importHealthCertificateRecursive(const KItinerary::ExtractorDocumentNode &node);
bool importHealthCertificateRecursive(const KItinerary::ExtractorDocumentNode &node);
void importPass(const QString &passId);
static ApplicationController *s_instance;
......
......@@ -48,19 +48,19 @@ static QString basePath()
return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QLatin1String("/health-certificates/");
}
void HealthCertificateManager::importCertificate(const QByteArray &rawData)
bool HealthCertificateManager::importCertificate(const QByteArray &rawData)
{
// check whether we know this certificate already
for (const auto &c : m_certificates) {
if (certificateRawData(c) == rawData) {
return;
return true;
}
}
#if HAVE_KHEALTHCERTIFICATE
CertData certData;
certData.cert = KHealthCertificateParser::parse(rawData);
if (certData.cert.isNull()) {
return;
return false;
}
auto path = basePath();
......@@ -71,7 +71,7 @@ void HealthCertificateManager::importCertificate(const QByteArray &rawData)
QFile f(path);
if (!f.open(QFile::WriteOnly)) {
qWarning() << f.errorString() << f.fileName();
return;
return false;
}
f.write(rawData);
f.close();
......@@ -81,6 +81,9 @@ void HealthCertificateManager::importCertificate(const QByteArray &rawData)
beginInsertRows({}, row, row);
m_certificates.insert(it, std::move(certData));
endInsertRows();
return true;
#else
return false;
#endif
}
......
......@@ -25,7 +25,7 @@ public:
/** Health certificate support is compiled in. */
bool isAvailable() const;
void importCertificate(const QByteArray &rawData);
bool importCertificate(const QByteArray &rawData);
Q_INVOKABLE void importCertificateFromClipboard();
Q_INVOKABLE void removeCertificate(int row);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment