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"
42 #include <Solid/Device>
43 #include <Solid/DeviceNotifier>
44 #include <Solid/OpticalDisc>
45 #include <Solid/OpticalDrive>
46 #include <Solid/StorageAccess>
47 #include <Solid/StorageDrive>
49 #include <views/dolphinview.h>
50 #include <views/viewproperties.h>
53 #include <Nepomuk/ResourceManager>
54 #include <Nepomuk/Query/ComparisonTerm>
55 #include <Nepomuk/Query/LiteralTerm>
56 #include <Nepomuk/Query/Query>
57 #include <Nepomuk/Query/ResourceTypeTerm>
58 #include <Nepomuk/Vocabulary/NFO>
59 #include <Nepomuk/Vocabulary/NIE>
63 // As long as KFilePlacesView from kdelibs is available in parallel, the
64 // system-bookmarks for "Recently Accessed" and "Search For" should be
65 // shown only inside the Places Panel. This is necessary as the stored
66 // URLs needs to get translated to a Nepomuk-search-URL on-the-fly to
67 // be independent from changes in the Nepomuk-search-URL-syntax.
68 // Hence a prefix to the application-name of the stored bookmarks is
69 // added, which is only read by PlacesItemModel.
70 const char* AppNamePrefix
= "-places-panel";
73 PlacesItemModel::PlacesItemModel(QObject
* parent
) :
74 KStandardItemModel(parent
),
75 m_nepomukRunning(false),
76 m_hiddenItemsShown(false),
81 m_systemBookmarksIndexes(),
83 m_hiddenItemToRemove(-1),
84 m_saveBookmarksTimer(0),
85 m_updateBookmarksTimer(0)
88 m_nepomukRunning
= (Nepomuk::ResourceManager::instance()->initialized());
90 const QString file
= KStandardDirs::locateLocal("data", "kfileplaces/bookmarks.xml");
91 m_bookmarkManager
= KBookmarkManager::managerForFile(file
, "kfilePlaces");
93 createSystemBookmarks();
94 initializeAvailableDevices();
97 const int syncBookmarksTimeout
= 1000;
99 m_saveBookmarksTimer
= new QTimer(this);
100 m_saveBookmarksTimer
->setInterval(syncBookmarksTimeout
);
101 m_saveBookmarksTimer
->setSingleShot(true);
102 connect(m_saveBookmarksTimer
, SIGNAL(timeout()), this, SLOT(saveBookmarks()));
104 m_updateBookmarksTimer
= new QTimer(this);
105 m_updateBookmarksTimer
->setInterval(syncBookmarksTimeout
);
106 m_updateBookmarksTimer
->setSingleShot(true);
107 connect(m_updateBookmarksTimer
, SIGNAL(timeout()), this, SLOT(updateBookmarks()));
109 connect(m_bookmarkManager
, SIGNAL(changed(QString
,QString
)),
110 m_updateBookmarksTimer
, SLOT(start()));
111 connect(m_bookmarkManager
, SIGNAL(bookmarksChanged(QString
)),
112 m_updateBookmarksTimer
, SLOT(start()));
115 PlacesItemModel::~PlacesItemModel()
118 qDeleteAll(m_bookmarkedItems
);
119 m_bookmarkedItems
.clear();
122 PlacesItem
* PlacesItemModel::createPlacesItem(const QString
& text
,
124 const QString
& iconName
)
126 const KBookmark bookmark
= PlacesItem::createBookmark(m_bookmarkManager
, text
, url
, iconName
);
127 return new PlacesItem(bookmark
);
130 PlacesItem
* PlacesItemModel::placesItem(int index
) const
132 return dynamic_cast<PlacesItem
*>(item(index
));
135 int PlacesItemModel::hiddenCount() const
138 int hiddenItemCount
= 0;
139 foreach (const PlacesItem
* item
, m_bookmarkedItems
) {
143 if (placesItem(modelIndex
)->isHidden()) {
150 return hiddenItemCount
;
153 void PlacesItemModel::setHiddenItemsShown(bool show
)
155 if (m_hiddenItemsShown
== show
) {
159 m_hiddenItemsShown
= show
;
162 // Move all items that are part of m_bookmarkedItems to the model.
163 QList
<PlacesItem
*> itemsToInsert
;
164 QList
<int> insertPos
;
166 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
167 if (m_bookmarkedItems
[i
]) {
168 itemsToInsert
.append(m_bookmarkedItems
[i
]);
169 m_bookmarkedItems
[i
] = 0;
170 insertPos
.append(modelIndex
);
175 // Inserting the items will automatically insert an item
176 // to m_bookmarkedItems in PlacesItemModel::onItemsInserted().
177 // The items are temporary saved in itemsToInsert, so
178 // m_bookmarkedItems can be shrinked now.
179 m_bookmarkedItems
.erase(m_bookmarkedItems
.begin(),
180 m_bookmarkedItems
.begin() + itemsToInsert
.count());
182 for (int i
= 0; i
< itemsToInsert
.count(); ++i
) {
183 insertItem(insertPos
[i
], itemsToInsert
[i
]);
186 Q_ASSERT(m_bookmarkedItems
.count() == count());
188 // Move all items of the model, where the "isHidden" property is true, to
189 // m_bookmarkedItems.
190 Q_ASSERT(m_bookmarkedItems
.count() == count());
191 for (int i
= count() - 1; i
>= 0; --i
) {
192 if (placesItem(i
)->isHidden()) {
198 #ifdef PLACESITEMMODEL_DEBUG
199 kDebug() << "Changed visibility of hidden items";
204 bool PlacesItemModel::hiddenItemsShown() const
206 return m_hiddenItemsShown
;
209 int PlacesItemModel::closestItem(const KUrl
& url
) const
214 for (int i
= 0; i
< count(); ++i
) {
215 const KUrl itemUrl
= placesItem(i
)->url();
216 if (itemUrl
.isParentOf(url
)) {
217 const int length
= itemUrl
.prettyUrl().length();
218 if (length
> maxLength
) {
228 QAction
* PlacesItemModel::ejectAction(int index
) const
230 const PlacesItem
* item
= placesItem(index
);
231 if (item
&& item
->device().is
<Solid::OpticalDisc
>()) {
232 return new QAction(KIcon("media-eject"), i18nc("@item", "Eject '%1'", item
->text()), 0);
238 QAction
* PlacesItemModel::teardownAction(int index
) const
240 const PlacesItem
* item
= placesItem(index
);
245 Solid::Device device
= item
->device();
246 const bool providesTearDown
= device
.is
<Solid::StorageAccess
>() &&
247 device
.as
<Solid::StorageAccess
>()->isAccessible();
248 if (!providesTearDown
) {
252 Solid::StorageDrive
* drive
= device
.as
<Solid::StorageDrive
>();
254 drive
= device
.parent().as
<Solid::StorageDrive
>();
257 bool hotPluggable
= false;
258 bool removable
= false;
260 hotPluggable
= drive
->isHotpluggable();
261 removable
= drive
->isRemovable();
266 const QString label
= item
->text();
267 if (device
.is
<Solid::OpticalDisc
>()) {
268 text
= i18nc("@item", "Release '%1'", label
);
269 } else if (removable
|| hotPluggable
) {
270 text
= i18nc("@item", "Safely Remove '%1'", label
);
271 iconName
= "media-eject";
273 text
= i18nc("@item", "Unmount '%1'", label
);
274 iconName
= "media-eject";
277 if (iconName
.isEmpty()) {
278 return new QAction(text
, 0);
281 return new QAction(KIcon(iconName
), text
, 0);
284 void PlacesItemModel::requestEject(int index
)
286 const PlacesItem
* item
= placesItem(index
);
288 Solid::OpticalDrive
* drive
= item
->device().parent().as
<Solid::OpticalDrive
>();
290 connect(drive
, SIGNAL(ejectDone(Solid::ErrorType
,QVariant
,QString
)),
291 this, SLOT(slotStorageTeardownDone(Solid::ErrorType
,QVariant
)));
299 void PlacesItemModel::requestTeardown(int index
)
301 const PlacesItem
* item
= placesItem(index
);
303 Solid::StorageAccess
* access
= item
->device().as
<Solid::StorageAccess
>();
305 connect(access
, SIGNAL(teardownDone(Solid::ErrorType
,QVariant
,QString
)),
306 this, SLOT(slotStorageTeardownDone(Solid::ErrorType
,QVariant
)));
309 const QString label
= item
->text();
310 const QString message
= i18nc("@info", "The device '%1' is not a disk and cannot be ejected.", label
);
311 emit
errorMessage(message
);
316 KUrl
PlacesItemModel::convertedUrl(const KUrl
& url
)
319 if (url
.protocol() == QLatin1String("timeline")) {
320 newUrl
= createTimelineUrl(url
);
321 } else if (url
.protocol() == QLatin1String("search")) {
322 newUrl
= createSearchUrl(url
);
328 void PlacesItemModel::onItemInserted(int index
)
330 const PlacesItem
* insertedItem
= placesItem(index
);
332 // Take care to apply the PlacesItemModel-order of the inserted item
333 // also to the bookmark-manager.
334 const KBookmark insertedBookmark
= insertedItem
->bookmark();
336 const PlacesItem
* previousItem
= placesItem(index
- 1);
337 KBookmark previousBookmark
;
339 previousBookmark
= previousItem
->bookmark();
342 m_bookmarkManager
->root().moveBookmark(insertedBookmark
, previousBookmark
);
345 if (index
== count() - 1) {
346 // The item has been appended as last item to the list. In this
347 // case assure that it is also appended after the hidden items and
348 // not before (like done otherwise).
349 m_bookmarkedItems
.append(0);
353 int bookmarkIndex
= 0;
354 while (bookmarkIndex
< m_bookmarkedItems
.count()) {
355 if (!m_bookmarkedItems
[bookmarkIndex
]) {
357 if (modelIndex
+ 1 == index
) {
363 m_bookmarkedItems
.insert(bookmarkIndex
, 0);
366 triggerBookmarksSaving();
368 #ifdef PLACESITEMMODEL_DEBUG
369 kDebug() << "Inserted item" << index
;
374 void PlacesItemModel::onItemRemoved(int index
, KStandardItem
* removedItem
)
376 PlacesItem
* placesItem
= dynamic_cast<PlacesItem
*>(removedItem
);
378 const KBookmark bookmark
= placesItem
->bookmark();
379 m_bookmarkManager
->root().deleteBookmark(bookmark
);
382 const int boomarkIndex
= bookmarkIndex(index
);
383 Q_ASSERT(!m_bookmarkedItems
[boomarkIndex
]);
384 m_bookmarkedItems
.removeAt(boomarkIndex
);
386 triggerBookmarksSaving();
388 #ifdef PLACESITEMMODEL_DEBUG
389 kDebug() << "Removed item" << index
;
394 void PlacesItemModel::onItemChanged(int index
, const QSet
<QByteArray
>& changedRoles
)
396 const PlacesItem
* changedItem
= placesItem(index
);
398 // Take care to apply the PlacesItemModel-order of the inserted item
399 // also to the bookmark-manager.
400 const KBookmark insertedBookmark
= changedItem
->bookmark();
402 const PlacesItem
* previousItem
= placesItem(index
- 1);
403 KBookmark previousBookmark
;
405 previousBookmark
= previousItem
->bookmark();
408 m_bookmarkManager
->root().moveBookmark(insertedBookmark
, previousBookmark
);
411 if (changedRoles
.contains("isHidden")) {
412 const PlacesItem
* shownItem
= placesItem(index
);
414 if (!m_hiddenItemsShown
&& shownItem
->isHidden()) {
415 m_hiddenItemToRemove
= index
;
416 QTimer::singleShot(0, this, SLOT(hideItem()));
420 triggerBookmarksSaving();
423 void PlacesItemModel::slotDeviceAdded(const QString
& udi
)
425 const Solid::Device
device(udi
);
426 if (m_predicate
.matches(device
)) {
427 m_availableDevices
<< udi
;
428 const KBookmark bookmark
= PlacesItem::createDeviceBookmark(m_bookmarkManager
, udi
);
429 appendItem(new PlacesItem(bookmark
));
433 void PlacesItemModel::slotDeviceRemoved(const QString
& udi
)
435 if (!m_availableDevices
.contains(udi
)) {
439 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
440 PlacesItem
* item
= m_bookmarkedItems
[i
];
441 if (item
&& item
->udi() == udi
) {
442 m_bookmarkedItems
.removeAt(i
);
448 for (int i
= 0; i
< count(); ++i
) {
449 if (placesItem(i
)->udi() == udi
) {
456 void PlacesItemModel::slotStorageTeardownDone(Solid::ErrorType error
, const QVariant
& errorData
)
458 if (error
&& errorData
.isValid()) {
459 emit
errorMessage(errorData
.toString());
463 void PlacesItemModel::hideItem()
465 hideItem(m_hiddenItemToRemove
);
466 m_hiddenItemToRemove
= -1;
469 void PlacesItemModel::updateBookmarks()
471 // Verify whether new bookmarks have been added or existing
472 // bookmarks have been changed.
473 KBookmarkGroup root
= m_bookmarkManager
->root();
474 KBookmark newBookmark
= root
.first();
475 while (!newBookmark
.isNull()) {
476 if (acceptBookmark(newBookmark
)) {
479 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
480 PlacesItem
* item
= m_bookmarkedItems
[i
];
482 item
= placesItem(modelIndex
);
486 const KBookmark oldBookmark
= item
->bookmark();
487 if (equalBookmarkIdentifiers(newBookmark
, oldBookmark
)) {
488 // The bookmark has been found in the model or as
489 // a hidden item. The content of the bookmark might
490 // have been changed, so an update is done.
492 if (newBookmark
.metaDataItem("UDI").isEmpty()) {
493 item
->setBookmark(newBookmark
);
500 PlacesItem
* item
= new PlacesItem(newBookmark
);
501 if (item
->isHidden() && !m_hiddenItemsShown
) {
502 m_bookmarkedItems
.append(item
);
509 newBookmark
= root
.next(newBookmark
);
512 // Remove items that are not part of the bookmark-manager anymore
514 for (int i
= m_bookmarkedItems
.count() - 1; i
>= 0; --i
) {
515 PlacesItem
* item
= m_bookmarkedItems
[i
];
516 const bool itemIsPartOfModel
= (item
== 0);
517 if (itemIsPartOfModel
) {
518 item
= placesItem(modelIndex
);
521 bool hasBeenRemoved
= true;
522 const KBookmark oldBookmark
= item
->bookmark();
523 KBookmark newBookmark
= root
.first();
524 while (!newBookmark
.isNull()) {
525 if (equalBookmarkIdentifiers(newBookmark
, oldBookmark
)) {
526 hasBeenRemoved
= false;
529 newBookmark
= root
.next(newBookmark
);
532 if (hasBeenRemoved
) {
533 if (m_bookmarkedItems
[i
]) {
534 delete m_bookmarkedItems
[i
];
535 m_bookmarkedItems
.removeAt(i
);
537 removeItem(modelIndex
);
542 if (itemIsPartOfModel
) {
548 void PlacesItemModel::saveBookmarks()
550 m_bookmarkManager
->emitChanged(m_bookmarkManager
->root());
553 void PlacesItemModel::loadBookmarks()
555 KBookmarkGroup root
= m_bookmarkManager
->root();
556 KBookmark bookmark
= root
.first();
557 QSet
<QString
> devices
= m_availableDevices
;
559 QSet
<KUrl
> missingSystemBookmarks
;
560 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
561 missingSystemBookmarks
.insert(data
.url
);
564 // The bookmarks might have a mixed order of places, devices and search-groups due
565 // to the compatibility with the KFilePlacesPanel. In Dolphin's places panel the
566 // items should always be collected in one group so the items are collected first
567 // in separate lists before inserting them.
568 QList
<PlacesItem
*> placesItems
;
569 QList
<PlacesItem
*> recentlyAccessedItems
;
570 QList
<PlacesItem
*> searchForItems
;
571 QList
<PlacesItem
*> devicesItems
;
573 while (!bookmark
.isNull()) {
574 const bool deviceAvailable
= devices
.remove(bookmark
.metaDataItem("UDI"));
575 if (acceptBookmark(bookmark
)) {
576 PlacesItem
* item
= new PlacesItem(bookmark
);
577 if (deviceAvailable
) {
578 devicesItems
.append(item
);
580 const KUrl url
= bookmark
.url();
581 if (missingSystemBookmarks
.contains(url
)) {
582 missingSystemBookmarks
.remove(url
);
584 // Apply the translated text to the system bookmarks, otherwise an outdated
585 // translation might be shown.
586 const int index
= m_systemBookmarksIndexes
.value(url
);
587 item
->setText(m_systemBookmarks
[index
].text
);
588 item
->setSystemItem(true);
591 switch (item
->groupType()) {
592 case PlacesItem::PlacesType
: placesItems
.append(item
); break;
593 case PlacesItem::RecentlyAccessedType
: recentlyAccessedItems
.append(item
); break;
594 case PlacesItem::SearchForType
: searchForItems
.append(item
); break;
595 case PlacesItem::DevicesType
:
596 default: Q_ASSERT(false); break;
601 bookmark
= root
.next(bookmark
);
604 if (!missingSystemBookmarks
.isEmpty()) {
605 // The current bookmarks don't contain all system-bookmarks. Add the missing
607 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
608 if (missingSystemBookmarks
.contains(data
.url
)) {
609 PlacesItem
* item
= createSystemPlacesItem(data
);
610 switch (item
->groupType()) {
611 case PlacesItem::PlacesType
: placesItems
.append(item
); break;
612 case PlacesItem::RecentlyAccessedType
: recentlyAccessedItems
.append(item
); break;
613 case PlacesItem::SearchForType
: searchForItems
.append(item
); break;
614 case PlacesItem::DevicesType
:
615 default: Q_ASSERT(false); break;
621 // Create items for devices that have not been stored as bookmark yet
622 foreach (const QString
& udi
, devices
) {
623 const KBookmark bookmark
= PlacesItem::createDeviceBookmark(m_bookmarkManager
, udi
);
624 devicesItems
.append(new PlacesItem(bookmark
));
627 QList
<PlacesItem
*> items
;
628 items
.append(placesItems
);
629 items
.append(recentlyAccessedItems
);
630 items
.append(searchForItems
);
631 items
.append(devicesItems
);
633 foreach (PlacesItem
* item
, items
) {
634 if (!m_hiddenItemsShown
&& item
->isHidden()) {
635 m_bookmarkedItems
.append(item
);
641 #ifdef PLACESITEMMODEL_DEBUG
642 kDebug() << "Loaded bookmarks";
647 bool PlacesItemModel::acceptBookmark(const KBookmark
& bookmark
) const
649 const QString udi
= bookmark
.metaDataItem("UDI");
650 const KUrl url
= bookmark
.url();
651 const QString appName
= bookmark
.metaDataItem("OnlyInApp");
652 const bool deviceAvailable
= m_availableDevices
.contains(udi
);
654 const bool allowedHere
= (appName
.isEmpty()
655 || appName
== KGlobal::mainComponent().componentName()
656 || appName
== KGlobal::mainComponent().componentName() + AppNamePrefix
)
657 && (m_nepomukRunning
|| (url
.protocol() != QLatin1String("timeline") &&
658 url
.protocol() != QLatin1String("search")));
660 return (udi
.isEmpty() && allowedHere
) || deviceAvailable
;
663 PlacesItem
* PlacesItemModel::createSystemPlacesItem(const SystemBookmarkData
& data
)
665 KBookmark bookmark
= PlacesItem::createBookmark(m_bookmarkManager
,
670 const QString protocol
= data
.url
.protocol();
671 if (protocol
== QLatin1String("timeline") || protocol
== QLatin1String("search")) {
672 // As long as the KFilePlacesView from kdelibs is available, the system-bookmarks
673 // for "Recently Accessed" and "Search For" should be a setting available only
674 // in the Places Panel (see description of AppNamePrefix for more details).
675 const QString appName
= KGlobal::mainComponent().componentName() + AppNamePrefix
;
676 bookmark
.setMetaDataItem("OnlyInApp", appName
);
679 PlacesItem
* item
= new PlacesItem(bookmark
);
680 item
->setSystemItem(true);
682 // Create default view-properties for all "Search For" and "Recently Accessed" bookmarks
683 // in case if the user has not already created custom view-properties for a corresponding
685 const bool createDefaultViewProperties
= (item
->groupType() == PlacesItem::SearchForType
||
686 item
->groupType() == PlacesItem::RecentlyAccessedType
) &&
687 !GeneralSettings::self()->globalViewProps();
688 if (createDefaultViewProperties
) {
689 ViewProperties
props(convertedUrl(data
.url
));
690 if (!props
.exist()) {
691 const QString path
= data
.url
.path();
692 if (path
== QLatin1String("/documents")) {
693 props
.setViewMode(DolphinView::DetailsView
);
694 props
.setPreviewsShown(false);
695 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "path");
696 } else if (path
== QLatin1String("/images")) {
697 props
.setViewMode(DolphinView::IconsView
);
698 props
.setPreviewsShown(true);
699 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "imageSize");
700 } else if (path
== QLatin1String("/audio")) {
701 props
.setViewMode(DolphinView::DetailsView
);
702 props
.setPreviewsShown(false);
703 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "artist" << "album");
704 } else if (path
== QLatin1String("/videos")) {
705 props
.setViewMode(DolphinView::IconsView
);
706 props
.setPreviewsShown(true);
707 props
.setVisibleRoles(QList
<QByteArray
>() << "text");
708 } else if (data
.url
.protocol() == "timeline") {
709 props
.setViewMode(DolphinView::DetailsView
);
710 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "date");
718 void PlacesItemModel::createSystemBookmarks()
720 Q_ASSERT(m_systemBookmarks
.isEmpty());
721 Q_ASSERT(m_systemBookmarksIndexes
.isEmpty());
723 const QString timeLineIcon
= "package_utility_time"; // TODO: Ask the Oxygen team to create
724 // a custom icon for the timeline-protocol
726 m_systemBookmarks
.append(SystemBookmarkData(KUrl(KUser().homeDir()),
728 i18nc("@item", "Home")));
729 m_systemBookmarks
.append(SystemBookmarkData(KUrl("remote:/"),
731 i18nc("@item", "Network")));
732 m_systemBookmarks
.append(SystemBookmarkData(KUrl("/"),
734 i18nc("@item", "Root")));
735 m_systemBookmarks
.append(SystemBookmarkData(KUrl("trash:/"),
737 i18nc("@item", "Trash")));
739 if (m_nepomukRunning
) {
740 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/today"),
742 i18nc("@item Recently Accessed", "Today")));
743 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/yesterday"),
745 i18nc("@item Recently Accessed", "Yesterday")));
746 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/thismonth"),
748 i18nc("@item Recently Accessed", "This Month")));
749 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/lastmonth"),
751 i18nc("@item Recently Accessed", "Last Month")));
752 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/documents"),
754 i18nc("@item Commonly Accessed", "Documents")));
755 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/images"),
757 i18nc("@item Commonly Accessed", "Images")));
758 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/audio"),
760 i18nc("@item Commonly Accessed", "Audio Files")));
761 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/videos"),
763 i18nc("@item Commonly Accessed", "Videos")));
766 for (int i
= 0; i
< m_systemBookmarks
.count(); ++i
) {
767 m_systemBookmarksIndexes
.insert(m_systemBookmarks
[i
].url
, i
);
771 void PlacesItemModel::initializeAvailableDevices()
773 m_predicate
= Solid::Predicate::fromString(
774 "[[[[ StorageVolume.ignored == false AND [ StorageVolume.usage == 'FileSystem' OR StorageVolume.usage == 'Encrypted' ]]"
776 "[ IS StorageAccess AND StorageDrive.driveType == 'Floppy' ]]"
778 "OpticalDisc.availableContent & 'Audio' ]"
780 "StorageAccess.ignored == false ]");
781 Q_ASSERT(m_predicate
.isValid());
783 Solid::DeviceNotifier
* notifier
= Solid::DeviceNotifier::instance();
784 connect(notifier
, SIGNAL(deviceAdded(QString
)), this, SLOT(slotDeviceAdded(QString
)));
785 connect(notifier
, SIGNAL(deviceRemoved(QString
)), this, SLOT(slotDeviceRemoved(QString
)));
787 const QList
<Solid::Device
>& deviceList
= Solid::Device::listFromQuery(m_predicate
);
788 foreach(const Solid::Device
& device
, deviceList
) {
789 m_availableDevices
<< device
.udi();
793 int PlacesItemModel::bookmarkIndex(int index
) const
795 int bookmarkIndex
= 0;
797 while (bookmarkIndex
< m_bookmarkedItems
.count()) {
798 if (!m_bookmarkedItems
[bookmarkIndex
]) {
799 if (modelIndex
== index
) {
807 return bookmarkIndex
>= m_bookmarkedItems
.count() ? -1 : bookmarkIndex
;
810 void PlacesItemModel::hideItem(int index
)
812 PlacesItem
* shownItem
= placesItem(index
);
817 shownItem
->setHidden(true);
818 if (m_hiddenItemsShown
) {
819 // Removing items from the model is not allowed if all hidden
820 // items should be shown.
824 const int newIndex
= bookmarkIndex(index
);
826 const KBookmark hiddenBookmark
= shownItem
->bookmark();
827 PlacesItem
* hiddenItem
= new PlacesItem(hiddenBookmark
);
829 const PlacesItem
* previousItem
= placesItem(index
- 1);
830 KBookmark previousBookmark
;
832 previousBookmark
= previousItem
->bookmark();
835 const bool updateBookmark
= (m_bookmarkManager
->root().indexOf(hiddenBookmark
) >= 0);
838 if (updateBookmark
) {
839 // removeItem() also removed the bookmark from m_bookmarkManager in
840 // PlacesItemModel::onItemRemoved(). However for hidden items the
841 // bookmark should still be remembered, so readd it again:
842 m_bookmarkManager
->root().addBookmark(hiddenBookmark
);
843 m_bookmarkManager
->root().moveBookmark(hiddenBookmark
, previousBookmark
);
844 triggerBookmarksSaving();
847 m_bookmarkedItems
.insert(newIndex
, hiddenItem
);
851 void PlacesItemModel::triggerBookmarksSaving()
853 if (m_saveBookmarksTimer
) {
854 m_saveBookmarksTimer
->start();
858 bool PlacesItemModel::equalBookmarkIdentifiers(const KBookmark
& b1
, const KBookmark
& b2
)
860 const QString udi1
= b1
.metaDataItem("UDI");
861 const QString udi2
= b2
.metaDataItem("UDI");
862 if (!udi1
.isEmpty() && !udi2
.isEmpty()) {
865 return b1
.metaDataItem("ID") == b2
.metaDataItem("ID");
869 KUrl
PlacesItemModel::createTimelineUrl(const KUrl
& url
)
871 // TODO: Clarify with the Nepomuk-team whether it makes sense
872 // provide default-timeline-URLs like 'yesterday', 'this month'
876 const QString path
= url
.pathOrUrl();
877 if (path
.endsWith("yesterday")) {
878 const QDate date
= QDate::currentDate().addDays(-1);
879 const int year
= date
.year();
880 const int month
= date
.month();
881 const int day
= date
.day();
882 timelineUrl
= "timeline:/" + timelineDateString(year
, month
) +
883 '/' + timelineDateString(year
, month
, day
);
884 } else if (path
.endsWith("thismonth")) {
885 const QDate date
= QDate::currentDate();
886 timelineUrl
= "timeline:/" + timelineDateString(date
.year(), date
.month());
887 } else if (path
.endsWith("lastmonth")) {
888 const QDate date
= QDate::currentDate().addMonths(-1);
889 timelineUrl
= "timeline:/" + timelineDateString(date
.year(), date
.month());
891 Q_ASSERT(path
.endsWith("today"));
898 QString
PlacesItemModel::timelineDateString(int year
, int month
, int day
)
900 QString date
= QString::number(year
) + '-';
904 date
+= QString::number(month
);
911 date
+= QString::number(day
);
917 KUrl
PlacesItemModel::createSearchUrl(const KUrl
& url
)
922 const QString path
= url
.pathOrUrl();
923 if (path
.endsWith("documents")) {
924 searchUrl
= searchUrlForTerm(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Document()));
925 } else if (path
.endsWith("images")) {
926 searchUrl
= searchUrlForTerm(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Image()));
927 } else if (path
.endsWith("audio")) {
928 searchUrl
= searchUrlForTerm(Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
929 Nepomuk::Query::LiteralTerm("audio")));
930 } else if (path
.endsWith("videos")) {
931 searchUrl
= searchUrlForTerm(Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
932 Nepomuk::Query::LiteralTerm("video")));
944 KUrl
PlacesItemModel::searchUrlForTerm(const Nepomuk::Query::Term
& term
)
946 const Nepomuk::Query::Query
query(term
);
947 return query
.toSearchUrl();
951 #ifdef PLACESITEMMODEL_DEBUG
952 void PlacesItemModel::showModelState()
954 kDebug() << "=================================";
955 kDebug() << "Model:";
956 kDebug() << "hidden-index model-index text";
958 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
959 if (m_bookmarkedItems
[i
]) {
960 kDebug() << i
<< "(Hidden) " << " " << m_bookmarkedItems
[i
]->dataValue("text").toString();
962 if (item(modelIndex
)) {
963 kDebug() << i
<< " " << modelIndex
<< " " << item(modelIndex
)->dataValue("text").toString();
965 kDebug() << i
<< " " << modelIndex
<< " " << "(not available yet)";
972 kDebug() << "Bookmarks:";
974 int bookmarkIndex
= 0;
975 KBookmarkGroup root
= m_bookmarkManager
->root();
976 KBookmark bookmark
= root
.first();
977 while (!bookmark
.isNull()) {
978 const QString udi
= bookmark
.metaDataItem("UDI");
979 const QString text
= udi
.isEmpty() ? bookmark
.text() : udi
;
980 if (bookmark
.metaDataItem("IsHidden") == QLatin1String("true")) {
981 kDebug() << bookmarkIndex
<< "(Hidden)" << text
;
983 kDebug() << bookmarkIndex
<< " " << text
;
986 bookmark
= root
.next(bookmark
);
992 #include "placesitemmodel.moc"