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