File indexing completed on 2024-12-22 05:00:50

0001 /*
0002    SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "archivejob.h"
0008 #include "archivemailagent_debug.h"
0009 #include "archivemailinfo.h"
0010 #include "archivemailkernel.h"
0011 #include "archivemailmanager.h"
0012 
0013 #include <MailCommon/BackupJob>
0014 #include <MailCommon/MailUtil>
0015 
0016 #include <Akonadi/EntityMimeTypeFilterModel>
0017 
0018 #include <KLocalizedString>
0019 #include <KNotification>
0020 
0021 ArchiveJob::ArchiveJob(ArchiveMailManager *manager, ArchiveMailInfo *info, const Akonadi::Collection &folder, bool immediate)
0022     : MailCommon::ScheduledJob(folder, immediate)
0023     , mDefaultIconName(QStringLiteral("kmail"))
0024     , mInfo(info)
0025     , mManager(manager)
0026 {
0027 }
0028 
0029 ArchiveJob::~ArchiveJob()
0030 {
0031     delete mInfo;
0032 }
0033 
0034 void ArchiveJob::execute()
0035 {
0036     if (mInfo) {
0037         Akonadi::Collection collection(mInfo->saveCollectionId());
0038         const QString realPath = MailCommon::Util::fullCollectionPath(collection);
0039         if (realPath.isEmpty()) {
0040             qCDebug(ARCHIVEMAILAGENT_LOG) << " We cannot find real path, collection doesn't exist";
0041             mManager->collectionDoesntExist(mInfo);
0042             deleteLater();
0043             return;
0044         }
0045         if (mInfo->url().isEmpty()) {
0046             qCDebug(ARCHIVEMAILAGENT_LOG) << " Path is empty";
0047             mManager->collectionDoesntExist(mInfo);
0048             deleteLater();
0049             return;
0050         }
0051 
0052         bool dirExit = true;
0053         const QUrl archivePath = mInfo->realUrl(realPath, dirExit);
0054         if (!dirExit) {
0055             mManager->backupDone(mInfo);
0056             KNotification::event(QStringLiteral("archivemailfolderdoesntexist"),
0057                                  QString(),
0058                                  i18n("Directory does not exist. Please verify settings. Archive postponed."),
0059                                  mDefaultIconName,
0060                                  KNotification::CloseOnTimeout,
0061                                  QStringLiteral("akonadi_archivemail_agent"));
0062             deleteLater();
0063             return;
0064         }
0065 
0066         auto backupJob = new MailCommon::BackupJob();
0067         backupJob->setRootFolder(Akonadi::EntityTreeModel::updatedCollection(mManager->kernel()->collectionModel(), collection));
0068 
0069         backupJob->setSaveLocation(archivePath);
0070         backupJob->setArchiveType(mInfo->archiveType());
0071         backupJob->setDeleteFoldersAfterCompletion(false);
0072         backupJob->setRecursive(mInfo->saveSubCollection());
0073         backupJob->setDisplayMessageBox(false);
0074         backupJob->setRealPath(realPath);
0075         const QString summary = i18n("Start to archive %1", realPath);
0076         KNotification::event(QStringLiteral("archivemailstarted"),
0077                              QString(),
0078                              summary,
0079                              mDefaultIconName,
0080                              KNotification::CloseOnTimeout,
0081                              QStringLiteral("akonadi_archivemail_agent"));
0082         connect(backupJob, &MailCommon::BackupJob::backupDone, this, &ArchiveJob::slotBackupDone);
0083         connect(backupJob, &MailCommon::BackupJob::error, this, &ArchiveJob::slotError);
0084         backupJob->start();
0085     }
0086 }
0087 
0088 void ArchiveJob::slotError(const QString &error)
0089 {
0090     KNotification::event(QStringLiteral("archivemailerror"),
0091                          QString(),
0092                          error,
0093                          mDefaultIconName,
0094                          KNotification::CloseOnTimeout,
0095                          QStringLiteral("akonadi_archivemail_agent"));
0096     mManager->backupDone(mInfo);
0097     deleteLater();
0098 }
0099 
0100 void ArchiveJob::slotBackupDone(const QString &info)
0101 {
0102     KNotification::event(QStringLiteral("archivemailfinished"),
0103                          QString(),
0104                          info,
0105                          mDefaultIconName,
0106                          KNotification::CloseOnTimeout,
0107                          QStringLiteral("akonadi_archivemail_agent"));
0108     mManager->backupDone(mInfo);
0109     deleteLater();
0110 }
0111 
0112 void ArchiveJob::kill()
0113 {
0114     ScheduledJob::kill();
0115 }
0116 
0117 MailCommon::ScheduledJob *ScheduledArchiveTask::run()
0118 {
0119     return folder().isValid() ? new ArchiveJob(mManager, mInfo, folder(), isImmediate()) : nullptr;
0120 }
0121 
0122 #include "moc_archivejob.cpp"