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