Kgapi

childreference.cpp
1/*
2 SPDX-FileCopyrightText: 2012 Andrius da Costa Ribas <andriusmao@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "childreference.h"
8#include "utils_p.h"
9
10#include <QJsonDocument>
11#include <QVariantMap>
12
13using namespace KGAPI2;
14using namespace KGAPI2::Drive;
15
16class Q_DECL_HIDDEN ChildReference::Private
17{
18public:
19 Private();
20 Private(const Private &other);
21
22 QString id;
23 QUrl selfLink;
24 QUrl childLink;
25
26 static ChildReferencePtr fromJSON(const QVariantMap &map);
27};
28
29ChildReference::Private::Private()
30{
31}
32
33ChildReference::Private::Private(const Private &other)
34 : id(other.id)
35 , selfLink(other.selfLink)
36 , childLink(other.childLink)
37{
38}
39
40ChildReferencePtr ChildReference::Private::fromJSON(const QVariantMap &map)
41{
42 if (!map.contains(QLatin1StringView("kind")) || map[QStringLiteral("kind")].toString() != QLatin1StringView("drive#childReference")) {
43 return ChildReferencePtr();
44 }
45
46 ChildReferencePtr reference(new ChildReference(map[QStringLiteral("id")].toString()));
47 reference->d->selfLink = map[QStringLiteral("selfLink")].toUrl();
48 reference->d->childLink = map[QStringLiteral("childLink")].toUrl();
49
50 return reference;
51}
52
53ChildReference::ChildReference(const QString &id)
54 : KGAPI2::Object()
55 , d(new Private)
56{
57 d->id = id;
58}
59
60ChildReference::ChildReference(const ChildReference &other)
61 : KGAPI2::Object(other)
62 , d(new Private(*(other.d)))
63{
64}
65
66ChildReference::~ChildReference()
67{
68 delete d;
69}
70
71bool ChildReference::operator==(const ChildReference &other) const
72{
73 if (!Object::operator==(other)) {
74 return false;
75 }
76 GAPI_COMPARE(id)
77 GAPI_COMPARE(selfLink)
78 GAPI_COMPARE(childLink)
79 return true;
80}
81
83{
84 return d->id;
85}
86
88{
89 return d->selfLink;
90}
91
93{
94 return d->childLink;
95}
96
97ChildReferencePtr ChildReference::fromJSON(const QByteArray &jsonData)
98{
99 QJsonDocument document = QJsonDocument::fromJson(jsonData);
100 if (document.isNull()) {
101 return ChildReferencePtr();
102 }
103
104 const QVariant data = document.toVariant();
105 return Private::fromJSON(data.toMap());
106}
107
108ChildReferencesList ChildReference::fromJSONFeed(const QByteArray &jsonData, FeedData &feedData)
109{
110 QJsonDocument document = QJsonDocument::fromJson(jsonData);
111 if (document.isNull()) {
112 return ChildReferencesList();
113 }
114
115 const QVariant data = document.toVariant();
116 const QVariantMap map = data.toMap();
117 if (!map.contains(QLatin1StringView("kind")) || map[QStringLiteral("kind")].toString() != QLatin1StringView("drive#childList")) {
118 return ChildReferencesList();
119 }
120
122 const QVariantList items = map[QStringLiteral("items")].toList();
123 for (const QVariant &item : items) {
124 ChildReferencePtr reference = Private::fromJSON(item.toMap());
125
126 if (!reference.isNull()) {
127 list << reference;
128 }
129 }
130
131 if (map.contains(QLatin1StringView("nextLink"))) {
132 feedData.nextPageUrl = map[QStringLiteral("nextLink")].toUrl();
133 }
134
135 return list;
136}
137
138QByteArray ChildReference::toJSON(const ChildReferencePtr &reference)
139{
140 QVariantMap map;
141
142 map[QStringLiteral("id")] = reference->id();
143
145 return document.toJson(QJsonDocument::Compact);
146}
ChildReference contains a reference to a folder's child.
QUrl selfLink() const
Returns a link back to this reference.
QString id() const
Returns the id of the child.
QUrl childLink() const
Returns a link to the child.
Structure to store additional information about a feed.
Definition types.h:24
QUrl nextPageUrl
Link to next page of feed.
Definition types.h:38
Base class for all objects.
Definition object.h:31
char * toString(const EngineQuery &query)
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonDocument fromVariant(const QVariant &variant)
bool isNull() const const
QByteArray toJson(JsonFormat format) const const
QVariant toVariant() const const
bool isNull() const const
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
QMap< QString, QVariant > toMap() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:19:52 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.