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

0001 /*
0002     SPDX-FileCopyrightText: 2014 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
0003     SPDX-FileContributor: Kevin Krammer <kevin.krammer@kdab.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "kolabchangetagtask.h"
0009 #include "kolabresource_debug.h"
0010 
0011 #include <Akonadi/ItemFetchJob>
0012 #include <Akonadi/ItemFetchScope>
0013 
0014 KolabChangeTagTask::KolabChangeTagTask(const ResourceStateInterface::Ptr &resource, const QSharedPointer<TagConverter> &tagConverter, QObject *parent)
0015     : KolabRelationResourceTask(resource, parent)
0016     , mTagConverter(tagConverter)
0017 {
0018 }
0019 
0020 void KolabChangeTagTask::startRelationTask(KIMAP::Session *session)
0021 {
0022     mSession = session;
0023 
0024     auto fetch = new Akonadi::ItemFetchJob(resourceState()->tag());
0025     fetch->fetchScope().setCacheOnly(true);
0026     fetch->fetchScope().setFetchGid(true);
0027     fetch->fetchScope().setAncestorRetrieval(Akonadi::ItemFetchScope::All);
0028     fetch->fetchScope().fetchFullPayload(true);
0029     connect(fetch, &KJob::result, this, &KolabChangeTagTask::onItemsFetchDone);
0030 }
0031 
0032 void KolabChangeTagTask::onItemsFetchDone(KJob *job)
0033 {
0034     if (job->error()) {
0035         qCWarning(KOLABRESOURCE_LOG) << "ItemFetch failed: " << job->errorString();
0036         cancelTask(job->errorString());
0037         return;
0038     }
0039 
0040     const Akonadi::Item::List items = static_cast<Akonadi::ItemFetchJob *>(job)->items();
0041 
0042     auto changeHelper = new TagChangeHelper(this);
0043 
0044     connect(changeHelper, &TagChangeHelper::applyCollectionChanges, this, &KolabChangeTagTask::onApplyCollectionChanged);
0045     connect(changeHelper, &TagChangeHelper::cancelTask, this, &KolabChangeTagTask::onCancelTask);
0046     connect(changeHelper, &TagChangeHelper::changeCommitted, this, &KolabChangeTagTask::onChangeCommitted);
0047 
0048     changeHelper->start(resourceState()->tag(), mTagConverter->createMessage(resourceState()->tag(), items, resourceState()->userName()), mSession);
0049 }
0050 
0051 void KolabChangeTagTask::onApplyCollectionChanged(const Akonadi::Collection &collection)
0052 {
0053     mRelationCollection = collection;
0054     applyCollectionChanges(collection);
0055 }
0056 
0057 void KolabChangeTagTask::onCancelTask(const QString &errorText)
0058 {
0059     cancelTask(errorText);
0060 }
0061 
0062 void KolabChangeTagTask::onChangeCommitted()
0063 {
0064     changeProcessed();
0065 }
0066 
0067 #include "moc_kolabchangetagtask.cpp"