]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitem.cpp
Use PlacesItemModel instead of KFilePlacesModel
[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 <KIcon>
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_signalHandler(0),
41 m_trashDirLister(0),
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 KUrl& url)
55 {
56 // The default check in KStandardItem::setDataValue()
57 // for equal values does not work with a custom value
58 // like KUrl. Hence do a manual check to prevent that
59 // setting an equal URL results in an itemsChanged()
60 // signal.
61 if (dataValue("url").value<KUrl>() != url) {
62 delete m_trashDirLister;
63 if (url.protocol() == 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, SIGNAL(completed()),
72 m_signalHandler, SLOT(onTrashDirListerCompleted()));
73 m_trashDirLister->openUrl(url);
74 }
75
76 setDataValue("url", url);
77 }
78 }
79
80 KUrl PlacesItem::url() const
81 {
82 return dataValue("url").value<KUrl>();
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 m_bookmark = bookmark;
123
124 delete m_access;
125 delete m_volume;
126 delete m_disc;
127
128
129 const QString udi = bookmark.metaDataItem("UDI");
130 if (udi.isEmpty()) {
131 setIcon(bookmark.icon());
132 setText(bookmark.text());
133 setUrl(bookmark.url());
134 } else {
135 initializeDevice(udi);
136 }
137
138 const GroupType type = groupType();
139 if (icon().isEmpty()) {
140 switch (type) {
141 case RecentlyAccessedType: setIcon("package_utility_time"); break;
142 case SearchForType: setIcon("nepomuk"); break;
143 case PlacesType:
144 default: setIcon("folder");
145 }
146
147 }
148
149 switch (type) {
150 case PlacesType: setGroup(i18nc("@item", "Places")); break;
151 case RecentlyAccessedType: setGroup(i18nc("@item", "Recently Accessed")); break;
152 case SearchForType: setGroup(i18nc("@item", "Search For")); break;
153 case DevicesType: setGroup(i18nc("@item", "Devices")); break;
154 default: Q_ASSERT(false); break;
155 }
156
157 setHidden(bookmark.metaDataItem("IsHidden") == QLatin1String("true"));
158 }
159
160 KBookmark PlacesItem::bookmark() const
161 {
162 return m_bookmark;
163 }
164
165 PlacesItem::GroupType PlacesItem::groupType() const
166 {
167 if (udi().isEmpty()) {
168 const QString protocol = url().protocol();
169 if (protocol == QLatin1String("timeline")) {
170 return RecentlyAccessedType;
171 }
172
173 if (protocol.contains(QLatin1String("search"))) {
174 return SearchForType;
175 }
176
177 return PlacesType;
178 }
179
180 return DevicesType;
181 }
182
183 KBookmark PlacesItem::createBookmark(KBookmarkManager* manager,
184 const QString& text,
185 const KUrl& url,
186 const QString& iconName)
187 {
188 KBookmarkGroup root = manager->root();
189 if (root.isNull()) {
190 return KBookmark();
191 }
192
193 KBookmark bookmark = root.addBookmark(text, url, iconName);
194 bookmark.setFullText(text);
195 bookmark.setMetaDataItem("ID", generateNewId());
196
197 return bookmark;
198 }
199
200 KBookmark PlacesItem::createDeviceBookmark(KBookmarkManager* manager,
201 const QString& udi)
202 {
203 KBookmarkGroup root = manager->root();
204 if (root.isNull()) {
205 return KBookmark();
206 }
207
208 KBookmark bookmark = root.createNewSeparator();
209 bookmark.setMetaDataItem("UDI", udi);
210 bookmark.setMetaDataItem("isSystemItem", "true");
211 return bookmark;
212 }
213
214 void PlacesItem::onDataValueChanged(const QByteArray& role,
215 const QVariant& current,
216 const QVariant& previous)
217 {
218 Q_UNUSED(current);
219 Q_UNUSED(previous);
220
221 if (!m_bookmark.isNull()) {
222 updateBookmarkForRole(role);
223 }
224 }
225
226 void PlacesItem::onDataChanged(const QHash<QByteArray, QVariant>& current,
227 const QHash<QByteArray, QVariant>& previous)
228 {
229 Q_UNUSED(previous);
230
231 if (!m_bookmark.isNull()) {
232 QHashIterator<QByteArray, QVariant> it(current);
233 while (it.hasNext()) {
234 it.next();
235 updateBookmarkForRole(it.key());
236 }
237 }
238 }
239
240 void PlacesItem::initializeDevice(const QString& udi)
241 {
242 m_device = Solid::Device(udi);
243 if (!m_device.isValid()) {
244 return;
245 }
246
247 m_access = m_device.as<Solid::StorageAccess>();
248 m_volume = m_device.as<Solid::StorageVolume>();
249 m_disc = m_device.as<Solid::OpticalDisc>();
250
251 setText(m_device.description());
252 setIcon(m_device.icon());
253 setIconOverlays(m_device.emblems());
254 setUdi(udi);
255
256 if (m_access) {
257 setUrl(m_access->filePath());
258 QObject::connect(m_access, SIGNAL(accessibilityChanged(bool,QString)),
259 m_signalHandler, SLOT(onAccessibilityChanged()));
260 } else if (m_disc && (m_disc->availableContent() & Solid::OpticalDisc::Audio) != 0) {
261 const QString device = m_device.as<Solid::Block>()->device();
262 setUrl(QString("audiocd:/?device=%1").arg(device));
263 }
264 }
265
266 void PlacesItem::onAccessibilityChanged()
267 {
268 setIconOverlays(m_device.emblems());
269 }
270
271 void PlacesItem::onTrashDirListerCompleted()
272 {
273 Q_ASSERT(url().protocol() == QLatin1String("trash"));
274
275 const bool isTrashEmpty = m_trashDirLister->items().isEmpty();
276 setIcon(isTrashEmpty ? "user-trash" : "user-trash-full");
277 }
278
279 void PlacesItem::updateBookmarkForRole(const QByteArray& role)
280 {
281 Q_ASSERT(!m_bookmark.isNull());
282 if (role == "iconName") {
283 m_bookmark.setIcon(icon());
284 } else if (role == "text") {
285 m_bookmark.setFullText(text());
286 } else if (role == "url") {
287 m_bookmark.setUrl(url());
288 } else if (role == "udi)") {
289 m_bookmark.setMetaDataItem("UDI", udi());
290 } else if (role == "isSystemItem") {
291 m_bookmark.setMetaDataItem("isSystemItem", isSystemItem() ? "true" : "false");
292 } else if (role == "isHidden") {
293 m_bookmark.setMetaDataItem("IsHidden", isHidden() ? "true" : "false");
294 }
295 }
296
297 QString PlacesItem::generateNewId()
298 {
299 // The ID-generation must be different as done in KFilePlacesItem from kdelibs
300 // to prevent identical IDs, because 'count' is of course not shared. We append a
301 // " (V2)" to indicate that the ID has been generated by
302 // a new version of the places view.
303 static int count = 0;
304 return QString::number(QDateTime::currentDateTime().toTime_t()) +
305 '/' + QString::number(count++) + " (V2)";
306 }