]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinplacesmodelsingleton.cpp
Rewrite search integration
[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(QObject *parent)
17 : KFilePlacesModel(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() << DragAndDropHelper::arkDndPathMimeType();
55 return types;
56 }
57
58 bool DolphinPlacesModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
59 {
60 // We make the view accept the drag by returning them from mimeTypes()
61 // but the drop should be handled exclusively by PlacesPanel::slotUrlsDropped
62 if (DragAndDropHelper::isArkDndMimeType(data)) {
63 return false;
64 }
65
66 return KFilePlacesModel::dropMimeData(data, action, row, column, parent);
67 }
68
69 QVariant DolphinPlacesModel::data(const QModelIndex &index, int role) const
70 {
71 switch (role) {
72 case Qt::DecorationRole:
73 if (isTrash(index)) {
74 if (m_isEmpty) {
75 return QIcon::fromTheme(QStringLiteral("user-trash"));
76 } else {
77 return QIcon::fromTheme(QStringLiteral("user-trash-full"));
78 }
79 }
80 break;
81 case KFilePlacesModel::GroupRole: {
82 // When panels are unlocked, avoid a double "Places" heading,
83 // one from the panel title bar, one from the places view section.
84 if (!m_panelsLocked) {
85 const auto groupType = KFilePlacesModel::groupType(index);
86 if (groupType == KFilePlacesModel::PlacesType) {
87 return QString();
88 }
89 }
90 break;
91 }
92 }
93
94 return KFilePlacesModel::data(index, role);
95 }
96
97 void DolphinPlacesModel::slotTrashEmptinessChanged(bool isEmpty)
98 {
99 if (m_isEmpty == isEmpty) {
100 return;
101 }
102
103 // NOTE Trash::isEmpty() reads the config file whereas emptinessChanged is
104 // hooked up to whether a dirlister in trash:/ has any files and they disagree...
105 m_isEmpty = isEmpty;
106
107 for (int i = 0; i < rowCount(); ++i) {
108 const QModelIndex index = this->index(i, 0);
109 if (isTrash(index)) {
110 Q_EMIT dataChanged(index, index, {Qt::DecorationRole});
111 }
112 }
113 }
114
115 bool DolphinPlacesModel::isTrash(const QModelIndex &index) const
116 {
117 return url(index) == QUrl(QStringLiteral("trash:/"));
118 }
119
120 DolphinPlacesModelSingleton::DolphinPlacesModelSingleton()
121 : m_placesModel(new DolphinPlacesModel())
122 {
123 }
124
125 DolphinPlacesModelSingleton &DolphinPlacesModelSingleton::instance()
126 {
127 static DolphinPlacesModelSingleton s_self;
128 return s_self;
129 }
130
131 DolphinPlacesModel *DolphinPlacesModelSingleton::placesModel() const
132 {
133 return m_placesModel.data();
134 }
135
136 #include "moc_dolphinplacesmodelsingleton.cpp"