Kgapi

parentreference.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 "parentreference.h"
8#include "parentreference_p.h"
9#include "utils_p.h"
10
11#include <QJsonDocument>
12#include <QVariantMap>
13
14using namespace KGAPI2;
15using namespace KGAPI2::Drive;
16
17ParentReference::Private::Private()
18 : isRoot(false)
19{
20}
21
22ParentReference::Private::Private(const Private &other)
23 : id(other.id)
24 , selfLink(other.selfLink)
25 , parentLink(other.parentLink)
26 , isRoot(other.isRoot)
27{
28}
29
30ParentReferencePtr ParentReference::Private::fromJSON(const QVariantMap &map)
31{
32 if (!map.contains(QLatin1StringView("kind")) || map[QStringLiteral("kind")].toString() != QLatin1StringView("drive#parentReference")) {
33 return ParentReferencePtr();
34 }
35
36 ParentReferencePtr reference(new ParentReference(map[QStringLiteral("id")].toString()));
37 reference->d->selfLink = map[QStringLiteral("selfLink")].toUrl();
38 reference->d->parentLink = map[QStringLiteral("parentLink")].toUrl();
39 reference->d->isRoot = map[QStringLiteral("isRoot")].toBool();
40
41 return reference;
42}
43
44QVariantMap ParentReference::Private::toJSON(const ParentReferencePtr &reference)
45{
46 QVariantMap map;
47 if (!reference->d->id.isEmpty()) {
48 map[QStringLiteral("id")] = reference->id();
49 }
50 if (!reference->d->selfLink.isEmpty()) {
51 map[QStringLiteral("selfLink")] = reference->d->selfLink;
52 }
53 if (!reference->d->parentLink.isEmpty()) {
54 map[QStringLiteral("parentLink")] = reference->d->parentLink;
55 }
56 if (reference->d->isRoot) { // default is false
57 map[QStringLiteral("isRoot")] = reference->d->isRoot;
58 }
59
60 return map;
61}
62
63ParentReference::ParentReference(const QString &id)
64 : KGAPI2::Object()
65 , d(new Private)
66{
67 d->id = id;
68}
69
70ParentReference::ParentReference(const ParentReference &other)
71 : KGAPI2::Object(other)
72 , d(new Private(*(other.d)))
73{
74}
75
76ParentReference::~ParentReference()
77{
78 delete d;
79}
80
81bool ParentReference::operator==(const ParentReference &other) const
82{
83 if (!Object::operator==(other)) {
84 return false;
85 }
86 GAPI_COMPARE(id)
87 GAPI_COMPARE(selfLink)
88 GAPI_COMPARE(parentLink)
89 GAPI_COMPARE(isRoot)
90 return true;
91}
92
94{
95 return d->id;
96}
97
99{
100 return d->selfLink;
101}
102
104{
105 return d->parentLink;
106}
107
109{
110 return d->isRoot;
111}
112
113ParentReferencePtr ParentReference::fromJSON(const QByteArray &jsonData)
114{
115 QJsonDocument document = QJsonDocument::fromJson(jsonData);
116 if (document.isNull()) {
117 return ParentReferencePtr();
118 }
119
120 const QVariant data = document.toVariant();
121 return Private::fromJSON(data.toMap());
122}
123
124ParentReferencesList ParentReference::fromJSONFeed(const QByteArray &jsonData)
125{
126 QJsonDocument document = QJsonDocument::fromJson(jsonData);
127 if (document.isNull()) {
128 return ParentReferencesList();
129 }
130
131 const QVariant data = document.toVariant();
132 const QVariantMap map = data.toMap();
133 if (!map.contains(QLatin1StringView("kind")) || map[QStringLiteral("kind")].toString() != QLatin1StringView("drive#parentList")) {
134 return ParentReferencesList();
135 }
136
138 const QVariantList items = map[QStringLiteral("items")].toList();
139 for (const QVariant &item : items) {
140 const ParentReferencePtr reference = Private::fromJSON(item.toMap());
141
142 if (!reference.isNull()) {
143 list << reference;
144 }
145 }
146
147 return list;
148}
149
150QByteArray ParentReference::toJSON(const ParentReferencePtr &reference)
151{
152 const QVariantMap map = Private::toJSON(reference);
153
155 return document.toJson(QJsonDocument::Compact);
156}
ParentReference contains a reference to a file's parent.
QUrl parentLink() const
Returns a link to the parent.
bool isRoot() const
Returns whether or not the parent is the root folder.
QString id() const
Returns the id of the parent.
QUrl selfLink() const
Returns a link back to this reference.
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 Sat Dec 21 2024 17:00:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.