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 void PlacesItemModel::appendItemToGroup(PlacesItem
* item
)
236 while (i
< count() && placesItem(i
)->group() != item
->group()) {
240 bool inserted
= false;
241 while (!inserted
&& i
< count()) {
242 if (placesItem(i
)->group() != item
->group()) {
255 QAction
* PlacesItemModel::ejectAction(int index
) const
257 const PlacesItem
* item
= placesItem(index
);
258 if (item
&& item
->device().is
<Solid::OpticalDisc
>()) {
259 return new QAction(KIcon("media-eject"), i18nc("@item", "Eject '%1'", item
->text()), 0);
265 QAction
* PlacesItemModel::teardownAction(int index
) const
267 const PlacesItem
* item
= placesItem(index
);
272 Solid::Device device
= item
->device();
273 const bool providesTearDown
= device
.is
<Solid::StorageAccess
>() &&
274 device
.as
<Solid::StorageAccess
>()->isAccessible();
275 if (!providesTearDown
) {
279 Solid::StorageDrive
* drive
= device
.as
<Solid::StorageDrive
>();
281 drive
= device
.parent().as
<Solid::StorageDrive
>();
284 bool hotPluggable
= false;
285 bool removable
= false;
287 hotPluggable
= drive
->isHotpluggable();
288 removable
= drive
->isRemovable();
293 const QString label
= item
->text();
294 if (device
.is
<Solid::OpticalDisc
>()) {
295 text
= i18nc("@item", "Release '%1'", label
);
296 } else if (removable
|| hotPluggable
) {
297 text
= i18nc("@item", "Safely Remove '%1'", label
);
298 iconName
= "media-eject";
300 text
= i18nc("@item", "Unmount '%1'", label
);
301 iconName
= "media-eject";
304 if (iconName
.isEmpty()) {
305 return new QAction(text
, 0);
308 return new QAction(KIcon(iconName
), text
, 0);
311 void PlacesItemModel::requestEject(int index
)
313 const PlacesItem
* item
= placesItem(index
);
315 Solid::OpticalDrive
* drive
= item
->device().parent().as
<Solid::OpticalDrive
>();
317 connect(drive
, SIGNAL(ejectDone(Solid::ErrorType
,QVariant
,QString
)),
318 this, SLOT(slotStorageTeardownDone(Solid::ErrorType
,QVariant
)));
321 const QString label
= item
->text();
322 const QString message
= i18nc("@info", "The device '%1' is not a disk and cannot be ejected.", label
);
323 emit
errorMessage(message
);
328 void PlacesItemModel::requestTeardown(int index
)
330 const PlacesItem
* item
= placesItem(index
);
332 Solid::StorageAccess
* access
= item
->device().as
<Solid::StorageAccess
>();
334 connect(access
, SIGNAL(teardownDone(Solid::ErrorType
,QVariant
,QString
)),
335 this, SLOT(slotStorageTeardownDone(Solid::ErrorType
,QVariant
)));
341 QMimeData
* PlacesItemModel::createMimeData(const QSet
<int>& indexes
) const
346 QDataStream
stream(&itemData
, QIODevice::WriteOnly
);
348 foreach (int index
, indexes
) {
349 const KUrl itemUrl
= placesItem(index
)->url();
350 if (itemUrl
.isValid()) {
356 QMimeData
* mimeData
= new QMimeData();
357 if (!urls
.isEmpty()) {
358 urls
.populateMimeData(mimeData
);
360 mimeData
->setData(internalMimeType(), itemData
);
365 void PlacesItemModel::dropMimeData(int index
, const QMimeData
* mimeData
)
367 if (mimeData
->hasFormat(internalMimeType())) {
368 // The item has been moved inside the view
369 QByteArray itemData
= mimeData
->data(internalMimeType());
370 QDataStream
stream(&itemData
, QIODevice::ReadOnly
);
374 PlacesItem
* oldItem
= placesItem(oldIndex
);
379 PlacesItem
* newItem
= new PlacesItem(oldItem
->bookmark());
380 removeItem(oldIndex
);
382 if (oldIndex
<= index
) {
386 const int dropIndex
= groupedDropIndex(index
, newItem
);
387 insertItem(dropIndex
, newItem
);
388 } else if (mimeData
->hasFormat("text/uri-list")) {
389 // One or more items must be added to the model
390 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
391 for (int i
= urls
.count() - 1; i
>= 0; --i
) {
392 const KUrl
& url
= urls
[i
];
394 QString text
= url
.fileName();
395 if (text
.isEmpty()) {
399 PlacesItem
* newItem
= createPlacesItem(text
, url
);
400 const int dropIndex
= groupedDropIndex(index
, newItem
);
401 insertItem(dropIndex
, newItem
);
406 KUrl
PlacesItemModel::convertedUrl(const KUrl
& url
)
409 if (url
.protocol() == QLatin1String("timeline")) {
410 newUrl
= createTimelineUrl(url
);
411 } else if (url
.protocol() == QLatin1String("search")) {
412 newUrl
= createSearchUrl(url
);
418 void PlacesItemModel::onItemInserted(int index
)
420 const PlacesItem
* insertedItem
= placesItem(index
);
422 // Take care to apply the PlacesItemModel-order of the inserted item
423 // also to the bookmark-manager.
424 const KBookmark insertedBookmark
= insertedItem
->bookmark();
426 const PlacesItem
* previousItem
= placesItem(index
- 1);
427 KBookmark previousBookmark
;
429 previousBookmark
= previousItem
->bookmark();
432 m_bookmarkManager
->root().moveBookmark(insertedBookmark
, previousBookmark
);
435 if (index
== count() - 1) {
436 // The item has been appended as last item to the list. In this
437 // case assure that it is also appended after the hidden items and
438 // not before (like done otherwise).
439 m_bookmarkedItems
.append(0);
443 int bookmarkIndex
= 0;
444 while (bookmarkIndex
< m_bookmarkedItems
.count()) {
445 if (!m_bookmarkedItems
[bookmarkIndex
]) {
447 if (modelIndex
+ 1 == index
) {
453 m_bookmarkedItems
.insert(bookmarkIndex
, 0);
456 triggerBookmarksSaving();
458 #ifdef PLACESITEMMODEL_DEBUG
459 kDebug() << "Inserted item" << index
;
464 void PlacesItemModel::onItemRemoved(int index
, KStandardItem
* removedItem
)
466 PlacesItem
* placesItem
= dynamic_cast<PlacesItem
*>(removedItem
);
468 const KBookmark bookmark
= placesItem
->bookmark();
469 m_bookmarkManager
->root().deleteBookmark(bookmark
);
472 const int boomarkIndex
= bookmarkIndex(index
);
473 Q_ASSERT(!m_bookmarkedItems
[boomarkIndex
]);
474 m_bookmarkedItems
.removeAt(boomarkIndex
);
476 triggerBookmarksSaving();
478 #ifdef PLACESITEMMODEL_DEBUG
479 kDebug() << "Removed item" << index
;
484 void PlacesItemModel::onItemChanged(int index
, const QSet
<QByteArray
>& changedRoles
)
486 const PlacesItem
* changedItem
= placesItem(index
);
488 // Take care to apply the PlacesItemModel-order of the changed item
489 // also to the bookmark-manager.
490 const KBookmark insertedBookmark
= changedItem
->bookmark();
492 const PlacesItem
* previousItem
= placesItem(index
- 1);
493 KBookmark previousBookmark
;
495 previousBookmark
= previousItem
->bookmark();
498 m_bookmarkManager
->root().moveBookmark(insertedBookmark
, previousBookmark
);
501 if (changedRoles
.contains("isHidden")) {
502 if (!m_hiddenItemsShown
&& changedItem
->isHidden()) {
503 m_hiddenItemToRemove
= index
;
504 QTimer::singleShot(0, this, SLOT(hideItem()));
508 triggerBookmarksSaving();
511 void PlacesItemModel::slotDeviceAdded(const QString
& udi
)
513 const Solid::Device
device(udi
);
514 if (m_predicate
.matches(device
)) {
515 m_availableDevices
<< udi
;
516 const KBookmark bookmark
= PlacesItem::createDeviceBookmark(m_bookmarkManager
, udi
);
517 appendItem(new PlacesItem(bookmark
));
521 void PlacesItemModel::slotDeviceRemoved(const QString
& udi
)
523 if (!m_availableDevices
.contains(udi
)) {
527 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
528 PlacesItem
* item
= m_bookmarkedItems
[i
];
529 if (item
&& item
->udi() == udi
) {
530 m_bookmarkedItems
.removeAt(i
);
536 for (int i
= 0; i
< count(); ++i
) {
537 if (placesItem(i
)->udi() == udi
) {
544 void PlacesItemModel::slotStorageTeardownDone(Solid::ErrorType error
, const QVariant
& errorData
)
546 if (error
&& errorData
.isValid()) {
547 emit
errorMessage(errorData
.toString());
551 void PlacesItemModel::hideItem()
553 hideItem(m_hiddenItemToRemove
);
554 m_hiddenItemToRemove
= -1;
557 void PlacesItemModel::updateBookmarks()
559 // Verify whether new bookmarks have been added or existing
560 // bookmarks have been changed.
561 KBookmarkGroup root
= m_bookmarkManager
->root();
562 KBookmark newBookmark
= root
.first();
563 while (!newBookmark
.isNull()) {
564 if (acceptBookmark(newBookmark
)) {
567 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
568 PlacesItem
* item
= m_bookmarkedItems
[i
];
570 item
= placesItem(modelIndex
);
574 const KBookmark oldBookmark
= item
->bookmark();
575 if (equalBookmarkIdentifiers(newBookmark
, oldBookmark
)) {
576 // The bookmark has been found in the model or as
577 // a hidden item. The content of the bookmark might
578 // have been changed, so an update is done.
580 if (newBookmark
.metaDataItem("UDI").isEmpty()) {
581 item
->setBookmark(newBookmark
);
588 PlacesItem
* item
= new PlacesItem(newBookmark
);
589 if (item
->isHidden() && !m_hiddenItemsShown
) {
590 m_bookmarkedItems
.append(item
);
592 appendItemToGroup(item
);
597 newBookmark
= root
.next(newBookmark
);
600 // Remove items that are not part of the bookmark-manager anymore
602 for (int i
= m_bookmarkedItems
.count() - 1; i
>= 0; --i
) {
603 PlacesItem
* item
= m_bookmarkedItems
[i
];
604 const bool itemIsPartOfModel
= (item
== 0);
605 if (itemIsPartOfModel
) {
606 item
= placesItem(modelIndex
);
609 bool hasBeenRemoved
= true;
610 const KBookmark oldBookmark
= item
->bookmark();
611 KBookmark newBookmark
= root
.first();
612 while (!newBookmark
.isNull()) {
613 if (equalBookmarkIdentifiers(newBookmark
, oldBookmark
)) {
614 hasBeenRemoved
= false;
617 newBookmark
= root
.next(newBookmark
);
620 if (hasBeenRemoved
) {
621 if (m_bookmarkedItems
[i
]) {
622 delete m_bookmarkedItems
[i
];
623 m_bookmarkedItems
.removeAt(i
);
625 removeItem(modelIndex
);
630 if (itemIsPartOfModel
) {
636 void PlacesItemModel::saveBookmarks()
638 m_bookmarkManager
->emitChanged(m_bookmarkManager
->root());
641 void PlacesItemModel::loadBookmarks()
643 KBookmarkGroup root
= m_bookmarkManager
->root();
644 KBookmark bookmark
= root
.first();
645 QSet
<QString
> devices
= m_availableDevices
;
647 QSet
<KUrl
> missingSystemBookmarks
;
648 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
649 missingSystemBookmarks
.insert(data
.url
);
652 // The bookmarks might have a mixed order of places, devices and search-groups due
653 // to the compatibility with the KFilePlacesPanel. In Dolphin's places panel the
654 // items should always be collected in one group so the items are collected first
655 // in separate lists before inserting them.
656 QList
<PlacesItem
*> placesItems
;
657 QList
<PlacesItem
*> recentlyAccessedItems
;
658 QList
<PlacesItem
*> searchForItems
;
659 QList
<PlacesItem
*> devicesItems
;
661 while (!bookmark
.isNull()) {
662 if (acceptBookmark(bookmark
)) {
663 PlacesItem
* item
= new PlacesItem(bookmark
);
664 if (item
->groupType() == PlacesItem::DevicesType
) {
665 devices
.remove(item
->udi());
666 devicesItems
.append(item
);
668 const KUrl url
= bookmark
.url();
669 if (missingSystemBookmarks
.contains(url
)) {
670 missingSystemBookmarks
.remove(url
);
672 // Apply the translated text to the system bookmarks, otherwise an outdated
673 // translation might be shown.
674 const int index
= m_systemBookmarksIndexes
.value(url
);
675 item
->setText(m_systemBookmarks
[index
].text
);
676 item
->setSystemItem(true);
679 switch (item
->groupType()) {
680 case PlacesItem::PlacesType
: placesItems
.append(item
); break;
681 case PlacesItem::RecentlyAccessedType
: recentlyAccessedItems
.append(item
); break;
682 case PlacesItem::SearchForType
: searchForItems
.append(item
); break;
683 case PlacesItem::DevicesType
:
684 default: Q_ASSERT(false); break;
689 bookmark
= root
.next(bookmark
);
692 if (!missingSystemBookmarks
.isEmpty()) {
693 // The current bookmarks don't contain all system-bookmarks. Add the missing
695 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
696 if (missingSystemBookmarks
.contains(data
.url
)) {
697 PlacesItem
* item
= createSystemPlacesItem(data
);
698 switch (item
->groupType()) {
699 case PlacesItem::PlacesType
: placesItems
.append(item
); break;
700 case PlacesItem::RecentlyAccessedType
: recentlyAccessedItems
.append(item
); break;
701 case PlacesItem::SearchForType
: searchForItems
.append(item
); break;
702 case PlacesItem::DevicesType
:
703 default: Q_ASSERT(false); break;
709 // Create items for devices that have not been stored as bookmark yet
710 foreach (const QString
& udi
, devices
) {
711 const KBookmark bookmark
= PlacesItem::createDeviceBookmark(m_bookmarkManager
, udi
);
712 devicesItems
.append(new PlacesItem(bookmark
));
715 QList
<PlacesItem
*> items
;
716 items
.append(placesItems
);
717 items
.append(recentlyAccessedItems
);
718 items
.append(searchForItems
);
719 items
.append(devicesItems
);
721 foreach (PlacesItem
* item
, items
) {
722 if (!m_hiddenItemsShown
&& item
->isHidden()) {
723 m_bookmarkedItems
.append(item
);
729 #ifdef PLACESITEMMODEL_DEBUG
730 kDebug() << "Loaded bookmarks";
735 bool PlacesItemModel::acceptBookmark(const KBookmark
& bookmark
) const
737 const QString udi
= bookmark
.metaDataItem("UDI");
738 const KUrl url
= bookmark
.url();
739 const QString appName
= bookmark
.metaDataItem("OnlyInApp");
740 const bool deviceAvailable
= m_availableDevices
.contains(udi
);
742 const bool allowedHere
= (appName
.isEmpty()
743 || appName
== KGlobal::mainComponent().componentName()
744 || appName
== KGlobal::mainComponent().componentName() + AppNamePrefix
)
745 && (m_nepomukRunning
|| (url
.protocol() != QLatin1String("timeline") &&
746 url
.protocol() != QLatin1String("search")));
748 return (udi
.isEmpty() && allowedHere
) || deviceAvailable
;
751 PlacesItem
* PlacesItemModel::createSystemPlacesItem(const SystemBookmarkData
& data
)
753 KBookmark bookmark
= PlacesItem::createBookmark(m_bookmarkManager
,
758 const QString protocol
= data
.url
.protocol();
759 if (protocol
== QLatin1String("timeline") || protocol
== QLatin1String("search")) {
760 // As long as the KFilePlacesView from kdelibs is available, the system-bookmarks
761 // for "Recently Accessed" and "Search For" should be a setting available only
762 // in the Places Panel (see description of AppNamePrefix for more details).
763 const QString appName
= KGlobal::mainComponent().componentName() + AppNamePrefix
;
764 bookmark
.setMetaDataItem("OnlyInApp", appName
);
767 PlacesItem
* item
= new PlacesItem(bookmark
);
768 item
->setSystemItem(true);
770 // Create default view-properties for all "Search For" and "Recently Accessed" bookmarks
771 // in case if the user has not already created custom view-properties for a corresponding
773 const bool createDefaultViewProperties
= (item
->groupType() == PlacesItem::SearchForType
||
774 item
->groupType() == PlacesItem::RecentlyAccessedType
) &&
775 !GeneralSettings::self()->globalViewProps();
776 if (createDefaultViewProperties
) {
777 ViewProperties
props(convertedUrl(data
.url
));
778 if (!props
.exist()) {
779 const QString path
= data
.url
.path();
780 if (path
== QLatin1String("/documents")) {
781 props
.setViewMode(DolphinView::DetailsView
);
782 props
.setPreviewsShown(false);
783 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "path");
784 } else if (path
== QLatin1String("/images")) {
785 props
.setViewMode(DolphinView::IconsView
);
786 props
.setPreviewsShown(true);
787 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "imageSize");
788 } else if (path
== QLatin1String("/audio")) {
789 props
.setViewMode(DolphinView::DetailsView
);
790 props
.setPreviewsShown(false);
791 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "artist" << "album");
792 } else if (path
== QLatin1String("/videos")) {
793 props
.setViewMode(DolphinView::IconsView
);
794 props
.setPreviewsShown(true);
795 props
.setVisibleRoles(QList
<QByteArray
>() << "text");
796 } else if (data
.url
.protocol() == "timeline") {
797 props
.setViewMode(DolphinView::DetailsView
);
798 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "date");
806 void PlacesItemModel::createSystemBookmarks()
808 Q_ASSERT(m_systemBookmarks
.isEmpty());
809 Q_ASSERT(m_systemBookmarksIndexes
.isEmpty());
811 const QString timeLineIcon
= "package_utility_time"; // TODO: Ask the Oxygen team to create
812 // a custom icon for the timeline-protocol
814 m_systemBookmarks
.append(SystemBookmarkData(KUrl(KUser().homeDir()),
816 i18nc("@item", "Home")));
817 m_systemBookmarks
.append(SystemBookmarkData(KUrl("remote:/"),
819 i18nc("@item", "Network")));
820 m_systemBookmarks
.append(SystemBookmarkData(KUrl("/"),
822 i18nc("@item", "Root")));
823 m_systemBookmarks
.append(SystemBookmarkData(KUrl("trash:/"),
825 i18nc("@item", "Trash")));
827 if (m_nepomukRunning
) {
828 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/today"),
830 i18nc("@item Recently Accessed", "Today")));
831 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/yesterday"),
833 i18nc("@item Recently Accessed", "Yesterday")));
834 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/thismonth"),
836 i18nc("@item Recently Accessed", "This Month")));
837 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/lastmonth"),
839 i18nc("@item Recently Accessed", "Last Month")));
840 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/documents"),
842 i18nc("@item Commonly Accessed", "Documents")));
843 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/images"),
845 i18nc("@item Commonly Accessed", "Images")));
846 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/audio"),
848 i18nc("@item Commonly Accessed", "Audio Files")));
849 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/videos"),
851 i18nc("@item Commonly Accessed", "Videos")));
854 for (int i
= 0; i
< m_systemBookmarks
.count(); ++i
) {
855 m_systemBookmarksIndexes
.insert(m_systemBookmarks
[i
].url
, i
);
859 void PlacesItemModel::initializeAvailableDevices()
861 m_predicate
= Solid::Predicate::fromString(
862 "[[[[ StorageVolume.ignored == false AND [ StorageVolume.usage == 'FileSystem' OR StorageVolume.usage == 'Encrypted' ]]"
864 "[ IS StorageAccess AND StorageDrive.driveType == 'Floppy' ]]"
866 "OpticalDisc.availableContent & 'Audio' ]"
868 "StorageAccess.ignored == false ]");
869 Q_ASSERT(m_predicate
.isValid());
871 Solid::DeviceNotifier
* notifier
= Solid::DeviceNotifier::instance();
872 connect(notifier
, SIGNAL(deviceAdded(QString
)), this, SLOT(slotDeviceAdded(QString
)));
873 connect(notifier
, SIGNAL(deviceRemoved(QString
)), this, SLOT(slotDeviceRemoved(QString
)));
875 const QList
<Solid::Device
>& deviceList
= Solid::Device::listFromQuery(m_predicate
);
876 foreach (const Solid::Device
& device
, deviceList
) {
877 m_availableDevices
<< device
.udi();
881 int PlacesItemModel::bookmarkIndex(int index
) const
883 int bookmarkIndex
= 0;
885 while (bookmarkIndex
< m_bookmarkedItems
.count()) {
886 if (!m_bookmarkedItems
[bookmarkIndex
]) {
887 if (modelIndex
== index
) {
895 return bookmarkIndex
>= m_bookmarkedItems
.count() ? -1 : bookmarkIndex
;
898 void PlacesItemModel::hideItem(int index
)
900 PlacesItem
* shownItem
= placesItem(index
);
905 shownItem
->setHidden(true);
906 if (m_hiddenItemsShown
) {
907 // Removing items from the model is not allowed if all hidden
908 // items should be shown.
912 const int newIndex
= bookmarkIndex(index
);
914 const KBookmark hiddenBookmark
= shownItem
->bookmark();
915 PlacesItem
* hiddenItem
= new PlacesItem(hiddenBookmark
);
917 const PlacesItem
* previousItem
= placesItem(index
- 1);
918 KBookmark previousBookmark
;
920 previousBookmark
= previousItem
->bookmark();
923 const bool updateBookmark
= (m_bookmarkManager
->root().indexOf(hiddenBookmark
) >= 0);
926 if (updateBookmark
) {
927 // removeItem() also removed the bookmark from m_bookmarkManager in
928 // PlacesItemModel::onItemRemoved(). However for hidden items the
929 // bookmark should still be remembered, so readd it again:
930 m_bookmarkManager
->root().addBookmark(hiddenBookmark
);
931 m_bookmarkManager
->root().moveBookmark(hiddenBookmark
, previousBookmark
);
932 triggerBookmarksSaving();
935 m_bookmarkedItems
.insert(newIndex
, hiddenItem
);
939 void PlacesItemModel::triggerBookmarksSaving()
941 if (m_saveBookmarksTimer
) {
942 m_saveBookmarksTimer
->start();
946 QString
PlacesItemModel::internalMimeType() const
948 return "application/x-dolphinplacesmodel-" +
949 QString::number((qptrdiff
)this);
952 int PlacesItemModel::groupedDropIndex(int index
, const PlacesItem
* item
) const
956 int dropIndex
= index
;
957 const PlacesItem::GroupType type
= item
->groupType();
959 const int itemCount
= count();
961 dropIndex
= itemCount
;
964 // Search nearest previous item with the same group
965 int previousIndex
= -1;
966 for (int i
= dropIndex
- 1; i
>= 0; --i
) {
967 if (placesItem(i
)->groupType() == type
) {
973 // Search nearest next item with the same group
975 for (int i
= dropIndex
; i
< count(); ++i
) {
976 if (placesItem(i
)->groupType() == type
) {
982 // Adjust the drop-index to be inserted to the
983 // nearest item with the same group.
984 if (previousIndex
>= 0 && nextIndex
>= 0) {
985 dropIndex
= (dropIndex
- previousIndex
< nextIndex
- dropIndex
) ?
986 previousIndex
+ 1 : nextIndex
;
987 } else if (previousIndex
>= 0) {
988 dropIndex
= previousIndex
+ 1;
989 } else if (nextIndex
>= 0) {
990 dropIndex
= nextIndex
;
996 bool PlacesItemModel::equalBookmarkIdentifiers(const KBookmark
& b1
, const KBookmark
& b2
)
998 const QString udi1
= b1
.metaDataItem("UDI");
999 const QString udi2
= b2
.metaDataItem("UDI");
1000 if (!udi1
.isEmpty() && !udi2
.isEmpty()) {
1001 return udi1
== udi2
;
1003 return b1
.metaDataItem("ID") == b2
.metaDataItem("ID");
1007 KUrl
PlacesItemModel::createTimelineUrl(const KUrl
& url
)
1009 // TODO: Clarify with the Nepomuk-team whether it makes sense
1010 // provide default-timeline-URLs like 'yesterday', 'this month'
1011 // and 'last month'.
1014 const QString path
= url
.pathOrUrl();
1015 if (path
.endsWith("yesterday")) {
1016 const QDate date
= QDate::currentDate().addDays(-1);
1017 const int year
= date
.year();
1018 const int month
= date
.month();
1019 const int day
= date
.day();
1020 timelineUrl
= "timeline:/" + timelineDateString(year
, month
) +
1021 '/' + timelineDateString(year
, month
, day
);
1022 } else if (path
.endsWith("thismonth")) {
1023 const QDate date
= QDate::currentDate();
1024 timelineUrl
= "timeline:/" + timelineDateString(date
.year(), date
.month());
1025 } else if (path
.endsWith("lastmonth")) {
1026 const QDate date
= QDate::currentDate().addMonths(-1);
1027 timelineUrl
= "timeline:/" + timelineDateString(date
.year(), date
.month());
1029 Q_ASSERT(path
.endsWith("today"));
1036 QString
PlacesItemModel::timelineDateString(int year
, int month
, int day
)
1038 QString date
= QString::number(year
) + '-';
1042 date
+= QString::number(month
);
1049 date
+= QString::number(day
);
1055 KUrl
PlacesItemModel::createSearchUrl(const KUrl
& url
)
1060 const QString path
= url
.pathOrUrl();
1061 if (path
.endsWith("documents")) {
1062 searchUrl
= searchUrlForTerm(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Document()));
1063 } else if (path
.endsWith("images")) {
1064 searchUrl
= searchUrlForTerm(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Image()));
1065 } else if (path
.endsWith("audio")) {
1066 searchUrl
= searchUrlForTerm(Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
1067 Nepomuk::Query::LiteralTerm("audio")));
1068 } else if (path
.endsWith("videos")) {
1069 searchUrl
= searchUrlForTerm(Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
1070 Nepomuk::Query::LiteralTerm("video")));
1082 KUrl
PlacesItemModel::searchUrlForTerm(const Nepomuk::Query::Term
& term
)
1084 const Nepomuk::Query::Query
query(term
);
1085 return query
.toSearchUrl();
1089 #ifdef PLACESITEMMODEL_DEBUG
1090 void PlacesItemModel::showModelState()
1092 kDebug() << "=================================";
1093 kDebug() << "Model:";
1094 kDebug() << "hidden-index model-index text";
1096 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
1097 if (m_bookmarkedItems
[i
]) {
1098 kDebug() << i
<< "(Hidden) " << " " << m_bookmarkedItems
[i
]->dataValue("text").toString();
1100 if (item(modelIndex
)) {
1101 kDebug() << i
<< " " << modelIndex
<< " " << item(modelIndex
)->dataValue("text").toString();
1103 kDebug() << i
<< " " << modelIndex
<< " " << "(not available yet)";
1110 kDebug() << "Bookmarks:";
1112 int bookmarkIndex
= 0;
1113 KBookmarkGroup root
= m_bookmarkManager
->root();
1114 KBookmark bookmark
= root
.first();
1115 while (!bookmark
.isNull()) {
1116 const QString udi
= bookmark
.metaDataItem("UDI");
1117 const QString text
= udi
.isEmpty() ? bookmark
.text() : udi
;
1118 if (bookmark
.metaDataItem("IsHidden") == QLatin1String("true")) {
1119 kDebug() << bookmarkIndex
<< "(Hidden)" << text
;
1121 kDebug() << bookmarkIndex
<< " " << text
;
1124 bookmark
= root
.next(bookmark
);
1130 #include "placesitemmodel.moc"