]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Ignore trailing slashes when comparing place URLs
authorWolfgang Müller <wolf@oriole.systems>
Thu, 19 Sep 2024 09:46:09 +0000 (11:46 +0200)
committerWolfgang Müller <wolf@oriole.systems>
Thu, 19 Sep 2024 09:46:09 +0000 (11:46 +0200)
There's two locations where place URLs are compared in Dolphin. One
is in DolphinContextMenu::placeExists, which determines whether or not
to show an "Add to Places" context menu entry. The other one is in
DolphinViewContainer::caption, which provides the place name for use in
the window title, if applicable.

Neither of these functions correctly normalize the URL to account for
trailing slashes. Whilst placeExists() does not even attempt it,
caption() was changed in 681d8bb6c (Fix wrong window titles, 2019-09-15)
to support this using a regular expression.

However, caption() fails to escape the URL before incorporating it in
the regular expression, leading to failed matches and errors like the
following when browsing to directories that do not happen to make up a
valid regular expression:

    QString(View)::contains(): called on an invalid QRegularExpression
    object (pattern is '\A(?:file:///home/foo/[Z-A]/?)\z')

Instead of relying on complex and possibly brittle regular expressions,
use KFilePlacesModel's closestItem() function to find the closest
matching URL and then finally check whether the normalized URLs match
exactly.

src/dolphincontextmenu.cpp
src/dolphinviewcontainer.cpp

index 68f6dbd21240a103b30dcee39c1c2911916747f1..3ce1d1d51c82fb178d202f458d00c5ba82ee9f83 100644 (file)
@@ -381,9 +381,8 @@ bool DolphinContextMenu::placeExists(const QUrl &url) const
 {
     const KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
 
 {
     const KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
 
-    const auto &matchedPlaces = placesModel->match(placesModel->index(0, 0), KFilePlacesModel::UrlRole, url, 1, Qt::MatchExactly);
-
-    return !matchedPlaces.isEmpty();
+    QModelIndex url_index = placesModel->closestItem(url);
+    return url_index.isValid() && placesModel->url(url_index).matches(url, QUrl::StripTrailingSlash);
 }
 
 QAction *DolphinContextMenu::createPasteAction()
 }
 
 QAction *DolphinContextMenu::createPasteAction()
index ef76042b833fbec4a87ce8aa271149d5ae6b59b8..e55519d04300b270967fcf744b026907d5f76e21 100644 (file)
@@ -552,12 +552,11 @@ QString DolphinViewContainer::caption() const
     }
 
     KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
     }
 
     KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
-    const QString pattern = url().adjusted(QUrl::StripTrailingSlash).toString(QUrl::FullyEncoded).append("/?");
-    const auto &matchedPlaces =
-        placesModel->match(placesModel->index(0, 0), KFilePlacesModel::UrlRole, QRegularExpression::anchoredPattern(pattern), 1, Qt::MatchRegularExpression);
 
 
-    if (!matchedPlaces.isEmpty()) {
-        return placesModel->text(matchedPlaces.first());
+    QModelIndex url_index = placesModel->closestItem(url());
+
+    if (url_index.isValid() && placesModel->url(url_index).matches(url(), QUrl::StripTrailingSlash)) {
+        return placesModel->text(url_index);
     }
 
     if (!url().isLocalFile()) {
     }
 
     if (!url().isLocalFile()) {