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