File indexing completed on 2024-05-26 05:27:32

0001 /*
0002  * Copyright (C) 2017 Christian Mollekopf <chrigi_1@fastmail.fm>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 
0021 #pragma once
0022 
0023 #include <QHash>
0024 #include <QString>
0025 #include <QVariant>
0026 #include <functional>
0027 
0028 #include "applicationdomaintype.h"
0029 
0030 namespace Sink {
0031 namespace Private {
0032 
0033 template <typename T>
0034 QVariant parseString(const QString &);
0035 
0036 template <>
0037 QVariant parseString<QString>(const QString &s);
0038 
0039 template <>
0040 QVariant parseString<QByteArray>(const QString &s);
0041 
0042 template <>
0043 QVariant parseString<Sink::ApplicationDomain::Reference>(const QString &s);
0044 
0045 template <>
0046 QVariant parseString<bool>(const QString &s);
0047 
0048 template <>
0049 QVariant parseString<int>(const QString &s);
0050 
0051 template <>
0052 QVariant parseString<QList<QByteArray>>(const QString &s);
0053 
0054 template <>
0055 QVariant parseString<QDateTime>(const QString &s);
0056 
0057 template <>
0058 QVariant parseString<Sink::ApplicationDomain::Mail::Contact>(const QString &s);
0059 
0060 template <>
0061 QVariant parseString<QList<Sink::ApplicationDomain::Mail::Contact>>(const QString &s);
0062 
0063 class PropertyRegistry
0064 {
0065 public:
0066     struct Type {
0067         struct Property {
0068             std::function<QVariant(const QString &)> parser;
0069         };
0070         QHash<QByteArray, Property> properties;
0071     };
0072 
0073     QHash<QByteArray, Type> registry;
0074 
0075     static PropertyRegistry &instance();
0076 
0077     template <typename PropertyType>
0078     void registerProperty(const QByteArray &entityType) {
0079         registry[entityType].properties[PropertyType::name].parser = Sink::Private::parseString<typename PropertyType::Type>;
0080     }
0081 
0082     QVariant parse(const QByteArray &type, const QByteArray &property, const QString &value);
0083 };
0084 
0085     }
0086 }
0087