]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Add support to tags: scheme in DolphinQuery
authorIsmael Asensio <isma.af@gmail.com>
Sun, 14 Jun 2020 11:41:29 +0000 (13:41 +0200)
committerIsmael Asensio <isma.af@gmail.com>
Sat, 11 Jul 2020 18:33:58 +0000 (18:33 +0000)
It allows to start a search from a `tags:/mytag/` view.
The use case is to refine a search on additional terms (another tags,
ratings, etc)

src/search/dolphinquery.cpp
src/tests/dolphinquerytest.cpp

index 663ed9909ded157bf6df0a1a560024dae79ee576..4b549937808a3623d8c553a22b909338695665a1 100644 (file)
@@ -73,6 +73,14 @@ namespace {
         return textParts;
     }
 #endif
+
+    QString trimChar(const QString& text, const QLatin1Char aChar)
+    {
+        const int start = text.startsWith(aChar) ? 1 : 0;
+        const int end = (text.length() > 1 && text.endsWith(aChar)) ? 1 : 0;
+
+        return text.mid(start, text.length() - start - end);
+    }
 }
 
 
@@ -83,6 +91,10 @@ DolphinQuery DolphinQuery::fromSearchUrl(const QUrl& searchUrl)
 
     if (searchUrl.scheme() == QLatin1String("baloosearch")) {
         model.parseBalooQuery();
+    } else if (searchUrl.scheme() == QLatin1String("tags")) {
+        // tags can contain # symbols or slashes within the Url
+        QString tag = trimChar(searchUrl.toString(QUrl::RemoveScheme), QLatin1Char('/'));
+        model.m_searchTerms << QStringLiteral("tag:%1").arg(tag);
     }
 
     return model;
@@ -92,6 +104,7 @@ bool DolphinQuery::supportsScheme(const QString& urlScheme)
 {
     static const QStringList supportedSchemes = {
         QStringLiteral("baloosearch"),
+        QStringLiteral("tags"),
     };
 
     return supportedSchemes.contains(urlScheme);
index 25bd26b815b6b688cfc336827fd6cc78d17ef916..9e1c7673774cf7c39c84db427906a25d3577397b 100644 (file)
@@ -150,6 +150,15 @@ void DolphinSearchBoxTest::testBalooSearchParsing_data()
     QTest::newRow("allTerms/space")
         << balooQueryUrl(textS + " " + filenameS + " " + rating + " AND " + modified + " AND " + tagS)
         << textS + " " + filenameS << QStringList({modified, rating, tagR}) << true << true;
+
+    // Test tags:/ URL scheme
+    const auto tagUrl   = [](const QString &tag) { return QUrl(QStringLiteral("tags:/%1/").arg(tag)); };
+    const auto tagTerms = [](const QString &tag) { return QStringList{QStringLiteral("tag:%1").arg(tag)}; };
+
+    QTest::newRow("tagsUrl")       << tagUrl("tagA")             << "" << tagTerms("tagA")             << false << false;
+    QTest::newRow("tagsUrl/space") << tagUrl("tagB with spaces") << "" << tagTerms("tagB with spaces") << false << false;
+    QTest::newRow("tagsUrl/hash")  << tagUrl("tagC#hash")        << "" << tagTerms("tagC#hash")        << false << false;
+    QTest::newRow("tagsUrl/slash") << tagUrl("tagD/with/slash")  << "" << tagTerms("tagD/with/slash")  << false << false;
 }
 
 /**