1 /*
2 SPDX-FileCopyrightText: 2013-2021 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 #include "folderarchiveagentjob.h"
7 #include "folderarchiveaccountinfo.h"
8 #include "folderarchiveagentcheckcollection.h"
9 #include "folderarchivecache.h"
10 #include "folderarchivemanager.h"
11
12 #include "kmcommands.h"
13
14 #include <Akonadi/CollectionFetchJob>
15 #include <Akonadi/ItemMoveJob>
16
17 #include <KLocalizedString>
18
FolderArchiveAgentJob(FolderArchiveManager * manager,FolderArchiveAccountInfo * info,const Akonadi::Item::List & lstItem,QObject * parent)19 FolderArchiveAgentJob::FolderArchiveAgentJob(FolderArchiveManager *manager, FolderArchiveAccountInfo *info, const Akonadi::Item::List &lstItem, QObject *parent)
20 : QObject(parent)
21 , mListItem(lstItem)
22 , mManager(manager)
23 , mInfo(info)
24 {
25 }
26
27 FolderArchiveAgentJob::~FolderArchiveAgentJob() = default;
28
start()29 void FolderArchiveAgentJob::start()
30 {
31 if (!mInfo->isValid()) {
32 sendError(i18n("Archive folder not defined. Please verify settings for account %1", mInfo->instanceName()));
33 return;
34 }
35 if (mListItem.isEmpty()) {
36 sendError(i18n("No messages selected."));
37 return;
38 }
39
40 if (mInfo->folderArchiveType() == FolderArchiveAccountInfo::UniqueFolder) {
41 auto fetchCollection = new Akonadi::CollectionFetchJob(Akonadi::Collection(mInfo->archiveTopLevel()), Akonadi::CollectionFetchJob::Base);
42 connect(fetchCollection, &Akonadi::CollectionFetchJob::result, this, &FolderArchiveAgentJob::slotFetchCollection);
43 } else {
44 const Akonadi::Collection::Id id = mManager->folderArchiveCache()->collectionId(mInfo);
45 if (id != -1) {
46 auto fetchCollection = new Akonadi::CollectionFetchJob(Akonadi::Collection(id), Akonadi::CollectionFetchJob::Base);
47 connect(fetchCollection, &Akonadi::CollectionFetchJob::result, this, &FolderArchiveAgentJob::slotFetchCollection);
48 } else {
49 auto checkCol = new FolderArchiveAgentCheckCollection(mInfo, this);
50 connect(checkCol, &FolderArchiveAgentCheckCollection::collectionIdFound, this, &FolderArchiveAgentJob::slotCollectionIdFound);
51 connect(checkCol, &FolderArchiveAgentCheckCollection::checkFailed, this, &FolderArchiveAgentJob::slotCheckFailed);
52 checkCol->start();
53 }
54 }
55 }
56
slotCheckFailed(const QString & message)57 void FolderArchiveAgentJob::slotCheckFailed(const QString &message)
58 {
59 sendError(i18n("Cannot fetch collection. %1", message));
60 }
61
slotFetchCollection(KJob * job)62 void FolderArchiveAgentJob::slotFetchCollection(KJob *job)
63 {
64 if (job->error()) {
65 sendError(i18n("Cannot fetch collection. %1", job->errorString()));
66 return;
67 }
68 auto fetchCollectionJob = static_cast<Akonadi::CollectionFetchJob *>(job);
69 Akonadi::Collection::List collections = fetchCollectionJob->collections();
70 if (collections.isEmpty()) {
71 sendError(i18n("List of collections is empty. %1", job->errorString()));
72 return;
73 }
74 sloMoveMailsToCollection(collections.at(0));
75 }
76
slotCollectionIdFound(const Akonadi::Collection & col)77 void FolderArchiveAgentJob::slotCollectionIdFound(const Akonadi::Collection &col)
78 {
79 mManager->folderArchiveCache()->addToCache(mInfo->instanceName(), col.id());
80 sloMoveMailsToCollection(col);
81 }
82
sloMoveMailsToCollection(const Akonadi::Collection & col)83 void FolderArchiveAgentJob::sloMoveMailsToCollection(const Akonadi::Collection &col)
84 {
85 if (Akonadi::Collection::CanCreateItem & col.rights()) {
86 auto command = new KMMoveCommand(col, mListItem, -1);
87 connect(command, &KMMoveCommand::moveDone, this, &FolderArchiveAgentJob::slotMoveMessages);
88 command->start();
89 } else {
90 sendError(i18n("This folder %1 is read only. Please verify the configuration of account %2", col.name(), mInfo->instanceName()));
91 }
92 }
93
sendError(const QString & error)94 void FolderArchiveAgentJob::sendError(const QString &error)
95 {
96 mManager->moveFailed(error);
97 }
98
slotMoveMessages(KMMoveCommand * command)99 void FolderArchiveAgentJob::slotMoveMessages(KMMoveCommand *command)
100 {
101 if (command->result() == KMCommand::Failed) {
102 sendError(i18n("Cannot move messages."));
103 return;
104 }
105 mManager->moveDone();
106 }
107