]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinplacesmodelsingleton.cpp
Merge branch 'release/22.04'
[dolphin.git] / src / dolphinplacesmodelsingleton.cpp
1 /*
2 * SPDX-FileCopyrightText: 2018 Kai Uwe Broulik <kde@privat.broulik.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "dolphinplacesmodelsingleton.h"
8 #include "trash/dolphintrash.h"
9 #include "views/draganddrophelper.h"
10
11 #include <KAboutData>
12 #include <KFilePlacesModel>
13
14 #include <QIcon>
15 #include <QMimeData>
16
17 DolphinPlacesModel::DolphinPlacesModel(const QString &alternativeApplicationName, QObject *parent)
18 : KFilePlacesModel(alternativeApplicationName, parent)
19 {
20 connect(&Trash::instance(), &Trash::emptinessChanged, this, &DolphinPlacesModel::slotTrashEmptinessChanged);
21 }
22
23 DolphinPlacesModel::~DolphinPlacesModel() = default;
24
25 bool DolphinPlacesModel::panelsLocked() const
26 {
27 return m_panelsLocked;
28 }
29
30 void DolphinPlacesModel::setPanelsLocked(bool locked)
31 {
32 if (m_panelsLocked == locked) {
33 return;
34 }
35
36 m_panelsLocked = locked;
37
38 if (rowCount() > 0) {
39 int lastPlace = rowCount() - 1;
40
41 for (int i = 0; i < rowCount(); ++i) {
42 if (KFilePlacesModel::groupType(index(i, 0)) != KFilePlacesModel::PlacesType) {
43 lastPlace = i - 1;
44 break;
45 }
46 }
47
48 Q_EMIT dataChanged(index(0, 0), index(lastPlace, 0), {KFilePlacesModel::GroupRole});
49 }
50 }
51
52 QStringList DolphinPlacesModel::mimeTypes() const
53 {
54 QStringList types = KFilePlacesModel::mimeTypes();
55 types << DragAndDropHelper::arkDndServiceMimeType()
56 << DragAndDropHelper::arkDndPathMimeType();
57 return types;
58 }
59
60 bool DolphinPlacesModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
61 {
62 // We make the view accept the drag by returning them from mimeTypes()
63 // but the drop should be handled exclusively by PlacesPanel::slotUrlsDropped
64 if (DragAndDropHelper::isArkDndMimeType(data)) {
65 return false;
66 }
67
68 return KFilePlacesModel::dropMimeData(data, action, row, column, parent);
69 }
70
71 QVariant DolphinPlacesModel::data(const QModelIndex &index, int role) const
72 {
73 switch (role) {
74 case Qt::DecorationRole:
75 if (isTrash(index)) {
76 if (m_isEmpty) {
77 return QIcon::fromTheme(QStringLiteral("user-trash"));
78 } else {
79 return QIcon::fromTheme(QStringLiteral("user-trash-full"));
80 }
81 }
82 break;
83 case KFilePlacesModel::GroupRole: {
84 // When panels are unlocked, avoid a double "Places" heading,
85 // one from the panel title bar, one from the places view section.
86 if (!m_panelsLocked) {
87 const auto groupType = KFilePlacesModel::groupType(index);
88 if (groupType == KFilePlacesModel::PlacesType) {
89 return QString();
90 }
91 }
92 break;
93 }
94 }
95
96 return KFilePlacesModel::data(index, role);
97 }
98
99 void DolphinPlacesModel::slotTrashEmptinessChanged(bool isEmpty)
100 {
101 if (m_isEmpty == isEmpty) {
102 return;
103 }
104
105 // NOTE Trash::isEmpty() reads the config file whereas emptinessChanged is
106 // hooked up to whether a dirlister in trash:/ has any files and they disagree...
107 m_isEmpty = isEmpty;
108
109 for (int i = 0; i < rowCount(); ++i) {
110 const QModelIndex index = this->index(i, 0);
111 if (isTrash(index)) {
112 Q_EMIT dataChanged(index, index, {Qt::DecorationRole});
113 }
114 }
115 }
116
117 bool DolphinPlacesModel::isTrash(const QModelIndex &index) const
118 {
119 return url(index) == QUrl(QStringLiteral("trash:/"));
120 }
121
122 DolphinPlacesModelSingleton::DolphinPlacesModelSingleton()
123 : m_placesModel(new DolphinPlacesModel(KAboutData::applicationData().componentName() + applicationNameSuffix()))
124 {
125
126 }
127
128 DolphinPlacesModelSingleton &DolphinPlacesModelSingleton::instance()
129 {
130 static DolphinPlacesModelSingleton s_self;
131 return s_self;
132 }
133
134 DolphinPlacesModel *DolphinPlacesModelSingleton::placesModel() const
135 {
136 return m_placesModel.data();
137 }
138
139 QString DolphinPlacesModelSingleton::applicationNameSuffix()
140 {
141 return QStringLiteral("-places-panel");
142 }