Kgapi

post.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 "post.h"
8
9#include <QJsonDocument>
10#include <QUrlQuery>
11
12using namespace KGAPI2;
13using namespace KGAPI2::Blogger;
14
15class Q_DECL_HIDDEN Post::Private
16{
17public:
18 Private();
19
20 static PostPtr fromJSON(const QVariant &json);
21 static QVariant toJSON(const PostPtr &post);
22
23 QString id;
24 QString blogId;
25 QDateTime published;
26 QDateTime updated;
27 QUrl url;
28 QString title;
29 QString content;
30 QString authorId;
31 QString authorName;
32 QUrl authorUrl;
33 QUrl authorImageUrl;
34 uint commentsCount = 0;
35 QStringList labels;
36 QVariant customMetaData;
38 double latitude = -1;
39 double longitude = -1;
40 QList<QUrl> images;
42};
43
44Post::Private::Private()
45{
46}
47
48Post::Post()
49 : d(new Private)
50{
51}
52
53Post::~Post()
54{
55 delete d;
56}
57
58QString Post::id()
59{
60 return d->id;
61}
62
63void Post::setId(const QString &id)
64{
65 d->id = id;
66}
67
68QString Post::blogId()
69{
70 return d->blogId;
71}
72
73void Post::setBlogId(const QString &id)
74{
75 d->blogId = id;
76}
77
78QDateTime Post::published() const
79{
80 return d->published;
81}
82
83void Post::setPublished(const QDateTime &published)
84{
85 d->published = published;
86}
87
88QDateTime Post::updated() const
89{
90 return d->updated;
91}
92
93void Post::setUpdated(const QDateTime &updated)
94{
95 d->updated = updated;
96}
97
98QUrl Post::url() const
99{
100 return d->url;
101}
102
103void Post::setUrl(const QUrl &url)
104{
105 d->url = url;
106}
107
108QString Post::title() const
109{
110 return d->title;
111}
112
113void Post::setTitle(const QString &title)
114{
115 d->title = title;
116}
117
118QString Post::content() const
119{
120 return d->content;
121}
122
123void Post::setContent(const QString &content)
124{
125 d->content = content;
126}
127
128QString Post::authorId() const
129{
130 return d->authorId;
131}
132
133QString Post::authorName() const
134{
135 return d->authorName;
136}
137
138QUrl Post::authorUrl() const
139{
140 return d->authorUrl;
141}
142
143QUrl Post::authorImageUrl() const
144{
145 return d->authorImageUrl;
146}
147
148uint Post::commentsCount() const
149{
150 return d->commentsCount;
151}
152
153QStringList Post::labels() const
154{
155 return d->labels;
156}
157
158void Post::setLabels(const QStringList &labels)
159{
160 d->labels = labels;
161}
162
163QVariant Post::customMetaData() const
164{
165 return d->customMetaData;
166}
167
168void Post::setCustomMetaData(const QVariant &metadata)
169{
170 d->customMetaData = metadata;
171}
172
173QString Post::location() const
174{
175 return d->location;
176}
177
178void Post::setLocation(const QString &location)
179{
180 d->location = location;
181}
182
183double Post::latitude() const
184{
185 return d->latitude;
186}
187
188void Post::setLatitude(double lat)
189{
190 d->latitude = lat;
191}
192
193double Post::longitude() const
194{
195 return d->longitude;
196}
197
198void Post::setLongitute(double lng)
199{
200 d->longitude = lng;
201}
202
203QList<QUrl> Post::images() const
204{
205 return d->images;
206}
207
208void Post::setImages(const QList<QUrl> &images)
209{
210 d->images = images;
211}
212
213QString Post::status() const
214{
215 return d->status;
216}
217
218PostPtr Post::Private::fromJSON(const QVariant &json)
219{
220 PostPtr post(new Post);
221 const QVariantMap map = json.toMap();
222
223 post->d->id = map[QStringLiteral("id")].toString();
224 post->d->blogId = map[QStringLiteral("blog")].toMap()[QStringLiteral("id")].toString();
225 post->d->published = QDateTime::fromString(map[QStringLiteral("published")].toString(), Qt::ISODate);
226 post->d->updated = QDateTime::fromString(map[QStringLiteral("updated")].toString(), Qt::ISODate);
227 post->d->url = map[QStringLiteral("url")].toUrl();
228 post->d->title = map[QStringLiteral("title")].toString();
229 post->d->content = map[QStringLiteral("content")].toString();
230 const QVariantMap author = map[QStringLiteral("author")].toMap();
231 post->d->authorId = author[QStringLiteral("id")].toString();
232 post->d->authorName = author[QStringLiteral("displayName")].toString();
233 post->d->authorUrl = author[QStringLiteral("url")].toUrl();
234 post->d->authorImageUrl = author[QStringLiteral("image")].toMap()[QStringLiteral("url")].toUrl();
235 post->d->commentsCount = map[QStringLiteral("replies")].toMap()[QStringLiteral("totalItems")].toUInt();
236 post->d->labels = map[QStringLiteral("labels")].toStringList();
237 post->d->customMetaData = map[QStringLiteral("customMetaData")];
238 const QVariantMap location = map[QStringLiteral("location")].toMap();
239 post->d->location = location[QStringLiteral("name")].toString();
240 post->d->latitude = location[QStringLiteral("lat")].toDouble();
241 post->d->longitude = location[QStringLiteral("lng")].toDouble();
242
243 const QVariantList variantList = map[QStringLiteral("images")].toList();
244 for (const QVariant &url : variantList) {
245 post->d->images << url.toMap()[QStringLiteral("url")].toUrl();
246 }
247 post->d->status = map[QStringLiteral("status")].toString();
248
249 return post;
250}
251
252QVariant Post::Private::toJSON(const PostPtr &post)
253{
254 QVariantMap json;
255 json[QStringLiteral("kind")] = QStringLiteral("blogger#post");
256 if (!post->d->id.isEmpty()) {
257 json[QStringLiteral("id")] = post->d->id;
258 }
259 if (!post->d->blogId.isEmpty()) {
260 QVariantMap blog;
261 blog[QStringLiteral("id")] = post->d->blogId;
262 json[QStringLiteral("blog")] = blog;
263 }
264
265 if (post->d->published.isValid()) {
266 json[QStringLiteral("published")] = post->d->published.toString(Qt::ISODate);
267 }
268 if (post->d->updated.isValid()) {
269 json[QStringLiteral("updated")] = post->d->updated.toString(Qt::ISODate);
270 }
271 json[QStringLiteral("title")] = post->d->title;
272 json[QStringLiteral("content")] = post->d->content;
273 if (!post->d->labels.isEmpty()) {
274 json[QStringLiteral("labels")] = post->d->labels;
275 }
276 if (!post->d->customMetaData.isNull()) {
277 QJsonDocument document = QJsonDocument::fromVariant(post->d->customMetaData);
278 json[QStringLiteral("customMetaData")] = document.toJson(QJsonDocument::Compact);
279 }
280 if (!post->d->location.isEmpty() && post->d->latitude > -1 && post->d->longitude > -1) {
281 QVariantMap location;
282 location[QStringLiteral("name")] = post->d->location;
283 location[QStringLiteral("lat")] = post->d->latitude;
284 location[QStringLiteral("lng")] = post->d->longitude;
285 json[QStringLiteral("location")] = location;
286 }
287 if (!post->d->images.isEmpty()) {
288 QVariantList images;
289 for (const QUrl &url : std::as_const(post->d->images)) {
290 QVariantMap image;
291 image[QStringLiteral("url")] = url.toString();
292 images << image;
293 }
294 json[QStringLiteral("images")] = images;
295 }
296
297 return json;
298}
299
300PostPtr Post::fromJSON(const QByteArray &rawData)
301{
302 QJsonDocument document = QJsonDocument::fromJson(rawData);
303 if (document.isNull()) {
304 return PostPtr();
305 }
306
307 const QVariant json = document.toVariant();
308 const QVariantMap map = json.toMap();
309 if (map[QStringLiteral("kind")].toString() != QLatin1StringView("blogger#post")) {
310 return PostPtr();
311 }
312
313 return Private::fromJSON(map);
314}
315
316ObjectsList Post::fromJSONFeed(const QByteArray &rawData, FeedData &feedData)
317{
318 QJsonDocument document = QJsonDocument::fromJson(rawData);
319 if (document.isNull()) {
320 return ObjectsList();
321 }
322
323 const QVariant json = document.toVariant();
324 const QVariantMap map = json.toMap();
325 if (map[QStringLiteral("kind")].toString() != QLatin1StringView("blogger#postList")) {
326 return ObjectsList();
327 }
328
329 if (!map[QStringLiteral("nextPageToken")].toString().isEmpty()) {
330 feedData.nextPageUrl = feedData.requestUrl;
331 QUrlQuery query(feedData.nextPageUrl);
332 query.removeQueryItem(QStringLiteral("pageToken"));
333 query.addQueryItem(QStringLiteral("pageToken"), map[QStringLiteral("nextPageToken")].toString());
334 feedData.nextPageUrl.setQuery(query);
335 }
337 const QVariantList variantList = map[QStringLiteral("items")].toList();
338 list.reserve(variantList.size());
339 for (const QVariant &item : variantList) {
340 list << Private::fromJSON(item);
341 }
342 return list;
343}
344
345QByteArray Post::toJSON(const PostPtr &post)
346{
347 QJsonDocument document = QJsonDocument::fromVariant(Private::toJSON(post));
348 return document.toJson(QJsonDocument::Compact);
349}
Structure to store additional information about a feed.
Definition types.h:24
QUrl requestUrl
Original URL of the request.
Definition types.h:39
QUrl nextPageUrl
Link to next page of feed.
Definition types.h:38
Q_SCRIPTABLE CaptureState status()
char * toString(const EngineQuery &query)
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
QVariant location(const QVariant &res)
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)
void setQuery(const QString &query, ParsingMode mode)
double toDouble(bool *ok) const const
QMap< QString, QVariant > toMap() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.