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>
38 #include "placesitem.h"
44 #include <Solid/Device>
45 #include <Solid/DeviceNotifier>
46 #include <Solid/OpticalDisc>
47 #include <Solid/OpticalDrive>
48 #include <Solid/StorageAccess>
49 #include <Solid/StorageDrive>
51 #include <views/dolphinview.h>
52 #include <views/viewproperties.h>
55 #include <baloo/query.h>
56 #include <baloo/indexerconfig.h>
60 // As long as KFilePlacesView from kdelibs is available in parallel, the
61 // system-bookmarks for "Recently Accessed" and "Search For" should be
62 // shown only inside the Places Panel. This is necessary as the stored
63 // URLs needs to get translated to a Baloo-search-URL on-the-fly to
64 // be independent from changes in the Baloo-search-URL-syntax.
65 // Hence a prefix to the application-name of the stored bookmarks is
66 // added, which is only read by PlacesItemModel.
67 const char* AppNamePrefix
= "-places-panel";
70 PlacesItemModel::PlacesItemModel(QObject
* parent
) :
71 KStandardItemModel(parent
),
72 m_fileIndexingEnabled(false),
73 m_hiddenItemsShown(false),
78 m_systemBookmarksIndexes(),
80 m_hiddenItemToRemove(-1),
81 m_saveBookmarksTimer(0),
82 m_updateBookmarksTimer(0),
83 m_storageSetupInProgress()
86 Baloo::IndexerConfig config
;
87 m_fileIndexingEnabled
= config
.fileIndexingEnabled();
89 const QString file
= KStandardDirs::locateLocal("data", "kfileplaces/bookmarks.xml");
90 m_bookmarkManager
= KBookmarkManager::managerForFile(file
, "kfilePlaces");
92 createSystemBookmarks();
93 initializeAvailableDevices();
96 const int syncBookmarksTimeout
= 100;
98 m_saveBookmarksTimer
= new QTimer(this);
99 m_saveBookmarksTimer
->setInterval(syncBookmarksTimeout
);
100 m_saveBookmarksTimer
->setSingleShot(true);
101 connect(m_saveBookmarksTimer
, SIGNAL(timeout()), this, SLOT(saveBookmarks()));
103 m_updateBookmarksTimer
= new QTimer(this);
104 m_updateBookmarksTimer
->setInterval(syncBookmarksTimeout
);
105 m_updateBookmarksTimer
->setSingleShot(true);
106 connect(m_updateBookmarksTimer
, SIGNAL(timeout()), this, SLOT(updateBookmarks()));
108 connect(m_bookmarkManager
, SIGNAL(changed(QString
,QString
)),
109 m_updateBookmarksTimer
, SLOT(start()));
110 connect(m_bookmarkManager
, SIGNAL(bookmarksChanged(QString
)),
111 m_updateBookmarksTimer
, SLOT(start()));
114 PlacesItemModel::~PlacesItemModel()
117 qDeleteAll(m_bookmarkedItems
);
118 m_bookmarkedItems
.clear();
121 PlacesItem
* PlacesItemModel::createPlacesItem(const QString
& text
,
123 const QString
& iconName
)
125 const KBookmark bookmark
= PlacesItem::createBookmark(m_bookmarkManager
, text
, url
, iconName
);
126 return new PlacesItem(bookmark
);
129 PlacesItem
* PlacesItemModel::placesItem(int index
) const
131 return dynamic_cast<PlacesItem
*>(item(index
));
134 int PlacesItemModel::hiddenCount() const
137 int hiddenItemCount
= 0;
138 foreach (const PlacesItem
* item
, m_bookmarkedItems
) {
142 if (placesItem(modelIndex
)->isHidden()) {
149 return hiddenItemCount
;
152 void PlacesItemModel::setHiddenItemsShown(bool show
)
154 if (m_hiddenItemsShown
== show
) {
158 m_hiddenItemsShown
= show
;
161 // Move all items that are part of m_bookmarkedItems to the model.
162 QList
<PlacesItem
*> itemsToInsert
;
163 QList
<int> insertPos
;
165 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
166 if (m_bookmarkedItems
[i
]) {
167 itemsToInsert
.append(m_bookmarkedItems
[i
]);
168 m_bookmarkedItems
[i
] = 0;
169 insertPos
.append(modelIndex
);
174 // Inserting the items will automatically insert an item
175 // to m_bookmarkedItems in PlacesItemModel::onItemsInserted().
176 // The items are temporary saved in itemsToInsert, so
177 // m_bookmarkedItems can be shrinked now.
178 m_bookmarkedItems
.erase(m_bookmarkedItems
.begin(),
179 m_bookmarkedItems
.begin() + itemsToInsert
.count());
181 for (int i
= 0; i
< itemsToInsert
.count(); ++i
) {
182 insertItem(insertPos
[i
], itemsToInsert
[i
]);
185 Q_ASSERT(m_bookmarkedItems
.count() == count());
187 // Move all items of the model, where the "isHidden" property is true, to
188 // m_bookmarkedItems.
189 Q_ASSERT(m_bookmarkedItems
.count() == count());
190 for (int i
= count() - 1; i
>= 0; --i
) {
191 if (placesItem(i
)->isHidden()) {
197 #ifdef PLACESITEMMODEL_DEBUG
198 kDebug() << "Changed visibility of hidden items";
203 bool PlacesItemModel::hiddenItemsShown() const
205 return m_hiddenItemsShown
;
208 int PlacesItemModel::closestItem(const KUrl
& url
) const
213 for (int i
= 0; i
< count(); ++i
) {
214 const KUrl itemUrl
= placesItem(i
)->url();
215 if (itemUrl
.isParentOf(url
)) {
216 const int length
= itemUrl
.prettyUrl().length();
217 if (length
> maxLength
) {
227 void PlacesItemModel::appendItemToGroup(PlacesItem
* item
)
234 while (i
< count() && placesItem(i
)->group() != item
->group()) {
238 bool inserted
= false;
239 while (!inserted
&& i
< count()) {
240 if (placesItem(i
)->group() != item
->group()) {
253 QAction
* PlacesItemModel::ejectAction(int index
) const
255 const PlacesItem
* item
= placesItem(index
);
256 if (item
&& item
->device().is
<Solid::OpticalDisc
>()) {
257 return new QAction(KIcon("media-eject"), i18nc("@item", "Eject '%1'", item
->text()), 0);
263 QAction
* PlacesItemModel::teardownAction(int index
) const
265 const PlacesItem
* item
= placesItem(index
);
270 Solid::Device device
= item
->device();
271 const bool providesTearDown
= device
.is
<Solid::StorageAccess
>() &&
272 device
.as
<Solid::StorageAccess
>()->isAccessible();
273 if (!providesTearDown
) {
277 Solid::StorageDrive
* drive
= device
.as
<Solid::StorageDrive
>();
279 drive
= device
.parent().as
<Solid::StorageDrive
>();
282 bool hotPluggable
= false;
283 bool removable
= false;
285 hotPluggable
= drive
->isHotpluggable();
286 removable
= drive
->isRemovable();
291 const QString label
= item
->text();
292 if (device
.is
<Solid::OpticalDisc
>()) {
293 text
= i18nc("@item", "Release '%1'", label
);
294 } else if (removable
|| hotPluggable
) {
295 text
= i18nc("@item", "Safely Remove '%1'", label
);
296 iconName
= "media-eject";
298 text
= i18nc("@item", "Unmount '%1'", label
);
299 iconName
= "media-eject";
302 if (iconName
.isEmpty()) {
303 return new QAction(text
, 0);
306 return new QAction(KIcon(iconName
), text
, 0);
309 void PlacesItemModel::requestEject(int index
)
311 const PlacesItem
* item
= placesItem(index
);
313 Solid::OpticalDrive
* drive
= item
->device().parent().as
<Solid::OpticalDrive
>();
315 connect(drive
, SIGNAL(ejectDone(Solid::ErrorType
,QVariant
,QString
)),
316 this, SLOT(slotStorageTeardownDone(Solid::ErrorType
,QVariant
)));
319 const QString label
= item
->text();
320 const QString message
= i18nc("@info", "The device '%1' is not a disk and cannot be ejected.", label
);
321 emit
errorMessage(message
);
326 void PlacesItemModel::requestTeardown(int index
)
328 const PlacesItem
* item
= placesItem(index
);
330 Solid::StorageAccess
* access
= item
->device().as
<Solid::StorageAccess
>();
332 connect(access
, SIGNAL(teardownDone(Solid::ErrorType
,QVariant
,QString
)),
333 this, SLOT(slotStorageTeardownDone(Solid::ErrorType
,QVariant
)));
339 bool PlacesItemModel::storageSetupNeeded(int index
) const
341 const PlacesItem
* item
= placesItem(index
);
342 return item
? item
->storageSetupNeeded() : false;
345 void PlacesItemModel::requestStorageSetup(int index
)
347 const PlacesItem
* item
= placesItem(index
);
352 Solid::Device device
= item
->device();
353 const bool setup
= device
.is
<Solid::StorageAccess
>()
354 && !m_storageSetupInProgress
.contains(device
.as
<Solid::StorageAccess
>())
355 && !device
.as
<Solid::StorageAccess
>()->isAccessible();
357 Solid::StorageAccess
* access
= device
.as
<Solid::StorageAccess
>();
359 m_storageSetupInProgress
[access
] = index
;
361 connect(access
, SIGNAL(setupDone(Solid::ErrorType
,QVariant
,QString
)),
362 this, SLOT(slotStorageSetupDone(Solid::ErrorType
,QVariant
,QString
)));
368 QMimeData
* PlacesItemModel::createMimeData(const KItemSet
& indexes
) const
373 QDataStream
stream(&itemData
, QIODevice::WriteOnly
);
375 foreach (int index
, indexes
) {
376 const KUrl itemUrl
= placesItem(index
)->url();
377 if (itemUrl
.isValid()) {
383 QMimeData
* mimeData
= new QMimeData();
384 if (!urls
.isEmpty()) {
385 urls
.populateMimeData(mimeData
);
387 mimeData
->setData(internalMimeType(), itemData
);
392 bool PlacesItemModel::supportsDropping(int index
) const
394 return index
>= 0 && index
< count();
397 void PlacesItemModel::dropMimeDataBefore(int index
, const QMimeData
* mimeData
)
399 if (mimeData
->hasFormat(internalMimeType())) {
400 // The item has been moved inside the view
401 QByteArray itemData
= mimeData
->data(internalMimeType());
402 QDataStream
stream(&itemData
, QIODevice::ReadOnly
);
405 if (oldIndex
== index
|| oldIndex
== index
- 1) {
406 // No moving has been done
410 PlacesItem
* oldItem
= placesItem(oldIndex
);
415 PlacesItem
* newItem
= new PlacesItem(oldItem
->bookmark());
416 removeItem(oldIndex
);
418 if (oldIndex
< index
) {
422 const int dropIndex
= groupedDropIndex(index
, newItem
);
423 insertItem(dropIndex
, newItem
);
424 } else if (mimeData
->hasFormat("text/uri-list")) {
425 // One or more items must be added to the model
426 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
427 for (int i
= urls
.count() - 1; i
>= 0; --i
) {
428 const KUrl
& url
= urls
[i
];
430 QString text
= url
.fileName();
431 if (text
.isEmpty()) {
435 if (url
.isLocalFile() && !QFileInfo(url
.toLocalFile()).isDir()) {
436 // Only directories are allowed
440 PlacesItem
* newItem
= createPlacesItem(text
, url
);
441 const int dropIndex
= groupedDropIndex(index
, newItem
);
442 insertItem(dropIndex
, newItem
);
447 KUrl
PlacesItemModel::convertedUrl(const KUrl
& url
)
450 if (url
.protocol() == QLatin1String("timeline")) {
451 newUrl
= createTimelineUrl(url
);
452 } else if (url
.protocol() == QLatin1String("search")) {
453 newUrl
= createSearchUrl(url
);
459 void PlacesItemModel::onItemInserted(int index
)
461 const PlacesItem
* insertedItem
= placesItem(index
);
463 // Take care to apply the PlacesItemModel-order of the inserted item
464 // also to the bookmark-manager.
465 const KBookmark insertedBookmark
= insertedItem
->bookmark();
467 const PlacesItem
* previousItem
= placesItem(index
- 1);
468 KBookmark previousBookmark
;
470 previousBookmark
= previousItem
->bookmark();
473 m_bookmarkManager
->root().moveBookmark(insertedBookmark
, previousBookmark
);
476 if (index
== count() - 1) {
477 // The item has been appended as last item to the list. In this
478 // case assure that it is also appended after the hidden items and
479 // not before (like done otherwise).
480 m_bookmarkedItems
.append(0);
484 int bookmarkIndex
= 0;
485 while (bookmarkIndex
< m_bookmarkedItems
.count()) {
486 if (!m_bookmarkedItems
[bookmarkIndex
]) {
488 if (modelIndex
+ 1 == index
) {
494 m_bookmarkedItems
.insert(bookmarkIndex
, 0);
497 triggerBookmarksSaving();
499 #ifdef PLACESITEMMODEL_DEBUG
500 kDebug() << "Inserted item" << index
;
505 void PlacesItemModel::onItemRemoved(int index
, KStandardItem
* removedItem
)
507 PlacesItem
* placesItem
= dynamic_cast<PlacesItem
*>(removedItem
);
509 const KBookmark bookmark
= placesItem
->bookmark();
510 m_bookmarkManager
->root().deleteBookmark(bookmark
);
513 const int boomarkIndex
= bookmarkIndex(index
);
514 Q_ASSERT(!m_bookmarkedItems
[boomarkIndex
]);
515 m_bookmarkedItems
.removeAt(boomarkIndex
);
517 triggerBookmarksSaving();
519 #ifdef PLACESITEMMODEL_DEBUG
520 kDebug() << "Removed item" << index
;
525 void PlacesItemModel::onItemChanged(int index
, const QSet
<QByteArray
>& changedRoles
)
527 const PlacesItem
* changedItem
= placesItem(index
);
529 // Take care to apply the PlacesItemModel-order of the changed item
530 // also to the bookmark-manager.
531 const KBookmark insertedBookmark
= changedItem
->bookmark();
533 const PlacesItem
* previousItem
= placesItem(index
- 1);
534 KBookmark previousBookmark
;
536 previousBookmark
= previousItem
->bookmark();
539 m_bookmarkManager
->root().moveBookmark(insertedBookmark
, previousBookmark
);
542 if (changedRoles
.contains("isHidden")) {
543 if (!m_hiddenItemsShown
&& changedItem
->isHidden()) {
544 m_hiddenItemToRemove
= index
;
545 QTimer::singleShot(0, this, SLOT(hideItem()));
549 triggerBookmarksSaving();
552 void PlacesItemModel::slotDeviceAdded(const QString
& udi
)
554 const Solid::Device
device(udi
);
556 if (!m_predicate
.matches(device
)) {
560 m_availableDevices
<< udi
;
561 const KBookmark bookmark
= PlacesItem::createDeviceBookmark(m_bookmarkManager
, udi
);
562 appendItem(new PlacesItem(bookmark
));
565 void PlacesItemModel::slotDeviceRemoved(const QString
& udi
)
567 if (!m_availableDevices
.contains(udi
)) {
571 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
572 PlacesItem
* item
= m_bookmarkedItems
[i
];
573 if (item
&& item
->udi() == udi
) {
574 m_bookmarkedItems
.removeAt(i
);
580 for (int i
= 0; i
< count(); ++i
) {
581 if (placesItem(i
)->udi() == udi
) {
588 void PlacesItemModel::slotStorageTeardownDone(Solid::ErrorType error
, const QVariant
& errorData
)
590 if (error
&& errorData
.isValid()) {
591 emit
errorMessage(errorData
.toString());
595 void PlacesItemModel::slotStorageSetupDone(Solid::ErrorType error
,
596 const QVariant
& errorData
,
601 const int index
= m_storageSetupInProgress
.take(sender());
602 const PlacesItem
* item
= placesItem(index
);
608 if (errorData
.isValid()) {
609 emit
errorMessage(i18nc("@info", "An error occurred while accessing '%1', the system responded: %2",
611 errorData
.toString()));
613 emit
errorMessage(i18nc("@info", "An error occurred while accessing '%1'",
616 emit
storageSetupDone(index
, false);
618 emit
storageSetupDone(index
, true);
622 void PlacesItemModel::hideItem()
624 hideItem(m_hiddenItemToRemove
);
625 m_hiddenItemToRemove
= -1;
628 void PlacesItemModel::updateBookmarks()
630 // Verify whether new bookmarks have been added or existing
631 // bookmarks have been changed.
632 KBookmarkGroup root
= m_bookmarkManager
->root();
633 KBookmark newBookmark
= root
.first();
634 while (!newBookmark
.isNull()) {
635 if (acceptBookmark(newBookmark
, m_availableDevices
)) {
638 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
639 PlacesItem
* item
= m_bookmarkedItems
[i
];
641 item
= placesItem(modelIndex
);
645 const KBookmark oldBookmark
= item
->bookmark();
646 if (equalBookmarkIdentifiers(newBookmark
, oldBookmark
)) {
647 // The bookmark has been found in the model or as
648 // a hidden item. The content of the bookmark might
649 // have been changed, so an update is done.
651 if (newBookmark
.metaDataItem("UDI").isEmpty()) {
652 item
->setBookmark(newBookmark
);
659 const QString udi
= newBookmark
.metaDataItem("UDI");
663 * Only add a new places item, if the item text is not empty
664 * and if the device is available. Fixes the strange behaviour -
665 * add a places item without text in the Places section - when you
666 * remove a device (e.g. a usb stick) without unmounting.
668 if (udi
.isEmpty() || Solid::Device(udi
).isValid()) {
669 PlacesItem
* item
= new PlacesItem(newBookmark
);
670 if (item
->isHidden() && !m_hiddenItemsShown
) {
671 m_bookmarkedItems
.append(item
);
673 appendItemToGroup(item
);
679 newBookmark
= root
.next(newBookmark
);
682 // Remove items that are not part of the bookmark-manager anymore
684 for (int i
= m_bookmarkedItems
.count() - 1; i
>= 0; --i
) {
685 PlacesItem
* item
= m_bookmarkedItems
[i
];
686 const bool itemIsPartOfModel
= (item
== 0);
687 if (itemIsPartOfModel
) {
688 item
= placesItem(modelIndex
);
691 bool hasBeenRemoved
= true;
692 const KBookmark oldBookmark
= item
->bookmark();
693 KBookmark newBookmark
= root
.first();
694 while (!newBookmark
.isNull()) {
695 if (equalBookmarkIdentifiers(newBookmark
, oldBookmark
)) {
696 hasBeenRemoved
= false;
699 newBookmark
= root
.next(newBookmark
);
702 if (hasBeenRemoved
) {
703 if (m_bookmarkedItems
[i
]) {
704 delete m_bookmarkedItems
[i
];
705 m_bookmarkedItems
.removeAt(i
);
707 removeItem(modelIndex
);
712 if (itemIsPartOfModel
) {
718 void PlacesItemModel::saveBookmarks()
720 m_bookmarkManager
->emitChanged(m_bookmarkManager
->root());
723 void PlacesItemModel::loadBookmarks()
725 KBookmarkGroup root
= m_bookmarkManager
->root();
726 KBookmark bookmark
= root
.first();
727 QSet
<QString
> devices
= m_availableDevices
;
729 QSet
<KUrl
> missingSystemBookmarks
;
730 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
731 missingSystemBookmarks
.insert(data
.url
);
734 // The bookmarks might have a mixed order of places, devices and search-groups due
735 // to the compatibility with the KFilePlacesPanel. In Dolphin's places panel the
736 // items should always be collected in one group so the items are collected first
737 // in separate lists before inserting them.
738 QList
<PlacesItem
*> placesItems
;
739 QList
<PlacesItem
*> recentlyAccessedItems
;
740 QList
<PlacesItem
*> searchForItems
;
741 QList
<PlacesItem
*> devicesItems
;
743 while (!bookmark
.isNull()) {
744 if (acceptBookmark(bookmark
, devices
)) {
745 PlacesItem
* item
= new PlacesItem(bookmark
);
746 if (item
->groupType() == PlacesItem::DevicesType
) {
747 devices
.remove(item
->udi());
748 devicesItems
.append(item
);
750 const KUrl url
= bookmark
.url();
751 if (missingSystemBookmarks
.contains(url
)) {
752 missingSystemBookmarks
.remove(url
);
754 // Try to retranslate the text of system bookmarks to have translated
755 // items when changing the language. In case if the user has applied a custom
756 // text, the retranslation will fail and the users custom text is still used.
757 // It is important to use "KFile System Bookmarks" as context (see
758 // createSystemBookmarks()).
759 item
->setText(i18nc("KFile System Bookmarks", bookmark
.text().toUtf8().data()));
760 item
->setSystemItem(true);
763 switch (item
->groupType()) {
764 case PlacesItem::PlacesType
: placesItems
.append(item
); break;
765 case PlacesItem::RecentlyAccessedType
: recentlyAccessedItems
.append(item
); break;
766 case PlacesItem::SearchForType
: searchForItems
.append(item
); break;
767 case PlacesItem::DevicesType
:
768 default: Q_ASSERT(false); break;
773 bookmark
= root
.next(bookmark
);
776 if (!missingSystemBookmarks
.isEmpty()) {
777 // The current bookmarks don't contain all system-bookmarks. Add the missing
779 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
780 if (missingSystemBookmarks
.contains(data
.url
)) {
781 PlacesItem
* item
= createSystemPlacesItem(data
);
782 switch (item
->groupType()) {
783 case PlacesItem::PlacesType
: placesItems
.append(item
); break;
784 case PlacesItem::RecentlyAccessedType
: recentlyAccessedItems
.append(item
); break;
785 case PlacesItem::SearchForType
: searchForItems
.append(item
); break;
786 case PlacesItem::DevicesType
:
787 default: Q_ASSERT(false); break;
793 // Create items for devices that have not been stored as bookmark yet
794 foreach (const QString
& udi
, devices
) {
795 const KBookmark bookmark
= PlacesItem::createDeviceBookmark(m_bookmarkManager
, udi
);
796 devicesItems
.append(new PlacesItem(bookmark
));
799 QList
<PlacesItem
*> items
;
800 items
.append(placesItems
);
801 items
.append(recentlyAccessedItems
);
802 items
.append(searchForItems
);
803 items
.append(devicesItems
);
805 foreach (PlacesItem
* item
, items
) {
806 if (!m_hiddenItemsShown
&& item
->isHidden()) {
807 m_bookmarkedItems
.append(item
);
813 #ifdef PLACESITEMMODEL_DEBUG
814 kDebug() << "Loaded bookmarks";
819 bool PlacesItemModel::acceptBookmark(const KBookmark
& bookmark
,
820 const QSet
<QString
>& availableDevices
) const
822 const QString udi
= bookmark
.metaDataItem("UDI");
823 const KUrl url
= bookmark
.url();
824 const QString appName
= bookmark
.metaDataItem("OnlyInApp");
825 const bool deviceAvailable
= availableDevices
.contains(udi
);
827 const bool allowedHere
= (appName
.isEmpty()
828 || appName
== KGlobal::mainComponent().componentName()
829 || appName
== KGlobal::mainComponent().componentName() + AppNamePrefix
)
830 && (m_fileIndexingEnabled
|| (url
.protocol() != QLatin1String("timeline") &&
831 url
.protocol() != QLatin1String("search")));
833 return (udi
.isEmpty() && allowedHere
) || deviceAvailable
;
836 PlacesItem
* PlacesItemModel::createSystemPlacesItem(const SystemBookmarkData
& data
)
838 KBookmark bookmark
= PlacesItem::createBookmark(m_bookmarkManager
,
843 const QString protocol
= data
.url
.protocol();
844 if (protocol
== QLatin1String("timeline") || protocol
== QLatin1String("search")) {
845 // As long as the KFilePlacesView from kdelibs is available, the system-bookmarks
846 // for "Recently Accessed" and "Search For" should be a setting available only
847 // in the Places Panel (see description of AppNamePrefix for more details).
848 const QString appName
= KGlobal::mainComponent().componentName() + AppNamePrefix
;
849 bookmark
.setMetaDataItem("OnlyInApp", appName
);
852 PlacesItem
* item
= new PlacesItem(bookmark
);
853 item
->setSystemItem(true);
855 // Create default view-properties for all "Search For" and "Recently Accessed" bookmarks
856 // in case if the user has not already created custom view-properties for a corresponding
858 const bool createDefaultViewProperties
= (item
->groupType() == PlacesItem::SearchForType
||
859 item
->groupType() == PlacesItem::RecentlyAccessedType
) &&
860 !GeneralSettings::self()->globalViewProps();
861 if (createDefaultViewProperties
) {
862 ViewProperties
props(convertedUrl(data
.url
));
863 if (!props
.exist()) {
864 const QString path
= data
.url
.path();
865 if (path
== QLatin1String("/documents")) {
866 props
.setViewMode(DolphinView::DetailsView
);
867 props
.setPreviewsShown(false);
868 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "path");
869 } else if (path
== QLatin1String("/images")) {
870 props
.setViewMode(DolphinView::IconsView
);
871 props
.setPreviewsShown(true);
872 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "imageSize");
873 } else if (path
== QLatin1String("/audio")) {
874 props
.setViewMode(DolphinView::DetailsView
);
875 props
.setPreviewsShown(false);
876 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "artist" << "album");
877 } else if (path
== QLatin1String("/videos")) {
878 props
.setViewMode(DolphinView::IconsView
);
879 props
.setPreviewsShown(true);
880 props
.setVisibleRoles(QList
<QByteArray
>() << "text");
881 } else if (data
.url
.protocol() == "timeline") {
882 props
.setViewMode(DolphinView::DetailsView
);
883 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "date");
891 void PlacesItemModel::createSystemBookmarks()
893 Q_ASSERT(m_systemBookmarks
.isEmpty());
894 Q_ASSERT(m_systemBookmarksIndexes
.isEmpty());
896 // Note: The context of the I18N_NOOP2 must be "KFile System Bookmarks". The real
897 // i18nc call is done after reading the bookmark. The reason why the i18nc call is not
898 // done here is because otherwise switching the language would not result in retranslating the
900 m_systemBookmarks
.append(SystemBookmarkData(KUrl(KUser().homeDir()),
902 I18N_NOOP2("KFile System Bookmarks", "Home")));
903 m_systemBookmarks
.append(SystemBookmarkData(KUrl("remote:/"),
905 I18N_NOOP2("KFile System Bookmarks", "Network")));
906 m_systemBookmarks
.append(SystemBookmarkData(KUrl("/"),
908 I18N_NOOP2("KFile System Bookmarks", "Root")));
909 m_systemBookmarks
.append(SystemBookmarkData(KUrl("trash:/"),
911 I18N_NOOP2("KFile System Bookmarks", "Trash")));
913 if (m_fileIndexingEnabled
) {
914 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/today"),
916 I18N_NOOP2("KFile System Bookmarks", "Today")));
917 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/yesterday"),
919 I18N_NOOP2("KFile System Bookmarks", "Yesterday")));
920 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/thismonth"),
921 "view-calendar-month",
922 I18N_NOOP2("KFile System Bookmarks", "This Month")));
923 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/lastmonth"),
924 "view-calendar-month",
925 I18N_NOOP2("KFile System Bookmarks", "Last Month")));
926 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/documents"),
928 I18N_NOOP2("KFile System Bookmarks", "Documents")));
929 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/images"),
931 I18N_NOOP2("KFile System Bookmarks", "Images")));
932 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/audio"),
934 I18N_NOOP2("KFile System Bookmarks", "Audio Files")));
935 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/videos"),
937 I18N_NOOP2("KFile System Bookmarks", "Videos")));
940 for (int i
= 0; i
< m_systemBookmarks
.count(); ++i
) {
941 m_systemBookmarksIndexes
.insert(m_systemBookmarks
[i
].url
, i
);
945 void PlacesItemModel::clear() {
946 m_bookmarkedItems
.clear();
947 KStandardItemModel::clear();
950 void PlacesItemModel::initializeAvailableDevices()
952 QString
predicate("[[[[ StorageVolume.ignored == false AND [ StorageVolume.usage == 'FileSystem' OR StorageVolume.usage == 'Encrypted' ]]"
954 "[ IS StorageAccess AND StorageDrive.driveType == 'Floppy' ]]"
956 "OpticalDisc.availableContent & 'Audio' ]"
958 "StorageAccess.ignored == false ]");
961 if (KProtocolInfo::isKnownProtocol("mtp")) {
962 predicate
.prepend("[");
963 predicate
.append(" OR PortableMediaPlayer.supportedProtocols == 'mtp']");
966 m_predicate
= Solid::Predicate::fromString(predicate
);
967 Q_ASSERT(m_predicate
.isValid());
969 Solid::DeviceNotifier
* notifier
= Solid::DeviceNotifier::instance();
970 connect(notifier
, SIGNAL(deviceAdded(QString
)), this, SLOT(slotDeviceAdded(QString
)));
971 connect(notifier
, SIGNAL(deviceRemoved(QString
)), this, SLOT(slotDeviceRemoved(QString
)));
973 const QList
<Solid::Device
>& deviceList
= Solid::Device::listFromQuery(m_predicate
);
974 foreach (const Solid::Device
& device
, deviceList
) {
975 m_availableDevices
<< device
.udi();
979 int PlacesItemModel::bookmarkIndex(int index
) const
981 int bookmarkIndex
= 0;
983 while (bookmarkIndex
< m_bookmarkedItems
.count()) {
984 if (!m_bookmarkedItems
[bookmarkIndex
]) {
985 if (modelIndex
== index
) {
993 return bookmarkIndex
>= m_bookmarkedItems
.count() ? -1 : bookmarkIndex
;
996 void PlacesItemModel::hideItem(int index
)
998 PlacesItem
* shownItem
= placesItem(index
);
1003 shownItem
->setHidden(true);
1004 if (m_hiddenItemsShown
) {
1005 // Removing items from the model is not allowed if all hidden
1006 // items should be shown.
1010 const int newIndex
= bookmarkIndex(index
);
1011 if (newIndex
>= 0) {
1012 const KBookmark hiddenBookmark
= shownItem
->bookmark();
1013 PlacesItem
* hiddenItem
= new PlacesItem(hiddenBookmark
);
1015 const PlacesItem
* previousItem
= placesItem(index
- 1);
1016 KBookmark previousBookmark
;
1018 previousBookmark
= previousItem
->bookmark();
1021 const bool updateBookmark
= (m_bookmarkManager
->root().indexOf(hiddenBookmark
) >= 0);
1024 if (updateBookmark
) {
1025 // removeItem() also removed the bookmark from m_bookmarkManager in
1026 // PlacesItemModel::onItemRemoved(). However for hidden items the
1027 // bookmark should still be remembered, so readd it again:
1028 m_bookmarkManager
->root().addBookmark(hiddenBookmark
);
1029 m_bookmarkManager
->root().moveBookmark(hiddenBookmark
, previousBookmark
);
1030 triggerBookmarksSaving();
1033 m_bookmarkedItems
.insert(newIndex
, hiddenItem
);
1037 void PlacesItemModel::triggerBookmarksSaving()
1039 if (m_saveBookmarksTimer
) {
1040 m_saveBookmarksTimer
->start();
1044 QString
PlacesItemModel::internalMimeType() const
1046 return "application/x-dolphinplacesmodel-" +
1047 QString::number((qptrdiff
)this);
1050 int PlacesItemModel::groupedDropIndex(int index
, const PlacesItem
* item
) const
1054 int dropIndex
= index
;
1055 const PlacesItem::GroupType type
= item
->groupType();
1057 const int itemCount
= count();
1059 dropIndex
= itemCount
;
1062 // Search nearest previous item with the same group
1063 int previousIndex
= -1;
1064 for (int i
= dropIndex
- 1; i
>= 0; --i
) {
1065 if (placesItem(i
)->groupType() == type
) {
1071 // Search nearest next item with the same group
1073 for (int i
= dropIndex
; i
< count(); ++i
) {
1074 if (placesItem(i
)->groupType() == type
) {
1080 // Adjust the drop-index to be inserted to the
1081 // nearest item with the same group.
1082 if (previousIndex
>= 0 && nextIndex
>= 0) {
1083 dropIndex
= (dropIndex
- previousIndex
< nextIndex
- dropIndex
) ?
1084 previousIndex
+ 1 : nextIndex
;
1085 } else if (previousIndex
>= 0) {
1086 dropIndex
= previousIndex
+ 1;
1087 } else if (nextIndex
>= 0) {
1088 dropIndex
= nextIndex
;
1094 bool PlacesItemModel::equalBookmarkIdentifiers(const KBookmark
& b1
, const KBookmark
& b2
)
1096 const QString udi1
= b1
.metaDataItem("UDI");
1097 const QString udi2
= b2
.metaDataItem("UDI");
1098 if (!udi1
.isEmpty() && !udi2
.isEmpty()) {
1099 return udi1
== udi2
;
1101 return b1
.metaDataItem("ID") == b2
.metaDataItem("ID");
1105 KUrl
PlacesItemModel::createTimelineUrl(const KUrl
& url
)
1107 // TODO: Clarify with the Baloo-team whether it makes sense
1108 // provide default-timeline-URLs like 'yesterday', 'this month'
1109 // and 'last month'.
1112 const QString path
= url
.pathOrUrl();
1113 if (path
.endsWith(QLatin1String("yesterday"))) {
1114 const QDate date
= QDate::currentDate().addDays(-1);
1115 const int year
= date
.year();
1116 const int month
= date
.month();
1117 const int day
= date
.day();
1118 timelineUrl
= "timeline:/" + timelineDateString(year
, month
) +
1119 '/' + timelineDateString(year
, month
, day
);
1120 } else if (path
.endsWith(QLatin1String("thismonth"))) {
1121 const QDate date
= QDate::currentDate();
1122 timelineUrl
= "timeline:/" + timelineDateString(date
.year(), date
.month());
1123 } else if (path
.endsWith(QLatin1String("lastmonth"))) {
1124 const QDate date
= QDate::currentDate().addMonths(-1);
1125 timelineUrl
= "timeline:/" + timelineDateString(date
.year(), date
.month());
1127 Q_ASSERT(path
.endsWith(QLatin1String("today")));
1134 QString
PlacesItemModel::timelineDateString(int year
, int month
, int day
)
1136 QString date
= QString::number(year
) + '-';
1140 date
+= QString::number(month
);
1147 date
+= QString::number(day
);
1153 KUrl
PlacesItemModel::createSearchUrl(const KUrl
& url
)
1158 const QString path
= url
.pathOrUrl();
1159 if (path
.endsWith(QLatin1String("documents"))) {
1160 searchUrl
= searchUrlForType("Document");
1161 } else if (path
.endsWith(QLatin1String("images"))) {
1162 searchUrl
= searchUrlForType("Image");
1163 } else if (path
.endsWith(QLatin1String("audio"))) {
1164 searchUrl
= searchUrlForType("Audio");
1165 } else if (path
.endsWith(QLatin1String("videos"))) {
1166 searchUrl
= searchUrlForType("Video");
1178 KUrl
PlacesItemModel::searchUrlForType(const QString
& type
)
1181 query
.addType("File");
1182 query
.addType(type
);
1184 return query
.toSearchUrl();
1188 #ifdef PLACESITEMMODEL_DEBUG
1189 void PlacesItemModel::showModelState()
1191 kDebug() << "=================================";
1192 kDebug() << "Model:";
1193 kDebug() << "hidden-index model-index text";
1195 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
1196 if (m_bookmarkedItems
[i
]) {
1197 kDebug() << i
<< "(Hidden) " << " " << m_bookmarkedItems
[i
]->dataValue("text").toString();
1199 if (item(modelIndex
)) {
1200 kDebug() << i
<< " " << modelIndex
<< " " << item(modelIndex
)->dataValue("text").toString();
1202 kDebug() << i
<< " " << modelIndex
<< " " << "(not available yet)";
1209 kDebug() << "Bookmarks:";
1211 int bookmarkIndex
= 0;
1212 KBookmarkGroup root
= m_bookmarkManager
->root();
1213 KBookmark bookmark
= root
.first();
1214 while (!bookmark
.isNull()) {
1215 const QString udi
= bookmark
.metaDataItem("UDI");
1216 const QString text
= udi
.isEmpty() ? bookmark
.text() : udi
;
1217 if (bookmark
.metaDataItem("IsHidden") == QLatin1String("true")) {
1218 kDebug() << bookmarkIndex
<< "(Hidden)" << text
;
1220 kDebug() << bookmarkIndex
<< " " << text
;
1223 bookmark
= root
.next(bookmark
);
1229 #include "placesitemmodel.moc"