1 /*
2 SPDX-FileCopyrightText: 2012-2021 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "archivejob.h"
8 #include "archivemailagent_debug.h"
9 #include "archivemailinfo.h"
10 #include "archivemailkernel.h"
11 #include "archivemailmanager.h"
12
13 #include <MailCommon/BackupJob>
14 #include <MailCommon/MailUtil>
15
16 #include <Akonadi/EntityMimeTypeFilterModel>
17
18 #include <KLocalizedString>
19 #include <KNotification>
20
ArchiveJob(ArchiveMailManager * manager,ArchiveMailInfo * info,const Akonadi::Collection & folder,bool immediate)21 ArchiveJob::ArchiveJob(ArchiveMailManager *manager, ArchiveMailInfo *info, const Akonadi::Collection &folder, bool immediate)
22 : MailCommon::ScheduledJob(folder, immediate)
23 , mInfo(info)
24 , mManager(manager)
25 {
26 mDefaultIconName = QStringLiteral("kmail");
27 }
28
~ArchiveJob()29 ArchiveJob::~ArchiveJob()
30 {
31 delete mInfo;
32 }
33
execute()34 void ArchiveJob::execute()
35 {
36 if (mInfo) {
37 Akonadi::Collection collection(mInfo->saveCollectionId());
38 const QString realPath = MailCommon::Util::fullCollectionPath(collection);
39 if (realPath.isEmpty()) {
40 qCDebug(ARCHIVEMAILAGENT_LOG) << " We cannot find real path, collection doesn't exist";
41 mManager->collectionDoesntExist(mInfo);
42 deleteLater();
43 return;
44 }
45 if (mInfo->url().isEmpty()) {
46 qCDebug(ARCHIVEMAILAGENT_LOG) << " Path is empty";
47 mManager->collectionDoesntExist(mInfo);
48 deleteLater();
49 return;
50 }
51
52 bool dirExit = true;
53 const QUrl archivePath = mInfo->realUrl(realPath, dirExit);
54 if (!dirExit) {
55 mManager->backupDone(mInfo);
56 KNotification::event(QStringLiteral("archivemailfolderdoesntexist"),
57 QString(),
58 i18n("Directory does not exist. Please verify settings. Archive postponed."),
59 mDefaultIconName,
60 nullptr,
61 KNotification::CloseOnTimeout,
62 QStringLiteral("akonadi_archivemail_agent"));
63 deleteLater();
64 return;
65 }
66
67 auto backupJob = new MailCommon::BackupJob();
68 backupJob->setRootFolder(Akonadi::EntityTreeModel::updatedCollection(mManager->kernel()->collectionModel(), collection));
69
70 backupJob->setSaveLocation(archivePath);
71 backupJob->setArchiveType(mInfo->archiveType());
72 backupJob->setDeleteFoldersAfterCompletion(false);
73 backupJob->setRecursive(mInfo->saveSubCollection());
74 backupJob->setDisplayMessageBox(false);
75 backupJob->setRealPath(realPath);
76 const QString summary = i18n("Start to archive %1", realPath);
77 KNotification::event(QStringLiteral("archivemailstarted"),
78 QString(),
79 summary,
80 mDefaultIconName,
81 nullptr,
82 KNotification::CloseOnTimeout,
83 QStringLiteral("akonadi_archivemail_agent"));
84 connect(backupJob, &MailCommon::BackupJob::backupDone, this, &ArchiveJob::slotBackupDone);
85 connect(backupJob, &MailCommon::BackupJob::error, this, &ArchiveJob::slotError);
86 backupJob->start();
87 }
88 }
89
slotError(const QString & error)90 void ArchiveJob::slotError(const QString &error)
91 {
92 KNotification::event(QStringLiteral("archivemailerror"),
93 QString(),
94 error,
95 mDefaultIconName,
96 nullptr,
97 KNotification::CloseOnTimeout,
98 QStringLiteral("akonadi_archivemail_agent"));
99 mManager->backupDone(mInfo);
100 deleteLater();
101 }
102
slotBackupDone(const QString & info)103 void ArchiveJob::slotBackupDone(const QString &info)
104 {
105 KNotification::event(QStringLiteral("archivemailfinished"),
106 QString(),
107 info,
108 mDefaultIconName,
109 nullptr,
110 KNotification::CloseOnTimeout,
111 QStringLiteral("akonadi_archivemail_agent"));
112 mManager->backupDone(mInfo);
113 deleteLater();
114 }
115
kill()116 void ArchiveJob::kill()
117 {
118 ScheduledJob::kill();
119 }
120
run()121 MailCommon::ScheduledJob *ScheduledArchiveTask::run()
122 {
123 return folder().isValid() ? new ArchiveJob(mManager, mInfo, folder(), isImmediate()) : nullptr;
124 }
125