]>
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
),
44 m_signalHandler
= new PlacesItemSignalHandler(this);
45 setBookmark(bookmark
);
48 PlacesItem::~PlacesItem()
50 delete m_signalHandler
;
51 delete m_trashDirLister
;
54 void PlacesItem::setUrl(const KUrl
& url
)
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()
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
);
76 setDataValue("url", url
);
80 KUrl
PlacesItem::url() const
82 return dataValue("url").value
<KUrl
>();
85 void PlacesItem::setUdi(const QString
& udi
)
87 setDataValue("udi", udi
);
90 QString
PlacesItem::udi() const
92 return dataValue("udi").toString();
95 void PlacesItem::setHidden(bool hidden
)
97 setDataValue("isHidden", hidden
);
100 bool PlacesItem::isHidden() const
102 return dataValue("isHidden").toBool();
105 void PlacesItem::setSystemItem(bool isSystemItem
)
107 setDataValue("isSystemItem", isSystemItem
);
110 bool PlacesItem::isSystemItem() const
112 return dataValue("isSystemItem").toBool();
115 Solid::Device
PlacesItem::device() const
120 void PlacesItem::setBookmark(const KBookmark
& bookmark
)
122 m_bookmark
= bookmark
;
129 const QString udi
= bookmark
.metaDataItem("UDI");
131 setIcon(bookmark
.icon());
132 setText(bookmark
.text());
133 setUrl(bookmark
.url());
135 initializeDevice(udi
);
138 const GroupType type
= groupType();
139 if (icon().isEmpty()) {
141 case RecentlyAccessedType
: setIcon("chronometer"); break;
142 case SearchForType
: setIcon("nepomuk"); break;
144 default: setIcon("folder");
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;
157 setHidden(bookmark
.metaDataItem("IsHidden") == QLatin1String("true"));
160 KBookmark
PlacesItem::bookmark() const
165 PlacesItem::GroupType
PlacesItem::groupType() const
167 if (udi().isEmpty()) {
168 const QString protocol
= url().protocol();
169 if (protocol
== QLatin1String("timeline")) {
170 return RecentlyAccessedType
;
173 if (protocol
.contains(QLatin1String("search"))) {
174 return SearchForType
;
183 bool PlacesItem::storageSetupNeeded() const
185 return m_access
? !m_access
->isAccessible() : false;
188 KBookmark
PlacesItem::createBookmark(KBookmarkManager
* manager
,
191 const QString
& iconName
)
193 KBookmarkGroup root
= manager
->root();
198 KBookmark bookmark
= root
.addBookmark(text
, url
, iconName
);
199 bookmark
.setFullText(text
);
200 bookmark
.setMetaDataItem("ID", generateNewId());
205 KBookmark
PlacesItem::createDeviceBookmark(KBookmarkManager
* manager
,
208 KBookmarkGroup root
= manager
->root();
213 KBookmark bookmark
= root
.createNewSeparator();
214 bookmark
.setMetaDataItem("UDI", udi
);
215 bookmark
.setMetaDataItem("isSystemItem", "true");
219 void PlacesItem::onDataValueChanged(const QByteArray
& role
,
220 const QVariant
& current
,
221 const QVariant
& previous
)
226 if (!m_bookmark
.isNull()) {
227 updateBookmarkForRole(role
);
231 void PlacesItem::onDataChanged(const QHash
<QByteArray
, QVariant
>& current
,
232 const QHash
<QByteArray
, QVariant
>& previous
)
236 if (!m_bookmark
.isNull()) {
237 QHashIterator
<QByteArray
, QVariant
> it(current
);
238 while (it
.hasNext()) {
240 updateBookmarkForRole(it
.key());
245 void PlacesItem::initializeDevice(const QString
& udi
)
247 m_device
= Solid::Device(udi
);
248 if (!m_device
.isValid()) {
252 m_access
= m_device
.as
<Solid::StorageAccess
>();
253 m_volume
= m_device
.as
<Solid::StorageVolume
>();
254 m_disc
= m_device
.as
<Solid::OpticalDisc
>();
256 setText(m_device
.description());
257 setIcon(m_device
.icon());
258 setIconOverlays(m_device
.emblems());
262 setUrl(m_access
->filePath());
263 QObject::connect(m_access
, SIGNAL(accessibilityChanged(bool,QString
)),
264 m_signalHandler
, SLOT(onAccessibilityChanged()));
265 } else if (m_disc
&& (m_disc
->availableContent() & Solid::OpticalDisc::Audio
) != 0) {
266 const QString device
= m_device
.as
<Solid::Block
>()->device();
267 setUrl(QString("audiocd:/?device=%1").arg(device
));
271 void PlacesItem::onAccessibilityChanged()
273 setIconOverlays(m_device
.emblems());
274 setUrl(m_access
->filePath());
277 void PlacesItem::onTrashDirListerCompleted()
279 Q_ASSERT(url().protocol() == QLatin1String("trash"));
281 const bool isTrashEmpty
= m_trashDirLister
->items().isEmpty();
282 setIcon(isTrashEmpty
? "user-trash" : "user-trash-full");
285 void PlacesItem::updateBookmarkForRole(const QByteArray
& role
)
287 Q_ASSERT(!m_bookmark
.isNull());
288 if (role
== "iconName") {
289 m_bookmark
.setIcon(icon());
290 } else if (role
== "text") {
291 m_bookmark
.setFullText(text());
292 } else if (role
== "url") {
293 m_bookmark
.setUrl(url());
294 } else if (role
== "udi)") {
295 m_bookmark
.setMetaDataItem("UDI", udi());
296 } else if (role
== "isSystemItem") {
297 m_bookmark
.setMetaDataItem("isSystemItem", isSystemItem() ? "true" : "false");
298 } else if (role
== "isHidden") {
299 m_bookmark
.setMetaDataItem("IsHidden", isHidden() ? "true" : "false");
303 QString
PlacesItem::generateNewId()
305 // The ID-generation must be different as done in KFilePlacesItem from kdelibs
306 // to prevent identical IDs, because 'count' is of course not shared. We append a
307 // " (V2)" to indicate that the ID has been generated by
308 // a new version of the places view.
309 static int count
= 0;
310 return QString::number(QDateTime::currentDateTime().toTime_t()) +
311 '/' + QString::number(count
++) + " (V2)";