X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/7cee23157f099837fffc22380b85ac33a2006a49..9281664e5b1e492087604264b145b390e8880e81:/src/dolphinplacesmodelsingleton.cpp diff --git a/src/dolphinplacesmodelsingleton.cpp b/src/dolphinplacesmodelsingleton.cpp index 1020ba186..301150394 100644 --- a/src/dolphinplacesmodelsingleton.cpp +++ b/src/dolphinplacesmodelsingleton.cpp @@ -1,31 +1,125 @@ -/*************************************************************************** - * Copyright (C) 2018 Kai Uwe Broulik * - * * - * 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 + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ #include "dolphinplacesmodelsingleton.h" +#include "trash/dolphintrash.h" +#include "views/draganddrophelper.h" #include -#include -DolphinPlacesModelSingleton::DolphinPlacesModelSingleton() - : m_placesModel(new KFilePlacesModel(KAboutData::applicationData().componentName() + applicationNameSuffix())) +#include +#include + +DolphinPlacesModel::DolphinPlacesModel(QObject *parent) + : KFilePlacesModel(parent) +{ + connect(&Trash::instance(), &Trash::emptinessChanged, this, &DolphinPlacesModel::slotTrashEmptinessChanged); +} + +DolphinPlacesModel::~DolphinPlacesModel() = default; + +bool DolphinPlacesModel::panelsLocked() const +{ + return m_panelsLocked; +} + +void DolphinPlacesModel::setPanelsLocked(bool locked) +{ + if (m_panelsLocked == locked) { + return; + } + + m_panelsLocked = locked; + + if (rowCount() > 0) { + int lastPlace = rowCount() - 1; + + for (int i = 0; i < rowCount(); ++i) { + if (KFilePlacesModel::groupType(index(i, 0)) != KFilePlacesModel::PlacesType) { + lastPlace = i - 1; + break; + } + } + + Q_EMIT dataChanged(index(0, 0), index(lastPlace, 0), {KFilePlacesModel::GroupRole}); + } +} + +QStringList DolphinPlacesModel::mimeTypes() const +{ + QStringList types = KFilePlacesModel::mimeTypes(); + types << DragAndDropHelper::arkDndServiceMimeType() << DragAndDropHelper::arkDndPathMimeType(); + return types; +} + +bool DolphinPlacesModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) { + // We make the view accept the drag by returning them from mimeTypes() + // but the drop should be handled exclusively by PlacesPanel::slotUrlsDropped + if (DragAndDropHelper::isArkDndMimeType(data)) { + return false; + } + return KFilePlacesModel::dropMimeData(data, action, row, column, parent); +} + +QVariant DolphinPlacesModel::data(const QModelIndex &index, int role) const +{ + switch (role) { + case Qt::DecorationRole: + if (isTrash(index)) { + if (m_isEmpty) { + return QIcon::fromTheme(QStringLiteral("user-trash")); + } else { + return QIcon::fromTheme(QStringLiteral("user-trash-full")); + } + } + break; + case KFilePlacesModel::GroupRole: { + // When panels are unlocked, avoid a double "Places" heading, + // one from the panel title bar, one from the places view section. + if (!m_panelsLocked) { + const auto groupType = KFilePlacesModel::groupType(index); + if (groupType == KFilePlacesModel::PlacesType) { + return QString(); + } + } + break; + } + } + + 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 DolphinPlacesModel()) +{ } DolphinPlacesModelSingleton &DolphinPlacesModelSingleton::instance() @@ -34,12 +128,9 @@ DolphinPlacesModelSingleton &DolphinPlacesModelSingleton::instance() return s_self; } -KFilePlacesModel *DolphinPlacesModelSingleton::placesModel() const +DolphinPlacesModel *DolphinPlacesModelSingleton::placesModel() const { return m_placesModel.data(); } -QString DolphinPlacesModelSingleton::applicationNameSuffix() -{ - return QStringLiteral("-places-panel"); -} +#include "moc_dolphinplacesmodelsingleton.cpp"