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

0001 /*
0002  * This file is part of LibKGAPI library
0003  *
0004  * SPDX-FileCopyrightText: 2013 Daniel Vrátil <dvratil@redhat.com>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007  */
0008 
0009 #include "taskfetchjob.h"
0010 #include "debug.h"
0011 #include "task.h"
0012 #include "tasksservice.h"
0013 #include "utils.h"
0014 
0015 #include <QNetworkReply>
0016 #include <QNetworkRequest>
0017 #include <QUrlQuery>
0018 
0019 using namespace KGAPI2;
0020 
0021 namespace
0022 {
0023 static const auto ShowDeletedParam = QStringLiteral("showDeleted");
0024 static const auto ShowCompletedParam = QStringLiteral("showCompleted");
0025 static const auto UpdatedMinParam = QStringLiteral("updatedMin");
0026 static const auto CompletedMinParam = QStringLiteral("completedMin");
0027 static const auto CompletedMaxParam = QStringLiteral("completedMax");
0028 static const auto DueMinParam = QStringLiteral("dueMin");
0029 static const auto DueMaxParam = QStringLiteral("dueMax");
0030 
0031 static constexpr bool FetchDeletedDefault = false;
0032 static constexpr bool FetchCompletedDefault = false;
0033 }
0034 
0035 class Q_DECL_HIDDEN TaskFetchJob::Private
0036 {
0037 public:
0038     QString taskId;
0039     QString taskListId;
0040     bool fetchDeleted = true;
0041     bool fetchCompleted = true;
0042     quint64 updatedTimestamp;
0043     quint64 completedMin;
0044     quint64 completedMax;
0045     quint64 dueMin;
0046     quint64 dueMax;
0047 };
0048 
0049 TaskFetchJob::TaskFetchJob(const QString &taskListId, const AccountPtr &account, QObject *parent)
0050     : FetchJob(account, parent)
0051     , d(new Private())
0052 {
0053     d->taskListId = taskListId;
0054 }
0055 
0056 TaskFetchJob::TaskFetchJob(const QString &taskId, const QString &taskListId, const AccountPtr &account, QObject *parent)
0057     : FetchJob(account, parent)
0058     , d(new Private())
0059 {
0060     d->taskId = taskId;
0061     d->taskListId = taskListId;
0062 }
0063 
0064 TaskFetchJob::~TaskFetchJob() = default;
0065 
0066 void TaskFetchJob::setFetchOnlyUpdated(quint64 timestamp)
0067 {
0068     if (isRunning()) {
0069         qCWarning(KGAPIDebug) << "Can't modify fetchOnlyUpdated property when job is running";
0070         return;
0071     }
0072     d->updatedTimestamp = timestamp;
0073 }
0074 
0075 quint64 TaskFetchJob::fetchOnlyUpdated()
0076 {
0077     return d->updatedTimestamp;
0078 }
0079 
0080 void TaskFetchJob::setFetchCompleted(bool fetchCompleted)
0081 {
0082     if (isRunning()) {
0083         qCWarning(KGAPIDebug) << "Can't modify fetchCompleted property when job is running";
0084         return;
0085     }
0086     d->fetchCompleted = fetchCompleted;
0087 }
0088 
0089 bool TaskFetchJob::fetchCompleted() const
0090 {
0091     return d->fetchCompleted;
0092 }
0093 
0094 void TaskFetchJob::setFetchDeleted(bool fetchDeleted)
0095 {
0096     if (isRunning()) {
0097         qCWarning(KGAPIDebug) << "Can't modify fetchDeleted property when job is running";
0098         return;
0099     }
0100     d->fetchDeleted = fetchDeleted;
0101 }
0102 
0103 bool TaskFetchJob::fetchDeleted() const
0104 {
0105     return d->fetchDeleted;
0106 }
0107 
0108 void TaskFetchJob::setCompletedMin(quint64 timestamp)
0109 {
0110     if (isRunning()) {
0111         qCWarning(KGAPIDebug) << "Can't modify completedMin property when job is running";
0112         return;
0113     }
0114     d->completedMin = timestamp;
0115 }
0116 
0117 quint64 TaskFetchJob::completedMin() const
0118 {
0119     return d->completedMin;
0120 }
0121 
0122 void TaskFetchJob::setCompletedMax(quint64 timestamp)
0123 {
0124     if (isRunning()) {
0125         qCWarning(KGAPIDebug) << "Can't modify completedMax property when job is running";
0126         return;
0127     }
0128     d->completedMax = timestamp;
0129 }
0130 
0131 quint64 TaskFetchJob::completedMax() const
0132 {
0133     return d->completedMax;
0134 }
0135 
0136 void TaskFetchJob::setDueMin(quint64 timestamp)
0137 {
0138     if (isRunning()) {
0139         qCWarning(KGAPIDebug) << "Can't modify dueMin property when job is running";
0140         return;
0141     }
0142     d->dueMin = timestamp;
0143 }
0144 
0145 quint64 TaskFetchJob::dueMin() const
0146 {
0147     return d->dueMin;
0148 }
0149 
0150 void TaskFetchJob::setDueMax(quint64 timestamp)
0151 {
0152     if (isRunning()) {
0153         qCWarning(KGAPIDebug) << "Can't modify dueMax property when job is running";
0154         return;
0155     }
0156     d->dueMax = timestamp;
0157 }
0158 
0159 quint64 TaskFetchJob::dueMax() const
0160 {
0161     return d->dueMax;
0162 }
0163 
0164 void TaskFetchJob::start()
0165 {
0166     QUrl url;
0167     if (d->taskId.isEmpty()) {
0168         url = TasksService::fetchAllTasksUrl(d->taskListId);
0169         QUrlQuery query(url);
0170         if (d->fetchDeleted != FetchDeletedDefault) {
0171             query.addQueryItem(ShowDeletedParam, Utils::bool2Str(d->fetchDeleted));
0172         }
0173         if (d->fetchCompleted != FetchCompletedDefault) {
0174             query.addQueryItem(ShowCompletedParam, Utils::bool2Str(d->fetchCompleted));
0175         }
0176         if (d->updatedTimestamp > 0) {
0177             query.addQueryItem(UpdatedMinParam, Utils::ts2Str(d->updatedTimestamp));
0178         }
0179         if (d->completedMin > 0) {
0180             query.addQueryItem(CompletedMinParam, Utils::ts2Str(d->completedMin));
0181         }
0182         if (d->completedMax > 0) {
0183             query.addQueryItem(CompletedMaxParam, Utils::ts2Str(d->completedMax));
0184         }
0185         if (d->dueMin > 0) {
0186             query.addQueryItem(DueMinParam, Utils::ts2Str(d->dueMin));
0187         }
0188         if (d->dueMax > 0) {
0189             query.addQueryItem(DueMaxParam, Utils::ts2Str(d->dueMax));
0190         }
0191         url.setQuery(query);
0192     } else {
0193         url = TasksService::fetchTaskUrl(d->taskListId, d->taskId);
0194     }
0195 
0196     const QNetworkRequest request(url);
0197     enqueueRequest(request);
0198 }
0199 
0200 ObjectsList TaskFetchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
0201 {
0202     FeedData feedData;
0203     feedData.requestUrl = reply->url();
0204 
0205     ObjectsList items;
0206     const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
0207     ContentType ct = Utils::stringToContentType(contentType);
0208     if (ct == KGAPI2::JSON) {
0209         if (d->taskId.isEmpty()) {
0210             items = TasksService::parseJSONFeed(rawData, feedData);
0211         } else {
0212             items << TasksService::JSONToTask(rawData);
0213         }
0214     } else {
0215         setError(KGAPI2::InvalidResponse);
0216         setErrorString(tr("Invalid response content type"));
0217         emitFinished();
0218         return items;
0219     }
0220 
0221     if (feedData.nextPageUrl.isValid()) {
0222         const QNetworkRequest request(feedData.nextPageUrl);
0223         enqueueRequest(request);
0224     }
0225 
0226     return items;
0227 }
0228 
0229 #include "moc_taskfetchjob.cpp"