Kgapi

change.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 "change.h"
8#include "file_p.h"
9#include "utils_p.h"
10
11#include <QJsonDocument>
12#include <QVariantMap>
13
14using namespace KGAPI2;
15using namespace KGAPI2::Drive;
16
17class Q_DECL_HIDDEN Change::Private
18{
19public:
20 Private();
21 Private(const Private &other);
22
23 qlonglong id = -1;
24 QString fileId;
25 QUrl selfLink;
26 bool deleted = false;
27 FilePtr file;
28
29 static ChangePtr fromJSON(const QVariantMap &map);
30};
31
32Change::Private::Private()
33{
34}
35
36Change::Private::Private(const Private &other)
37 : id(other.id)
38 , fileId(other.fileId)
39 , selfLink(other.selfLink)
40 , deleted(other.deleted)
41 , file(other.file)
42{
43}
44
45ChangePtr Change::Private::fromJSON(const QVariantMap &map)
46{
47 if (!map.contains(QLatin1StringView("kind")) || map[QStringLiteral("kind")].toString() != QLatin1StringView("drive#change")) {
48 return ChangePtr();
49 }
50
51 ChangePtr change(new Change);
52 change->d->id = map[QStringLiteral("id")].toLongLong();
53 change->d->fileId = map[QStringLiteral("fileId")].toString();
54 change->d->selfLink = map[QStringLiteral("selfLink")].toUrl();
55 change->d->deleted = map[QStringLiteral("deleted")].toBool();
56 change->d->file = File::Private::fromJSON(map[QStringLiteral("file")].toMap());
57
58 return change;
59}
60
61Change::Change()
62 : KGAPI2::Object()
63 , d(new Private)
64{
65}
66
67Change::Change(const Change &other)
68 : KGAPI2::Object(other)
69 , d(new Private(*(other.d)))
70{
71}
72
73Change::~Change()
74{
75 delete d;
76}
77
78bool Change::operator==(const Change &other) const
79{
80 if (!Object::operator==(other)) {
81 return false;
82 }
83
84 GAPI_COMPARE(id)
85 GAPI_COMPARE(fileId)
86 GAPI_COMPARE(selfLink)
87 GAPI_COMPARE(deleted)
88 GAPI_COMPARE_SHAREDPTRS(file)
89 return true;
90}
91
92qlonglong Change::id() const
93{
94 return d->id;
95}
96
98{
99 return d->fileId;
100}
101
103{
104 return d->selfLink;
105}
106
107bool Change::deleted() const
108{
109 return d->deleted;
110}
111
113{
114 return d->file;
115}
116
117ChangePtr Change::fromJSON(const QByteArray &jsonData)
118{
119 QJsonDocument document = QJsonDocument::fromJson(jsonData);
120 if (document.isNull()) {
121 return ChangePtr();
122 }
123
124 const QVariant data = document.toVariant();
125 return Private::fromJSON(data.toMap());
126}
127
128ChangesList Change::fromJSONFeed(const QByteArray &jsonData, FeedData &feedData)
129{
130 QJsonDocument document = QJsonDocument::fromJson(jsonData);
131 if (document.isNull()) {
132 return ChangesList();
133 }
134
135 const QVariant data = document.toVariant();
136 const QVariantMap map = data.toMap();
137 if (!map.contains(QLatin1StringView("kind")) || map[QStringLiteral("kind")].toString() != QLatin1StringView("drive#changeList")) {
138 return ChangesList();
139 }
140
141 if (map.contains(QLatin1StringView("nextLink"))) {
142 feedData.nextPageUrl = map[QStringLiteral("nextLink")].toUrl();
143 }
144
146 const QVariantList items = map[QStringLiteral("items")].toList();
147 for (const QVariant &item : items) {
148 const ChangePtr change = Private::fromJSON(item.toMap());
149
150 if (!change.isNull()) {
151 list << change;
152 }
153 }
154
155 return list;
156}
Change contains the representation of a change to a file.
Definition change.h:33
QString fileId() const
Returns the id of the file associated with this change.
Definition change.cpp:97
bool deleted() const
Returns whether this file has been deleted.
Definition change.cpp:107
qlonglong id() const
Returns the id of the change.
Definition change.cpp:92
FilePtr file() const
Returns the updated state of the file.
Definition change.cpp:112
QUrl selfLink() const
Returns a link back to this change.
Definition change.cpp:102
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)
const QVariantMap toMap(const MODEL &model)
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)
bool isNull() 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 Mon Nov 4 2024 16:36:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.