File indexing completed on 2024-05-05 17:45:00
0001 /* 0002 SPDX-FileCopyrightText: 2007 Glenn Ergeerts <glenn.ergeerts@telenet.be> 0003 SPDX-FileCopyrightText: 2012 Marco Gulino <marco.gulino@xpeppers.com> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "bookmarkmatch.h" 0009 #include <QVariant> 0010 0011 // TODO: test 0012 0013 BookmarkMatch::BookmarkMatch(const QIcon &icon, const QString &searchTerm, const QString &bookmarkTitle, const QString &bookmarkURL, const QString &description) 0014 : m_icon(icon) 0015 , m_searchTerm(searchTerm) 0016 , m_bookmarkTitle(bookmarkTitle) 0017 , m_bookmarkURL(bookmarkURL) 0018 , m_description(description) 0019 { 0020 } 0021 0022 Plasma::QueryMatch BookmarkMatch::asQueryMatch(Plasma::AbstractRunner *runner) 0023 { 0024 Plasma::QueryMatch::Type type; 0025 qreal relevance = 0; 0026 0027 if (m_bookmarkTitle.compare(m_searchTerm, Qt::CaseInsensitive) == 0 0028 || (!m_description.isEmpty() && m_description.compare(m_searchTerm, Qt::CaseInsensitive) == 0)) { 0029 type = Plasma::QueryMatch::ExactMatch; 0030 relevance = 1.0; 0031 } else if (m_bookmarkTitle.contains(m_searchTerm, Qt::CaseInsensitive)) { 0032 type = Plasma::QueryMatch::PossibleMatch; 0033 relevance = 0.45; 0034 } else if (!m_description.isEmpty() && m_description.contains(m_searchTerm, Qt::CaseInsensitive)) { 0035 type = Plasma::QueryMatch::PossibleMatch; 0036 relevance = 0.3; 0037 } else if (m_bookmarkURL.contains(m_searchTerm, Qt::CaseInsensitive)) { 0038 type = Plasma::QueryMatch::PossibleMatch; 0039 relevance = 0.2; 0040 } else { 0041 type = Plasma::QueryMatch::PossibleMatch; 0042 relevance = 0.18; 0043 } 0044 0045 bool isNameEmpty = m_bookmarkTitle.isEmpty(); 0046 bool isDescriptionEmpty = m_description.isEmpty(); 0047 0048 Plasma::QueryMatch match(runner); 0049 match.setType(type); 0050 match.setRelevance(relevance); 0051 match.setIcon(m_icon); 0052 match.setSubtext(m_bookmarkURL); 0053 0054 // Try to set the following as text in this order: name, description, url 0055 match.setText(isNameEmpty ? (!isDescriptionEmpty ? m_description : m_bookmarkURL) : m_bookmarkTitle); 0056 0057 match.setData(m_bookmarkURL); 0058 match.setUrls({QUrl(m_bookmarkURL)}); 0059 return match; 0060 } 0061 0062 void BookmarkMatch::addTo(QList<BookmarkMatch> &listOfResults, bool addEvenOnNoMatch) 0063 { 0064 if (!addEvenOnNoMatch && !(matches(m_searchTerm, m_bookmarkTitle) || matches(m_searchTerm, m_description) || matches(m_searchTerm, m_bookmarkURL))) { 0065 return; 0066 } 0067 listOfResults << *this; 0068 } 0069 0070 bool BookmarkMatch::matches(const QString &search, const QString &matchingField) 0071 { 0072 return !matchingField.simplified().isEmpty() && matchingField.contains(search, Qt::CaseInsensitive); 0073 }