File indexing completed on 2024-11-17 04:44:59

0001 /*
0002     SPDX-FileCopyrightText: 2015-2017 Krzysztof Nowicki <krissn@op.pl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ewssubscribedfoldersjob.h"
0008 
0009 #include "ewsclient.h"
0010 #include "ewsgetfolderrequest.h"
0011 #include "ewsresource_debug.h"
0012 #include "ewssettings.h"
0013 
0014 EwsSubscribedFoldersJob::EwsSubscribedFoldersJob(EwsClient &client, EwsSettings *settings, QObject *parent)
0015     : EwsJob(parent)
0016     , mClient(client)
0017     , mSettings(settings)
0018 {
0019 }
0020 
0021 EwsSubscribedFoldersJob::~EwsSubscribedFoldersJob() = default;
0022 
0023 void EwsSubscribedFoldersJob::start()
0024 {
0025     EwsId::List ids;
0026 
0027     // Before subscribing make sure the subscription list doesn't contain invalid folders.
0028     // Do this also for the default list in order to transform the distinguished IDs into real ones.
0029     const auto serverSubscriptionList = mSettings->serverSubscriptionList();
0030     if (serverSubscriptionList == QStringList() << QStringLiteral("default")) {
0031         ids = defaultSubscriptionFolders();
0032     } else {
0033         ids.reserve(serverSubscriptionList.count());
0034         for (const QString &id : serverSubscriptionList) {
0035             ids << EwsId(id);
0036         }
0037     }
0038 
0039     auto req = new EwsGetFolderRequest(mClient, this);
0040     req->setFolderShape(EwsFolderShape(EwsShapeIdOnly));
0041     req->setFolderIds(ids);
0042     req->setProperty("ids", QVariant::fromValue<EwsId::List>(ids));
0043     connect(req, &EwsRequest::result, this, &EwsSubscribedFoldersJob::verifySubFoldersRequestFinished);
0044     req->start();
0045 }
0046 
0047 void EwsSubscribedFoldersJob::verifySubFoldersRequestFinished(KJob *job)
0048 {
0049     if (!job->error()) {
0050         auto req = qobject_cast<EwsGetFolderRequest *>(job);
0051         Q_ASSERT(req);
0052 
0053         mFolders.clear();
0054         auto sourceIds = req->property("ids").value<EwsId::List>();
0055         QStringList idList;
0056 
0057         Q_ASSERT(req->responses().size() == sourceIds.size());
0058 
0059         auto it = sourceIds.cbegin();
0060 
0061         const auto responses{req->responses()};
0062         for (const EwsGetFolderRequest::Response &resp : responses) {
0063             if (resp.isSuccess()) {
0064                 // Take just the id without the change key as the actual folder version is irrelevant
0065                 // here
0066                 const QString id = resp.folder()[EwsFolderFieldFolderId].value<EwsId>().id();
0067                 mFolders << EwsId(id);
0068                 idList << id;
0069             } else {
0070                 qCWarningNC(EWSRES_LOG) << QStringLiteral("Invalid folder %1 - skipping").arg(it->id());
0071             }
0072             it++;
0073         }
0074 
0075         // Once verified write the final list back to the configuration.
0076         mSettings->setServerSubscriptionList(idList);
0077     } else {
0078         setErrorMsg(job->errorString(), job->error());
0079     }
0080     emitResult();
0081 }
0082 
0083 const EwsId::List &EwsSubscribedFoldersJob::defaultSubscriptionFolders()
0084 {
0085     static const EwsId::List list = {
0086         EwsId(EwsDIdInbox),
0087         EwsId(EwsDIdCalendar),
0088         EwsId(EwsDIdTasks),
0089         EwsId(EwsDIdContacts),
0090     };
0091 
0092     return list;
0093 }
0094 
0095 #include "moc_ewssubscribedfoldersjob.cpp"