11#include "akonadi_search_pim_debug.h"
12#include "contactquery.h"
13#include "resultiterator_p.h"
17#include <QStandardPaths>
21class Akonadi::Search::PIM::ContactQueryPrivate
31 ContactQuery::MatchCriteria criteria;
34ContactQuery::ContactQuery()
36 , d(new ContactQueryPrivate)
38 d->criteria = StartsWithMatch;
41ContactQuery::~ContactQuery() =
default;
43void ContactQuery::matchName(
const QString &name)
48void ContactQuery::matchNickname(
const QString &nick)
53void ContactQuery::matchEmail(
const QString &email)
58void ContactQuery::matchUID(
const QString &uid)
63void ContactQuery::match(
const QString &str)
68int ContactQuery::limit()
const
73void ContactQuery::setLimit(
int limit)
78ContactQuery::MatchCriteria ContactQuery::matchCriteria()
const
83void ContactQuery::setMatchCriteria(ContactQuery::MatchCriteria m)
90 const QString dir = defaultLocation(QStringLiteral(
"contacts"));
95 }
catch (
const Xapian::DatabaseOpeningError &) {
96 qCWarning(AKONADI_SEARCH_PIM_LOG) <<
"Xapian Database does not exist at " <<
dir;
98 }
catch (
const Xapian::DatabaseCorruptError &) {
99 qCWarning(AKONADI_SEARCH_PIM_LOG) <<
"Xapian Database corrupted";
101 }
catch (
const Xapian::DatabaseError &e) {
102 qCWarning(AKONADI_SEARCH_PIM_LOG) <<
"Failed to open Xapian database:" <<
QString::fromStdString(e.get_error_string());
105 qCWarning(AKONADI_SEARCH_PIM_LOG) <<
"Random exception, but we do not want to crash";
111 if (d->criteria == ExactMatch) {
112 if (!d->any.isEmpty()) {
114 m_queries << Xapian::Query(ba.
constData());
117 if (!d->name.isEmpty()) {
118 const QByteArray ba =
"NA" + d->name.toUtf8();
119 m_queries << Xapian::Query(ba.
constData());
122 if (!d->nick.isEmpty()) {
123 const QByteArray ba =
"NI" + d->nick.toUtf8();
124 m_queries << Xapian::Query(ba.
constData());
127 if (!d->email.isEmpty()) {
129 m_queries << Xapian::Query(ba.
constData());
132 if (!d->uid.isEmpty()) {
133 const QByteArray ba =
"UID" + d->uid.toUtf8();
134 m_queries << Xapian::Query(ba.
constData());
136 }
else if (d->criteria == StartsWithMatch) {
137 if (!d->any.isEmpty()) {
138 Xapian::QueryParser parser;
139 parser.set_database(db);
141 m_queries << parser.parse_query(ba.
constData(), Xapian::QueryParser::FLAG_PARTIAL);
144 if (!d->name.isEmpty()) {
145 Xapian::QueryParser parser;
146 parser.set_database(db);
147 parser.add_prefix(
"",
"NA");
149 m_queries << parser.parse_query(ba.
constData(), Xapian::QueryParser::FLAG_PARTIAL);
152 if (!d->nick.isEmpty()) {
153 Xapian::QueryParser parser;
154 parser.set_database(db);
155 parser.add_prefix(
"",
"NI");
157 m_queries << parser.parse_query(ba.
constData(), Xapian::QueryParser::FLAG_PARTIAL);
161 if (!d->email.isEmpty()) {
162 Xapian::QueryParser parser;
163 parser.set_database(db);
165 m_queries << parser.parse_query(ba.
constData(), Xapian::QueryParser::FLAG_PARTIAL);
168 if (!d->uid.isEmpty()) {
169 Xapian::QueryParser parser;
170 parser.set_database(db);
171 parser.add_prefix(
"",
"UID");
173 m_queries << parser.parse_query(ba.
constData(), Xapian::QueryParser::FLAG_PARTIAL);
177 Xapian::Query
query(Xapian::Query::OP_OR, m_queries.
begin(), m_queries.
end());
178 qCDebug(AKONADI_SEARCH_PIM_LOG) <<
query.get_description().c_str();
180 Xapian::Enquire enquire(db);
181 enquire.set_query(query);
187 Xapian::MSet matches = enquire.get_mset(0, d->limit);
190 iter.d->init(matches);
192 }
catch (
const Xapian::Error &e) {
std::optional< QSqlQuery > query(const QString &queryStatement)
KIOCORE_EXPORT QString dir(const QString &fileClass)
QString name(StandardAction id)
const char * constData() const const
QByteArray encodeName(const QString &fileName)
QString fromStdString(const std::string &str)