File indexing completed on 2024-05-12 05:22:10

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "blogfetchjob.h"
0008 #include "account.h"
0009 #include "blog.h"
0010 #include "bloggerservice.h"
0011 #include "utils.h"
0012 
0013 #include <QNetworkReply>
0014 #include <QNetworkRequest>
0015 
0016 using namespace KGAPI2;
0017 using namespace KGAPI2::Blogger;
0018 
0019 class Q_DECL_HIDDEN BlogFetchJob::Private
0020 {
0021 public:
0022     Private(const QString &id, FetchBy fetchBy);
0023     ~Private();
0024 
0025     const QString id;
0026     const FetchBy fetchBy;
0027 };
0028 
0029 BlogFetchJob::Private::Private(const QString &id_, FetchBy fetchBy_)
0030     : id(id_)
0031     , fetchBy(fetchBy_)
0032 {
0033 }
0034 
0035 BlogFetchJob::Private::~Private()
0036 {
0037 }
0038 
0039 BlogFetchJob::BlogFetchJob(const QString &id, FetchBy fetchBy, const AccountPtr &account, QObject *parent)
0040     : FetchJob(account, parent)
0041     , d(new Private(id, fetchBy))
0042 {
0043 }
0044 
0045 BlogFetchJob::~BlogFetchJob()
0046 {
0047     delete d;
0048 }
0049 
0050 void BlogFetchJob::start()
0051 {
0052     QNetworkRequest request;
0053     switch (d->fetchBy) {
0054     case FetchByBlogId:
0055         request.setUrl(BloggerService::fetchBlogByBlogIdUrl(d->id));
0056         break;
0057     case FetchByBlogUrl:
0058         request.setUrl(BloggerService::fetchBlogByBlogUrlUrl(d->id));
0059         break;
0060     case FetchByUserId:
0061         request.setUrl(BloggerService::fetchBlogsByUserIdUrl(d->id));
0062         break;
0063     }
0064 
0065     enqueueRequest(request);
0066 }
0067 
0068 ObjectsList BlogFetchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
0069 {
0070     ObjectsList items;
0071 
0072     const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
0073     ContentType ct = Utils::stringToContentType(contentType);
0074     if (ct == KGAPI2::JSON) {
0075         if (d->fetchBy == FetchByUserId) {
0076             items << Blog::fromJSONFeed(rawData);
0077         } else {
0078             items << Blog::fromJSON(rawData);
0079         }
0080     } else {
0081         setError(KGAPI2::InvalidResponse);
0082         setErrorString(tr("Invalid response content type"));
0083     }
0084 
0085     emitFinished();
0086     return items;
0087 }
0088 
0089 #include "moc_blogfetchjob.cpp"