]>
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("package_utility_time"); 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 KBookmark
PlacesItem::createBookmark(KBookmarkManager
* manager
,
186 const QString
& iconName
)
188 KBookmarkGroup root
= manager
->root();
193 KBookmark bookmark
= root
.addBookmark(text
, url
, iconName
);
194 bookmark
.setFullText(text
);
195 bookmark
.setMetaDataItem("ID", generateNewId());
200 KBookmark
PlacesItem::createDeviceBookmark(KBookmarkManager
* manager
,
203 KBookmarkGroup root
= manager
->root();
208 KBookmark bookmark
= root
.createNewSeparator();
209 bookmark
.setMetaDataItem("UDI", udi
);
210 bookmark
.setMetaDataItem("isSystemItem", "true");
214 void PlacesItem::onDataValueChanged(const QByteArray
& role
,
215 const QVariant
& current
,
216 const QVariant
& previous
)
221 if (!m_bookmark
.isNull()) {
222 updateBookmarkForRole(role
);
226 void PlacesItem::onDataChanged(const QHash
<QByteArray
, QVariant
>& current
,
227 const QHash
<QByteArray
, QVariant
>& previous
)
231 if (!m_bookmark
.isNull()) {
232 QHashIterator
<QByteArray
, QVariant
> it(current
);
233 while (it
.hasNext()) {
235 updateBookmarkForRole(it
.key());
240 void PlacesItem::initializeDevice(const QString
& udi
)
242 m_device
= Solid::Device(udi
);
243 if (!m_device
.isValid()) {
247 m_access
= m_device
.as
<Solid::StorageAccess
>();
248 m_volume
= m_device
.as
<Solid::StorageVolume
>();
249 m_disc
= m_device
.as
<Solid::OpticalDisc
>();
251 setText(m_device
.description());
252 setIcon(m_device
.icon());
253 setIconOverlays(m_device
.emblems());
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
));
266 void PlacesItem::onAccessibilityChanged()
268 setIconOverlays(m_device
.emblems());
271 void PlacesItem::onTrashDirListerCompleted()
273 Q_ASSERT(url().protocol() == QLatin1String("trash"));
275 const bool isTrashEmpty
= m_trashDirLister
->items().isEmpty();
276 setIcon(isTrashEmpty
? "user-trash" : "user-trash-full");
279 void PlacesItem::updateBookmarkForRole(const QByteArray
& role
)
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");
297 QString
PlacesItem::generateNewId()
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)";