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 KUrl
PlacesItemModel::convertedUrl(const KUrl
& url
)
345 if (url
.protocol() == QLatin1String("timeline")) {
346 newUrl
= createTimelineUrl(url
);
347 } else if (url
.protocol() == QLatin1String("search")) {
348 newUrl
= createSearchUrl(url
);
354 void PlacesItemModel::onItemInserted(int index
)
356 const PlacesItem
* insertedItem
= placesItem(index
);
358 // Take care to apply the PlacesItemModel-order of the inserted item
359 // also to the bookmark-manager.
360 const KBookmark insertedBookmark
= insertedItem
->bookmark();
362 const PlacesItem
* previousItem
= placesItem(index
- 1);
363 KBookmark previousBookmark
;
365 previousBookmark
= previousItem
->bookmark();
368 m_bookmarkManager
->root().moveBookmark(insertedBookmark
, previousBookmark
);
371 if (index
== count() - 1) {
372 // The item has been appended as last item to the list. In this
373 // case assure that it is also appended after the hidden items and
374 // not before (like done otherwise).
375 m_bookmarkedItems
.append(0);
379 int bookmarkIndex
= 0;
380 while (bookmarkIndex
< m_bookmarkedItems
.count()) {
381 if (!m_bookmarkedItems
[bookmarkIndex
]) {
383 if (modelIndex
+ 1 == index
) {
389 m_bookmarkedItems
.insert(bookmarkIndex
, 0);
392 triggerBookmarksSaving();
394 #ifdef PLACESITEMMODEL_DEBUG
395 kDebug() << "Inserted item" << index
;
400 void PlacesItemModel::onItemRemoved(int index
, KStandardItem
* removedItem
)
402 PlacesItem
* placesItem
= dynamic_cast<PlacesItem
*>(removedItem
);
404 const KBookmark bookmark
= placesItem
->bookmark();
405 m_bookmarkManager
->root().deleteBookmark(bookmark
);
408 const int boomarkIndex
= bookmarkIndex(index
);
409 Q_ASSERT(!m_bookmarkedItems
[boomarkIndex
]);
410 m_bookmarkedItems
.removeAt(boomarkIndex
);
412 triggerBookmarksSaving();
414 #ifdef PLACESITEMMODEL_DEBUG
415 kDebug() << "Removed item" << index
;
420 void PlacesItemModel::onItemChanged(int index
, const QSet
<QByteArray
>& changedRoles
)
422 const PlacesItem
* changedItem
= placesItem(index
);
424 // Take care to apply the PlacesItemModel-order of the changed item
425 // also to the bookmark-manager.
426 const KBookmark insertedBookmark
= changedItem
->bookmark();
428 const PlacesItem
* previousItem
= placesItem(index
- 1);
429 KBookmark previousBookmark
;
431 previousBookmark
= previousItem
->bookmark();
434 m_bookmarkManager
->root().moveBookmark(insertedBookmark
, previousBookmark
);
437 if (changedRoles
.contains("isHidden")) {
438 if (!m_hiddenItemsShown
&& changedItem
->isHidden()) {
439 m_hiddenItemToRemove
= index
;
440 QTimer::singleShot(0, this, SLOT(hideItem()));
444 triggerBookmarksSaving();
447 void PlacesItemModel::slotDeviceAdded(const QString
& udi
)
449 const Solid::Device
device(udi
);
450 if (m_predicate
.matches(device
)) {
451 m_availableDevices
<< udi
;
452 const KBookmark bookmark
= PlacesItem::createDeviceBookmark(m_bookmarkManager
, udi
);
453 appendItem(new PlacesItem(bookmark
));
457 void PlacesItemModel::slotDeviceRemoved(const QString
& udi
)
459 if (!m_availableDevices
.contains(udi
)) {
463 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
464 PlacesItem
* item
= m_bookmarkedItems
[i
];
465 if (item
&& item
->udi() == udi
) {
466 m_bookmarkedItems
.removeAt(i
);
472 for (int i
= 0; i
< count(); ++i
) {
473 if (placesItem(i
)->udi() == udi
) {
480 void PlacesItemModel::slotStorageTeardownDone(Solid::ErrorType error
, const QVariant
& errorData
)
482 if (error
&& errorData
.isValid()) {
483 emit
errorMessage(errorData
.toString());
487 void PlacesItemModel::hideItem()
489 hideItem(m_hiddenItemToRemove
);
490 m_hiddenItemToRemove
= -1;
493 void PlacesItemModel::updateBookmarks()
495 // Verify whether new bookmarks have been added or existing
496 // bookmarks have been changed.
497 KBookmarkGroup root
= m_bookmarkManager
->root();
498 KBookmark newBookmark
= root
.first();
499 while (!newBookmark
.isNull()) {
500 if (acceptBookmark(newBookmark
)) {
503 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
504 PlacesItem
* item
= m_bookmarkedItems
[i
];
506 item
= placesItem(modelIndex
);
510 const KBookmark oldBookmark
= item
->bookmark();
511 if (equalBookmarkIdentifiers(newBookmark
, oldBookmark
)) {
512 // The bookmark has been found in the model or as
513 // a hidden item. The content of the bookmark might
514 // have been changed, so an update is done.
516 if (newBookmark
.metaDataItem("UDI").isEmpty()) {
517 item
->setBookmark(newBookmark
);
524 PlacesItem
* item
= new PlacesItem(newBookmark
);
525 if (item
->isHidden() && !m_hiddenItemsShown
) {
526 m_bookmarkedItems
.append(item
);
533 newBookmark
= root
.next(newBookmark
);
536 // Remove items that are not part of the bookmark-manager anymore
538 for (int i
= m_bookmarkedItems
.count() - 1; i
>= 0; --i
) {
539 PlacesItem
* item
= m_bookmarkedItems
[i
];
540 const bool itemIsPartOfModel
= (item
== 0);
541 if (itemIsPartOfModel
) {
542 item
= placesItem(modelIndex
);
545 bool hasBeenRemoved
= true;
546 const KBookmark oldBookmark
= item
->bookmark();
547 KBookmark newBookmark
= root
.first();
548 while (!newBookmark
.isNull()) {
549 if (equalBookmarkIdentifiers(newBookmark
, oldBookmark
)) {
550 hasBeenRemoved
= false;
553 newBookmark
= root
.next(newBookmark
);
556 if (hasBeenRemoved
) {
557 if (m_bookmarkedItems
[i
]) {
558 delete m_bookmarkedItems
[i
];
559 m_bookmarkedItems
.removeAt(i
);
561 removeItem(modelIndex
);
566 if (itemIsPartOfModel
) {
572 void PlacesItemModel::saveBookmarks()
574 m_bookmarkManager
->emitChanged(m_bookmarkManager
->root());
577 void PlacesItemModel::loadBookmarks()
579 KBookmarkGroup root
= m_bookmarkManager
->root();
580 KBookmark bookmark
= root
.first();
581 QSet
<QString
> devices
= m_availableDevices
;
583 QSet
<KUrl
> missingSystemBookmarks
;
584 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
585 missingSystemBookmarks
.insert(data
.url
);
588 // The bookmarks might have a mixed order of places, devices and search-groups due
589 // to the compatibility with the KFilePlacesPanel. In Dolphin's places panel the
590 // items should always be collected in one group so the items are collected first
591 // in separate lists before inserting them.
592 QList
<PlacesItem
*> placesItems
;
593 QList
<PlacesItem
*> recentlyAccessedItems
;
594 QList
<PlacesItem
*> searchForItems
;
595 QList
<PlacesItem
*> devicesItems
;
597 while (!bookmark
.isNull()) {
598 const bool deviceAvailable
= devices
.remove(bookmark
.metaDataItem("UDI"));
599 if (acceptBookmark(bookmark
)) {
600 PlacesItem
* item
= new PlacesItem(bookmark
);
601 if (deviceAvailable
) {
602 devicesItems
.append(item
);
604 const KUrl url
= bookmark
.url();
605 if (missingSystemBookmarks
.contains(url
)) {
606 missingSystemBookmarks
.remove(url
);
608 // Apply the translated text to the system bookmarks, otherwise an outdated
609 // translation might be shown.
610 const int index
= m_systemBookmarksIndexes
.value(url
);
611 item
->setText(m_systemBookmarks
[index
].text
);
612 item
->setSystemItem(true);
615 switch (item
->groupType()) {
616 case PlacesItem::PlacesType
: placesItems
.append(item
); break;
617 case PlacesItem::RecentlyAccessedType
: recentlyAccessedItems
.append(item
); break;
618 case PlacesItem::SearchForType
: searchForItems
.append(item
); break;
619 case PlacesItem::DevicesType
:
620 default: Q_ASSERT(false); break;
625 bookmark
= root
.next(bookmark
);
628 if (!missingSystemBookmarks
.isEmpty()) {
629 // The current bookmarks don't contain all system-bookmarks. Add the missing
631 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
632 if (missingSystemBookmarks
.contains(data
.url
)) {
633 PlacesItem
* item
= createSystemPlacesItem(data
);
634 switch (item
->groupType()) {
635 case PlacesItem::PlacesType
: placesItems
.append(item
); break;
636 case PlacesItem::RecentlyAccessedType
: recentlyAccessedItems
.append(item
); break;
637 case PlacesItem::SearchForType
: searchForItems
.append(item
); break;
638 case PlacesItem::DevicesType
:
639 default: Q_ASSERT(false); break;
645 // Create items for devices that have not been stored as bookmark yet
646 foreach (const QString
& udi
, devices
) {
647 const KBookmark bookmark
= PlacesItem::createDeviceBookmark(m_bookmarkManager
, udi
);
648 devicesItems
.append(new PlacesItem(bookmark
));
651 QList
<PlacesItem
*> items
;
652 items
.append(placesItems
);
653 items
.append(recentlyAccessedItems
);
654 items
.append(searchForItems
);
655 items
.append(devicesItems
);
657 foreach (PlacesItem
* item
, items
) {
658 if (!m_hiddenItemsShown
&& item
->isHidden()) {
659 m_bookmarkedItems
.append(item
);
665 #ifdef PLACESITEMMODEL_DEBUG
666 kDebug() << "Loaded bookmarks";
671 bool PlacesItemModel::acceptBookmark(const KBookmark
& bookmark
) const
673 const QString udi
= bookmark
.metaDataItem("UDI");
674 const KUrl url
= bookmark
.url();
675 const QString appName
= bookmark
.metaDataItem("OnlyInApp");
676 const bool deviceAvailable
= m_availableDevices
.contains(udi
);
678 const bool allowedHere
= (appName
.isEmpty()
679 || appName
== KGlobal::mainComponent().componentName()
680 || appName
== KGlobal::mainComponent().componentName() + AppNamePrefix
)
681 && (m_nepomukRunning
|| (url
.protocol() != QLatin1String("timeline") &&
682 url
.protocol() != QLatin1String("search")));
684 return (udi
.isEmpty() && allowedHere
) || deviceAvailable
;
687 PlacesItem
* PlacesItemModel::createSystemPlacesItem(const SystemBookmarkData
& data
)
689 KBookmark bookmark
= PlacesItem::createBookmark(m_bookmarkManager
,
694 const QString protocol
= data
.url
.protocol();
695 if (protocol
== QLatin1String("timeline") || protocol
== QLatin1String("search")) {
696 // As long as the KFilePlacesView from kdelibs is available, the system-bookmarks
697 // for "Recently Accessed" and "Search For" should be a setting available only
698 // in the Places Panel (see description of AppNamePrefix for more details).
699 const QString appName
= KGlobal::mainComponent().componentName() + AppNamePrefix
;
700 bookmark
.setMetaDataItem("OnlyInApp", appName
);
703 PlacesItem
* item
= new PlacesItem(bookmark
);
704 item
->setSystemItem(true);
706 // Create default view-properties for all "Search For" and "Recently Accessed" bookmarks
707 // in case if the user has not already created custom view-properties for a corresponding
709 const bool createDefaultViewProperties
= (item
->groupType() == PlacesItem::SearchForType
||
710 item
->groupType() == PlacesItem::RecentlyAccessedType
) &&
711 !GeneralSettings::self()->globalViewProps();
712 if (createDefaultViewProperties
) {
713 ViewProperties
props(convertedUrl(data
.url
));
714 if (!props
.exist()) {
715 const QString path
= data
.url
.path();
716 if (path
== QLatin1String("/documents")) {
717 props
.setViewMode(DolphinView::DetailsView
);
718 props
.setPreviewsShown(false);
719 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "path");
720 } else if (path
== QLatin1String("/images")) {
721 props
.setViewMode(DolphinView::IconsView
);
722 props
.setPreviewsShown(true);
723 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "imageSize");
724 } else if (path
== QLatin1String("/audio")) {
725 props
.setViewMode(DolphinView::DetailsView
);
726 props
.setPreviewsShown(false);
727 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "artist" << "album");
728 } else if (path
== QLatin1String("/videos")) {
729 props
.setViewMode(DolphinView::IconsView
);
730 props
.setPreviewsShown(true);
731 props
.setVisibleRoles(QList
<QByteArray
>() << "text");
732 } else if (data
.url
.protocol() == "timeline") {
733 props
.setViewMode(DolphinView::DetailsView
);
734 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "date");
742 void PlacesItemModel::createSystemBookmarks()
744 Q_ASSERT(m_systemBookmarks
.isEmpty());
745 Q_ASSERT(m_systemBookmarksIndexes
.isEmpty());
747 const QString timeLineIcon
= "package_utility_time"; // TODO: Ask the Oxygen team to create
748 // a custom icon for the timeline-protocol
750 m_systemBookmarks
.append(SystemBookmarkData(KUrl(KUser().homeDir()),
752 i18nc("@item", "Home")));
753 m_systemBookmarks
.append(SystemBookmarkData(KUrl("remote:/"),
755 i18nc("@item", "Network")));
756 m_systemBookmarks
.append(SystemBookmarkData(KUrl("/"),
758 i18nc("@item", "Root")));
759 m_systemBookmarks
.append(SystemBookmarkData(KUrl("trash:/"),
761 i18nc("@item", "Trash")));
763 if (m_nepomukRunning
) {
764 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/today"),
766 i18nc("@item Recently Accessed", "Today")));
767 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/yesterday"),
769 i18nc("@item Recently Accessed", "Yesterday")));
770 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/thismonth"),
772 i18nc("@item Recently Accessed", "This Month")));
773 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/lastmonth"),
775 i18nc("@item Recently Accessed", "Last Month")));
776 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/documents"),
778 i18nc("@item Commonly Accessed", "Documents")));
779 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/images"),
781 i18nc("@item Commonly Accessed", "Images")));
782 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/audio"),
784 i18nc("@item Commonly Accessed", "Audio Files")));
785 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/videos"),
787 i18nc("@item Commonly Accessed", "Videos")));
790 for (int i
= 0; i
< m_systemBookmarks
.count(); ++i
) {
791 m_systemBookmarksIndexes
.insert(m_systemBookmarks
[i
].url
, i
);
795 void PlacesItemModel::initializeAvailableDevices()
797 m_predicate
= Solid::Predicate::fromString(
798 "[[[[ StorageVolume.ignored == false AND [ StorageVolume.usage == 'FileSystem' OR StorageVolume.usage == 'Encrypted' ]]"
800 "[ IS StorageAccess AND StorageDrive.driveType == 'Floppy' ]]"
802 "OpticalDisc.availableContent & 'Audio' ]"
804 "StorageAccess.ignored == false ]");
805 Q_ASSERT(m_predicate
.isValid());
807 Solid::DeviceNotifier
* notifier
= Solid::DeviceNotifier::instance();
808 connect(notifier
, SIGNAL(deviceAdded(QString
)), this, SLOT(slotDeviceAdded(QString
)));
809 connect(notifier
, SIGNAL(deviceRemoved(QString
)), this, SLOT(slotDeviceRemoved(QString
)));
811 const QList
<Solid::Device
>& deviceList
= Solid::Device::listFromQuery(m_predicate
);
812 foreach(const Solid::Device
& device
, deviceList
) {
813 m_availableDevices
<< device
.udi();
817 int PlacesItemModel::bookmarkIndex(int index
) const
819 int bookmarkIndex
= 0;
821 while (bookmarkIndex
< m_bookmarkedItems
.count()) {
822 if (!m_bookmarkedItems
[bookmarkIndex
]) {
823 if (modelIndex
== index
) {
831 return bookmarkIndex
>= m_bookmarkedItems
.count() ? -1 : bookmarkIndex
;
834 void PlacesItemModel::hideItem(int index
)
836 PlacesItem
* shownItem
= placesItem(index
);
841 shownItem
->setHidden(true);
842 if (m_hiddenItemsShown
) {
843 // Removing items from the model is not allowed if all hidden
844 // items should be shown.
848 const int newIndex
= bookmarkIndex(index
);
850 const KBookmark hiddenBookmark
= shownItem
->bookmark();
851 PlacesItem
* hiddenItem
= new PlacesItem(hiddenBookmark
);
853 const PlacesItem
* previousItem
= placesItem(index
- 1);
854 KBookmark previousBookmark
;
856 previousBookmark
= previousItem
->bookmark();
859 const bool updateBookmark
= (m_bookmarkManager
->root().indexOf(hiddenBookmark
) >= 0);
862 if (updateBookmark
) {
863 // removeItem() also removed the bookmark from m_bookmarkManager in
864 // PlacesItemModel::onItemRemoved(). However for hidden items the
865 // bookmark should still be remembered, so readd it again:
866 m_bookmarkManager
->root().addBookmark(hiddenBookmark
);
867 m_bookmarkManager
->root().moveBookmark(hiddenBookmark
, previousBookmark
);
868 triggerBookmarksSaving();
871 m_bookmarkedItems
.insert(newIndex
, hiddenItem
);
875 void PlacesItemModel::triggerBookmarksSaving()
877 if (m_saveBookmarksTimer
) {
878 m_saveBookmarksTimer
->start();
882 bool PlacesItemModel::equalBookmarkIdentifiers(const KBookmark
& b1
, const KBookmark
& b2
)
884 const QString udi1
= b1
.metaDataItem("UDI");
885 const QString udi2
= b2
.metaDataItem("UDI");
886 if (!udi1
.isEmpty() && !udi2
.isEmpty()) {
889 return b1
.metaDataItem("ID") == b2
.metaDataItem("ID");
893 KUrl
PlacesItemModel::createTimelineUrl(const KUrl
& url
)
895 // TODO: Clarify with the Nepomuk-team whether it makes sense
896 // provide default-timeline-URLs like 'yesterday', 'this month'
900 const QString path
= url
.pathOrUrl();
901 if (path
.endsWith("yesterday")) {
902 const QDate date
= QDate::currentDate().addDays(-1);
903 const int year
= date
.year();
904 const int month
= date
.month();
905 const int day
= date
.day();
906 timelineUrl
= "timeline:/" + timelineDateString(year
, month
) +
907 '/' + timelineDateString(year
, month
, day
);
908 } else if (path
.endsWith("thismonth")) {
909 const QDate date
= QDate::currentDate();
910 timelineUrl
= "timeline:/" + timelineDateString(date
.year(), date
.month());
911 } else if (path
.endsWith("lastmonth")) {
912 const QDate date
= QDate::currentDate().addMonths(-1);
913 timelineUrl
= "timeline:/" + timelineDateString(date
.year(), date
.month());
915 Q_ASSERT(path
.endsWith("today"));
922 QString
PlacesItemModel::timelineDateString(int year
, int month
, int day
)
924 QString date
= QString::number(year
) + '-';
928 date
+= QString::number(month
);
935 date
+= QString::number(day
);
941 KUrl
PlacesItemModel::createSearchUrl(const KUrl
& url
)
946 const QString path
= url
.pathOrUrl();
947 if (path
.endsWith("documents")) {
948 searchUrl
= searchUrlForTerm(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Document()));
949 } else if (path
.endsWith("images")) {
950 searchUrl
= searchUrlForTerm(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Image()));
951 } else if (path
.endsWith("audio")) {
952 searchUrl
= searchUrlForTerm(Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
953 Nepomuk::Query::LiteralTerm("audio")));
954 } else if (path
.endsWith("videos")) {
955 searchUrl
= searchUrlForTerm(Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
956 Nepomuk::Query::LiteralTerm("video")));
968 KUrl
PlacesItemModel::searchUrlForTerm(const Nepomuk::Query::Term
& term
)
970 const Nepomuk::Query::Query
query(term
);
971 return query
.toSearchUrl();
975 #ifdef PLACESITEMMODEL_DEBUG
976 void PlacesItemModel::showModelState()
978 kDebug() << "=================================";
979 kDebug() << "Model:";
980 kDebug() << "hidden-index model-index text";
982 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
983 if (m_bookmarkedItems
[i
]) {
984 kDebug() << i
<< "(Hidden) " << " " << m_bookmarkedItems
[i
]->dataValue("text").toString();
986 if (item(modelIndex
)) {
987 kDebug() << i
<< " " << modelIndex
<< " " << item(modelIndex
)->dataValue("text").toString();
989 kDebug() << i
<< " " << modelIndex
<< " " << "(not available yet)";
996 kDebug() << "Bookmarks:";
998 int bookmarkIndex
= 0;
999 KBookmarkGroup root
= m_bookmarkManager
->root();
1000 KBookmark bookmark
= root
.first();
1001 while (!bookmark
.isNull()) {
1002 const QString udi
= bookmark
.metaDataItem("UDI");
1003 const QString text
= udi
.isEmpty() ? bookmark
.text() : udi
;
1004 if (bookmark
.metaDataItem("IsHidden") == QLatin1String("true")) {
1005 kDebug() << bookmarkIndex
<< "(Hidden)" << text
;
1007 kDebug() << bookmarkIndex
<< " " << text
;
1010 bookmark
= root
.next(bookmark
);
1016 #include "placesitemmodel.moc"