parttypehelper.cpp 2.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
    Copyright (c) 2011 Volker Krause <vkrause@kde.org>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "parttypehelper.h"

#include "selectquerybuilder.h"
#include "entities.h"

Daniel Vrátil's avatar
Daniel Vrátil committed
25
#include <QStringList>
26

Daniel Vrátil's avatar
Daniel Vrátil committed
27
28
#include <tuple>

29
using namespace Akonadi::Server;
30

Daniel Vrátil's avatar
Daniel Vrátil committed
31
std::pair<QString, QString> PartTypeHelper::parseFqName(const QString &fqName)
32
{
Laurent Montel's avatar
Laurent Montel committed
33
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
34
    const QStringList name = fqName.split(QLatin1Char(':'), QString::SkipEmptyParts);
Laurent Montel's avatar
Laurent Montel committed
35
36
37
#else
    const QStringList name = fqName.split(QLatin1Char(':'), Qt::SkipEmptyParts);
#endif
38
39
40
    if (name.size() != 2) {
        throw PartTypeException("Invalid part type name.");
    }
Daniel Vrátil's avatar
Daniel Vrátil committed
41
    return {name.first(), name.last()};
42
43
}

44
PartType PartTypeHelper::fromFqName(const QString &fqName)
45
{
Daniel Vrátil's avatar
Daniel Vrátil committed
46
    return std::apply(qOverload<const QString &, const QString &>(PartTypeHelper::fromFqName), parseFqName(fqName));
47
48
}

49
PartType PartTypeHelper::fromFqName(const QByteArray &fqName)
50
{
51
    return fromFqName(QLatin1String(fqName));
52
53
}

Guy Maurel's avatar
Guy Maurel committed
54
PartType PartTypeHelper::fromFqName(const QString &ns, const QString &name)
55
{
56
    const PartType partType = PartType::retrieveByFQNameOrCreate(ns, name);
Daniel Vrátil's avatar
Daniel Vrátil committed
57
    if (!partType.isValid()) {
58
        throw PartTypeException("Failed to append part type");
59
    }
Daniel Vrátil's avatar
Daniel Vrátil committed
60
    return partType;
61
}
62

63
Query::Condition PartTypeHelper::conditionFromFqName(const QString &fqName)
64
{
Daniel Vrátil's avatar
Daniel Vrátil committed
65
    const auto [ns, name] = parseFqName(fqName);
66
67
    Query::Condition c;
    c.setSubQueryMode(Query::And);
Daniel Vrátil's avatar
Daniel Vrátil committed
68
69
    c.addValueCondition(PartType::nsFullColumnName(), Query::Equals, ns);
    c.addValueCondition(PartType::nameFullColumnName(), Query::Equals, name);
70
    return c;
71
72
}

73
Query::Condition PartTypeHelper::conditionFromFqNames(const QStringList &fqNames)
74
{
75
76
    Query::Condition c;
    c.setSubQueryMode(Query::Or);
Laurent Montel's avatar
Laurent Montel committed
77
    for (const QString &fqName : fqNames) {
78
79
80
        c.addCondition(conditionFromFqName(fqName));
    }
    return c;
81
}
Volker Krause's avatar
Volker Krause committed
82

83
QString PartTypeHelper::fullName(const PartType &type)
84
{
85
    return type.ns() + QLatin1String(":") + type.name();
86
}