]> cloud.milkyroute.net Git - dolphin.git/blob - src/trash/dolphintrash.cpp
Merge branch 'release/22.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 m_trashDirLister->setAutoErrorHandlingEnabled(false);
26 m_trashDirLister->setDelayedMimeTypes(true);
27 auto trashDirContentChanged = [this]() {
28 bool isTrashEmpty = m_trashDirLister->items().isEmpty();
29 Q_EMIT emptinessChanged(isTrashEmpty);
30 };
31 connect(m_trashDirLister, &KCoreDirLister::completed, this, trashDirContentChanged);
32 connect(m_trashDirLister, &KDirLister::itemsDeleted, this, trashDirContentChanged);
33 m_trashDirLister->openUrl(QUrl(QStringLiteral("trash:/")));
34 }
35
36 Trash::~Trash()
37 {
38 delete m_trashDirLister;
39 }
40
41 Trash &Trash::instance()
42 {
43 static Trash result;
44 return result;
45 }
46
47 KIO::Job *Trash::empty(QWidget *window)
48 {
49 KIO::JobUiDelegate uiDelegate;
50 uiDelegate.setWindow(window);
51 bool confirmed = uiDelegate.askDeleteConfirmation(QList<QUrl>(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation);
52 if (confirmed) {
53 KIO::Job* job = KIO::emptyTrash();
54 KJobWidgets::setWindow(job, window);
55 job->uiDelegate()->setAutoErrorHandlingEnabled(true);
56 // as long as KIO doesn't do this, do it ourselves
57 connect(job, &KIO::Job::result, []() {
58 KNotification::event(QStringLiteral("Trash: emptied"), i18n("Trash Emptied"),
59 i18n("The Trash was emptied."), QStringLiteral("user-trash"),
60 nullptr, KNotification::DefaultEvent);
61 });
62 return job;
63 }
64 return nullptr;
65 }
66
67 bool Trash::isEmpty()
68 {
69 KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig);
70 return (trashConfig.group("Status").readEntry("Empty", true));
71 }
72