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>
35 #include <KStandardDirs>
37 #include "placesitem.h"
43 #include <Solid/Device>
44 #include <Solid/DeviceNotifier>
45 #include <Solid/OpticalDisc>
46 #include <Solid/OpticalDrive>
47 #include <Solid/StorageAccess>
48 #include <Solid/StorageDrive>
50 #include <views/dolphinview.h>
51 #include <views/viewproperties.h>
54 #include <Nepomuk/ResourceManager>
55 #include <Nepomuk/Query/ComparisonTerm>
56 #include <Nepomuk/Query/LiteralTerm>
57 #include <Nepomuk/Query/Query>
58 #include <Nepomuk/Query/ResourceTypeTerm>
59 #include <Nepomuk/Vocabulary/NFO>
60 #include <Nepomuk/Vocabulary/NIE>
64 // As long as KFilePlacesView from kdelibs is available in parallel, the
65 // system-bookmarks for "Recently Accessed" and "Search For" should be
66 // shown only inside the Places Panel. This is necessary as the stored
67 // URLs needs to get translated to a Nepomuk-search-URL on-the-fly to
68 // be independent from changes in the Nepomuk-search-URL-syntax.
69 // Hence a prefix to the application-name of the stored bookmarks is
70 // added, which is only read by PlacesItemModel.
71 const char* AppNamePrefix
= "-places-panel";
74 PlacesItemModel::PlacesItemModel(QObject
* parent
) :
75 KStandardItemModel(parent
),
76 m_nepomukRunning(false),
77 m_hiddenItemsShown(false),
82 m_systemBookmarksIndexes(),
84 m_hiddenItemToRemove(-1),
85 m_saveBookmarksTimer(0),
86 m_updateBookmarksTimer(0)
89 m_nepomukRunning
= (Nepomuk::ResourceManager::instance()->initialized());
91 const QString file
= KStandardDirs::locateLocal("data", "kfileplaces/bookmarks.xml");
92 m_bookmarkManager
= KBookmarkManager::managerForFile(file
, "kfilePlaces");
94 createSystemBookmarks();
95 initializeAvailableDevices();
98 const int syncBookmarksTimeout
= 100;
100 m_saveBookmarksTimer
= new QTimer(this);
101 m_saveBookmarksTimer
->setInterval(syncBookmarksTimeout
);
102 m_saveBookmarksTimer
->setSingleShot(true);
103 connect(m_saveBookmarksTimer
, SIGNAL(timeout()), this, SLOT(saveBookmarks()));
105 m_updateBookmarksTimer
= new QTimer(this);
106 m_updateBookmarksTimer
->setInterval(syncBookmarksTimeout
);
107 m_updateBookmarksTimer
->setSingleShot(true);
108 connect(m_updateBookmarksTimer
, SIGNAL(timeout()), this, SLOT(updateBookmarks()));
110 connect(m_bookmarkManager
, SIGNAL(changed(QString
,QString
)),
111 m_updateBookmarksTimer
, SLOT(start()));
112 connect(m_bookmarkManager
, SIGNAL(bookmarksChanged(QString
)),
113 m_updateBookmarksTimer
, SLOT(start()));
116 PlacesItemModel::~PlacesItemModel()
119 qDeleteAll(m_bookmarkedItems
);
120 m_bookmarkedItems
.clear();
123 PlacesItem
* PlacesItemModel::createPlacesItem(const QString
& text
,
125 const QString
& iconName
)
127 const KBookmark bookmark
= PlacesItem::createBookmark(m_bookmarkManager
, text
, url
, iconName
);
128 return new PlacesItem(bookmark
);
131 PlacesItem
* PlacesItemModel::placesItem(int index
) const
133 return dynamic_cast<PlacesItem
*>(item(index
));
136 int PlacesItemModel::hiddenCount() const
139 int hiddenItemCount
= 0;
140 foreach (const PlacesItem
* item
, m_bookmarkedItems
) {
144 if (placesItem(modelIndex
)->isHidden()) {
151 return hiddenItemCount
;
154 void PlacesItemModel::setHiddenItemsShown(bool show
)
156 if (m_hiddenItemsShown
== show
) {
160 m_hiddenItemsShown
= show
;
163 // Move all items that are part of m_bookmarkedItems to the model.
164 QList
<PlacesItem
*> itemsToInsert
;
165 QList
<int> insertPos
;
167 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
168 if (m_bookmarkedItems
[i
]) {
169 itemsToInsert
.append(m_bookmarkedItems
[i
]);
170 m_bookmarkedItems
[i
] = 0;
171 insertPos
.append(modelIndex
);
176 // Inserting the items will automatically insert an item
177 // to m_bookmarkedItems in PlacesItemModel::onItemsInserted().
178 // The items are temporary saved in itemsToInsert, so
179 // m_bookmarkedItems can be shrinked now.
180 m_bookmarkedItems
.erase(m_bookmarkedItems
.begin(),
181 m_bookmarkedItems
.begin() + itemsToInsert
.count());
183 for (int i
= 0; i
< itemsToInsert
.count(); ++i
) {
184 insertItem(insertPos
[i
], itemsToInsert
[i
]);
187 Q_ASSERT(m_bookmarkedItems
.count() == count());
189 // Move all items of the model, where the "isHidden" property is true, to
190 // m_bookmarkedItems.
191 Q_ASSERT(m_bookmarkedItems
.count() == count());
192 for (int i
= count() - 1; i
>= 0; --i
) {
193 if (placesItem(i
)->isHidden()) {
199 #ifdef PLACESITEMMODEL_DEBUG
200 kDebug() << "Changed visibility of hidden items";
205 bool PlacesItemModel::hiddenItemsShown() const
207 return m_hiddenItemsShown
;
210 int PlacesItemModel::closestItem(const KUrl
& url
) const
215 for (int i
= 0; i
< count(); ++i
) {
216 const KUrl itemUrl
= placesItem(i
)->url();
217 if (itemUrl
.isParentOf(url
)) {
218 const int length
= itemUrl
.prettyUrl().length();
219 if (length
> maxLength
) {
229 QAction
* PlacesItemModel::ejectAction(int index
) const
231 const PlacesItem
* item
= placesItem(index
);
232 if (item
&& item
->device().is
<Solid::OpticalDisc
>()) {
233 return new QAction(KIcon("media-eject"), i18nc("@item", "Eject '%1'", item
->text()), 0);
239 QAction
* PlacesItemModel::teardownAction(int index
) const
241 const PlacesItem
* item
= placesItem(index
);
246 Solid::Device device
= item
->device();
247 const bool providesTearDown
= device
.is
<Solid::StorageAccess
>() &&
248 device
.as
<Solid::StorageAccess
>()->isAccessible();
249 if (!providesTearDown
) {
253 Solid::StorageDrive
* drive
= device
.as
<Solid::StorageDrive
>();
255 drive
= device
.parent().as
<Solid::StorageDrive
>();
258 bool hotPluggable
= false;
259 bool removable
= false;
261 hotPluggable
= drive
->isHotpluggable();
262 removable
= drive
->isRemovable();
267 const QString label
= item
->text();
268 if (device
.is
<Solid::OpticalDisc
>()) {
269 text
= i18nc("@item", "Release '%1'", label
);
270 } else if (removable
|| hotPluggable
) {
271 text
= i18nc("@item", "Safely Remove '%1'", label
);
272 iconName
= "media-eject";
274 text
= i18nc("@item", "Unmount '%1'", label
);
275 iconName
= "media-eject";
278 if (iconName
.isEmpty()) {
279 return new QAction(text
, 0);
282 return new QAction(KIcon(iconName
), text
, 0);
285 void PlacesItemModel::requestEject(int index
)
287 const PlacesItem
* item
= placesItem(index
);
289 Solid::OpticalDrive
* drive
= item
->device().parent().as
<Solid::OpticalDrive
>();
291 connect(drive
, SIGNAL(ejectDone(Solid::ErrorType
,QVariant
,QString
)),
292 this, SLOT(slotStorageTeardownDone(Solid::ErrorType
,QVariant
)));
295 const QString label
= item
->text();
296 const QString message
= i18nc("@info", "The device '%1' is not a disk and cannot be ejected.", label
);
297 emit
errorMessage(message
);
302 void PlacesItemModel::requestTeardown(int index
)
304 const PlacesItem
* item
= placesItem(index
);
306 Solid::StorageAccess
* access
= item
->device().as
<Solid::StorageAccess
>();
308 connect(access
, SIGNAL(teardownDone(Solid::ErrorType
,QVariant
,QString
)),
309 this, SLOT(slotStorageTeardownDone(Solid::ErrorType
,QVariant
)));
315 QMimeData
* PlacesItemModel::createMimeData(const QSet
<int>& indexes
) const
320 QDataStream
stream(&itemData
, QIODevice::WriteOnly
);
322 foreach (int index
, indexes
) {
323 const KUrl itemUrl
= placesItem(index
)->url();
324 if (itemUrl
.isValid()) {
330 QMimeData
* mimeData
= new QMimeData();
331 if (!urls
.isEmpty()) {
332 urls
.populateMimeData(mimeData
);
334 mimeData
->setData(internalMimeType(), itemData
);
339 void PlacesItemModel::dropMimeData(int index
, const QMimeData
* mimeData
)
341 if (mimeData
->hasFormat(internalMimeType())) {
342 // The item has been moved inside the view
343 QByteArray itemData
= mimeData
->data(internalMimeType());
344 QDataStream
stream(&itemData
, QIODevice::ReadOnly
);
348 PlacesItem
* oldItem
= placesItem(oldIndex
);
353 PlacesItem
* newItem
= new PlacesItem(oldItem
->bookmark());
354 removeItem(oldIndex
);
356 if (oldIndex
<= index
) {
360 const int dropIndex
= groupedDropIndex(index
, newItem
);
361 insertItem(dropIndex
, newItem
);
362 } else if (mimeData
->hasFormat("text/uri-list")) {
363 // One or more items must be added to the model
364 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
365 for (int i
= urls
.count() - 1; i
>= 0; --i
) {
366 const KUrl
& url
= urls
[i
];
368 QString text
= url
.fileName();
369 if (text
.isEmpty()) {
373 KBookmark bookmark
= PlacesItem::createBookmark(m_bookmarkManager
,
377 PlacesItem
* newItem
= new PlacesItem(bookmark
);
378 const int dropIndex
= groupedDropIndex(index
, newItem
);
379 insertItem(dropIndex
, newItem
);
384 KUrl
PlacesItemModel::convertedUrl(const KUrl
& url
)
387 if (url
.protocol() == QLatin1String("timeline")) {
388 newUrl
= createTimelineUrl(url
);
389 } else if (url
.protocol() == QLatin1String("search")) {
390 newUrl
= createSearchUrl(url
);
396 void PlacesItemModel::onItemInserted(int index
)
398 const PlacesItem
* insertedItem
= placesItem(index
);
400 // Take care to apply the PlacesItemModel-order of the inserted item
401 // also to the bookmark-manager.
402 const KBookmark insertedBookmark
= insertedItem
->bookmark();
404 const PlacesItem
* previousItem
= placesItem(index
- 1);
405 KBookmark previousBookmark
;
407 previousBookmark
= previousItem
->bookmark();
410 m_bookmarkManager
->root().moveBookmark(insertedBookmark
, previousBookmark
);
413 if (index
== count() - 1) {
414 // The item has been appended as last item to the list. In this
415 // case assure that it is also appended after the hidden items and
416 // not before (like done otherwise).
417 m_bookmarkedItems
.append(0);
421 int bookmarkIndex
= 0;
422 while (bookmarkIndex
< m_bookmarkedItems
.count()) {
423 if (!m_bookmarkedItems
[bookmarkIndex
]) {
425 if (modelIndex
+ 1 == index
) {
431 m_bookmarkedItems
.insert(bookmarkIndex
, 0);
434 triggerBookmarksSaving();
436 #ifdef PLACESITEMMODEL_DEBUG
437 kDebug() << "Inserted item" << index
;
442 void PlacesItemModel::onItemRemoved(int index
, KStandardItem
* removedItem
)
444 PlacesItem
* placesItem
= dynamic_cast<PlacesItem
*>(removedItem
);
446 const KBookmark bookmark
= placesItem
->bookmark();
447 m_bookmarkManager
->root().deleteBookmark(bookmark
);
450 const int boomarkIndex
= bookmarkIndex(index
);
451 Q_ASSERT(!m_bookmarkedItems
[boomarkIndex
]);
452 m_bookmarkedItems
.removeAt(boomarkIndex
);
454 triggerBookmarksSaving();
456 #ifdef PLACESITEMMODEL_DEBUG
457 kDebug() << "Removed item" << index
;
462 void PlacesItemModel::onItemChanged(int index
, const QSet
<QByteArray
>& changedRoles
)
464 const PlacesItem
* changedItem
= placesItem(index
);
466 // Take care to apply the PlacesItemModel-order of the changed item
467 // also to the bookmark-manager.
468 const KBookmark insertedBookmark
= changedItem
->bookmark();
470 const PlacesItem
* previousItem
= placesItem(index
- 1);
471 KBookmark previousBookmark
;
473 previousBookmark
= previousItem
->bookmark();
476 m_bookmarkManager
->root().moveBookmark(insertedBookmark
, previousBookmark
);
479 if (changedRoles
.contains("isHidden")) {
480 if (!m_hiddenItemsShown
&& changedItem
->isHidden()) {
481 m_hiddenItemToRemove
= index
;
482 QTimer::singleShot(0, this, SLOT(hideItem()));
486 triggerBookmarksSaving();
489 void PlacesItemModel::slotDeviceAdded(const QString
& udi
)
491 const Solid::Device
device(udi
);
492 if (m_predicate
.matches(device
)) {
493 m_availableDevices
<< udi
;
494 const KBookmark bookmark
= PlacesItem::createDeviceBookmark(m_bookmarkManager
, udi
);
495 appendItem(new PlacesItem(bookmark
));
499 void PlacesItemModel::slotDeviceRemoved(const QString
& udi
)
501 if (!m_availableDevices
.contains(udi
)) {
505 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
506 PlacesItem
* item
= m_bookmarkedItems
[i
];
507 if (item
&& item
->udi() == udi
) {
508 m_bookmarkedItems
.removeAt(i
);
514 for (int i
= 0; i
< count(); ++i
) {
515 if (placesItem(i
)->udi() == udi
) {
522 void PlacesItemModel::slotStorageTeardownDone(Solid::ErrorType error
, const QVariant
& errorData
)
524 if (error
&& errorData
.isValid()) {
525 emit
errorMessage(errorData
.toString());
529 void PlacesItemModel::hideItem()
531 hideItem(m_hiddenItemToRemove
);
532 m_hiddenItemToRemove
= -1;
535 void PlacesItemModel::updateBookmarks()
537 // Verify whether new bookmarks have been added or existing
538 // bookmarks have been changed.
539 KBookmarkGroup root
= m_bookmarkManager
->root();
540 KBookmark newBookmark
= root
.first();
541 while (!newBookmark
.isNull()) {
542 if (acceptBookmark(newBookmark
)) {
545 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
546 PlacesItem
* item
= m_bookmarkedItems
[i
];
548 item
= placesItem(modelIndex
);
552 const KBookmark oldBookmark
= item
->bookmark();
553 if (equalBookmarkIdentifiers(newBookmark
, oldBookmark
)) {
554 // The bookmark has been found in the model or as
555 // a hidden item. The content of the bookmark might
556 // have been changed, so an update is done.
558 if (newBookmark
.metaDataItem("UDI").isEmpty()) {
559 item
->setBookmark(newBookmark
);
566 PlacesItem
* item
= new PlacesItem(newBookmark
);
567 if (item
->isHidden() && !m_hiddenItemsShown
) {
568 m_bookmarkedItems
.append(item
);
575 newBookmark
= root
.next(newBookmark
);
578 // Remove items that are not part of the bookmark-manager anymore
580 for (int i
= m_bookmarkedItems
.count() - 1; i
>= 0; --i
) {
581 PlacesItem
* item
= m_bookmarkedItems
[i
];
582 const bool itemIsPartOfModel
= (item
== 0);
583 if (itemIsPartOfModel
) {
584 item
= placesItem(modelIndex
);
587 bool hasBeenRemoved
= true;
588 const KBookmark oldBookmark
= item
->bookmark();
589 KBookmark newBookmark
= root
.first();
590 while (!newBookmark
.isNull()) {
591 if (equalBookmarkIdentifiers(newBookmark
, oldBookmark
)) {
592 hasBeenRemoved
= false;
595 newBookmark
= root
.next(newBookmark
);
598 if (hasBeenRemoved
) {
599 if (m_bookmarkedItems
[i
]) {
600 delete m_bookmarkedItems
[i
];
601 m_bookmarkedItems
.removeAt(i
);
603 removeItem(modelIndex
);
608 if (itemIsPartOfModel
) {
614 void PlacesItemModel::saveBookmarks()
616 m_bookmarkManager
->emitChanged(m_bookmarkManager
->root());
619 void PlacesItemModel::loadBookmarks()
621 KBookmarkGroup root
= m_bookmarkManager
->root();
622 KBookmark bookmark
= root
.first();
623 QSet
<QString
> devices
= m_availableDevices
;
625 QSet
<KUrl
> missingSystemBookmarks
;
626 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
627 missingSystemBookmarks
.insert(data
.url
);
630 // The bookmarks might have a mixed order of places, devices and search-groups due
631 // to the compatibility with the KFilePlacesPanel. In Dolphin's places panel the
632 // items should always be collected in one group so the items are collected first
633 // in separate lists before inserting them.
634 QList
<PlacesItem
*> placesItems
;
635 QList
<PlacesItem
*> recentlyAccessedItems
;
636 QList
<PlacesItem
*> searchForItems
;
637 QList
<PlacesItem
*> devicesItems
;
639 while (!bookmark
.isNull()) {
640 if (acceptBookmark(bookmark
)) {
641 PlacesItem
* item
= new PlacesItem(bookmark
);
642 if (item
->groupType() == PlacesItem::DevicesType
) {
643 devices
.remove(item
->udi());
644 devicesItems
.append(item
);
646 const KUrl url
= bookmark
.url();
647 if (missingSystemBookmarks
.contains(url
)) {
648 missingSystemBookmarks
.remove(url
);
650 // Apply the translated text to the system bookmarks, otherwise an outdated
651 // translation might be shown.
652 const int index
= m_systemBookmarksIndexes
.value(url
);
653 item
->setText(m_systemBookmarks
[index
].text
);
654 item
->setSystemItem(true);
657 switch (item
->groupType()) {
658 case PlacesItem::PlacesType
: placesItems
.append(item
); break;
659 case PlacesItem::RecentlyAccessedType
: recentlyAccessedItems
.append(item
); break;
660 case PlacesItem::SearchForType
: searchForItems
.append(item
); break;
661 case PlacesItem::DevicesType
:
662 default: Q_ASSERT(false); break;
667 bookmark
= root
.next(bookmark
);
670 if (!missingSystemBookmarks
.isEmpty()) {
671 // The current bookmarks don't contain all system-bookmarks. Add the missing
673 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
674 if (missingSystemBookmarks
.contains(data
.url
)) {
675 PlacesItem
* item
= createSystemPlacesItem(data
);
676 switch (item
->groupType()) {
677 case PlacesItem::PlacesType
: placesItems
.append(item
); break;
678 case PlacesItem::RecentlyAccessedType
: recentlyAccessedItems
.append(item
); break;
679 case PlacesItem::SearchForType
: searchForItems
.append(item
); break;
680 case PlacesItem::DevicesType
:
681 default: Q_ASSERT(false); break;
687 // Create items for devices that have not been stored as bookmark yet
688 foreach (const QString
& udi
, devices
) {
689 const KBookmark bookmark
= PlacesItem::createDeviceBookmark(m_bookmarkManager
, udi
);
690 devicesItems
.append(new PlacesItem(bookmark
));
693 QList
<PlacesItem
*> items
;
694 items
.append(placesItems
);
695 items
.append(recentlyAccessedItems
);
696 items
.append(searchForItems
);
697 items
.append(devicesItems
);
699 foreach (PlacesItem
* item
, items
) {
700 if (!m_hiddenItemsShown
&& item
->isHidden()) {
701 m_bookmarkedItems
.append(item
);
707 #ifdef PLACESITEMMODEL_DEBUG
708 kDebug() << "Loaded bookmarks";
713 bool PlacesItemModel::acceptBookmark(const KBookmark
& bookmark
) const
715 const QString udi
= bookmark
.metaDataItem("UDI");
716 const KUrl url
= bookmark
.url();
717 const QString appName
= bookmark
.metaDataItem("OnlyInApp");
718 const bool deviceAvailable
= m_availableDevices
.contains(udi
);
720 const bool allowedHere
= (appName
.isEmpty()
721 || appName
== KGlobal::mainComponent().componentName()
722 || appName
== KGlobal::mainComponent().componentName() + AppNamePrefix
)
723 && (m_nepomukRunning
|| (url
.protocol() != QLatin1String("timeline") &&
724 url
.protocol() != QLatin1String("search")));
726 return (udi
.isEmpty() && allowedHere
) || deviceAvailable
;
729 PlacesItem
* PlacesItemModel::createSystemPlacesItem(const SystemBookmarkData
& data
)
731 KBookmark bookmark
= PlacesItem::createBookmark(m_bookmarkManager
,
736 const QString protocol
= data
.url
.protocol();
737 if (protocol
== QLatin1String("timeline") || protocol
== QLatin1String("search")) {
738 // As long as the KFilePlacesView from kdelibs is available, the system-bookmarks
739 // for "Recently Accessed" and "Search For" should be a setting available only
740 // in the Places Panel (see description of AppNamePrefix for more details).
741 const QString appName
= KGlobal::mainComponent().componentName() + AppNamePrefix
;
742 bookmark
.setMetaDataItem("OnlyInApp", appName
);
745 PlacesItem
* item
= new PlacesItem(bookmark
);
746 item
->setSystemItem(true);
748 // Create default view-properties for all "Search For" and "Recently Accessed" bookmarks
749 // in case if the user has not already created custom view-properties for a corresponding
751 const bool createDefaultViewProperties
= (item
->groupType() == PlacesItem::SearchForType
||
752 item
->groupType() == PlacesItem::RecentlyAccessedType
) &&
753 !GeneralSettings::self()->globalViewProps();
754 if (createDefaultViewProperties
) {
755 ViewProperties
props(convertedUrl(data
.url
));
756 if (!props
.exist()) {
757 const QString path
= data
.url
.path();
758 if (path
== QLatin1String("/documents")) {
759 props
.setViewMode(DolphinView::DetailsView
);
760 props
.setPreviewsShown(false);
761 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "path");
762 } else if (path
== QLatin1String("/images")) {
763 props
.setViewMode(DolphinView::IconsView
);
764 props
.setPreviewsShown(true);
765 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "imageSize");
766 } else if (path
== QLatin1String("/audio")) {
767 props
.setViewMode(DolphinView::DetailsView
);
768 props
.setPreviewsShown(false);
769 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "artist" << "album");
770 } else if (path
== QLatin1String("/videos")) {
771 props
.setViewMode(DolphinView::IconsView
);
772 props
.setPreviewsShown(true);
773 props
.setVisibleRoles(QList
<QByteArray
>() << "text");
774 } else if (data
.url
.protocol() == "timeline") {
775 props
.setViewMode(DolphinView::DetailsView
);
776 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "date");
784 void PlacesItemModel::createSystemBookmarks()
786 Q_ASSERT(m_systemBookmarks
.isEmpty());
787 Q_ASSERT(m_systemBookmarksIndexes
.isEmpty());
789 const QString timeLineIcon
= "package_utility_time"; // TODO: Ask the Oxygen team to create
790 // a custom icon for the timeline-protocol
792 m_systemBookmarks
.append(SystemBookmarkData(KUrl(KUser().homeDir()),
794 i18nc("@item", "Home")));
795 m_systemBookmarks
.append(SystemBookmarkData(KUrl("remote:/"),
797 i18nc("@item", "Network")));
798 m_systemBookmarks
.append(SystemBookmarkData(KUrl("/"),
800 i18nc("@item", "Root")));
801 m_systemBookmarks
.append(SystemBookmarkData(KUrl("trash:/"),
803 i18nc("@item", "Trash")));
805 if (m_nepomukRunning
) {
806 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/today"),
808 i18nc("@item Recently Accessed", "Today")));
809 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/yesterday"),
811 i18nc("@item Recently Accessed", "Yesterday")));
812 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/thismonth"),
814 i18nc("@item Recently Accessed", "This Month")));
815 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/lastmonth"),
817 i18nc("@item Recently Accessed", "Last Month")));
818 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/documents"),
820 i18nc("@item Commonly Accessed", "Documents")));
821 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/images"),
823 i18nc("@item Commonly Accessed", "Images")));
824 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/audio"),
826 i18nc("@item Commonly Accessed", "Audio Files")));
827 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/videos"),
829 i18nc("@item Commonly Accessed", "Videos")));
832 for (int i
= 0; i
< m_systemBookmarks
.count(); ++i
) {
833 m_systemBookmarksIndexes
.insert(m_systemBookmarks
[i
].url
, i
);
837 void PlacesItemModel::initializeAvailableDevices()
839 m_predicate
= Solid::Predicate::fromString(
840 "[[[[ StorageVolume.ignored == false AND [ StorageVolume.usage == 'FileSystem' OR StorageVolume.usage == 'Encrypted' ]]"
842 "[ IS StorageAccess AND StorageDrive.driveType == 'Floppy' ]]"
844 "OpticalDisc.availableContent & 'Audio' ]"
846 "StorageAccess.ignored == false ]");
847 Q_ASSERT(m_predicate
.isValid());
849 Solid::DeviceNotifier
* notifier
= Solid::DeviceNotifier::instance();
850 connect(notifier
, SIGNAL(deviceAdded(QString
)), this, SLOT(slotDeviceAdded(QString
)));
851 connect(notifier
, SIGNAL(deviceRemoved(QString
)), this, SLOT(slotDeviceRemoved(QString
)));
853 const QList
<Solid::Device
>& deviceList
= Solid::Device::listFromQuery(m_predicate
);
854 foreach (const Solid::Device
& device
, deviceList
) {
855 m_availableDevices
<< device
.udi();
859 int PlacesItemModel::bookmarkIndex(int index
) const
861 int bookmarkIndex
= 0;
863 while (bookmarkIndex
< m_bookmarkedItems
.count()) {
864 if (!m_bookmarkedItems
[bookmarkIndex
]) {
865 if (modelIndex
== index
) {
873 return bookmarkIndex
>= m_bookmarkedItems
.count() ? -1 : bookmarkIndex
;
876 void PlacesItemModel::hideItem(int index
)
878 PlacesItem
* shownItem
= placesItem(index
);
883 shownItem
->setHidden(true);
884 if (m_hiddenItemsShown
) {
885 // Removing items from the model is not allowed if all hidden
886 // items should be shown.
890 const int newIndex
= bookmarkIndex(index
);
892 const KBookmark hiddenBookmark
= shownItem
->bookmark();
893 PlacesItem
* hiddenItem
= new PlacesItem(hiddenBookmark
);
895 const PlacesItem
* previousItem
= placesItem(index
- 1);
896 KBookmark previousBookmark
;
898 previousBookmark
= previousItem
->bookmark();
901 const bool updateBookmark
= (m_bookmarkManager
->root().indexOf(hiddenBookmark
) >= 0);
904 if (updateBookmark
) {
905 // removeItem() also removed the bookmark from m_bookmarkManager in
906 // PlacesItemModel::onItemRemoved(). However for hidden items the
907 // bookmark should still be remembered, so readd it again:
908 m_bookmarkManager
->root().addBookmark(hiddenBookmark
);
909 m_bookmarkManager
->root().moveBookmark(hiddenBookmark
, previousBookmark
);
910 triggerBookmarksSaving();
913 m_bookmarkedItems
.insert(newIndex
, hiddenItem
);
917 void PlacesItemModel::triggerBookmarksSaving()
919 if (m_saveBookmarksTimer
) {
920 m_saveBookmarksTimer
->start();
924 QString
PlacesItemModel::internalMimeType() const
926 return "application/x-dolphinplacesmodel-" +
927 QString::number((qptrdiff
)this);
930 int PlacesItemModel::groupedDropIndex(int index
, const PlacesItem
* item
) const
934 int dropIndex
= index
;
935 const PlacesItem::GroupType type
= item
->groupType();
937 const int itemCount
= count();
939 dropIndex
= itemCount
;
942 // Search nearest previous item with the same group
943 int previousIndex
= -1;
944 for (int i
= dropIndex
- 1; i
>= 0; --i
) {
945 if (placesItem(i
)->groupType() == type
) {
951 // Search nearest next item with the same group
953 for (int i
= dropIndex
; i
< count(); ++i
) {
954 if (placesItem(i
)->groupType() == type
) {
960 // Adjust the drop-index to be inserted to the
961 // nearest item with the same group.
962 if (previousIndex
>= 0 && nextIndex
>= 0) {
963 dropIndex
= (dropIndex
- previousIndex
< nextIndex
- dropIndex
) ?
964 previousIndex
+ 1 : nextIndex
;
965 } else if (previousIndex
>= 0) {
966 dropIndex
= previousIndex
+ 1;
967 } else if (nextIndex
>= 0) {
968 dropIndex
= nextIndex
;
974 bool PlacesItemModel::equalBookmarkIdentifiers(const KBookmark
& b1
, const KBookmark
& b2
)
976 const QString udi1
= b1
.metaDataItem("UDI");
977 const QString udi2
= b2
.metaDataItem("UDI");
978 if (!udi1
.isEmpty() && !udi2
.isEmpty()) {
981 return b1
.metaDataItem("ID") == b2
.metaDataItem("ID");
985 KUrl
PlacesItemModel::createTimelineUrl(const KUrl
& url
)
987 // TODO: Clarify with the Nepomuk-team whether it makes sense
988 // provide default-timeline-URLs like 'yesterday', 'this month'
992 const QString path
= url
.pathOrUrl();
993 if (path
.endsWith("yesterday")) {
994 const QDate date
= QDate::currentDate().addDays(-1);
995 const int year
= date
.year();
996 const int month
= date
.month();
997 const int day
= date
.day();
998 timelineUrl
= "timeline:/" + timelineDateString(year
, month
) +
999 '/' + timelineDateString(year
, month
, day
);
1000 } else if (path
.endsWith("thismonth")) {
1001 const QDate date
= QDate::currentDate();
1002 timelineUrl
= "timeline:/" + timelineDateString(date
.year(), date
.month());
1003 } else if (path
.endsWith("lastmonth")) {
1004 const QDate date
= QDate::currentDate().addMonths(-1);
1005 timelineUrl
= "timeline:/" + timelineDateString(date
.year(), date
.month());
1007 Q_ASSERT(path
.endsWith("today"));
1014 QString
PlacesItemModel::timelineDateString(int year
, int month
, int day
)
1016 QString date
= QString::number(year
) + '-';
1020 date
+= QString::number(month
);
1027 date
+= QString::number(day
);
1033 KUrl
PlacesItemModel::createSearchUrl(const KUrl
& url
)
1038 const QString path
= url
.pathOrUrl();
1039 if (path
.endsWith("documents")) {
1040 searchUrl
= searchUrlForTerm(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Document()));
1041 } else if (path
.endsWith("images")) {
1042 searchUrl
= searchUrlForTerm(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Image()));
1043 } else if (path
.endsWith("audio")) {
1044 searchUrl
= searchUrlForTerm(Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
1045 Nepomuk::Query::LiteralTerm("audio")));
1046 } else if (path
.endsWith("videos")) {
1047 searchUrl
= searchUrlForTerm(Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
1048 Nepomuk::Query::LiteralTerm("video")));
1060 KUrl
PlacesItemModel::searchUrlForTerm(const Nepomuk::Query::Term
& term
)
1062 const Nepomuk::Query::Query
query(term
);
1063 return query
.toSearchUrl();
1067 #ifdef PLACESITEMMODEL_DEBUG
1068 void PlacesItemModel::showModelState()
1070 kDebug() << "=================================";
1071 kDebug() << "Model:";
1072 kDebug() << "hidden-index model-index text";
1074 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
1075 if (m_bookmarkedItems
[i
]) {
1076 kDebug() << i
<< "(Hidden) " << " " << m_bookmarkedItems
[i
]->dataValue("text").toString();
1078 if (item(modelIndex
)) {
1079 kDebug() << i
<< " " << modelIndex
<< " " << item(modelIndex
)->dataValue("text").toString();
1081 kDebug() << i
<< " " << modelIndex
<< " " << "(not available yet)";
1088 kDebug() << "Bookmarks:";
1090 int bookmarkIndex
= 0;
1091 KBookmarkGroup root
= m_bookmarkManager
->root();
1092 KBookmark bookmark
= root
.first();
1093 while (!bookmark
.isNull()) {
1094 const QString udi
= bookmark
.metaDataItem("UDI");
1095 const QString text
= udi
.isEmpty() ? bookmark
.text() : udi
;
1096 if (bookmark
.metaDataItem("IsHidden") == QLatin1String("true")) {
1097 kDebug() << bookmarkIndex
<< "(Hidden)" << text
;
1099 kDebug() << bookmarkIndex
<< " " << text
;
1102 bookmark
= root
.next(bookmark
);
1108 #include "placesitemmodel.moc"