Akonadi Search

notequery.cpp
1/*
2 * This file is part of the KDE Akonadi Search Project
3 * SPDX-FileCopyrightText: 2014-2025 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 : d(new NoteQueryPrivate)
34{
35}
36
37NoteQuery::~NoteQuery() = default;
38
39void NoteQuery::matchTitle(const QString &title)
40{
41 d->title = title;
42}
43
44void NoteQuery::matchNote(const QString &note)
45{
46 d->note = note;
47}
48
49void NoteQuery::setLimit(int limit)
50{
51 d->limit = limit;
52}
53
54int NoteQuery::limit() const
55{
56 return d->limit;
57}
58
59ResultIterator NoteQuery::exec()
60{
61 const QString dir = defaultLocation(QStringLiteral("notes"));
62
63 Xapian::Database db;
64 try {
65 db = Xapian::Database(QFile::encodeName(dir).toStdString());
66 } catch (const Xapian::DatabaseOpeningError &) {
67 qCWarning(AKONADI_SEARCH_PIM_LOG) << "Xapian Database does not exist at " << dir;
68 return {};
69 } catch (const Xapian::DatabaseCorruptError &) {
70 qCWarning(AKONADI_SEARCH_PIM_LOG) << "Xapian Database corrupted";
71 return {};
72 } catch (const Xapian::DatabaseError &e) {
73 qCWarning(AKONADI_SEARCH_PIM_LOG) << "Failed to open Xapian database:" << QString::fromStdString(e.get_error_string());
74 return {};
75 } catch (...) {
76 qCWarning(AKONADI_SEARCH_PIM_LOG) << "Random exception, but we do not want to crash";
77 return {};
78 }
79
80 QList<Xapian::Query> m_queries;
81
82 if (!d->note.isEmpty()) {
83 Xapian::QueryParser parser;
84 parser.set_database(db);
85 parser.add_prefix("", "BO");
86
87 const QByteArray baNote = d->note.toUtf8();
88 m_queries << parser.parse_query(baNote.constData(), Xapian::QueryParser::FLAG_PARTIAL);
89 }
90
91 if (!d->title.isEmpty()) {
92 Xapian::QueryParser parser;
93 parser.set_database(db);
94 parser.add_prefix("", "SU");
95 parser.set_default_op(Xapian::Query::OP_AND);
96
97 const QByteArray baTitle = d->title.toUtf8();
98 m_queries << parser.parse_query(baTitle.constData(), Xapian::QueryParser::FLAG_PARTIAL);
99 }
100 try {
101 Xapian::Query query(Xapian::Query::OP_OR, m_queries.begin(), m_queries.end());
102 // qDebug() << query.get_description().c_str();
103
104 Xapian::Enquire enquire(db);
105 enquire.set_query(query);
106
107 if (d->limit == 0) {
108 d->limit = 10000;
109 }
110
111 Xapian::MSet matches = enquire.get_mset(0, d->limit);
112
113 ResultIterator iter;
114 iter.d->init(matches);
115 return iter;
116 } catch (const Xapian::Error &e) {
117 qCWarning(AKONADI_SEARCH_PIM_LOG) << QString::fromStdString(e.get_type()) << QString::fromStdString(e.get_description());
118 return {};
119 }
120}
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-2025 The KDE developers.
Generated on Fri Apr 25 2025 11:51:42 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.