]> cloud.milkyroute.net Git - dolphin.git/blob - src/trash/dolphintrash.cpp
957091e548ceef298d821abcbb3513748b572492
[dolphin.git] / src / trash / dolphintrash.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2018 by Roman Inflianskas <infroma@gmail.com> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphintrash.h"
22
23 #include <KIO/JobUiDelegate>
24 #include <KJobWidgets>
25 #include <QList>
26 #include <KNotification>
27 #include <KConfig>
28 #include <KConfigGroup>
29 #include <KLocalizedString>
30
31 Trash::Trash()
32 : m_trashDirLister(new KDirLister())
33 {
34 // The trash icon must always be updated dependent on whether
35 // the trash is empty or not. We use a KDirLister that automatically
36 // watches for changes if the number of items has been changed.
37 m_trashDirLister->setAutoErrorHandlingEnabled(false, nullptr);
38 m_trashDirLister->setDelayedMimeTypes(true);
39 auto trashDirContentChanged = [this]() {
40 bool isTrashEmpty = m_trashDirLister->items().isEmpty();
41 emit emptinessChanged(isTrashEmpty);
42 };
43 connect(m_trashDirLister, QOverload<>::of(&KCoreDirLister::completed), this, trashDirContentChanged);
44 connect(m_trashDirLister, &KDirLister::itemsDeleted, this, trashDirContentChanged);
45 m_trashDirLister->openUrl(QUrl(QStringLiteral("trash:/")));
46 }
47
48 Trash::~Trash()
49 {
50 delete m_trashDirLister;
51 }
52
53 Trash &Trash::instance()
54 {
55 static Trash result;
56 return result;
57 }
58
59 KIO::Job *Trash::empty(QWidget *window)
60 {
61 KIO::JobUiDelegate uiDelegate;
62 uiDelegate.setWindow(window);
63 bool confirmed = uiDelegate.askDeleteConfirmation(QList<QUrl>(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation);
64 if (confirmed) {
65 KIO::Job* job = KIO::emptyTrash();
66 KJobWidgets::setWindow(job, window);
67 job->uiDelegate()->setAutoErrorHandlingEnabled(true);
68 // as long as KIO doesn't do this, do it ourselves
69 connect(job, &KIO::Job::result, []() {
70 KNotification::event(QStringLiteral("Trash: emptied"), i18n("Trash Emptied"),
71 i18n("The Trash was emptied."), QStringLiteral("user-trash"),
72 nullptr, KNotification::DefaultEvent);
73 });
74 return job;
75 }
76 return nullptr;
77 }
78
79 bool Trash::isEmpty()
80 {
81 KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig);
82 return (trashConfig.group("Status").readEntry("Empty", true));
83 }
84