]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitem.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
4 * Based on KFilePlacesItem from kdelibs: *
5 * Copyright (C) 2007 Kevin Ottens <ervin@kde.org> *
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. *
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. *
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 ***************************************************************************/
23 #include "placesitem.h"
25 #include <KBookmarkManager>
30 #include "placesitemsignalhandler.h"
32 #include <Solid/Block>
34 PlacesItem::PlacesItem(const KBookmark
& bookmark
, PlacesItem
* parent
) :
35 KStandardItem(parent
),
45 m_signalHandler
= new PlacesItemSignalHandler(this);
46 setBookmark(bookmark
);
49 PlacesItem::~PlacesItem()
51 delete m_signalHandler
;
52 delete m_trashDirLister
;
55 void PlacesItem::setUrl(const KUrl
& url
)
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()
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
, SIGNAL(completed()),
73 m_signalHandler
, SLOT(onTrashDirListerCompleted()));
74 m_trashDirLister
->openUrl(url
);
77 setDataValue("url", url
);
81 KUrl
PlacesItem::url() const
83 return dataValue("url").value
<KUrl
>();
86 void PlacesItem::setUdi(const QString
& udi
)
88 setDataValue("udi", udi
);
91 QString
PlacesItem::udi() const
93 return dataValue("udi").toString();
96 void PlacesItem::setHidden(bool hidden
)
98 setDataValue("isHidden", hidden
);
101 bool PlacesItem::isHidden() const
103 return dataValue("isHidden").toBool();
106 void PlacesItem::setSystemItem(bool isSystemItem
)
108 setDataValue("isSystemItem", isSystemItem
);
111 bool PlacesItem::isSystemItem() const
113 return dataValue("isSystemItem").toBool();
116 Solid::Device
PlacesItem::device() const
121 void PlacesItem::setBookmark(const KBookmark
& bookmark
)
123 m_bookmark
= bookmark
;
131 const QString udi
= bookmark
.metaDataItem("UDI");
133 setIcon(bookmark
.icon());
134 setText(bookmark
.text());
135 setUrl(bookmark
.url());
137 initializeDevice(udi
);
140 const GroupType type
= groupType();
141 if (icon().isEmpty()) {
143 case RecentlyAccessedType
: setIcon("chronometer"); break;
144 case SearchForType
: setIcon("nepomuk"); break;
146 default: setIcon("folder");
152 case PlacesType
: setGroup(i18nc("@item", "Places")); break;
153 case RecentlyAccessedType
: setGroup(i18nc("@item", "Recently Accessed")); break;
154 case SearchForType
: setGroup(i18nc("@item", "Search For")); break;
155 case DevicesType
: setGroup(i18nc("@item", "Devices")); break;
156 default: Q_ASSERT(false); break;
159 setHidden(bookmark
.metaDataItem("IsHidden") == QLatin1String("true"));
162 KBookmark
PlacesItem::bookmark() const
167 PlacesItem::GroupType
PlacesItem::groupType() const
169 if (udi().isEmpty()) {
170 const QString protocol
= url().protocol();
171 if (protocol
== QLatin1String("timeline")) {
172 return RecentlyAccessedType
;
175 if (protocol
.contains(QLatin1String("search"))) {
176 return SearchForType
;
179 if (protocol
== QLatin1String("bluetooth")) {
189 bool PlacesItem::storageSetupNeeded() const
191 return m_access
? !m_access
->isAccessible() : false;
194 KBookmark
PlacesItem::createBookmark(KBookmarkManager
* manager
,
197 const QString
& iconName
)
199 KBookmarkGroup root
= manager
->root();
204 KBookmark bookmark
= root
.addBookmark(text
, url
, iconName
);
205 bookmark
.setFullText(text
);
206 bookmark
.setMetaDataItem("ID", generateNewId());
211 KBookmark
PlacesItem::createDeviceBookmark(KBookmarkManager
* manager
,
214 KBookmarkGroup root
= manager
->root();
219 KBookmark bookmark
= root
.createNewSeparator();
220 bookmark
.setMetaDataItem("UDI", udi
);
221 bookmark
.setMetaDataItem("isSystemItem", "true");
225 void PlacesItem::onDataValueChanged(const QByteArray
& role
,
226 const QVariant
& current
,
227 const QVariant
& previous
)
232 if (!m_bookmark
.isNull()) {
233 updateBookmarkForRole(role
);
237 void PlacesItem::onDataChanged(const QHash
<QByteArray
, QVariant
>& current
,
238 const QHash
<QByteArray
, QVariant
>& previous
)
242 if (!m_bookmark
.isNull()) {
243 QHashIterator
<QByteArray
, QVariant
> it(current
);
244 while (it
.hasNext()) {
246 updateBookmarkForRole(it
.key());
251 void PlacesItem::initializeDevice(const QString
& udi
)
253 m_device
= Solid::Device(udi
);
254 if (!m_device
.isValid()) {
258 m_access
= m_device
.as
<Solid::StorageAccess
>();
259 m_volume
= m_device
.as
<Solid::StorageVolume
>();
260 m_disc
= m_device
.as
<Solid::OpticalDisc
>();
261 m_mtp
= m_device
.as
<Solid::PortableMediaPlayer
>();
263 setText(m_device
.description());
264 setIcon(m_device
.icon());
265 setIconOverlays(m_device
.emblems());
269 setUrl(m_access
->filePath());
270 QObject::connect(m_access
, SIGNAL(accessibilityChanged(bool,QString
)),
271 m_signalHandler
, SLOT(onAccessibilityChanged()));
272 } else if (m_disc
&& (m_disc
->availableContent() & Solid::OpticalDisc::Audio
) != 0) {
273 const QString device
= m_device
.as
<Solid::Block
>()->device();
274 setUrl(QString("audiocd:/?device=%1").arg(device
));
276 setUrl(QString("mtp:udi=%1").arg(m_device
.udi()));
280 void PlacesItem::onAccessibilityChanged()
282 setIconOverlays(m_device
.emblems());
283 setUrl(m_access
->filePath());
286 void PlacesItem::onTrashDirListerCompleted()
288 Q_ASSERT(url().protocol() == QLatin1String("trash"));
290 const bool isTrashEmpty
= m_trashDirLister
->items().isEmpty();
291 setIcon(isTrashEmpty
? "user-trash" : "user-trash-full");
294 void PlacesItem::updateBookmarkForRole(const QByteArray
& role
)
296 Q_ASSERT(!m_bookmark
.isNull());
297 if (role
== "iconName") {
298 m_bookmark
.setIcon(icon());
299 } else if (role
== "text") {
300 m_bookmark
.setFullText(text());
301 } else if (role
== "url") {
302 m_bookmark
.setUrl(url());
303 } else if (role
== "udi)") {
304 m_bookmark
.setMetaDataItem("UDI", udi());
305 } else if (role
== "isSystemItem") {
306 m_bookmark
.setMetaDataItem("isSystemItem", isSystemItem() ? "true" : "false");
307 } else if (role
== "isHidden") {
308 m_bookmark
.setMetaDataItem("IsHidden", isHidden() ? "true" : "false");
312 QString
PlacesItem::generateNewId()
314 // The ID-generation must be different as done in KFilePlacesItem from kdelibs
315 // to prevent identical IDs, because 'count' is of course not shared. We append a
316 // " (V2)" to indicate that the ID has been generated by
317 // a new version of the places view.
318 static int count
= 0;
319 return QString::number(QDateTime::currentDateTime().toTime_t()) +
320 '/' + QString::number(count
++) + " (V2)";