]> cloud.milkyroute.net Git - dolphin.git/blob - src/trash/dolphintrash.cpp
Copy Location: Make sure to export path with native separators
[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 <KLocalizedString>
13 #include <KNotification>
14 #include <QList>
15
16 #include <KIO/DeleteOrTrashJob>
17 #include <kio_version.h>
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 static void notifyEmptied()
48 {
49 // As long as KIO doesn't do this, do it ourselves
50 KNotification::event(QStringLiteral("Trash: emptied"),
51 i18n("Trash Emptied"),
52 i18n("The Trash was emptied."),
53 QStringLiteral("user-trash"),
54 nullptr,
55 KNotification::DefaultEvent);
56 }
57
58 void Trash::empty(QWidget *window)
59 {
60 using Iface = KIO::AskUserActionInterface;
61 auto *emptyJob = new KIO::DeleteOrTrashJob(QList<QUrl>{}, Iface::EmptyTrash, Iface::DefaultConfirmation, window);
62 QObject::connect(emptyJob, &KIO::Job::result, notifyEmptied);
63 emptyJob->start();
64 }
65
66 bool Trash::isEmpty()
67 {
68 KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig);
69 return (trashConfig.group("Status").readEntry("Empty", true));
70 }
71
72 #include "moc_dolphintrash.cpp"