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