File indexing completed on 2024-05-12 05:22:11
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 "commentfetchjob.h" 0008 #include "bloggerservice.h" 0009 #include "comment.h" 0010 #include "utils.h" 0011 0012 #include <QNetworkReply> 0013 #include <QNetworkRequest> 0014 #include <QUrlQuery> 0015 0016 using namespace KGAPI2; 0017 using namespace KGAPI2::Blogger; 0018 0019 class Q_DECL_HIDDEN CommentFetchJob::Private 0020 { 0021 public: 0022 Private(const QString &blogId, const QString &postId, const QString &commentId, CommentFetchJob *parent); 0023 ~Private(); 0024 0025 const QString blogId; 0026 const QString postId; 0027 const QString commentId; 0028 uint maxResults; 0029 QDateTime startDate; 0030 QDateTime endDate; 0031 bool fetchBodies; 0032 0033 private: 0034 CommentFetchJob *const q; 0035 }; 0036 0037 CommentFetchJob::Private::Private(const QString &blogId_, const QString &postId_, const QString &commentId_, CommentFetchJob *parent) 0038 : blogId(blogId_) 0039 , postId(postId_) 0040 , commentId(commentId_) 0041 , maxResults(0) 0042 , fetchBodies(true) 0043 , q(parent) 0044 { 0045 } 0046 0047 CommentFetchJob::Private::~Private() 0048 { 0049 } 0050 0051 CommentFetchJob::CommentFetchJob(const QString &blogId, const AccountPtr &account, QObject *parent) 0052 : FetchJob(account, parent) 0053 , d(new Private(blogId, QString(), QString(), this)) 0054 { 0055 } 0056 0057 CommentFetchJob::CommentFetchJob(const QString &blogId, const QString &postId, const AccountPtr &account, QObject *parent) 0058 : FetchJob(account, parent) 0059 , d(new Private(blogId, postId, QString(), this)) 0060 { 0061 } 0062 0063 CommentFetchJob::CommentFetchJob(const QString &blogId, const QString &postId, const QString &commentId, const AccountPtr &account, QObject *parent) 0064 : FetchJob(account, parent) 0065 , d(new Private(blogId, postId, commentId, this)) 0066 { 0067 } 0068 0069 CommentFetchJob::~CommentFetchJob() 0070 { 0071 delete d; 0072 } 0073 0074 QDateTime CommentFetchJob::endDate() const 0075 { 0076 return d->endDate; 0077 } 0078 0079 void CommentFetchJob::setEndDate(const QDateTime &endDate) 0080 { 0081 d->endDate = endDate; 0082 } 0083 0084 QDateTime CommentFetchJob::startDate() const 0085 { 0086 return d->startDate; 0087 } 0088 0089 void CommentFetchJob::setStartDate(const QDateTime &startDate) 0090 { 0091 d->startDate = startDate; 0092 } 0093 0094 uint CommentFetchJob::maxResults() const 0095 { 0096 return d->maxResults; 0097 } 0098 0099 void CommentFetchJob::setMaxResults(uint maxResults) 0100 { 0101 d->maxResults = maxResults; 0102 } 0103 0104 bool CommentFetchJob::fetchBodies() const 0105 { 0106 return d->fetchBodies; 0107 } 0108 0109 void CommentFetchJob::setFetchBodies(bool fetchBodies) 0110 { 0111 d->fetchBodies = fetchBodies; 0112 } 0113 0114 void CommentFetchJob::start() 0115 { 0116 QUrl url = BloggerService::fetchCommentsUrl(d->blogId, d->postId, d->commentId); 0117 QUrlQuery query(url); 0118 if (d->startDate.isValid()) { 0119 query.addQueryItem(QStringLiteral("startDate"), d->startDate.toString(Qt::ISODate)); 0120 } 0121 if (d->endDate.isValid()) { 0122 query.addQueryItem(QStringLiteral("endDate"), d->endDate.toString(Qt::ISODate)); 0123 } 0124 if (d->maxResults > 0) { 0125 query.addQueryItem(QStringLiteral("maxResults"), QString::number(d->maxResults)); 0126 } 0127 query.addQueryItem(QStringLiteral("fetchBodies"), Utils::bool2Str(d->fetchBodies)); 0128 if (account()) { 0129 query.addQueryItem(QStringLiteral("view"), QStringLiteral("ADMIN")); 0130 } 0131 url.setQuery(query); 0132 0133 const QNetworkRequest request(url); 0134 enqueueRequest(request); 0135 } 0136 0137 ObjectsList CommentFetchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) 0138 { 0139 FeedData feedData; 0140 feedData.requestUrl = reply->request().url(); 0141 0142 ObjectsList items; 0143 0144 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString(); 0145 ContentType ct = Utils::stringToContentType(contentType); 0146 if (ct == KGAPI2::JSON) { 0147 if (d->commentId.isEmpty()) { 0148 items = Comment::fromJSONFeed(rawData, feedData); 0149 } else { 0150 items << Comment::fromJSON(rawData); 0151 } 0152 } else { 0153 setError(KGAPI2::InvalidResponse); 0154 setErrorString(tr("Invalid response content type")); 0155 emitFinished(); 0156 return items; 0157 } 0158 0159 if (feedData.nextPageUrl.isValid()) { 0160 const QNetworkRequest request(feedData.nextPageUrl); 0161 enqueueRequest(request); 0162 } else { 0163 emitFinished(); 0164 } 0165 0166 return items; 0167 } 0168 0169 #include "moc_commentfetchjob.cpp"