File indexing completed on 2024-05-12 05:22:11

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "post.h"
0008 
0009 #include <QJsonDocument>
0010 #include <QUrlQuery>
0011 
0012 using namespace KGAPI2;
0013 using namespace KGAPI2::Blogger;
0014 
0015 class Q_DECL_HIDDEN Post::Private
0016 {
0017 public:
0018     Private();
0019 
0020     static PostPtr fromJSON(const QVariant &json);
0021     static QVariant toJSON(const PostPtr &post);
0022 
0023     QString id;
0024     QString blogId;
0025     QDateTime published;
0026     QDateTime updated;
0027     QUrl url;
0028     QString title;
0029     QString content;
0030     QString authorId;
0031     QString authorName;
0032     QUrl authorUrl;
0033     QUrl authorImageUrl;
0034     uint commentsCount = 0;
0035     QStringList labels;
0036     QVariant customMetaData;
0037     QString location;
0038     double latitude = -1;
0039     double longitude = -1;
0040     QList<QUrl> images;
0041     QString status;
0042 };
0043 
0044 Post::Private::Private()
0045 {
0046 }
0047 
0048 Post::Post()
0049     : d(new Private)
0050 {
0051 }
0052 
0053 Post::~Post()
0054 {
0055     delete d;
0056 }
0057 
0058 QString Post::id()
0059 {
0060     return d->id;
0061 }
0062 
0063 void Post::setId(const QString &id)
0064 {
0065     d->id = id;
0066 }
0067 
0068 QString Post::blogId()
0069 {
0070     return d->blogId;
0071 }
0072 
0073 void Post::setBlogId(const QString &id)
0074 {
0075     d->blogId = id;
0076 }
0077 
0078 QDateTime Post::published() const
0079 {
0080     return d->published;
0081 }
0082 
0083 void Post::setPublished(const QDateTime &published)
0084 {
0085     d->published = published;
0086 }
0087 
0088 QDateTime Post::updated() const
0089 {
0090     return d->updated;
0091 }
0092 
0093 void Post::setUpdated(const QDateTime &updated)
0094 {
0095     d->updated = updated;
0096 }
0097 
0098 QUrl Post::url() const
0099 {
0100     return d->url;
0101 }
0102 
0103 void Post::setUrl(const QUrl &url)
0104 {
0105     d->url = url;
0106 }
0107 
0108 QString Post::title() const
0109 {
0110     return d->title;
0111 }
0112 
0113 void Post::setTitle(const QString &title)
0114 {
0115     d->title = title;
0116 }
0117 
0118 QString Post::content() const
0119 {
0120     return d->content;
0121 }
0122 
0123 void Post::setContent(const QString &content)
0124 {
0125     d->content = content;
0126 }
0127 
0128 QString Post::authorId() const
0129 {
0130     return d->authorId;
0131 }
0132 
0133 QString Post::authorName() const
0134 {
0135     return d->authorName;
0136 }
0137 
0138 QUrl Post::authorUrl() const
0139 {
0140     return d->authorUrl;
0141 }
0142 
0143 QUrl Post::authorImageUrl() const
0144 {
0145     return d->authorImageUrl;
0146 }
0147 
0148 uint Post::commentsCount() const
0149 {
0150     return d->commentsCount;
0151 }
0152 
0153 QStringList Post::labels() const
0154 {
0155     return d->labels;
0156 }
0157 
0158 void Post::setLabels(const QStringList &labels)
0159 {
0160     d->labels = labels;
0161 }
0162 
0163 QVariant Post::customMetaData() const
0164 {
0165     return d->customMetaData;
0166 }
0167 
0168 void Post::setCustomMetaData(const QVariant &metadata)
0169 {
0170     d->customMetaData = metadata;
0171 }
0172 
0173 QString Post::location() const
0174 {
0175     return d->location;
0176 }
0177 
0178 void Post::setLocation(const QString &location)
0179 {
0180     d->location = location;
0181 }
0182 
0183 double Post::latitude() const
0184 {
0185     return d->latitude;
0186 }
0187 
0188 void Post::setLatitude(double lat)
0189 {
0190     d->latitude = lat;
0191 }
0192 
0193 double Post::longitude() const
0194 {
0195     return d->longitude;
0196 }
0197 
0198 void Post::setLongitute(double lng)
0199 {
0200     d->longitude = lng;
0201 }
0202 
0203 QList<QUrl> Post::images() const
0204 {
0205     return d->images;
0206 }
0207 
0208 void Post::setImages(const QList<QUrl> &images)
0209 {
0210     d->images = images;
0211 }
0212 
0213 QString Post::status() const
0214 {
0215     return d->status;
0216 }
0217 
0218 PostPtr Post::Private::fromJSON(const QVariant &json)
0219 {
0220     PostPtr post(new Post);
0221     const QVariantMap map = json.toMap();
0222 
0223     post->d->id = map[QStringLiteral("id")].toString();
0224     post->d->blogId = map[QStringLiteral("blog")].toMap()[QStringLiteral("id")].toString();
0225     post->d->published = QDateTime::fromString(map[QStringLiteral("published")].toString(), Qt::ISODate);
0226     post->d->updated = QDateTime::fromString(map[QStringLiteral("updated")].toString(), Qt::ISODate);
0227     post->d->url = map[QStringLiteral("url")].toUrl();
0228     post->d->title = map[QStringLiteral("title")].toString();
0229     post->d->content = map[QStringLiteral("content")].toString();
0230     const QVariantMap author = map[QStringLiteral("author")].toMap();
0231     post->d->authorId = author[QStringLiteral("id")].toString();
0232     post->d->authorName = author[QStringLiteral("displayName")].toString();
0233     post->d->authorUrl = author[QStringLiteral("url")].toUrl();
0234     post->d->authorImageUrl = author[QStringLiteral("image")].toMap()[QStringLiteral("url")].toUrl();
0235     post->d->commentsCount = map[QStringLiteral("replies")].toMap()[QStringLiteral("totalItems")].toUInt();
0236     post->d->labels = map[QStringLiteral("labels")].toStringList();
0237     post->d->customMetaData = map[QStringLiteral("customMetaData")];
0238     const QVariantMap location = map[QStringLiteral("location")].toMap();
0239     post->d->location = location[QStringLiteral("name")].toString();
0240     post->d->latitude = location[QStringLiteral("lat")].toDouble();
0241     post->d->longitude = location[QStringLiteral("lng")].toDouble();
0242 
0243     const QVariantList variantList = map[QStringLiteral("images")].toList();
0244     for (const QVariant &url : variantList) {
0245         post->d->images << url.toMap()[QStringLiteral("url")].toUrl();
0246     }
0247     post->d->status = map[QStringLiteral("status")].toString();
0248 
0249     return post;
0250 }
0251 
0252 QVariant Post::Private::toJSON(const PostPtr &post)
0253 {
0254     QVariantMap json;
0255     json[QStringLiteral("kind")] = QStringLiteral("blogger#post");
0256     if (!post->d->id.isEmpty()) {
0257         json[QStringLiteral("id")] = post->d->id;
0258     }
0259     if (!post->d->blogId.isEmpty()) {
0260         QVariantMap blog;
0261         blog[QStringLiteral("id")] = post->d->blogId;
0262         json[QStringLiteral("blog")] = blog;
0263     }
0264 
0265     if (post->d->published.isValid()) {
0266         json[QStringLiteral("published")] = post->d->published.toString(Qt::ISODate);
0267     }
0268     if (post->d->updated.isValid()) {
0269         json[QStringLiteral("updated")] = post->d->updated.toString(Qt::ISODate);
0270     }
0271     json[QStringLiteral("title")] = post->d->title;
0272     json[QStringLiteral("content")] = post->d->content;
0273     if (!post->d->labels.isEmpty()) {
0274         json[QStringLiteral("labels")] = post->d->labels;
0275     }
0276     if (!post->d->customMetaData.isNull()) {
0277         QJsonDocument document = QJsonDocument::fromVariant(post->d->customMetaData);
0278         json[QStringLiteral("customMetaData")] = document.toJson(QJsonDocument::Compact);
0279     }
0280     if (!post->d->location.isEmpty() && post->d->latitude > -1 && post->d->longitude > -1) {
0281         QVariantMap location;
0282         location[QStringLiteral("name")] = post->d->location;
0283         location[QStringLiteral("lat")] = post->d->latitude;
0284         location[QStringLiteral("lng")] = post->d->longitude;
0285         json[QStringLiteral("location")] = location;
0286     }
0287     if (!post->d->images.isEmpty()) {
0288         QVariantList images;
0289         for (const QUrl &url : std::as_const(post->d->images)) {
0290             QVariantMap image;
0291             image[QStringLiteral("url")] = url.toString();
0292             images << image;
0293         }
0294         json[QStringLiteral("images")] = images;
0295     }
0296 
0297     return json;
0298 }
0299 
0300 PostPtr Post::fromJSON(const QByteArray &rawData)
0301 {
0302     QJsonDocument document = QJsonDocument::fromJson(rawData);
0303     if (document.isNull()) {
0304         return PostPtr();
0305     }
0306 
0307     const QVariant json = document.toVariant();
0308     const QVariantMap map = json.toMap();
0309     if (map[QStringLiteral("kind")].toString() != QLatin1StringView("blogger#post")) {
0310         return PostPtr();
0311     }
0312 
0313     return Private::fromJSON(map);
0314 }
0315 
0316 ObjectsList Post::fromJSONFeed(const QByteArray &rawData, FeedData &feedData)
0317 {
0318     QJsonDocument document = QJsonDocument::fromJson(rawData);
0319     if (document.isNull()) {
0320         return ObjectsList();
0321     }
0322 
0323     const QVariant json = document.toVariant();
0324     const QVariantMap map = json.toMap();
0325     if (map[QStringLiteral("kind")].toString() != QLatin1StringView("blogger#postList")) {
0326         return ObjectsList();
0327     }
0328 
0329     if (!map[QStringLiteral("nextPageToken")].toString().isEmpty()) {
0330         feedData.nextPageUrl = feedData.requestUrl;
0331         QUrlQuery query(feedData.nextPageUrl);
0332         query.removeQueryItem(QStringLiteral("pageToken"));
0333         query.addQueryItem(QStringLiteral("pageToken"), map[QStringLiteral("nextPageToken")].toString());
0334         feedData.nextPageUrl.setQuery(query);
0335     }
0336     ObjectsList list;
0337     const QVariantList variantList = map[QStringLiteral("items")].toList();
0338     list.reserve(variantList.size());
0339     for (const QVariant &item : variantList) {
0340         list << Private::fromJSON(item);
0341     }
0342     return list;
0343 }
0344 
0345 QByteArray Post::toJSON(const PostPtr &post)
0346 {
0347     QJsonDocument document = QJsonDocument::fromVariant(Private::toJSON(post));
0348     return document.toJson(QJsonDocument::Compact);
0349 }