File indexing completed on 2024-04-21 05:45:42

0001 /*
0002  * SPDX-FileCopyrightText: 2018 Kai Uwe Broulik <kde@privat.broulik.de>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "dolphinplacesmodelsingleton.h"
0008 #include "trash/dolphintrash.h"
0009 #include "views/draganddrophelper.h"
0010 
0011 #include <KAboutData>
0012 
0013 #include <QIcon>
0014 #include <QMimeData>
0015 
0016 DolphinPlacesModel::DolphinPlacesModel(QObject *parent)
0017     : KFilePlacesModel(parent)
0018 {
0019     connect(&Trash::instance(), &Trash::emptinessChanged, this, &DolphinPlacesModel::slotTrashEmptinessChanged);
0020 }
0021 
0022 DolphinPlacesModel::~DolphinPlacesModel() = default;
0023 
0024 bool DolphinPlacesModel::panelsLocked() const
0025 {
0026     return m_panelsLocked;
0027 }
0028 
0029 void DolphinPlacesModel::setPanelsLocked(bool locked)
0030 {
0031     if (m_panelsLocked == locked) {
0032         return;
0033     }
0034 
0035     m_panelsLocked = locked;
0036 
0037     if (rowCount() > 0) {
0038         int lastPlace = rowCount() - 1;
0039 
0040         for (int i = 0; i < rowCount(); ++i) {
0041             if (KFilePlacesModel::groupType(index(i, 0)) != KFilePlacesModel::PlacesType) {
0042                 lastPlace = i - 1;
0043                 break;
0044             }
0045         }
0046 
0047         Q_EMIT dataChanged(index(0, 0), index(lastPlace, 0), {KFilePlacesModel::GroupRole});
0048     }
0049 }
0050 
0051 QStringList DolphinPlacesModel::mimeTypes() const
0052 {
0053     QStringList types = KFilePlacesModel::mimeTypes();
0054     types << DragAndDropHelper::arkDndServiceMimeType() << DragAndDropHelper::arkDndPathMimeType();
0055     return types;
0056 }
0057 
0058 bool DolphinPlacesModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
0059 {
0060     // We make the view accept the drag by returning them from mimeTypes()
0061     // but the drop should be handled exclusively by PlacesPanel::slotUrlsDropped
0062     if (DragAndDropHelper::isArkDndMimeType(data)) {
0063         return false;
0064     }
0065 
0066     return KFilePlacesModel::dropMimeData(data, action, row, column, parent);
0067 }
0068 
0069 QVariant DolphinPlacesModel::data(const QModelIndex &index, int role) const
0070 {
0071     switch (role) {
0072     case Qt::DecorationRole:
0073         if (isTrash(index)) {
0074             if (m_isEmpty) {
0075                 return QIcon::fromTheme(QStringLiteral("user-trash"));
0076             } else {
0077                 return QIcon::fromTheme(QStringLiteral("user-trash-full"));
0078             }
0079         }
0080         break;
0081     case KFilePlacesModel::GroupRole: {
0082         // When panels are unlocked, avoid a double "Places" heading,
0083         // one from the panel title bar, one from the places view section.
0084         if (!m_panelsLocked) {
0085             const auto groupType = KFilePlacesModel::groupType(index);
0086             if (groupType == KFilePlacesModel::PlacesType) {
0087                 return QString();
0088             }
0089         }
0090         break;
0091     }
0092     }
0093 
0094     return KFilePlacesModel::data(index, role);
0095 }
0096 
0097 void DolphinPlacesModel::slotTrashEmptinessChanged(bool isEmpty)
0098 {
0099     if (m_isEmpty == isEmpty) {
0100         return;
0101     }
0102 
0103     // NOTE Trash::isEmpty() reads the config file whereas emptinessChanged is
0104     // hooked up to whether a dirlister in trash:/ has any files and they disagree...
0105     m_isEmpty = isEmpty;
0106 
0107     for (int i = 0; i < rowCount(); ++i) {
0108         const QModelIndex index = this->index(i, 0);
0109         if (isTrash(index)) {
0110             Q_EMIT dataChanged(index, index, {Qt::DecorationRole});
0111         }
0112     }
0113 }
0114 
0115 bool DolphinPlacesModel::isTrash(const QModelIndex &index) const
0116 {
0117     return url(index) == QUrl(QStringLiteral("trash:/"));
0118 }
0119 
0120 DolphinPlacesModelSingleton::DolphinPlacesModelSingleton()
0121     : m_placesModel(new DolphinPlacesModel())
0122 {
0123 }
0124 
0125 DolphinPlacesModelSingleton &DolphinPlacesModelSingleton::instance()
0126 {
0127     static DolphinPlacesModelSingleton s_self;
0128     return s_self;
0129 }
0130 
0131 DolphinPlacesModel *DolphinPlacesModelSingleton::placesModel() const
0132 {
0133     return m_placesModel.data();
0134 }
0135 
0136 #include "moc_dolphinplacesmodelsingleton.cpp"