File indexing completed on 2024-10-27 04:51:03

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "folderarchiveagentjob.h"
0007 #include "folderarchiveaccountinfo.h"
0008 #include "folderarchiveagentcheckcollection.h"
0009 #include "folderarchivecache.h"
0010 #include "folderarchivemanager.h"
0011 
0012 #include "kmcommands.h"
0013 
0014 #include <Akonadi/CollectionFetchJob>
0015 #include <Akonadi/ItemMoveJob>
0016 
0017 #include <KLocalizedString>
0018 
0019 FolderArchiveAgentJob::FolderArchiveAgentJob(FolderArchiveManager *manager, FolderArchiveAccountInfo *info, const Akonadi::Item::List &lstItem, QObject *parent)
0020     : QObject(parent)
0021     , mListItem(lstItem)
0022     , mManager(manager)
0023     , mInfo(info)
0024 {
0025 }
0026 
0027 FolderArchiveAgentJob::~FolderArchiveAgentJob() = default;
0028 
0029 void FolderArchiveAgentJob::start()
0030 {
0031     if (!mInfo->isValid()) {
0032         sendError(i18n("Archive folder not defined. Please verify settings for account %1", mInfo->instanceName()));
0033         return;
0034     }
0035     if (mListItem.isEmpty()) {
0036         sendError(i18n("No messages selected."));
0037         return;
0038     }
0039 
0040     if (mInfo->folderArchiveType() == FolderArchiveAccountInfo::UniqueFolder) {
0041         auto fetchCollection = new Akonadi::CollectionFetchJob(Akonadi::Collection(mInfo->archiveTopLevel()), Akonadi::CollectionFetchJob::Base);
0042         connect(fetchCollection, &Akonadi::CollectionFetchJob::result, this, &FolderArchiveAgentJob::slotFetchCollection);
0043     } else {
0044         const Akonadi::Collection::Id id = mManager->folderArchiveCache()->collectionId(mInfo);
0045         if (id != -1) {
0046             auto fetchCollection = new Akonadi::CollectionFetchJob(Akonadi::Collection(id), Akonadi::CollectionFetchJob::Base);
0047             connect(fetchCollection, &Akonadi::CollectionFetchJob::result, this, &FolderArchiveAgentJob::slotFetchCollection);
0048         } else {
0049             auto checkCol = new FolderArchiveAgentCheckCollection(mInfo, this);
0050             connect(checkCol, &FolderArchiveAgentCheckCollection::collectionIdFound, this, &FolderArchiveAgentJob::slotCollectionIdFound);
0051             connect(checkCol, &FolderArchiveAgentCheckCollection::checkFailed, this, &FolderArchiveAgentJob::slotCheckFailed);
0052             checkCol->start();
0053         }
0054     }
0055 }
0056 
0057 void FolderArchiveAgentJob::slotCheckFailed(const QString &message)
0058 {
0059     sendError(i18n("Cannot fetch collection. %1", message));
0060 }
0061 
0062 void FolderArchiveAgentJob::slotFetchCollection(KJob *job)
0063 {
0064     if (job->error()) {
0065         sendError(i18n("Cannot fetch collection. %1", job->errorString()));
0066         return;
0067     }
0068     auto fetchCollectionJob = static_cast<Akonadi::CollectionFetchJob *>(job);
0069     Akonadi::Collection::List collections = fetchCollectionJob->collections();
0070     if (collections.isEmpty()) {
0071         sendError(i18n("List of collections is empty. %1", job->errorString()));
0072         return;
0073     }
0074     sloMoveMailsToCollection(collections.at(0));
0075 }
0076 
0077 void FolderArchiveAgentJob::slotCollectionIdFound(const Akonadi::Collection &col)
0078 {
0079     mManager->folderArchiveCache()->addToCache(mInfo->instanceName(), col.id());
0080     sloMoveMailsToCollection(col);
0081 }
0082 
0083 void FolderArchiveAgentJob::sloMoveMailsToCollection(const Akonadi::Collection &col)
0084 {
0085     if (Akonadi::Collection::CanCreateItem & col.rights()) {
0086         auto command = new KMMoveCommand(col, mListItem, -1);
0087         connect(command, &KMMoveCommand::moveDone, this, &FolderArchiveAgentJob::slotMoveMessages);
0088         command->start();
0089     } else {
0090         sendError(i18n("This folder %1 is read only. Please verify the configuration of account %2", col.name(), mInfo->instanceName()));
0091     }
0092 }
0093 
0094 void FolderArchiveAgentJob::sendError(const QString &error)
0095 {
0096     mManager->moveFailed(error);
0097 }
0098 
0099 void FolderArchiveAgentJob::slotMoveMessages(KMMoveCommand *command)
0100 {
0101     if (command->result() == KMCommand::Failed) {
0102         sendError(i18n("Cannot move messages."));
0103         return;
0104     }
0105     mManager->moveDone();
0106 }
0107 
0108 #include "moc_folderarchiveagentjob.cpp"