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"
26 #include "dolphin_generalsettings.h"
29 #include <KBookmarkGroup>
30 #include <KBookmarkManager>
31 #include <KComponentData>
34 #include <kprotocolinfo.h>
36 #include <KStandardDirs>
39 #include "placesitem.h"
45 #include <Solid/Device>
46 #include <Solid/DeviceNotifier>
47 #include <Solid/OpticalDisc>
48 #include <Solid/OpticalDrive>
49 #include <Solid/StorageAccess>
50 #include <Solid/StorageDrive>
52 #include <views/dolphinview.h>
53 #include <views/viewproperties.h>
56 #include <baloo/query.h>
57 #include <baloo/indexerconfig.h>
61 // As long as KFilePlacesView from kdelibs is available in parallel, the
62 // system-bookmarks for "Recently Accessed" and "Search For" should be
63 // shown only inside the Places Panel. This is necessary as the stored
64 // URLs needs to get translated to a Baloo-search-URL on-the-fly to
65 // be independent from changes in the Baloo-search-URL-syntax.
66 // Hence a prefix to the application-name of the stored bookmarks is
67 // added, which is only read by PlacesItemModel.
68 const char* AppNamePrefix
= "-places-panel";
71 PlacesItemModel::PlacesItemModel(QObject
* parent
) :
72 KStandardItemModel(parent
),
73 m_fileIndexingEnabled(false),
74 m_hiddenItemsShown(false),
79 m_systemBookmarksIndexes(),
81 m_hiddenItemToRemove(-1),
82 m_saveBookmarksTimer(0),
83 m_updateBookmarksTimer(0),
84 m_storageSetupInProgress()
87 Baloo::IndexerConfig config
;
88 m_fileIndexingEnabled
= config
.fileIndexingEnabled();
90 const QString file
= KStandardDirs::locateLocal("data", "kfileplaces/bookmarks.xml");
91 m_bookmarkManager
= KBookmarkManager::managerForFile(file
, "kfilePlaces");
93 createSystemBookmarks();
94 initializeAvailableDevices();
97 const int syncBookmarksTimeout
= 100;
99 m_saveBookmarksTimer
= new QTimer(this);
100 m_saveBookmarksTimer
->setInterval(syncBookmarksTimeout
);
101 m_saveBookmarksTimer
->setSingleShot(true);
102 connect(m_saveBookmarksTimer
, SIGNAL(timeout()), this, SLOT(saveBookmarks()));
104 m_updateBookmarksTimer
= new QTimer(this);
105 m_updateBookmarksTimer
->setInterval(syncBookmarksTimeout
);
106 m_updateBookmarksTimer
->setSingleShot(true);
107 connect(m_updateBookmarksTimer
, SIGNAL(timeout()), this, SLOT(updateBookmarks()));
109 connect(m_bookmarkManager
, SIGNAL(changed(QString
,QString
)),
110 m_updateBookmarksTimer
, SLOT(start()));
111 connect(m_bookmarkManager
, SIGNAL(bookmarksChanged(QString
)),
112 m_updateBookmarksTimer
, SLOT(start()));
115 PlacesItemModel::~PlacesItemModel()
118 qDeleteAll(m_bookmarkedItems
);
119 m_bookmarkedItems
.clear();
122 PlacesItem
* PlacesItemModel::createPlacesItem(const QString
& text
,
124 const QString
& iconName
)
126 const KBookmark bookmark
= PlacesItem::createBookmark(m_bookmarkManager
, text
, url
, iconName
);
127 return new PlacesItem(bookmark
);
130 PlacesItem
* PlacesItemModel::placesItem(int index
) const
132 return dynamic_cast<PlacesItem
*>(item(index
));
135 int PlacesItemModel::hiddenCount() const
138 int hiddenItemCount
= 0;
139 foreach (const PlacesItem
* item
, m_bookmarkedItems
) {
143 if (placesItem(modelIndex
)->isHidden()) {
150 return hiddenItemCount
;
153 void PlacesItemModel::setHiddenItemsShown(bool show
)
155 if (m_hiddenItemsShown
== show
) {
159 m_hiddenItemsShown
= show
;
162 // Move all items that are part of m_bookmarkedItems to the model.
163 QList
<PlacesItem
*> itemsToInsert
;
164 QList
<int> insertPos
;
166 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
167 if (m_bookmarkedItems
[i
]) {
168 itemsToInsert
.append(m_bookmarkedItems
[i
]);
169 m_bookmarkedItems
[i
] = 0;
170 insertPos
.append(modelIndex
);
175 // Inserting the items will automatically insert an item
176 // to m_bookmarkedItems in PlacesItemModel::onItemsInserted().
177 // The items are temporary saved in itemsToInsert, so
178 // m_bookmarkedItems can be shrinked now.
179 m_bookmarkedItems
.erase(m_bookmarkedItems
.begin(),
180 m_bookmarkedItems
.begin() + itemsToInsert
.count());
182 for (int i
= 0; i
< itemsToInsert
.count(); ++i
) {
183 insertItem(insertPos
[i
], itemsToInsert
[i
]);
186 Q_ASSERT(m_bookmarkedItems
.count() == count());
188 // Move all items of the model, where the "isHidden" property is true, to
189 // m_bookmarkedItems.
190 Q_ASSERT(m_bookmarkedItems
.count() == count());
191 for (int i
= count() - 1; i
>= 0; --i
) {
192 if (placesItem(i
)->isHidden()) {
198 #ifdef PLACESITEMMODEL_DEBUG
199 kDebug() << "Changed visibility of hidden items";
204 bool PlacesItemModel::hiddenItemsShown() const
206 return m_hiddenItemsShown
;
209 int PlacesItemModel::closestItem(const KUrl
& url
) const
214 for (int i
= 0; i
< count(); ++i
) {
215 const KUrl itemUrl
= placesItem(i
)->url();
216 if (itemUrl
.isParentOf(url
)) {
217 const int length
= itemUrl
.prettyUrl().length();
218 if (length
> maxLength
) {
228 void PlacesItemModel::appendItemToGroup(PlacesItem
* item
)
235 while (i
< count() && placesItem(i
)->group() != item
->group()) {
239 bool inserted
= false;
240 while (!inserted
&& i
< count()) {
241 if (placesItem(i
)->group() != item
->group()) {
254 QAction
* PlacesItemModel::ejectAction(int index
) const
256 const PlacesItem
* item
= placesItem(index
);
257 if (item
&& item
->device().is
<Solid::OpticalDisc
>()) {
258 return new QAction(KIcon("media-eject"), i18nc("@item", "Eject '%1'", item
->text()), 0);
264 QAction
* PlacesItemModel::teardownAction(int index
) const
266 const PlacesItem
* item
= placesItem(index
);
271 Solid::Device device
= item
->device();
272 const bool providesTearDown
= device
.is
<Solid::StorageAccess
>() &&
273 device
.as
<Solid::StorageAccess
>()->isAccessible();
274 if (!providesTearDown
) {
278 Solid::StorageDrive
* drive
= device
.as
<Solid::StorageDrive
>();
280 drive
= device
.parent().as
<Solid::StorageDrive
>();
283 bool hotPluggable
= false;
284 bool removable
= false;
286 hotPluggable
= drive
->isHotpluggable();
287 removable
= drive
->isRemovable();
292 const QString label
= item
->text();
293 if (device
.is
<Solid::OpticalDisc
>()) {
294 text
= i18nc("@item", "Release '%1'", label
);
295 } else if (removable
|| hotPluggable
) {
296 text
= i18nc("@item", "Safely Remove '%1'", label
);
297 iconName
= "media-eject";
299 text
= i18nc("@item", "Unmount '%1'", label
);
300 iconName
= "media-eject";
303 if (iconName
.isEmpty()) {
304 return new QAction(text
, 0);
307 return new QAction(KIcon(iconName
), text
, 0);
310 void PlacesItemModel::requestEject(int index
)
312 const PlacesItem
* item
= placesItem(index
);
314 Solid::OpticalDrive
* drive
= item
->device().parent().as
<Solid::OpticalDrive
>();
316 connect(drive
, SIGNAL(ejectDone(Solid::ErrorType
,QVariant
,QString
)),
317 this, SLOT(slotStorageTeardownDone(Solid::ErrorType
,QVariant
)));
320 const QString label
= item
->text();
321 const QString message
= i18nc("@info", "The device '%1' is not a disk and cannot be ejected.", label
);
322 emit
errorMessage(message
);
327 void PlacesItemModel::requestTeardown(int index
)
329 const PlacesItem
* item
= placesItem(index
);
331 Solid::StorageAccess
* access
= item
->device().as
<Solid::StorageAccess
>();
333 connect(access
, SIGNAL(teardownDone(Solid::ErrorType
,QVariant
,QString
)),
334 this, SLOT(slotStorageTeardownDone(Solid::ErrorType
,QVariant
)));
340 bool PlacesItemModel::storageSetupNeeded(int index
) const
342 const PlacesItem
* item
= placesItem(index
);
343 return item
? item
->storageSetupNeeded() : false;
346 void PlacesItemModel::requestStorageSetup(int index
)
348 const PlacesItem
* item
= placesItem(index
);
353 Solid::Device device
= item
->device();
354 const bool setup
= device
.is
<Solid::StorageAccess
>()
355 && !m_storageSetupInProgress
.contains(device
.as
<Solid::StorageAccess
>())
356 && !device
.as
<Solid::StorageAccess
>()->isAccessible();
358 Solid::StorageAccess
* access
= device
.as
<Solid::StorageAccess
>();
360 m_storageSetupInProgress
[access
] = index
;
362 connect(access
, SIGNAL(setupDone(Solid::ErrorType
,QVariant
,QString
)),
363 this, SLOT(slotStorageSetupDone(Solid::ErrorType
,QVariant
,QString
)));
369 QMimeData
* PlacesItemModel::createMimeData(const KItemSet
& indexes
) const
374 QDataStream
stream(&itemData
, QIODevice::WriteOnly
);
376 foreach (int index
, indexes
) {
377 const KUrl itemUrl
= placesItem(index
)->url();
378 if (itemUrl
.isValid()) {
384 QMimeData
* mimeData
= new QMimeData();
385 if (!urls
.isEmpty()) {
386 urls
.populateMimeData(mimeData
);
388 mimeData
->setData(internalMimeType(), itemData
);
393 bool PlacesItemModel::supportsDropping(int index
) const
395 return index
>= 0 && index
< count();
398 void PlacesItemModel::dropMimeDataBefore(int index
, const QMimeData
* mimeData
)
400 if (mimeData
->hasFormat(internalMimeType())) {
401 // The item has been moved inside the view
402 QByteArray itemData
= mimeData
->data(internalMimeType());
403 QDataStream
stream(&itemData
, QIODevice::ReadOnly
);
406 if (oldIndex
== index
|| oldIndex
== index
- 1) {
407 // No moving has been done
411 PlacesItem
* oldItem
= placesItem(oldIndex
);
416 PlacesItem
* newItem
= new PlacesItem(oldItem
->bookmark());
417 removeItem(oldIndex
);
419 if (oldIndex
< index
) {
423 const int dropIndex
= groupedDropIndex(index
, newItem
);
424 insertItem(dropIndex
, newItem
);
425 } else if (mimeData
->hasFormat("text/uri-list")) {
426 // One or more items must be added to the model
427 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
428 for (int i
= urls
.count() - 1; i
>= 0; --i
) {
429 const KUrl
& url
= urls
[i
];
431 QString text
= url
.fileName();
432 if (text
.isEmpty()) {
436 if (url
.isLocalFile() && !QFileInfo(url
.toLocalFile()).isDir()) {
437 // Only directories are allowed
441 PlacesItem
* newItem
= createPlacesItem(text
, url
);
442 const int dropIndex
= groupedDropIndex(index
, newItem
);
443 insertItem(dropIndex
, newItem
);
448 KUrl
PlacesItemModel::convertedUrl(const KUrl
& url
)
451 if (url
.protocol() == QLatin1String("timeline")) {
452 newUrl
= createTimelineUrl(url
);
453 } else if (url
.protocol() == QLatin1String("search")) {
454 newUrl
= createSearchUrl(url
);
460 void PlacesItemModel::onItemInserted(int index
)
462 const PlacesItem
* insertedItem
= placesItem(index
);
464 // Take care to apply the PlacesItemModel-order of the inserted item
465 // also to the bookmark-manager.
466 const KBookmark insertedBookmark
= insertedItem
->bookmark();
468 const PlacesItem
* previousItem
= placesItem(index
- 1);
469 KBookmark previousBookmark
;
471 previousBookmark
= previousItem
->bookmark();
474 m_bookmarkManager
->root().moveBookmark(insertedBookmark
, previousBookmark
);
477 if (index
== count() - 1) {
478 // The item has been appended as last item to the list. In this
479 // case assure that it is also appended after the hidden items and
480 // not before (like done otherwise).
481 m_bookmarkedItems
.append(0);
485 int bookmarkIndex
= 0;
486 while (bookmarkIndex
< m_bookmarkedItems
.count()) {
487 if (!m_bookmarkedItems
[bookmarkIndex
]) {
489 if (modelIndex
+ 1 == index
) {
495 m_bookmarkedItems
.insert(bookmarkIndex
, 0);
498 triggerBookmarksSaving();
500 #ifdef PLACESITEMMODEL_DEBUG
501 kDebug() << "Inserted item" << index
;
506 void PlacesItemModel::onItemRemoved(int index
, KStandardItem
* removedItem
)
508 PlacesItem
* placesItem
= dynamic_cast<PlacesItem
*>(removedItem
);
510 const KBookmark bookmark
= placesItem
->bookmark();
511 m_bookmarkManager
->root().deleteBookmark(bookmark
);
514 const int boomarkIndex
= bookmarkIndex(index
);
515 Q_ASSERT(!m_bookmarkedItems
[boomarkIndex
]);
516 m_bookmarkedItems
.removeAt(boomarkIndex
);
518 triggerBookmarksSaving();
520 #ifdef PLACESITEMMODEL_DEBUG
521 kDebug() << "Removed item" << index
;
526 void PlacesItemModel::onItemChanged(int index
, const QSet
<QByteArray
>& changedRoles
)
528 const PlacesItem
* changedItem
= placesItem(index
);
530 // Take care to apply the PlacesItemModel-order of the changed item
531 // also to the bookmark-manager.
532 const KBookmark insertedBookmark
= changedItem
->bookmark();
534 const PlacesItem
* previousItem
= placesItem(index
- 1);
535 KBookmark previousBookmark
;
537 previousBookmark
= previousItem
->bookmark();
540 m_bookmarkManager
->root().moveBookmark(insertedBookmark
, previousBookmark
);
543 if (changedRoles
.contains("isHidden")) {
544 if (!m_hiddenItemsShown
&& changedItem
->isHidden()) {
545 m_hiddenItemToRemove
= index
;
546 QTimer::singleShot(0, this, SLOT(hideItem()));
550 triggerBookmarksSaving();
553 void PlacesItemModel::slotDeviceAdded(const QString
& udi
)
555 const Solid::Device
device(udi
);
557 if (!m_predicate
.matches(device
)) {
561 m_availableDevices
<< udi
;
562 const KBookmark bookmark
= PlacesItem::createDeviceBookmark(m_bookmarkManager
, udi
);
563 appendItem(new PlacesItem(bookmark
));
566 void PlacesItemModel::slotDeviceRemoved(const QString
& udi
)
568 if (!m_availableDevices
.contains(udi
)) {
572 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
573 PlacesItem
* item
= m_bookmarkedItems
[i
];
574 if (item
&& item
->udi() == udi
) {
575 m_bookmarkedItems
.removeAt(i
);
581 for (int i
= 0; i
< count(); ++i
) {
582 if (placesItem(i
)->udi() == udi
) {
589 void PlacesItemModel::slotStorageTeardownDone(Solid::ErrorType error
, const QVariant
& errorData
)
591 if (error
&& errorData
.isValid()) {
592 emit
errorMessage(errorData
.toString());
596 void PlacesItemModel::slotStorageSetupDone(Solid::ErrorType error
,
597 const QVariant
& errorData
,
602 const int index
= m_storageSetupInProgress
.take(sender());
603 const PlacesItem
* item
= placesItem(index
);
609 if (errorData
.isValid()) {
610 emit
errorMessage(i18nc("@info", "An error occurred while accessing '%1', the system responded: %2",
612 errorData
.toString()));
614 emit
errorMessage(i18nc("@info", "An error occurred while accessing '%1'",
617 emit
storageSetupDone(index
, false);
619 emit
storageSetupDone(index
, true);
623 void PlacesItemModel::hideItem()
625 hideItem(m_hiddenItemToRemove
);
626 m_hiddenItemToRemove
= -1;
629 void PlacesItemModel::updateBookmarks()
631 // Verify whether new bookmarks have been added or existing
632 // bookmarks have been changed.
633 KBookmarkGroup root
= m_bookmarkManager
->root();
634 KBookmark newBookmark
= root
.first();
635 while (!newBookmark
.isNull()) {
636 if (acceptBookmark(newBookmark
, m_availableDevices
)) {
639 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
640 PlacesItem
* item
= m_bookmarkedItems
[i
];
642 item
= placesItem(modelIndex
);
646 const KBookmark oldBookmark
= item
->bookmark();
647 if (equalBookmarkIdentifiers(newBookmark
, oldBookmark
)) {
648 // The bookmark has been found in the model or as
649 // a hidden item. The content of the bookmark might
650 // have been changed, so an update is done.
652 if (newBookmark
.metaDataItem("UDI").isEmpty()) {
653 item
->setBookmark(newBookmark
);
660 const QString udi
= newBookmark
.metaDataItem("UDI");
664 * Only add a new places item, if the item text is not empty
665 * and if the device is available. Fixes the strange behaviour -
666 * add a places item without text in the Places section - when you
667 * remove a device (e.g. a usb stick) without unmounting.
669 if (udi
.isEmpty() || Solid::Device(udi
).isValid()) {
670 PlacesItem
* item
= new PlacesItem(newBookmark
);
671 if (item
->isHidden() && !m_hiddenItemsShown
) {
672 m_bookmarkedItems
.append(item
);
674 appendItemToGroup(item
);
680 newBookmark
= root
.next(newBookmark
);
683 // Remove items that are not part of the bookmark-manager anymore
685 for (int i
= m_bookmarkedItems
.count() - 1; i
>= 0; --i
) {
686 PlacesItem
* item
= m_bookmarkedItems
[i
];
687 const bool itemIsPartOfModel
= (item
== 0);
688 if (itemIsPartOfModel
) {
689 item
= placesItem(modelIndex
);
692 bool hasBeenRemoved
= true;
693 const KBookmark oldBookmark
= item
->bookmark();
694 KBookmark newBookmark
= root
.first();
695 while (!newBookmark
.isNull()) {
696 if (equalBookmarkIdentifiers(newBookmark
, oldBookmark
)) {
697 hasBeenRemoved
= false;
700 newBookmark
= root
.next(newBookmark
);
703 if (hasBeenRemoved
) {
704 if (m_bookmarkedItems
[i
]) {
705 delete m_bookmarkedItems
[i
];
706 m_bookmarkedItems
.removeAt(i
);
708 removeItem(modelIndex
);
713 if (itemIsPartOfModel
) {
719 void PlacesItemModel::saveBookmarks()
721 m_bookmarkManager
->emitChanged(m_bookmarkManager
->root());
724 void PlacesItemModel::loadBookmarks()
726 KBookmarkGroup root
= m_bookmarkManager
->root();
727 KBookmark bookmark
= root
.first();
728 QSet
<QString
> devices
= m_availableDevices
;
730 QSet
<KUrl
> missingSystemBookmarks
;
731 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
732 missingSystemBookmarks
.insert(data
.url
);
735 // The bookmarks might have a mixed order of places, devices and search-groups due
736 // to the compatibility with the KFilePlacesPanel. In Dolphin's places panel the
737 // items should always be collected in one group so the items are collected first
738 // in separate lists before inserting them.
739 QList
<PlacesItem
*> placesItems
;
740 QList
<PlacesItem
*> recentlyAccessedItems
;
741 QList
<PlacesItem
*> searchForItems
;
742 QList
<PlacesItem
*> devicesItems
;
744 while (!bookmark
.isNull()) {
745 if (acceptBookmark(bookmark
, devices
)) {
746 PlacesItem
* item
= new PlacesItem(bookmark
);
747 if (item
->groupType() == PlacesItem::DevicesType
) {
748 devices
.remove(item
->udi());
749 devicesItems
.append(item
);
751 const KUrl url
= bookmark
.url();
752 if (missingSystemBookmarks
.contains(url
)) {
753 missingSystemBookmarks
.remove(url
);
755 // Try to retranslate the text of system bookmarks to have translated
756 // items when changing the language. In case if the user has applied a custom
757 // text, the retranslation will fail and the users custom text is still used.
758 // It is important to use "KFile System Bookmarks" as context (see
759 // createSystemBookmarks()).
760 item
->setText(i18nc("KFile System Bookmarks", bookmark
.text().toUtf8().data()));
761 item
->setSystemItem(true);
764 switch (item
->groupType()) {
765 case PlacesItem::PlacesType
: placesItems
.append(item
); break;
766 case PlacesItem::RecentlyAccessedType
: recentlyAccessedItems
.append(item
); break;
767 case PlacesItem::SearchForType
: searchForItems
.append(item
); break;
768 case PlacesItem::DevicesType
:
769 default: Q_ASSERT(false); break;
774 bookmark
= root
.next(bookmark
);
777 if (!missingSystemBookmarks
.isEmpty()) {
778 // The current bookmarks don't contain all system-bookmarks. Add the missing
780 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
781 if (missingSystemBookmarks
.contains(data
.url
)) {
782 PlacesItem
* item
= createSystemPlacesItem(data
);
783 switch (item
->groupType()) {
784 case PlacesItem::PlacesType
: placesItems
.append(item
); break;
785 case PlacesItem::RecentlyAccessedType
: recentlyAccessedItems
.append(item
); break;
786 case PlacesItem::SearchForType
: searchForItems
.append(item
); break;
787 case PlacesItem::DevicesType
:
788 default: Q_ASSERT(false); break;
794 // Create items for devices that have not been stored as bookmark yet
795 foreach (const QString
& udi
, devices
) {
796 const KBookmark bookmark
= PlacesItem::createDeviceBookmark(m_bookmarkManager
, udi
);
797 devicesItems
.append(new PlacesItem(bookmark
));
800 QList
<PlacesItem
*> items
;
801 items
.append(placesItems
);
802 items
.append(recentlyAccessedItems
);
803 items
.append(searchForItems
);
804 items
.append(devicesItems
);
806 foreach (PlacesItem
* item
, items
) {
807 if (!m_hiddenItemsShown
&& item
->isHidden()) {
808 m_bookmarkedItems
.append(item
);
814 #ifdef PLACESITEMMODEL_DEBUG
815 kDebug() << "Loaded bookmarks";
820 bool PlacesItemModel::acceptBookmark(const KBookmark
& bookmark
,
821 const QSet
<QString
>& availableDevices
) const
823 const QString udi
= bookmark
.metaDataItem("UDI");
824 const KUrl url
= bookmark
.url();
825 const QString appName
= bookmark
.metaDataItem("OnlyInApp");
826 const bool deviceAvailable
= availableDevices
.contains(udi
);
828 const bool allowedHere
= (appName
.isEmpty()
829 || appName
== KGlobal::mainComponent().componentName()
830 || appName
== KGlobal::mainComponent().componentName() + AppNamePrefix
)
831 && (m_fileIndexingEnabled
|| (url
.protocol() != QLatin1String("timeline") &&
832 url
.protocol() != QLatin1String("search")));
834 return (udi
.isEmpty() && allowedHere
) || deviceAvailable
;
837 PlacesItem
* PlacesItemModel::createSystemPlacesItem(const SystemBookmarkData
& data
)
839 KBookmark bookmark
= PlacesItem::createBookmark(m_bookmarkManager
,
844 const QString protocol
= data
.url
.protocol();
845 if (protocol
== QLatin1String("timeline") || protocol
== QLatin1String("search")) {
846 // As long as the KFilePlacesView from kdelibs is available, the system-bookmarks
847 // for "Recently Accessed" and "Search For" should be a setting available only
848 // in the Places Panel (see description of AppNamePrefix for more details).
849 const QString appName
= KGlobal::mainComponent().componentName() + AppNamePrefix
;
850 bookmark
.setMetaDataItem("OnlyInApp", appName
);
853 PlacesItem
* item
= new PlacesItem(bookmark
);
854 item
->setSystemItem(true);
856 // Create default view-properties for all "Search For" and "Recently Accessed" bookmarks
857 // in case if the user has not already created custom view-properties for a corresponding
859 const bool createDefaultViewProperties
= (item
->groupType() == PlacesItem::SearchForType
||
860 item
->groupType() == PlacesItem::RecentlyAccessedType
) &&
861 !GeneralSettings::self()->globalViewProps();
862 if (createDefaultViewProperties
) {
863 ViewProperties
props(convertedUrl(data
.url
));
864 if (!props
.exist()) {
865 const QString path
= data
.url
.path();
866 if (path
== QLatin1String("/documents")) {
867 props
.setViewMode(DolphinView::DetailsView
);
868 props
.setPreviewsShown(false);
869 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "path");
870 } else if (path
== QLatin1String("/images")) {
871 props
.setViewMode(DolphinView::IconsView
);
872 props
.setPreviewsShown(true);
873 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "imageSize");
874 } else if (path
== QLatin1String("/audio")) {
875 props
.setViewMode(DolphinView::DetailsView
);
876 props
.setPreviewsShown(false);
877 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "artist" << "album");
878 } else if (path
== QLatin1String("/videos")) {
879 props
.setViewMode(DolphinView::IconsView
);
880 props
.setPreviewsShown(true);
881 props
.setVisibleRoles(QList
<QByteArray
>() << "text");
882 } else if (data
.url
.protocol() == "timeline") {
883 props
.setViewMode(DolphinView::DetailsView
);
884 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "date");
892 void PlacesItemModel::createSystemBookmarks()
894 Q_ASSERT(m_systemBookmarks
.isEmpty());
895 Q_ASSERT(m_systemBookmarksIndexes
.isEmpty());
897 // Note: The context of the I18N_NOOP2 must be "KFile System Bookmarks". The real
898 // i18nc call is done after reading the bookmark. The reason why the i18nc call is not
899 // done here is because otherwise switching the language would not result in retranslating the
901 m_systemBookmarks
.append(SystemBookmarkData(KUrl(KUser().homeDir()),
903 I18N_NOOP2("KFile System Bookmarks", "Home")));
904 m_systemBookmarks
.append(SystemBookmarkData(KUrl("remote:/"),
906 I18N_NOOP2("KFile System Bookmarks", "Network")));
907 m_systemBookmarks
.append(SystemBookmarkData(KUrl("/"),
909 I18N_NOOP2("KFile System Bookmarks", "Root")));
910 m_systemBookmarks
.append(SystemBookmarkData(KUrl("trash:/"),
912 I18N_NOOP2("KFile System Bookmarks", "Trash")));
914 if (m_fileIndexingEnabled
) {
915 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/today"),
917 I18N_NOOP2("KFile System Bookmarks", "Today")));
918 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/yesterday"),
920 I18N_NOOP2("KFile System Bookmarks", "Yesterday")));
921 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/thismonth"),
922 "view-calendar-month",
923 I18N_NOOP2("KFile System Bookmarks", "This Month")));
924 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/lastmonth"),
925 "view-calendar-month",
926 I18N_NOOP2("KFile System Bookmarks", "Last Month")));
927 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/documents"),
929 I18N_NOOP2("KFile System Bookmarks", "Documents")));
930 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/images"),
932 I18N_NOOP2("KFile System Bookmarks", "Images")));
933 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/audio"),
935 I18N_NOOP2("KFile System Bookmarks", "Audio Files")));
936 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/videos"),
938 I18N_NOOP2("KFile System Bookmarks", "Videos")));
941 for (int i
= 0; i
< m_systemBookmarks
.count(); ++i
) {
942 m_systemBookmarksIndexes
.insert(m_systemBookmarks
[i
].url
, i
);
946 void PlacesItemModel::clear() {
947 m_bookmarkedItems
.clear();
948 KStandardItemModel::clear();
951 void PlacesItemModel::initializeAvailableDevices()
953 QString
predicate("[[[[ StorageVolume.ignored == false AND [ StorageVolume.usage == 'FileSystem' OR StorageVolume.usage == 'Encrypted' ]]"
955 "[ IS StorageAccess AND StorageDrive.driveType == 'Floppy' ]]"
957 "OpticalDisc.availableContent & 'Audio' ]"
959 "StorageAccess.ignored == false ]");
962 if (KProtocolInfo::isKnownProtocol("mtp")) {
963 predicate
.prepend("[");
964 predicate
.append(" OR PortableMediaPlayer.supportedProtocols == 'mtp']");
967 m_predicate
= Solid::Predicate::fromString(predicate
);
968 Q_ASSERT(m_predicate
.isValid());
970 Solid::DeviceNotifier
* notifier
= Solid::DeviceNotifier::instance();
971 connect(notifier
, SIGNAL(deviceAdded(QString
)), this, SLOT(slotDeviceAdded(QString
)));
972 connect(notifier
, SIGNAL(deviceRemoved(QString
)), this, SLOT(slotDeviceRemoved(QString
)));
974 const QList
<Solid::Device
>& deviceList
= Solid::Device::listFromQuery(m_predicate
);
975 foreach (const Solid::Device
& device
, deviceList
) {
976 m_availableDevices
<< device
.udi();
980 int PlacesItemModel::bookmarkIndex(int index
) const
982 int bookmarkIndex
= 0;
984 while (bookmarkIndex
< m_bookmarkedItems
.count()) {
985 if (!m_bookmarkedItems
[bookmarkIndex
]) {
986 if (modelIndex
== index
) {
994 return bookmarkIndex
>= m_bookmarkedItems
.count() ? -1 : bookmarkIndex
;
997 void PlacesItemModel::hideItem(int index
)
999 PlacesItem
* shownItem
= placesItem(index
);
1004 shownItem
->setHidden(true);
1005 if (m_hiddenItemsShown
) {
1006 // Removing items from the model is not allowed if all hidden
1007 // items should be shown.
1011 const int newIndex
= bookmarkIndex(index
);
1012 if (newIndex
>= 0) {
1013 const KBookmark hiddenBookmark
= shownItem
->bookmark();
1014 PlacesItem
* hiddenItem
= new PlacesItem(hiddenBookmark
);
1016 const PlacesItem
* previousItem
= placesItem(index
- 1);
1017 KBookmark previousBookmark
;
1019 previousBookmark
= previousItem
->bookmark();
1022 const bool updateBookmark
= (m_bookmarkManager
->root().indexOf(hiddenBookmark
) >= 0);
1025 if (updateBookmark
) {
1026 // removeItem() also removed the bookmark from m_bookmarkManager in
1027 // PlacesItemModel::onItemRemoved(). However for hidden items the
1028 // bookmark should still be remembered, so readd it again:
1029 m_bookmarkManager
->root().addBookmark(hiddenBookmark
);
1030 m_bookmarkManager
->root().moveBookmark(hiddenBookmark
, previousBookmark
);
1031 triggerBookmarksSaving();
1034 m_bookmarkedItems
.insert(newIndex
, hiddenItem
);
1038 void PlacesItemModel::triggerBookmarksSaving()
1040 if (m_saveBookmarksTimer
) {
1041 m_saveBookmarksTimer
->start();
1045 QString
PlacesItemModel::internalMimeType() const
1047 return "application/x-dolphinplacesmodel-" +
1048 QString::number((qptrdiff
)this);
1051 int PlacesItemModel::groupedDropIndex(int index
, const PlacesItem
* item
) const
1055 int dropIndex
= index
;
1056 const PlacesItem::GroupType type
= item
->groupType();
1058 const int itemCount
= count();
1060 dropIndex
= itemCount
;
1063 // Search nearest previous item with the same group
1064 int previousIndex
= -1;
1065 for (int i
= dropIndex
- 1; i
>= 0; --i
) {
1066 if (placesItem(i
)->groupType() == type
) {
1072 // Search nearest next item with the same group
1074 for (int i
= dropIndex
; i
< count(); ++i
) {
1075 if (placesItem(i
)->groupType() == type
) {
1081 // Adjust the drop-index to be inserted to the
1082 // nearest item with the same group.
1083 if (previousIndex
>= 0 && nextIndex
>= 0) {
1084 dropIndex
= (dropIndex
- previousIndex
< nextIndex
- dropIndex
) ?
1085 previousIndex
+ 1 : nextIndex
;
1086 } else if (previousIndex
>= 0) {
1087 dropIndex
= previousIndex
+ 1;
1088 } else if (nextIndex
>= 0) {
1089 dropIndex
= nextIndex
;
1095 bool PlacesItemModel::equalBookmarkIdentifiers(const KBookmark
& b1
, const KBookmark
& b2
)
1097 const QString udi1
= b1
.metaDataItem("UDI");
1098 const QString udi2
= b2
.metaDataItem("UDI");
1099 if (!udi1
.isEmpty() && !udi2
.isEmpty()) {
1100 return udi1
== udi2
;
1102 return b1
.metaDataItem("ID") == b2
.metaDataItem("ID");
1106 KUrl
PlacesItemModel::createTimelineUrl(const KUrl
& url
)
1108 // TODO: Clarify with the Baloo-team whether it makes sense
1109 // provide default-timeline-URLs like 'yesterday', 'this month'
1110 // and 'last month'.
1113 const QString path
= url
.pathOrUrl();
1114 if (path
.endsWith(QLatin1String("yesterday"))) {
1115 const QDate date
= QDate::currentDate().addDays(-1);
1116 const int year
= date
.year();
1117 const int month
= date
.month();
1118 const int day
= date
.day();
1119 timelineUrl
= "timeline:/" + timelineDateString(year
, month
) +
1120 '/' + timelineDateString(year
, month
, day
);
1121 } else if (path
.endsWith(QLatin1String("thismonth"))) {
1122 const QDate date
= QDate::currentDate();
1123 timelineUrl
= "timeline:/" + timelineDateString(date
.year(), date
.month());
1124 } else if (path
.endsWith(QLatin1String("lastmonth"))) {
1125 const QDate date
= QDate::currentDate().addMonths(-1);
1126 timelineUrl
= "timeline:/" + timelineDateString(date
.year(), date
.month());
1128 Q_ASSERT(path
.endsWith(QLatin1String("today")));
1135 QString
PlacesItemModel::timelineDateString(int year
, int month
, int day
)
1137 QString date
= QString::number(year
) + '-';
1141 date
+= QString::number(month
);
1148 date
+= QString::number(day
);
1154 KUrl
PlacesItemModel::createSearchUrl(const KUrl
& url
)
1159 const QString path
= url
.pathOrUrl();
1160 if (path
.endsWith(QLatin1String("documents"))) {
1161 searchUrl
= searchUrlForType("Document");
1162 } else if (path
.endsWith(QLatin1String("images"))) {
1163 searchUrl
= searchUrlForType("Image");
1164 } else if (path
.endsWith(QLatin1String("audio"))) {
1165 searchUrl
= searchUrlForType("Audio");
1166 } else if (path
.endsWith(QLatin1String("videos"))) {
1167 searchUrl
= searchUrlForType("Video");
1179 KUrl
PlacesItemModel::searchUrlForType(const QString
& type
)
1182 query
.addType("File");
1183 query
.addType(type
);
1185 return query
.toSearchUrl();
1189 #ifdef PLACESITEMMODEL_DEBUG
1190 void PlacesItemModel::showModelState()
1192 kDebug() << "=================================";
1193 kDebug() << "Model:";
1194 kDebug() << "hidden-index model-index text";
1196 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
1197 if (m_bookmarkedItems
[i
]) {
1198 kDebug() << i
<< "(Hidden) " << " " << m_bookmarkedItems
[i
]->dataValue("text").toString();
1200 if (item(modelIndex
)) {
1201 kDebug() << i
<< " " << modelIndex
<< " " << item(modelIndex
)->dataValue("text").toString();
1203 kDebug() << i
<< " " << modelIndex
<< " " << "(not available yet)";
1210 kDebug() << "Bookmarks:";
1212 int bookmarkIndex
= 0;
1213 KBookmarkGroup root
= m_bookmarkManager
->root();
1214 KBookmark bookmark
= root
.first();
1215 while (!bookmark
.isNull()) {
1216 const QString udi
= bookmark
.metaDataItem("UDI");
1217 const QString text
= udi
.isEmpty() ? bookmark
.text() : udi
;
1218 if (bookmark
.metaDataItem("IsHidden") == QLatin1String("true")) {
1219 kDebug() << bookmarkIndex
<< "(Hidden)" << text
;
1221 kDebug() << bookmarkIndex
<< " " << text
;
1224 bookmark
= root
.next(bookmark
);
1230 #include "placesitemmodel.moc"