]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitem.cpp
93cc4d5c3d9be14c02c22f06dcff86e7af6c28d4
[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
25 #include <KBookmarkManager>
26 #include "dolphindebug.h"
27 #include <KDirLister>
28 #include <KLocalizedString>
29 #include "placesitemsignalhandler.h"
30 #include <QDateTime>
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_trashDirLister(nullptr),
42 m_bookmark()
43 {
44 m_signalHandler = new PlacesItemSignalHandler(this);
45 setBookmark(bookmark);
46 }
47
48 PlacesItem::~PlacesItem()
49 {
50 delete m_signalHandler;
51 delete m_trashDirLister;
52 }
53
54 void PlacesItem::setUrl(const QUrl &url)
55 {
56 // The default check in KStandardItem::setDataValue()
57 // for equal values does not work with a custom value
58 // like QUrl. Hence do a manual check to prevent that
59 // setting an equal URL results in an itemsChanged()
60 // signal.
61 if (dataValue("url").toUrl() != url) {
62 delete m_trashDirLister;
63 if (url.scheme() == QLatin1String("trash")) {
64 // The trash icon must always be updated dependent on whether
65 // the trash is empty or not. We use a KDirLister that automatically
66 // watches for changes if the number of items has been changed.
67 // The update of the icon is handled in onTrashDirListerCompleted().
68 m_trashDirLister = new KDirLister();
69 m_trashDirLister->setAutoErrorHandlingEnabled(false, 0);
70 m_trashDirLister->setDelayedMimeTypes(true);
71 QObject::connect(m_trashDirLister.data(), static_cast<void(KDirLister::*)()>(&KDirLister::completed),
72 m_signalHandler.data(), &PlacesItemSignalHandler::onTrashDirListerCompleted);
73 m_trashDirLister->openUrl(url);
74 }
75
76 setDataValue("url", url);
77 }
78 }
79
80 QUrl PlacesItem::url() const
81 {
82 return dataValue("url").toUrl();
83 }
84
85 void PlacesItem::setUdi(const QString& udi)
86 {
87 setDataValue("udi", udi);
88 }
89
90 QString PlacesItem::udi() const
91 {
92 return dataValue("udi").toString();
93 }
94
95 void PlacesItem::setHidden(bool hidden)
96 {
97 setDataValue("isHidden", hidden);
98 }
99
100 bool PlacesItem::isHidden() const
101 {
102 return dataValue("isHidden").toBool();
103 }
104
105 void PlacesItem::setSystemItem(bool isSystemItem)
106 {
107 setDataValue("isSystemItem", isSystemItem);
108 }
109
110 bool PlacesItem::isSystemItem() const
111 {
112 return dataValue("isSystemItem").toBool();
113 }
114
115 Solid::Device PlacesItem::device() const
116 {
117 return m_device;
118 }
119
120 void PlacesItem::setBookmark(const KBookmark& bookmark)
121 {
122 if (bookmark == m_bookmark) {
123 return;
124 }
125
126 m_bookmark = bookmark;
127
128 delete m_access;
129 delete m_volume;
130 delete m_disc;
131 delete m_mtp;
132
133
134 const QString udi = bookmark.metaDataItem(QStringLiteral("UDI"));
135 if (udi.isEmpty()) {
136 setIcon(bookmark.icon());
137 setText(i18nc("KFile System Bookmarks", bookmark.text().toUtf8().constData()));
138 setUrl(bookmark.url());
139 } else {
140 initializeDevice(udi);
141 }
142
143 const GroupType type = groupType();
144 if (icon().isEmpty()) {
145 switch (type) {
146 case RecentlySavedType: setIcon(QStringLiteral("chronometer")); break;
147 case SearchForType: setIcon(QStringLiteral("system-search")); break;
148 case PlacesType:
149 default: setIcon(QStringLiteral("folder"));
150 }
151
152 }
153
154 switch (type) {
155 case PlacesType: setGroup(i18nc("@item", "Places")); break;
156 case RecentlySavedType: setGroup(i18nc("@item", "Recently Saved")); break;
157 case SearchForType: setGroup(i18nc("@item", "Search For")); break;
158 case DevicesType: setGroup(i18nc("@item", "Devices")); break;
159 default: Q_ASSERT(false); break;
160 }
161
162 setHidden(bookmark.metaDataItem(QStringLiteral("IsHidden")) == QLatin1String("true"));
163 }
164
165 KBookmark PlacesItem::bookmark() const
166 {
167 return m_bookmark;
168 }
169
170 PlacesItem::GroupType PlacesItem::groupType() const
171 {
172 if (udi().isEmpty()) {
173 const QString protocol = url().scheme();
174 if (protocol == QLatin1String("timeline")) {
175 return RecentlySavedType;
176 }
177
178 if (protocol.contains(QLatin1String("search"))) {
179 return SearchForType;
180 }
181
182 if (protocol == QLatin1String("bluetooth") || protocol == QLatin1String("obexftp") || protocol == QLatin1String("kdeconnect")) {
183 return DevicesType;
184 }
185
186 return PlacesType;
187 }
188
189 return DevicesType;
190 }
191
192 bool PlacesItem::storageSetupNeeded() const
193 {
194 return m_access ? !m_access->isAccessible() : false;
195 }
196
197 KBookmark PlacesItem::createBookmark(KBookmarkManager* manager,
198 const QString& text,
199 const QUrl& url,
200 const QString& iconName)
201 {
202 KBookmarkGroup root = manager->root();
203 if (root.isNull()) {
204 return KBookmark();
205 }
206
207 KBookmark bookmark = root.addBookmark(text, url, iconName);
208 bookmark.setFullText(text);
209 bookmark.setMetaDataItem(QStringLiteral("ID"), generateNewId());
210
211 return bookmark;
212 }
213
214 KBookmark PlacesItem::createDeviceBookmark(KBookmarkManager* manager,
215 const QString& udi)
216 {
217 KBookmarkGroup root = manager->root();
218 if (root.isNull()) {
219 return KBookmark();
220 }
221
222 KBookmark bookmark = root.createNewSeparator();
223 bookmark.setMetaDataItem(QStringLiteral("UDI"), udi);
224 bookmark.setMetaDataItem(QStringLiteral("isSystemItem"), QStringLiteral("true"));
225 return bookmark;
226 }
227
228 void PlacesItem::onDataValueChanged(const QByteArray& role,
229 const QVariant& current,
230 const QVariant& previous)
231 {
232 Q_UNUSED(current);
233 Q_UNUSED(previous);
234
235 if (!m_bookmark.isNull()) {
236 updateBookmarkForRole(role);
237 }
238 }
239
240 void PlacesItem::onDataChanged(const QHash<QByteArray, QVariant>& current,
241 const QHash<QByteArray, QVariant>& previous)
242 {
243 Q_UNUSED(previous);
244
245 if (!m_bookmark.isNull()) {
246 QHashIterator<QByteArray, QVariant> it(current);
247 while (it.hasNext()) {
248 it.next();
249 updateBookmarkForRole(it.key());
250 }
251 }
252 }
253
254 void PlacesItem::initializeDevice(const QString& udi)
255 {
256 m_device = Solid::Device(udi);
257 if (!m_device.isValid()) {
258 return;
259 }
260
261 m_access = m_device.as<Solid::StorageAccess>();
262 m_volume = m_device.as<Solid::StorageVolume>();
263 m_disc = m_device.as<Solid::OpticalDisc>();
264 m_mtp = m_device.as<Solid::PortableMediaPlayer>();
265
266 setText(m_device.description());
267 setIcon(m_device.icon());
268 setIconOverlays(m_device.emblems());
269 setUdi(udi);
270
271 if (m_access) {
272 setUrl(QUrl::fromLocalFile(m_access->filePath()));
273 QObject::connect(m_access.data(), &Solid::StorageAccess::accessibilityChanged,
274 m_signalHandler.data(), &PlacesItemSignalHandler::onAccessibilityChanged);
275 QObject::connect(m_access.data(), &Solid::StorageAccess::teardownRequested,
276 m_signalHandler.data(), &PlacesItemSignalHandler::onTearDownRequested);
277 } else if (m_disc && (m_disc->availableContent() & Solid::OpticalDisc::Audio) != 0) {
278 Solid::Block *block = m_device.as<Solid::Block>();
279 if (block) {
280 const QString device = block->device();
281 setUrl(QUrl(QStringLiteral("audiocd:/?device=%1").arg(device)));
282 } else {
283 setUrl(QUrl(QStringLiteral("audiocd:/")));
284 }
285 } else if (m_mtp) {
286 setUrl(QUrl(QStringLiteral("mtp:udi=%1").arg(m_device.udi())));
287 }
288 }
289
290 void PlacesItem::onAccessibilityChanged()
291 {
292 setIconOverlays(m_device.emblems());
293 setUrl(QUrl::fromLocalFile(m_access->filePath()));
294 }
295
296 void PlacesItem::onTrashDirListerCompleted()
297 {
298 Q_ASSERT(url().scheme() == QLatin1String("trash"));
299
300 const bool isTrashEmpty = m_trashDirLister->items().isEmpty();
301 setIcon(isTrashEmpty ? QStringLiteral("user-trash") : QStringLiteral("user-trash-full"));
302 }
303
304 void PlacesItem::updateBookmarkForRole(const QByteArray& role)
305 {
306 Q_ASSERT(!m_bookmark.isNull());
307 if (role == "iconName") {
308 m_bookmark.setIcon(icon());
309 } else if (role == "text") {
310 // Only store the text in the KBookmark if it is not the translation of
311 // the current text. This makes sure that the text is re-translated if
312 // the user chooses another language, or the translation itself changes.
313 //
314 // NOTE: It is important to use "KFile System Bookmarks" as context
315 // (see PlacesItemModel::createSystemBookmarks()).
316 if (text() != i18nc("KFile System Bookmarks", m_bookmark.text().toUtf8().data())) {
317 m_bookmark.setFullText(text());
318 }
319 } else if (role == "url") {
320 m_bookmark.setUrl(url());
321 } else if (role == "udi") {
322 m_bookmark.setMetaDataItem(QStringLiteral("UDI"), udi());
323 } else if (role == "isSystemItem") {
324 m_bookmark.setMetaDataItem(QStringLiteral("isSystemItem"), isSystemItem() ? QStringLiteral("true") : QStringLiteral("false"));
325 } else if (role == "isHidden") {
326 m_bookmark.setMetaDataItem(QStringLiteral("IsHidden"), isHidden() ? QStringLiteral("true") : QStringLiteral("false"));
327 }
328 }
329
330 QString PlacesItem::generateNewId()
331 {
332 // The ID-generation must be different as done in KFilePlacesItem from kdelibs
333 // to prevent identical IDs, because 'count' is of course not shared. We append a
334 // " (V2)" to indicate that the ID has been generated by
335 // a new version of the places view.
336 static int count = 0;
337 return QString::number(QDateTime::currentDateTimeUtc().toTime_t()) +
338 '/' + QString::number(count++) + " (V2)";
339 }
340
341 PlacesItemSignalHandler *PlacesItem::signalHandler() const
342 {
343 return m_signalHandler.data();
344 }