File indexing completed on 2025-01-05 04:47:00

0001 /*
0002     SPDX-FileCopyrightText: 2011 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "parttypehelper.h"
0008 
0009 #include "entities.h"
0010 #include "selectquerybuilder.h"
0011 
0012 #include <QStringList>
0013 
0014 #include <tuple>
0015 
0016 using namespace Akonadi::Server;
0017 
0018 std::pair<QString, QString> PartTypeHelper::parseFqName(const QString &fqName)
0019 {
0020     const QStringList name = fqName.split(QLatin1Char(':'), Qt::SkipEmptyParts);
0021     if (name.size() != 2) {
0022         throw PartTypeException("Invalid part type name.");
0023     }
0024     return {name.first(), name.last()};
0025 }
0026 
0027 PartType PartTypeHelper::fromFqName(const QString &fqName)
0028 {
0029     return std::apply(qOverload<const QString &, const QString &>(PartTypeHelper::fromFqName), parseFqName(fqName));
0030 }
0031 
0032 PartType PartTypeHelper::fromFqName(const QByteArray &fqName)
0033 {
0034     return fromFqName(QLatin1StringView(fqName));
0035 }
0036 
0037 PartType PartTypeHelper::fromFqName(const QString &ns, const QString &name)
0038 {
0039     const PartType partType = PartType::retrieveByFQNameOrCreate(ns, name);
0040     if (!partType.isValid()) {
0041         throw PartTypeException("Failed to append part type");
0042     }
0043     return partType;
0044 }
0045 
0046 Query::Condition PartTypeHelper::conditionFromFqName(const QString &fqName)
0047 {
0048     const auto [ns, name] = parseFqName(fqName);
0049     Query::Condition c;
0050     c.setSubQueryMode(Query::And);
0051     c.addValueCondition(PartType::nsFullColumnName(), Query::Equals, ns);
0052     c.addValueCondition(PartType::nameFullColumnName(), Query::Equals, name);
0053     return c;
0054 }
0055 
0056 Query::Condition PartTypeHelper::conditionFromFqNames(const QStringList &fqNames)
0057 {
0058     Query::Condition c;
0059     c.setSubQueryMode(Query::Or);
0060     for (const QString &fqName : fqNames) {
0061         c.addCondition(conditionFromFqName(fqName));
0062     }
0063     return c;
0064 }
0065 
0066 QString PartTypeHelper::fullName(const PartType &type)
0067 {
0068     return type.ns() + QLatin1StringView(":") + type.name();
0069 }