Kgapi

postfetchjob.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 "postfetchjob.h"
8#include "bloggerservice.h"
9#include "post.h"
10#include "utils.h"
11
12#include <QNetworkReply>
13#include <QNetworkRequest>
14#include <QUrlQuery>
15
16using namespace KGAPI2;
17using namespace KGAPI2::Blogger;
18
19class Q_DECL_HIDDEN PostFetchJob::Private
20{
21public:
22 Private(const QString &blogId, const QString &postId, PostFetchJob *parent);
23
24 QString blogId;
25 QString postId;
26
27 bool fetchBodies = true;
28 bool fetchImages = true;
29 uint maxResults = 0;
30 QStringList filterLabels;
31 QDateTime startDate;
32 QDateTime endDate;
33 StatusFilters statusFilter = All;
34
35private:
36 PostFetchJob *const q;
37};
38
39PostFetchJob::Private::Private(const QString &blogId_, const QString &postId_, PostFetchJob *parent)
40 : blogId(blogId_)
41 , postId(postId_)
42 , q(parent)
43{
44}
45
46PostFetchJob::PostFetchJob(const QString &blogId, const AccountPtr &account, QObject *parent)
47 : FetchJob(account, parent)
48 , d(new Private(blogId, QString(), this))
49{
50}
51
52PostFetchJob::PostFetchJob(const QString &blogId, const QString &postId, const AccountPtr &account, QObject *parent)
53 : FetchJob(account, parent)
54 , d(new Private(blogId, postId, this))
55{
56}
57
58PostFetchJob::~PostFetchJob()
59{
60 delete d;
61}
62
63bool PostFetchJob::fetchBodies() const
64{
65 return d->fetchBodies;
66}
67
68void PostFetchJob::setFetchBodies(bool fetchBodies)
69{
70 d->fetchBodies = fetchBodies;
71}
72
73bool PostFetchJob::fetchImages() const
74{
75 return d->fetchImages;
76}
77
78void PostFetchJob::setFetchImages(bool fetchImages)
79{
80 d->fetchImages = fetchImages;
81}
82
83uint PostFetchJob::maxResults() const
84{
85 return d->maxResults;
86}
87
88void PostFetchJob::setMaxResults(uint maxResults)
89{
90 d->maxResults = maxResults;
91}
92
93QStringList PostFetchJob::filterLabels() const
94{
95 return d->filterLabels;
96}
97
98void PostFetchJob::setFilterLabels(const QStringList &labels)
99{
100 d->filterLabels = labels;
101}
102
103QDateTime PostFetchJob::startDate() const
104{
105 return d->startDate;
106}
107
108void PostFetchJob::setStartDate(const QDateTime &startDate)
109{
110 d->startDate = startDate;
111}
112
113QDateTime PostFetchJob::endDate() const
114{
115 return d->endDate;
116}
117
118void PostFetchJob::setEndDate(const QDateTime &endDate)
119{
120 d->endDate = endDate;
121}
122
123void PostFetchJob::setStatusFilter(PostFetchJob::StatusFilters filter)
124{
125 d->statusFilter = filter;
126}
127
128PostFetchJob::StatusFilters PostFetchJob::statusFilter() const
129{
130 return d->statusFilter;
131}
132
133void PostFetchJob::start()
134{
135 QUrl url = BloggerService::fetchPostUrl(d->blogId, d->postId);
136 QUrlQuery query(url);
137 if (d->postId.isEmpty()) {
138 if (d->startDate.isValid()) {
139 query.addQueryItem(QStringLiteral("startDate"), d->startDate.toString(Qt::ISODate));
140 }
141 if (d->endDate.isValid()) {
142 query.addQueryItem(QStringLiteral("endDate"), d->endDate.toString(Qt::ISODate));
143 }
144 if (d->maxResults > 0) {
145 query.addQueryItem(QStringLiteral("maxResults"), QString::number(d->maxResults));
146 }
147 if (!d->filterLabels.isEmpty()) {
148 query.addQueryItem(QStringLiteral("labels"), d->filterLabels.join(QLatin1Char(',')));
149 }
150 query.addQueryItem(QStringLiteral("fetchBodies"), Utils::bool2Str(d->fetchBodies));
151 query.addQueryItem(QStringLiteral("fetchImages"), Utils::bool2Str(d->fetchImages));
152 }
153 if (account()) {
154 query.addQueryItem(QStringLiteral("view"), QStringLiteral("ADMIN"));
155 }
156 if (d->statusFilter & Draft) {
157 query.addQueryItem(QStringLiteral("status"), QStringLiteral("draft"));
158 }
159 if (d->statusFilter & Live) {
160 query.addQueryItem(QStringLiteral("status"), QStringLiteral("live"));
161 }
162 if (d->statusFilter & Scheduled) {
163 query.addQueryItem(QStringLiteral("status"), QStringLiteral("scheduled"));
164 }
165 url.setQuery(query);
166 const QNetworkRequest request(url);
167 enqueueRequest(request);
168}
169
170ObjectsList PostFetchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
171{
172 FeedData feedData;
173 feedData.requestUrl = reply->request().url();
174
176 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
177 ContentType ct = Utils::stringToContentType(contentType);
178 if (ct == KGAPI2::JSON) {
179 if (d->postId.isEmpty()) {
180 items = Post::fromJSONFeed(rawData, feedData);
181 } else {
182 items << Post::fromJSON(rawData);
183 }
184 } else {
186 setErrorString(tr("Invalid response content type"));
187 emitFinished();
188 return items;
189 }
190
191 if (feedData.nextPageUrl.isValid()) {
192 const QNetworkRequest request(feedData.nextPageUrl);
193 enqueueRequest(request);
194 } else {
195 emitFinished();
196 }
197
198 return items;
199}
200
201#include "moc_postfetchjob.cpp"
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
Abstract superclass for all jobs that fetch resources from Google.
Definition fetchjob.h:25
virtual ObjectsList items() const
Returns all items fetched by this job.
Definition fetchjob.cpp:41
void setErrorString(const QString &errorString)
Set job error description to errorString.
Definition job.cpp:401
AccountPtr account() const
Returns account used to authenticate requests.
Definition job.cpp:436
virtual void emitFinished()
Emits Job::finished() signal.
Definition job.cpp:493
void setError(KGAPI2::Error error)
Set job error to error.
Definition job.cpp:386
virtual void enqueueRequest(const QNetworkRequest &request, const QByteArray &data=QByteArray(), const QString &contentType=QString())
Enqueues request in dispatcher queue.
Definition job.cpp:513
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
@ InvalidResponse
LibKGAPI error - Google returned invalid response.
Definition types.h:183
ContentType
Definition types.h:210
bool isValid() const const
QString toString(QStringView format, QCalendar cal) const const
bool isEmpty() const const
QVariant header(QNetworkRequest::KnownHeaders header) const const
QNetworkRequest request() const const
QUrl url() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
bool isEmpty() const const
QString number(double n, char format, int precision)
QString join(QChar separator) const const
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
bool isValid() const const
void setQuery(const QString &query, ParsingMode mode)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:11:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.