2020-08-25 17:07:38 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2018 Kai Uwe Broulik <kde@privat.broulik.de>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2018-03-19 08:57:24 +00:00
|
|
|
|
|
|
|
#include "dolphinplacesmodelsingleton.h"
|
2021-12-16 18:29:22 +00:00
|
|
|
#include "trash/dolphintrash.h"
|
2022-04-07 19:41:43 +00:00
|
|
|
#include "views/draganddrophelper.h"
|
2018-03-19 08:57:24 +00:00
|
|
|
|
|
|
|
#include <KAboutData>
|
|
|
|
|
2021-12-16 18:29:22 +00:00
|
|
|
#include <QIcon>
|
2022-04-07 19:41:43 +00:00
|
|
|
#include <QMimeData>
|
2021-12-16 18:29:22 +00:00
|
|
|
|
|
|
|
DolphinPlacesModel::DolphinPlacesModel(const QString &alternativeApplicationName, QObject *parent)
|
|
|
|
: KFilePlacesModel(alternativeApplicationName, parent)
|
|
|
|
{
|
|
|
|
connect(&Trash::instance(), &Trash::emptinessChanged, this, &DolphinPlacesModel::slotTrashEmptinessChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
DolphinPlacesModel::~DolphinPlacesModel() = default;
|
|
|
|
|
2022-01-28 18:40:11 +00:00
|
|
|
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});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 19:41:43 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-12-16 18:29:22 +00:00
|
|
|
QVariant DolphinPlacesModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2022-01-28 18:40:11 +00:00
|
|
|
switch (role) {
|
|
|
|
case Qt::DecorationRole:
|
2021-12-16 18:29:22 +00:00
|
|
|
if (isTrash(index)) {
|
|
|
|
if (m_isEmpty) {
|
|
|
|
return QIcon::fromTheme(QStringLiteral("user-trash"));
|
|
|
|
} else {
|
|
|
|
return QIcon::fromTheme(QStringLiteral("user-trash-full"));
|
|
|
|
}
|
|
|
|
}
|
2022-01-28 18:40:11 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-12-16 18:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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:/"));
|
|
|
|
}
|
|
|
|
|
2018-03-19 08:57:24 +00:00
|
|
|
DolphinPlacesModelSingleton::DolphinPlacesModelSingleton()
|
2021-12-16 18:29:22 +00:00
|
|
|
: m_placesModel(new DolphinPlacesModel(KAboutData::applicationData().componentName() + applicationNameSuffix()))
|
2018-03-19 08:57:24 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DolphinPlacesModelSingleton &DolphinPlacesModelSingleton::instance()
|
|
|
|
{
|
|
|
|
static DolphinPlacesModelSingleton s_self;
|
|
|
|
return s_self;
|
|
|
|
}
|
|
|
|
|
2022-01-28 18:39:07 +00:00
|
|
|
DolphinPlacesModel *DolphinPlacesModelSingleton::placesModel() const
|
2018-03-19 08:57:24 +00:00
|
|
|
{
|
|
|
|
return m_placesModel.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DolphinPlacesModelSingleton::applicationNameSuffix()
|
|
|
|
{
|
|
|
|
return QStringLiteral("-places-panel");
|
|
|
|
}
|