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
= 1000;
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
);
335 const QString internalMimeType
= "application/x-dolphinplacesmodel-" +
336 QString::number((qptrdiff
)this);
337 mimeData
->setData(internalMimeType
, itemData
);
342 bool PlacesItemModel::supportsDropping(int index
) const
348 KUrl
PlacesItemModel::convertedUrl(const KUrl
& url
)
351 if (url
.protocol() == QLatin1String("timeline")) {
352 newUrl
= createTimelineUrl(url
);
353 } else if (url
.protocol() == QLatin1String("search")) {
354 newUrl
= createSearchUrl(url
);
360 void PlacesItemModel::onItemInserted(int index
)
362 const PlacesItem
* insertedItem
= placesItem(index
);
364 // Take care to apply the PlacesItemModel-order of the inserted item
365 // also to the bookmark-manager.
366 const KBookmark insertedBookmark
= insertedItem
->bookmark();
368 const PlacesItem
* previousItem
= placesItem(index
- 1);
369 KBookmark previousBookmark
;
371 previousBookmark
= previousItem
->bookmark();
374 m_bookmarkManager
->root().moveBookmark(insertedBookmark
, previousBookmark
);
377 if (index
== count() - 1) {
378 // The item has been appended as last item to the list. In this
379 // case assure that it is also appended after the hidden items and
380 // not before (like done otherwise).
381 m_bookmarkedItems
.append(0);
385 int bookmarkIndex
= 0;
386 while (bookmarkIndex
< m_bookmarkedItems
.count()) {
387 if (!m_bookmarkedItems
[bookmarkIndex
]) {
389 if (modelIndex
+ 1 == index
) {
395 m_bookmarkedItems
.insert(bookmarkIndex
, 0);
398 triggerBookmarksSaving();
400 #ifdef PLACESITEMMODEL_DEBUG
401 kDebug() << "Inserted item" << index
;
406 void PlacesItemModel::onItemRemoved(int index
, KStandardItem
* removedItem
)
408 PlacesItem
* placesItem
= dynamic_cast<PlacesItem
*>(removedItem
);
410 const KBookmark bookmark
= placesItem
->bookmark();
411 m_bookmarkManager
->root().deleteBookmark(bookmark
);
414 const int boomarkIndex
= bookmarkIndex(index
);
415 Q_ASSERT(!m_bookmarkedItems
[boomarkIndex
]);
416 m_bookmarkedItems
.removeAt(boomarkIndex
);
418 triggerBookmarksSaving();
420 #ifdef PLACESITEMMODEL_DEBUG
421 kDebug() << "Removed item" << index
;
426 void PlacesItemModel::onItemChanged(int index
, const QSet
<QByteArray
>& changedRoles
)
428 const PlacesItem
* changedItem
= placesItem(index
);
430 // Take care to apply the PlacesItemModel-order of the changed item
431 // also to the bookmark-manager.
432 const KBookmark insertedBookmark
= changedItem
->bookmark();
434 const PlacesItem
* previousItem
= placesItem(index
- 1);
435 KBookmark previousBookmark
;
437 previousBookmark
= previousItem
->bookmark();
440 m_bookmarkManager
->root().moveBookmark(insertedBookmark
, previousBookmark
);
443 if (changedRoles
.contains("isHidden")) {
444 if (!m_hiddenItemsShown
&& changedItem
->isHidden()) {
445 m_hiddenItemToRemove
= index
;
446 QTimer::singleShot(0, this, SLOT(hideItem()));
450 triggerBookmarksSaving();
453 void PlacesItemModel::slotDeviceAdded(const QString
& udi
)
455 const Solid::Device
device(udi
);
456 if (m_predicate
.matches(device
)) {
457 m_availableDevices
<< udi
;
458 const KBookmark bookmark
= PlacesItem::createDeviceBookmark(m_bookmarkManager
, udi
);
459 appendItem(new PlacesItem(bookmark
));
463 void PlacesItemModel::slotDeviceRemoved(const QString
& udi
)
465 if (!m_availableDevices
.contains(udi
)) {
469 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
470 PlacesItem
* item
= m_bookmarkedItems
[i
];
471 if (item
&& item
->udi() == udi
) {
472 m_bookmarkedItems
.removeAt(i
);
478 for (int i
= 0; i
< count(); ++i
) {
479 if (placesItem(i
)->udi() == udi
) {
486 void PlacesItemModel::slotStorageTeardownDone(Solid::ErrorType error
, const QVariant
& errorData
)
488 if (error
&& errorData
.isValid()) {
489 emit
errorMessage(errorData
.toString());
493 void PlacesItemModel::hideItem()
495 hideItem(m_hiddenItemToRemove
);
496 m_hiddenItemToRemove
= -1;
499 void PlacesItemModel::updateBookmarks()
501 // Verify whether new bookmarks have been added or existing
502 // bookmarks have been changed.
503 KBookmarkGroup root
= m_bookmarkManager
->root();
504 KBookmark newBookmark
= root
.first();
505 while (!newBookmark
.isNull()) {
506 if (acceptBookmark(newBookmark
)) {
509 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
510 PlacesItem
* item
= m_bookmarkedItems
[i
];
512 item
= placesItem(modelIndex
);
516 const KBookmark oldBookmark
= item
->bookmark();
517 if (equalBookmarkIdentifiers(newBookmark
, oldBookmark
)) {
518 // The bookmark has been found in the model or as
519 // a hidden item. The content of the bookmark might
520 // have been changed, so an update is done.
522 if (newBookmark
.metaDataItem("UDI").isEmpty()) {
523 item
->setBookmark(newBookmark
);
530 PlacesItem
* item
= new PlacesItem(newBookmark
);
531 if (item
->isHidden() && !m_hiddenItemsShown
) {
532 m_bookmarkedItems
.append(item
);
539 newBookmark
= root
.next(newBookmark
);
542 // Remove items that are not part of the bookmark-manager anymore
544 for (int i
= m_bookmarkedItems
.count() - 1; i
>= 0; --i
) {
545 PlacesItem
* item
= m_bookmarkedItems
[i
];
546 const bool itemIsPartOfModel
= (item
== 0);
547 if (itemIsPartOfModel
) {
548 item
= placesItem(modelIndex
);
551 bool hasBeenRemoved
= true;
552 const KBookmark oldBookmark
= item
->bookmark();
553 KBookmark newBookmark
= root
.first();
554 while (!newBookmark
.isNull()) {
555 if (equalBookmarkIdentifiers(newBookmark
, oldBookmark
)) {
556 hasBeenRemoved
= false;
559 newBookmark
= root
.next(newBookmark
);
562 if (hasBeenRemoved
) {
563 if (m_bookmarkedItems
[i
]) {
564 delete m_bookmarkedItems
[i
];
565 m_bookmarkedItems
.removeAt(i
);
567 removeItem(modelIndex
);
572 if (itemIsPartOfModel
) {
578 void PlacesItemModel::saveBookmarks()
580 m_bookmarkManager
->emitChanged(m_bookmarkManager
->root());
583 void PlacesItemModel::loadBookmarks()
585 KBookmarkGroup root
= m_bookmarkManager
->root();
586 KBookmark bookmark
= root
.first();
587 QSet
<QString
> devices
= m_availableDevices
;
589 QSet
<KUrl
> missingSystemBookmarks
;
590 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
591 missingSystemBookmarks
.insert(data
.url
);
594 // The bookmarks might have a mixed order of places, devices and search-groups due
595 // to the compatibility with the KFilePlacesPanel. In Dolphin's places panel the
596 // items should always be collected in one group so the items are collected first
597 // in separate lists before inserting them.
598 QList
<PlacesItem
*> placesItems
;
599 QList
<PlacesItem
*> recentlyAccessedItems
;
600 QList
<PlacesItem
*> searchForItems
;
601 QList
<PlacesItem
*> devicesItems
;
603 while (!bookmark
.isNull()) {
604 const bool deviceAvailable
= devices
.remove(bookmark
.metaDataItem("UDI"));
605 if (acceptBookmark(bookmark
)) {
606 PlacesItem
* item
= new PlacesItem(bookmark
);
607 if (deviceAvailable
) {
608 devicesItems
.append(item
);
610 const KUrl url
= bookmark
.url();
611 if (missingSystemBookmarks
.contains(url
)) {
612 missingSystemBookmarks
.remove(url
);
614 // Apply the translated text to the system bookmarks, otherwise an outdated
615 // translation might be shown.
616 const int index
= m_systemBookmarksIndexes
.value(url
);
617 item
->setText(m_systemBookmarks
[index
].text
);
618 item
->setSystemItem(true);
621 switch (item
->groupType()) {
622 case PlacesItem::PlacesType
: placesItems
.append(item
); break;
623 case PlacesItem::RecentlyAccessedType
: recentlyAccessedItems
.append(item
); break;
624 case PlacesItem::SearchForType
: searchForItems
.append(item
); break;
625 case PlacesItem::DevicesType
:
626 default: Q_ASSERT(false); break;
631 bookmark
= root
.next(bookmark
);
634 if (!missingSystemBookmarks
.isEmpty()) {
635 // The current bookmarks don't contain all system-bookmarks. Add the missing
637 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
638 if (missingSystemBookmarks
.contains(data
.url
)) {
639 PlacesItem
* item
= createSystemPlacesItem(data
);
640 switch (item
->groupType()) {
641 case PlacesItem::PlacesType
: placesItems
.append(item
); break;
642 case PlacesItem::RecentlyAccessedType
: recentlyAccessedItems
.append(item
); break;
643 case PlacesItem::SearchForType
: searchForItems
.append(item
); break;
644 case PlacesItem::DevicesType
:
645 default: Q_ASSERT(false); break;
651 // Create items for devices that have not been stored as bookmark yet
652 foreach (const QString
& udi
, devices
) {
653 const KBookmark bookmark
= PlacesItem::createDeviceBookmark(m_bookmarkManager
, udi
);
654 devicesItems
.append(new PlacesItem(bookmark
));
657 QList
<PlacesItem
*> items
;
658 items
.append(placesItems
);
659 items
.append(recentlyAccessedItems
);
660 items
.append(searchForItems
);
661 items
.append(devicesItems
);
663 foreach (PlacesItem
* item
, items
) {
664 if (!m_hiddenItemsShown
&& item
->isHidden()) {
665 m_bookmarkedItems
.append(item
);
671 #ifdef PLACESITEMMODEL_DEBUG
672 kDebug() << "Loaded bookmarks";
677 bool PlacesItemModel::acceptBookmark(const KBookmark
& bookmark
) const
679 const QString udi
= bookmark
.metaDataItem("UDI");
680 const KUrl url
= bookmark
.url();
681 const QString appName
= bookmark
.metaDataItem("OnlyInApp");
682 const bool deviceAvailable
= m_availableDevices
.contains(udi
);
684 const bool allowedHere
= (appName
.isEmpty()
685 || appName
== KGlobal::mainComponent().componentName()
686 || appName
== KGlobal::mainComponent().componentName() + AppNamePrefix
)
687 && (m_nepomukRunning
|| (url
.protocol() != QLatin1String("timeline") &&
688 url
.protocol() != QLatin1String("search")));
690 return (udi
.isEmpty() && allowedHere
) || deviceAvailable
;
693 PlacesItem
* PlacesItemModel::createSystemPlacesItem(const SystemBookmarkData
& data
)
695 KBookmark bookmark
= PlacesItem::createBookmark(m_bookmarkManager
,
700 const QString protocol
= data
.url
.protocol();
701 if (protocol
== QLatin1String("timeline") || protocol
== QLatin1String("search")) {
702 // As long as the KFilePlacesView from kdelibs is available, the system-bookmarks
703 // for "Recently Accessed" and "Search For" should be a setting available only
704 // in the Places Panel (see description of AppNamePrefix for more details).
705 const QString appName
= KGlobal::mainComponent().componentName() + AppNamePrefix
;
706 bookmark
.setMetaDataItem("OnlyInApp", appName
);
709 PlacesItem
* item
= new PlacesItem(bookmark
);
710 item
->setSystemItem(true);
712 // Create default view-properties for all "Search For" and "Recently Accessed" bookmarks
713 // in case if the user has not already created custom view-properties for a corresponding
715 const bool createDefaultViewProperties
= (item
->groupType() == PlacesItem::SearchForType
||
716 item
->groupType() == PlacesItem::RecentlyAccessedType
) &&
717 !GeneralSettings::self()->globalViewProps();
718 if (createDefaultViewProperties
) {
719 ViewProperties
props(convertedUrl(data
.url
));
720 if (!props
.exist()) {
721 const QString path
= data
.url
.path();
722 if (path
== QLatin1String("/documents")) {
723 props
.setViewMode(DolphinView::DetailsView
);
724 props
.setPreviewsShown(false);
725 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "path");
726 } else if (path
== QLatin1String("/images")) {
727 props
.setViewMode(DolphinView::IconsView
);
728 props
.setPreviewsShown(true);
729 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "imageSize");
730 } else if (path
== QLatin1String("/audio")) {
731 props
.setViewMode(DolphinView::DetailsView
);
732 props
.setPreviewsShown(false);
733 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "artist" << "album");
734 } else if (path
== QLatin1String("/videos")) {
735 props
.setViewMode(DolphinView::IconsView
);
736 props
.setPreviewsShown(true);
737 props
.setVisibleRoles(QList
<QByteArray
>() << "text");
738 } else if (data
.url
.protocol() == "timeline") {
739 props
.setViewMode(DolphinView::DetailsView
);
740 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "date");
748 void PlacesItemModel::createSystemBookmarks()
750 Q_ASSERT(m_systemBookmarks
.isEmpty());
751 Q_ASSERT(m_systemBookmarksIndexes
.isEmpty());
753 const QString timeLineIcon
= "package_utility_time"; // TODO: Ask the Oxygen team to create
754 // a custom icon for the timeline-protocol
756 m_systemBookmarks
.append(SystemBookmarkData(KUrl(KUser().homeDir()),
758 i18nc("@item", "Home")));
759 m_systemBookmarks
.append(SystemBookmarkData(KUrl("remote:/"),
761 i18nc("@item", "Network")));
762 m_systemBookmarks
.append(SystemBookmarkData(KUrl("/"),
764 i18nc("@item", "Root")));
765 m_systemBookmarks
.append(SystemBookmarkData(KUrl("trash:/"),
767 i18nc("@item", "Trash")));
769 if (m_nepomukRunning
) {
770 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/today"),
772 i18nc("@item Recently Accessed", "Today")));
773 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/yesterday"),
775 i18nc("@item Recently Accessed", "Yesterday")));
776 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/thismonth"),
778 i18nc("@item Recently Accessed", "This Month")));
779 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/lastmonth"),
781 i18nc("@item Recently Accessed", "Last Month")));
782 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/documents"),
784 i18nc("@item Commonly Accessed", "Documents")));
785 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/images"),
787 i18nc("@item Commonly Accessed", "Images")));
788 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/audio"),
790 i18nc("@item Commonly Accessed", "Audio Files")));
791 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/videos"),
793 i18nc("@item Commonly Accessed", "Videos")));
796 for (int i
= 0; i
< m_systemBookmarks
.count(); ++i
) {
797 m_systemBookmarksIndexes
.insert(m_systemBookmarks
[i
].url
, i
);
801 void PlacesItemModel::initializeAvailableDevices()
803 m_predicate
= Solid::Predicate::fromString(
804 "[[[[ StorageVolume.ignored == false AND [ StorageVolume.usage == 'FileSystem' OR StorageVolume.usage == 'Encrypted' ]]"
806 "[ IS StorageAccess AND StorageDrive.driveType == 'Floppy' ]]"
808 "OpticalDisc.availableContent & 'Audio' ]"
810 "StorageAccess.ignored == false ]");
811 Q_ASSERT(m_predicate
.isValid());
813 Solid::DeviceNotifier
* notifier
= Solid::DeviceNotifier::instance();
814 connect(notifier
, SIGNAL(deviceAdded(QString
)), this, SLOT(slotDeviceAdded(QString
)));
815 connect(notifier
, SIGNAL(deviceRemoved(QString
)), this, SLOT(slotDeviceRemoved(QString
)));
817 const QList
<Solid::Device
>& deviceList
= Solid::Device::listFromQuery(m_predicate
);
818 foreach(const Solid::Device
& device
, deviceList
) {
819 m_availableDevices
<< device
.udi();
823 int PlacesItemModel::bookmarkIndex(int index
) const
825 int bookmarkIndex
= 0;
827 while (bookmarkIndex
< m_bookmarkedItems
.count()) {
828 if (!m_bookmarkedItems
[bookmarkIndex
]) {
829 if (modelIndex
== index
) {
837 return bookmarkIndex
>= m_bookmarkedItems
.count() ? -1 : bookmarkIndex
;
840 void PlacesItemModel::hideItem(int index
)
842 PlacesItem
* shownItem
= placesItem(index
);
847 shownItem
->setHidden(true);
848 if (m_hiddenItemsShown
) {
849 // Removing items from the model is not allowed if all hidden
850 // items should be shown.
854 const int newIndex
= bookmarkIndex(index
);
856 const KBookmark hiddenBookmark
= shownItem
->bookmark();
857 PlacesItem
* hiddenItem
= new PlacesItem(hiddenBookmark
);
859 const PlacesItem
* previousItem
= placesItem(index
- 1);
860 KBookmark previousBookmark
;
862 previousBookmark
= previousItem
->bookmark();
865 const bool updateBookmark
= (m_bookmarkManager
->root().indexOf(hiddenBookmark
) >= 0);
868 if (updateBookmark
) {
869 // removeItem() also removed the bookmark from m_bookmarkManager in
870 // PlacesItemModel::onItemRemoved(). However for hidden items the
871 // bookmark should still be remembered, so readd it again:
872 m_bookmarkManager
->root().addBookmark(hiddenBookmark
);
873 m_bookmarkManager
->root().moveBookmark(hiddenBookmark
, previousBookmark
);
874 triggerBookmarksSaving();
877 m_bookmarkedItems
.insert(newIndex
, hiddenItem
);
881 void PlacesItemModel::triggerBookmarksSaving()
883 if (m_saveBookmarksTimer
) {
884 m_saveBookmarksTimer
->start();
888 bool PlacesItemModel::equalBookmarkIdentifiers(const KBookmark
& b1
, const KBookmark
& b2
)
890 const QString udi1
= b1
.metaDataItem("UDI");
891 const QString udi2
= b2
.metaDataItem("UDI");
892 if (!udi1
.isEmpty() && !udi2
.isEmpty()) {
895 return b1
.metaDataItem("ID") == b2
.metaDataItem("ID");
899 KUrl
PlacesItemModel::createTimelineUrl(const KUrl
& url
)
901 // TODO: Clarify with the Nepomuk-team whether it makes sense
902 // provide default-timeline-URLs like 'yesterday', 'this month'
906 const QString path
= url
.pathOrUrl();
907 if (path
.endsWith("yesterday")) {
908 const QDate date
= QDate::currentDate().addDays(-1);
909 const int year
= date
.year();
910 const int month
= date
.month();
911 const int day
= date
.day();
912 timelineUrl
= "timeline:/" + timelineDateString(year
, month
) +
913 '/' + timelineDateString(year
, month
, day
);
914 } else if (path
.endsWith("thismonth")) {
915 const QDate date
= QDate::currentDate();
916 timelineUrl
= "timeline:/" + timelineDateString(date
.year(), date
.month());
917 } else if (path
.endsWith("lastmonth")) {
918 const QDate date
= QDate::currentDate().addMonths(-1);
919 timelineUrl
= "timeline:/" + timelineDateString(date
.year(), date
.month());
921 Q_ASSERT(path
.endsWith("today"));
928 QString
PlacesItemModel::timelineDateString(int year
, int month
, int day
)
930 QString date
= QString::number(year
) + '-';
934 date
+= QString::number(month
);
941 date
+= QString::number(day
);
947 KUrl
PlacesItemModel::createSearchUrl(const KUrl
& url
)
952 const QString path
= url
.pathOrUrl();
953 if (path
.endsWith("documents")) {
954 searchUrl
= searchUrlForTerm(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Document()));
955 } else if (path
.endsWith("images")) {
956 searchUrl
= searchUrlForTerm(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Image()));
957 } else if (path
.endsWith("audio")) {
958 searchUrl
= searchUrlForTerm(Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
959 Nepomuk::Query::LiteralTerm("audio")));
960 } else if (path
.endsWith("videos")) {
961 searchUrl
= searchUrlForTerm(Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
962 Nepomuk::Query::LiteralTerm("video")));
974 KUrl
PlacesItemModel::searchUrlForTerm(const Nepomuk::Query::Term
& term
)
976 const Nepomuk::Query::Query
query(term
);
977 return query
.toSearchUrl();
981 #ifdef PLACESITEMMODEL_DEBUG
982 void PlacesItemModel::showModelState()
984 kDebug() << "=================================";
985 kDebug() << "Model:";
986 kDebug() << "hidden-index model-index text";
988 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
989 if (m_bookmarkedItems
[i
]) {
990 kDebug() << i
<< "(Hidden) " << " " << m_bookmarkedItems
[i
]->dataValue("text").toString();
992 if (item(modelIndex
)) {
993 kDebug() << i
<< " " << modelIndex
<< " " << item(modelIndex
)->dataValue("text").toString();
995 kDebug() << i
<< " " << modelIndex
<< " " << "(not available yet)";
1002 kDebug() << "Bookmarks:";
1004 int bookmarkIndex
= 0;
1005 KBookmarkGroup root
= m_bookmarkManager
->root();
1006 KBookmark bookmark
= root
.first();
1007 while (!bookmark
.isNull()) {
1008 const QString udi
= bookmark
.metaDataItem("UDI");
1009 const QString text
= udi
.isEmpty() ? bookmark
.text() : udi
;
1010 if (bookmark
.metaDataItem("IsHidden") == QLatin1String("true")) {
1011 kDebug() << bookmarkIndex
<< "(Hidden)" << text
;
1013 kDebug() << bookmarkIndex
<< " " << text
;
1016 bookmark
= root
.next(bookmark
);
1022 #include "placesitemmodel.moc"