]> cloud.milkyroute.net Git - dolphin.git/blob - src/trash/dolphintrash.cpp
SVN_SILENT made messages (.desktop file) - always resolve ours
[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 #include <Solid/Device>
16 #include <Solid/DeviceNotifier>
17 #include <Solid/StorageAccess>
18
19 #include <QList>
20
21 Trash::Trash()
22 : m_trashDirLister(new KDirLister())
23 {
24 // The trash icon must always be updated dependent on whether
25 // the trash is empty or not. We use a KDirLister that automatically
26 // watches for changes if the number of items has been changed.
27 m_trashDirLister->setAutoErrorHandlingEnabled(false);
28 m_trashDirLister->setDelayedMimeTypes(true);
29 auto trashDirContentChanged = [this]() {
30 bool isTrashEmpty = m_trashDirLister->items().isEmpty();
31 Q_EMIT emptinessChanged(isTrashEmpty);
32 };
33 connect(m_trashDirLister, &KCoreDirLister::completed, this, trashDirContentChanged);
34 connect(m_trashDirLister, &KDirLister::itemsDeleted, this, trashDirContentChanged);
35
36 // Update trash directory when removable storage devices are changed to keep it in sync with the
37 // storage device .Trash-1000 folders
38 Solid::DeviceNotifier *notifier = Solid::DeviceNotifier::instance();
39 connect(notifier, &Solid::DeviceNotifier::deviceAdded, this, [this](const QString &device) {
40 const Solid::Device dev(device);
41 if (dev.isValid() && dev.is<Solid::StorageAccess>()) {
42 const Solid::StorageAccess *access = dev.as<Solid::StorageAccess>();
43 connect(access, &Solid::StorageAccess::accessibilityChanged, this, [this]() {
44 m_trashDirLister->updateDirectory(QUrl(QStringLiteral("trash:/")));
45 });
46 }
47 });
48 connect(notifier, &Solid::DeviceNotifier::deviceRemoved, this, [this](const QString &device) {
49 const Solid::Device dev(device);
50 if (dev.isValid() && dev.is<Solid::StorageAccess>()) {
51 m_trashDirLister->updateDirectory(QUrl(QStringLiteral("trash:/")));
52 }
53 });
54
55 m_trashDirLister->openUrl(QUrl(QStringLiteral("trash:/")));
56 }
57
58 Trash::~Trash()
59 {
60 delete m_trashDirLister;
61 }
62
63 Trash &Trash::instance()
64 {
65 static Trash result;
66 return result;
67 }
68
69 static void notifyEmptied()
70 {
71 // As long as KIO doesn't do this, do it ourselves
72 KNotification::event(QStringLiteral("Trash: emptied"),
73 i18n("Trash Emptied"),
74 i18n("The Trash was emptied."),
75 QStringLiteral("user-trash"),
76 KNotification::DefaultEvent);
77 }
78
79 void Trash::empty(QWidget *window)
80 {
81 using Iface = KIO::AskUserActionInterface;
82 auto *emptyJob = new KIO::DeleteOrTrashJob(QList<QUrl>{}, Iface::EmptyTrash, Iface::DefaultConfirmation, window);
83 QObject::connect(emptyJob, &KIO::Job::result, notifyEmptied);
84 emptyJob->start();
85 }
86
87 bool Trash::isEmpty()
88 {
89 KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig);
90 return (trashConfig.group(QStringLiteral("Status")).readEntry("Empty", true));
91 }
92
93 #include "moc_dolphintrash.cpp"