Kgapi

blog.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 "blog.h"
8
9#include <QJsonDocument>
10
11using namespace KGAPI2::Blogger;
12
13class Q_DECL_HIDDEN Blog::Private
14{
15public:
16 Private();
17
18 static BlogPtr fromJSON(const QVariant &json);
19
20 QString id;
22 QString description;
23 QDateTime published;
24 QDateTime updated;
25 QUrl url;
26 uint postsCount = 0;
27 uint pagesCount = 0;
28 QString language;
29 QString languageVariant;
31 QVariant customMetaData;
32};
33
34Blog::Private::Private()
35{
36}
37
38Blog::Blog()
39 : Object()
40 , d(new Private)
41{
42}
43
44Blog::~Blog()
45{
46 delete d;
47}
48
49QString Blog::id() const
50{
51 return d->id;
52}
53
54QString Blog::name() const
55{
56 return d->name;
57}
58
59QString Blog::description() const
60{
61 return d->description;
62}
63
64QDateTime Blog::published() const
65{
66 return d->published;
67}
68
69QDateTime Blog::updated() const
70{
71 return d->updated;
72}
73
74QUrl Blog::url() const
75{
76 return d->url;
77}
78
79uint Blog::postsCount() const
80{
81 return d->postsCount;
82}
83
84uint Blog::pagesCount() const
85{
86 return d->pagesCount;
87}
88
89QString Blog::language() const
90{
91 return d->language;
92}
93
94QString Blog::country() const
95{
96 return d->country;
97}
98
99QString Blog::languageVariant() const
100{
101 return d->languageVariant;
102}
103
104QVariant Blog::customMetaData() const
105{
106 return d->customMetaData;
107}
108
109BlogPtr Blog::Private::fromJSON(const QVariant &json)
110{
111 BlogPtr blog(new Blog);
112 const QVariantMap map = json.toMap();
113 blog->d->id = map[QStringLiteral("id")].toString();
114 blog->d->name = map[QStringLiteral("name")].toString();
115 blog->d->description = map[QStringLiteral("description")].toString();
116 blog->d->published = QDateTime::fromString(map[QStringLiteral("published")].toString(), Qt::ISODate);
117 blog->d->updated = QDateTime::fromString(map[QStringLiteral("updated")].toString(), Qt::ISODate);
118 blog->d->url = map[QStringLiteral("url")].toUrl();
119 blog->d->postsCount = map[QStringLiteral("posts")].toMap()[QStringLiteral("totalItems")].toUInt();
120 blog->d->pagesCount = map[QStringLiteral("pages")].toMap()[QStringLiteral("totalItems")].toUInt();
121 const QVariantMap locale = map[QStringLiteral("locale")].toMap();
122 blog->d->language = locale[QStringLiteral("language")].toString();
123 blog->d->country = locale[QStringLiteral("country")].toString();
124 blog->d->languageVariant = locale[QStringLiteral("variant")].toString();
125 const QString metadata = map[QStringLiteral("customMetaData")].toString();
126 QJsonDocument document = QJsonDocument::fromJson(metadata.toUtf8());
127 blog->d->customMetaData = document.toVariant();
128
129 return blog;
130}
131
132BlogPtr Blog::fromJSON(const QByteArray &rawData)
133{
134 QJsonDocument document = QJsonDocument::fromJson(rawData);
135 if (document.isNull()) {
136 return BlogPtr();
137 }
138
139 const QVariant json = document.toVariant();
140 const QVariantMap map = json.toMap();
141 if (map[QStringLiteral("kind")].toString() != QLatin1StringView("blogger#blog")) {
142 return BlogPtr();
143 }
144
145 return Private::fromJSON(map);
146}
147
148BlogsList Blog::fromJSONFeed(const QByteArray &rawData)
149{
150 QJsonDocument document = QJsonDocument::fromJson(rawData);
151 if (document.isNull()) {
152 return BlogsList();
153 }
154
155 const QVariant json = document.toVariant();
156 const QVariantMap map = json.toMap();
157 if (map[QStringLiteral("kind")].toString() != QLatin1StringView("blogger#blogList")) {
158 return BlogsList();
159 }
160
161 BlogsList items;
162 const QVariantList blogs = map[QStringLiteral("items")].toList();
163 items.reserve(blogs.size());
164 for (const QVariant &blog : blogs) {
165 items << Blog::Private::fromJSON(blog);
166 }
167 return items;
168}
Base class for all objects.
Definition object.h:31
char * toString(const EngineQuery &query)
QString name(StandardAction id)
KI18NLOCALEDATA_EXPORT KCountry country(const char *ianaId)
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)
QByteArray toUtf8() 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 Sat Dec 21 2024 17:00:11 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.