Commit 9873fe82 authored by Volker Krause's avatar Volker Krause
Browse files

Make the context API in ExtractorEngine type-independent as well

parent b82ebd53
Pipeline #55221 passed with stages
in 12 minutes and 49 seconds
......@@ -58,7 +58,7 @@ private Q_SLOTS:
QTest::addColumn<QString>("contextFile");
QTest::addColumn<QString>("inputFile");
for (const QDir &baseDir : {QStringLiteral(SOURCE_DIR "/extractordata"), QStringLiteral(SOURCE_DIR "/../../kitinerary-tests")}) {
for (const QDir baseDir : {QStringLiteral(SOURCE_DIR "/extractordata"), QStringLiteral(SOURCE_DIR "/../../kitinerary-tests")}) {
if (!baseDir.exists()) {
continue;
}
......@@ -96,14 +96,13 @@ private Q_SLOTS:
if (cf.open(QFile::ReadOnly)) {
contextMsg.setContent(cf.readAll());
contextMsg.parse();
m_engine.setContext(&contextMsg);
m_engine.setContext(QVariant::fromValue(&contextMsg), u"message/rfc822");
} else if (inputFile.endsWith(QLatin1String(".eml"))) {
contextMsg.setContent(inFile.readAll());
inFile.seek(0);
contextMsg.parse();
m_engine.setContext(&contextMsg);
m_engine.setContext(QVariant::fromValue(&contextMsg), u"message/rfc822");
} else {
m_engine.setContext(nullptr);
m_engine.setContextDate(QDateTime({2018, 1, 1}, {0, 0}));
}
......
......@@ -109,6 +109,8 @@ public:
std::vector<GenericExtractor::Result> m_genericResults;
QJsonArray m_result;
QJSEngine m_engine;
ExtractorDocumentNode m_rootNode;
ExtractorDocumentNode m_contextNode;
ExtractorDocumentNodeFactory m_nodeFactory;
ExtractorRepository m_repo;
BarcodeDecoder m_barcodeDecoder;
......@@ -248,6 +250,9 @@ void ExtractorEngine::clear()
d->m_ownedMimeContent.reset();
d->m_barcodeDecoder.clearCache();
d->m_usedExtractor.clear();
d->m_rootNode = {};
d->m_contextNode = {};
}
void ExtractorEnginePrivate::resetContent()
......@@ -309,6 +314,8 @@ void ExtractorEngine::setData(const QByteArray &data, QStringView fileName, QStr
const auto nameType = ExtractorInput::typeFromFileName(fileName.toString());
const auto contentType = ExtractorInput::typeFromContent(data);
setData(data, nameType == ExtractorInput::Unknown ? contentType : nameType);
d->m_rootNode = d->m_nodeFactory.createNode(data, fileName, mimeType);
}
void ExtractorEngine::setData(const QByteArray &data, ExtractorInput::Type type)
......@@ -324,6 +331,7 @@ void ExtractorEngine::setData(const QByteArray &data, ExtractorInput::Type type)
void ExtractorEngine::setContent(const QVariant &data, QStringView mimeType)
{
d->m_rootNode = d->m_nodeFactory.createNode(data, mimeType);
// ### temporary scaffolding until we have the new extractor engine
switch (ExtractorInput::typeFromMimeType(mimeType.toString())) {
case ExtractorInput::Text:
......@@ -467,6 +475,20 @@ void ExtractorEngine::setContext(KMime::Content *context)
setContextDate({});
}
void ExtractorEngine::setContext(const QVariant &data, QStringView mimeType)
{
// ### temporary migration scaffolding
if (mimeType == QLatin1String("message/rfc822")) {
if (data.value<KMime::Content*>()) {
setContext(data.value<KMime::Content*>());
} else {
setContext(data.value<KMime::Message*>());
}
}
d->m_contextNode = d->m_nodeFactory.createNode(data, mimeType);
}
void ExtractorEnginePrivate::setContext(PdfDocument *pdf)
{
auto dt = pdf->modificationTime();
......@@ -480,6 +502,8 @@ void ExtractorEnginePrivate::setContext(PdfDocument *pdf)
void ExtractorEngine::setContextDate(const QDateTime &dt)
{
d->m_contextNode.setContextDateTime(dt);
d->m_context->m_senderDate = dt;
d->m_jsonLdApi->setContextDate(dt);
d->m_barcodeApi->setContextDate(dt);
......
......@@ -179,7 +179,15 @@ public:
* Calling this method is not necessary when using setContent,
* only when using any of the other content setter methods directly.
*/
void setContext(KMime::Content *context);
[[deprecated("set setContext()")]] void setContext(KMime::Content *context);
/** Provide a document part that is only used to determine which extractor to use,
* but not for extraction itself.
* This can for example be the MIME message part wrapping a document to extract.
* Using this is not necessary when this document part is already included in
* what is passed to setContent() already anyway.
*/
void setContext(const QVariant &data, QStringView mimeType);
/** Set the date the extracted document has been issued at.
* This does not need to be perfectly accurate and is used to
......
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