]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphinplacesmodelsingleton.cpp
Port back to KFilePlacesView
[dolphin.git] / src / dolphinplacesmodelsingleton.cpp
index 1020ba1867da1c762ea3a2b738041232e463e0ef..754070c9298bc6f610f5119a8b3d5dfd1a7dd895 100644 (file)
@@ -1,29 +1,65 @@
-/***************************************************************************
- *   Copyright (C) 2018 Kai Uwe Broulik <kde@privat.broulik.de>            *
- *                                                                         *
- *   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            *
- ***************************************************************************/
+/*
+ * SPDX-FileCopyrightText: 2018 Kai Uwe Broulik <kde@privat.broulik.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
 
 #include "dolphinplacesmodelsingleton.h"
+#include "trash/dolphintrash.h"
 
 #include <KAboutData>
 #include <KFilePlacesModel>
 
+#include <QIcon>
+
+DolphinPlacesModel::DolphinPlacesModel(const QString &alternativeApplicationName, QObject *parent)
+    : KFilePlacesModel(alternativeApplicationName, parent)
+{
+    connect(&Trash::instance(), &Trash::emptinessChanged, this, &DolphinPlacesModel::slotTrashEmptinessChanged);
+}
+
+DolphinPlacesModel::~DolphinPlacesModel() = default;
+
+QVariant DolphinPlacesModel::data(const QModelIndex &index, int role) const
+{
+    if (role == Qt::DecorationRole) {
+        if (isTrash(index)) {
+            if (m_isEmpty) {
+                return QIcon::fromTheme(QStringLiteral("user-trash"));
+            } else {
+                return QIcon::fromTheme(QStringLiteral("user-trash-full"));
+            }
+        }
+    }
+
+    return KFilePlacesModel::data(index, role);
+}
+
+void DolphinPlacesModel::slotTrashEmptinessChanged(bool isEmpty)
+{
+    if (m_isEmpty == isEmpty) {
+        return;
+    }
+
+    // NOTE Trash::isEmpty() reads the config file whereas emptinessChanged is
+    // hooked up to whether a dirlister in trash:/ has any files and they disagree...
+    m_isEmpty = isEmpty;
+
+    for (int i = 0; i < rowCount(); ++i) {
+        const QModelIndex index = this->index(i, 0);
+        if (isTrash(index)) {
+            Q_EMIT dataChanged(index, index, {Qt::DecorationRole});
+        }
+    }
+}
+
+bool DolphinPlacesModel::isTrash(const QModelIndex &index) const
+{
+    return url(index) == QUrl(QStringLiteral("trash:/"));
+}
+
 DolphinPlacesModelSingleton::DolphinPlacesModelSingleton()
-    : m_placesModel(new KFilePlacesModel(KAboutData::applicationData().componentName() + applicationNameSuffix()))
+    : m_placesModel(new DolphinPlacesModel(KAboutData::applicationData().componentName() + applicationNameSuffix()))
 {
 
 }