Kgapi

postsearchjob.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 "postsearchjob.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 PostSearchJob::Private
20{
21public:
22 Private(const QString &blogId, const QString &query, PostSearchJob *parent);
23
24 const QString blogId;
25 const QString query;
26 bool fetchBodies = true;
27
28private:
29 PostSearchJob *const q;
30};
31
32PostSearchJob::Private::Private(const QString &blogId_, const QString &query_, PostSearchJob *parent)
33 : blogId(blogId_)
34 , query(query_)
35 , fetchBodies(true)
36 , q(parent)
37{
38}
39
40PostSearchJob::PostSearchJob(const QString &blogId, const QString &query, const AccountPtr &account, QObject *parent)
41 : FetchJob(account, parent)
42 , d(new Private(blogId, query, this))
43{
44}
45
46PostSearchJob::~PostSearchJob()
47{
48 delete d;
49}
50
51bool PostSearchJob::fetchBodies() const
52{
53 return d->fetchBodies;
54}
55
56void PostSearchJob::setFetchBodies(bool fetchBodies)
57{
58 d->fetchBodies = fetchBodies;
59}
60
61void PostSearchJob::start()
62{
63 QUrl url = BloggerService::searchPostUrl(d->blogId);
64 QUrlQuery query(url);
65 query.addQueryItem(QStringLiteral("q"), d->query);
66 query.addQueryItem(QStringLiteral("fetchBodies"), Utils::bool2Str(d->fetchBodies));
67 url.setQuery(query);
68 const QNetworkRequest request(url);
69 enqueueRequest(request);
70}
71
72ObjectsList PostSearchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
73{
74 FeedData feedData;
75 feedData.requestUrl = reply->request().url();
76
78
79 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
80 ContentType ct = Utils::stringToContentType(contentType);
81 if (ct == KGAPI2::JSON) {
82 items = Post::fromJSONFeed(rawData, feedData);
83 } else {
85 setErrorString(tr("Invalid response content type"));
87 return items;
88 }
89
90 if (feedData.nextPageUrl.isValid()) {
91 const QNetworkRequest request(feedData.nextPageUrl);
92 enqueueRequest(request);
93 } else {
95 }
96
97 return items;
98}
99
100#include "moc_postsearchjob.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
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
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 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.