]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinplacesmodelsingleton.cpp
Merge branch 'release/21.12'
[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
10 #include <KAboutData>
11 #include <KFilePlacesModel>
12
13 #include <QIcon>
14
15 DolphinPlacesModel::DolphinPlacesModel(const QString &alternativeApplicationName, QObject *parent)
16 : KFilePlacesModel(alternativeApplicationName, parent)
17 {
18 connect(&Trash::instance(), &Trash::emptinessChanged, this, &DolphinPlacesModel::slotTrashEmptinessChanged);
19 }
20
21 DolphinPlacesModel::~DolphinPlacesModel() = default;
22
23 QVariant DolphinPlacesModel::data(const QModelIndex &index, int role) const
24 {
25 if (role == Qt::DecorationRole) {
26 if (isTrash(index)) {
27 if (m_isEmpty) {
28 return QIcon::fromTheme(QStringLiteral("user-trash"));
29 } else {
30 return QIcon::fromTheme(QStringLiteral("user-trash-full"));
31 }
32 }
33 }
34
35 return KFilePlacesModel::data(index, role);
36 }
37
38 void DolphinPlacesModel::slotTrashEmptinessChanged(bool isEmpty)
39 {
40 if (m_isEmpty == isEmpty) {
41 return;
42 }
43
44 // NOTE Trash::isEmpty() reads the config file whereas emptinessChanged is
45 // hooked up to whether a dirlister in trash:/ has any files and they disagree...
46 m_isEmpty = isEmpty;
47
48 for (int i = 0; i < rowCount(); ++i) {
49 const QModelIndex index = this->index(i, 0);
50 if (isTrash(index)) {
51 Q_EMIT dataChanged(index, index, {Qt::DecorationRole});
52 }
53 }
54 }
55
56 bool DolphinPlacesModel::isTrash(const QModelIndex &index) const
57 {
58 return url(index) == QUrl(QStringLiteral("trash:/"));
59 }
60
61 DolphinPlacesModelSingleton::DolphinPlacesModelSingleton()
62 : m_placesModel(new DolphinPlacesModel(KAboutData::applicationData().componentName() + applicationNameSuffix()))
63 {
64
65 }
66
67 DolphinPlacesModelSingleton &DolphinPlacesModelSingleton::instance()
68 {
69 static DolphinPlacesModelSingleton s_self;
70 return s_self;
71 }
72
73 KFilePlacesModel *DolphinPlacesModelSingleton::placesModel() const
74 {
75 return m_placesModel.data();
76 }
77
78 QString DolphinPlacesModelSingleton::applicationNameSuffix()
79 {
80 return QStringLiteral("-places-panel");
81 }