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"
10using namespace Qt::Literals::StringLiterals;
11
12#include "akonadi_search_pim_debug.h"
13#include "contactquery.h"
14
15#include <QVariant>
16
17#include <Akonadi/ServerManager>
18#include <QDir>
19#include <QJsonDocument>
20#include <QStandardPaths>
21
22using namespace Akonadi::Search::PIM;
23
24Query::Query() = default;
25
26Query::~Query() = default;
27
28Query *Query::fromJSON(const QByteArray &json)
29{
31 QJsonDocument doc = QJsonDocument::fromJson(json, &error);
32 if (doc.isNull()) {
33 qCWarning(AKONADI_SEARCH_PIM_LOG) << "Could not parse json query" << error.errorString();
34 return nullptr;
35 }
36
37 const QVariantMap result = doc.toVariant().toMap();
38 const QString type = result[QStringLiteral("type")].toString().toLower();
39 if (type != "contact"_L1) {
40 qCWarning(AKONADI_SEARCH_PIM_LOG) << "Can only handle contact queries";
41 return nullptr;
42 }
43
44 auto cq = new ContactQuery();
45 cq->matchName(result[QStringLiteral("name")].toString());
46 cq->matchNickname(result[QStringLiteral("nick")].toString());
47 cq->matchEmail(result[QStringLiteral("email")].toString());
48 cq->matchUID(result[QStringLiteral("uid")].toString());
49 cq->match(result[QStringLiteral("$")].toString());
50
51 const QString criteria = result[QStringLiteral("matchCriteria")].toString().toLower();
52 if (criteria == "exact"_L1) {
53 cq->setMatchCriteria(ContactQuery::ExactMatch);
54 } else if (criteria == "startswith"_L1) {
55 cq->setMatchCriteria(ContactQuery::StartsWithMatch);
56 }
57
58 cq->setLimit(result[QStringLiteral("limit")].toInt());
59
60 return cq;
61}
62
63QString Query::defaultLocation(const QString &dbName)
64{
65 // First look into the old location from Baloo times in ~/.local/share/baloo,
66 // because we don't migrate the database files automatically.
67 QString basePath;
68 bool hasInstanceIdentifier = Akonadi::ServerManager::hasInstanceIdentifier();
69 if (hasInstanceIdentifier) {
70 basePath = QStringLiteral("baloo/instances/%1").arg(Akonadi::ServerManager::instanceIdentifier());
71 } else {
72 basePath = QStringLiteral("baloo");
73 }
74 QString dbPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/%1/%2/").arg(basePath, dbName);
75 if (QDir(dbPath).exists()) {
76 return dbPath;
77 }
78
79 // If the database does not exist in old Baloo folders, than use the new
80 // location in Akonadi's datadir in ~/.local/share/akonadi/search_db.
81 if (hasInstanceIdentifier) {
82 basePath = QStringLiteral("akonadi/instance/%1/search_db").arg(Akonadi::ServerManager::instanceIdentifier());
83 } else {
84 basePath = QStringLiteral("akonadi/search_db");
85 }
86 dbPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/%1/%2/").arg(basePath, dbName);
87 QDir().mkpath(dbPath);
88 return dbPath;
89}
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 Sat Dec 21 2024 16:56:07 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.