File indexing completed on 2024-05-12 05:21:34

0001 /*
0002    SPDX-FileCopyrightText: 2017-2018 Volker Krause <vkrause@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "field.h"
0010 #include "kpkpass_export.h"
0011 
0012 #include <QList>
0013 #include <QObject>
0014 
0015 #include <memory>
0016 
0017 class QByteArray;
0018 class QColor;
0019 class QDateTime;
0020 class QString;
0021 class QUrl;
0022 class QVariant;
0023 
0024 namespace KPkPass
0025 {
0026 class Barcode;
0027 class Location;
0028 class PassPrivate;
0029 
0030 /** Base class for a pkpass file.
0031  *  @see https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/PassKit_PG/index.html
0032  *  @see https://developer.apple.com/library/content/documentation/UserExperience/Reference/PassKit_Bundle/Chapters/TopLevel.html
0033  */
0034 class KPKPASS_EXPORT Pass : public QObject
0035 {
0036     Q_OBJECT
0037     Q_PROPERTY(Type type READ type CONSTANT)
0038 
0039     Q_PROPERTY(QString description READ description CONSTANT)
0040     Q_PROPERTY(QString organizationName READ organizationName CONSTANT)
0041     Q_PROPERTY(QString passTypeIdentifier READ passTypeIdentifier CONSTANT)
0042     Q_PROPERTY(QString serialNumber READ serialNumber CONSTANT)
0043 
0044     Q_PROPERTY(QDateTime expirationDate READ expirationDate CONSTANT)
0045     Q_PROPERTY(bool isVoided READ isVoided CONSTANT)
0046 
0047     Q_PROPERTY(QDateTime relevantDate READ relevantDate CONSTANT)
0048 
0049     Q_PROPERTY(QColor backgroundColor READ backgroundColor CONSTANT)
0050     Q_PROPERTY(QColor foregroundColor READ foregroundColor CONSTANT)
0051     Q_PROPERTY(QString groupingIdentifier READ groupingIdentifier CONSTANT)
0052     Q_PROPERTY(QColor labelColor READ labelColor CONSTANT)
0053     Q_PROPERTY(QString logoText READ logoText CONSTANT)
0054 
0055     Q_PROPERTY(bool hasIcon READ hasIcon CONSTANT)
0056     Q_PROPERTY(bool hasLogo READ hasLogo CONSTANT)
0057     Q_PROPERTY(bool hasStrip READ hasStrip CONSTANT)
0058     Q_PROPERTY(bool hasBackground READ hasBackground CONSTANT)
0059     Q_PROPERTY(bool hasFooter READ hasFooter CONSTANT)
0060     Q_PROPERTY(bool hasThumbnail READ hasThumbnail CONSTANT)
0061 
0062     // needs to be QVariantList just for QML (Grantlee would also work with QList<Field>
0063     Q_PROPERTY(QList<KPkPass::Barcode> barcodes READ barcodes CONSTANT)
0064     Q_PROPERTY(QList<KPkPass::Field> auxiliaryFields READ auxiliaryFields CONSTANT)
0065     Q_PROPERTY(QList<KPkPass::Field> backFields READ backFields CONSTANT)
0066     Q_PROPERTY(QList<KPkPass::Field> headerFields READ headerFields CONSTANT)
0067     Q_PROPERTY(QList<KPkPass::Field> primaryFields READ primaryFields CONSTANT)
0068     Q_PROPERTY(QList<KPkPass::Field> secondaryFields READ secondaryFields CONSTANT)
0069     Q_PROPERTY(QList<KPkPass::Location> locations READ locations CONSTANT)
0070     Q_PROPERTY(QVariantMap field READ fieldsVariantMap CONSTANT)
0071 
0072 public:
0073     ~Pass() override;
0074 
0075     /** Type of the pass. */
0076     enum Type { BoardingPass, Coupon, EventTicket, Generic, StoreCard };
0077     Q_ENUM(Type)
0078     [[nodiscard]] Type type() const;
0079 
0080     // standard keys
0081     [[nodiscard]] QString description() const;
0082     [[nodiscard]] QString organizationName() const;
0083     [[nodiscard]] QString passTypeIdentifier() const;
0084     [[nodiscard]] QString serialNumber() const;
0085 
0086     // expiration keys
0087     [[nodiscard]] QDateTime expirationDate() const;
0088     [[nodiscard]] bool isVoided() const;
0089 
0090     // relevance keys
0091     /** Locations associated with this pass. */
0092     [[nodiscard]] QList<Location> locations() const;
0093     /** Distance in meters to any of the pass locations before this pass becomes relevant. */
0094     [[nodiscard]] int maximumDistance() const;
0095     [[nodiscard]] QDateTime relevantDate() const;
0096 
0097     // visual appearance keys
0098     /** Returns all barcodes defined in the pass. */
0099     [[nodiscard]] QList<Barcode> barcodes() const;
0100     [[nodiscard]] QColor backgroundColor() const;
0101     [[nodiscard]] QColor foregroundColor() const;
0102     [[nodiscard]] QString groupingIdentifier() const;
0103     [[nodiscard]] QColor labelColor() const;
0104     [[nodiscard]] QString logoText() const;
0105 
0106     /** Returns @c true if an image asset with the given base name exists.
0107      *  @param baseName The name of the asset, without the file type and high dpi extensions.
0108      *  @since 5.20.41
0109      */
0110     bool hasImage(const QString &baseName) const;
0111     bool hasIcon() const;
0112     bool hasLogo() const;
0113     bool hasStrip() const;
0114     bool hasBackground() const;
0115     bool hasFooter() const;
0116     bool hasThumbnail() const;
0117 
0118     /** Returns an image asset of this pass.
0119      *  @param baseName The name of the asset, without the file name extension.
0120      *  @param devicePixelRatio The device pixel ration, for loading highdpi assets.
0121      */
0122     [[nodiscard]] QImage image(const QString &baseName, unsigned int devicePixelRatio = 1) const;
0123     /** Returns the pass icon. */
0124     Q_INVOKABLE [[nodiscard]] QImage icon(unsigned int devicePixelRatio = 1) const;
0125     /** Returns the pass logo. */
0126     Q_INVOKABLE [[nodiscard]] QImage logo(unsigned int devicePixelRatio = 1) const;
0127     /** Returns the strip image if present. */
0128     Q_INVOKABLE [[nodiscard]] QImage strip(unsigned int devicePixelRatio = 1) const;
0129     /** Returns the background image if present. */
0130     Q_INVOKABLE [[nodiscard]] QImage background(unsigned int devicePixelRatio = 1) const;
0131     /** Returns the footer image if present. */
0132     Q_INVOKABLE [[nodiscard]] QImage footer(unsigned int devicePixelRatio = 1) const;
0133     /** Returns the thumbnail image if present. */
0134     Q_INVOKABLE [[nodiscard]] QImage thumbnail(unsigned int devicePixelRatio = 1) const;
0135 
0136     // web service keys
0137     [[nodiscard]] QString authenticationToken() const;
0138     [[nodiscard]] QUrl webServiceUrl() const;
0139     /** Pass update URL.
0140      * @see https://developer.apple.com/library/content/documentation/PassKit/Reference/PassKit_WebService/WebService.html
0141      */
0142     [[nodiscard]] QUrl passUpdateUrl() const;
0143 
0144     QList<Field> auxiliaryFields() const;
0145     QList<Field> backFields() const;
0146     QList<Field> headerFields() const;
0147     QList<Field> primaryFields() const;
0148     QList<Field> secondaryFields() const;
0149 
0150     /** Returns the field with key @p key. */
0151     Field field(const QString &key) const;
0152     /** Returns all fields found in this pass. */
0153     QList<Field> fields() const;
0154 
0155     /** Create a appropriate sub-class based on the pkpass file type. */
0156     static Pass *fromData(const QByteArray &data, QObject *parent = nullptr);
0157     /** Create a appropriate sub-class based on the pkpass file type. */
0158     static Pass *fromFile(const QString &fileName, QObject *parent = nullptr);
0159 
0160     /** The raw data of this pass.
0161      *  That is the binary representation of the ZIP archive which contains
0162      *  all the pass data.
0163      *  @since 5.20.41
0164      */
0165     QByteArray rawData() const;
0166 
0167 protected:
0168     ///@cond internal
0169     friend class Barcode;
0170     friend class Field;
0171     friend class PassPrivate;
0172     explicit Pass(Type passType, QObject *parent = nullptr);
0173     std::unique_ptr<PassPrivate> d;
0174     ///@endcond
0175 
0176 private:
0177     QVariantMap fieldsVariantMap() const;
0178 };
0179 
0180 }