1 | /* |
2 | This file is part of the KDE Baloo Project |
3 | SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.1-or-later |
6 | */ |
7 | |
8 | #ifndef BALOO_ENGINEQUERY_H |
9 | #define BALOO_ENGINEQUERY_H |
10 | |
11 | #include "engine_export.h" |
12 | |
13 | #include <QByteArray> |
14 | #include <QVector> |
15 | #include <QDebug> |
16 | |
17 | namespace Baloo { |
18 | |
19 | class BALOO_ENGINE_EXPORT EngineQuery |
20 | { |
21 | public: |
22 | enum Operation { |
23 | Equal, |
24 | StartsWith, |
25 | Phrase, |
26 | }; |
27 | |
28 | EngineQuery(); |
29 | EngineQuery(const QByteArray& term, Operation op = Equal); |
30 | EngineQuery(const QVector<EngineQuery> &subQueries); |
31 | |
32 | QByteArray term() const { |
33 | return m_term; |
34 | } |
35 | |
36 | Operation op() const { |
37 | return m_op; |
38 | } |
39 | |
40 | void setOp(const Operation& op) { |
41 | m_op = op; |
42 | } |
43 | |
44 | bool leaf() const { |
45 | return !m_term.isEmpty(); |
46 | } |
47 | |
48 | bool empty() { |
49 | return m_subQueries.isEmpty() && m_term.isEmpty(); |
50 | } |
51 | |
52 | QVector<EngineQuery> subQueries() const { |
53 | return m_subQueries; |
54 | } |
55 | |
56 | bool operator ==(const EngineQuery& q) const { |
57 | return m_term == q.m_term && m_op == q.m_op && m_subQueries == q.m_subQueries; |
58 | } |
59 | private: |
60 | QByteArray m_term; |
61 | Operation m_op; |
62 | |
63 | QVector<EngineQuery> m_subQueries; |
64 | }; |
65 | |
66 | inline QDebug operator<<(QDebug d, const Baloo::EngineQuery& q) |
67 | { |
68 | QDebugStateSaver state(d); |
69 | d.setAutoInsertSpaces(false); |
70 | |
71 | using Operation = Baloo::EngineQuery::Operation; |
72 | if ((q.op() == Operation::Equal) || q.op() == Operation::StartsWith) { |
73 | Q_ASSERT(q.subQueries().isEmpty()); |
74 | return d << q.term() << (q.op() == Operation::StartsWith ? ".." : "" ); |
75 | } |
76 | |
77 | Q_ASSERT(q.op() == Operation::Phrase); |
78 | d << "[PHRASE" ; |
79 | for (auto &sq : q.subQueries()) { |
80 | d << " " << sq; |
81 | } |
82 | return d << "]" ; |
83 | } |
84 | |
85 | /** |
86 | * Helper for QTest |
87 | * \sa QTest::toString |
88 | * |
89 | * @since: 5.70 |
90 | */ |
91 | inline char *toString(const EngineQuery& query) |
92 | { |
93 | QString buffer; |
94 | QDebug stream(&buffer); |
95 | stream << query; |
96 | return qstrdup(buffer.toUtf8().constData()); |
97 | } |
98 | |
99 | } // namespace Baloo |
100 | #endif |
101 | |