File indexing completed on 2025-02-02 05:08:58
0001 /* 0002 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 #include "folderarchiveagentcheckcollection.h" 0007 #include "folderarchiveaccountinfo.h" 0008 #include "kmail_debug.h" 0009 0010 #include <KLocalizedString> 0011 0012 #include <Akonadi/CollectionCreateJob> 0013 #include <Akonadi/CollectionFetchJob> 0014 0015 FolderArchiveAgentCheckCollection::FolderArchiveAgentCheckCollection(FolderArchiveAccountInfo *info, QObject *parent) 0016 : QObject(parent) 0017 , mCurrentDate(QDate::currentDate()) 0018 , mInfo(info) 0019 { 0020 } 0021 0022 FolderArchiveAgentCheckCollection::~FolderArchiveAgentCheckCollection() = default; 0023 0024 void FolderArchiveAgentCheckCollection::start() 0025 { 0026 Akonadi::Collection col(mInfo->archiveTopLevel()); 0027 auto job = new Akonadi::CollectionFetchJob(col, Akonadi::CollectionFetchJob::FirstLevel); 0028 connect(job, &Akonadi::CollectionFetchJob::result, this, &FolderArchiveAgentCheckCollection::slotInitialCollectionFetchingFirstLevelDone); 0029 } 0030 0031 void FolderArchiveAgentCheckCollection::slotInitialCollectionFetchingFirstLevelDone(KJob *job) 0032 { 0033 if (job->error()) { 0034 qCWarning(KMAIL_LOG) << job->errorString(); 0035 Q_EMIT checkFailed(i18n("Cannot fetch collection. %1", job->errorString())); 0036 return; 0037 } 0038 0039 QString folderName; 0040 switch (mInfo->folderArchiveType()) { 0041 case FolderArchiveAccountInfo::UniqueFolder: 0042 // Nothing 0043 break; 0044 case FolderArchiveAccountInfo::FolderByMonths: 0045 // TODO translate ? 0046 folderName = QStringLiteral("%1-%2").arg(mCurrentDate.month()).arg(mCurrentDate.year()); 0047 break; 0048 case FolderArchiveAccountInfo::FolderByYears: 0049 folderName = QStringLiteral("%1").arg(mCurrentDate.year()); 0050 break; 0051 } 0052 0053 if (folderName.isEmpty()) { 0054 Q_EMIT checkFailed(i18n("Folder name not defined.")); 0055 return; 0056 } 0057 0058 auto fetchJob = qobject_cast<Akonadi::CollectionFetchJob *>(job); 0059 0060 const Akonadi::Collection::List cols = fetchJob->collections(); 0061 for (const Akonadi::Collection &collection : cols) { 0062 if (collection.name() == folderName) { 0063 Q_EMIT collectionIdFound(collection); 0064 return; 0065 } 0066 } 0067 createNewFolder(folderName); 0068 } 0069 0070 void FolderArchiveAgentCheckCollection::createNewFolder(const QString &name) 0071 { 0072 Akonadi::Collection parentCollection(mInfo->archiveTopLevel()); 0073 Akonadi::Collection collection; 0074 collection.setParentCollection(parentCollection); 0075 collection.setName(name); 0076 collection.setContentMimeTypes(QStringList() << QStringLiteral("message/rfc822")); 0077 0078 auto job = new Akonadi::CollectionCreateJob(collection); 0079 connect(job, &Akonadi::CollectionCreateJob::result, this, &FolderArchiveAgentCheckCollection::slotCreateNewFolder); 0080 } 0081 0082 void FolderArchiveAgentCheckCollection::slotCreateNewFolder(KJob *job) 0083 { 0084 if (job->error()) { 0085 qCWarning(KMAIL_LOG) << job->errorString(); 0086 Q_EMIT checkFailed(i18n("Unable to create folder. %1", job->errorString())); 0087 return; 0088 } 0089 auto createJob = qobject_cast<Akonadi::CollectionCreateJob *>(job); 0090 Q_EMIT collectionIdFound(createJob->collection()); 0091 } 0092 0093 #include "moc_folderarchiveagentcheckcollection.cpp"