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