Akonadi Search

notequery.cpp
1 /*
2  * This file is part of the KDE Akonadi Search Project
3  * SPDX-FileCopyrightText: 2014-2023 Laurent Montel <[email protected]>
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 
20 using namespace Akonadi::Search::PIM;
21 
22 class Akonadi::Search::PIM::NoteQueryPrivate
23 {
24 public:
25  NoteQueryPrivate() = default;
26 
27  QString title;
28  QString note;
29  int limit = 0;
30 };
31 
32 NoteQuery::NoteQuery()
33  : Query()
34  , d(new NoteQueryPrivate)
35 {
36 }
37 
38 NoteQuery::~NoteQuery() = default;
39 
40 void NoteQuery::matchTitle(const QString &title)
41 {
42  d->title = title;
43 }
44 
45 void NoteQuery::matchNote(const QString &note)
46 {
47  d->note = note;
48 }
49 
50 void NoteQuery::setLimit(int limit)
51 {
52  d->limit = limit;
53 }
54 
55 int NoteQuery::limit() const
56 {
57  return d->limit;
58 }
59 
60 ResultIterator 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 }
std::optional< QSqlQuery > query(const QString &queryStatement)
QByteArray encodeName(const QString &fileName)
QString fromStdString(const std::string &str)
Query base class.
Definition: lib/query.h:23
PIM specific search API.
KIOFILEWIDGETS_EXPORT QString dir(const QString &fileClass)
const char * constData() const const
QList::iterator begin()
QList::iterator end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Fri Dec 1 2023 04:09:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.