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 changed 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 if (!m_hiddenItemsShown
&& changedItem
->isHidden()) {
413 m_hiddenItemToRemove
= index
;
414 QTimer::singleShot(0, this, SLOT(hideItem()));
418 triggerBookmarksSaving();
421 void PlacesItemModel::slotDeviceAdded(const QString
& udi
)
423 const Solid::Device
device(udi
);
424 if (m_predicate
.matches(device
)) {
425 m_availableDevices
<< udi
;
426 const KBookmark bookmark
= PlacesItem::createDeviceBookmark(m_bookmarkManager
, udi
);
427 appendItem(new PlacesItem(bookmark
));
431 void PlacesItemModel::slotDeviceRemoved(const QString
& udi
)
433 if (!m_availableDevices
.contains(udi
)) {
437 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
438 PlacesItem
* item
= m_bookmarkedItems
[i
];
439 if (item
&& item
->udi() == udi
) {
440 m_bookmarkedItems
.removeAt(i
);
446 for (int i
= 0; i
< count(); ++i
) {
447 if (placesItem(i
)->udi() == udi
) {
454 void PlacesItemModel::slotStorageTeardownDone(Solid::ErrorType error
, const QVariant
& errorData
)
456 if (error
&& errorData
.isValid()) {
457 emit
errorMessage(errorData
.toString());
461 void PlacesItemModel::hideItem()
463 hideItem(m_hiddenItemToRemove
);
464 m_hiddenItemToRemove
= -1;
467 void PlacesItemModel::updateBookmarks()
469 // Verify whether new bookmarks have been added or existing
470 // bookmarks have been changed.
471 KBookmarkGroup root
= m_bookmarkManager
->root();
472 KBookmark newBookmark
= root
.first();
473 while (!newBookmark
.isNull()) {
474 if (acceptBookmark(newBookmark
)) {
477 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
478 PlacesItem
* item
= m_bookmarkedItems
[i
];
480 item
= placesItem(modelIndex
);
484 const KBookmark oldBookmark
= item
->bookmark();
485 if (equalBookmarkIdentifiers(newBookmark
, oldBookmark
)) {
486 // The bookmark has been found in the model or as
487 // a hidden item. The content of the bookmark might
488 // have been changed, so an update is done.
490 if (newBookmark
.metaDataItem("UDI").isEmpty()) {
491 item
->setBookmark(newBookmark
);
498 PlacesItem
* item
= new PlacesItem(newBookmark
);
499 if (item
->isHidden() && !m_hiddenItemsShown
) {
500 m_bookmarkedItems
.append(item
);
507 newBookmark
= root
.next(newBookmark
);
510 // Remove items that are not part of the bookmark-manager anymore
512 for (int i
= m_bookmarkedItems
.count() - 1; i
>= 0; --i
) {
513 PlacesItem
* item
= m_bookmarkedItems
[i
];
514 const bool itemIsPartOfModel
= (item
== 0);
515 if (itemIsPartOfModel
) {
516 item
= placesItem(modelIndex
);
519 bool hasBeenRemoved
= true;
520 const KBookmark oldBookmark
= item
->bookmark();
521 KBookmark newBookmark
= root
.first();
522 while (!newBookmark
.isNull()) {
523 if (equalBookmarkIdentifiers(newBookmark
, oldBookmark
)) {
524 hasBeenRemoved
= false;
527 newBookmark
= root
.next(newBookmark
);
530 if (hasBeenRemoved
) {
531 if (m_bookmarkedItems
[i
]) {
532 delete m_bookmarkedItems
[i
];
533 m_bookmarkedItems
.removeAt(i
);
535 removeItem(modelIndex
);
540 if (itemIsPartOfModel
) {
546 void PlacesItemModel::saveBookmarks()
548 m_bookmarkManager
->emitChanged(m_bookmarkManager
->root());
551 void PlacesItemModel::loadBookmarks()
553 KBookmarkGroup root
= m_bookmarkManager
->root();
554 KBookmark bookmark
= root
.first();
555 QSet
<QString
> devices
= m_availableDevices
;
557 QSet
<KUrl
> missingSystemBookmarks
;
558 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
559 missingSystemBookmarks
.insert(data
.url
);
562 // The bookmarks might have a mixed order of places, devices and search-groups due
563 // to the compatibility with the KFilePlacesPanel. In Dolphin's places panel the
564 // items should always be collected in one group so the items are collected first
565 // in separate lists before inserting them.
566 QList
<PlacesItem
*> placesItems
;
567 QList
<PlacesItem
*> recentlyAccessedItems
;
568 QList
<PlacesItem
*> searchForItems
;
569 QList
<PlacesItem
*> devicesItems
;
571 while (!bookmark
.isNull()) {
572 const bool deviceAvailable
= devices
.remove(bookmark
.metaDataItem("UDI"));
573 if (acceptBookmark(bookmark
)) {
574 PlacesItem
* item
= new PlacesItem(bookmark
);
575 if (deviceAvailable
) {
576 devicesItems
.append(item
);
578 const KUrl url
= bookmark
.url();
579 if (missingSystemBookmarks
.contains(url
)) {
580 missingSystemBookmarks
.remove(url
);
582 // Apply the translated text to the system bookmarks, otherwise an outdated
583 // translation might be shown.
584 const int index
= m_systemBookmarksIndexes
.value(url
);
585 item
->setText(m_systemBookmarks
[index
].text
);
586 item
->setSystemItem(true);
589 switch (item
->groupType()) {
590 case PlacesItem::PlacesType
: placesItems
.append(item
); break;
591 case PlacesItem::RecentlyAccessedType
: recentlyAccessedItems
.append(item
); break;
592 case PlacesItem::SearchForType
: searchForItems
.append(item
); break;
593 case PlacesItem::DevicesType
:
594 default: Q_ASSERT(false); break;
599 bookmark
= root
.next(bookmark
);
602 if (!missingSystemBookmarks
.isEmpty()) {
603 // The current bookmarks don't contain all system-bookmarks. Add the missing
605 foreach (const SystemBookmarkData
& data
, m_systemBookmarks
) {
606 if (missingSystemBookmarks
.contains(data
.url
)) {
607 PlacesItem
* item
= createSystemPlacesItem(data
);
608 switch (item
->groupType()) {
609 case PlacesItem::PlacesType
: placesItems
.append(item
); break;
610 case PlacesItem::RecentlyAccessedType
: recentlyAccessedItems
.append(item
); break;
611 case PlacesItem::SearchForType
: searchForItems
.append(item
); break;
612 case PlacesItem::DevicesType
:
613 default: Q_ASSERT(false); break;
619 // Create items for devices that have not been stored as bookmark yet
620 foreach (const QString
& udi
, devices
) {
621 const KBookmark bookmark
= PlacesItem::createDeviceBookmark(m_bookmarkManager
, udi
);
622 devicesItems
.append(new PlacesItem(bookmark
));
625 QList
<PlacesItem
*> items
;
626 items
.append(placesItems
);
627 items
.append(recentlyAccessedItems
);
628 items
.append(searchForItems
);
629 items
.append(devicesItems
);
631 foreach (PlacesItem
* item
, items
) {
632 if (!m_hiddenItemsShown
&& item
->isHidden()) {
633 m_bookmarkedItems
.append(item
);
639 #ifdef PLACESITEMMODEL_DEBUG
640 kDebug() << "Loaded bookmarks";
645 bool PlacesItemModel::acceptBookmark(const KBookmark
& bookmark
) const
647 const QString udi
= bookmark
.metaDataItem("UDI");
648 const KUrl url
= bookmark
.url();
649 const QString appName
= bookmark
.metaDataItem("OnlyInApp");
650 const bool deviceAvailable
= m_availableDevices
.contains(udi
);
652 const bool allowedHere
= (appName
.isEmpty()
653 || appName
== KGlobal::mainComponent().componentName()
654 || appName
== KGlobal::mainComponent().componentName() + AppNamePrefix
)
655 && (m_nepomukRunning
|| (url
.protocol() != QLatin1String("timeline") &&
656 url
.protocol() != QLatin1String("search")));
658 return (udi
.isEmpty() && allowedHere
) || deviceAvailable
;
661 PlacesItem
* PlacesItemModel::createSystemPlacesItem(const SystemBookmarkData
& data
)
663 KBookmark bookmark
= PlacesItem::createBookmark(m_bookmarkManager
,
668 const QString protocol
= data
.url
.protocol();
669 if (protocol
== QLatin1String("timeline") || protocol
== QLatin1String("search")) {
670 // As long as the KFilePlacesView from kdelibs is available, the system-bookmarks
671 // for "Recently Accessed" and "Search For" should be a setting available only
672 // in the Places Panel (see description of AppNamePrefix for more details).
673 const QString appName
= KGlobal::mainComponent().componentName() + AppNamePrefix
;
674 bookmark
.setMetaDataItem("OnlyInApp", appName
);
677 PlacesItem
* item
= new PlacesItem(bookmark
);
678 item
->setSystemItem(true);
680 // Create default view-properties for all "Search For" and "Recently Accessed" bookmarks
681 // in case if the user has not already created custom view-properties for a corresponding
683 const bool createDefaultViewProperties
= (item
->groupType() == PlacesItem::SearchForType
||
684 item
->groupType() == PlacesItem::RecentlyAccessedType
) &&
685 !GeneralSettings::self()->globalViewProps();
686 if (createDefaultViewProperties
) {
687 ViewProperties
props(convertedUrl(data
.url
));
688 if (!props
.exist()) {
689 const QString path
= data
.url
.path();
690 if (path
== QLatin1String("/documents")) {
691 props
.setViewMode(DolphinView::DetailsView
);
692 props
.setPreviewsShown(false);
693 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "path");
694 } else if (path
== QLatin1String("/images")) {
695 props
.setViewMode(DolphinView::IconsView
);
696 props
.setPreviewsShown(true);
697 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "imageSize");
698 } else if (path
== QLatin1String("/audio")) {
699 props
.setViewMode(DolphinView::DetailsView
);
700 props
.setPreviewsShown(false);
701 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "artist" << "album");
702 } else if (path
== QLatin1String("/videos")) {
703 props
.setViewMode(DolphinView::IconsView
);
704 props
.setPreviewsShown(true);
705 props
.setVisibleRoles(QList
<QByteArray
>() << "text");
706 } else if (data
.url
.protocol() == "timeline") {
707 props
.setViewMode(DolphinView::DetailsView
);
708 props
.setVisibleRoles(QList
<QByteArray
>() << "text" << "date");
716 void PlacesItemModel::createSystemBookmarks()
718 Q_ASSERT(m_systemBookmarks
.isEmpty());
719 Q_ASSERT(m_systemBookmarksIndexes
.isEmpty());
721 const QString timeLineIcon
= "package_utility_time"; // TODO: Ask the Oxygen team to create
722 // a custom icon for the timeline-protocol
724 m_systemBookmarks
.append(SystemBookmarkData(KUrl(KUser().homeDir()),
726 i18nc("@item", "Home")));
727 m_systemBookmarks
.append(SystemBookmarkData(KUrl("remote:/"),
729 i18nc("@item", "Network")));
730 m_systemBookmarks
.append(SystemBookmarkData(KUrl("/"),
732 i18nc("@item", "Root")));
733 m_systemBookmarks
.append(SystemBookmarkData(KUrl("trash:/"),
735 i18nc("@item", "Trash")));
737 if (m_nepomukRunning
) {
738 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/today"),
740 i18nc("@item Recently Accessed", "Today")));
741 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/yesterday"),
743 i18nc("@item Recently Accessed", "Yesterday")));
744 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/thismonth"),
746 i18nc("@item Recently Accessed", "This Month")));
747 m_systemBookmarks
.append(SystemBookmarkData(KUrl("timeline:/lastmonth"),
749 i18nc("@item Recently Accessed", "Last Month")));
750 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/documents"),
752 i18nc("@item Commonly Accessed", "Documents")));
753 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/images"),
755 i18nc("@item Commonly Accessed", "Images")));
756 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/audio"),
758 i18nc("@item Commonly Accessed", "Audio Files")));
759 m_systemBookmarks
.append(SystemBookmarkData(KUrl("search:/videos"),
761 i18nc("@item Commonly Accessed", "Videos")));
764 for (int i
= 0; i
< m_systemBookmarks
.count(); ++i
) {
765 m_systemBookmarksIndexes
.insert(m_systemBookmarks
[i
].url
, i
);
769 void PlacesItemModel::initializeAvailableDevices()
771 m_predicate
= Solid::Predicate::fromString(
772 "[[[[ StorageVolume.ignored == false AND [ StorageVolume.usage == 'FileSystem' OR StorageVolume.usage == 'Encrypted' ]]"
774 "[ IS StorageAccess AND StorageDrive.driveType == 'Floppy' ]]"
776 "OpticalDisc.availableContent & 'Audio' ]"
778 "StorageAccess.ignored == false ]");
779 Q_ASSERT(m_predicate
.isValid());
781 Solid::DeviceNotifier
* notifier
= Solid::DeviceNotifier::instance();
782 connect(notifier
, SIGNAL(deviceAdded(QString
)), this, SLOT(slotDeviceAdded(QString
)));
783 connect(notifier
, SIGNAL(deviceRemoved(QString
)), this, SLOT(slotDeviceRemoved(QString
)));
785 const QList
<Solid::Device
>& deviceList
= Solid::Device::listFromQuery(m_predicate
);
786 foreach(const Solid::Device
& device
, deviceList
) {
787 m_availableDevices
<< device
.udi();
791 int PlacesItemModel::bookmarkIndex(int index
) const
793 int bookmarkIndex
= 0;
795 while (bookmarkIndex
< m_bookmarkedItems
.count()) {
796 if (!m_bookmarkedItems
[bookmarkIndex
]) {
797 if (modelIndex
== index
) {
805 return bookmarkIndex
>= m_bookmarkedItems
.count() ? -1 : bookmarkIndex
;
808 void PlacesItemModel::hideItem(int index
)
810 PlacesItem
* shownItem
= placesItem(index
);
815 shownItem
->setHidden(true);
816 if (m_hiddenItemsShown
) {
817 // Removing items from the model is not allowed if all hidden
818 // items should be shown.
822 const int newIndex
= bookmarkIndex(index
);
824 const KBookmark hiddenBookmark
= shownItem
->bookmark();
825 PlacesItem
* hiddenItem
= new PlacesItem(hiddenBookmark
);
827 const PlacesItem
* previousItem
= placesItem(index
- 1);
828 KBookmark previousBookmark
;
830 previousBookmark
= previousItem
->bookmark();
833 const bool updateBookmark
= (m_bookmarkManager
->root().indexOf(hiddenBookmark
) >= 0);
836 if (updateBookmark
) {
837 // removeItem() also removed the bookmark from m_bookmarkManager in
838 // PlacesItemModel::onItemRemoved(). However for hidden items the
839 // bookmark should still be remembered, so readd it again:
840 m_bookmarkManager
->root().addBookmark(hiddenBookmark
);
841 m_bookmarkManager
->root().moveBookmark(hiddenBookmark
, previousBookmark
);
842 triggerBookmarksSaving();
845 m_bookmarkedItems
.insert(newIndex
, hiddenItem
);
849 void PlacesItemModel::triggerBookmarksSaving()
851 if (m_saveBookmarksTimer
) {
852 m_saveBookmarksTimer
->start();
856 bool PlacesItemModel::equalBookmarkIdentifiers(const KBookmark
& b1
, const KBookmark
& b2
)
858 const QString udi1
= b1
.metaDataItem("UDI");
859 const QString udi2
= b2
.metaDataItem("UDI");
860 if (!udi1
.isEmpty() && !udi2
.isEmpty()) {
863 return b1
.metaDataItem("ID") == b2
.metaDataItem("ID");
867 KUrl
PlacesItemModel::createTimelineUrl(const KUrl
& url
)
869 // TODO: Clarify with the Nepomuk-team whether it makes sense
870 // provide default-timeline-URLs like 'yesterday', 'this month'
874 const QString path
= url
.pathOrUrl();
875 if (path
.endsWith("yesterday")) {
876 const QDate date
= QDate::currentDate().addDays(-1);
877 const int year
= date
.year();
878 const int month
= date
.month();
879 const int day
= date
.day();
880 timelineUrl
= "timeline:/" + timelineDateString(year
, month
) +
881 '/' + timelineDateString(year
, month
, day
);
882 } else if (path
.endsWith("thismonth")) {
883 const QDate date
= QDate::currentDate();
884 timelineUrl
= "timeline:/" + timelineDateString(date
.year(), date
.month());
885 } else if (path
.endsWith("lastmonth")) {
886 const QDate date
= QDate::currentDate().addMonths(-1);
887 timelineUrl
= "timeline:/" + timelineDateString(date
.year(), date
.month());
889 Q_ASSERT(path
.endsWith("today"));
896 QString
PlacesItemModel::timelineDateString(int year
, int month
, int day
)
898 QString date
= QString::number(year
) + '-';
902 date
+= QString::number(month
);
909 date
+= QString::number(day
);
915 KUrl
PlacesItemModel::createSearchUrl(const KUrl
& url
)
920 const QString path
= url
.pathOrUrl();
921 if (path
.endsWith("documents")) {
922 searchUrl
= searchUrlForTerm(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Document()));
923 } else if (path
.endsWith("images")) {
924 searchUrl
= searchUrlForTerm(Nepomuk::Query::ResourceTypeTerm(Nepomuk::Vocabulary::NFO::Image()));
925 } else if (path
.endsWith("audio")) {
926 searchUrl
= searchUrlForTerm(Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
927 Nepomuk::Query::LiteralTerm("audio")));
928 } else if (path
.endsWith("videos")) {
929 searchUrl
= searchUrlForTerm(Nepomuk::Query::ComparisonTerm(Nepomuk::Vocabulary::NIE::mimeType(),
930 Nepomuk::Query::LiteralTerm("video")));
942 KUrl
PlacesItemModel::searchUrlForTerm(const Nepomuk::Query::Term
& term
)
944 const Nepomuk::Query::Query
query(term
);
945 return query
.toSearchUrl();
949 #ifdef PLACESITEMMODEL_DEBUG
950 void PlacesItemModel::showModelState()
952 kDebug() << "=================================";
953 kDebug() << "Model:";
954 kDebug() << "hidden-index model-index text";
956 for (int i
= 0; i
< m_bookmarkedItems
.count(); ++i
) {
957 if (m_bookmarkedItems
[i
]) {
958 kDebug() << i
<< "(Hidden) " << " " << m_bookmarkedItems
[i
]->dataValue("text").toString();
960 if (item(modelIndex
)) {
961 kDebug() << i
<< " " << modelIndex
<< " " << item(modelIndex
)->dataValue("text").toString();
963 kDebug() << i
<< " " << modelIndex
<< " " << "(not available yet)";
970 kDebug() << "Bookmarks:";
972 int bookmarkIndex
= 0;
973 KBookmarkGroup root
= m_bookmarkManager
->root();
974 KBookmark bookmark
= root
.first();
975 while (!bookmark
.isNull()) {
976 const QString udi
= bookmark
.metaDataItem("UDI");
977 const QString text
= udi
.isEmpty() ? bookmark
.text() : udi
;
978 if (bookmark
.metaDataItem("IsHidden") == QLatin1String("true")) {
979 kDebug() << bookmarkIndex
<< "(Hidden)" << text
;
981 kDebug() << bookmarkIndex
<< " " << text
;
984 bookmark
= root
.next(bookmark
);
990 #include "placesitemmodel.moc"