diff --git a/autotests/kfileplacesmodeltest.cpp b/autotests/kfileplacesmodeltest.cpp --- a/autotests/kfileplacesmodeltest.cpp +++ b/autotests/kfileplacesmodeltest.cpp @@ -87,9 +87,10 @@ void testPlaceGroupHiddenGroupIndexesIntegrity(); void testPlaceGroupHiddenSignal(); void testPlaceGroupHiddenRole(); + void testFilterWithAlternativeApplicationName(); private: - QStringList placesUrls() const; + QStringList placesUrls(KFilePlacesModel *model = nullptr) const; QDBusInterface *fakeManager(); QDBusInterface *fakeDevice(const QString &udi); @@ -138,12 +139,16 @@ QFile::remove(bookmarksFile()); } -QStringList KFilePlacesModelTest::placesUrls() const +QStringList KFilePlacesModelTest::placesUrls(KFilePlacesModel *model) const { + KFilePlacesModel *currentModel = model; + if (!currentModel) { + currentModel = m_places; + } QStringList urls; - for (int row = 0; row < m_places->rowCount(); ++row) { - QModelIndex index = m_places->index(row, 0); - urls << m_places->url(index).toDisplayString(QUrl::PreferLocalFile); + for (int row = 0; row < currentModel->rowCount(); ++row) { + QModelIndex index = currentModel->index(row, 0); + urls << currentModel->url(index).toDisplayString(QUrl::PreferLocalFile); } return urls; } @@ -1259,6 +1264,30 @@ } } +void KFilePlacesModelTest::testFilterWithAlternativeApplicationName() +{ + const QStringList urls = initialListOfUrls(); + const QString alternativeApplicationName = QStringLiteral("kfile_places_model_test"); + + KBookmarkManager *bookmarkManager = KBookmarkManager::managerForFile(bookmarksFile(), QStringLiteral("kfilePlaces")); + KBookmarkGroup root = bookmarkManager->root(); + + // create a new entry with alternative application name + KBookmark bookmark = root.addBookmark(QStringLiteral("Extra entry"), QUrl(QStringLiteral("search:/videos-alternative")), {}); + const QString id = QUuid::createUuid().toString(); + bookmark.setMetaDataItem(QStringLiteral("ID"), id); + bookmark.setMetaDataItem(QStringLiteral("OnlyInApp"), alternativeApplicationName); + bookmarkManager->emitChanged(bookmarkManager->root()); + + // make sure that the entry is not visible on the original model + CHECK_PLACES_URLS(urls); + + // create a new model with alternativeApplicationName + KFilePlacesModel *newModel = new KFilePlacesModel(alternativeApplicationName); + QTRY_COMPARE(placesUrls(newModel).count(QStringLiteral("search:/videos-alternative")), 1); + delete newModel; +} + QTEST_MAIN(KFilePlacesModelTest) #include "kfileplacesmodeltest.moc" diff --git a/src/filewidgets/kfileplacesmodel.h b/src/filewidgets/kfileplacesmodel.h --- a/src/filewidgets/kfileplacesmodel.h +++ b/src/filewidgets/kfileplacesmodel.h @@ -66,6 +66,14 @@ }; KFilePlacesModel(QObject *parent = nullptr); + /** + * @brief Construct a new KFilePlacesModel with an alternativeApplicationName + * @param alternativeApplicationName This value will be used to filter bookmarks in addition to the actual application name + * @param parent Parent object + * @since 5.43 + * @todo kf6: merge contstructors + */ + KFilePlacesModel(const QString &alternativeApplicationName, QObject *parent = nullptr); ~KFilePlacesModel(); QUrl url(const QModelIndex &index) const; diff --git a/src/filewidgets/kfileplacesmodel.cpp b/src/filewidgets/kfileplacesmodel.cpp --- a/src/filewidgets/kfileplacesmodel.cpp +++ b/src/filewidgets/kfileplacesmodel.cpp @@ -193,6 +193,8 @@ const bool fileIndexingEnabled; + QString alternativeApplicationName; + void reloadAndSignal(); QList loadBookmarkList(); int findNearestPosition(int source, int target); @@ -209,11 +211,12 @@ bool isBalooUrl(const QUrl &url) const; }; -KFilePlacesModel::KFilePlacesModel(QObject *parent) +KFilePlacesModel::KFilePlacesModel(const QString &alternativeApplicationName, QObject *parent) : QAbstractItemModel(parent), d(new Private(this)) { const QString file = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/user-places.xbel"; d->bookmarkManager = KBookmarkManager::managerForExternalFile(file); + d->alternativeApplicationName = alternativeApplicationName; // Let's put some places in there if it's empty. KBookmarkGroup root = d->bookmarkManager->root(); @@ -328,6 +331,11 @@ QTimer::singleShot(0, this, SLOT(_k_initDeviceList())); } +KFilePlacesModel::KFilePlacesModel(QObject *parent) + : KFilePlacesModel({}, parent) +{ +} + KFilePlacesModel::~KFilePlacesModel() { delete d; @@ -660,7 +668,9 @@ devices.erase(it); } - bool allowedHere = appName.isEmpty() || (appName == QCoreApplication::instance()->applicationName()); + bool allowedHere = appName.isEmpty() || + ((appName == QCoreApplication::instance()->applicationName()) || + (appName == alternativeApplicationName)); bool isSupportedUrl = isBalooUrl(url) ? fileIndexingEnabled : true; if ((isSupportedUrl && udi.isEmpty() && allowedHere) || deviceAvailable) {