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"
27 #include "dolphindebug.h"
28 #include "dolphinplacesmodelsingleton.h"
29 #include "placesitem.h"
30 #include "placesitemsignalhandler.h"
31 #include "views/dolphinview.h"
32 #include "views/viewproperties.h"
35 #include <KLocalizedString>
36 #include <KUrlMimeData>
37 #include <Solid/DeviceNotifier>
38 #include <Solid/OpticalDrive>
39 #include <KCoreAddons/KProcessList>
40 #include <KCoreAddons/KListOpenFilesJob>
48 static QList
<QUrl
> balooURLs
= {
49 QUrl(QStringLiteral("timeline:/today")),
50 QUrl(QStringLiteral("timeline:/yesterday")),
51 QUrl(QStringLiteral("timeline:/thismonth")),
52 QUrl(QStringLiteral("timeline:/lastmonth")),
53 QUrl(QStringLiteral("search:/documents")),
54 QUrl(QStringLiteral("search:/images")),
55 QUrl(QStringLiteral("search:/audio")),
56 QUrl(QStringLiteral("search:/videos"))
60 PlacesItemModel::PlacesItemModel(QObject
* parent
) :
61 KStandardItemModel(parent
),
62 m_hiddenItemsShown(false),
63 m_deviceToTearDown(nullptr),
64 m_storageSetupInProgress(),
65 m_sourceModel(DolphinPlacesModelSingleton::instance().placesModel())
69 initializeDefaultViewProperties();
71 connect(m_sourceModel
, &KFilePlacesModel::rowsInserted
, this, &PlacesItemModel::onSourceModelRowsInserted
);
72 connect(m_sourceModel
, &KFilePlacesModel::rowsAboutToBeRemoved
, this, &PlacesItemModel::onSourceModelRowsAboutToBeRemoved
);
73 connect(m_sourceModel
, &KFilePlacesModel::dataChanged
, this, &PlacesItemModel::onSourceModelDataChanged
);
74 connect(m_sourceModel
, &KFilePlacesModel::rowsAboutToBeMoved
, this, &PlacesItemModel::onSourceModelRowsAboutToBeMoved
);
75 connect(m_sourceModel
, &KFilePlacesModel::rowsMoved
, this, &PlacesItemModel::onSourceModelRowsMoved
);
76 connect(m_sourceModel
, &KFilePlacesModel::groupHiddenChanged
, this, &PlacesItemModel::onSourceModelGroupHiddenChanged
);
79 PlacesItemModel::~PlacesItemModel()
83 void PlacesItemModel::createPlacesItem(const QString
&text
, const QUrl
&url
, const QString
&iconName
, const QString
&appName
)
85 createPlacesItem(text
, url
, iconName
, appName
, -1);
88 void PlacesItemModel::createPlacesItem(const QString
&text
, const QUrl
&url
, const QString
&iconName
, const QString
&appName
, int after
)
90 m_sourceModel
->addPlace(text
, url
, iconName
, appName
, mapToSource(after
));
93 PlacesItem
* PlacesItemModel::placesItem(int index
) const
95 return dynamic_cast<PlacesItem
*>(item(index
));
98 int PlacesItemModel::hiddenCount() const
100 return m_sourceModel
->hiddenCount();
103 void PlacesItemModel::setHiddenItemsShown(bool show
)
105 if (m_hiddenItemsShown
== show
) {
109 m_hiddenItemsShown
= show
;
112 for (int r
= 0, rMax
= m_sourceModel
->rowCount(); r
< rMax
; r
++) {
113 const QModelIndex index
= m_sourceModel
->index(r
, 0);
114 if (!m_sourceModel
->isHidden(index
)) {
117 addItemFromSourceModel(index
);
120 for (int r
= 0, rMax
= m_sourceModel
->rowCount(); r
< rMax
; r
++) {
121 const QModelIndex index
= m_sourceModel
->index(r
, 0);
122 if (m_sourceModel
->isHidden(index
)) {
123 removeItemByIndex(index
);
129 bool PlacesItemModel::hiddenItemsShown() const
131 return m_hiddenItemsShown
;
134 int PlacesItemModel::closestItem(const QUrl
& url
) const
136 return mapFromSource(m_sourceModel
->closestItem(url
));
139 // look for the correct position for the item based on source model
140 void PlacesItemModel::insertSortedItem(PlacesItem
* item
)
146 const KBookmark iBookmark
= item
->bookmark();
147 const QString iBookmarkId
= bookmarkId(iBookmark
);
148 QModelIndex sourceIndex
;
151 for(int r
= 0, rMax
= m_sourceModel
->rowCount(); r
< rMax
; r
++) {
152 sourceIndex
= m_sourceModel
->index(r
, 0);
153 const KBookmark sourceBookmark
= m_sourceModel
->bookmarkForIndex(sourceIndex
);
155 if (bookmarkId(sourceBookmark
) == iBookmarkId
) {
159 if (m_hiddenItemsShown
|| !m_sourceModel
->isHidden(sourceIndex
)) {
164 m_indexMap
.insert(pos
, sourceIndex
);
165 insertItem(pos
, item
);
168 void PlacesItemModel::onItemInserted(int index
)
170 KStandardItemModel::onItemInserted(index
);
173 void PlacesItemModel::onItemRemoved(int index
, KStandardItem
* removedItem
)
175 m_indexMap
.removeAt(index
);
177 KStandardItemModel::onItemRemoved(index
, removedItem
);
180 void PlacesItemModel::onItemChanged(int index
, const QSet
<QByteArray
>& changedRoles
)
182 const QModelIndex sourceIndex
= mapToSource(index
);
183 const PlacesItem
*changedItem
= placesItem(mapFromSource(sourceIndex
));
185 if (!changedItem
|| !sourceIndex
.isValid()) {
186 qWarning() << "invalid item changed signal";
189 if (changedRoles
.contains("isHidden")) {
190 if (m_sourceModel
->isHidden(sourceIndex
) != changedItem
->isHidden()) {
191 m_sourceModel
->setPlaceHidden(sourceIndex
, changedItem
->isHidden());
193 m_sourceModel
->refresh();
196 KStandardItemModel::onItemChanged(index
, changedRoles
);
199 QAction
* PlacesItemModel::ejectAction(int index
) const
201 const PlacesItem
* item
= placesItem(index
);
202 if (item
&& item
->device().is
<Solid::OpticalDisc
>()) {
203 return new QAction(QIcon::fromTheme(QStringLiteral("media-eject")), i18nc("@item", "Eject"), nullptr);
209 QAction
* PlacesItemModel::teardownAction(int index
) const
211 const PlacesItem
* item
= placesItem(index
);
216 Solid::Device device
= item
->device();
217 const bool providesTearDown
= device
.is
<Solid::StorageAccess
>() &&
218 device
.as
<Solid::StorageAccess
>()->isAccessible();
219 if (!providesTearDown
) {
223 Solid::StorageDrive
* drive
= device
.as
<Solid::StorageDrive
>();
225 drive
= device
.parent().as
<Solid::StorageDrive
>();
228 bool hotPluggable
= false;
229 bool removable
= false;
231 hotPluggable
= drive
->isHotpluggable();
232 removable
= drive
->isRemovable();
237 if (device
.is
<Solid::OpticalDisc
>()) {
238 text
= i18nc("@item", "Release");
239 } else if (removable
|| hotPluggable
) {
240 text
= i18nc("@item", "Safely Remove");
241 iconName
= QStringLiteral("media-eject");
243 text
= i18nc("@item", "Unmount");
244 iconName
= QStringLiteral("media-eject");
247 if (iconName
.isEmpty()) {
248 return new QAction(text
, nullptr);
251 return new QAction(QIcon::fromTheme(iconName
), text
, nullptr);
254 void PlacesItemModel::requestEject(int index
)
256 const PlacesItem
* item
= placesItem(index
);
258 Solid::OpticalDrive
* drive
= item
->device().parent().as
<Solid::OpticalDrive
>();
260 connect(drive
, &Solid::OpticalDrive::ejectDone
,
261 this, &PlacesItemModel::slotStorageTearDownDone
);
264 const QString label
= item
->text();
265 const QString message
= i18nc("@info", "The device '%1' is not a disk and cannot be ejected.", label
);
266 emit
errorMessage(message
);
271 void PlacesItemModel::requestTearDown(int index
)
273 const PlacesItem
* item
= placesItem(index
);
275 Solid::StorageAccess
*tmp
= item
->device().as
<Solid::StorageAccess
>();
277 m_deviceToTearDown
= tmp
;
278 // disconnect the Solid::StorageAccess::teardownRequested
279 // to prevent emitting PlacesItemModel::storageTearDownExternallyRequested
280 // after we have emitted PlacesItemModel::storageTearDownRequested
281 disconnect(tmp
, &Solid::StorageAccess::teardownRequested
,
282 item
->signalHandler(), &PlacesItemSignalHandler::onTearDownRequested
);
283 emit
storageTearDownRequested(tmp
->filePath());
288 bool PlacesItemModel::storageSetupNeeded(int index
) const
290 const PlacesItem
* item
= placesItem(index
);
291 return item
? item
->storageSetupNeeded() : false;
294 void PlacesItemModel::requestStorageSetup(int index
)
296 const PlacesItem
* item
= placesItem(index
);
301 Solid::Device device
= item
->device();
302 const bool setup
= device
.is
<Solid::StorageAccess
>()
303 && !m_storageSetupInProgress
.contains(device
.as
<Solid::StorageAccess
>())
304 && !device
.as
<Solid::StorageAccess
>()->isAccessible();
306 Solid::StorageAccess
* access
= device
.as
<Solid::StorageAccess
>();
308 m_storageSetupInProgress
[access
] = index
;
310 connect(access
, &Solid::StorageAccess::setupDone
,
311 this, &PlacesItemModel::slotStorageSetupDone
);
317 QMimeData
* PlacesItemModel::createMimeData(const KItemSet
& indexes
) const
322 QDataStream
stream(&itemData
, QIODevice::WriteOnly
);
324 for (int index
: indexes
) {
325 const QUrl itemUrl
= placesItem(index
)->url();
326 if (itemUrl
.isValid()) {
332 QMimeData
* mimeData
= new QMimeData();
333 if (!urls
.isEmpty()) {
334 mimeData
->setUrls(urls
);
336 // #378954: prevent itemDropEvent() drops if there isn't a source url.
337 mimeData
->setData(blacklistItemDropEventMimeType(), QByteArrayLiteral("true"));
339 mimeData
->setData(internalMimeType(), itemData
);
344 bool PlacesItemModel::supportsDropping(int index
) const
346 return index
>= 0 && index
< count();
349 void PlacesItemModel::dropMimeDataBefore(int index
, const QMimeData
* mimeData
)
351 if (mimeData
->hasFormat(internalMimeType())) {
352 // The item has been moved inside the view
353 QByteArray itemData
= mimeData
->data(internalMimeType());
354 QDataStream
stream(&itemData
, QIODevice::ReadOnly
);
358 QModelIndex sourceIndex
= mapToSource(index
);
359 QModelIndex oldSourceIndex
= mapToSource(oldIndex
);
361 m_sourceModel
->movePlace(oldSourceIndex
.row(), sourceIndex
.row());
362 } else if (mimeData
->hasFormat(QStringLiteral("text/uri-list"))) {
363 // One or more items must be added to the model
364 const QList
<QUrl
> urls
= KUrlMimeData::urlsFromMimeData(mimeData
);
365 for (int i
= urls
.count() - 1; i
>= 0; --i
) {
366 const QUrl
& url
= urls
[i
];
368 QString text
= url
.fileName();
369 if (text
.isEmpty()) {
373 if ((url
.isLocalFile() && !QFileInfo(url
.toLocalFile()).isDir())
374 || url
.scheme() == QLatin1String("trash")) {
375 // Only directories outside the trash are allowed
379 createPlacesItem(text
, url
, KIO::iconNameForUrl(url
), {}, qMax(0, index
- 1));
382 // will save bookmark alteration and fix sort if that is broken by the drag/drop operation
386 void PlacesItemModel::addItemFromSourceModel(const QModelIndex
&index
)
388 if (!m_hiddenItemsShown
&& m_sourceModel
->isHidden(index
)) {
392 const KBookmark bookmark
= m_sourceModel
->bookmarkForIndex(index
);
393 Q_ASSERT(!bookmark
.isNull());
394 PlacesItem
*item
= new PlacesItem(bookmark
);
395 updateItem(item
, index
);
396 insertSortedItem(item
);
398 if (m_sourceModel
->isDevice(index
)) {
399 connect(item
->signalHandler(), &PlacesItemSignalHandler::tearDownExternallyRequested
,
400 this, &PlacesItemModel::storageTearDownExternallyRequested
);
404 void PlacesItemModel::removeItemByIndex(const QModelIndex
&sourceIndex
)
406 QString id
= bookmarkId(m_sourceModel
->bookmarkForIndex(sourceIndex
));
408 for (int i
= 0, iMax
= count(); i
< iMax
; ++i
) {
409 if (bookmarkId(placesItem(i
)->bookmark()) == id
) {
416 QString
PlacesItemModel::bookmarkId(const KBookmark
&bookmark
) const
418 QString id
= bookmark
.metaDataItem(QStringLiteral("UDI"));
420 id
= bookmark
.metaDataItem(QStringLiteral("ID"));
425 void PlacesItemModel::initializeDefaultViewProperties() const
427 for(int i
= 0, iMax
= m_sourceModel
->rowCount(); i
< iMax
; i
++) {
428 const QModelIndex index
= m_sourceModel
->index(i
, 0);
429 const PlacesItem
*item
= placesItem(mapFromSource(index
));
434 // Create default view-properties for all "Search For" and "Recently Saved" bookmarks
435 // in case the user has not already created custom view-properties for a corresponding
437 const bool createDefaultViewProperties
= item
->isSearchOrTimelineUrl() && !GeneralSettings::self()->globalViewProps();
438 if (createDefaultViewProperties
) {
439 const QUrl itemUrl
= item
->url();
440 ViewProperties
props(KFilePlacesModel::convertedUrl(itemUrl
));
441 if (!props
.exist()) {
442 const QString path
= itemUrl
.path();
443 if (path
== QLatin1String("/documents")) {
444 props
.setViewMode(DolphinView::DetailsView
);
445 props
.setPreviewsShown(false);
446 props
.setVisibleRoles({"text", "path"});
447 } else if (path
== QLatin1String("/images")) {
448 props
.setViewMode(DolphinView::IconsView
);
449 props
.setPreviewsShown(true);
450 props
.setVisibleRoles({"text", "height", "width"});
451 } else if (path
== QLatin1String("/audio")) {
452 props
.setViewMode(DolphinView::DetailsView
);
453 props
.setPreviewsShown(false);
454 props
.setVisibleRoles({"text", "artist", "album"});
455 } else if (path
== QLatin1String("/videos")) {
456 props
.setViewMode(DolphinView::IconsView
);
457 props
.setPreviewsShown(true);
458 props
.setVisibleRoles({"text"});
459 } else if (itemUrl
.scheme() == QLatin1String("timeline")) {
460 props
.setViewMode(DolphinView::DetailsView
);
461 props
.setVisibleRoles({"text", "modificationtime"});
469 void PlacesItemModel::updateItem(PlacesItem
*item
, const QModelIndex
&index
)
471 item
->setGroup(index
.data(KFilePlacesModel::GroupRole
).toString());
472 item
->setIcon(index
.data(KFilePlacesModel::IconNameRole
).toString());
473 item
->setGroupHidden(index
.data(KFilePlacesModel::GroupHiddenRole
).toBool());
476 void PlacesItemModel::slotStorageTearDownDone(Solid::ErrorType error
, const QVariant
& errorData
)
478 if (error
&& errorData
.isValid()) {
479 if (error
== Solid::ErrorType::DeviceBusy
) {
480 KListOpenFilesJob
* listOpenFilesJob
= new KListOpenFilesJob(m_deviceToTearDown
->filePath());
481 connect(listOpenFilesJob
, &KIO::Job::result
, this, [this, listOpenFilesJob
](KJob
*) {
482 const KProcessList::KProcessInfoList blockingProcesses
= listOpenFilesJob
->processInfoList();
484 if (blockingProcesses
.isEmpty()) {
485 errorString
= i18n("One or more files on this device are open within an application.");
487 QStringList blockingApps
;
488 for (const auto& process
: blockingProcesses
) {
489 blockingApps
<< process
.name();
491 blockingApps
.removeDuplicates();
492 errorString
= xi18np("One or more files on this device are opened in application <application>\"%2\"</application>.",
493 "One or more files on this device are opened in following applications: <application>%2</application>.",
494 blockingApps
.count(), blockingApps
.join(i18nc("separator in list of apps blocking device unmount", ", ")));
496 emit
errorMessage(errorString
);
498 listOpenFilesJob
->start();
500 emit
errorMessage(errorData
.toString());
503 disconnect(m_deviceToTearDown
, &Solid::StorageAccess::teardownDone
,
504 this, &PlacesItemModel::slotStorageTearDownDone
);
505 m_deviceToTearDown
= nullptr;
508 void PlacesItemModel::slotStorageSetupDone(Solid::ErrorType error
,
509 const QVariant
& errorData
,
514 const int index
= m_storageSetupInProgress
.take(sender());
515 const PlacesItem
* item
= placesItem(index
);
520 if (error
!= Solid::NoError
) {
521 if (errorData
.isValid()) {
522 emit
errorMessage(i18nc("@info", "An error occurred while accessing '%1', the system responded: %2",
524 errorData
.toString()));
526 emit
errorMessage(i18nc("@info", "An error occurred while accessing '%1'",
529 emit
storageSetupDone(index
, false);
531 emit
storageSetupDone(index
, true);
535 void PlacesItemModel::onSourceModelRowsInserted(const QModelIndex
&parent
, int first
, int last
)
537 for (int i
= first
; i
<= last
; i
++) {
538 const QModelIndex index
= m_sourceModel
->index(i
, 0, parent
);
539 addItemFromSourceModel(index
);
543 void PlacesItemModel::onSourceModelRowsAboutToBeRemoved(const QModelIndex
&parent
, int first
, int last
)
545 for(int r
= first
; r
<= last
; r
++) {
546 const QModelIndex index
= m_sourceModel
->index(r
, 0, parent
);
547 int oldIndex
= mapFromSource(index
);
548 if (oldIndex
!= -1) {
549 removeItem(oldIndex
);
554 void PlacesItemModel::onSourceModelRowsAboutToBeMoved(const QModelIndex
&parent
, int start
, int end
, const QModelIndex
&destination
, int row
)
556 Q_UNUSED(destination
);
559 for(int r
= start
; r
<= end
; r
++) {
560 const QModelIndex sourceIndex
= m_sourceModel
->index(r
, 0, parent
);
562 removeItem(mapFromSource(sourceIndex
));
566 void PlacesItemModel::onSourceModelRowsMoved(const QModelIndex
&parent
, int start
, int end
, const QModelIndex
&destination
, int row
)
568 Q_UNUSED(destination
);
571 const int blockSize
= (end
- start
) + 1;
573 for (int r
= start
; r
<= end
; r
++) {
574 // insert the moved item in the new position
575 const int targetRow
= row
+ (start
- r
) - (r
< row
? blockSize
: 0);
576 const QModelIndex targetIndex
= m_sourceModel
->index(targetRow
, 0, destination
);
578 addItemFromSourceModel(targetIndex
);
582 void PlacesItemModel::onSourceModelDataChanged(const QModelIndex
&topLeft
, const QModelIndex
&bottomRight
, const QVector
<int> &roles
)
586 for (int r
= topLeft
.row(); r
<= bottomRight
.row(); r
++) {
587 const QModelIndex sourceIndex
= m_sourceModel
->index(r
, 0);
588 const KBookmark bookmark
= m_sourceModel
->bookmarkForIndex(sourceIndex
);
589 PlacesItem
*placeItem
= itemFromBookmark(bookmark
);
591 if (placeItem
&& (!m_hiddenItemsShown
&& m_sourceModel
->isHidden(sourceIndex
))) {
592 //hide item if it became invisible
593 removeItem(index(placeItem
));
597 if (!placeItem
&& (m_hiddenItemsShown
|| !m_sourceModel
->isHidden(sourceIndex
))) {
598 //show item if it became visible
599 addItemFromSourceModel(sourceIndex
);
603 if (placeItem
&& !m_sourceModel
->isDevice(sourceIndex
)) {
604 placeItem
->setText(bookmark
.text());
605 placeItem
->setIcon(sourceIndex
.data(KFilePlacesModel::IconNameRole
).toString());
606 placeItem
->setUrl(m_sourceModel
->url(sourceIndex
));
607 placeItem
->bookmark().setMetaDataItem(QStringLiteral("OnlyInApp"),
608 bookmark
.metaDataItem(QStringLiteral("OnlyInApp")));
609 // must update the bookmark object
610 placeItem
->setBookmark(bookmark
);
615 void PlacesItemModel::onSourceModelGroupHiddenChanged(KFilePlacesModel::GroupType group
, bool hidden
)
617 const auto groupIndexes
= m_sourceModel
->groupIndexes(group
);
618 for (const QModelIndex
&sourceIndex
: groupIndexes
) {
619 PlacesItem
*item
= placesItem(mapFromSource(sourceIndex
));
621 item
->setGroupHidden(hidden
);
626 void PlacesItemModel::cleanupBookmarks()
628 // KIO model now provides support for baloo urls, and because of that we
629 // need to remove old URLs that were visible only in Dolphin to avoid duplication
632 const QModelIndex sourceIndex
= m_sourceModel
->index(row
, 0);
633 const KBookmark bookmark
= m_sourceModel
->bookmarkForIndex(sourceIndex
);
634 const QUrl url
= bookmark
.url();
635 const QString appName
= bookmark
.metaDataItem(QStringLiteral("OnlyInApp"));
637 if ((appName
== KAboutData::applicationData().componentName() ||
638 appName
== KAboutData::applicationData().componentName() + DolphinPlacesModelSingleton::applicationNameSuffix()) && balooURLs
.contains(url
)) {
639 qCDebug(DolphinDebug
) << "Removing old baloo url:" << url
;
640 m_sourceModel
->removePlace(sourceIndex
);
644 } while (row
< m_sourceModel
->rowCount());
647 void PlacesItemModel::loadBookmarks()
649 for(int r
= 0, rMax
= m_sourceModel
->rowCount(); r
< rMax
; r
++) {
650 const QModelIndex sourceIndex
= m_sourceModel
->index(r
, 0);
651 if (m_hiddenItemsShown
|| !m_sourceModel
->isHidden(sourceIndex
)) {
652 addItemFromSourceModel(sourceIndex
);
657 void PlacesItemModel::clear() {
658 KStandardItemModel::clear();
661 void PlacesItemModel::proceedWithTearDown()
663 Q_ASSERT(m_deviceToTearDown
);
665 connect(m_deviceToTearDown
, &Solid::StorageAccess::teardownDone
,
666 this, &PlacesItemModel::slotStorageTearDownDone
);
667 m_deviceToTearDown
->teardown();
670 void PlacesItemModel::deleteItem(int index
)
672 QModelIndex sourceIndex
= mapToSource(index
);
673 Q_ASSERT(sourceIndex
.isValid());
674 m_sourceModel
->removePlace(sourceIndex
);
677 void PlacesItemModel::refresh()
679 m_sourceModel
->refresh();
682 void PlacesItemModel::hideItem(int index
)
684 PlacesItem
* shownItem
= placesItem(index
);
689 shownItem
->setHidden(true);
692 QString
PlacesItemModel::internalMimeType() const
694 return "application/x-dolphinplacesmodel-" +
695 QString::number((qptrdiff
)this);
698 int PlacesItemModel::groupedDropIndex(int index
, const PlacesItem
* item
) const
702 int dropIndex
= index
;
703 const QString group
= item
->group();
705 const int itemCount
= count();
707 dropIndex
= itemCount
;
710 // Search nearest previous item with the same group
711 int previousIndex
= -1;
712 for (int i
= dropIndex
- 1; i
>= 0; --i
) {
713 if (placesItem(i
)->group() == group
) {
719 // Search nearest next item with the same group
721 for (int i
= dropIndex
; i
< count(); ++i
) {
722 if (placesItem(i
)->group() == group
) {
728 // Adjust the drop-index to be inserted to the
729 // nearest item with the same group.
730 if (previousIndex
>= 0 && nextIndex
>= 0) {
731 dropIndex
= (dropIndex
- previousIndex
< nextIndex
- dropIndex
) ?
732 previousIndex
+ 1 : nextIndex
;
733 } else if (previousIndex
>= 0) {
734 dropIndex
= previousIndex
+ 1;
735 } else if (nextIndex
>= 0) {
736 dropIndex
= nextIndex
;
742 bool PlacesItemModel::equalBookmarkIdentifiers(const KBookmark
& b1
, const KBookmark
& b2
)
744 const QString udi1
= b1
.metaDataItem(QStringLiteral("UDI"));
745 const QString udi2
= b2
.metaDataItem(QStringLiteral("UDI"));
746 if (!udi1
.isEmpty() && !udi2
.isEmpty()) {
749 return b1
.metaDataItem(QStringLiteral("ID")) == b2
.metaDataItem(QStringLiteral("ID"));
753 int PlacesItemModel::mapFromSource(const QModelIndex
&index
) const
755 if (!index
.isValid()) {
759 return m_indexMap
.indexOf(index
);
762 bool PlacesItemModel::isDir(int index
) const
768 KFilePlacesModel::GroupType
PlacesItemModel::groupType(int row
) const
770 return m_sourceModel
->groupType(mapToSource(row
));
773 bool PlacesItemModel::isGroupHidden(KFilePlacesModel::GroupType type
) const
775 return m_sourceModel
->isGroupHidden(type
);
778 void PlacesItemModel::setGroupHidden(KFilePlacesModel::GroupType type
, bool hidden
)
780 return m_sourceModel
->setGroupHidden(type
, hidden
);
783 QModelIndex
PlacesItemModel::mapToSource(int row
) const
785 return m_indexMap
.value(row
);
788 PlacesItem
*PlacesItemModel::itemFromBookmark(const KBookmark
&bookmark
) const
790 const QString id
= bookmarkId(bookmark
);
791 for (int i
= 0, iMax
= count(); i
< iMax
; i
++) {
792 PlacesItem
*item
= placesItem(i
);
793 const KBookmark itemBookmark
= item
->bookmark();
794 if (bookmarkId(itemBookmark
) == id
) {