Kgapi

page.cpp
1/*
2 * SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6
7#include "page.h"
8
9#include <QJsonDocument>
10#include <QVariant>
11
12using namespace KGAPI2;
13using namespace KGAPI2::Blogger;
14
15class Q_DECL_HIDDEN Page::Private
16{
17public:
18 Private();
19 ~Private();
20
21 static PagePtr fromJSON(const QVariant &json);
22 static QVariant toJSON(const PagePtr &page);
23
24 QString id;
25 QString blogId;
26 QDateTime published;
27 QDateTime updated;
28 QUrl url;
29 QString title;
30 QString content;
31 QString authorId;
32 QString authorName;
33 QUrl authorUrl;
34 QUrl authorImageUrl;
35 Status status;
36};
37
38Page::Private::Private()
39 : status(Page::UnknownStatus)
40{
41}
42
43Page::Private::~Private()
44{
45}
46
47Page::Page()
48 : Object()
49 , d(new Private)
50{
51}
52
53Page::~Page()
54{
55 delete d;
56}
57
58QString Page::id() const
59{
60 return d->id;
61}
62
63void Page::setId(const QString &id)
64{
65 d->id = id;
66}
67
68QString Page::blogId() const
69{
70 return d->blogId;
71}
72
73void Page::setBlogId(const QString &blogId)
74{
75 d->blogId = blogId;
76}
77
78QDateTime Page::published() const
79{
80 return d->published;
81}
82
83void Page::setPublished(const QDateTime &published)
84{
85 d->published = published;
86}
87
88QDateTime Page::updated() const
89{
90 return d->updated;
91}
92
93void Page::setUpdated(const QDateTime &updated)
94{
95 d->updated = updated;
96}
97
98QUrl Page::url() const
99{
100 return d->url;
101}
102
103void Page::setUrl(const QUrl &url)
104{
105 d->url = url;
106}
107
108QString Page::title() const
109{
110 return d->title;
111}
112
113void Page::setTitle(const QString &title)
114{
115 d->title = title;
116}
117
119{
120 return d->content;
121}
122
123void Page::setContent(const QString &content)
124{
125 d->content = content;
126}
127
128QString Page::authorId() const
129{
130 return d->authorId;
131}
132
133QString Page::authorName() const
134{
135 return d->authorName;
136}
137
138QUrl Page::authorUrl() const
139{
140 return d->authorUrl;
141}
142
143QUrl Page::authorImageUrl() const
144{
145 return d->authorImageUrl;
146}
147
148Page::Status Page::status() const
149{
150 return d->status;
151}
152
153void Page::setStatus(Page::Status status)
154{
155 d->status = status;
156}
157
158PagePtr Page::Private::fromJSON(const QVariant &json)
159{
160 PagePtr page(new Page);
161 const QVariantMap map = json.toMap();
162
163 page->d->id = map[QStringLiteral("id")].toString();
164 page->d->blogId = map[QStringLiteral("blog")].toMap()[QStringLiteral("id")].toString();
165 page->d->published = QDateTime::fromString(map[QStringLiteral("published")].toString(), Qt::ISODate);
166 page->d->updated = QDateTime::fromString(map[QStringLiteral("updated")].toString(), Qt::ISODate);
167 page->d->url = map[QStringLiteral("url")].toUrl();
168 page->d->title = map[QStringLiteral("title")].toString();
169 page->d->content = map[QStringLiteral("content")].toString();
170 const QVariantMap author = map[QStringLiteral("author")].toMap();
171 page->d->authorId = author[QStringLiteral("id")].toString();
172 page->d->authorName = author[QStringLiteral("displayName")].toString();
173 page->d->authorUrl = author[QStringLiteral("url")].toUrl();
174 page->d->authorImageUrl = author[QStringLiteral("image")].toMap()[QStringLiteral("url")].toUrl();
175
176 const QString status = map[QStringLiteral("status")].toString();
177 if (status == QLatin1StringView("LIVE")) {
178 page->d->status = Live;
179 } else if (status == QLatin1StringView("DRAFT")) {
180 page->d->status = Draft;
181 } else if (status == QLatin1StringView("IMPORTED")) {
182 page->d->status = Imported;
183 } else {
184 page->d->status = UnknownStatus;
185 }
186
187 return page;
188}
189
190QVariant Page::Private::toJSON(const PagePtr &page)
191{
192 QVariantMap map;
193 map[QStringLiteral("kind")] = QLatin1StringView("blogger#page");
194
195 if (!page->d->id.isEmpty()) {
196 map[QStringLiteral("id")] = page->d->id;
197 }
198 map[QStringLiteral("blogId")] = page->d->blogId;
199 if (page->d->published.isValid()) {
200 map[QStringLiteral("published")] = page->d->published.toString(Qt::ISODate);
201 }
202 if (page->d->updated.isValid()) {
203 map[QStringLiteral("updated")] = page->d->updated.toString(Qt::ISODate);
204 }
205 map[QStringLiteral("url")] = page->d->url.toString();
206 map[QStringLiteral("title")] = page->d->title;
207 map[QStringLiteral("content")] = page->d->content;
208 switch (page->d->status) {
209 case Blogger::Page::Draft:
210 map[QStringLiteral("status")] = QLatin1StringView("DRAFT");
211 break;
212 case Blogger::Page::Live:
213 map[QStringLiteral("status")] = QLatin1StringView("LIVE");
214 break;
215 default:
216 break; // not supported for writing
217 }
218
219 return map;
220}
221
222PagePtr Page::fromJSON(const QByteArray &rawData)
223{
224 QJsonDocument document = QJsonDocument::fromJson(rawData);
225 if (document.isNull()) {
226 return PagePtr();
227 }
228
229 const QVariant json = document.toVariant();
230 const QVariantMap map = json.toMap();
231 if (map[QStringLiteral("kind")].toString() != QLatin1StringView("blogger#page")) {
232 return PagePtr();
233 }
234
235 return Private::fromJSON(map);
236}
237
238ObjectsList Page::fromJSONFeed(const QByteArray &rawData)
239{
240 QJsonDocument document = QJsonDocument::fromJson(rawData);
241 if (document.isNull()) {
242 return ObjectsList();
243 }
244 const QVariant json = document.toVariant();
245 const QVariantMap map = json.toMap();
246 if (map[QStringLiteral("kind")].toString() != QLatin1StringView("blogger#pageList")) {
247 return ObjectsList();
248 }
249
251 const QVariantList variantList = map[QStringLiteral("items")].toList();
252 list.reserve(variantList.size());
253 for (const QVariant &item : variantList) {
254 list << Private::fromJSON(item);
255 }
256 return list;
257}
258
259QByteArray Page::toJSON(const PagePtr &page)
260{
261 QJsonDocument document = QJsonDocument::fromVariant(Private::toJSON(page));
262 return document.toJson(QJsonDocument::Compact);
263}
Base class for all objects.
Definition object.h:31
string title
alias content
Q_SCRIPTABLE CaptureState status()
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)
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
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
void reserve(qsizetype size)
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
QString url(FormattingOptions options) const const
QMap< QString, QVariant > toMap() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:54:34 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.