Kgapi

change.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 "change.h"
22 #include "file_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 class Q_DECL_HIDDEN Change::Private
32 {
33  public:
34  Private();
35  Private(const Private &other);
36 
37  qlonglong id;
38  QString fileId;
39  QUrl selfLink;
40  bool deleted;
41  FilePtr file;
42 
43  static ChangePtr fromJSON(const QVariantMap &map);
44 };
45 
46 Change::Private::Private():
47  id(-1),
48  deleted(false)
49 {
50 }
51 
52 Change::Private::Private(const Private &other):
53  id(other.id),
54  fileId(other.fileId),
55  selfLink(other.selfLink),
56  deleted(other.deleted),
57  file(other.file)
58 {
59 }
60 
61 ChangePtr Change::Private::fromJSON(const QVariantMap &map)
62 {
63  if (!map.contains(QLatin1String("kind")) ||
64  map[QStringLiteral("kind")].toString() != QLatin1String("drive#change")) {
65  return ChangePtr();
66  }
67 
68  ChangePtr change(new Change);
69  change->d->id = map[QStringLiteral("id")].toLongLong();
70  change->d->fileId = map[QStringLiteral("fileId")].toString();
71  change->d->selfLink = map[QStringLiteral("selfLink")].toUrl();
72  change->d->deleted = map[QStringLiteral("deleted")].toBool();
73  change->d->file = File::Private::fromJSON(map[QStringLiteral("file")].toMap());
74 
75  return change;
76 }
77 
78 Change::Change():
79  KGAPI2::Object(),
80  d(new Private)
81 {
82 }
83 
84 Change::Change(const Change &other):
85  KGAPI2::Object(other),
86  d(new Private(*(other.d)))
87 {
88 }
89 
90 Change::~Change()
91 {
92  delete d;
93 }
94 
95 bool Change::operator==(const Change &other) const
96 {
97  if (!Object::operator==(other)) {
98  return false;
99  }
100 
101  GAPI_COMPARE(id)
102  GAPI_COMPARE(fileId)
103  GAPI_COMPARE(selfLink)
104  GAPI_COMPARE(deleted)
105  GAPI_COMPARE_SHAREDPTRS(file)
106  return true;
107 }
108 
109 qlonglong Change::id() const
110 {
111  return d->id;
112 }
113 
114 QString Change::fileId() const
115 {
116  return d->fileId;
117 }
118 
119 QUrl Change::selfLink() const
120 {
121  return d->selfLink;
122 }
123 
124 bool Change::deleted() const
125 {
126  return d->deleted;
127 }
128 
129 FilePtr Change::file() const
130 {
131  return d->file;
132 }
133 
134 ChangePtr Change::fromJSON(const QByteArray &jsonData)
135 {
136  QJsonDocument document = QJsonDocument::fromJson(jsonData);
137  if (document.isNull()) {
138  return ChangePtr();
139  }
140 
141  const QVariant data = document.toVariant();
142  return Private::fromJSON(data.toMap());
143 }
144 
145 ChangesList Change::fromJSONFeed(const QByteArray &jsonData, FeedData &feedData)
146 {
147  QJsonDocument document = QJsonDocument::fromJson(jsonData);
148  if (document.isNull()) {
149  return ChangesList();
150  }
151 
152  const QVariant data = document.toVariant();
153  const QVariantMap map = data.toMap();
154  if (!map.contains(QLatin1String("kind")) ||
155  map[QStringLiteral("kind")].toString() != QLatin1String("drive#changeList")) {
156  return ChangesList();
157  }
158 
159  if (map.contains(QLatin1String("nextLink"))) {
160  feedData.nextPageUrl = map[QStringLiteral("nextLink")].toUrl();
161  }
162 
163  ChangesList list;
164  const QVariantList items = map[QStringLiteral("items")].toList();
165  for (const QVariant & item : items) {
166  const ChangePtr change = Private::fromJSON(item.toMap());
167 
168  if (!change.isNull()) {
169  list << change;
170  }
171  }
172 
173  return list;
174 }
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
Base class for all objects.
Definition: object.h:45
Change contains the representation of a change to a file.
Definition: change.h:47
QUrl nextPageUrl
Link to next page of feed.
Definition: types.h:52
Structure to store additional information about a feed.
Definition: types.h:38
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
bool isNull() const const
qlonglong id() const
Returns the id of the change.
Definition: change.cpp:109
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 30 2020 22:38:42 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.