Astyle kdelibs
[kdepim.git] / kmail / folderarchive / folderarchiveagentjob.cpp
blob71da45e243486fe3c01631d32eae2a993110b266
1 /*
2 Copyright (c) 2013, 2014 Montel Laurent <montel@kde.org>
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License, version 2, as
6 published by the Free Software Foundation.
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "folderarchiveagentjob.h"
19 #include "folderarchiveaccountinfo.h"
20 #include "folderarchiveagentcheckcollection.h"
21 #include "folderarchivemanager.h"
22 #include "folderarchivecache.h"
24 #include "kmcommands.h"
26 #include <AkonadiCore/ItemMoveJob>
27 #include <AkonadiCore/CollectionFetchJob>
29 #include <KLocalizedString>
31 FolderArchiveAgentJob::FolderArchiveAgentJob(FolderArchiveManager *manager, FolderArchiveAccountInfo *info, const QList<Akonadi::Item> &lstItem, QObject *parent)
32 : QObject(parent),
33 mLstItem(lstItem),
34 mManager(manager),
35 mInfo(info)
39 FolderArchiveAgentJob::~FolderArchiveAgentJob()
43 void FolderArchiveAgentJob::start()
45 if (!mInfo->isValid()) {
46 sendError(i18n("Archive folder not defined. Please verify settings for account %1", mInfo->instanceName()));
47 return;
49 if (mLstItem.isEmpty()) {
50 sendError(i18n("No messages selected."));
51 return;
54 if (mInfo->folderArchiveType() == FolderArchiveAccountInfo::UniqueFolder) {
55 Akonadi::CollectionFetchJob *fetchCollection = new Akonadi::CollectionFetchJob(Akonadi::Collection(mInfo->archiveTopLevel()), Akonadi::CollectionFetchJob::Base);
56 connect(fetchCollection, &Akonadi::CollectionFetchJob::result, this, &FolderArchiveAgentJob::slotFetchCollection);
57 } else {
58 Akonadi::Collection::Id id = mManager->folderArchiveCache()->collectionId(mInfo);
59 if (id != -1) {
60 Akonadi::CollectionFetchJob *fetchCollection = new Akonadi::CollectionFetchJob(Akonadi::Collection(id), Akonadi::CollectionFetchJob::Base);
61 connect(fetchCollection, &Akonadi::CollectionFetchJob::result, this, &FolderArchiveAgentJob::slotFetchCollection);
62 } else {
63 FolderArchiveAgentCheckCollection *checkCol = new FolderArchiveAgentCheckCollection(mInfo, this);
64 connect(checkCol, &FolderArchiveAgentCheckCollection::collectionIdFound, this, &FolderArchiveAgentJob::slotCollectionIdFound);
65 connect(checkCol, &FolderArchiveAgentCheckCollection::checkFailed, this, &FolderArchiveAgentJob::slotCheckFailder);
66 checkCol->start();
71 void FolderArchiveAgentJob::slotCheckFailder(const QString &message)
73 sendError(i18n("Cannot fetch collection. %1", message));
76 void FolderArchiveAgentJob::slotFetchCollection(KJob *job)
78 if (job->error()) {
79 sendError(i18n("Cannot fetch collection. %1", job->errorString()));
80 return;
82 Akonadi::CollectionFetchJob *fetchCollectionJob = static_cast<Akonadi::CollectionFetchJob *>(job);
83 Akonadi::Collection::List collections = fetchCollectionJob->collections();
84 if (collections.isEmpty()) {
85 sendError(i18n("List of collections is empty. %1", job->errorString()));
86 return;
88 sloMoveMailsToCollection(collections.first());
91 void FolderArchiveAgentJob::slotCollectionIdFound(const Akonadi::Collection &col)
93 mManager->folderArchiveCache()->addToCache(mInfo->instanceName(), col.id());
94 sloMoveMailsToCollection(col);
97 void FolderArchiveAgentJob::sloMoveMailsToCollection(const Akonadi::Collection &col)
99 KMMoveCommand *command = new KMMoveCommand(col, mLstItem, -1);
100 connect(command, &KMMoveCommand::moveDone, this, &FolderArchiveAgentJob::slotMoveMessages);
101 command->start();
104 void FolderArchiveAgentJob::sendError(const QString &error)
106 mManager->moveFailed(error);
109 void FolderArchiveAgentJob::slotMoveMessages(KMMoveCommand *command)
111 if (command->result() == KMCommand::Failed) {
112 sendError(i18n("Cannot move messages."));
113 return;
115 mManager->moveDone();