File indexing completed on 2024-05-26 05:17:00

0001 /*
0002     Copyright (c) 2010 Tobias Koenig <tokoe@kde.org>
0003 
0004     This program is free software; you can redistribute it and/or modify
0005     it under the terms of the GNU General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or
0007     (at your option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0012     GNU General Public License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program; if not, write to the Free Software
0016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0017 */
0018 
0019 #include "davitemmodifyjob.h"
0020 
0021 #include "davitemfetchjob.h"
0022 #include "davmanager.h"
0023 #include "daverror.h"
0024 #include "davjob.h"
0025 
0026 using namespace KDAV2;
0027 
0028 DavItemModifyJob::DavItemModifyJob(const DavItem &item, QObject *parent)
0029     : DavJobBase(parent), mItem(item), mFreshResponseCode(0)
0030 {
0031 }
0032 
0033 void DavItemModifyJob::start()
0034 {
0035     auto job = DavManager::self()->createModifyJob(mItem.data(), itemUrl(), mItem.contentType().toUtf8(), mItem.etag().toUtf8());
0036     connect(job, &DavJob::result, this, &DavItemModifyJob::davJobFinished);
0037 }
0038 
0039 DavItem DavItemModifyJob::item() const
0040 {
0041     return mItem;
0042 }
0043 
0044 DavItem DavItemModifyJob::freshItem() const
0045 {
0046     return mFreshItem;
0047 }
0048 
0049 int DavItemModifyJob::freshResponseCode() const
0050 {
0051     return mFreshResponseCode;
0052 }
0053 
0054 QUrl DavItemModifyJob::itemUrl() const
0055 {
0056     return mItem.url().url();
0057 }
0058 
0059 void DavItemModifyJob::davJobFinished(KJob *job)
0060 {
0061     auto storedJob = static_cast<DavJob*>(job);
0062 
0063     if (storedJob->error()) {
0064         setErrorFromJob(storedJob, ERR_ITEMMODIFY);
0065 
0066         if (hasConflict()) {
0067             DavItemFetchJob *fetchJob = new DavItemFetchJob(mItem);
0068             connect(fetchJob, &DavItemFetchJob::result, this, &DavItemModifyJob::conflictingItemFetched);
0069             fetchJob->start();
0070         } else {
0071             emitResult();
0072         }
0073 
0074         return;
0075     }
0076 
0077     const auto location = storedJob->getLocationHeader();
0078     QUrl url;
0079     if (location.isEmpty()) {
0080         url = storedJob->url();
0081     } else if (location.startsWith(QLatin1Char('/'))) {
0082         url = storedJob->url();
0083         url.setPath(location, QUrl::TolerantMode);
0084     } else {
0085         url = QUrl::fromUserInput(location);
0086     }
0087 
0088     url.setUserInfo(itemUrl().userInfo());
0089     mItem.setUrl(DavUrl(url, mItem.url().protocol()));
0090 
0091     DavItemFetchJob *fetchJob = new DavItemFetchJob(mItem);
0092     connect(fetchJob, &DavItemFetchJob::result, this, &DavItemModifyJob::itemRefreshed);
0093     fetchJob->start();
0094 }
0095 
0096 void DavItemModifyJob::itemRefreshed(KJob *job)
0097 {
0098     if (!job->error()) {
0099         DavItemFetchJob *fetchJob = qobject_cast<DavItemFetchJob *>(job);
0100         mItem.setEtag(fetchJob->item().etag());
0101     } else {
0102         mItem.setEtag(QString());
0103     }
0104     emitResult();
0105 }
0106 
0107 void DavItemModifyJob::conflictingItemFetched(KJob *job)
0108 {
0109     DavItemFetchJob *fetchJob = qobject_cast<DavItemFetchJob *>(job);
0110     mFreshResponseCode = fetchJob->latestHttpStatusCode();
0111 
0112     if (!job->error()) {
0113         mFreshItem = fetchJob->item();
0114     }
0115 
0116     emitResult();
0117 }
0118