Kgapi

postcreatejob.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 "postcreatejob.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 PostCreateJob::Private
20{
21public:
22 Private(const PostPtr &post, bool isDraft);
23
24 const PostPtr post;
25 const bool isDraft;
26};
27
28PostCreateJob::Private::Private(const PostPtr &post_, bool isDraft_)
29 : post(post_)
30 , isDraft(isDraft_)
31{
32}
33
34PostCreateJob::PostCreateJob(const PostPtr &post, bool isDraft, const AccountPtr &account, QObject *parent)
35 : CreateJob(account, parent)
36 , d(new Private(post, isDraft))
37{
38}
39
40PostCreateJob::~PostCreateJob()
41{
42 delete d;
43}
44
45void PostCreateJob::start()
46{
47 QUrl url = BloggerService::createPostUrl(d->post->blogId());
48 if (d->isDraft) {
49 QUrlQuery query(url);
50 query.addQueryItem(QStringLiteral("isDraft"), Utils::bool2Str(d->isDraft));
51 url.setQuery(query);
52 }
53 QNetworkRequest request(url);
54
55 const QByteArray rawData = Post::toJSON(d->post);
56
57 enqueueRequest(request, rawData, QStringLiteral("application/json"));
58}
59
60ObjectsList PostCreateJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
61{
62 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
63 ContentType ct = Utils::stringToContentType(contentType);
65 if (ct != KGAPI2::JSON) {
67 setErrorString(tr("Invalid response content type"));
69 return items;
70 }
71
72 items << Post::fromJSON(rawData);
74 return items;
75}
76
77#include "moc_postcreatejob.cpp"
Abstract superclass for all jobs that create new objects on the server.
Definition createjob.h:26
virtual ObjectsList items() const
Definition createjob.cpp:40
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
QString tr(const char *sourceText, const char *disambiguation, int n)
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.