Kgapi

childreference.cpp
1 /*
2  SPDX-FileCopyrightText: 2012 Andrius da Costa Ribas <[email protected]>
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 <QVariantMap>
11 #include <QJsonDocument>
12 
13 using namespace KGAPI2;
14 using namespace KGAPI2::Drive;
15 
16 class Q_DECL_HIDDEN ChildReference::Private
17 {
18  public:
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 
29 ChildReference::Private::Private()
30 {
31 }
32 
33 ChildReference::Private::Private(const Private &other):
34  id(other.id),
35  selfLink(other.selfLink),
36  childLink(other.childLink)
37 {
38 }
39 
40 ChildReferencePtr ChildReference::Private::fromJSON(const QVariantMap &map)
41 {
42  if (!map.contains(QLatin1String("kind")) ||
43  map[QStringLiteral("kind")].toString() != QLatin1String("drive#childReference")) {
44  return ChildReferencePtr();
45  }
46 
47  ChildReferencePtr reference(new ChildReference(map[QStringLiteral("id")].toString()));
48  reference->d->selfLink = map[QStringLiteral("selfLink")].toUrl();
49  reference->d->childLink = map[QStringLiteral("childLink")].toUrl();
50 
51  return reference;
52 }
53 
54 ChildReference::ChildReference(const QString &id):
55  KGAPI2::Object(),
56  d(new Private)
57 {
58  d->id = id;
59 }
60 
61 ChildReference::ChildReference(const ChildReference &other):
62  KGAPI2::Object(other),
63  d(new Private(*(other.d)))
64 {
65 }
66 
67 ChildReference::~ChildReference()
68 {
69  delete d;
70 }
71 
72 bool ChildReference::operator==(const ChildReference &other) const
73 {
74  if (!Object::operator==(other)) {
75  return false;
76  }
77  GAPI_COMPARE(id)
78  GAPI_COMPARE(selfLink)
79  GAPI_COMPARE(childLink)
80  return true;
81 }
82 
84 {
85  return d->id;
86 }
87 
89 {
90  return d->selfLink;
91 }
92 
94 {
95  return d->childLink;
96 }
97 
98 ChildReferencePtr ChildReference::fromJSON(const QByteArray &jsonData)
99 {
100  QJsonDocument document = QJsonDocument::fromJson(jsonData);
101  if (document.isNull()) {
102  return ChildReferencePtr();
103  }
104 
105  const QVariant data = document.toVariant();
106  return Private::fromJSON(data.toMap());
107 }
108 
109 ChildReferencesList ChildReference::fromJSONFeed(const QByteArray &jsonData,
110  FeedData &feedData)
111 {
112  QJsonDocument document = QJsonDocument::fromJson(jsonData);
113  if (document.isNull()) {
114  return ChildReferencesList();
115  }
116 
117  const QVariant data = document.toVariant();
118  const QVariantMap map = data.toMap();
119  if (!map.contains(QLatin1String("kind")) ||
120  map[QStringLiteral("kind")].toString() != QLatin1String("drive#childList")) {
121  return ChildReferencesList();
122  }
123 
124  ChildReferencesList list;
125  const QVariantList items = map[QStringLiteral("items")].toList();
126  for (const QVariant & item : items) {
127  ChildReferencePtr reference = Private::fromJSON(item.toMap());
128 
129  if (!reference.isNull()) {
130  list << reference;
131  }
132  }
133 
134  if (map.contains(QLatin1String("nextLink"))) {
135  feedData.nextPageUrl = map[QStringLiteral("nextLink")].toUrl();
136  }
137 
138  return list;
139 }
140 
141 QByteArray ChildReference::toJSON(const ChildReferencePtr &reference)
142 {
143  QVariantMap map;
144 
145  map[QStringLiteral("id")] = reference->id();
146 
148  return document.toJson(QJsonDocument::Compact);
149 }
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QUrl selfLink() const
Returns a link back to this reference.
QByteArray toJson() const const
Base class for all objects.
Definition: object.h:30
QUrl nextPageUrl
Link to next page of feed.
Definition: types.h:37
QJsonDocument fromVariant(const QVariant &variant)
ChildReference contains a reference to a folder&#39;s child.
Structure to store additional information about a feed.
Definition: types.h:23
bool isNull() const const
A job to fetch a single map tile described by a StaticMapUrl.
Definition: blog.h:15
QUrl selfLink() const
Returns a link back to this change.
Definition: change.cpp:103
QVariant toVariant() const const
QMap< QString, QVariant > toMap() const const
char * toString(const T &value)
bool isNull() const const
qlonglong id() const
Returns the id of the change.
Definition: change.cpp:93
QUrl childLink() const
Returns a link to the child.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Jul 29 2021 23:11:32 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.