]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitem.cpp
Remove custom icon selection for trash
[dolphin.git] / src / panels / places / placesitem.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on KFilePlacesItem from kdelibs: *
5 * Copyright (C) 2007 Kevin Ottens <ervin@kde.org> *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
22
23 #include "placesitem.h"
24 #include "trash/dolphintrash.h"
25
26 #include "dolphindebug.h"
27 #include "placesitemsignalhandler.h"
28
29 #include <KDirLister>
30 #include <KLocalizedString>
31 #include <Solid/Block>
32
33 PlacesItem::PlacesItem(const KBookmark& bookmark, PlacesItem* parent) :
34 KStandardItem(parent),
35 m_device(),
36 m_access(),
37 m_volume(),
38 m_disc(),
39 m_mtp(),
40 m_signalHandler(nullptr),
41 m_bookmark()
42 {
43 m_signalHandler = new PlacesItemSignalHandler(this);
44 setBookmark(bookmark);
45 }
46
47 PlacesItem::~PlacesItem()
48 {
49 delete m_signalHandler;
50 }
51
52 void PlacesItem::setUrl(const QUrl &url)
53 {
54 // The default check in KStandardItem::setDataValue()
55 // for equal values does not work with a custom value
56 // like QUrl. Hence do a manual check to prevent that
57 // setting an equal URL results in an itemsChanged()
58 // signal.
59 if (dataValue("url").toUrl() != url) {
60 if (url.scheme() == QLatin1String("trash")) {
61 QObject::connect(&Trash::instance(), &Trash::emptinessChanged, m_signalHandler.data(), &PlacesItemSignalHandler::onTrashEmptinessChanged);
62 }
63
64 setDataValue("url", url);
65 }
66 }
67
68 QUrl PlacesItem::url() const
69 {
70 return dataValue("url").toUrl();
71 }
72
73 void PlacesItem::setUdi(const QString& udi)
74 {
75 setDataValue("udi", udi);
76 }
77
78 QString PlacesItem::udi() const
79 {
80 return dataValue("udi").toString();
81 }
82
83 void PlacesItem::setHidden(bool hidden)
84 {
85 setDataValue("isHidden", hidden);
86 }
87
88 bool PlacesItem::isHidden() const
89 {
90 return dataValue("isHidden").toBool();
91 }
92
93 bool PlacesItem::isGroupHidden() const
94 {
95 return dataValue("isGroupHidden").toBool();
96 }
97
98 void PlacesItem::setGroupHidden(bool hidden)
99 {
100 setDataValue("isGroupHidden", hidden);
101 }
102
103 void PlacesItem::setSystemItem(bool isSystemItem)
104 {
105 setDataValue("isSystemItem", isSystemItem);
106 }
107
108 bool PlacesItem::isSystemItem() const
109 {
110 return dataValue("isSystemItem").toBool();
111 }
112
113 Solid::Device PlacesItem::device() const
114 {
115 return m_device;
116 }
117
118 void PlacesItem::setBookmark(const KBookmark& bookmark)
119 {
120 const bool bookmarkDataChanged = !(bookmark == m_bookmark);
121
122 // bookmark object must be updated to keep in sync with source model
123 m_bookmark = bookmark;
124
125 if (!bookmarkDataChanged) {
126 return;
127 }
128
129 delete m_access;
130 delete m_volume;
131 delete m_disc;
132 delete m_mtp;
133
134 const QString udi = bookmark.metaDataItem(QStringLiteral("UDI"));
135 if (udi.isEmpty()) {
136 setIcon(bookmark.icon());
137 setText(i18ndc("kio5", "KFile System Bookmarks", bookmark.text().toUtf8().constData()));
138 setUrl(bookmark.url());
139 setSystemItem(bookmark.metaDataItem(QStringLiteral("isSystemItem")) == QLatin1String("true"));
140 } else {
141 initializeDevice(udi);
142 }
143
144 setHidden(bookmark.metaDataItem(QStringLiteral("IsHidden")) == QLatin1String("true"));
145 }
146
147 KBookmark PlacesItem::bookmark() const
148 {
149 return m_bookmark;
150 }
151
152 bool PlacesItem::storageSetupNeeded() const
153 {
154 return m_access ? !m_access->isAccessible() : false;
155 }
156
157 bool PlacesItem::isSearchOrTimelineUrl() const
158 {
159 const QString urlScheme = url().scheme();
160 return (urlScheme.contains("search") || urlScheme.contains("timeline"));
161 }
162
163 void PlacesItem::onDataValueChanged(const QByteArray& role,
164 const QVariant& current,
165 const QVariant& previous)
166 {
167 Q_UNUSED(current);
168 Q_UNUSED(previous);
169
170 if (!m_bookmark.isNull()) {
171 updateBookmarkForRole(role);
172 }
173 }
174
175 void PlacesItem::onDataChanged(const QHash<QByteArray, QVariant>& current,
176 const QHash<QByteArray, QVariant>& previous)
177 {
178 Q_UNUSED(previous);
179
180 if (!m_bookmark.isNull()) {
181 QHashIterator<QByteArray, QVariant> it(current);
182 while (it.hasNext()) {
183 it.next();
184 updateBookmarkForRole(it.key());
185 }
186 }
187 }
188
189 void PlacesItem::initializeDevice(const QString& udi)
190 {
191 m_device = Solid::Device(udi);
192 if (!m_device.isValid()) {
193 return;
194 }
195
196 m_access = m_device.as<Solid::StorageAccess>();
197 m_volume = m_device.as<Solid::StorageVolume>();
198 m_disc = m_device.as<Solid::OpticalDisc>();
199 m_mtp = m_device.as<Solid::PortableMediaPlayer>();
200
201 setText(m_device.description());
202 setIcon(m_device.icon());
203 setIconOverlays(m_device.emblems());
204 setUdi(udi);
205
206 if (m_access) {
207 setUrl(QUrl::fromLocalFile(m_access->filePath()));
208 QObject::connect(m_access.data(), &Solid::StorageAccess::accessibilityChanged,
209 m_signalHandler.data(), &PlacesItemSignalHandler::onAccessibilityChanged);
210 QObject::connect(m_access.data(), &Solid::StorageAccess::teardownRequested,
211 m_signalHandler.data(), &PlacesItemSignalHandler::onTearDownRequested);
212 } else if (m_disc && (m_disc->availableContent() & Solid::OpticalDisc::Audio) != 0) {
213 Solid::Block *block = m_device.as<Solid::Block>();
214 if (block) {
215 const QString device = block->device();
216 setUrl(QUrl(QStringLiteral("audiocd:/?device=%1").arg(device)));
217 } else {
218 setUrl(QUrl(QStringLiteral("audiocd:/")));
219 }
220 } else if (m_mtp) {
221 setUrl(QUrl(QStringLiteral("mtp:udi=%1").arg(m_device.udi())));
222 }
223 }
224
225 void PlacesItem::onAccessibilityChanged()
226 {
227 setIconOverlays(m_device.emblems());
228 setUrl(QUrl::fromLocalFile(m_access->filePath()));
229 }
230
231 void PlacesItem::updateBookmarkForRole(const QByteArray& role)
232 {
233 Q_ASSERT(!m_bookmark.isNull());
234 if (role == "iconName") {
235 m_bookmark.setIcon(icon());
236 } else if (role == "text") {
237 // Only store the text in the KBookmark if it is not the translation of
238 // the current text. This makes sure that the text is re-translated if
239 // the user chooses another language, or the translation itself changes.
240 //
241 // NOTE: It is important to use "KFile System Bookmarks" as context
242 // (see PlacesItemModel::createSystemBookmarks()).
243 if (text() != i18ndc("kio5", "KFile System Bookmarks", m_bookmark.text().toUtf8().data())) {
244 m_bookmark.setFullText(text());
245 }
246 } else if (role == "url") {
247 m_bookmark.setUrl(url());
248 } else if (role == "udi") {
249 m_bookmark.setMetaDataItem(QStringLiteral("UDI"), udi());
250 } else if (role == "isSystemItem") {
251 m_bookmark.setMetaDataItem(QStringLiteral("isSystemItem"), isSystemItem() ? QStringLiteral("true") : QStringLiteral("false"));
252 } else if (role == "isHidden") {
253 m_bookmark.setMetaDataItem(QStringLiteral("IsHidden"), isHidden() ? QStringLiteral("true") : QStringLiteral("false"));
254 }
255 }
256
257 QString PlacesItem::generateNewId()
258 {
259 // The ID-generation must be different as done in KFilePlacesItem from kdelibs
260 // to prevent identical IDs, because 'count' is of course not shared. We append a
261 // " (V2)" to indicate that the ID has been generated by
262 // a new version of the places view.
263 static int count = 0;
264 return QString::number(QDateTime::currentDateTimeUtc().toTime_t()) +
265 '/' + QString::number(count++) + " (V2)";
266 }
267
268 PlacesItemSignalHandler *PlacesItem::signalHandler() const
269 {
270 return m_signalHandler.data();
271 }