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