1 /*
2 SPDX-FileCopyrightText: 2013-2021 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 #include "folderarchiveagentcheckcollection.h"
7 #include "folderarchiveaccountinfo.h"
8 #include "kmail_debug.h"
9
10 #include <KLocalizedString>
11
12 #include <Akonadi/CollectionCreateJob>
13 #include <Akonadi/CollectionFetchJob>
14
FolderArchiveAgentCheckCollection(FolderArchiveAccountInfo * info,QObject * parent)15 FolderArchiveAgentCheckCollection::FolderArchiveAgentCheckCollection(FolderArchiveAccountInfo *info, QObject *parent)
16 : QObject(parent)
17 , mCurrentDate(QDate::currentDate())
18 , mInfo(info)
19 {
20 }
21
22 FolderArchiveAgentCheckCollection::~FolderArchiveAgentCheckCollection() = default;
23
start()24 void FolderArchiveAgentCheckCollection::start()
25 {
26 Akonadi::Collection col(mInfo->archiveTopLevel());
27 auto job = new Akonadi::CollectionFetchJob(col, Akonadi::CollectionFetchJob::FirstLevel);
28 connect(job, &Akonadi::CollectionFetchJob::result, this, &FolderArchiveAgentCheckCollection::slotInitialCollectionFetchingFirstLevelDone);
29 }
30
slotInitialCollectionFetchingFirstLevelDone(KJob * job)31 void FolderArchiveAgentCheckCollection::slotInitialCollectionFetchingFirstLevelDone(KJob *job)
32 {
33 if (job->error()) {
34 qCWarning(KMAIL_LOG) << job->errorString();
35 Q_EMIT checkFailed(i18n("Cannot fetch collection. %1", job->errorString()));
36 return;
37 }
38
39 QString folderName;
40 switch (mInfo->folderArchiveType()) {
41 case FolderArchiveAccountInfo::UniqueFolder:
42 // Nothing
43 break;
44 case FolderArchiveAccountInfo::FolderByMonths:
45 // TODO translate ?
46 folderName = QStringLiteral("%1-%2").arg(mCurrentDate.month()).arg(mCurrentDate.year());
47 break;
48 case FolderArchiveAccountInfo::FolderByYears:
49 folderName = QStringLiteral("%1").arg(mCurrentDate.year());
50 break;
51 }
52
53 if (folderName.isEmpty()) {
54 Q_EMIT checkFailed(i18n("Folder name not defined."));
55 return;
56 }
57
58 auto fetchJob = qobject_cast<Akonadi::CollectionFetchJob *>(job);
59
60 const Akonadi::Collection::List cols = fetchJob->collections();
61 for (const Akonadi::Collection &collection : cols) {
62 if (collection.name() == folderName) {
63 Q_EMIT collectionIdFound(collection);
64 return;
65 }
66 }
67 createNewFolder(folderName);
68 }
69
createNewFolder(const QString & name)70 void FolderArchiveAgentCheckCollection::createNewFolder(const QString &name)
71 {
72 Akonadi::Collection parentCollection(mInfo->archiveTopLevel());
73 Akonadi::Collection collection;
74 collection.setParentCollection(parentCollection);
75 collection.setName(name);
76 collection.setContentMimeTypes(QStringList() << QStringLiteral("message/rfc822"));
77
78 auto job = new Akonadi::CollectionCreateJob(collection);
79 connect(job, &Akonadi::CollectionCreateJob::result, this, &FolderArchiveAgentCheckCollection::slotCreateNewFolder);
80 }
81
slotCreateNewFolder(KJob * job)82 void FolderArchiveAgentCheckCollection::slotCreateNewFolder(KJob *job)
83 {
84 if (job->error()) {
85 qCWarning(KMAIL_LOG) << job->errorString();
86 Q_EMIT checkFailed(i18n("Unable to create folder. %1", job->errorString()));
87 return;
88 }
89 auto createJob = qobject_cast<Akonadi::CollectionCreateJob *>(job);
90 Q_EMIT collectionIdFound(createJob->collection());
91 }
92