1 /*
2 SPDX-FileCopyrightText: 2007 Glenn Ergeerts <glenn.ergeerts@telenet.be>
3 SPDX-FileCopyrightText: 2012 Marco Gulino <marco.gulino@xpeppers.com>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8 #include "bookmarkmatch.h"
9 #include <QVariant>
10
11 // TODO: test
12
BookmarkMatch(const QIcon & icon,const QString & searchTerm,const QString & bookmarkTitle,const QString & bookmarkURL,const QString & description)13 BookmarkMatch::BookmarkMatch(const QIcon &icon, const QString &searchTerm, const QString &bookmarkTitle, const QString &bookmarkURL, const QString &description)
14 : m_icon(icon)
15 , m_searchTerm(searchTerm)
16 , m_bookmarkTitle(bookmarkTitle)
17 , m_bookmarkURL(bookmarkURL)
18 , m_description(description)
19 {
20 }
21
asQueryMatch(Plasma::AbstractRunner * runner)22 Plasma::QueryMatch BookmarkMatch::asQueryMatch(Plasma::AbstractRunner *runner)
23 {
24 Plasma::QueryMatch::Type type;
25 qreal relevance = 0;
26
27 if (m_bookmarkTitle.compare(m_searchTerm, Qt::CaseInsensitive) == 0
28 || (!m_description.isEmpty() && m_description.compare(m_searchTerm, Qt::CaseInsensitive) == 0)) {
29 type = Plasma::QueryMatch::ExactMatch;
30 relevance = 1.0;
31 } else if (m_bookmarkTitle.contains(m_searchTerm, Qt::CaseInsensitive)) {
32 type = Plasma::QueryMatch::PossibleMatch;
33 relevance = 0.45;
34 } else if (!m_description.isEmpty() && m_description.contains(m_searchTerm, Qt::CaseInsensitive)) {
35 type = Plasma::QueryMatch::PossibleMatch;
36 relevance = 0.3;
37 } else if (m_bookmarkURL.contains(m_searchTerm, Qt::CaseInsensitive)) {
38 type = Plasma::QueryMatch::PossibleMatch;
39 relevance = 0.2;
40 } else {
41 type = Plasma::QueryMatch::PossibleMatch;
42 relevance = 0.18;
43 }
44
45 bool isNameEmpty = m_bookmarkTitle.isEmpty();
46 bool isDescriptionEmpty = m_description.isEmpty();
47
48 Plasma::QueryMatch match(runner);
49 match.setType(type);
50 match.setRelevance(relevance);
51 match.setIcon(m_icon);
52 match.setSubtext(m_bookmarkURL);
53
54 // Try to set the following as text in this order: name, description, url
55 match.setText(isNameEmpty ? (!isDescriptionEmpty ? m_description : m_bookmarkURL) : m_bookmarkTitle);
56
57 match.setData(m_bookmarkURL);
58 match.setUrls({QUrl(m_bookmarkURL)});
59 return match;
60 }
61
addTo(QList<BookmarkMatch> & listOfResults,bool addEvenOnNoMatch)62 void BookmarkMatch::addTo(QList<BookmarkMatch> &listOfResults, bool addEvenOnNoMatch)
63 {
64 if (!addEvenOnNoMatch && !(matches(m_searchTerm, m_bookmarkTitle) || matches(m_searchTerm, m_description) || matches(m_searchTerm, m_bookmarkURL))) {
65 return;
66 }
67 listOfResults << *this;
68 }
69
matches(const QString & search,const QString & matchingField)70 bool BookmarkMatch::matches(const QString &search, const QString &matchingField)
71 {
72 return !matchingField.simplified().isEmpty() && matchingField.contains(search, Qt::CaseInsensitive);
73 }
74