Warning, file /pim/akonadi-search/lib/query.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * This file is part of the KDE Akonadi Search Project
0003  * SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006  *
0007  */
0008 
0009 #include "query.h"
0010 #include "akonadi_search_pim_debug.h"
0011 #include "contactquery.h"
0012 
0013 #include <QVariant>
0014 
0015 #include <Akonadi/ServerManager>
0016 #include <QDir>
0017 #include <QJsonDocument>
0018 #include <QStandardPaths>
0019 
0020 using namespace Akonadi::Search::PIM;
0021 
0022 Query::Query() = default;
0023 
0024 Query::~Query() = default;
0025 
0026 Query *Query::fromJSON(const QByteArray &json)
0027 {
0028     QJsonParseError error;
0029     QJsonDocument doc = QJsonDocument::fromJson(json, &error);
0030     if (doc.isNull()) {
0031         qCWarning(AKONADI_SEARCH_PIM_LOG) << "Could not parse json query" << error.errorString();
0032         return nullptr;
0033     }
0034 
0035     const QVariantMap result = doc.toVariant().toMap();
0036     const QString type = result[QStringLiteral("type")].toString().toLower();
0037     if (type != QLatin1StringView("contact")) {
0038         qCWarning(AKONADI_SEARCH_PIM_LOG) << "Can only handle contact queries";
0039         return nullptr;
0040     }
0041 
0042     auto cq = new ContactQuery();
0043     cq->matchName(result[QStringLiteral("name")].toString());
0044     cq->matchNickname(result[QStringLiteral("nick")].toString());
0045     cq->matchEmail(result[QStringLiteral("email")].toString());
0046     cq->matchUID(result[QStringLiteral("uid")].toString());
0047     cq->match(result[QStringLiteral("$")].toString());
0048 
0049     const QString criteria = result[QStringLiteral("matchCriteria")].toString().toLower();
0050     if (criteria == QLatin1StringView("exact")) {
0051         cq->setMatchCriteria(ContactQuery::ExactMatch);
0052     } else if (criteria == QLatin1StringView("startswith")) {
0053         cq->setMatchCriteria(ContactQuery::StartsWithMatch);
0054     }
0055 
0056     cq->setLimit(result[QStringLiteral("limit")].toInt());
0057 
0058     return cq;
0059 }
0060 
0061 QString Query::defaultLocation(const QString &dbName)
0062 {
0063     // First look into the old location from Baloo times in ~/.local/share/baloo,
0064     // because we don't migrate the database files automatically.
0065     QString basePath;
0066     bool hasInstanceIdentifier = Akonadi::ServerManager::hasInstanceIdentifier();
0067     if (hasInstanceIdentifier) {
0068         basePath = QStringLiteral("baloo/instances/%1").arg(Akonadi::ServerManager::instanceIdentifier());
0069     } else {
0070         basePath = QStringLiteral("baloo");
0071     }
0072     QString dbPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/%1/%2/").arg(basePath, dbName);
0073     if (QDir(dbPath).exists()) {
0074         return dbPath;
0075     }
0076 
0077     // If the database does not exist in old Baloo folders, than use the new
0078     // location in Akonadi's datadir in ~/.local/share/akonadi/search_db.
0079     if (hasInstanceIdentifier) {
0080         basePath = QStringLiteral("akonadi/instance/%1/search_db").arg(Akonadi::ServerManager::instanceIdentifier());
0081     } else {
0082         basePath = QStringLiteral("akonadi/search_db");
0083     }
0084     dbPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/%1/%2/").arg(basePath, dbName);
0085     QDir().mkpath(dbPath);
0086     return dbPath;
0087 }