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