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>
47 PlacesItemModel::PlacesItemModel(QObject
* parent
) :
48 KStandardItemModel(parent
),
49 m_hiddenItemsShown(false),
50 m_deviceToTearDown(nullptr),
51 m_storageSetupInProgress(),
52 m_sourceModel(DolphinPlacesModelSingleton::instance().placesModel())
56 initializeDefaultViewProperties();
58 connect(m_sourceModel
, &KFilePlacesModel::rowsInserted
, this, &PlacesItemModel::onSourceModelRowsInserted
);
59 connect(m_sourceModel
, &KFilePlacesModel::rowsAboutToBeRemoved
, this, &PlacesItemModel::onSourceModelRowsAboutToBeRemoved
);
60 connect(m_sourceModel
, &KFilePlacesModel::dataChanged
, this, &PlacesItemModel::onSourceModelDataChanged
);
61 connect(m_sourceModel
, &KFilePlacesModel::rowsAboutToBeMoved
, this, &PlacesItemModel::onSourceModelRowsAboutToBeMoved
);
62 connect(m_sourceModel
, &KFilePlacesModel::rowsMoved
, this, &PlacesItemModel::onSourceModelRowsMoved
);
63 connect(m_sourceModel
, &KFilePlacesModel::groupHiddenChanged
, this, &PlacesItemModel::onSourceModelGroupHiddenChanged
);
66 PlacesItemModel::~PlacesItemModel()
70 void PlacesItemModel::createPlacesItem(const QString
&text
, const QUrl
&url
, const QString
&iconName
, const QString
&appName
)
72 createPlacesItem(text
, url
, iconName
, appName
, -1);
75 void PlacesItemModel::createPlacesItem(const QString
&text
, const QUrl
&url
, const QString
&iconName
, const QString
&appName
, int after
)
77 m_sourceModel
->addPlace(text
, url
, iconName
, appName
, mapToSource(after
));
80 PlacesItem
* PlacesItemModel::placesItem(int index
) const
82 return dynamic_cast<PlacesItem
*>(item(index
));
85 int PlacesItemModel::hiddenCount() const
87 return m_sourceModel
->hiddenCount();
90 void PlacesItemModel::setHiddenItemsShown(bool show
)
92 if (m_hiddenItemsShown
== show
) {
96 m_hiddenItemsShown
= show
;
99 for (int r
= 0, rMax
= m_sourceModel
->rowCount(); r
< rMax
; r
++) {
100 const QModelIndex index
= m_sourceModel
->index(r
, 0);
101 if (!m_sourceModel
->isHidden(index
)) {
104 addItemFromSourceModel(index
);
107 for (int r
= 0, rMax
= m_sourceModel
->rowCount(); r
< rMax
; r
++) {
108 const QModelIndex index
= m_sourceModel
->index(r
, 0);
109 if (m_sourceModel
->isHidden(index
)) {
110 removeItemByIndex(index
);
116 bool PlacesItemModel::hiddenItemsShown() const
118 return m_hiddenItemsShown
;
121 int PlacesItemModel::closestItem(const QUrl
& url
) const
123 return mapFromSource(m_sourceModel
->closestItem(url
));
126 // look for the correct position for the item based on source model
127 void PlacesItemModel::insertSortedItem(PlacesItem
* item
)
133 const KBookmark iBookmark
= item
->bookmark();
134 const QString iBookmarkId
= bookmarkId(iBookmark
);
135 QModelIndex sourceIndex
;
138 for(int r
= 0, rMax
= m_sourceModel
->rowCount(); r
< rMax
; r
++) {
139 sourceIndex
= m_sourceModel
->index(r
, 0);
140 const KBookmark sourceBookmark
= m_sourceModel
->bookmarkForIndex(sourceIndex
);
142 if (bookmarkId(sourceBookmark
) == iBookmarkId
) {
146 if (m_hiddenItemsShown
|| !m_sourceModel
->isHidden(sourceIndex
)) {
151 m_indexMap
.insert(pos
, sourceIndex
);
152 insertItem(pos
, item
);
155 void PlacesItemModel::onItemInserted(int index
)
157 KStandardItemModel::onItemInserted(index
);
160 void PlacesItemModel::onItemRemoved(int index
, KStandardItem
* removedItem
)
162 m_indexMap
.removeAt(index
);
164 KStandardItemModel::onItemRemoved(index
, removedItem
);
167 void PlacesItemModel::onItemChanged(int index
, const QSet
<QByteArray
>& changedRoles
)
169 const QModelIndex sourceIndex
= mapToSource(index
);
170 const PlacesItem
*changedItem
= placesItem(mapFromSource(sourceIndex
));
172 if (!changedItem
|| !sourceIndex
.isValid()) {
173 qWarning() << "invalid item changed signal";
176 if (changedRoles
.contains("isHidden")) {
177 if (m_sourceModel
->isHidden(sourceIndex
) != changedItem
->isHidden()) {
178 m_sourceModel
->setPlaceHidden(sourceIndex
, changedItem
->isHidden());
180 m_sourceModel
->refresh();
183 KStandardItemModel::onItemChanged(index
, changedRoles
);
186 QAction
* PlacesItemModel::ejectAction(int index
) const
188 const PlacesItem
* item
= placesItem(index
);
189 if (item
&& item
->device().is
<Solid::OpticalDisc
>()) {
190 return new QAction(QIcon::fromTheme(QStringLiteral("media-eject")), i18nc("@item", "Eject"), nullptr);
196 QAction
* PlacesItemModel::teardownAction(int index
) const
198 const PlacesItem
* item
= placesItem(index
);
203 Solid::Device device
= item
->device();
204 const bool providesTearDown
= device
.is
<Solid::StorageAccess
>() &&
205 device
.as
<Solid::StorageAccess
>()->isAccessible();
206 if (!providesTearDown
) {
210 Solid::StorageDrive
* drive
= device
.as
<Solid::StorageDrive
>();
212 drive
= device
.parent().as
<Solid::StorageDrive
>();
215 bool hotPluggable
= false;
216 bool removable
= false;
218 hotPluggable
= drive
->isHotpluggable();
219 removable
= drive
->isRemovable();
224 if (device
.is
<Solid::OpticalDisc
>()) {
225 text
= i18nc("@item", "Release");
226 } else if (removable
|| hotPluggable
) {
227 text
= i18nc("@item", "Safely Remove");
228 iconName
= QStringLiteral("media-eject");
230 text
= i18nc("@item", "Unmount");
231 iconName
= QStringLiteral("media-eject");
234 if (iconName
.isEmpty()) {
235 return new QAction(text
, nullptr);
238 return new QAction(QIcon::fromTheme(iconName
), text
, nullptr);
241 void PlacesItemModel::requestEject(int index
)
243 const PlacesItem
* item
= placesItem(index
);
245 Solid::OpticalDrive
* drive
= item
->device().parent().as
<Solid::OpticalDrive
>();
247 connect(drive
, &Solid::OpticalDrive::ejectDone
,
248 this, &PlacesItemModel::slotStorageTearDownDone
);
251 const QString label
= item
->text();
252 const QString message
= i18nc("@info", "The device '%1' is not a disk and cannot be ejected.", label
);
253 emit
errorMessage(message
);
258 void PlacesItemModel::requestTearDown(int index
)
260 const PlacesItem
* item
= placesItem(index
);
262 Solid::StorageAccess
*tmp
= item
->device().as
<Solid::StorageAccess
>();
264 m_deviceToTearDown
= tmp
;
265 // disconnect the Solid::StorageAccess::teardownRequested
266 // to prevent emitting PlacesItemModel::storageTearDownExternallyRequested
267 // after we have emitted PlacesItemModel::storageTearDownRequested
268 disconnect(tmp
, &Solid::StorageAccess::teardownRequested
,
269 item
->signalHandler(), &PlacesItemSignalHandler::onTearDownRequested
);
270 emit
storageTearDownRequested(tmp
->filePath());
275 bool PlacesItemModel::storageSetupNeeded(int index
) const
277 const PlacesItem
* item
= placesItem(index
);
278 return item
? item
->storageSetupNeeded() : false;
281 void PlacesItemModel::requestStorageSetup(int index
)
283 const PlacesItem
* item
= placesItem(index
);
288 Solid::Device device
= item
->device();
289 const bool setup
= device
.is
<Solid::StorageAccess
>()
290 && !m_storageSetupInProgress
.contains(device
.as
<Solid::StorageAccess
>())
291 && !device
.as
<Solid::StorageAccess
>()->isAccessible();
293 Solid::StorageAccess
* access
= device
.as
<Solid::StorageAccess
>();
295 m_storageSetupInProgress
[access
] = index
;
297 connect(access
, &Solid::StorageAccess::setupDone
,
298 this, &PlacesItemModel::slotStorageSetupDone
);
304 QMimeData
* PlacesItemModel::createMimeData(const KItemSet
& indexes
) const
309 QDataStream
stream(&itemData
, QIODevice::WriteOnly
);
311 for (int index
: indexes
) {
312 const QUrl itemUrl
= placesItem(index
)->url();
313 if (itemUrl
.isValid()) {
319 QMimeData
* mimeData
= new QMimeData();
320 if (!urls
.isEmpty()) {
321 mimeData
->setUrls(urls
);
323 // #378954: prevent itemDropEvent() drops if there isn't a source url.
324 mimeData
->setData(blacklistItemDropEventMimeType(), QByteArrayLiteral("true"));
326 mimeData
->setData(internalMimeType(), itemData
);
331 bool PlacesItemModel::supportsDropping(int index
) const
333 return index
>= 0 && index
< count();
336 void PlacesItemModel::dropMimeDataBefore(int index
, const QMimeData
* mimeData
)
338 if (mimeData
->hasFormat(internalMimeType())) {
339 // The item has been moved inside the view
340 QByteArray itemData
= mimeData
->data(internalMimeType());
341 QDataStream
stream(&itemData
, QIODevice::ReadOnly
);
345 QModelIndex sourceIndex
= mapToSource(index
);
346 QModelIndex oldSourceIndex
= mapToSource(oldIndex
);
348 m_sourceModel
->movePlace(oldSourceIndex
.row(), sourceIndex
.row());
349 } else if (mimeData
->hasFormat(QStringLiteral("text/uri-list"))) {
350 // One or more items must be added to the model
351 const QList
<QUrl
> urls
= KUrlMimeData::urlsFromMimeData(mimeData
);
352 for (int i
= urls
.count() - 1; i
>= 0; --i
) {
353 const QUrl
& url
= urls
[i
];
355 QString text
= url
.fileName();
356 if (text
.isEmpty()) {
360 if ((url
.isLocalFile() && !QFileInfo(url
.toLocalFile()).isDir())
361 || url
.scheme() == QLatin1String("trash")) {
362 // Only directories outside the trash are allowed
366 createPlacesItem(text
, url
, KIO::iconNameForUrl(url
), {}, qMax(0, index
- 1));
369 // will save bookmark alteration and fix sort if that is broken by the drag/drop operation
373 void PlacesItemModel::addItemFromSourceModel(const QModelIndex
&index
)
375 if (!m_hiddenItemsShown
&& m_sourceModel
->isHidden(index
)) {
379 const KBookmark bookmark
= m_sourceModel
->bookmarkForIndex(index
);
380 Q_ASSERT(!bookmark
.isNull());
381 PlacesItem
*item
= new PlacesItem(bookmark
);
382 updateItem(item
, index
);
383 insertSortedItem(item
);
385 if (m_sourceModel
->isDevice(index
)) {
386 connect(item
->signalHandler(), &PlacesItemSignalHandler::tearDownExternallyRequested
,
387 this, &PlacesItemModel::storageTearDownExternallyRequested
);
391 void PlacesItemModel::removeItemByIndex(const QModelIndex
&sourceIndex
)
393 QString id
= bookmarkId(m_sourceModel
->bookmarkForIndex(sourceIndex
));
395 for (int i
= 0, iMax
= count(); i
< iMax
; ++i
) {
396 if (bookmarkId(placesItem(i
)->bookmark()) == id
) {
403 QString
PlacesItemModel::bookmarkId(const KBookmark
&bookmark
) const
405 QString id
= bookmark
.metaDataItem(QStringLiteral("UDI"));
407 id
= bookmark
.metaDataItem(QStringLiteral("ID"));
412 void PlacesItemModel::initializeDefaultViewProperties() const
414 for(int i
= 0, iMax
= m_sourceModel
->rowCount(); i
< iMax
; i
++) {
415 const QModelIndex index
= m_sourceModel
->index(i
, 0);
416 const PlacesItem
*item
= placesItem(mapFromSource(index
));
421 // Create default view-properties for all "Search For" and "Recently Saved" bookmarks
422 // in case the user has not already created custom view-properties for a corresponding
424 const bool createDefaultViewProperties
= item
->isSearchOrTimelineUrl() && !GeneralSettings::self()->globalViewProps();
425 if (createDefaultViewProperties
) {
426 const QUrl itemUrl
= item
->url();
427 ViewProperties
props(KFilePlacesModel::convertedUrl(itemUrl
));
428 if (!props
.exist()) {
429 const QString path
= itemUrl
.path();
430 if (path
== QLatin1String("/documents")) {
431 props
.setViewMode(DolphinView::DetailsView
);
432 props
.setPreviewsShown(false);
433 props
.setVisibleRoles({"text", "path"});
434 } else if (path
== QLatin1String("/images")) {
435 props
.setViewMode(DolphinView::IconsView
);
436 props
.setPreviewsShown(true);
437 props
.setVisibleRoles({"text", "height", "width"});
438 } else if (path
== QLatin1String("/audio")) {
439 props
.setViewMode(DolphinView::DetailsView
);
440 props
.setPreviewsShown(false);
441 props
.setVisibleRoles({"text", "artist", "album"});
442 } else if (path
== QLatin1String("/videos")) {
443 props
.setViewMode(DolphinView::IconsView
);
444 props
.setPreviewsShown(true);
445 props
.setVisibleRoles({"text"});
446 } else if (itemUrl
.scheme() == QLatin1String("timeline")) {
447 props
.setViewMode(DolphinView::DetailsView
);
448 props
.setVisibleRoles({"text", "modificationtime"});
456 void PlacesItemModel::updateItem(PlacesItem
*item
, const QModelIndex
&index
)
458 item
->setGroup(index
.data(KFilePlacesModel::GroupRole
).toString());
459 item
->setIcon(index
.data(KFilePlacesModel::IconNameRole
).toString());
460 item
->setGroupHidden(index
.data(KFilePlacesModel::GroupHiddenRole
).toBool());
463 void PlacesItemModel::slotStorageTearDownDone(Solid::ErrorType error
, const QVariant
& errorData
)
465 if (error
&& errorData
.isValid()) {
466 if (error
== Solid::ErrorType::DeviceBusy
) {
467 KListOpenFilesJob
* listOpenFilesJob
= new KListOpenFilesJob(m_deviceToTearDown
->filePath());
468 connect(listOpenFilesJob
, &KIO::Job::result
, this, [this, listOpenFilesJob
](KJob
*) {
469 const KProcessList::KProcessInfoList blockingProcesses
= listOpenFilesJob
->processInfoList();
471 if (blockingProcesses
.isEmpty()) {
472 errorString
= i18n("One or more files on this device are open within an application.");
474 QStringList blockingApps
;
475 for (const auto& process
: blockingProcesses
) {
476 blockingApps
<< process
.name();
478 blockingApps
.removeDuplicates();
479 errorString
= xi18np("One or more files on this device are opened in application <application>\"%2\"</application>.",
480 "One or more files on this device are opened in following applications: <application>%2</application>.",
481 blockingApps
.count(), blockingApps
.join(i18nc("separator in list of apps blocking device unmount", ", ")));
483 emit
errorMessage(errorString
);
485 listOpenFilesJob
->start();
487 emit
errorMessage(errorData
.toString());
490 disconnect(m_deviceToTearDown
, &Solid::StorageAccess::teardownDone
,
491 this, &PlacesItemModel::slotStorageTearDownDone
);
492 m_deviceToTearDown
= nullptr;
495 void PlacesItemModel::slotStorageSetupDone(Solid::ErrorType error
,
496 const QVariant
& errorData
,
501 const int index
= m_storageSetupInProgress
.take(sender());
502 const PlacesItem
* item
= placesItem(index
);
507 if (error
!= Solid::NoError
) {
508 if (errorData
.isValid()) {
509 emit
errorMessage(i18nc("@info", "An error occurred while accessing '%1', the system responded: %2",
511 errorData
.toString()));
513 emit
errorMessage(i18nc("@info", "An error occurred while accessing '%1'",
516 emit
storageSetupDone(index
, false);
518 emit
storageSetupDone(index
, true);
522 void PlacesItemModel::onSourceModelRowsInserted(const QModelIndex
&parent
, int first
, int last
)
524 for (int i
= first
; i
<= last
; i
++) {
525 const QModelIndex index
= m_sourceModel
->index(i
, 0, parent
);
526 addItemFromSourceModel(index
);
530 void PlacesItemModel::onSourceModelRowsAboutToBeRemoved(const QModelIndex
&parent
, int first
, int last
)
532 for(int r
= first
; r
<= last
; r
++) {
533 const QModelIndex index
= m_sourceModel
->index(r
, 0, parent
);
534 int oldIndex
= mapFromSource(index
);
535 if (oldIndex
!= -1) {
536 removeItem(oldIndex
);
541 void PlacesItemModel::onSourceModelRowsAboutToBeMoved(const QModelIndex
&parent
, int start
, int end
, const QModelIndex
&destination
, int row
)
543 Q_UNUSED(destination
)
546 for(int r
= start
; r
<= end
; r
++) {
547 const QModelIndex sourceIndex
= m_sourceModel
->index(r
, 0, parent
);
549 removeItem(mapFromSource(sourceIndex
));
553 void PlacesItemModel::onSourceModelRowsMoved(const QModelIndex
&parent
, int start
, int end
, const QModelIndex
&destination
, int row
)
555 Q_UNUSED(destination
)
558 const int blockSize
= (end
- start
) + 1;
560 for (int r
= start
; r
<= end
; r
++) {
561 // insert the moved item in the new position
562 const int targetRow
= row
+ (start
- r
) - (r
< row
? blockSize
: 0);
563 const QModelIndex targetIndex
= m_sourceModel
->index(targetRow
, 0, destination
);
565 addItemFromSourceModel(targetIndex
);
569 void PlacesItemModel::onSourceModelDataChanged(const QModelIndex
&topLeft
, const QModelIndex
&bottomRight
, const QVector
<int> &roles
)
573 for (int r
= topLeft
.row(); r
<= bottomRight
.row(); r
++) {
574 const QModelIndex sourceIndex
= m_sourceModel
->index(r
, 0);
575 const KBookmark bookmark
= m_sourceModel
->bookmarkForIndex(sourceIndex
);
576 PlacesItem
*placeItem
= itemFromBookmark(bookmark
);
578 if (placeItem
&& (!m_hiddenItemsShown
&& m_sourceModel
->isHidden(sourceIndex
))) {
579 //hide item if it became invisible
580 removeItem(index(placeItem
));
584 if (!placeItem
&& (m_hiddenItemsShown
|| !m_sourceModel
->isHidden(sourceIndex
))) {
585 //show item if it became visible
586 addItemFromSourceModel(sourceIndex
);
590 if (placeItem
&& !m_sourceModel
->isDevice(sourceIndex
)) {
591 placeItem
->setText(bookmark
.text());
592 placeItem
->setIcon(sourceIndex
.data(KFilePlacesModel::IconNameRole
).toString());
593 placeItem
->setUrl(m_sourceModel
->url(sourceIndex
));
594 placeItem
->bookmark().setMetaDataItem(QStringLiteral("OnlyInApp"),
595 bookmark
.metaDataItem(QStringLiteral("OnlyInApp")));
596 // must update the bookmark object
597 placeItem
->setBookmark(bookmark
);
602 void PlacesItemModel::onSourceModelGroupHiddenChanged(KFilePlacesModel::GroupType group
, bool hidden
)
604 const auto groupIndexes
= m_sourceModel
->groupIndexes(group
);
605 for (const QModelIndex
&sourceIndex
: groupIndexes
) {
606 PlacesItem
*item
= placesItem(mapFromSource(sourceIndex
));
608 item
->setGroupHidden(hidden
);
613 void PlacesItemModel::cleanupBookmarks()
615 // KIO model now provides support for baloo urls, and because of that we
616 // need to remove old URLs that were visible only in Dolphin to avoid duplication
618 static const QVector
<QUrl
> balooURLs
= {
619 QUrl(QStringLiteral("timeline:/today")),
620 QUrl(QStringLiteral("timeline:/yesterday")),
621 QUrl(QStringLiteral("timeline:/thismonth")),
622 QUrl(QStringLiteral("timeline:/lastmonth")),
623 QUrl(QStringLiteral("search:/documents")),
624 QUrl(QStringLiteral("search:/images")),
625 QUrl(QStringLiteral("search:/audio")),
626 QUrl(QStringLiteral("search:/videos"))
631 const QModelIndex sourceIndex
= m_sourceModel
->index(row
, 0);
632 const KBookmark bookmark
= m_sourceModel
->bookmarkForIndex(sourceIndex
);
633 const QUrl url
= bookmark
.url();
634 const QString appName
= bookmark
.metaDataItem(QStringLiteral("OnlyInApp"));
636 if ((appName
== KAboutData::applicationData().componentName() ||
637 appName
== KAboutData::applicationData().componentName() + DolphinPlacesModelSingleton::applicationNameSuffix()) && balooURLs
.contains(url
)) {
638 qCDebug(DolphinDebug
) << "Removing old baloo url:" << url
;
639 m_sourceModel
->removePlace(sourceIndex
);
643 } while (row
< m_sourceModel
->rowCount());
646 void PlacesItemModel::loadBookmarks()
648 for(int r
= 0, rMax
= m_sourceModel
->rowCount(); r
< rMax
; r
++) {
649 const QModelIndex sourceIndex
= m_sourceModel
->index(r
, 0);
650 if (m_hiddenItemsShown
|| !m_sourceModel
->isHidden(sourceIndex
)) {
651 addItemFromSourceModel(sourceIndex
);
656 void PlacesItemModel::clear() {
657 KStandardItemModel::clear();
660 void PlacesItemModel::proceedWithTearDown()
662 Q_ASSERT(m_deviceToTearDown
);
664 connect(m_deviceToTearDown
, &Solid::StorageAccess::teardownDone
,
665 this, &PlacesItemModel::slotStorageTearDownDone
);
666 m_deviceToTearDown
->teardown();
669 void PlacesItemModel::deleteItem(int index
)
671 QModelIndex sourceIndex
= mapToSource(index
);
672 Q_ASSERT(sourceIndex
.isValid());
673 m_sourceModel
->removePlace(sourceIndex
);
676 void PlacesItemModel::refresh()
678 m_sourceModel
->refresh();
681 void PlacesItemModel::hideItem(int index
)
683 PlacesItem
* shownItem
= placesItem(index
);
688 shownItem
->setHidden(true);
691 QString
PlacesItemModel::internalMimeType() const
693 return "application/x-dolphinplacesmodel-" +
694 QString::number((qptrdiff
)this);
697 int PlacesItemModel::groupedDropIndex(int index
, const PlacesItem
* item
) const
701 int dropIndex
= index
;
702 const QString group
= item
->group();
704 const int itemCount
= count();
706 dropIndex
= itemCount
;
709 // Search nearest previous item with the same group
710 int previousIndex
= -1;
711 for (int i
= dropIndex
- 1; i
>= 0; --i
) {
712 if (placesItem(i
)->group() == group
) {
718 // Search nearest next item with the same group
720 for (int i
= dropIndex
; i
< count(); ++i
) {
721 if (placesItem(i
)->group() == group
) {
727 // Adjust the drop-index to be inserted to the
728 // nearest item with the same group.
729 if (previousIndex
>= 0 && nextIndex
>= 0) {
730 dropIndex
= (dropIndex
- previousIndex
< nextIndex
- dropIndex
) ?
731 previousIndex
+ 1 : nextIndex
;
732 } else if (previousIndex
>= 0) {
733 dropIndex
= previousIndex
+ 1;
734 } else if (nextIndex
>= 0) {
735 dropIndex
= nextIndex
;
741 bool PlacesItemModel::equalBookmarkIdentifiers(const KBookmark
& b1
, const KBookmark
& b2
)
743 const QString udi1
= b1
.metaDataItem(QStringLiteral("UDI"));
744 const QString udi2
= b2
.metaDataItem(QStringLiteral("UDI"));
745 if (!udi1
.isEmpty() && !udi2
.isEmpty()) {
748 return b1
.metaDataItem(QStringLiteral("ID")) == b2
.metaDataItem(QStringLiteral("ID"));
752 int PlacesItemModel::mapFromSource(const QModelIndex
&index
) const
754 if (!index
.isValid()) {
758 return m_indexMap
.indexOf(index
);
761 bool PlacesItemModel::isDir(int index
) const
767 KFilePlacesModel::GroupType
PlacesItemModel::groupType(int row
) const
769 return m_sourceModel
->groupType(mapToSource(row
));
772 bool PlacesItemModel::isGroupHidden(KFilePlacesModel::GroupType type
) const
774 return m_sourceModel
->isGroupHidden(type
);
777 void PlacesItemModel::setGroupHidden(KFilePlacesModel::GroupType type
, bool hidden
)
779 return m_sourceModel
->setGroupHidden(type
, hidden
);
782 QModelIndex
PlacesItemModel::mapToSource(int row
) const
784 return m_indexMap
.value(row
);
787 PlacesItem
*PlacesItemModel::itemFromBookmark(const KBookmark
&bookmark
) const
789 const QString id
= bookmarkId(bookmark
);
790 for (int i
= 0, iMax
= count(); i
< iMax
; i
++) {
791 PlacesItem
*item
= placesItem(i
);
792 const KBookmark itemBookmark
= item
->bookmark();
793 if (bookmarkId(itemBookmark
) == id
) {