Kgapi

comment.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 "comment.h"
8
9#include <QJsonDocument>
10#include <QUrlQuery>
11#include <QVariant>
12
13using namespace KGAPI2;
14using namespace KGAPI2::Blogger;
15
16class Q_DECL_HIDDEN Comment::Private
17{
18public:
19 Private();
20
21 static CommentPtr fromJSON(const QVariant &json);
22
23 QString id;
24 QString postId;
25 QString blogId;
26 QDateTime published;
27 QDateTime updated;
28 QString content;
29 QString authorId;
30 QString authorName;
31 QUrl authorUrl;
32 QUrl authorImageUrl;
33 QString inReplyTo;
35};
36
37Comment::Private::Private()
38{
39}
40
41Comment::Comment()
42 : Object()
43 , d(new Private)
44{
45}
46
47Comment::~Comment()
48{
49 delete d;
50}
51
52QString Comment::id() const
53{
54 return d->id;
55}
56
57void Comment::setId(const QString &id)
58{
59 d->id = id;
60}
61
62QString Comment::postId() const
63{
64 return d->postId;
65}
66
67void Comment::setPostId(const QString &postId)
68{
69 d->postId = postId;
70}
71
72QString Comment::blogId() const
73{
74 return d->blogId;
75}
76
77void Comment::setBlogId(const QString &blogId)
78{
79 d->blogId = blogId;
80}
81
82QDateTime Comment::published() const
83{
84 return d->published;
85}
86
87void Comment::setPublished(const QDateTime &published)
88{
89 d->published = published;
90}
91
92QDateTime Comment::updated() const
93{
94 return d->updated;
95}
96
97void Comment::setUpdated(const QDateTime &updated)
98{
99 d->updated = updated;
100}
101
102QString Comment::content() const
103{
104 return d->content;
105}
106
107void Comment::setContent(const QString &content)
108{
109 d->content = content;
110}
111
112QString Comment::authorId() const
113{
114 return d->authorId;
115}
116
117void Comment::setAuthorId(const QString &authorId)
118{
119 d->authorId = authorId;
120}
121
122QString Comment::authorName() const
123{
124 return d->authorName;
125}
126
127void Comment::setAuthorName(const QString &authorName)
128{
129 d->authorName = authorName;
130}
131
132QUrl Comment::authorUrl() const
133{
134 return d->authorUrl;
135}
136
137void Comment::setAuthorUrl(const QUrl &url)
138{
139 d->authorUrl = url;
140}
141
142QUrl Comment::authorImageUrl() const
143{
144 return d->authorImageUrl;
145}
146
147void Comment::setAuthorImageUrl(const QUrl &authorImageUrl)
148{
149 d->authorImageUrl = authorImageUrl;
150}
151
152QString Comment::inReplyTo() const
153{
154 return d->inReplyTo;
155}
156
157void Comment::setInReplyTo(const QString &inReplyTo)
158{
159 d->inReplyTo = inReplyTo;
160}
161
162QString Comment::status() const
163{
164 return d->status;
165}
166
167void Comment::setStatus(const QString &status)
168{
169 d->status = status;
170}
171
172CommentPtr Comment::Private::fromJSON(const QVariant &json)
173{
174 CommentPtr comment(new Comment);
175 const QVariantMap map = json.toMap();
176
177 comment->d->id = map[QStringLiteral("id")].toString();
178 comment->d->postId = map[QStringLiteral("post")].toMap()[QStringLiteral("id")].toString();
179 comment->d->blogId = map[QStringLiteral("blog")].toMap()[QStringLiteral("id")].toString();
180 comment->d->published = QDateTime::fromString(map[QStringLiteral("published")].toString(), Qt::ISODate);
181 comment->d->updated = QDateTime::fromString(map[QStringLiteral("updated")].toString(), Qt::ISODate);
182 comment->d->content = map[QStringLiteral("content")].toString();
183 const QVariantMap author = map[QStringLiteral("author")].toMap();
184 comment->d->authorId = author[QStringLiteral("id")].toString();
185 comment->d->authorName = author[QStringLiteral("displayName")].toString();
186 comment->d->authorUrl = author[QStringLiteral("url")].toUrl();
187 comment->d->authorImageUrl = author[QStringLiteral("image")].toMap()[QStringLiteral("url")].toUrl();
188 comment->d->inReplyTo = map[QStringLiteral("inReplyTo")].toMap()[QStringLiteral("id")].toString();
189 comment->d->status = map[QStringLiteral("status")].toString();
190
191 return comment;
192}
193
194CommentPtr Comment::fromJSON(const QByteArray &rawData)
195{
196 QJsonDocument document = QJsonDocument::fromJson(rawData);
197 if (document.isNull()) {
198 return CommentPtr();
199 }
200 const QVariant json = document.toVariant();
201 const QVariantMap map = json.toMap();
202 if (map[QStringLiteral("kind")].toString() != QLatin1StringView("blogger#comment")) {
203 return CommentPtr();
204 }
205
206 return Comment::Private::fromJSON(map);
207}
208
209ObjectsList Comment::fromJSONFeed(const QByteArray &rawData, FeedData &feedData)
210{
211 QJsonDocument document = QJsonDocument::fromJson(rawData);
212 if (document.isNull()) {
213 return ObjectsList();
214 }
215 const QVariant json = document.toVariant();
216 const QVariantMap map = json.toMap();
217 if (map[QStringLiteral("kind")].toString() != QLatin1StringView("blogger#commentList")) {
218 return ObjectsList();
219 }
220
221 ObjectsList items;
222 if (!map[QStringLiteral("nextPageToken")].toString().isEmpty()) {
223 feedData.nextPageUrl = feedData.requestUrl;
224 QUrlQuery query(feedData.nextPageUrl);
225 query.removeQueryItem(QStringLiteral("pageToken"));
226 query.addQueryItem(QStringLiteral("pageToken"), map[QStringLiteral("nextPageToken")].toString());
227 feedData.nextPageUrl.setQuery(query);
228 }
229 const QVariantList variantList = map[QStringLiteral("items")].toList();
230 items.reserve(variantList.size());
231 for (const QVariant &v : variantList) {
232 items << Comment::Private::fromJSON(v);
233 }
234
235 return items;
236}
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
Base class for all objects.
Definition object.h:31
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
NETWORKMANAGERQT_EXPORT NetworkManager::Status status()
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
bool isNull() 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)
QMap< QString, QVariant > toMap() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:59:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.