File indexing completed on 2024-04-28 17:03:20

0001 /*
0002  * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
0003  * SPDX-FileCopyrightText: 2018 Roman Inflianskas <infroma@gmail.com>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include "dolphintrash.h"
0009 
0010 #include <KConfig>
0011 #include <KConfigGroup>
0012 #include <KIO/DeleteOrTrashJob>
0013 #include <KLocalizedString>
0014 #include <KNotification>
0015 
0016 #include <QList>
0017 
0018 Trash::Trash()
0019     : m_trashDirLister(new KDirLister())
0020 {
0021     // The trash icon must always be updated dependent on whether
0022     // the trash is empty or not. We use a KDirLister that automatically
0023     // watches for changes if the number of items has been changed.
0024     m_trashDirLister->setAutoErrorHandlingEnabled(false);
0025     m_trashDirLister->setDelayedMimeTypes(true);
0026     auto trashDirContentChanged = [this]() {
0027         bool isTrashEmpty = m_trashDirLister->items().isEmpty();
0028         Q_EMIT emptinessChanged(isTrashEmpty);
0029     };
0030     connect(m_trashDirLister, &KCoreDirLister::completed, this, trashDirContentChanged);
0031     connect(m_trashDirLister, &KDirLister::itemsDeleted, this, trashDirContentChanged);
0032     m_trashDirLister->openUrl(QUrl(QStringLiteral("trash:/")));
0033 }
0034 
0035 Trash::~Trash()
0036 {
0037     delete m_trashDirLister;
0038 }
0039 
0040 Trash &Trash::instance()
0041 {
0042     static Trash result;
0043     return result;
0044 }
0045 
0046 static void notifyEmptied()
0047 {
0048     // As long as KIO doesn't do this, do it ourselves
0049     KNotification::event(QStringLiteral("Trash: emptied"),
0050                          i18n("Trash Emptied"),
0051                          i18n("The Trash was emptied."),
0052                          QStringLiteral("user-trash"),
0053                          KNotification::DefaultEvent);
0054 }
0055 
0056 void Trash::empty(QWidget *window)
0057 {
0058     using Iface = KIO::AskUserActionInterface;
0059     auto *emptyJob = new KIO::DeleteOrTrashJob(QList<QUrl>{}, Iface::EmptyTrash, Iface::DefaultConfirmation, window);
0060     QObject::connect(emptyJob, &KIO::Job::result, notifyEmptied);
0061     emptyJob->start();
0062 }
0063 
0064 bool Trash::isEmpty()
0065 {
0066     KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig);
0067     return (trashConfig.group(QStringLiteral("Status")).readEntry("Empty", true));
0068 }
0069 
0070 #include "moc_dolphintrash.cpp"