File indexing completed on 2024-04-14 03:49:41

0001 /*
0002     This file is part of the KDE Baloo Project
0003     SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #ifndef BALOO_ENGINEQUERY_H
0009 #define BALOO_ENGINEQUERY_H
0010 
0011 #include "engine_export.h"
0012 
0013 #include <QByteArray>
0014 #include <QVector>
0015 #include <QDebug>
0016 
0017 namespace Baloo {
0018 
0019 class BALOO_ENGINE_EXPORT EngineQuery
0020 {
0021 public:
0022     enum Operation {
0023         Equal,
0024         StartsWith,
0025         Phrase,
0026     };
0027 
0028     EngineQuery();
0029     EngineQuery(const QByteArray& term, Operation op = Equal);
0030     EngineQuery(const QVector<EngineQuery> &subQueries);
0031 
0032     QByteArray term() const {
0033         return m_term;
0034     }
0035 
0036     Operation op() const {
0037         return m_op;
0038     }
0039 
0040     void setOp(const Operation& op) {
0041         m_op = op;
0042     }
0043 
0044     bool leaf() const {
0045         return !m_term.isEmpty();
0046     }
0047 
0048     bool empty() {
0049         return m_subQueries.isEmpty() && m_term.isEmpty();
0050     }
0051 
0052     QVector<EngineQuery> subQueries() const {
0053         return m_subQueries;
0054     }
0055 
0056     bool operator ==(const EngineQuery& q) const {
0057         return m_term == q.m_term && m_op == q.m_op && m_subQueries == q.m_subQueries;
0058     }
0059 private:
0060     QByteArray m_term;
0061     Operation m_op;
0062 
0063     QVector<EngineQuery> m_subQueries;
0064 };
0065 
0066 inline QDebug operator<<(QDebug d, const Baloo::EngineQuery& q)
0067 {
0068     QDebugStateSaver state(d);
0069     d.setAutoInsertSpaces(false);
0070 
0071     using Operation = Baloo::EngineQuery::Operation;
0072     if ((q.op() == Operation::Equal) || q.op() == Operation::StartsWith) {
0073         Q_ASSERT(q.subQueries().isEmpty());
0074         return d << q.term() << (q.op() == Operation::StartsWith ? ".." : "");
0075     }
0076 
0077     Q_ASSERT(q.op() == Operation::Phrase);
0078     d << "[PHRASE";
0079     for (auto &sq : q.subQueries()) {
0080         d << " " << sq;
0081     }
0082     return d << "]";
0083 }
0084 
0085 /**
0086  * Helper for QTest
0087  * \sa QTest::toString
0088  *
0089  * @since: 5.70
0090  */
0091 inline char *toString(const EngineQuery& query)
0092 {
0093     QString buffer;
0094     QDebug stream(&buffer);
0095     stream << query;
0096     return qstrdup(buffer.toUtf8().constData());
0097 }
0098 
0099 } // namespace Baloo
0100 #endif