Kgapi

commentfetchjob.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 "commentfetchjob.h"
8#include "bloggerservice.h"
9#include "comment.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 CommentFetchJob::Private
20{
21public:
22 Private(const QString &blogId, const QString &postId, const QString &commentId, CommentFetchJob *parent);
23 ~Private();
24
25 const QString blogId;
26 const QString postId;
27 const QString commentId;
28 uint maxResults;
29 QDateTime startDate;
30 QDateTime endDate;
31 bool fetchBodies;
32
33private:
34 CommentFetchJob *const q;
35};
36
37CommentFetchJob::Private::Private(const QString &blogId_, const QString &postId_, const QString &commentId_, CommentFetchJob *parent)
38 : blogId(blogId_)
39 , postId(postId_)
40 , commentId(commentId_)
41 , maxResults(0)
42 , fetchBodies(true)
43 , q(parent)
44{
45}
46
47CommentFetchJob::Private::~Private()
48{
49}
50
51CommentFetchJob::CommentFetchJob(const QString &blogId, const AccountPtr &account, QObject *parent)
53 , d(new Private(blogId, QString(), QString(), this))
54{
55}
56
57CommentFetchJob::CommentFetchJob(const QString &blogId, const QString &postId, const AccountPtr &account, QObject *parent)
58 : FetchJob(account, parent)
59 , d(new Private(blogId, postId, QString(), this))
60{
61}
62
63CommentFetchJob::CommentFetchJob(const QString &blogId, const QString &postId, const QString &commentId, const AccountPtr &account, QObject *parent)
64 : FetchJob(account, parent)
65 , d(new Private(blogId, postId, commentId, this))
66{
67}
68
69CommentFetchJob::~CommentFetchJob()
70{
71 delete d;
72}
73
74QDateTime CommentFetchJob::endDate() const
75{
76 return d->endDate;
77}
78
79void CommentFetchJob::setEndDate(const QDateTime &endDate)
80{
81 d->endDate = endDate;
82}
83
84QDateTime CommentFetchJob::startDate() const
85{
86 return d->startDate;
87}
88
89void CommentFetchJob::setStartDate(const QDateTime &startDate)
90{
91 d->startDate = startDate;
92}
93
94uint CommentFetchJob::maxResults() const
95{
96 return d->maxResults;
97}
98
99void CommentFetchJob::setMaxResults(uint maxResults)
100{
101 d->maxResults = maxResults;
102}
103
104bool CommentFetchJob::fetchBodies() const
105{
106 return d->fetchBodies;
107}
108
109void CommentFetchJob::setFetchBodies(bool fetchBodies)
110{
111 d->fetchBodies = fetchBodies;
112}
113
114void CommentFetchJob::start()
115{
116 QUrl url = BloggerService::fetchCommentsUrl(d->blogId, d->postId, d->commentId);
117 QUrlQuery query(url);
118 if (d->startDate.isValid()) {
119 query.addQueryItem(QStringLiteral("startDate"), d->startDate.toString(Qt::ISODate));
120 }
121 if (d->endDate.isValid()) {
122 query.addQueryItem(QStringLiteral("endDate"), d->endDate.toString(Qt::ISODate));
123 }
124 if (d->maxResults > 0) {
125 query.addQueryItem(QStringLiteral("maxResults"), QString::number(d->maxResults));
126 }
127 query.addQueryItem(QStringLiteral("fetchBodies"), Utils::bool2Str(d->fetchBodies));
128 if (account()) {
129 query.addQueryItem(QStringLiteral("view"), QStringLiteral("ADMIN"));
130 }
131 url.setQuery(query);
132
133 const QNetworkRequest request(url);
134 enqueueRequest(request);
135}
136
137ObjectsList CommentFetchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
138{
139 FeedData feedData;
140 feedData.requestUrl = reply->request().url();
141
143
144 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
145 ContentType ct = Utils::stringToContentType(contentType);
146 if (ct == KGAPI2::JSON) {
147 if (d->commentId.isEmpty()) {
148 items = Comment::fromJSONFeed(rawData, feedData);
149 } else {
150 items << Comment::fromJSON(rawData);
151 }
152 } else {
154 setErrorString(tr("Invalid response content type"));
155 emitFinished();
156 return items;
157 }
158
159 if (feedData.nextPageUrl.isValid()) {
160 const QNetworkRequest request(feedData.nextPageUrl);
161 enqueueRequest(request);
162 } else {
163 emitFinished();
164 }
165
166 return items;
167}
168
169#include "moc_commentfetchjob.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
QVariant header(QNetworkRequest::KnownHeaders header) const const
QNetworkRequest request() const const
QUrl url() const const
QObject * parent() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
bool isEmpty() const const
QString number(double n, char format, int precision)
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 Tue Mar 26 2024 11:19:51 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.