]>
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
;
128 const QString udi
= bookmark
.metaDataItem("UDI");
130 setIcon(bookmark
.icon());
131 setText(bookmark
.text());
132 setUrl(bookmark
.url());
134 initializeDevice(udi
);
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;
145 setHidden(bookmark
.metaDataItem("IsHidden") == QLatin1String("true"));
148 KBookmark
PlacesItem::bookmark() const
153 PlacesItem::GroupType
PlacesItem::groupType() const
155 if (udi().isEmpty()) {
156 const QString protocol
= url().protocol();
157 if (protocol
== QLatin1String("timeline")) {
158 return RecentlyAccessedType
;
161 if (protocol
== QLatin1String("search")) {
162 return SearchForType
;
171 KBookmark
PlacesItem::createBookmark(KBookmarkManager
* manager
,
174 const QString
& iconName
)
176 KBookmarkGroup root
= manager
->root();
181 KBookmark bookmark
= root
.addBookmark(text
, url
, iconName
);
182 bookmark
.setFullText(text
);
183 bookmark
.setMetaDataItem("ID", generateNewId());
188 KBookmark
PlacesItem::createDeviceBookmark(KBookmarkManager
* manager
,
191 KBookmarkGroup root
= manager
->root();
196 KBookmark bookmark
= root
.createNewSeparator();
197 bookmark
.setMetaDataItem("UDI", udi
);
198 bookmark
.setMetaDataItem("isSystemItem", "true");
202 void PlacesItem::onDataValueChanged(const QByteArray
& role
,
203 const QVariant
& current
,
204 const QVariant
& previous
)
209 if (!m_bookmark
.isNull()) {
210 updateBookmarkForRole(role
);
214 void PlacesItem::onDataChanged(const QHash
<QByteArray
, QVariant
>& current
,
215 const QHash
<QByteArray
, QVariant
>& previous
)
219 if (!m_bookmark
.isNull()) {
220 QHashIterator
<QByteArray
, QVariant
> it(current
);
221 while (it
.hasNext()) {
223 updateBookmarkForRole(it
.key());
228 void PlacesItem::initializeDevice(const QString
& udi
)
230 m_device
= Solid::Device(udi
);
231 if (!m_device
.isValid()) {
235 m_access
= m_device
.as
<Solid::StorageAccess
>();
236 m_volume
= m_device
.as
<Solid::StorageVolume
>();
237 m_disc
= m_device
.as
<Solid::OpticalDisc
>();
239 setText(m_device
.description());
240 setIcon(m_device
.icon());
241 setIconOverlays(m_device
.emblems());
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
));
254 void PlacesItem::onAccessibilityChanged()
256 setIconOverlays(m_device
.emblems());
259 void PlacesItem::onTrashDirListerCompleted()
261 Q_ASSERT(url().protocol() == QLatin1String("trash"));
263 const bool isTrashEmpty
= m_trashDirLister
->items().isEmpty();
264 setIcon(isTrashEmpty
? "user-trash" : "user-trash-full");
267 void PlacesItem::updateBookmarkForRole(const QByteArray
& role
)
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");
285 QString
PlacesItem::generateNewId()
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)";