]>
cloud.milkyroute.net Git - dolphin.git/blob - src/trash/dolphintrash.cpp
2 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2018 Roman Inflianskas <infroma@gmail.com>
5 * SPDX-License-Identifier: GPL-2.0-or-later
8 #include "dolphintrash.h"
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>
22 : m_trashDirLister(new KDirLister())
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
);
33 connect(m_trashDirLister
, &KCoreDirLister::completed
, this, trashDirContentChanged
);
34 connect(m_trashDirLister
, &KDirLister::itemsDeleted
, this, trashDirContentChanged
);
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:/")));
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:/")));
55 m_trashDirLister
->openUrl(QUrl(QStringLiteral("trash:/")));
60 delete m_trashDirLister
;
63 Trash
&Trash::instance()
69 static void notifyEmptied()
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
);
79 void Trash::empty(QWidget
*window
)
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
);
89 KConfig
trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig
);
90 return (trashConfig
.group(QStringLiteral("Status")).readEntry("Empty", true));
93 #include "moc_dolphintrash.cpp"