diff --git a/src/panels/places/placesitemmodel.h b/src/panels/places/placesitemmodel.h --- a/src/panels/places/placesitemmodel.h +++ b/src/panels/places/placesitemmodel.h @@ -168,17 +168,16 @@ private: /** - * Loads the bookmarks from the bookmark-manager and creates items for - * the model or moves hidden items to m_bookmarkedItems. + * Remove bookmarks created by the previous version of dolphin that are + * not valid anymore */ - void loadBookmarks(); + void cleanupBookmarks(); /** - * @return True, if the bookmark can be accepted in the context of the - * current application (e.g. bookmarks from other applications - * will be ignored). + * Loads the bookmarks from the bookmark-manager and creates items for + * the model or moves hidden items to m_bookmarkedItems. */ - bool acceptBookmark(const KBookmark& bookmark) const; + void loadBookmarks(); QString internalMimeType() const; diff --git a/src/panels/places/placesitemmodel.cpp b/src/panels/places/placesitemmodel.cpp --- a/src/panels/places/placesitemmodel.cpp +++ b/src/panels/places/placesitemmodel.cpp @@ -53,18 +53,29 @@ #include namespace { - // Hence a prefix to the application-name of the stored bookmarks is + // A suffix to the application-name of the stored bookmarks is // added, which is only read by PlacesItemModel. - const char AppNamePrefix[] = "-places-panel"; + const QString AppNameSuffix = QStringLiteral("-places-panel"); + static QList balooURLs = { + QUrl(QStringLiteral("timeline:/today")), + QUrl(QStringLiteral("timeline:/yesterday")), + QUrl(QStringLiteral("timeline:/thismonth")), + QUrl(QStringLiteral("timeline:/lastmonth")), + QUrl(QStringLiteral("search:/documents")), + QUrl(QStringLiteral("search:/images")), + QUrl(QStringLiteral("search:/audio")), + QUrl(QStringLiteral("search:/videos")) + }; } PlacesItemModel::PlacesItemModel(QObject* parent) : KStandardItemModel(parent), m_hiddenItemsShown(false), m_deviceToTearDown(nullptr), m_storageSetupInProgress(), - m_sourceModel(new KFilePlacesModel(this)) + m_sourceModel(new KFilePlacesModel(KAboutData::applicationData().componentName() + AppNameSuffix, this)) { + cleanupBookmarks(); loadBookmarks(); initializeDefaultViewProperties(); @@ -153,12 +164,13 @@ for(int r = 0, rMax = m_sourceModel->rowCount(); r < rMax; r++) { sourceIndex = m_sourceModel->index(r, 0); + const KBookmark sourceBookmark = m_sourceModel->bookmarkForIndex(sourceIndex); - if (bookmarkId(m_sourceModel->bookmarkForIndex(sourceIndex)) == iBookmarkId) { + if (bookmarkId(sourceBookmark) == iBookmarkId) { break; } - if (!m_sourceModel->isHidden(sourceIndex)) { + if (m_hiddenItemsShown || !m_sourceModel->isHidden(sourceIndex)) { pos++; } } @@ -559,11 +571,7 @@ const int targetRow = row + (start - r) - (r < row ? blockSize : 0); const QModelIndex targetIndex = m_sourceModel->index(targetRow, 0, destination); - const KBookmark bookmark = m_sourceModel->bookmarkForIndex(targetIndex); - PlacesItem *item = new PlacesItem(bookmark); - updateItem(item, targetIndex); - - insertSortedItem(item); + addItemFromSourceModel(targetIndex); } } @@ -608,13 +616,33 @@ } } +void PlacesItemModel::cleanupBookmarks() +{ + // KIO model now provides support for baloo urls, and because of that we + // need to remove old URLs that were visible only in Dolphin to avoid duplication + int row = 0; + do { + const QModelIndex sourceIndex = m_sourceModel->index(row, 0); + const KBookmark bookmark = m_sourceModel->bookmarkForIndex(sourceIndex); + const QUrl url = bookmark.url(); + const QString appName = bookmark.metaDataItem(QStringLiteral("OnlyInApp")); + + if ((appName == KAboutData::applicationData().componentName() || + appName == KAboutData::applicationData().componentName() + AppNameSuffix) && balooURLs.contains(url)) { + qCDebug(DolphinDebug) << "Removing old baloo url:" << url; + m_sourceModel->removePlace(sourceIndex); + } else { + row++; + } + } while (row < m_sourceModel->rowCount()); +} + void PlacesItemModel::loadBookmarks() { for(int r = 0, rMax = m_sourceModel->rowCount(); r < rMax; r++) { const QModelIndex sourceIndex = m_sourceModel->index(r, 0); KBookmark bookmark = m_sourceModel->bookmarkForIndex(sourceIndex); - if (acceptBookmark(bookmark) && - (m_hiddenItemsShown || !m_sourceModel->isHidden(sourceIndex))) { + if (m_hiddenItemsShown || !m_sourceModel->isHidden(sourceIndex)) { addItemFromSourceModel(sourceIndex); } } @@ -625,19 +653,6 @@ #endif } -bool PlacesItemModel::acceptBookmark(const KBookmark& bookmark) const -{ - const QString udi = bookmark.metaDataItem(QStringLiteral("UDI")); - const QUrl url = bookmark.url(); - const QString appName = bookmark.metaDataItem(QStringLiteral("OnlyInApp")); - - const bool allowedHere = (appName.isEmpty() - || appName == KAboutData::applicationData().componentName() - || appName == KAboutData::applicationData().componentName() + AppNamePrefix); - - return (udi.isEmpty() && allowedHere); -} - void PlacesItemModel::clear() { KStandardItemModel::clear(); } diff --git a/src/tests/placesitemmodeltest.cpp b/src/tests/placesitemmodeltest.cpp --- a/src/tests/placesitemmodeltest.cpp +++ b/src/tests/placesitemmodeltest.cpp @@ -83,6 +83,7 @@ void testIcons(); void testDragAndDrop(); void testHideDevices(); + void testDuplicatedEntries(); private: PlacesItemModel* m_model; @@ -780,6 +781,31 @@ m_model = new PlacesItemModel(); QTRY_COMPARE(m_model->count(), urls.count()); CHECK_PLACES_URLS(urls); + + // revert changes + m_model->setGroupHidden(KFilePlacesModel::RemovableDevicesType, false); + urls = initialUrls(); + QTRY_COMPARE(m_model->count(), urls.count()); + CHECK_PLACES_URLS(urls); +} + +void PlacesItemModelTest::testDuplicatedEntries() +{ + QStringList urls = initialUrls(); + // create a duplicated entry on bookmark + KBookmarkManager *bookmarkManager = KBookmarkManager::managerForFile(bookmarksFile(), QStringLiteral("kfilePlaces")); + KBookmarkGroup root = bookmarkManager->root(); + KBookmark bookmark = root.addBookmark(QStringLiteral("Duplicated Search Videos"), QUrl("search:/videos"), {}); + + const QString id = QUuid::createUuid().toString(); + bookmark.setMetaDataItem(QStringLiteral("ID"), id); + bookmark.setMetaDataItem(QStringLiteral("OnlyInApp"), KAboutData::applicationData().componentName()); + bookmarkManager->emitChanged(bookmarkManager->root()); + + PlacesItemModel *newModel = new PlacesItemModel(); + QTRY_COMPARE(placesUrls(newModel).count(QStringLiteral("search:/videos")), 1); + QTRY_COMPARE(urls, placesUrls(newModel)); + delete newModel; } QTEST_MAIN(PlacesItemModelTest)