Kgapi

postpublishjob.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 "postpublishjob.h"
8#include "bloggerservice.h"
9#include "post.h"
10#include "utils.h"
11
12#include <QNetworkAccessManager>
13#include <QNetworkReply>
14#include <QNetworkRequest>
15#include <QUrlQuery>
16
17using namespace KGAPI2;
18using namespace KGAPI2::Blogger;
19
20class Q_DECL_HIDDEN PostPublishJob::Private
21{
22public:
23 Private(const QString &blogId, const QString &postId, PostPublishJob::PublishAction action, const QDateTime &publishDate = QDateTime());
24
25 const QString blogId;
26 const QString postId;
27 const PublishAction action;
28 const QDateTime publishDate;
29
30 ObjectPtr response;
31};
32
33PostPublishJob::Private::Private(const QString &blogId_, const QString &postId_, PostPublishJob::PublishAction action_, const QDateTime &publishDate_)
34 : blogId(blogId_)
35 , postId(postId_)
36 , action(action_)
37 , publishDate(publishDate_)
38{
39}
40
41PostPublishJob::PostPublishJob(const PostPtr &post, PublishAction action, const AccountPtr &account, QObject *parent)
42 : Job(account, parent)
43 , d(new Private(post->blogId(), post->id(), action))
44{
45}
46
47PostPublishJob::PostPublishJob(const QString &blogId, const QString &postId, PublishAction action, const AccountPtr &account, QObject *parent)
48 : Job(account, parent)
49 , d(new Private(blogId, postId, action))
50{
51}
52
53PostPublishJob::PostPublishJob(const PostPtr &post, const QDateTime &publishDate, const AccountPtr &account, QObject *parent)
54 : Job(account, parent)
55 , d(new Private(post->blogId(), post->id(), Publish, publishDate))
56{
57}
58
59PostPublishJob::PostPublishJob(const QString &blogId, const QString &postId, const QDateTime &publishDate, const AccountPtr &account, QObject *parent)
60 : Job(account, parent)
61 , d(new Private(blogId, postId, Publish, publishDate))
62{
63}
64
65PostPublishJob::~PostPublishJob()
66{
67 delete d;
68}
69
70ObjectPtr PostPublishJob::item() const
71{
72 return d->response;
73}
74
75void PostPublishJob::start()
76{
77 QUrl url;
78 if (d->action == Blogger::PostPublishJob::Publish) {
79 url = BloggerService::publishPostUrl(d->blogId, d->postId);
80 QUrlQuery query(url);
81 if (d->publishDate.isValid()) {
82 query.addQueryItem(QStringLiteral("publishDate"), d->publishDate.toString(Qt::ISODate));
83 }
84 url.setQuery(query);
85 } else {
86 url = BloggerService::revertPostUrl(d->blogId, d->postId);
87 }
88
89 const QNetworkRequest request(url);
90 enqueueRequest(request);
91}
92
93void PostPublishJob::dispatchRequest(QNetworkAccessManager *accessManager, const QNetworkRequest &request, const QByteArray &data, const QString &contentType)
94{
95 Q_UNUSED(contentType)
96 accessManager->post(request, data);
97}
98
99void PostPublishJob::handleReply(const QNetworkReply *reply, const QByteArray &rawData)
100{
101 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
102 ContentType ct = Utils::stringToContentType(contentType);
103 if (ct != KGAPI2::JSON) {
105 setErrorString(tr("Invalid response content type"));
106 emitFinished();
107 }
108
109 d->response = Post::fromJSON(rawData);
110 emitFinished();
111}
112
113#include "moc_postpublishjob.cpp"
Abstract base class for all jobs in LibKGAPI.
Definition job.h: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
std::optional< QSqlQuery > query(const QString &queryStatement)
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
QNetworkReply * post(const QNetworkRequest &request, QHttpMultiPart *multiPart)
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 Sat Dec 21 2024 17:00:11 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.