1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
4 * Based on KFilePlacesModel from kdelibs: *
5 * Copyright (C) 2007 Kevin Ottens <ervin@kde.org> *
6 * Copyright (C) 2007 David Faure <faure@kde.org> *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
22 ***************************************************************************/
24 #include "placesitemmodel.h"
27 #include <KBookmarkGroup>
28 #include <KBookmarkManager>
29 #include <KComponentData>
33 #include <KStandardDirs>
35 #include "placesitem.h"
40 #include <Solid/Device>
41 #include <Solid/DeviceNotifier>
42 #include <Solid/OpticalDisc>
43 #include <Solid/OpticalDrive>
44 #include <Solid/StorageAccess>
45 #include <Solid/StorageDrive>
48 #include <Nepomuk/ResourceManager>
51 PlacesItemModel::PlacesItemModel(QObject
* parent
) :
52 KStandardItemModel(parent
),
53 m_nepomukRunning(false),
54 m_hiddenItemsShown(false),
59 m_systemBookmarksIndexes(),
61 m_hiddenItemToRemove(-1),
62 m_saveBookmarksTimer(0)
65 m_nepomukRunning
= (Nepomuk::ResourceManager::instance()->initialized());
67 const QString file
= KStandardDirs::locateLocal("data", "kfileplaces/bookmarks.xml");
68 m_bookmarkManager
= KBookmarkManager::managerForFile(file
, "kfilePlaces");
70 createSystemBookmarks();
71 initializeAvailableDevices();
74 m_saveBookmarksTimer
= new QTimer(this);
75 m_saveBookmarksTimer
->setInterval(100);
76 m_saveBookmarksTimer
->setSingleShot(true);
77 connect(m_saveBookmarksTimer
, SIGNAL(timeout()), this, SLOT(saveBookmarks()));
80 PlacesItemModel::~PlacesItemModel()
83 qDeleteAll(m_hiddenItems
);
84 m_hiddenItems
.clear();
87 PlacesItem
* PlacesItemModel::placesItem(int index
) const
89 return dynamic_cast<PlacesItem
*>(item(index
));
92 int PlacesItemModel::hiddenCount() const
96 foreach (const PlacesItem
* hiddenItem
, m_hiddenItems
) {
100 if (placesItem(modelIndex
)->isHidden()) {
110 void PlacesItemModel::setHiddenItemsShown(bool show
)
112 if (m_hiddenItemsShown
== show
) {
116 m_hiddenItemsShown
= show
;
119 // Move all items that are part of m_hiddenItems to the model.
121 for (int hiddenIndex
= 0; hiddenIndex
< m_hiddenItems
.count(); ++hiddenIndex
) {
122 if (m_hiddenItems
[hiddenIndex
]) {
123 PlacesItem
* visibleItem
= new PlacesItem(*m_hiddenItems
[hiddenIndex
]);
124 delete m_hiddenItems
[hiddenIndex
];
125 m_hiddenItems
.removeAt(hiddenIndex
);
126 insertItem(modelIndex
, visibleItem
);
127 Q_ASSERT(!m_hiddenItems
[hiddenIndex
]);
132 // Move all items of the model, where the "isHidden" property is true, to
134 Q_ASSERT(m_hiddenItems
.count() == count());
135 for (int i
= count() - 1; i
>= 0; --i
) {
136 PlacesItem
* visibleItem
= placesItem(i
);
137 if (visibleItem
->isHidden()) {
138 PlacesItem
* hiddenItem
= new PlacesItem(*visibleItem
);
140 m_hiddenItems
.insert(i
, hiddenItem
);
144 #ifdef PLACESITEMMODEL_DEBUG
145 kDebug() << "Changed visibility of hidden items";
150 bool PlacesItemModel::hiddenItemsShown() const
152 return m_hiddenItemsShown
;
155 int PlacesItemModel::closestItem(const KUrl
& url
) const
160 for (int i
= 0; i
< count(); ++i
) {
161 const KUrl itemUrl
= placesItem(i
)->url();
162 if (itemUrl
.isParentOf(url
)) {
163 const int length
= itemUrl
.prettyUrl().length();
164 if (length
> maxLength
) {
174 QString
PlacesItemModel::groupName(const KUrl
&url
) const
176 const QString protocol
= url
.protocol();
178 if (protocol
.contains(QLatin1String("search"))) {
179 return searchForGroupName();
182 if (protocol
== QLatin1String("timeline")) {
183 return recentlyAccessedGroupName();
186 return placesGroupName();
189 QAction
* PlacesItemModel::ejectAction(int index
) const
191 const PlacesItem
* item
= placesItem(index
);
192 if (item
&& item
->device().is
<Solid::OpticalDisc
>()) {
193 return new QAction(KIcon("media-eject"), i18nc("@item", "Eject '%1'", item
->text()), 0);
199 QAction
* PlacesItemModel::teardownAction(int index
) const
201 const PlacesItem
* item
= placesItem(index
);
206 Solid::Device device
= item
->device();
207 const bool providesTearDown
= device
.is
<Solid::StorageAccess
>() &&
208 device
.as
<Solid::StorageAccess
>()->isAccessible();
209 if (!providesTearDown
) {
213 Solid::StorageDrive
* drive
= device
.as
<Solid::StorageDrive
>();
215 drive
= device
.parent().as
<Solid::StorageDrive
>();
218 bool hotPluggable
= false;
219 bool removable
= false;
221 hotPluggable
= drive
->isHotpluggable();
222 removable
= drive
->isRemovable();
227 const QString label
= item
->text();
228 if (device
.is
<Solid::OpticalDisc
>()) {
229 text
= i18nc("@item", "Release '%1'", label
);
230 } else if (removable
|| hotPluggable
) {
231 text
= i18nc("@item", "Safely Remove '%1'", label
);
232 iconName
= "media-eject";
234 text
= i18nc("@item", "Unmount '%1'", label
);
235 iconName
= "media-eject";
238 if (iconName
.isEmpty()) {
239 return new QAction(text
, 0);
242 return new QAction(KIcon(iconName
), text
, 0);
245 void PlacesItemModel::requestEject(int index
)
247 const PlacesItem
* item
= placesItem(index
);
249 Solid::OpticalDrive
* drive
= item
->device().parent().as
<Solid::OpticalDrive
>();
251 connect(drive
, SIGNAL(ejectDone(Solid::ErrorType
,QVariant
,QString
)),
252 this, SLOT(slotStorageTeardownDone(Solid::ErrorType
,QVariant
)));
260 void PlacesItemModel::requestTeardown(int index
)
262 const PlacesItem
* item
= placesItem(index
);
264 Solid::StorageAccess
* access
= item
->device().as
<Solid::StorageAccess
>();
266 connect(access
, SIGNAL(teardownDone(Solid::ErrorType
,QVariant
,QString
)),
267 this, SLOT(slotStorageTeardownDone(Solid::ErrorType
,QVariant
)));
270 const QString label
= item
->text();
271 const QString message
= i18nc("@info", "The device '%1' is not a disk and cannot be ejected.", label
);
272 emit
errorMessage(message
);
277 void PlacesItemModel::onItemInserted(int index
)
279 if (index
== count() - 1) {
280 // The item has been appended as last item to the list. In this
281 // case assure that it is also appended after the hidden items and
282 // not before (like done otherwise).
283 m_hiddenItems
.append(0);
289 while (hiddenIndex
< m_hiddenItems
.count()) {
290 if (!m_hiddenItems
[hiddenIndex
]) {
292 if (modelIndex
+ 1 == index
) {
298 m_hiddenItems
.insert(hiddenIndex
, 0);
300 m_saveBookmarksTimer
->start();
302 #ifdef PLACESITEMMODEL_DEBUG
303 kDebug() << "Inserted item" << index
;
308 void PlacesItemModel::onItemRemoved(int index
)
310 const int removeIndex
= hiddenIndex(index
);
311 Q_ASSERT(!m_hiddenItems
[removeIndex
]);
312 m_hiddenItems
.removeAt(removeIndex
);
314 m_saveBookmarksTimer
->start();
316 #ifdef PLACESITEMMODEL_DEBUG
317 kDebug() << "Removed item" << index
;
322 void PlacesItemModel::onItemChanged(int index
, const QSet
<QByteArray
>& changedRoles
)
324 if (changedRoles
.contains("isHidden")) {
325 const PlacesItem
* shownItem
= placesItem(index
);
327 if (!m_hiddenItemsShown
&& shownItem
->isHidden()) {
328 m_hiddenItemToRemove
= index
;
329 QTimer::singleShot(0, this, SLOT(removeHiddenItem()));
332 m_saveBookmarksTimer
->start();
335 void PlacesItemModel::slotDeviceAdded(const QString
& udi
)
337 const Solid::Device
device(udi
);
338 if (m_predicate
.matches(device
)) {
339 m_availableDevices
<< udi
;
340 const KBookmark bookmark
= PlacesItem::createDeviceBookmark(m_bookmarkManager
, udi
);
341 appendItem(new PlacesItem(bookmark
));
345 void PlacesItemModel::slotDeviceRemoved(const QString
& udi
)
347 if (!m_availableDevices
.contains(udi
)) {
351 for (int i
= 0; i
< m_hiddenItems
.count(); ++i
) {
352 PlacesItem
* item
= m_hiddenItems
[i
];
353 if (item
&& item
->udi() == udi
) {
354 m_hiddenItems
.removeAt(i
);
360 for (int i
= 0; i
< count(); ++i
) {
361 if (placesItem(i
)->udi() == udi
) {
368 void PlacesItemModel::slotStorageTeardownDone(Solid::ErrorType error
, const QVariant
& errorData
)
370 if (error
&& errorData
.isValid()) {
371 emit
errorMessage(errorData
.toString());
375 void PlacesItemModel::removeHiddenItem()
377 const PlacesItem
* shownItem
= placesItem(m_hiddenItemToRemove
);
378 const int newIndex
= hiddenIndex(m_hiddenItemToRemove
);
379 if (shownItem
&& newIndex
>= 0) {
380 PlacesItem
* hiddenItem
= new PlacesItem(*shownItem
);
381 removeItem(m_hiddenItemToRemove
);
382 m_hiddenItems
.insert(newIndex
, hiddenItem
);
383 m_saveBookmarksTimer
->start();
385 m_hiddenItemToRemove
= -1;
389 void PlacesItemModel::saveBookmarks()
391 // TODO: Temporary deactivated until 100 % backward compatibility is provided
392 // m_bookmarkManager->emitChanged(m_bookmarkManager->root());
395 void PlacesItemModel::loadBookmarks()
397 KBookmarkGroup root
= m_bookmarkManager
->root();
398 KBookmark bookmark
= root
.first();
399 QSet
<QString
> devices
= m_availableDevices
;
401 QSet
<KUrl
> missingSystemBookmarks
;
402 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
403 missingSystemBookmarks
.insert(data
.url
);
406 // The bookmarks might have a mixed order of "places" and "devices". In
407 // Dolphin's places panel the devices should always be appended as last
408 // group, so they are collected as separate lists.
409 QList
<PlacesItem
*> placesItems
;
410 QList
<PlacesItem
*> devicesItems
;
412 while (!bookmark
.isNull()) {
413 const QString udi
= bookmark
.metaDataItem("UDI");
414 const KUrl url
= bookmark
.url();
415 const QString appName
= bookmark
.metaDataItem("OnlyInApp");
416 const bool deviceAvailable
= devices
.remove(udi
);
418 const bool allowedHere
= (appName
.isEmpty() || appName
== KGlobal::mainComponent().componentName())
419 && (m_nepomukRunning
|| (url
.protocol() != QLatin1String("timeline") &&
420 url
.protocol() != QLatin1String("search")));
422 if ((udi
.isEmpty() && allowedHere
) || deviceAvailable
) {
423 PlacesItem
* item
= new PlacesItem(bookmark
);
424 if (deviceAvailable
) {
425 devicesItems
.append(item
);
427 placesItems
.append(item
);
429 if (missingSystemBookmarks
.contains(url
)) {
430 missingSystemBookmarks
.remove(url
);
432 // Apply the translated text to the system bookmarks, otherwise an outdated
433 // translation might be shown.
434 const int index
= m_systemBookmarksIndexes
.value(url
);
435 item
->setText(m_systemBookmarks
[index
].text
);
436 item
->setSystemItem(true);
437 item
->setGroup(m_systemBookmarks
[index
].group
);
442 bookmark
= root
.next(bookmark
);
445 addItems(placesItems
);
447 if (!missingSystemBookmarks
.isEmpty()) {
448 // The current bookmarks don't contain all system-bookmarks. Add the missing
450 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
451 if (missingSystemBookmarks
.contains(data
.url
)) {
452 KBookmark bookmark
= PlacesItem::createBookmark(m_bookmarkManager
,
457 const QString protocol
= data
.url
.protocol();
458 if (protocol
== QLatin1String("timeline") || protocol
== QLatin1String("search")) {
459 // As long as the KFilePlacesView from kdelibs is available, the system-bookmarks
460 // for timeline and search should be a Dolphin-specific setting.
461 bookmark
.setMetaDataItem("OnlyInApp", KGlobal::mainComponent().componentName());
464 PlacesItem
* item
= new PlacesItem(bookmark
);
465 item
->setSystemItem(true);
466 item
->setGroup(data
.group
);
472 // Create items for devices that have not stored as bookmark yet
473 foreach (const QString
& udi
, devices
) {
474 const KBookmark bookmark
= PlacesItem::createDeviceBookmark(m_bookmarkManager
, udi
);
475 devicesItems
.append(new PlacesItem(bookmark
));
478 addItems(devicesItems
);
480 #ifdef PLACESITEMMODEL_DEBUG
481 kDebug() << "Loaded bookmarks";
486 void PlacesItemModel::addItems(const QList
<PlacesItem
*>& items
)
488 foreach (PlacesItem
* item
, items
) {
489 if (!m_hiddenItemsShown
&& item
->isHidden()) {
490 m_hiddenItems
.append(item
);
497 void PlacesItemModel::createSystemBookmarks()
499 Q_ASSERT(m_systemBookmarks
.isEmpty());
500 Q_ASSERT(m_systemBookmarksIndexes
.isEmpty());
502 const QString placesGroup
= placesGroupName();
503 const QString recentlyAccessedGroup
= recentlyAccessedGroupName();
504 const QString searchForGroup
= searchForGroupName();
505 const QString timeLineIcon
= "package_utility_time"; // TODO: Ask the Oxygen team to create
506 // a custom icon for the timeline-protocol
508 m_systemBookmarks
.append(SystemBookmarkData(KUrl(KUser().homeDir()),
510 i18nc("@item", "Home"),
512 m_systemBookmarks
.append(SystemBookmarkData(KUrl("remote:/"),
514 i18nc("@item", "Network"),
516 m_systemBookmarks
.append(SystemBookmarkData(KUrl("/"),
518 i18nc("@item", "Root"),
520 m_systemBookmarks
.append(SystemBookmarkData(KUrl("trash:/"),
522 i18nc("@item", "Trash"),
525 if (m_nepomukRunning
) {
526 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/today"),
528 i18nc("@item Recently Accessed", "Today"),
529 recentlyAccessedGroup
));
530 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/yesterday"),
532 i18nc("@item Recently Accessed", "Yesterday"),
533 recentlyAccessedGroup
));
534 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/thismonth"),
536 i18nc("@item Recently Accessed", "This Month"),
537 recentlyAccessedGroup
));
538 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/lastmonth"),
540 i18nc("@item Recently Accessed", "Last Month"),
541 recentlyAccessedGroup
));
542 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/documents"),
544 i18nc("@item Commonly Accessed", "Documents"),
546 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/images"),
548 i18nc("@item Commonly Accessed", "Images"),
550 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/audio"),
552 i18nc("@item Commonly Accessed", "Audio Files"),
554 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/videos"),
556 i18nc("@item Commonly Accessed", "Videos"),
560 for (int i
= 0; i
< m_systemBookmarks
.count(); ++i
) {
561 m_systemBookmarksIndexes
.insert(m_systemBookmarks
[i
].url
, i
);
565 void PlacesItemModel::initializeAvailableDevices()
567 m_predicate
= Solid::Predicate::fromString(
568 "[[[[ StorageVolume.ignored == false AND [ StorageVolume.usage == 'FileSystem' OR StorageVolume.usage == 'Encrypted' ]]"
570 "[ IS StorageAccess AND StorageDrive.driveType == 'Floppy' ]]"
572 "OpticalDisc.availableContent & 'Audio' ]"
574 "StorageAccess.ignored == false ]");
575 Q_ASSERT(m_predicate
.isValid());
577 Solid::DeviceNotifier
* notifier
= Solid::DeviceNotifier::instance();
578 connect(notifier
, SIGNAL(deviceAdded(QString
)), this, SLOT(slotDeviceAdded(QString
)));
579 connect(notifier
, SIGNAL(deviceRemoved(QString
)), this, SLOT(slotDeviceRemoved(QString
)));
581 const QList
<Solid::Device
>& deviceList
= Solid::Device::listFromQuery(m_predicate
);
582 foreach(const Solid::Device
& device
, deviceList
) {
583 m_availableDevices
<< device
.udi();
587 int PlacesItemModel::hiddenIndex(int index
) const
590 int visibleItemIndex
= 0;
591 while (hiddenIndex
< m_hiddenItems
.count()) {
592 if (!m_hiddenItems
[hiddenIndex
]) {
593 if (visibleItemIndex
== index
) {
601 return hiddenIndex
>= m_hiddenItems
.count() ? -1 : hiddenIndex
;
604 QString
PlacesItemModel::placesGroupName()
606 return i18nc("@item", "Places");
609 QString
PlacesItemModel::recentlyAccessedGroupName()
611 return i18nc("@item", "Recently Accessed");
614 QString
PlacesItemModel::searchForGroupName()
616 return i18nc("@item", "Search For");
619 #ifdef PLACESITEMMODEL_DEBUG
620 void PlacesItemModel::showModelState()
622 kDebug() << "hidden-index model-index text";
624 for (int i
= 0; i
< m_hiddenItems
.count(); ++i
) {
625 if (m_hiddenItems
[i
]) {
626 kDebug() << i
<< "(Hidden) " << " " << m_hiddenItems
[i
]->dataValue("text").toString();
629 kDebug() << i
<< " " << j
<< " " << item(j
)->dataValue("text").toString();
631 kDebug() << i
<< " " << j
<< " " << "(not available yet)";
639 #include "placesitemmodel.moc"