Akonadi Search

notequery.cpp
1/*
2 * This file is part of the KDE Akonadi Search Project
3 * SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 *
7 */
8
9#include <xapian.h>
10
11#include "akonadi_search_pim_debug.h"
12#include "notequery.h"
13#include "resultiterator_p.h"
14
15#include <QDebug>
16#include <QFile>
17#include <QList>
18#include <QStandardPaths>
19
20using namespace Akonadi::Search::PIM;
21
22class Akonadi::Search::PIM::NoteQueryPrivate
23{
24public:
25 NoteQueryPrivate() = default;
26
27 QString title;
28 QString note;
29 int limit = 0;
30};
31
32NoteQuery::NoteQuery()
33 : Query()
34 , d(new NoteQueryPrivate)
35{
36}
37
38NoteQuery::~NoteQuery() = default;
39
40void NoteQuery::matchTitle(const QString &title)
41{
42 d->title = title;
43}
44
45void NoteQuery::matchNote(const QString &note)
46{
47 d->note = note;
48}
49
50void NoteQuery::setLimit(int limit)
51{
52 d->limit = limit;
53}
54
55int NoteQuery::limit() const
56{
57 return d->limit;
58}
59
60ResultIterator NoteQuery::exec()
61{
62 const QString dir = defaultLocation(QStringLiteral("notes"));
63
64 Xapian::Database db;
65 try {
66 db = Xapian::Database(QFile::encodeName(dir).toStdString());
67 } catch (const Xapian::DatabaseOpeningError &) {
68 qCWarning(AKONADI_SEARCH_PIM_LOG) << "Xapian Database does not exist at " << dir;
69 return {};
70 } catch (const Xapian::DatabaseCorruptError &) {
71 qCWarning(AKONADI_SEARCH_PIM_LOG) << "Xapian Database corrupted";
72 return {};
73 } catch (const Xapian::DatabaseError &e) {
74 qCWarning(AKONADI_SEARCH_PIM_LOG) << "Failed to open Xapian database:" << QString::fromStdString(e.get_error_string());
75 return {};
76 } catch (...) {
77 qCWarning(AKONADI_SEARCH_PIM_LOG) << "Random exception, but we do not want to crash";
78 return {};
79 }
80
81 QList<Xapian::Query> m_queries;
82
83 if (!d->note.isEmpty()) {
84 Xapian::QueryParser parser;
85 parser.set_database(db);
86 parser.add_prefix("", "BO");
87
88 const QByteArray baNote = d->note.toUtf8();
89 m_queries << parser.parse_query(baNote.constData(), Xapian::QueryParser::FLAG_PARTIAL);
90 }
91
92 if (!d->title.isEmpty()) {
93 Xapian::QueryParser parser;
94 parser.set_database(db);
95 parser.add_prefix("", "SU");
96 parser.set_default_op(Xapian::Query::OP_AND);
97
98 const QByteArray baTitle = d->title.toUtf8();
99 m_queries << parser.parse_query(baTitle.constData(), Xapian::QueryParser::FLAG_PARTIAL);
100 }
101 try {
102 Xapian::Query query(Xapian::Query::OP_OR, m_queries.begin(), m_queries.end());
103 // qDebug() << query.get_description().c_str();
104
105 Xapian::Enquire enquire(db);
106 enquire.set_query(query);
107
108 if (d->limit == 0) {
109 d->limit = 10000;
110 }
111
112 Xapian::MSet matches = enquire.get_mset(0, d->limit);
113
114 ResultIterator iter;
115 iter.d->init(matches);
116 return iter;
117 } catch (const Xapian::Error &e) {
118 qCWarning(AKONADI_SEARCH_PIM_LOG) << QString::fromStdString(e.get_type()) << QString::fromStdString(e.get_description());
119 return {};
120 }
121}
Query base class.
Definition lib/query.h:24
PIM specific search API.
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
KIOCORE_EXPORT QString dir(const QString &fileClass)
const char * constData() const const
QByteArray encodeName(const QString &fileName)
iterator begin()
iterator end()
QString fromStdString(const std::string &str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:10:52 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.