]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitem.cpp
Merge branch 'Applications/18.04'
[dolphin.git] / src / panels / places / placesitem.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on KFilePlacesItem from kdelibs: *
5 * Copyright (C) 2007 Kevin Ottens <ervin@kde.org> *
6 * *
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. *
11 * *
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. *
16 * *
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 ***************************************************************************/
22
23 #include "placesitem.h"
24 #include "trash/dolphintrash.h"
25
26 #include "dolphindebug.h"
27 #include "placesitemsignalhandler.h"
28
29 #include <KDirLister>
30 #include <KLocalizedString>
31 #include <Solid/Block>
32
33 PlacesItem::PlacesItem(const KBookmark& bookmark, PlacesItem* parent) :
34 KStandardItem(parent),
35 m_device(),
36 m_access(),
37 m_volume(),
38 m_disc(),
39 m_mtp(),
40 m_signalHandler(nullptr),
41 m_trashDirLister(nullptr),
42 m_bookmark()
43 {
44 m_signalHandler = new PlacesItemSignalHandler(this);
45 setBookmark(bookmark);
46 }
47
48 PlacesItem::~PlacesItem()
49 {
50 delete m_signalHandler;
51 delete m_trashDirLister;
52 }
53
54 void PlacesItem::setUrl(const QUrl &url)
55 {
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()
60 // signal.
61 if (dataValue("url").toUrl() != url) {
62 delete m_trashDirLister;
63 if (url.scheme() == QLatin1String("trash")) {
64 QObject::connect(&Trash::instance(), &Trash::emptinessChanged, [this](bool isTrashEmpty){
65 setIcon(isTrashEmpty ? QStringLiteral("user-trash") : QStringLiteral("user-trash-full"));
66 });
67 }
68
69 setDataValue("url", url);
70 }
71 }
72
73 QUrl PlacesItem::url() const
74 {
75 return dataValue("url").toUrl();
76 }
77
78 void PlacesItem::setUdi(const QString& udi)
79 {
80 setDataValue("udi", udi);
81 }
82
83 QString PlacesItem::udi() const
84 {
85 return dataValue("udi").toString();
86 }
87
88 void PlacesItem::setHidden(bool hidden)
89 {
90 setDataValue("isHidden", hidden);
91 }
92
93 bool PlacesItem::isHidden() const
94 {
95 return dataValue("isHidden").toBool();
96 }
97
98 bool PlacesItem::isGroupHidden() const
99 {
100 return dataValue("isGroupHidden").toBool();
101 }
102
103 void PlacesItem::setGroupHidden(bool hidden)
104 {
105 setDataValue("isGroupHidden", hidden);
106 }
107
108 void PlacesItem::setSystemItem(bool isSystemItem)
109 {
110 setDataValue("isSystemItem", isSystemItem);
111 }
112
113 bool PlacesItem::isSystemItem() const
114 {
115 return dataValue("isSystemItem").toBool();
116 }
117
118 Solid::Device PlacesItem::device() const
119 {
120 return m_device;
121 }
122
123 void PlacesItem::setBookmark(const KBookmark& bookmark)
124 {
125 const bool bookmarkDataChanged = !(bookmark == m_bookmark);
126
127 // bookmark object must be updated to keep in sync with source model
128 m_bookmark = bookmark;
129
130 if (!bookmarkDataChanged) {
131 return;
132 }
133
134 delete m_access;
135 delete m_volume;
136 delete m_disc;
137 delete m_mtp;
138
139 const QString udi = bookmark.metaDataItem(QStringLiteral("UDI"));
140 if (udi.isEmpty()) {
141 setIcon(bookmark.icon());
142 setText(i18ndc("kio5", "KFile System Bookmarks", bookmark.text().toUtf8().constData()));
143 setUrl(bookmark.url());
144 setSystemItem(bookmark.metaDataItem(QStringLiteral("isSystemItem")) == QLatin1String("true"));
145 } else {
146 initializeDevice(udi);
147 }
148
149 setHidden(bookmark.metaDataItem(QStringLiteral("IsHidden")) == QLatin1String("true"));
150 }
151
152 KBookmark PlacesItem::bookmark() const
153 {
154 return m_bookmark;
155 }
156
157 bool PlacesItem::storageSetupNeeded() const
158 {
159 return m_access ? !m_access->isAccessible() : false;
160 }
161
162 bool PlacesItem::isSearchOrTimelineUrl() const
163 {
164 const QString urlScheme = url().scheme();
165 return (urlScheme.contains("search") || urlScheme.contains("timeline"));
166 }
167
168 void PlacesItem::onDataValueChanged(const QByteArray& role,
169 const QVariant& current,
170 const QVariant& previous)
171 {
172 Q_UNUSED(current);
173 Q_UNUSED(previous);
174
175 if (!m_bookmark.isNull()) {
176 updateBookmarkForRole(role);
177 }
178 }
179
180 void PlacesItem::onDataChanged(const QHash<QByteArray, QVariant>& current,
181 const QHash<QByteArray, QVariant>& previous)
182 {
183 Q_UNUSED(previous);
184
185 if (!m_bookmark.isNull()) {
186 QHashIterator<QByteArray, QVariant> it(current);
187 while (it.hasNext()) {
188 it.next();
189 updateBookmarkForRole(it.key());
190 }
191 }
192 }
193
194 void PlacesItem::initializeDevice(const QString& udi)
195 {
196 m_device = Solid::Device(udi);
197 if (!m_device.isValid()) {
198 return;
199 }
200
201 m_access = m_device.as<Solid::StorageAccess>();
202 m_volume = m_device.as<Solid::StorageVolume>();
203 m_disc = m_device.as<Solid::OpticalDisc>();
204 m_mtp = m_device.as<Solid::PortableMediaPlayer>();
205
206 setText(m_device.description());
207 setIcon(m_device.icon());
208 setIconOverlays(m_device.emblems());
209 setUdi(udi);
210
211 if (m_access) {
212 setUrl(QUrl::fromLocalFile(m_access->filePath()));
213 QObject::connect(m_access.data(), &Solid::StorageAccess::accessibilityChanged,
214 m_signalHandler.data(), &PlacesItemSignalHandler::onAccessibilityChanged);
215 QObject::connect(m_access.data(), &Solid::StorageAccess::teardownRequested,
216 m_signalHandler.data(), &PlacesItemSignalHandler::onTearDownRequested);
217 } else if (m_disc && (m_disc->availableContent() & Solid::OpticalDisc::Audio) != 0) {
218 Solid::Block *block = m_device.as<Solid::Block>();
219 if (block) {
220 const QString device = block->device();
221 setUrl(QUrl(QStringLiteral("audiocd:/?device=%1").arg(device)));
222 } else {
223 setUrl(QUrl(QStringLiteral("audiocd:/")));
224 }
225 } else if (m_mtp) {
226 setUrl(QUrl(QStringLiteral("mtp:udi=%1").arg(m_device.udi())));
227 }
228 }
229
230 void PlacesItem::onAccessibilityChanged()
231 {
232 setIconOverlays(m_device.emblems());
233 setUrl(QUrl::fromLocalFile(m_access->filePath()));
234 }
235
236 void PlacesItem::updateBookmarkForRole(const QByteArray& role)
237 {
238 Q_ASSERT(!m_bookmark.isNull());
239 if (role == "iconName") {
240 m_bookmark.setIcon(icon());
241 } else if (role == "text") {
242 // Only store the text in the KBookmark if it is not the translation of
243 // the current text. This makes sure that the text is re-translated if
244 // the user chooses another language, or the translation itself changes.
245 //
246 // NOTE: It is important to use "KFile System Bookmarks" as context
247 // (see PlacesItemModel::createSystemBookmarks()).
248 if (text() != i18ndc("kio5", "KFile System Bookmarks", m_bookmark.text().toUtf8().data())) {
249 m_bookmark.setFullText(text());
250 }
251 } else if (role == "url") {
252 m_bookmark.setUrl(url());
253 } else if (role == "udi") {
254 m_bookmark.setMetaDataItem(QStringLiteral("UDI"), udi());
255 } else if (role == "isSystemItem") {
256 m_bookmark.setMetaDataItem(QStringLiteral("isSystemItem"), isSystemItem() ? QStringLiteral("true") : QStringLiteral("false"));
257 } else if (role == "isHidden") {
258 m_bookmark.setMetaDataItem(QStringLiteral("IsHidden"), isHidden() ? QStringLiteral("true") : QStringLiteral("false"));
259 }
260 }
261
262 QString PlacesItem::generateNewId()
263 {
264 // The ID-generation must be different as done in KFilePlacesItem from kdelibs
265 // to prevent identical IDs, because 'count' is of course not shared. We append a
266 // " (V2)" to indicate that the ID has been generated by
267 // a new version of the places view.
268 static int count = 0;
269 return QString::number(QDateTime::currentDateTimeUtc().toTime_t()) +
270 '/' + QString::number(count++) + " (V2)";
271 }
272
273 PlacesItemSignalHandler *PlacesItem::signalHandler() const
274 {
275 return m_signalHandler.data();
276 }