File indexing completed on 2024-05-12 05:22:16
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 "changefetchjob.h" 0010 #include "change.h" 0011 #include "debug.h" 0012 #include "driveservice.h" 0013 #include "utils.h" 0014 0015 #include <QNetworkReply> 0016 #include <QNetworkRequest> 0017 #include <QUrlQuery> 0018 0019 using namespace KGAPI2; 0020 using namespace KGAPI2::Drive; 0021 0022 class Q_DECL_HIDDEN ChangeFetchJob::Private 0023 { 0024 public: 0025 Private(ChangeFetchJob *parent); 0026 0027 QString changeId; 0028 0029 bool includeDeleted = true; 0030 bool includeSubscribed = true; 0031 int maxResults = 0; 0032 qlonglong startChangeId = 0; 0033 bool includeItemsFromAllDrives = true; 0034 bool supportsAllDrives = true; 0035 0036 private: 0037 ChangeFetchJob *const q; 0038 }; 0039 0040 ChangeFetchJob::Private::Private(ChangeFetchJob *parent) 0041 : q(parent) 0042 { 0043 } 0044 0045 ChangeFetchJob::ChangeFetchJob(const QString &changeId, const AccountPtr &account, QObject *parent) 0046 : FetchJob(account, parent) 0047 , d(new Private(this)) 0048 { 0049 d->changeId = changeId; 0050 } 0051 0052 ChangeFetchJob::ChangeFetchJob(const AccountPtr &account, QObject *parent) 0053 : FetchJob(account, parent) 0054 , d(new Private(this)) 0055 { 0056 } 0057 0058 ChangeFetchJob::~ChangeFetchJob() 0059 { 0060 delete d; 0061 } 0062 0063 void ChangeFetchJob::setIncludeDeleted(bool includeDeleted) 0064 { 0065 if (isRunning()) { 0066 qCWarning(KGAPIDebug) << "Can't modify includeDeleted property when job is running"; 0067 return; 0068 } 0069 0070 d->includeDeleted = includeDeleted; 0071 } 0072 0073 bool ChangeFetchJob::includeDeleted() const 0074 { 0075 return d->includeDeleted; 0076 } 0077 0078 void ChangeFetchJob::setIncludeSubscribed(bool includeSubscribed) 0079 { 0080 if (isRunning()) { 0081 qCWarning(KGAPIDebug) << "Can't modify includeSubscribed property when job is running"; 0082 return; 0083 } 0084 0085 d->includeSubscribed = includeSubscribed; 0086 } 0087 0088 bool ChangeFetchJob::includeSubscribed() const 0089 { 0090 return d->includeSubscribed; 0091 } 0092 0093 void ChangeFetchJob::setMaxResults(int maxResults) 0094 { 0095 if (isRunning()) { 0096 qCWarning(KGAPIDebug) << "Can't modify maxResults property when job is running"; 0097 return; 0098 } 0099 0100 d->maxResults = maxResults; 0101 } 0102 0103 int ChangeFetchJob::maxResults() const 0104 { 0105 return d->maxResults; 0106 } 0107 0108 void ChangeFetchJob::setStartChangeId(qlonglong startChangeId) 0109 { 0110 if (isRunning()) { 0111 qCWarning(KGAPIDebug) << "Can't modify startChangeId property when job is running"; 0112 } 0113 0114 d->startChangeId = startChangeId; 0115 } 0116 0117 qlonglong ChangeFetchJob::startChangeId() const 0118 { 0119 return d->startChangeId; 0120 } 0121 0122 bool ChangeFetchJob::includeItemsFromAllDrives() const 0123 { 0124 return d->includeItemsFromAllDrives; 0125 } 0126 0127 void ChangeFetchJob::setIncludeItemsFromAllDrives(bool includeItemsFromAllDrives) 0128 { 0129 d->includeItemsFromAllDrives = includeItemsFromAllDrives; 0130 } 0131 0132 bool ChangeFetchJob::supportsAllDrives() const 0133 { 0134 return d->supportsAllDrives; 0135 } 0136 0137 void ChangeFetchJob::setSupportsAllDrives(bool supportsAllDrives) 0138 { 0139 d->supportsAllDrives = supportsAllDrives; 0140 } 0141 0142 void ChangeFetchJob::start() 0143 { 0144 QUrl url; 0145 if (d->changeId.isEmpty()) { 0146 url = DriveService::fetchChangesUrl(); 0147 QUrlQuery query(url); 0148 query.addQueryItem(QStringLiteral("includeDeleted"), Utils::bool2Str(d->includeDeleted)); 0149 query.addQueryItem(QStringLiteral("includeSubscribed"), Utils::bool2Str(d->includeSubscribed)); 0150 if (d->maxResults > 0) { 0151 query.addQueryItem(QStringLiteral("maxResults"), QString::number(d->maxResults)); 0152 } 0153 if (d->startChangeId > 0) { 0154 query.addQueryItem(QStringLiteral("startChangeId"), QString::number(d->startChangeId)); 0155 } 0156 query.addQueryItem(QStringLiteral("includeItemsFromAllDrives"), Utils::bool2Str(d->includeItemsFromAllDrives)); 0157 url.setQuery(query); 0158 } else { 0159 url = DriveService::fetchChangeUrl(d->changeId); 0160 } 0161 0162 QUrlQuery query(url); 0163 query.addQueryItem(QStringLiteral("supportsAllDrives"), Utils::bool2Str(d->supportsAllDrives)); 0164 url.setQuery(query); 0165 0166 QNetworkRequest request(url); 0167 enqueueRequest(request); 0168 } 0169 0170 ObjectsList ChangeFetchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) 0171 { 0172 FeedData feedData; 0173 feedData.requestUrl = reply->url(); 0174 0175 ObjectsList items; 0176 0177 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString(); 0178 ContentType ct = Utils::stringToContentType(contentType); 0179 if (ct == KGAPI2::JSON) { 0180 if (d->changeId.isEmpty()) { 0181 items << Change::fromJSONFeed(rawData, feedData); 0182 } else { 0183 items << Change::fromJSON(rawData); 0184 } 0185 } else { 0186 setError(KGAPI2::InvalidResponse); 0187 setErrorString(tr("Invalid response content type")); 0188 emitFinished(); 0189 return items; 0190 } 0191 0192 if (feedData.nextPageUrl.isValid()) { 0193 QNetworkRequest request(feedData.nextPageUrl); 0194 enqueueRequest(request); 0195 } 0196 0197 return items; 0198 } 0199 0200 #include "moc_changefetchjob.cpp"