]> cloud.milkyroute.net Git - dolphin.git/blob - src/trash/dolphintrash.cpp
Merge branch 'release/20.08'
[dolphin.git] / src / trash / dolphintrash.cpp
1 /*
2 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2018 Roman Inflianskas <infroma@gmail.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include "dolphintrash.h"
9
10 #include <KIO/JobUiDelegate>
11 #include <KJobWidgets>
12 #include <QList>
13 #include <KNotification>
14 #include <KConfig>
15 #include <KConfigGroup>
16 #include <KLocalizedString>
17
18 Trash::Trash()
19 : m_trashDirLister(new KDirLister())
20 {
21 // The trash icon must always be updated dependent on whether
22 // the trash is empty or not. We use a KDirLister that automatically
23 // watches for changes if the number of items has been changed.
24 m_trashDirLister->setAutoErrorHandlingEnabled(false, nullptr);
25 m_trashDirLister->setDelayedMimeTypes(true);
26 auto trashDirContentChanged = [this]() {
27 bool isTrashEmpty = m_trashDirLister->items().isEmpty();
28 emit emptinessChanged(isTrashEmpty);
29 };
30 connect(m_trashDirLister, QOverload<>::of(&KCoreDirLister::completed), this, trashDirContentChanged);
31 connect(m_trashDirLister, &KDirLister::itemsDeleted, this, trashDirContentChanged);
32 m_trashDirLister->openUrl(QUrl(QStringLiteral("trash:/")));
33 }
34
35 Trash::~Trash()
36 {
37 delete m_trashDirLister;
38 }
39
40 Trash &Trash::instance()
41 {
42 static Trash result;
43 return result;
44 }
45
46 KIO::Job *Trash::empty(QWidget *window)
47 {
48 KIO::JobUiDelegate uiDelegate;
49 uiDelegate.setWindow(window);
50 bool confirmed = uiDelegate.askDeleteConfirmation(QList<QUrl>(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation);
51 if (confirmed) {
52 KIO::Job* job = KIO::emptyTrash();
53 KJobWidgets::setWindow(job, window);
54 job->uiDelegate()->setAutoErrorHandlingEnabled(true);
55 // as long as KIO doesn't do this, do it ourselves
56 connect(job, &KIO::Job::result, []() {
57 KNotification::event(QStringLiteral("Trash: emptied"), i18n("Trash Emptied"),
58 i18n("The Trash was emptied."), QStringLiteral("user-trash"),
59 nullptr, KNotification::DefaultEvent);
60 });
61 return job;
62 }
63 return nullptr;
64 }
65
66 bool Trash::isEmpty()
67 {
68 KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig);
69 return (trashConfig.group("Status").readEntry("Empty", true));
70 }
71