Kgapi

parentreference.cpp
1 /*
2  Copyright 2012 Andrius da Costa Ribas <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) version 3, or any
8  later version accepted by the membership of KDE e.V. (or its
9  successor approved by the membership of KDE e.V.), which shall
10  act as a proxy defined in Section 6 of version 3 of the license.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library. If not, see <https://www.gnu.org/licenses/>.
19 */
20 
21 #include "parentreference.h"
22 #include "parentreference_p.h"
23 #include "utils_p.h"
24 
25 #include <QVariantMap>
26 #include <QJsonDocument>
27 
28 using namespace KGAPI2;
29 using namespace KGAPI2::Drive;
30 
31 ParentReference::Private::Private():
32  isRoot(false)
33 {
34 }
35 
36 ParentReference::Private::Private(const Private &other):
37  id(other.id),
38  selfLink(other.selfLink),
39  parentLink(other.parentLink),
40  isRoot(other.isRoot)
41 {
42 }
43 
44 ParentReferencePtr ParentReference::Private::fromJSON(const QVariantMap &map)
45 {
46  if (!map.contains(QLatin1String("kind")) ||
47  map[QStringLiteral("kind")].toString() != QLatin1String("drive#parentReference")) {
48  return ParentReferencePtr();
49  }
50 
51  ParentReferencePtr reference(new ParentReference(map[QStringLiteral("id")].toString()));
52  reference->d->selfLink = map[QStringLiteral("selfLink")].toUrl();
53  reference->d->parentLink = map[QStringLiteral("parentLink")].toUrl();
54  reference->d->isRoot = map[QStringLiteral("isRoot")].toBool();
55 
56  return reference;
57 }
58 
59 QVariantMap ParentReference::Private::toJSON(const ParentReferencePtr &reference)
60 {
61  QVariantMap map;
62  if (!reference->d->id.isEmpty()) {
63  map[QStringLiteral("id")] = reference->id();
64  }
65  if (!reference->d->selfLink.isEmpty()) {
66  map[QStringLiteral("selfLink")] = reference->d->selfLink;
67  }
68  if (!reference->d->parentLink.isEmpty()) {
69  map[QStringLiteral("parentLink")] = reference->d->parentLink;
70  }
71  if (reference->d->isRoot) { // default is false
72  map[QStringLiteral("isRoot")] = reference->d->isRoot;
73  }
74 
75  return map;
76 }
77 
78 ParentReference::ParentReference(const QString &id):
79  KGAPI2::Object(),
80  d(new Private)
81 {
82  d->id = id;
83 }
84 
85 ParentReference::ParentReference(const ParentReference &other):
86  KGAPI2::Object(other),
87  d(new Private(*(other.d)))
88 {
89 }
90 
91 ParentReference::~ParentReference()
92 {
93  delete d;
94 }
95 
96 bool ParentReference::operator==(const ParentReference &other) const
97 {
98  if (!Object::operator==(other)) {
99  return false;
100  }
101  GAPI_COMPARE(id)
102  GAPI_COMPARE(selfLink)
103  GAPI_COMPARE(parentLink)
104  GAPI_COMPARE(isRoot)
105  return true;
106 }
107 
109 {
110  return d->id;
111 }
112 
113 QUrl ParentReference::selfLink() const
114 {
115  return d->selfLink;
116 }
117 
118 QUrl ParentReference::parentLink() const
119 {
120  return d->parentLink;
121 }
122 
123 bool ParentReference::isRoot() const
124 {
125  return d->isRoot;
126 }
127 
128 ParentReferencePtr ParentReference::fromJSON(const QByteArray &jsonData)
129 {
130  QJsonDocument document = QJsonDocument::fromJson(jsonData);
131  if (document.isNull()) {
132  return ParentReferencePtr();
133  }
134 
135  const QVariant data = document.toVariant();
136  return Private::fromJSON(data.toMap());
137 }
138 
139 ParentReferencesList ParentReference::fromJSONFeed(const QByteArray &jsonData)
140 {
141  QJsonDocument document = QJsonDocument::fromJson(jsonData);
142  if (document.isNull()) {
143  return ParentReferencesList();
144  }
145 
146  const QVariant data = document.toVariant();
147  const QVariantMap map = data.toMap();
148  if (!map.contains(QLatin1String("kind")) ||
149  map[QStringLiteral("kind")].toString() != QLatin1String("drive#parentList")) {
150  return ParentReferencesList();
151  }
152 
154  const QVariantList items = map[QStringLiteral("items")].toList();
155  for (const QVariant & item : items) {
156  const ParentReferencePtr reference = Private::fromJSON(item.toMap());
157 
158  if (!reference.isNull()) {
159  list << reference;
160  }
161  }
162 
163  return list;
164 }
165 
166 QByteArray ParentReference::toJSON(const ParentReferencePtr &reference)
167 {
168  const QVariantMap map = Private::toJSON(reference);
169 
171  return document.toJson(QJsonDocument::Compact);
172 }
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
ParentReference contains a reference to a file&#39;s parent.
QByteArray toJson() const const
Base class for all objects.
Definition: object.h:45
QJsonDocument fromVariant(const QVariant &variant)
bool isNull() const const
A job to fetch a single map tile described by a StaticMapUrl.
Definition: blog.h:30
QVariant toVariant() const const
QMap< QString, QVariant > toMap() const const
char * toString(const T &value)
bool isNull() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun May 24 2020 22:39:01 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.