]> cloud.milkyroute.net Git - dolphin.git/blob - src/trash/dolphintrash.cpp
dd71135a52b166cd980d904d2cde73b6cd1df188
[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 <KConfig>
11 #include <KConfigGroup>
12 #include <KIO/DeleteOrTrashJob>
13 #include <KLocalizedString>
14 #include <KNotification>
15
16 #include <QList>
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);
25 m_trashDirLister->setDelayedMimeTypes(true);
26 auto trashDirContentChanged = [this]() {
27 bool isTrashEmpty = m_trashDirLister->items().isEmpty();
28 Q_EMIT emptinessChanged(isTrashEmpty);
29 };
30 connect(m_trashDirLister, &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 static void notifyEmptied()
47 {
48 // As long as KIO doesn't do this, do it ourselves
49 KNotification::event(QStringLiteral("Trash: emptied"),
50 i18n("Trash Emptied"),
51 i18n("The Trash was emptied."),
52 QStringLiteral("user-trash"),
53 KNotification::DefaultEvent);
54 }
55
56 void Trash::empty(QWidget *window)
57 {
58 using Iface = KIO::AskUserActionInterface;
59 auto *emptyJob = new KIO::DeleteOrTrashJob(QList<QUrl>{}, Iface::EmptyTrash, Iface::DefaultConfirmation, window);
60 QObject::connect(emptyJob, &KIO::Job::result, notifyEmptied);
61 emptyJob->start();
62 }
63
64 bool Trash::isEmpty()
65 {
66 KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig);
67 return (trashConfig.group("Status").readEntry("Empty", true));
68 }
69
70 #include "moc_dolphintrash.cpp"