diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -205,6 +205,7 @@ dolphinrecenttabsmenu.cpp dolphintabpage.cpp dolphintabwidget.cpp + trash/dolphintrash.cpp filterbar/filterbar.cpp main.cpp panels/information/filemetadataconfigurationdialog.cpp diff --git a/src/dolphincontextmenu.cpp b/src/dolphincontextmenu.cpp --- a/src/dolphincontextmenu.cpp +++ b/src/dolphincontextmenu.cpp @@ -56,6 +56,7 @@ #include "views/dolphinview.h" #include "views/viewmodecontroller.h" +#include "trash/dolphintrash.h" DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent, const QPoint& pos, @@ -142,8 +143,7 @@ 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::instance().isEmpty()); addAction(emptyTrashAction); addCustomActions(); @@ -154,13 +154,7 @@ addShowMenuBarAction(); if (exec(m_pos) == emptyTrashAction) { - KIO::JobUiDelegate uiDelegate; - uiDelegate.setWindow(m_mainWindow); - if (uiDelegate.askDeleteConfirmation(QList(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation)) { - KIO::Job* job = KIO::emptyTrash(); - KJobWidgets::setWindow(job, m_mainWindow); - job->uiDelegate()->setAutoErrorHandlingEnabled(true); - } + Trash::instance().empty(m_mainWindow); } } diff --git a/src/dolphinviewcontainer.h b/src/dolphinviewcontainer.h --- a/src/dolphinviewcontainer.h +++ b/src/dolphinviewcontainer.h @@ -31,6 +31,7 @@ #include #include +#include #ifdef KF5Activities_FOUND namespace KActivities { @@ -320,7 +321,9 @@ private: QVBoxLayout* m_topLayout; + QWidget* m_navigatorWidget; KUrlNavigator* m_urlNavigator; + QPushButton* m_emptyTrashButton; DolphinSearchBox* m_searchBox; KMessageWidget* m_messageWidget; diff --git a/src/dolphinviewcontainer.cpp b/src/dolphinviewcontainer.cpp --- a/src/dolphinviewcontainer.cpp +++ b/src/dolphinviewcontainer.cpp @@ -22,20 +22,18 @@ #include #include -#include #include #include +#include #include #include #include #include #include #include #include -#include #include -#include #include #ifdef KActivities_FOUND @@ -47,13 +45,14 @@ #include "filterbar/filterbar.h" #include "search/dolphinsearchbox.h" #include "statusbar/dolphinstatusbar.h" -#include "views/viewmodecontroller.h" -#include "views/viewproperties.h" +#include "trash/dolphintrash.h" DolphinViewContainer::DolphinViewContainer(const QUrl& url, QWidget* parent) : QWidget(parent), m_topLayout(nullptr), + m_navigatorWidget(nullptr), m_urlNavigator(nullptr), + m_emptyTrashButton(nullptr), m_searchBox(nullptr), m_messageWidget(nullptr), m_view(nullptr), @@ -72,6 +71,11 @@ m_topLayout->setSpacing(0); m_topLayout->setMargin(0); + m_navigatorWidget = new QWidget(this); + QHBoxLayout* navigatorLayout = new QHBoxLayout(m_navigatorWidget); + navigatorLayout->setSpacing(0); + navigatorLayout->setMargin(0); + m_urlNavigator = new KUrlNavigator(new KFilePlacesModel(this), url, this); connect(m_urlNavigator, &KUrlNavigator::activated, this, &DolphinViewContainer::activate); @@ -85,6 +89,13 @@ KUrlComboBox* editor = m_urlNavigator->editor(); editor->setCompletionMode(KCompletion::CompletionMode(settings->urlCompletionMode())); + m_emptyTrashButton = new QPushButton(QIcon::fromTheme(QStringLiteral("user-trash")), "&Empty Trash", this); + m_emptyTrashButton->setFlat(true); + connect(m_emptyTrashButton, &QPushButton::clicked, [this]() { Trash::instance().empty(this); }); + connect(&Trash::instance(), &Trash::emptinessChanged, m_emptyTrashButton, &QPushButton::setDisabled); + m_emptyTrashButton->setDisabled(Trash::instance().isEmpty()); + m_emptyTrashButton->hide(); + m_searchBox = new DolphinSearchBox(this); m_searchBox->hide(); connect(m_searchBox, &DolphinSearchBox::activated, this, &DolphinViewContainer::activate); @@ -149,6 +160,14 @@ #endif }); + connect(m_view, &DolphinView::directoryLoadingCompleted, [this](){ + if (m_view->url().scheme() == QLatin1String("trash")) { + m_emptyTrashButton->show(); + } else { + m_emptyTrashButton->hide(); + } + }); + // Initialize status bar m_statusBar = new DolphinStatusBar(this); m_statusBar->setUrl(m_view->url()); @@ -187,7 +206,10 @@ connect(m_view, &DolphinView::urlChanged, m_filterBar, &FilterBar::slotUrlChanged); - m_topLayout->addWidget(m_urlNavigator); + navigatorLayout->addWidget(m_urlNavigator); + navigatorLayout->addWidget(m_emptyTrashButton); + + m_topLayout->addWidget(m_navigatorWidget); m_topLayout->addWidget(m_searchBox); m_topLayout->addWidget(m_messageWidget); m_topLayout->addWidget(m_view); @@ -339,7 +361,7 @@ } m_searchBox->setVisible(enabled); - m_urlNavigator->setVisible(!enabled); + m_navigatorWidget->setVisible(!enabled); if (enabled) { const QUrl& locationUrl = m_urlNavigator->locationUrl(); diff --git a/src/panels/places/placesitem.h b/src/panels/places/placesitem.h --- a/src/panels/places/placesitem.h +++ b/src/panels/places/placesitem.h @@ -92,7 +92,7 @@ * 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(); + void onTrashEmptinessChanged(); /** * Applies the data-value from the role to m_bookmark. @@ -112,6 +112,7 @@ KBookmark m_bookmark; friend class PlacesItemSignalHandler; // Calls onAccessibilityChanged() + void onTrashEmptinessChanged(bool isTrashEmpty); }; #endif diff --git a/src/panels/places/placesitem.cpp b/src/panels/places/placesitem.cpp --- a/src/panels/places/placesitem.cpp +++ b/src/panels/places/placesitem.cpp @@ -20,12 +20,14 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * ***************************************************************************/ +#include "dolphindebug.h" #include "placesitem.h" +#include "placesitemsignalhandler.h" +#include "trash/dolphintrash.h" -#include "dolphindebug.h" +#include #include #include -#include "placesitemsignalhandler.h" #include PlacesItem::PlacesItem(const KBookmark& bookmark, PlacesItem* parent) : @@ -59,16 +61,9 @@ 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(&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); @@ -238,14 +233,6 @@ 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()); diff --git a/src/panels/places/placesitemsignalhandler.h b/src/panels/places/placesitemsignalhandler.h --- a/src/panels/places/placesitemsignalhandler.h +++ b/src/panels/places/placesitemsignalhandler.h @@ -56,11 +56,6 @@ */ void onAccessibilityChanged(); - /** - * Calls PlacesItem::onTrashDirListerCompleted() - */ - void onTrashDirListerCompleted(); - void onTearDownRequested(const QString& udi); signals: diff --git a/src/panels/places/placesitemsignalhandler.cpp b/src/panels/places/placesitemsignalhandler.cpp --- a/src/panels/places/placesitemsignalhandler.cpp +++ b/src/panels/places/placesitemsignalhandler.cpp @@ -39,13 +39,6 @@ } } -void PlacesItemSignalHandler::onTrashDirListerCompleted() -{ - if (m_item) { - m_item->onTrashDirListerCompleted(); - } -} - void PlacesItemSignalHandler::onTearDownRequested(const QString& udi) { Q_UNUSED(udi) diff --git a/src/panels/places/placespanel.h b/src/panels/places/placespanel.h --- a/src/panels/places/placespanel.h +++ b/src/panels/places/placespanel.h @@ -67,11 +67,9 @@ 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 emptyTrash(); void addEntry(); void editEntry(int index); diff --git a/src/panels/places/placespanel.cpp b/src/panels/places/placespanel.cpp --- a/src/panels/places/placespanel.cpp +++ b/src/panels/places/placespanel.cpp @@ -24,6 +24,7 @@ #include "placespanel.h" #include "dolphin_generalsettings.h" +#include "trash/dolphintrash.h" #include "global.h" #include @@ -225,7 +226,7 @@ QAction* action = menu.exec(pos.toPoint()); if (action) { if (action == emptyTrashAction) { - emptyTrash(); + Trash::instance().empty(this); } else { // The index might have changed if devices were added/removed while // the context menu was open. @@ -424,15 +425,6 @@ } } -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, @@ -452,17 +444,6 @@ } } -void PlacesPanel::emptyTrash() -{ - KIO::JobUiDelegate uiDelegate; - uiDelegate.setWindow(window()); - if (uiDelegate.askDeleteConfirmation(QList(), 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(); diff --git a/src/panels/places/placesitemsignalhandler.cpp b/src/trash/dolphintrash.h copy from src/panels/places/placesitemsignalhandler.cpp copy to src/trash/dolphintrash.h --- a/src/panels/places/placesitemsignalhandler.cpp +++ b/src/trash/dolphintrash.h @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2012 by Peter Penz * + * Copyright (C) 2018 by Roman Inflianskas * * * * 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 * @@ -17,44 +17,40 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * ***************************************************************************/ -#include "placesitemsignalhandler.h" +#ifndef DOLPHINTRASH_H +#define DOLPHINTRASH_H -#include "placesitem.h" +#include -PlacesItemSignalHandler::PlacesItemSignalHandler(PlacesItem* item, - QObject* parent) : - QObject(parent), - m_item(item) -{ -} +#include +#include -PlacesItemSignalHandler::~PlacesItemSignalHandler() +class Trash: public QObject { -} + Q_OBJECT -void PlacesItemSignalHandler::onAccessibilityChanged() -{ - if (m_item) { - m_item->onAccessibilityChanged(); - } -} +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; -void PlacesItemSignalHandler::onTrashDirListerCompleted() -{ - if (m_item) { - m_item->onTrashDirListerCompleted(); - } -} + static Trash& instance(); + KIO::Job* empty(QWidget *window) const; + bool isEmpty() const; -void PlacesItemSignalHandler::onTearDownRequested(const QString& udi) -{ - Q_UNUSED(udi) - if (m_item) { - Solid::StorageAccess *tmp = m_item->device().as(); - if (tmp) { - QString mountPath = tmp->filePath(); - emit tearDownExternallyRequested(mountPath); - } - } -} +signals: + void emptinessChanged(bool isEmpty); + +protected: + Trash(); + ~Trash(); + +private: + KDirLister *m_trashDirLister; + + void watchEmptinessChanged(); +}; +#endif // DOLPHINTRASH_H diff --git a/src/trash/dolphintrash.cpp b/src/trash/dolphintrash.cpp new file mode 100644 --- /dev/null +++ b/src/trash/dolphintrash.cpp @@ -0,0 +1,89 @@ +/*************************************************************************** + * Copyright (C) 2018 by Roman Inflianskas * + * * + * 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 +#include +#include +#include +#include +#include +#include + + +Trash& Trash::instance() +{ + static Trash result; + return result; +} + +KIO::Job *Trash::empty(QWidget *window) const +{ + KIO::JobUiDelegate uiDelegate; + uiDelegate.setWindow(window); + bool confirmed = uiDelegate.askDeleteConfirmation(QList(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation); + if (confirmed) { + KIO::Job* job = KIO::emptyTrash(); + KJobWidgets::setWindow(job, window); + job->uiDelegate()->setAutoErrorHandlingEnabled(true); + connect(job, &KIO::Job::result, [](){ + KNotification::event(QStringLiteral("Trash: emptied"), QString(), QPixmap(), nullptr, KNotification::DefaultEvent); + }); + return job; + } + return nullptr; +} + +bool Trash::isEmpty() const +{ + KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig); + return (trashConfig.group("Status").readEntry("Empty", true)); +} + +void Trash::watchEmptinessChanged() +{ + // 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 onTrashEmptinessChanged(). + m_trashDirLister = new KDirLister(); + m_trashDirLister->setAutoErrorHandlingEnabled(false, nullptr); + m_trashDirLister->setDelayedMimeTypes(true); + auto trashDirContentChanged = [this,m_trashDirLister](){ + bool isTrashEmpty = m_trashDirLister->items().isEmpty(); + emit emptinessChanged(isTrashEmpty); + }; + connect(m_trashDirLister, static_cast(&KDirLister::completed), trashDirContentChanged); + // This is bug in KIO. KDirLister::completed should fire up when KDirLister::itemsDeleted is fired. + // This connect is workaround. If you find a way how to fix KIO, DO IT and remove this connect. + connect(m_trashDirLister, static_cast(&KDirLister::itemsDeleted), + trashDirContentChanged); + m_trashDirLister->openUrl(QStringLiteral("trash:/")); +} + +Trash::Trash() +{ + watchEmptinessChanged(); +} + +Trash::~Trash() +{ + delete m_trashDirLister; +}