File indexing completed on 2024-04-28 17:03:10
0001 /* 0002 * SPDX-FileCopyrightText: 2019 Ismael Asensio <isma.af@mgmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "dolphinquery.h" 0008 0009 #include <QRegularExpression> 0010 0011 #include "config-dolphin.h" 0012 #if HAVE_BALOO 0013 #include <Baloo/Query> 0014 #endif 0015 0016 namespace 0017 { 0018 #if HAVE_BALOO 0019 /** Checks if a given term in the Baloo::Query::searchString() is a special search term 0020 * @return: the specific search token of the term, or an empty QString() if none is found 0021 */ 0022 QString searchTermToken(const QString &term) 0023 { 0024 static const QLatin1String searchTokens[]{QLatin1String("filename:"), 0025 QLatin1String("modified>="), 0026 QLatin1String("rating>="), 0027 QLatin1String("tag:"), 0028 QLatin1String("tag=")}; 0029 0030 for (const auto &searchToken : searchTokens) { 0031 if (term.startsWith(searchToken)) { 0032 return searchToken; 0033 } 0034 } 0035 return QString(); 0036 } 0037 0038 QString stripQuotes(const QString &text) 0039 { 0040 if (text.length() >= 2 && text.at(0) == QLatin1Char('"') && text.back() == QLatin1Char('"')) { 0041 return text.mid(1, text.size() - 2); 0042 } 0043 return text; 0044 } 0045 0046 QStringList splitOutsideQuotes(const QString &text) 0047 { 0048 // Match groups on 3 possible conditions: 0049 // - Groups with two leading quotes must close both on them (filename:""abc xyz" tuv") 0050 // - Groups enclosed in quotes 0051 // - Words separated by spaces 0052 static const QRegularExpression subTermsRegExp("(\\S*?\"\"[^\"]+\"[^\"]+\"+|\\S*?\"[^\"]+\"+|(?<=\\s|^)\\S+(?=\\s|$))"); 0053 auto subTermsMatchIterator = subTermsRegExp.globalMatch(text); 0054 0055 QStringList textParts; 0056 while (subTermsMatchIterator.hasNext()) { 0057 textParts << subTermsMatchIterator.next().captured(0); 0058 } 0059 return textParts; 0060 } 0061 #endif 0062 0063 QString trimChar(const QString &text, const QLatin1Char aChar) 0064 { 0065 const int start = text.startsWith(aChar) ? 1 : 0; 0066 const int end = (text.length() > 1 && text.endsWith(aChar)) ? 1 : 0; 0067 0068 return text.mid(start, text.length() - start - end); 0069 } 0070 } 0071 0072 DolphinQuery DolphinQuery::fromSearchUrl(const QUrl &searchUrl) 0073 { 0074 DolphinQuery model; 0075 model.m_searchUrl = searchUrl; 0076 0077 if (searchUrl.scheme() == QLatin1String("baloosearch")) { 0078 model.parseBalooQuery(); 0079 } else if (searchUrl.scheme() == QLatin1String("tags")) { 0080 // tags can contain # symbols or slashes within the Url 0081 QString tag = trimChar(searchUrl.toString(QUrl::RemoveScheme), QLatin1Char('/')); 0082 model.m_searchTerms << QStringLiteral("tag:%1").arg(tag); 0083 } 0084 0085 return model; 0086 } 0087 0088 bool DolphinQuery::supportsScheme(const QString &urlScheme) 0089 { 0090 static const QStringList supportedSchemes = { 0091 QStringLiteral("baloosearch"), 0092 QStringLiteral("tags"), 0093 }; 0094 0095 return supportedSchemes.contains(urlScheme); 0096 } 0097 0098 void DolphinQuery::parseBalooQuery() 0099 { 0100 #if HAVE_BALOO 0101 const Baloo::Query query = Baloo::Query::fromSearchUrl(m_searchUrl); 0102 0103 m_includeFolder = query.includeFolder(); 0104 0105 const QStringList types = query.types(); 0106 m_fileType = types.isEmpty() ? QString() : types.first(); 0107 0108 QStringList textParts; 0109 QString fileName; 0110 0111 const QStringList subTerms = splitOutsideQuotes(query.searchString()); 0112 for (const QString &subTerm : subTerms) { 0113 const QString token = searchTermToken(subTerm); 0114 const QString value = stripQuotes(subTerm.mid(token.length())); 0115 0116 if (token == QLatin1String("filename:")) { 0117 if (!value.isEmpty()) { 0118 fileName = value; 0119 m_hasFileName = true; 0120 } 0121 continue; 0122 } else if (!token.isEmpty()) { 0123 m_searchTerms << token + value; 0124 continue; 0125 } else if (subTerm == QLatin1String("AND") && subTerm != subTerms.at(0) && subTerm != subTerms.back()) { 0126 continue; 0127 } else if (!value.isEmpty()) { 0128 textParts << value; 0129 m_hasContentSearch = true; 0130 } 0131 } 0132 0133 if (m_hasFileName) { 0134 if (m_hasContentSearch) { 0135 textParts << QStringLiteral("filename:\"%1\"").arg(fileName); 0136 } else { 0137 textParts << fileName; 0138 } 0139 } 0140 0141 m_searchText = textParts.join(QLatin1Char(' ')); 0142 #endif 0143 } 0144 0145 QUrl DolphinQuery::searchUrl() const 0146 { 0147 return m_searchUrl; 0148 } 0149 0150 QString DolphinQuery::text() const 0151 { 0152 return m_searchText; 0153 } 0154 0155 QString DolphinQuery::type() const 0156 { 0157 return m_fileType; 0158 } 0159 0160 QStringList DolphinQuery::searchTerms() const 0161 { 0162 return m_searchTerms; 0163 } 0164 0165 QString DolphinQuery::includeFolder() const 0166 { 0167 return m_includeFolder; 0168 } 0169 0170 bool DolphinQuery::hasContentSearch() const 0171 { 0172 return m_hasContentSearch; 0173 } 0174 0175 bool DolphinQuery::hasFileName() const 0176 { 0177 return m_hasFileName; 0178 }