Kgapi

blogfetchjob.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 "blogfetchjob.h"
8#include "account.h"
9#include "blog.h"
10#include "bloggerservice.h"
11#include "utils.h"
12
13#include <QNetworkReply>
14#include <QNetworkRequest>
15
16using namespace KGAPI2;
17using namespace KGAPI2::Blogger;
18
19class Q_DECL_HIDDEN BlogFetchJob::Private
20{
21public:
22 Private(const QString &id, FetchBy fetchBy);
23 ~Private();
24
25 const QString id;
26 const FetchBy fetchBy;
27};
28
29BlogFetchJob::Private::Private(const QString &id_, FetchBy fetchBy_)
30 : id(id_)
31 , fetchBy(fetchBy_)
32{
33}
34
35BlogFetchJob::Private::~Private()
36{
37}
38
39BlogFetchJob::BlogFetchJob(const QString &id, FetchBy fetchBy, const AccountPtr &account, QObject *parent)
41 , d(new Private(id, fetchBy))
42{
43}
44
45BlogFetchJob::~BlogFetchJob()
46{
47 delete d;
48}
49
50void BlogFetchJob::start()
51{
52 QNetworkRequest request;
53 switch (d->fetchBy) {
54 case FetchByBlogId:
55 request.setUrl(BloggerService::fetchBlogByBlogIdUrl(d->id));
56 break;
57 case FetchByBlogUrl:
58 request.setUrl(BloggerService::fetchBlogByBlogUrlUrl(d->id));
59 break;
60 case FetchByUserId:
61 request.setUrl(BloggerService::fetchBlogsByUserIdUrl(d->id));
62 break;
63 }
64
65 enqueueRequest(request);
66}
67
68ObjectsList BlogFetchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
69{
71
72 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
73 ContentType ct = Utils::stringToContentType(contentType);
74 if (ct == KGAPI2::JSON) {
75 if (d->fetchBy == FetchByUserId) {
76 items << Blog::fromJSONFeed(rawData);
77 } else {
78 items << Blog::fromJSON(rawData);
79 }
80 } else {
82 setErrorString(tr("Invalid response content type"));
83 }
84
86 return items;
87}
88
89#include "moc_blogfetchjob.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
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
void setUrl(const QUrl &url)
QObject * parent() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:40 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.