1 /****************************************************************************
2 **
3 ** Copyright (C) 2015-2016 Oleg Shparber
4 ** Copyright (C) 2013-2014 Jerzy Kozera
5 ** Contact: https://go.zealdocs.org/l/contact
6 **
7 ** This file is part of Zeal.
8 **
9 ** Zeal is free software: you can redistribute it and/or modify
10 ** it under the terms of the GNU General Public License as published by
11 ** the Free Software Foundation, either version 3 of the License, or
12 ** (at your option) any later version.
13 **
14 ** Zeal is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ** GNU General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with Zeal. If not, see <https://www.gnu.org/licenses/>.
21 **
22 ****************************************************************************/
23 
24 #include "searchquery.h"
25 
26 using namespace Zeal::Registry;
27 
28 namespace {
29 const char prefixSeparator = ':';
30 const char keywordSeparator = ',';
31 }
32 
SearchQuery()33 SearchQuery::SearchQuery()
34 {
35 }
36 
SearchQuery(const QString & query,const QStringList & keywords)37 SearchQuery::SearchQuery(const QString &query, const QStringList &keywords) :
38     m_query(query)
39 {
40     setKeywords(keywords);
41 }
42 
fromString(const QString & str)43 SearchQuery SearchQuery::fromString(const QString &str)
44 {
45     const int sepAt = str.indexOf(prefixSeparator);
46     const int next = sepAt + 1;
47 
48     QString query;
49     QStringList keywords;
50     if (sepAt > 0 && (next >= str.size() || str.at(next) != prefixSeparator)) {
51         query = str.mid(next).trimmed();
52 
53         const QString keywordStr = str.left(sepAt).trimmed();
54         keywords = keywordStr.split(keywordSeparator);
55     } else {
56         query = str.trimmed();
57     }
58 
59     return SearchQuery(query, keywords);
60 }
61 
toString() const62 QString SearchQuery::toString() const
63 {
64     if (m_keywords.isEmpty())
65         return m_query;
66     else
67         return m_keywordPrefix + m_query;
68 }
69 
isEmpty() const70 bool SearchQuery::isEmpty() const
71 {
72     return m_query.isEmpty() && m_keywords.isEmpty();
73 }
74 
keywords() const75 QStringList SearchQuery::keywords() const
76 {
77     return m_keywords;
78 }
79 
setKeywords(const QStringList & list)80 void SearchQuery::setKeywords(const QStringList &list)
81 {
82     if (list.isEmpty())
83         return;
84 
85     m_keywords = list;
86     m_keywordPrefix = list.join(keywordSeparator) + prefixSeparator;
87 }
88 
hasKeywords() const89 bool SearchQuery::hasKeywords() const
90 {
91     return !m_keywords.isEmpty();
92 }
93 
hasKeywords(const QStringList & keywords) const94 bool SearchQuery::hasKeywords(const QStringList &keywords) const
95 {
96     for (const QString &keyword : keywords) {
97         if (m_keywords.contains(keyword, Qt::CaseInsensitive)) {
98             return true;
99         }
100     }
101 
102     return false;
103 }
104 
keywordPrefixSize() const105 int SearchQuery::keywordPrefixSize() const
106 {
107     return m_keywordPrefix.size();
108 }
109 
query() const110 QString SearchQuery::query() const
111 {
112     return m_query;
113 }
114 
setQuery(const QString & str)115 void SearchQuery::setQuery(const QString &str)
116 {
117     m_query = str;
118 }
119 
operator <<(QDataStream & out,const Zeal::Registry::SearchQuery & query)120 QDataStream &operator<<(QDataStream &out, const Zeal::Registry::SearchQuery &query)
121 {
122     out << query.toString();
123     return out;
124 }
125 
operator >>(QDataStream & in,Zeal::Registry::SearchQuery & query)126 QDataStream &operator>>(QDataStream &in, Zeal::Registry::SearchQuery &query)
127 {
128     QString str;
129     in >> str;
130     query = SearchQuery::fromString(str);
131     return in;
132 }
133