KItinerary

documentutil.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "documentutil.h"
8 
9 #include <KItinerary/JsonLdDocument>
10 #include <KItinerary/Reservation>
11 
12 #include <QCryptographicHash>
13 #include <QDebug>
14 #include <QString>
15 
16 using namespace KItinerary;
17 
19 {
21  hash.addData(data);
22  return QString::fromLatin1(hash.result().toHex());
23 }
24 
26 {
27  if (!JsonLd::canConvert<Reservation>(res)) {
28  return false;
29  }
30 
31  auto l = documentIds(res);
32  if (!l.contains(id)) {
33  l.push_back(id);
34  setDocumentIds(res, l);
35  return true;
36  }
37  return false;
38 }
39 
41 {
42  if (!JsonLd::canConvert<Reservation>(res)) {
43  return false;
44  }
45 
46  auto l = documentIds(res);
47  if (l.contains(id)) {
48  l.removeAll(id);
49  setDocumentIds(res, l);
50  return true;
51  }
52  return false;
53 }
54 
55 QVariantList DocumentUtil::documentIds(const QVariant &res)
56 {
57  if (!JsonLd::canConvert<Reservation>(res)) {
58  return {};
59  }
60  return JsonLd::convert<Reservation>(res).subjectOf();
61 }
62 
63 void DocumentUtil::setDocumentIds(QVariant &res, const QVariantList &docIds)
64 {
65  JsonLdDocument::writeProperty(res, "subjectOf", docIds);
66 }
bool removeDocumentId(QVariant &res, const QString &id)
Removes the document identifier id from reservation res.
void addData(const char *data, int length)
QByteArray result() const const
void setDocumentIds(QVariant &res, const QVariantList &docIds)
Sets the list of document identifiers for reservation res.
QString idForContent(const QByteArray &data)
Determine a document identifier for the given content.
bool addDocumentId(QVariant &res, const QString &id)
Add a document id to the reservation res.
QByteArray toHex() const const
QString fromLatin1(const char *str, int size)
static void writeProperty(QVariant &obj, const char *1, const QVariant &value)
Set property name on object obj to value value.
QVariantList documentIds(const QVariant &res)
Returns all document identifiers associcated with reservation res.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Aug 16 2022 03:54:13 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.