]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Add Trash (empty, isEmpty, emptinessChanged)
authorRoman Inflianskas <infroma@gmail.com>
Sat, 3 Mar 2018 16:44:56 +0000 (19:44 +0300)
committerRoman Inflianskas <infroma@gmail.com>
Sun, 11 Mar 2018 14:32:33 +0000 (17:32 +0300)
Summary: Add `Trash` class to handle all trash operations.

Reviewers: elvisangelaccio, markg, ngraham

Reviewed By: elvisangelaccio, markg, ngraham

Subscribers: ngraham, markg, rkflx, elvisangelaccio, #dolphin

Differential Revision: https://phabricator.kde.org/D11012

src/CMakeLists.txt
src/dolphincontextmenu.cpp
src/dolphinviewcontainer.cpp
src/panels/places/placesitem.cpp
src/panels/places/placesitem.h
src/panels/places/placesitemsignalhandler.cpp
src/panels/places/placesitemsignalhandler.h
src/panels/places/placespanel.cpp
src/panels/places/placespanel.h
src/trash/dolphintrash.cpp [new file with mode: 0644]
src/trash/dolphintrash.h [new file with mode: 0644]

index 89180b78056ba4177fd5dc983b8226efcd7f9c0d..58af19ebd39750f0e5c6d959b265bcb021c00d3f 100644 (file)
@@ -205,6 +205,7 @@ set(dolphinstatic_SRCS
     dolphinrecenttabsmenu.cpp
     dolphintabpage.cpp
     dolphintabwidget.cpp
     dolphinrecenttabsmenu.cpp
     dolphintabpage.cpp
     dolphintabwidget.cpp
+    trash/dolphintrash.cpp
     filterbar/filterbar.cpp
     main.cpp
     panels/information/filemetadataconfigurationdialog.cpp
     filterbar/filterbar.cpp
     main.cpp
     panels/information/filemetadataconfigurationdialog.cpp
index c35f442cba8422dbaecd74f13bd09026c13e537b..c4a66c19a77c0a46c42c1ce1dfd0a2aadacdfa17 100644 (file)
@@ -27,6 +27,7 @@
 #include "dolphinviewcontainer.h"
 #include "panels/places/placesitem.h"
 #include "panels/places/placesitemmodel.h"
 #include "dolphinviewcontainer.h"
 #include "panels/places/placesitem.h"
 #include "panels/places/placesitemmodel.h"
+#include "trash/dolphintrash.h"
 #include "views/dolphinview.h"
 #include "views/viewmodecontroller.h"
 
 #include "views/dolphinview.h"
 #include "views/viewmodecontroller.h"
 
@@ -139,8 +140,7 @@ void DolphinContextMenu::openTrashContextMenu()
     Q_ASSERT(m_context & TrashContext);
 
     QAction* emptyTrashAction = new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this);
     Q_ASSERT(m_context & TrashContext);
 
     QAction* emptyTrashAction = new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this);
-    KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig);
-    emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
+    emptyTrashAction->setEnabled(!Trash::isEmpty());
     addAction(emptyTrashAction);
 
     addCustomActions();
     addAction(emptyTrashAction);
 
     addCustomActions();
@@ -151,13 +151,7 @@ void DolphinContextMenu::openTrashContextMenu()
     addShowMenuBarAction();
 
     if (exec(m_pos) == emptyTrashAction) {
     addShowMenuBarAction();
 
     if (exec(m_pos) == emptyTrashAction) {
-        KIO::JobUiDelegate uiDelegate;
-        uiDelegate.setWindow(m_mainWindow);
-        if (uiDelegate.askDeleteConfirmation(QList<QUrl>(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation)) {
-            KIO::Job* job = KIO::emptyTrash();
-            KJobWidgets::setWindow(job, m_mainWindow);
-            job->uiDelegate()->setAutoErrorHandlingEnabled(true);
-        }
+        Trash::empty(m_mainWindow);
     }
 }
 
     }
 }
 
index 1620f3c368625c804d497111126a61c41940f9cd..6c5edc13f38e80f8d73b7a698a8a3cd3322c8335 100644 (file)
@@ -25,6 +25,7 @@
 #include "global.h"
 #include "search/dolphinsearchbox.h"
 #include "statusbar/dolphinstatusbar.h"
 #include "global.h"
 #include "search/dolphinsearchbox.h"
 #include "statusbar/dolphinstatusbar.h"
+#include "trash/dolphintrash.h"
 #include "views/viewmodecontroller.h"
 #include "views/viewproperties.h"
 
 #include "views/viewmodecontroller.h"
 #include "views/viewproperties.h"
 
index 20c19e1520daae2531acfe463d304cb51fa83abb..ff81e8e63026c5a5ba55746339bb4403b52df6d9 100644 (file)
@@ -21,6 +21,7 @@
  ***************************************************************************/
 
 #include "placesitem.h"
  ***************************************************************************/
 
 #include "placesitem.h"
+#include "trash/dolphintrash.h"
 
 #include "dolphindebug.h"
 #include "placesitemsignalhandler.h"
 
 #include "dolphindebug.h"
 #include "placesitemsignalhandler.h"
@@ -60,16 +61,9 @@ void PlacesItem::setUrl(const QUrl &url)
     if (dataValue("url").toUrl() != url) {
         delete m_trashDirLister;
         if (url.scheme() == QLatin1String("trash")) {
     if (dataValue("url").toUrl() != url) {
         delete m_trashDirLister;
         if (url.scheme() == QLatin1String("trash")) {
-            // The trash icon must always be updated dependent on whether
-            // the trash is empty or not. We use a KDirLister that automatically
-            // watches for changes if the number of items has been changed.
-            // The update of the icon is handled in onTrashDirListerCompleted().
-            m_trashDirLister = new KDirLister();
-            m_trashDirLister->setAutoErrorHandlingEnabled(false, nullptr);
-            m_trashDirLister->setDelayedMimeTypes(true);
-            QObject::connect(m_trashDirLister.data(), static_cast<void(KDirLister::*)()>(&KDirLister::completed),
-                             m_signalHandler.data(), &PlacesItemSignalHandler::onTrashDirListerCompleted);
-            m_trashDirLister->openUrl(url);
+            QObject::connect(&Trash::instance(), &Trash::emptinessChanged, [this](bool isTrashEmpty){
+                setIcon(isTrashEmpty ? QStringLiteral("user-trash") : QStringLiteral("user-trash-full"));
+            });
         }
 
         setDataValue("url", url);
         }
 
         setDataValue("url", url);
@@ -239,14 +233,6 @@ void PlacesItem::onAccessibilityChanged()
     setUrl(QUrl::fromLocalFile(m_access->filePath()));
 }
 
     setUrl(QUrl::fromLocalFile(m_access->filePath()));
 }
 
-void PlacesItem::onTrashDirListerCompleted()
-{
-    Q_ASSERT(url().scheme() == QLatin1String("trash"));
-
-    const bool isTrashEmpty = m_trashDirLister->items().isEmpty();
-    setIcon(isTrashEmpty ? QStringLiteral("user-trash") : QStringLiteral("user-trash-full"));
-}
-
 void PlacesItem::updateBookmarkForRole(const QByteArray& role)
 {
     Q_ASSERT(!m_bookmark.isNull());
 void PlacesItem::updateBookmarkForRole(const QByteArray& role)
 {
     Q_ASSERT(!m_bookmark.isNull());
index 680590dcd47a73c28541d73548fc231a55fba33e..3de626983267e9a558e32cd9d0e06a15b78b7045 100644 (file)
@@ -91,12 +91,6 @@ private:
      */
     void onAccessibilityChanged();
 
      */
     void onAccessibilityChanged();
 
-    /**
-     * Is invoked if the listing of the trash has been completed.
-     * Updates the state of the trash-icon to be empty or full.
-     */
-    void onTrashDirListerCompleted();
-
     /**
      * Applies the data-value from the role to m_bookmark.
      */
     /**
      * Applies the data-value from the role to m_bookmark.
      */
index 5531d1b631d2a71dbbf15628aadd9b7fd38de819..c85c8336ee492c451c9538c6ba4a19908e15c8d7 100644 (file)
@@ -39,13 +39,6 @@ void PlacesItemSignalHandler::onAccessibilityChanged()
     }
 }
 
     }
 }
 
-void PlacesItemSignalHandler::onTrashDirListerCompleted()
-{
-    if (m_item) {
-        m_item->onTrashDirListerCompleted();
-    }
-}
-
 void PlacesItemSignalHandler::onTearDownRequested(const QString& udi)
 {
     Q_UNUSED(udi)
 void PlacesItemSignalHandler::onTearDownRequested(const QString& udi)
 {
     Q_UNUSED(udi)
index 49c8e43c9694cad1bcf319d95c0f509b31131e04..6158d7180672e6024758550cf7050b53c2f1182c 100644 (file)
@@ -56,11 +56,6 @@ public slots:
      */
     void onAccessibilityChanged();
 
      */
     void onAccessibilityChanged();
 
-    /**
-     * Calls PlacesItem::onTrashDirListerCompleted()
-     */
-    void onTrashDirListerCompleted();
-
     void onTearDownRequested(const QString& udi);
 
 signals:
     void onTearDownRequested(const QString& udi);
 
 signals:
index 29acd034c7315699d17fee9de131aef133ec26ab..216b760381270ae87f309dd9199b2b896f592e22 100644 (file)
 
 #include "dolphin_generalsettings.h"
 #include "global.h"
 
 #include "dolphin_generalsettings.h"
 #include "global.h"
+#include "kitemviews/kitemlistcontainer.h"
+#include "kitemviews/kitemlistcontroller.h"
+#include "kitemviews/kitemlistselectionmanager.h"
+#include "kitemviews/kstandarditem.h"
 #include "placesitem.h"
 #include "placesitemeditdialog.h"
 #include "placesitemlistgroupheader.h"
 #include "placesitemlistwidget.h"
 #include "placesitemmodel.h"
 #include "placesview.h"
 #include "placesitem.h"
 #include "placesitemeditdialog.h"
 #include "placesitemlistgroupheader.h"
 #include "placesitemlistwidget.h"
 #include "placesitemmodel.h"
 #include "placesview.h"
-#include "kitemviews/kitemlistcontainer.h"
-#include "kitemviews/kitemlistcontroller.h"
-#include "kitemviews/kitemlistselectionmanager.h"
-#include "kitemviews/kstandarditem.h"
+#include "trash/dolphintrash.h"
 #include "views/draganddrophelper.h"
 
 #include <KDirNotify>
 #include "views/draganddrophelper.h"
 
 #include <KDirNotify>
@@ -226,7 +227,7 @@ void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
     QAction* action = menu.exec(pos.toPoint());
     if (action) {
         if (action == emptyTrashAction) {
     QAction* action = menu.exec(pos.toPoint());
     if (action) {
         if (action == emptyTrashAction) {
-            emptyTrash();
+            Trash::empty(this);
         } else {
             // The index might have changed if devices were added/removed while
             // the context menu was open.
         } else {
             // The index might have changed if devices were added/removed while
             // the context menu was open.
@@ -425,15 +426,6 @@ void PlacesPanel::slotUrlsDropped(const QUrl& dest, QDropEvent* event, QWidget*
     }
 }
 
     }
 }
 
-void PlacesPanel::slotTrashUpdated(KJob* job)
-{
-    if (job->error()) {
-        emit errorMessage(job->errorString());
-    }
-    // as long as KIO doesn't do this, do it ourselves
-    KNotification::event(QStringLiteral("Trash: emptied"), QString(), QPixmap(), nullptr, KNotification::DefaultEvent);
-}
-
 void PlacesPanel::slotStorageSetupDone(int index, bool success)
 {
     disconnect(m_model, &PlacesItemModel::storageSetupDone,
 void PlacesPanel::slotStorageSetupDone(int index, bool success)
 {
     disconnect(m_model, &PlacesItemModel::storageSetupDone,
@@ -453,17 +445,6 @@ void PlacesPanel::slotStorageSetupDone(int index, bool success)
     }
 }
 
     }
 }
 
-void PlacesPanel::emptyTrash()
-{
-    KIO::JobUiDelegate uiDelegate;
-    uiDelegate.setWindow(window());
-    if (uiDelegate.askDeleteConfirmation(QList<QUrl>(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation)) {
-        KIO::Job* job = KIO::emptyTrash();
-        KJobWidgets::setWindow(job, window());
-        connect(job, &KIO::Job::result, this, &PlacesPanel::slotTrashUpdated);
-    }
-}
-
 void PlacesPanel::addEntry()
 {
     const int index = m_controller->selectionManager()->currentItem();
 void PlacesPanel::addEntry()
 {
     const int index = m_controller->selectionManager()->currentItem();
index 7159357f8ce2f115540e8772106c5f758a491a4e..d626979401d58c554efa266aaefa771eb76583ab 100644 (file)
@@ -68,11 +68,9 @@ private slots:
     void slotItemDropEventStorageSetupDone(int index, bool success);
     void slotAboveItemDropEvent(int index, QGraphicsSceneDragDropEvent* event);
     void slotUrlsDropped(const QUrl& dest, QDropEvent* event, QWidget* parent);
     void slotItemDropEventStorageSetupDone(int index, bool success);
     void slotAboveItemDropEvent(int index, QGraphicsSceneDragDropEvent* event);
     void slotUrlsDropped(const QUrl& dest, QDropEvent* event, QWidget* parent);
-    void slotTrashUpdated(KJob* job);
     void slotStorageSetupDone(int index, bool success);
 
 private:
     void slotStorageSetupDone(int index, bool success);
 
 private:
-    void emptyTrash();
     void addEntry();
     void editEntry(int index);
 
     void addEntry();
     void editEntry(int index);
 
diff --git a/src/trash/dolphintrash.cpp b/src/trash/dolphintrash.cpp
new file mode 100644 (file)
index 0000000..10d9bad
--- /dev/null
@@ -0,0 +1,81 @@
+/***************************************************************************
+ *   Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com>             *
+ *   Copyright (C) 2018 by Roman Inflianskas <infroma@gmail.com>           *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
+ ***************************************************************************/
+
+#include "dolphintrash.h"
+
+#include <KIO/JobUiDelegate>
+#include <KJobWidgets>
+#include <QList>
+#include <KNotification>
+#include <KConfig>
+#include <KConfigGroup>
+
+
+Trash::Trash()
+    : m_trashDirLister(new KDirLister())
+{
+    // The trash icon must always be updated dependent on whether
+    // the trash is empty or not. We use a KDirLister that automatically
+    // watches for changes if the number of items has been changed.
+    m_trashDirLister->setAutoErrorHandlingEnabled(false, nullptr);
+    m_trashDirLister->setDelayedMimeTypes(true);
+    auto trashDirContentChanged = [this]() {
+        bool isTrashEmpty = m_trashDirLister->items().isEmpty();
+        emit emptinessChanged(isTrashEmpty);
+    };
+    connect(m_trashDirLister, static_cast<void(KDirLister::*)()>(&KDirLister::completed), trashDirContentChanged);
+    m_trashDirLister->openUrl(QStringLiteral("trash:/"));
+}
+
+Trash::~Trash()
+{
+    delete m_trashDirLister;
+}
+
+Trash &Trash::instance()
+{
+    static Trash result;
+    return result;
+}
+
+KIO::Job *Trash::empty(QWidget *window)
+{
+    KIO::JobUiDelegate uiDelegate;
+    uiDelegate.setWindow(window);
+    bool confirmed = uiDelegate.askDeleteConfirmation(QList<QUrl>(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation);
+    if (confirmed) {
+        KIO::Job* job = KIO::emptyTrash();
+        KJobWidgets::setWindow(job, window);
+        job->uiDelegate()->setAutoErrorHandlingEnabled(true);
+         // as long as KIO doesn't do this, do it ourselves
+        connect(job, &KIO::Job::result, [](){
+            KNotification::event(QStringLiteral("Trash: emptied"), QString(), QPixmap(), nullptr, KNotification::DefaultEvent);
+        });
+        return job;
+    }
+    return nullptr;
+}
+
+bool Trash::isEmpty()
+{
+    KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig);
+    return (trashConfig.group("Status").readEntry("Empty", true));
+}
+
diff --git a/src/trash/dolphintrash.h b/src/trash/dolphintrash.h
new file mode 100644 (file)
index 0000000..76da4a5
--- /dev/null
@@ -0,0 +1,54 @@
+/***************************************************************************
+ *   Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com>             *
+ *   Copyright (C) 2018 by Roman Inflianskas <infroma@gmail.com>           *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
+ ***************************************************************************/
+
+#ifndef DOLPHINTRASH_H
+#define DOLPHINTRASH_H
+
+#include <QWidget>
+
+#include <KIO/EmptyTrashJob>
+#include <KIOWidgets/KDirLister>
+
+class Trash: public QObject
+{
+    Q_OBJECT
+
+public:
+    // delete copy and move constructors and assign operators
+    Trash(Trash const&) = delete;
+    Trash(Trash&&) = delete;
+    Trash& operator=(Trash const&) = delete;
+    Trash& operator=(Trash &&) = delete;
+
+    static Trash& instance();
+    static KIO::Job* empty(QWidget *window);
+    static bool isEmpty();
+
+signals:
+    void emptinessChanged(bool isEmpty);
+
+private:
+    KDirLister *m_trashDirLister;
+
+    Trash();
+    ~Trash();
+};
+
+#endif // DOLPHINTRASH_H