File indexing completed on 2024-05-12 05:22:12
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 "postfetchjob.h" 0008 #include "bloggerservice.h" 0009 #include "post.h" 0010 #include "utils.h" 0011 0012 #include <QNetworkReply> 0013 #include <QNetworkRequest> 0014 #include <QUrlQuery> 0015 0016 using namespace KGAPI2; 0017 using namespace KGAPI2::Blogger; 0018 0019 class Q_DECL_HIDDEN PostFetchJob::Private 0020 { 0021 public: 0022 Private(const QString &blogId, const QString &postId, PostFetchJob *parent); 0023 0024 QString blogId; 0025 QString postId; 0026 0027 bool fetchBodies = true; 0028 bool fetchImages = true; 0029 uint maxResults = 0; 0030 QStringList filterLabels; 0031 QDateTime startDate; 0032 QDateTime endDate; 0033 StatusFilters statusFilter = All; 0034 0035 private: 0036 PostFetchJob *const q; 0037 }; 0038 0039 PostFetchJob::Private::Private(const QString &blogId_, const QString &postId_, PostFetchJob *parent) 0040 : blogId(blogId_) 0041 , postId(postId_) 0042 , q(parent) 0043 { 0044 } 0045 0046 PostFetchJob::PostFetchJob(const QString &blogId, const AccountPtr &account, QObject *parent) 0047 : FetchJob(account, parent) 0048 , d(new Private(blogId, QString(), this)) 0049 { 0050 } 0051 0052 PostFetchJob::PostFetchJob(const QString &blogId, const QString &postId, const AccountPtr &account, QObject *parent) 0053 : FetchJob(account, parent) 0054 , d(new Private(blogId, postId, this)) 0055 { 0056 } 0057 0058 PostFetchJob::~PostFetchJob() 0059 { 0060 delete d; 0061 } 0062 0063 bool PostFetchJob::fetchBodies() const 0064 { 0065 return d->fetchBodies; 0066 } 0067 0068 void PostFetchJob::setFetchBodies(bool fetchBodies) 0069 { 0070 d->fetchBodies = fetchBodies; 0071 } 0072 0073 bool PostFetchJob::fetchImages() const 0074 { 0075 return d->fetchImages; 0076 } 0077 0078 void PostFetchJob::setFetchImages(bool fetchImages) 0079 { 0080 d->fetchImages = fetchImages; 0081 } 0082 0083 uint PostFetchJob::maxResults() const 0084 { 0085 return d->maxResults; 0086 } 0087 0088 void PostFetchJob::setMaxResults(uint maxResults) 0089 { 0090 d->maxResults = maxResults; 0091 } 0092 0093 QStringList PostFetchJob::filterLabels() const 0094 { 0095 return d->filterLabels; 0096 } 0097 0098 void PostFetchJob::setFilterLabels(const QStringList &labels) 0099 { 0100 d->filterLabels = labels; 0101 } 0102 0103 QDateTime PostFetchJob::startDate() const 0104 { 0105 return d->startDate; 0106 } 0107 0108 void PostFetchJob::setStartDate(const QDateTime &startDate) 0109 { 0110 d->startDate = startDate; 0111 } 0112 0113 QDateTime PostFetchJob::endDate() const 0114 { 0115 return d->endDate; 0116 } 0117 0118 void PostFetchJob::setEndDate(const QDateTime &endDate) 0119 { 0120 d->endDate = endDate; 0121 } 0122 0123 void PostFetchJob::setStatusFilter(PostFetchJob::StatusFilters filter) 0124 { 0125 d->statusFilter = filter; 0126 } 0127 0128 PostFetchJob::StatusFilters PostFetchJob::statusFilter() const 0129 { 0130 return d->statusFilter; 0131 } 0132 0133 void PostFetchJob::start() 0134 { 0135 QUrl url = BloggerService::fetchPostUrl(d->blogId, d->postId); 0136 QUrlQuery query(url); 0137 if (d->postId.isEmpty()) { 0138 if (d->startDate.isValid()) { 0139 query.addQueryItem(QStringLiteral("startDate"), d->startDate.toString(Qt::ISODate)); 0140 } 0141 if (d->endDate.isValid()) { 0142 query.addQueryItem(QStringLiteral("endDate"), d->endDate.toString(Qt::ISODate)); 0143 } 0144 if (d->maxResults > 0) { 0145 query.addQueryItem(QStringLiteral("maxResults"), QString::number(d->maxResults)); 0146 } 0147 if (!d->filterLabels.isEmpty()) { 0148 query.addQueryItem(QStringLiteral("labels"), d->filterLabels.join(QLatin1Char(','))); 0149 } 0150 query.addQueryItem(QStringLiteral("fetchBodies"), Utils::bool2Str(d->fetchBodies)); 0151 query.addQueryItem(QStringLiteral("fetchImages"), Utils::bool2Str(d->fetchImages)); 0152 } 0153 if (account()) { 0154 query.addQueryItem(QStringLiteral("view"), QStringLiteral("ADMIN")); 0155 } 0156 if (d->statusFilter & Draft) { 0157 query.addQueryItem(QStringLiteral("status"), QStringLiteral("draft")); 0158 } 0159 if (d->statusFilter & Live) { 0160 query.addQueryItem(QStringLiteral("status"), QStringLiteral("live")); 0161 } 0162 if (d->statusFilter & Scheduled) { 0163 query.addQueryItem(QStringLiteral("status"), QStringLiteral("scheduled")); 0164 } 0165 url.setQuery(query); 0166 const QNetworkRequest request(url); 0167 enqueueRequest(request); 0168 } 0169 0170 ObjectsList PostFetchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) 0171 { 0172 FeedData feedData; 0173 feedData.requestUrl = reply->request().url(); 0174 0175 ObjectsList items; 0176 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString(); 0177 ContentType ct = Utils::stringToContentType(contentType); 0178 if (ct == KGAPI2::JSON) { 0179 if (d->postId.isEmpty()) { 0180 items = Post::fromJSONFeed(rawData, feedData); 0181 } else { 0182 items << Post::fromJSON(rawData); 0183 } 0184 } else { 0185 setError(KGAPI2::InvalidResponse); 0186 setErrorString(tr("Invalid response content type")); 0187 emitFinished(); 0188 return items; 0189 } 0190 0191 if (feedData.nextPageUrl.isValid()) { 0192 const QNetworkRequest request(feedData.nextPageUrl); 0193 enqueueRequest(request); 0194 } else { 0195 emitFinished(); 0196 } 0197 0198 return items; 0199 } 0200 0201 #include "moc_postfetchjob.cpp"