Akonadi Search

lib/query.cpp
1/*
2 * This file is part of the KDE Akonadi Search Project
3 * SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 *
7 */
8
9#include "query.h"
10#include "akonadi_search_pim_debug.h"
11#include "contactquery.h"
12
13#include <QVariant>
14
15#include <Akonadi/ServerManager>
16#include <QDir>
17#include <QJsonDocument>
18#include <QStandardPaths>
19
20using namespace Akonadi::Search::PIM;
21
22Query::Query() = default;
23
24Query::~Query() = default;
25
26Query *Query::fromJSON(const QByteArray &json)
27{
29 QJsonDocument doc = QJsonDocument::fromJson(json, &error);
30 if (doc.isNull()) {
31 qCWarning(AKONADI_SEARCH_PIM_LOG) << "Could not parse json query" << error.errorString();
32 return nullptr;
33 }
34
35 const QVariantMap result = doc.toVariant().toMap();
36 const QString type = result[QStringLiteral("type")].toString().toLower();
37 if (type != QLatin1StringView("contact")) {
38 qCWarning(AKONADI_SEARCH_PIM_LOG) << "Can only handle contact queries";
39 return nullptr;
40 }
41
42 auto cq = new ContactQuery();
43 cq->matchName(result[QStringLiteral("name")].toString());
44 cq->matchNickname(result[QStringLiteral("nick")].toString());
45 cq->matchEmail(result[QStringLiteral("email")].toString());
46 cq->matchUID(result[QStringLiteral("uid")].toString());
47 cq->match(result[QStringLiteral("$")].toString());
48
49 const QString criteria = result[QStringLiteral("matchCriteria")].toString().toLower();
50 if (criteria == QLatin1StringView("exact")) {
51 cq->setMatchCriteria(ContactQuery::ExactMatch);
52 } else if (criteria == QLatin1StringView("startswith")) {
53 cq->setMatchCriteria(ContactQuery::StartsWithMatch);
54 }
55
56 cq->setLimit(result[QStringLiteral("limit")].toInt());
57
58 return cq;
59}
60
61QString Query::defaultLocation(const QString &dbName)
62{
63 // First look into the old location from Baloo times in ~/.local/share/baloo,
64 // because we don't migrate the database files automatically.
65 QString basePath;
66 bool hasInstanceIdentifier = Akonadi::ServerManager::hasInstanceIdentifier();
67 if (hasInstanceIdentifier) {
68 basePath = QStringLiteral("baloo/instances/%1").arg(Akonadi::ServerManager::instanceIdentifier());
69 } else {
70 basePath = QStringLiteral("baloo");
71 }
72 QString dbPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/%1/%2/").arg(basePath, dbName);
73 if (QDir(dbPath).exists()) {
74 return dbPath;
75 }
76
77 // If the database does not exist in old Baloo folders, than use the new
78 // location in Akonadi's datadir in ~/.local/share/akonadi/search_db.
79 if (hasInstanceIdentifier) {
80 basePath = QStringLiteral("akonadi/instance/%1/search_db").arg(Akonadi::ServerManager::instanceIdentifier());
81 } else {
82 basePath = QStringLiteral("akonadi/search_db");
83 }
84 dbPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/%1/%2/").arg(basePath, dbName);
85 QDir().mkpath(dbPath);
86 return dbPath;
87}
Query for a list of contacts matching a criteria.
Query base class.
Definition lib/query.h:24
static bool hasInstanceIdentifier()
static QString instanceIdentifier()
PIM specific search API.
Type type(const QSqlDatabase &db)
char * toString(const EngineQuery &query)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
bool mkpath(const QString &dirPath) const const
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
bool isNull() const const
QVariant toVariant() const const
QString writableLocation(StandardLocation type)
QString arg(Args &&... args) const const
QMap< QString, QVariant > toMap() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:15:27 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.