File indexing completed on 2024-05-19 09:31:36

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 KRunner::QueryMatch BookmarkMatch::asQueryMatch(KRunner::AbstractRunner *runner)
0023 {
0024     KRunner::QueryMatch::CategoryRelevance categoryRelevance = KRunner::QueryMatch::CategoryRelevance::Low;
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         categoryRelevance = KRunner::QueryMatch::CategoryRelevance::Highest;
0030         relevance = 1.0;
0031     } else if (m_bookmarkTitle.contains(m_searchTerm, Qt::CaseInsensitive)) {
0032         relevance = 0.45;
0033     } else if (!m_description.isEmpty() && m_description.contains(m_searchTerm, Qt::CaseInsensitive)) {
0034         relevance = 0.3;
0035     } else if (m_bookmarkURL.contains(m_searchTerm, Qt::CaseInsensitive)) {
0036         relevance = 0.2;
0037     } else {
0038         relevance = 0.18;
0039     }
0040 
0041     bool isNameEmpty = m_bookmarkTitle.isEmpty();
0042     bool isDescriptionEmpty = m_description.isEmpty();
0043 
0044     KRunner::QueryMatch match(runner);
0045     match.setCategoryRelevance(categoryRelevance);
0046     match.setRelevance(relevance);
0047     match.setIcon(m_icon);
0048     match.setSubtext(m_bookmarkURL);
0049 
0050     // Try to set the following as text in this order: name, description, url
0051     match.setText(isNameEmpty ? (!isDescriptionEmpty ? m_description : m_bookmarkURL) : m_bookmarkTitle);
0052 
0053     match.setData(m_bookmarkURL);
0054     match.setUrls({QUrl(m_bookmarkURL)});
0055     return match;
0056 }
0057 
0058 void BookmarkMatch::addTo(QList<BookmarkMatch> &listOfResults, bool addEvenOnNoMatch)
0059 {
0060     if (!addEvenOnNoMatch && !(matches(m_searchTerm, m_bookmarkTitle) || matches(m_searchTerm, m_description) || matches(m_searchTerm, m_bookmarkURL))) {
0061         return;
0062     }
0063     listOfResults << *this;
0064 }
0065 
0066 bool BookmarkMatch::matches(const QString &search, const QString &matchingField)
0067 {
0068     return !matchingField.simplified().isEmpty() && matchingField.contains(search, Qt::CaseInsensitive);
0069 }