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>
26 #include "dolphindebug.h"
28 #include <KLocalizedString>
29 #include "placesitemsignalhandler.h"
31 #include <Solid/Block>
33 PlacesItem::PlacesItem(const KBookmark
& bookmark
, PlacesItem
* parent
) :
34 KStandardItem(parent
),
40 m_signalHandler(nullptr),
41 m_trashDirLister(nullptr),
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 QUrl
&url
)
56 // The default check in KStandardItem::setDataValue()
57 // for equal values does not work with a custom value
58 // like QUrl. Hence do a manual check to prevent that
59 // setting an equal URL results in an itemsChanged()
61 if (dataValue("url").toUrl() != url
) {
62 delete m_trashDirLister
;
63 if (url
.scheme() == 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, nullptr);
70 m_trashDirLister
->setDelayedMimeTypes(true);
71 QObject::connect(m_trashDirLister
.data(), static_cast<void(KDirLister::*)()>(&KDirLister::completed
),
72 m_signalHandler
.data(), &PlacesItemSignalHandler::onTrashDirListerCompleted
);
73 m_trashDirLister
->openUrl(url
);
76 setDataValue("url", url
);
80 QUrl
PlacesItem::url() const
82 return dataValue("url").toUrl();
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 bool PlacesItem::isGroupHidden() const
107 return dataValue("isGroupHidden").toBool();
110 void PlacesItem::setGroupHidden(bool hidden
)
112 setDataValue("isGroupHidden", hidden
);
115 void PlacesItem::setSystemItem(bool isSystemItem
)
117 setDataValue("isSystemItem", isSystemItem
);
120 bool PlacesItem::isSystemItem() const
122 return dataValue("isSystemItem").toBool();
125 Solid::Device
PlacesItem::device() const
130 void PlacesItem::setBookmark(const KBookmark
& bookmark
)
132 if (bookmark
== m_bookmark
) {
136 m_bookmark
= bookmark
;
143 const QString udi
= bookmark
.metaDataItem(QStringLiteral("UDI"));
145 setIcon(bookmark
.icon());
146 setText(i18nc("KFile System Bookmarks", bookmark
.text().toUtf8().constData()));
147 setUrl(bookmark
.url());
148 setSystemItem(bookmark
.metaDataItem(QStringLiteral("isSystemItem")) == QLatin1String("true"));
150 initializeDevice(udi
);
153 setHidden(bookmark
.metaDataItem(QStringLiteral("IsHidden")) == QLatin1String("true"));
156 KBookmark
PlacesItem::bookmark() const
161 bool PlacesItem::storageSetupNeeded() const
163 return m_access
? !m_access
->isAccessible() : false;
166 bool PlacesItem::isSearchOrTimelineUrl() const
168 const QString urlScheme
= url().scheme();
169 return (urlScheme
.contains("search") || urlScheme
.contains("timeline"));
172 void PlacesItem::onDataValueChanged(const QByteArray
& role
,
173 const QVariant
& current
,
174 const QVariant
& previous
)
179 if (!m_bookmark
.isNull()) {
180 updateBookmarkForRole(role
);
184 void PlacesItem::onDataChanged(const QHash
<QByteArray
, QVariant
>& current
,
185 const QHash
<QByteArray
, QVariant
>& previous
)
189 if (!m_bookmark
.isNull()) {
190 QHashIterator
<QByteArray
, QVariant
> it(current
);
191 while (it
.hasNext()) {
193 updateBookmarkForRole(it
.key());
198 void PlacesItem::initializeDevice(const QString
& udi
)
200 m_device
= Solid::Device(udi
);
201 if (!m_device
.isValid()) {
205 m_access
= m_device
.as
<Solid::StorageAccess
>();
206 m_volume
= m_device
.as
<Solid::StorageVolume
>();
207 m_disc
= m_device
.as
<Solid::OpticalDisc
>();
208 m_mtp
= m_device
.as
<Solid::PortableMediaPlayer
>();
210 setText(m_device
.description());
211 setIcon(m_device
.icon());
212 setIconOverlays(m_device
.emblems());
216 setUrl(QUrl::fromLocalFile(m_access
->filePath()));
217 QObject::connect(m_access
.data(), &Solid::StorageAccess::accessibilityChanged
,
218 m_signalHandler
.data(), &PlacesItemSignalHandler::onAccessibilityChanged
);
219 QObject::connect(m_access
.data(), &Solid::StorageAccess::teardownRequested
,
220 m_signalHandler
.data(), &PlacesItemSignalHandler::onTearDownRequested
);
221 } else if (m_disc
&& (m_disc
->availableContent() & Solid::OpticalDisc::Audio
) != 0) {
222 Solid::Block
*block
= m_device
.as
<Solid::Block
>();
224 const QString device
= block
->device();
225 setUrl(QUrl(QStringLiteral("audiocd:/?device=%1").arg(device
)));
227 setUrl(QUrl(QStringLiteral("audiocd:/")));
230 setUrl(QUrl(QStringLiteral("mtp:udi=%1").arg(m_device
.udi())));
234 void PlacesItem::onAccessibilityChanged()
236 setIconOverlays(m_device
.emblems());
237 setUrl(QUrl::fromLocalFile(m_access
->filePath()));
240 void PlacesItem::onTrashDirListerCompleted()
242 Q_ASSERT(url().scheme() == QLatin1String("trash"));
244 const bool isTrashEmpty
= m_trashDirLister
->items().isEmpty();
245 setIcon(isTrashEmpty
? QStringLiteral("user-trash") : QStringLiteral("user-trash-full"));
248 void PlacesItem::updateBookmarkForRole(const QByteArray
& role
)
250 Q_ASSERT(!m_bookmark
.isNull());
251 if (role
== "iconName") {
252 m_bookmark
.setIcon(icon());
253 } else if (role
== "text") {
254 // Only store the text in the KBookmark if it is not the translation of
255 // the current text. This makes sure that the text is re-translated if
256 // the user chooses another language, or the translation itself changes.
258 // NOTE: It is important to use "KFile System Bookmarks" as context
259 // (see PlacesItemModel::createSystemBookmarks()).
260 if (text() != i18nc("KFile System Bookmarks", m_bookmark
.text().toUtf8().data())) {
261 m_bookmark
.setFullText(text());
263 } else if (role
== "url") {
264 m_bookmark
.setUrl(url());
265 } else if (role
== "udi") {
266 m_bookmark
.setMetaDataItem(QStringLiteral("UDI"), udi());
267 } else if (role
== "isSystemItem") {
268 m_bookmark
.setMetaDataItem(QStringLiteral("isSystemItem"), isSystemItem() ? QStringLiteral("true") : QStringLiteral("false"));
269 } else if (role
== "isHidden") {
270 m_bookmark
.setMetaDataItem(QStringLiteral("IsHidden"), isHidden() ? QStringLiteral("true") : QStringLiteral("false"));
274 QString
PlacesItem::generateNewId()
276 // The ID-generation must be different as done in KFilePlacesItem from kdelibs
277 // to prevent identical IDs, because 'count' is of course not shared. We append a
278 // " (V2)" to indicate that the ID has been generated by
279 // a new version of the places view.
280 static int count
= 0;
281 return QString::number(QDateTime::currentDateTimeUtc().toTime_t()) +
282 '/' + QString::number(count
++) + " (V2)";
285 PlacesItemSignalHandler
*PlacesItem::signalHandler() const
287 return m_signalHandler
.data();