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

Factor out barcode decoding of document nodes

Will be reused in the PDF document node processor.
parent f5fc1c50
......@@ -76,6 +76,7 @@ target_sources(KPimItinerary PRIVATE
pdf/popplerglobalparams.cpp
pdf/popplerutils.cpp
processors/barcodedocumentprocessorhelper.cpp
processors/binarydocumentprocessor.cpp
processors/externalprocessor.cpp
processors/htmldocumentprocessor.cpp
......
/*
SPDX-FileCopyrightText: 2021-2022 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "barcodedocumentprocessorhelper.h"
#include <KItinerary/ExtractorDocumentNode>
#include <KItinerary/ExtractorDocumentNodeFactory>
#include <KItinerary/ExtractorEngine>
using namespace KItinerary;
void BarcodeDocumentProcessorHelper::expandNode(const QImage &img, BarcodeDecoder::BarcodeTypes barcodeHints, ExtractorDocumentNode &parent, const ExtractorEngine* engine)
{
// in case the barcode raw data (string or bytearray) gets detected as a type we handle,
// we nevertheless inject a raw data node in between. This is useful in cases where the
// content is parsable but that is actually not desired (e.g. JSON content in ticket barcodes).
const auto b = engine->barcodeDecoder()->decodeBinary(img, barcodeHints);
if (!b.isEmpty()) {
auto c = engine->documentNodeFactory()->createNode(b);
if (c.isA<QByteArray>() || c.isA<QString>()) {
parent.appendChild(c);
return;
}
auto rawNode = engine->documentNodeFactory()->createNode(QVariant::fromValue(b), u"application/octet-stream");
rawNode.appendChild(c);
parent.appendChild(rawNode);
return;
}
const auto s = engine->barcodeDecoder()->decodeString(img, barcodeHints);
if (!s.isEmpty()) {
auto c = engine->documentNodeFactory()->createNode(s.toUtf8());
if (c.isA<QByteArray>() || c.isA<QString>()) {
parent.appendChild(c);
return;
}
auto rawNode = engine->documentNodeFactory()->createNode(QVariant::fromValue(s), u"text/plain");
rawNode.appendChild(c);
parent.appendChild(rawNode);
return;
}
}
/*
SPDX-FileCopyrightText: 2021-2022 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef KITINERARY_BARCODEDOCUMENTPROCESSORHELPER_H
#define KITINERARY_BARCODEDOCUMENTPROCESSORHELPER_H
#include <barcodedecoder.h>
namespace KItinerary {
class ExtractorDocumentNode;
class ExtractorEngine;
/** Barcode-related document processor implementation details. */
namespace BarcodeDocumentProcessorHelper
{
void expandNode(const QImage &img, BarcodeDecoder::BarcodeTypes barcodeHints, ExtractorDocumentNode &parent, const ExtractorEngine *engine);
}
}
#endif // KITINERARY_BARCODEDOCUMENTPROCESSORHELPER_H
......@@ -5,6 +5,7 @@
*/
#include "imagedocumentprocessor.h"
#include "barcodedocumentprocessorhelper.h"
#include <KItinerary/BarcodeDecoder>
#include <KItinerary/ExtractorDocumentNodeFactory>
......@@ -36,33 +37,5 @@ void ImageDocumentProcessor::expandNode(ExtractorDocumentNode &node, const Extra
}
barcodeHints = BarcodeDecoder::maybeBarcode(img.width(), img.height(), barcodeHints);
// in case the barcode raw data (string or bytearray) gets detected as a type we handle,
// we nevertheless inject a raw data node in between. This is useful in cases where the
// content is parsable but that is actually not desired (e.g. JSON content in ticket barcodes).
const auto b = engine->barcodeDecoder()->decodeBinary(img, barcodeHints);
if (!b.isEmpty()) {
auto c = engine->documentNodeFactory()->createNode(b);
if (c.isA<QByteArray>() || c.isA<QString>()) {
node.appendChild(c);
return;
}
auto rawNode = engine->documentNodeFactory()->createNode(QVariant::fromValue(b), u"application/octet-stream");
rawNode.appendChild(c);
node.appendChild(rawNode);
return;
}
const auto s = engine->barcodeDecoder()->decodeString(img, barcodeHints);
if (!s.isEmpty()) {
auto c = engine->documentNodeFactory()->createNode(s.toUtf8());
if (c.isA<QByteArray>() || c.isA<QString>()) {
node.appendChild(c);
return;
}
auto rawNode = engine->documentNodeFactory()->createNode(QVariant::fromValue(s), u"text/plain");
rawNode.appendChild(c);
node.appendChild(rawNode);
return;
}
BarcodeDocumentProcessorHelper::expandNode(img, barcodeHints, node, engine);
}
Supports Markdown
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