File indexing completed on 2024-12-22 05:01:11

0001 /*
0002   SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 */
0007 
0008 #include "createtaskjob.h"
0009 #include "attributes/taskattribute.h"
0010 #include <Akonadi/MessageStatus>
0011 
0012 #include <Akonadi/ItemFetchJob>
0013 #include <Akonadi/ItemFetchScope>
0014 
0015 #include <Akonadi/ItemModifyJob>
0016 
0017 #include "kmail_debug.h"
0018 
0019 CreateTaskJob::CreateTaskJob(const Akonadi::Item::List &items, QObject *parent)
0020     : KJob(parent)
0021     , mListItem(items)
0022 {
0023 }
0024 
0025 CreateTaskJob::~CreateTaskJob() = default;
0026 
0027 void CreateTaskJob::start()
0028 {
0029     if (mListItem.isEmpty()) {
0030         emitResult();
0031         return;
0032     }
0033     fetchItems();
0034 }
0035 
0036 void CreateTaskJob::fetchItems()
0037 {
0038     auto job = new Akonadi::ItemFetchJob(mListItem, this);
0039     job->fetchScope().fetchFullPayload(true);
0040     job->fetchScope().setAncestorRetrieval(Akonadi::ItemFetchScope::Parent);
0041     job->fetchScope().fetchAttribute<TaskAttribute>();
0042     connect(job, &Akonadi::ItemFetchJob::result, this, &CreateTaskJob::itemFetchJobDone);
0043 }
0044 
0045 void CreateTaskJob::itemFetchJobDone(KJob *job)
0046 {
0047     if (job->error()) {
0048         qCDebug(KMAIL_LOG) << job->errorString();
0049         emitResult();
0050         deleteLater();
0051         return;
0052     }
0053     auto fetchjob = qobject_cast<Akonadi::ItemFetchJob *>(job);
0054     const Akonadi::Item::List lst = fetchjob->items();
0055     if (lst.isEmpty()) {
0056         emitResult();
0057         deleteLater();
0058         return;
0059     }
0060 
0061     bool parentStatus = false;
0062     // Toggle actions on threads toggle the whole thread
0063     // depending on the state of the parent.
0064     const Akonadi::Item first = lst.at(0);
0065     Akonadi::MessageStatus pStatus;
0066     pStatus.setStatusFromFlags(first.flags());
0067     if (pStatus & Akonadi::MessageStatus::statusToAct()) {
0068         parentStatus = true;
0069     } else {
0070         parentStatus = false;
0071     }
0072 
0073     Akonadi::Item::List itemsToModify;
0074     for (const Akonadi::Item &it : lst) {
0075         // qCDebug(KMAIL_LOG)<<" item ::"<<tmpItem;
0076         if (it.isValid()) {
0077             bool myStatus;
0078             Akonadi::MessageStatus itemStatus;
0079             itemStatus.setStatusFromFlags(it.flags());
0080             if (itemStatus & Akonadi::MessageStatus::statusToAct()) {
0081                 myStatus = true;
0082             } else {
0083                 myStatus = false;
0084             }
0085             if (myStatus != parentStatus) {
0086                 continue;
0087             }
0088         }
0089         Akonadi::Item item(it);
0090         const Akonadi::Item::Flag flag = *(Akonadi::MessageStatus::statusToAct().statusFlags().begin());
0091         if (item.hasFlag(flag)) {
0092             item.clearFlag(flag);
0093             itemsToModify.push_back(item);
0094             if (item.hasAttribute<TaskAttribute>()) {
0095                 // Change todo as done.
0096                 item.removeAttribute<TaskAttribute>();
0097             }
0098         } else {
0099             item.setFlag(flag);
0100             itemsToModify.push_back(item);
0101             // TODO add TaskAttribute();
0102         }
0103     }
0104 
0105     if (itemsToModify.isEmpty()) {
0106         slotModifyItemDone(nullptr);
0107     } else {
0108         auto modifyJob = new Akonadi::ItemModifyJob(itemsToModify, this);
0109         modifyJob->disableRevisionCheck();
0110         modifyJob->setIgnorePayload(true);
0111         connect(modifyJob, &Akonadi::ItemModifyJob::result, this, &CreateTaskJob::slotModifyItemDone);
0112     }
0113 }
0114 
0115 void CreateTaskJob::slotModifyItemDone(KJob *job)
0116 {
0117     if (job && job->error()) {
0118         qCDebug(KMAIL_LOG) << " error " << job->errorString();
0119     }
0120     deleteLater();
0121 }
0122 
0123 #include "moc_createtaskjob.cpp"