Kgapi

pagefetchjob.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 "pagefetchjob.h"
8#include "bloggerservice.h"
9#include "utils.h"
10
11#include <QNetworkReply>
12#include <QNetworkRequest>
13#include <QUrlQuery>
14
15using namespace KGAPI2;
16using namespace KGAPI2::Blogger;
17
18class Q_DECL_HIDDEN PageFetchJob::Private
19{
20public:
21 Private(const QString &blogId, const QString &pageId);
22
23 const QString blogId;
24 const QString pageId;
25
26 bool fetchContent = true;
27 StatusFilters statusFilter = All;
28};
29
30PageFetchJob::Private::Private(const QString &blogId_, const QString &pageId_)
31 : blogId(blogId_)
32 , pageId(pageId_)
33{
34}
35
36PageFetchJob::PageFetchJob(const QString &blogId, const AccountPtr &account, QObject *parent)
37 : FetchJob(account, parent)
38 , d(new Private(blogId, QString()))
39{
40}
41
42PageFetchJob::PageFetchJob(const QString &blogId, const QString &pageId, const AccountPtr &account, QObject *parent)
43 : FetchJob(account, parent)
44 , d(new Private(blogId, pageId))
45{
46}
47
48PageFetchJob::~PageFetchJob()
49{
50 delete d;
51}
52
53bool PageFetchJob::fetchContent() const
54{
55 return d->fetchContent;
56}
57
58void PageFetchJob::setFetchContent(bool fetchContent)
59{
60 d->fetchContent = fetchContent;
61}
62
63PageFetchJob::StatusFilters PageFetchJob::statusFilter() const
64{
65 return d->statusFilter;
66}
67
68void PageFetchJob::setStatusFilter(StatusFilters status)
69{
70 d->statusFilter = status;
71}
72
73void PageFetchJob::start()
74{
75 QUrl url = BloggerService::fetchPageUrl(d->blogId, d->pageId);
76 QUrlQuery query(url);
77 query.addQueryItem(QStringLiteral("fetchBodies"), Utils::bool2Str(d->fetchContent));
78 if (d->statusFilter & Draft) {
79 query.addQueryItem(QStringLiteral("status"), QStringLiteral("draft"));
80 }
81 if (d->statusFilter & Imported) {
82 query.addQueryItem(QStringLiteral("status"), QStringLiteral("imported"));
83 }
84 if (d->statusFilter & Live) {
85 query.addQueryItem(QStringLiteral("status"), QStringLiteral("live"));
86 }
87 if (account()) {
88 query.addQueryItem(QStringLiteral("view"), QStringLiteral("ADMIN"));
89 }
90 url.setQuery(query);
91 QNetworkRequest request(url);
92 enqueueRequest(request);
93}
94
95ObjectsList PageFetchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
96{
98
99 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
100 ContentType ct = Utils::stringToContentType(contentType);
101 if (ct == KGAPI2::JSON) {
102 if (d->pageId.isEmpty()) {
103 items = Page::fromJSONFeed(rawData);
104 } else {
105 items << Page::fromJSON(rawData);
106 }
107 } else {
109 setErrorString(tr("Invalid response content type"));
110 emitFinished();
111 return items;
112 }
113
114 emitFinished();
115 return items;
116}
117
118#include "moc_pagefetchjob.cpp"
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
Q_SCRIPTABLE CaptureState status()
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
QString tr(const char *sourceText, const char *disambiguation, int n)
bool isEmpty() 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 Nov 29 2024 11:54:34 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.