diff --git a/src/jsapi/barcode.cpp b/src/jsapi/barcode.cpp index 6dee574..65d35f2 100644 --- a/src/jsapi/barcode.cpp +++ b/src/jsapi/barcode.cpp @@ -1,128 +1,139 @@ /* Copyright (C) 2018 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "barcode.h" #include "bitarray.h" #include #include #include #include #include +#include +#include #include using namespace KItinerary; void JsApi::Barcode::setDecoder(BarcodeDecoder *decoder) { m_decoder = decoder; } QString JsApi::Barcode::decodePdf417(const QVariant &img) const { if (img.userType() == qMetaTypeId()) { const auto pdfImg = img.value(); if (!GenericPdfExtractor::maybeBarcode(pdfImg, BarcodeDecoder::PDF417)) { return {}; } return m_decoder->decodeString(pdfImg.image(), BarcodeDecoder::PDF417); } return {}; } QString JsApi::Barcode::decodeAztec(const QVariant &img) const { if (img.userType() == qMetaTypeId()) { const auto pdfImg = img.value(); if (!GenericPdfExtractor::maybeBarcode(pdfImg, BarcodeDecoder::Aztec)) { return {}; } return m_decoder->decodeString(pdfImg.image(), BarcodeDecoder::Aztec); } return {}; } QVariant JsApi::Barcode::decodeAztecBinary(const QVariant &img) const { if (img.userType() == qMetaTypeId()) { const auto pdfImg = img.value(); if (!GenericPdfExtractor::maybeBarcode(pdfImg, BarcodeDecoder::Aztec)) { return {}; } const auto content = m_decoder->decodeBinary(pdfImg.image(), BarcodeDecoder::Aztec); if (content.isEmpty()) { return {}; } return content; } return {}; } QString JsApi::Barcode::decodeQR(const QVariant &img) const { if (img.userType() == qMetaTypeId()) { const auto pdfImg = img.value(); if (!GenericPdfExtractor::maybeBarcode(pdfImg, BarcodeDecoder::QRCode)) { return {}; } return m_decoder->decodeString(pdfImg.image(), BarcodeDecoder::QRCode); } return {}; } QString JsApi::Barcode::decodeDataMatrix(const QVariant &img) const { if (img.userType() == qMetaTypeId()) { const auto pdfImg = img.value(); if (!GenericPdfExtractor::maybeBarcode(pdfImg, BarcodeDecoder::DataMatrix)) { return {}; } return m_decoder->decodeString(pdfImg.image(), BarcodeDecoder::DataMatrix); } return {}; } QVariant JsApi::Barcode::decodeUic9183(const QVariant &s) const { Uic9183Parser p; p.setContextDate(m_contextDate); p.parse(s.toByteArray()); return QVariant::fromValue(p); } QVariant JsApi::Barcode::decodeIataBcbp(const QString &s) const { return QVariant::fromValue(IataBcbpParser::parse(s, m_contextDate.date())); } +QVariant JsApi::Barcode::decodeVdvTicket(const QVariant &s) const +{ + VdvTicketParser p; + if (!p.parse(s.toByteArray())) { + return {}; + } + return QVariant::fromValue(p.ticket()); +} + QString JsApi::Barcode::toBase64(const QVariant &b) const { return QString::fromUtf8(b.toByteArray().toBase64()); } QVariant JsApi::Barcode::toBitArray(const QVariant &b) const { return QVariant::fromValue(JsApi::BitArray(b.toByteArray())); } void JsApi::Barcode::setContextDate(const QDateTime &dt) { m_contextDate = dt; } #include "moc_barcode.cpp" diff --git a/src/jsapi/barcode.h b/src/jsapi/barcode.h index 170ea5e..a090f34 100644 --- a/src/jsapi/barcode.h +++ b/src/jsapi/barcode.h @@ -1,85 +1,90 @@ /* Copyright (C) 2018 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef KITINERARY_JSAPI_BARCODE_H #define KITINERARY_JSAPI_BARCODE_H #include #include namespace KItinerary { class BarcodeDecoder; namespace JsApi { /** Barcode decoding functions. */ class Barcode : public QObject { Q_OBJECT public: /** Decode a PDF417 barcode image. * @param img An image containing the barcode, e.g. a PdfImage instance. */ Q_INVOKABLE QString decodePdf417(const QVariant &img) const; /** Decode a Aztec barcode image. * @param img An image containing the barcode, e.g. a PdfImage instance. */ Q_INVOKABLE QString decodeAztec(const QVariant &img) const; /** Decode a Aztec barcode image containing binary data. * @param img An image containing the barcode, e.g. a PdfImage instance. * @return a QByteArray, which from the JS perspective is essentially an opque handle. */ Q_INVOKABLE QVariant decodeAztecBinary(const QVariant &img) const; /** Decode as QR barcode image. * @param img An image containing the barcode, e.g. a PdfImage instance. */ Q_INVOKABLE QString decodeQR(const QVariant &img) const; /** Decode a DataMatrix barcode image. * @param img An image containing the barcode, e.g. a PdfImage instance. */ Q_INVOKABLE QString decodeDataMatrix(const QVariant &img) const; /** Decode an UIC 918.3 message from a train ticket Aztec code. * @param s A QByteArray containing the raw data from the barcode. * @returns An instance of Uic9183Parser. */ Q_INVOKABLE QVariant decodeUic9183(const QVariant &s) const; /** Decode an IATA BCBP message from a flight boarding pass barcode. * @returns A JSON-LD structure representing the boarding pass. */ Q_INVOKABLE QVariant decodeIataBcbp(const QString &s) const; + /** Decode an VDV ticket barcode. + * @param s A QByteArray containing the raw VDV barcode data. + * @returns An instance of VdvTicket. + */ + Q_INVOKABLE QVariant decodeVdvTicket(const QVariant &s) const; /** Converts the given QByteArray into an base64 encoded string. */ Q_INVOKABLE QString toBase64(const QVariant &b) const; /** Converts the given QByteArray into a BitArray. */ Q_INVOKABLE QVariant toBitArray(const QVariant &b) const; ///@cond internal void setContextDate(const QDateTime &dt); void setDecoder(BarcodeDecoder *decoder); ///@endcond private: QDateTime m_contextDate; BarcodeDecoder *m_decoder = nullptr; }; } } #endif // KITINERARY_JSAPI_BARCODE_H