KItinerary

documentutil.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "documentutil.h"
8
9#include <KItinerary/JsonLdDocument>
10
11#include <QCryptographicHash>
12#include <QDebug>
13#include <QString>
14#include <QUrl>
15
16using namespace Qt::Literals::StringLiterals;
17using namespace KItinerary;
18
25
26QString DocumentUtil::idForPkPass(const QString &passTypeIdentifier, const QString &serialNumber)
27{
28 QUrl url;
29 url.setScheme("pkpass"_L1);
30 url.setHost(passTypeIdentifier); // TODO can this contain problematic characters?
31 url.setPath('/'_L1 + serialNumber);
32 return url.toString();
33}
34
36{
37 auto l = documentIds(obj);
38 if (!l.contains(id) && !id.isEmpty()) {
39 l.push_back(id);
40 setDocumentIds(obj, l);
41 return true;
42 }
43 return false;
44}
45
47{
48 auto l = documentIds(obj);
49 if (l.contains(id)) {
50 l.removeAll(id);
51 setDocumentIds(obj, l);
52 return true;
53 }
54 return false;
55}
56
57QVariantList DocumentUtil::documentIds(const QVariant &obj)
58{
59 return JsonLdDocument::readProperty(obj, "subjectOf").toList();
60}
61
62void DocumentUtil::setDocumentIds(QVariant &obj, const QVariantList &docIds)
63{
64 JsonLdDocument::writeProperty(obj, "subjectOf", docIds);
65}
66
68{
69 const auto docIds = DocumentUtil::documentIds(obj);
70 for (const auto &docIdV : docIds) {
71 if (docIdV.typeId() != QMetaType::QString) {
72 continue;
73 }
74 auto docId = QUrl(docIdV.toString());
75 if (docId.scheme() == "pkpass"_L1) {
76 return docId;
77 }
78 }
79 return {};
80}
static void writeProperty(QVariant &obj, const char *name, const QVariant &value)
Set property name on object obj to value value.
static QVariant readProperty(const QVariant &obj, const char *name)
Read property name on object obj.
QString idForContent(const QByteArray &data)
Determine a document identifier for the given content.
bool addDocumentId(QVariant &obj, const QString &id)
Add a document id to the object obj.
QString idForPkPass(const QString &passTypeIdentifier, const QString &serialNumber)
Determine a document identifier for a Apple Wallet pass.
bool removeDocumentId(QVariant &obj, const QString &id)
Removes the document identifier id from object res.
void setDocumentIds(QVariant &obj, const QVariantList &docIds)
Sets the list of document identifiers for object obj.
QVariantList documentIds(const QVariant &obj)
Returns all document identifiers associated with object obj.
QUrl pkPassId(const QVariant &obj)
Returns a Apple Wallet pass identifier if present in the documents ids of obj.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
QByteArray toHex(char separator) const const
bool addData(QIODevice *device)
QByteArray result() const const
QString fromLatin1(QByteArrayView str)
void setHost(const QString &host, ParsingMode mode)
void setPath(const QString &path, ParsingMode mode)
void setScheme(const QString &scheme)
QString toString(FormattingOptions options) const const
QList< QVariant > toList() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:36 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.