archivemailwidget.cpp 12.2 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
   SPDX-FileCopyrightText: 2015-2021 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3

4
   SPDX-License-Identifier: GPL-2.0-or-later
5
6
7
8
9
*/

#include "archivemailwidget.h"
#include "addarchivemaildialog.h"
#include "archivemailagentutil.h"
10
#include "archivemailkernel.h"
11

Laurent Montel's avatar
Laurent Montel committed
12
#include <KIO/JobUiDelegate>
Laurent Montel's avatar
Laurent Montel committed
13
#include <KIO/OpenUrlJob>
Laurent Montel's avatar
Laurent Montel committed
14

15
#include "kmail-version.h"
16

17
#include <MailCommon/MailKernel>
Laurent Montel's avatar
Laurent Montel committed
18
#include <MailCommon/MailUtil>
19

Laurent Montel's avatar
Laurent Montel committed
20
#include <KAboutData>
21
#include <KConfigGroup>
Laurent Montel's avatar
Laurent Montel committed
22
#include <KLocalizedString>
23
#include <KMessageBox>
Laurent Montel's avatar
Laurent Montel committed
24
25
#include <KSharedConfig>
#include <QLocale>
26
27
#include <QMenu>

Laurent Montel's avatar
Laurent Montel committed
28
29
namespace
{
30
31
inline QString archiveMailCollectionPattern()
{
Laurent Montel's avatar
Laurent Montel committed
32
    return QStringLiteral("ArchiveMailCollection \\d+");
33
}
34
35

static const char myConfigGroupName[] = "ArchiveMailDialog";
36
37
38
}

ArchiveMailItem::ArchiveMailItem(QTreeWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
39
    : QTreeWidgetItem(parent)
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{
}

ArchiveMailItem::~ArchiveMailItem()
{
    delete mInfo;
}

void ArchiveMailItem::setInfo(ArchiveMailInfo *info)
{
    mInfo = info;
}

ArchiveMailInfo *ArchiveMailItem::info() const
{
    return mInfo;
}

Laurent Montel's avatar
Laurent Montel committed
58
ArchiveMailWidget::ArchiveMailWidget(const KSharedConfigPtr &config, QWidget *parent, const QVariantList &args)
59
    : Akonadi::AgentConfigurationBase(config, parent, args)
60
{
61
    ArchiveMailKernel *archiveMailKernel = ArchiveMailKernel::self();
Laurent Montel's avatar
Laurent Montel committed
62
63
    CommonKernel->registerKernelIf(archiveMailKernel); // register KernelIf early, it is used by the Filter classes
    CommonKernel->registerSettingsIf(archiveMailKernel); // SettingsIf is used in FolderTreeWidget
64

Laurent Montel's avatar
Laurent Montel committed
65
    auto w = new QWidget(parent);
Laurent Montel's avatar
Laurent Montel committed
66
67
    mWidget.setupUi(w);
    parent->layout()->addWidget(w);
68

69
70
    QStringList headers;
    headers << i18n("Name") << i18n("Last archive") << i18n("Next archive in") << i18n("Storage directory");
71
72
73
74
75
76
77
    mWidget.treeWidget->setHeaderLabels(headers);
    mWidget.treeWidget->setObjectName(QStringLiteral("treewidget"));
    mWidget.treeWidget->setSortingEnabled(true);
    mWidget.treeWidget->setRootIsDecorated(false);
    mWidget.treeWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
    mWidget.treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);

Laurent Montel's avatar
Laurent Montel committed
78
    connect(mWidget.treeWidget, &QWidget::customContextMenuRequested, this, &ArchiveMailWidget::slotCustomContextMenuRequested);
79

80
    connect(mWidget.deleteItem, &QAbstractButton::clicked, this, &ArchiveMailWidget::slotDeleteItem);
81
82
83
84
85
    connect(mWidget.modifyItem, &QAbstractButton::clicked, this, &ArchiveMailWidget::slotModifyItem);
    connect(mWidget.addItem, &QAbstractButton::clicked, this, &ArchiveMailWidget::slotAddItem);
    connect(mWidget.treeWidget, &QTreeWidget::itemChanged, this, &ArchiveMailWidget::slotItemChanged);
    connect(mWidget.treeWidget, &QTreeWidget::itemSelectionChanged, this, &ArchiveMailWidget::updateButtons);
    connect(mWidget.treeWidget, &QTreeWidget::itemDoubleClicked, this, &ArchiveMailWidget::slotModifyItem);
86
    updateButtons();
87

Laurent Montel's avatar
Laurent Montel committed
88
89
90
91
92
    KAboutData aboutData(QStringLiteral("archivemailagent"),
                         i18n("Archive Mail Agent"),
                         QStringLiteral(KDEPIM_VERSION),
                         i18n("Archive emails automatically."),
                         KAboutLicense::GPL_V2,
Laurent Montel's avatar
Laurent Montel committed
93
                         i18n("Copyright (C) 2014-2021 Laurent Montel"));
Laurent Montel's avatar
Laurent Montel committed
94
95
96
    aboutData.addAuthor(i18n("Laurent Montel"), i18n("Maintainer"), QStringLiteral("montel@kde.org"));

    aboutData.setTranslator(i18nc("NAME OF TRANSLATORS", "Your names"), i18nc("EMAIL OF TRANSLATORS", "Your emails"));
Laurent Montel's avatar
Laurent Montel committed
97
    aboutData.setProductName(QByteArrayLiteral("Akonadi/Archive Mail Agent"));
98
    setKAboutData(aboutData);
99
100
}

Laurent Montel's avatar
Laurent Montel committed
101
ArchiveMailWidget::~ArchiveMailWidget() = default;
102

Laurent Montel's avatar
Laurent Montel committed
103
void ArchiveMailWidget::slotCustomContextMenuRequested(const QPoint &)
104
{
105
106
    const QList<QTreeWidgetItem *> listItems = mWidget.treeWidget->selectedItems();
    QMenu menu(parentWidget());
Laurent Montel's avatar
Laurent Montel committed
107
    menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add..."), this, &ArchiveMailWidget::slotAddItem);
108
109
    if (!listItems.isEmpty()) {
        if (listItems.count() == 1) {
Laurent Montel's avatar
Laurent Montel committed
110
            menu.addSeparator();
Laurent Montel's avatar
Laurent Montel committed
111
            menu.addAction(i18n("Open Containing Folder..."), this, &ArchiveMailWidget::slotOpenFolder);
112
113
        }
        menu.addSeparator();
114
        menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("Delete"), this, &ArchiveMailWidget::slotDeleteItem);
115
116
117
118
119
120
    }
    menu.exec(QCursor::pos());
}

void ArchiveMailWidget::updateButtons()
{
121
    const QList<QTreeWidgetItem *> listItems = mWidget.treeWidget->selectedItems();
122
    if (listItems.isEmpty()) {
123
        mWidget.deleteItem->setEnabled(false);
124
        mWidget.modifyItem->setEnabled(false);
125
    } else if (listItems.count() == 1) {
126
        mWidget.deleteItem->setEnabled(true);
127
        mWidget.modifyItem->setEnabled(true);
128
    } else {
129
        mWidget.deleteItem->setEnabled(true);
130
        mWidget.modifyItem->setEnabled(false);
131
132
133
134
135
    }
}

void ArchiveMailWidget::needReloadConfig()
{
Laurent Montel's avatar
Laurent Montel committed
136
    // TODO add messagebox which informs that we save settings here.
137
    mWidget.treeWidget->clear();
138
139
140
141
142
    load();
}

void ArchiveMailWidget::load()
{
143
144
145
146
    const auto group = config()->group(myConfigGroupName);
    mWidget.treeWidget->header()->restoreState(group.readEntry("HeaderState", QByteArray()));

    const QStringList collectionList = config()->groupList().filter(QRegularExpression(archiveMailCollectionPattern()));
147
148
    const int numberOfCollection = collectionList.count();
    for (int i = 0; i < numberOfCollection; ++i) {
Laurent Montel's avatar
Laurent Montel committed
149
        KConfigGroup collectionGroup = config()->group(collectionList.at(i));
150
        auto info = new ArchiveMailInfo(collectionGroup);
151
152
153
154
155
156
157
158
159
160
161
        if (info->isValid()) {
            createOrUpdateItem(info);
        } else {
            delete info;
        }
    }
}

void ArchiveMailWidget::createOrUpdateItem(ArchiveMailInfo *info, ArchiveMailItem *item)
{
    if (!item) {
162
        item = new ArchiveMailItem(mWidget.treeWidget);
163
    }
Laurent Montel's avatar
Laurent Montel committed
164
165
166
    const QString folderName = i18n("Folder: %1", MailCommon::Util::fullCollectionPath(Akonadi::Collection(info->saveCollectionId())));
    item->setText(ArchiveMailWidget::Name, folderName);
    item->setToolTip(ArchiveMailWidget::Name, folderName);
167
    item->setCheckState(ArchiveMailWidget::Name, info->isEnabled() ? Qt::Checked : Qt::Unchecked);
Laurent Montel's avatar
Laurent Montel committed
168
169
170
    const QString path{info->url().toLocalFile()};
    item->setText(ArchiveMailWidget::StorageDirectory, path);
    item->setToolTip(ArchiveMailWidget::StorageDirectory, path);
171
    if (info->lastDateSaved().isValid()) {
Laurent Montel's avatar
Laurent Montel committed
172
173
174
        const QString date{QLocale().toString(info->lastDateSaved(), QLocale::ShortFormat)};
        item->setText(ArchiveMailWidget::LastArchiveDate, date);
        item->setToolTip(ArchiveMailWidget::LastArchiveDate, date);
175
176
        updateDiffDate(item, info);
    } else {
177
        item->setBackground(ArchiveMailWidget::NextArchive, Qt::green);
178
179
180
181
182
183
184
    }
    item->setInfo(info);
}

void ArchiveMailWidget::updateDiffDate(ArchiveMailItem *item, ArchiveMailInfo *info)
{
    const QDate diffDate = ArchiveMailAgentUtil::diffDate(info);
Laurent Montel's avatar
Laurent Montel committed
185
    const qint64 diff = QDate::currentDate().daysTo(diffDate);
Laurent Montel's avatar
Laurent Montel committed
186
187
    const QString dateStr{i18np("Tomorrow", "%1 days", diff)};
    item->setText(ArchiveMailWidget::NextArchive, dateStr);
188
189
    if (diff < 0) {
        if (info->isEnabled()) {
190
            item->setBackground(ArchiveMailWidget::NextArchive, Qt::red);
191
        } else {
192
            item->setBackground(ArchiveMailWidget::NextArchive, Qt::lightGray);
193
194
195
196
197
198
        }
    } else {
        item->setToolTip(ArchiveMailWidget::NextArchive, i18n("Archive will be done %1", QLocale().toString(diffDate, QLocale::ShortFormat)));
    }
}

199
bool ArchiveMailWidget::save() const
200
201
{
    if (!mChanged) {
202
        return false;
203
204
205
    }

    // first, delete all filter groups:
206
    const QStringList filterGroups = config()->groupList().filter(QRegularExpression(archiveMailCollectionPattern()));
207

Laurent Montel's avatar
Laurent Montel committed
208
    for (const QString &group : filterGroups) {
209
        config()->deleteGroup(group);
210
211
    }

212
    const int numberOfItem(mWidget.treeWidget->topLevelItemCount());
213
    for (int i = 0; i < numberOfItem; ++i) {
214
        auto mailItem = static_cast<ArchiveMailItem *>(mWidget.treeWidget->topLevelItem(i));
215
        if (mailItem->info()) {
216
            KConfigGroup group = config()->group(ArchiveMailAgentUtil::archivePattern.arg(mailItem->info()->saveCollectionId()));
217
218
219
            mailItem->info()->writeConfig(group);
        }
    }
220
221
222
223
224

    auto group = config()->group(myConfigGroupName);
    group.writeEntry("HeaderState", mWidget.treeWidget->header()->saveState());

    return true;
225
226
}

227
void ArchiveMailWidget::slotDeleteItem()
228
{
229
    const QList<QTreeWidgetItem *> listItems = mWidget.treeWidget->selectedItems();
230
231
232
233
234
    const int answer = KMessageBox::warningYesNo(parentWidget(),
                                                 i18n("Do you want to delete the selected items?"), i18nc("@title:window", "Delete Items"),
                                                 KStandardGuiItem::del(),
                                                 KStandardGuiItem::cancel());
    if (answer == KMessageBox::No) {
235
236
237
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
238
    for (QTreeWidgetItem *item : listItems) {
239
240
241
242
243
244
245
246
        delete item;
    }
    mChanged = true;
    updateButtons();
}

void ArchiveMailWidget::slotModifyItem()
{
247
    const QList<QTreeWidgetItem *> listItems = mWidget.treeWidget->selectedItems();
248
249
250
251
252
    if (listItems.count() == 1) {
        QTreeWidgetItem *item = listItems.at(0);
        if (!item) {
            return;
        }
253
        auto archiveItem = static_cast<ArchiveMailItem *>(item);
254
        QPointer<AddArchiveMailDialog> dialog = new AddArchiveMailDialog(archiveItem->info(), parentWidget());
255
256
257
258
259
260
261
262
263
264
265
        if (dialog->exec()) {
            ArchiveMailInfo *info = dialog->info();
            createOrUpdateItem(info, archiveItem);
            mChanged = true;
        }
        delete dialog;
    }
}

void ArchiveMailWidget::slotAddItem()
{
266
    QPointer<AddArchiveMailDialog> dialog = new AddArchiveMailDialog(nullptr, parentWidget());
267
268
269
    if (dialog->exec()) {
        ArchiveMailInfo *info = dialog->info();
        if (verifyExistingArchive(info)) {
270
            KMessageBox::error(parentWidget(), i18n("Cannot add a second archive for this folder. Modify the existing one instead."), i18n("Add Archive Mail"));
271
272
273
274
275
276
277
278
279
280
281
282
            delete info;
        } else {
            createOrUpdateItem(info);
            updateButtons();
            mChanged = true;
        }
    }
    delete dialog;
}

bool ArchiveMailWidget::verifyExistingArchive(ArchiveMailInfo *info) const
{
283
    const int numberOfItem(mWidget.treeWidget->topLevelItemCount());
284
    for (int i = 0; i < numberOfItem; ++i) {
285
        auto mailItem = static_cast<ArchiveMailItem *>(mWidget.treeWidget->topLevelItem(i));
286
287
288
289
290
291
292
293
294
295
296
297
        ArchiveMailInfo *archiveItemInfo = mailItem->info();
        if (archiveItemInfo) {
            if (info->saveCollectionId() == archiveItemInfo->saveCollectionId()) {
                return true;
            }
        }
    }
    return false;
}

void ArchiveMailWidget::slotOpenFolder()
{
298
    const QList<QTreeWidgetItem *> listItems = mWidget.treeWidget->selectedItems();
299
300
301
302
303
    if (listItems.count() == 1) {
        QTreeWidgetItem *item = listItems.first();
        if (!item) {
            return;
        }
304
        auto archiveItem = static_cast<ArchiveMailItem *>(item);
305
306
307
        ArchiveMailInfo *archiveItemInfo = archiveItem->info();
        if (archiveItemInfo) {
            const QUrl url = archiveItemInfo->url();
308
            auto job = new KIO::OpenUrlJob(url);
Laurent Montel's avatar
Laurent Montel committed
309
310
311
            job->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, parentWidget()));
            job->setRunExecutables(false);
            job->start();
312
313
314
315
316
317
318
        }
    }
}

void ArchiveMailWidget::slotItemChanged(QTreeWidgetItem *item, int col)
{
    if (item) {
319
        auto archiveItem = static_cast<ArchiveMailItem *>(item);
320
321
322
323
324
325
326
327
328
329
        if (archiveItem->info()) {
            if (col == ArchiveMailWidget::Name) {
                archiveItem->info()->setEnabled(archiveItem->checkState(ArchiveMailWidget::Name) == Qt::Checked);
                mChanged = true;
            } else if (col == ArchiveMailWidget::NextArchive) {
                updateDiffDate(archiveItem, archiveItem->info());
            }
        }
    }
}
330
331
332
333
334
335
336
337
338
339
340
341
342

QSize ArchiveMailWidget::restoreDialogSize() const
{
    auto group = config()->group(myConfigGroupName);
    const QSize size = group.readEntry("Size", QSize(500, 300));
    return size;
}

void ArchiveMailWidget::saveDialogSize(const QSize &size)
{
    auto group = config()->group(myConfigGroupName);
    group.writeEntry("Size", size);
}