File indexing completed on 2024-06-16 05:00:54

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 #include "propertyregistry.h"
0021 
0022 #include <log.h>
0023 
0024 namespace Sink {
0025 namespace Private {
0026 
0027 template <typename T>
0028 QVariant parseString(const QString &);
0029 
0030 template <>
0031 QVariant parseString<QString>(const QString &s)
0032 {
0033     return QVariant::fromValue(s);
0034 }
0035 
0036 template <>
0037 QVariant parseString<QByteArray>(const QString &s)
0038 {
0039     return QVariant::fromValue(s.toUtf8());
0040 }
0041 
0042 template <>
0043 QVariant parseString<Sink::ApplicationDomain::Reference>(const QString &s)
0044 {
0045     return QVariant::fromValue(Sink::ApplicationDomain::Reference{s.toLatin1()});
0046 }
0047 
0048 template <>
0049 QVariant parseString<bool>(const QString &s)
0050 {
0051     if (s == "true") {
0052         return QVariant::fromValue(true);
0053     }
0054     return QVariant::fromValue(false);
0055 }
0056 
0057 template <>
0058 QVariant parseString<int>(const QString &s)
0059 {
0060     bool ok = false;
0061     auto n = s.toInt(&ok);
0062     if (ok) {
0063         return QVariant::fromValue(n);
0064     }
0065     return {};
0066 }
0067 
0068 template <>
0069 QVariant parseString<QList<QByteArray>>(const QString &s)
0070 {
0071     auto list = s.split(',');
0072     QList<QByteArray> result;
0073     std::transform(list.constBegin(), list.constEnd(), std::back_inserter(result), [] (const QString &s) { return s.toUtf8(); });
0074     return QVariant::fromValue(result);
0075 }
0076 
0077 template <>
0078 QVariant parseString<QStringList>(const QString &s)
0079 {
0080     return s.split(',');
0081 }
0082 
0083 template <>
0084 QVariant parseString<QDateTime>(const QString &s)
0085 {
0086     return QVariant::fromValue(QDateTime::fromString(s, Qt::ISODate));
0087 }
0088 
0089 template <>
0090 QVariant parseString<Sink::ApplicationDomain::Mail::Contact>(const QString &s)
0091 {
0092     Q_ASSERT(false);
0093     return QVariant{};
0094 }
0095 
0096 template <>
0097 QVariant parseString<QList<Sink::ApplicationDomain::Mail::Contact>>(const QString &s)
0098 {
0099     Q_ASSERT(false);
0100     return QVariant{};
0101 }
0102 
0103 template <>
0104 QVariant parseString<QList<Sink::ApplicationDomain::Contact::Email>>(const QString &s)
0105 {
0106     Q_ASSERT(false);
0107     return QVariant{};
0108 }
0109 
0110 PropertyRegistry &PropertyRegistry::instance()
0111 {
0112     static PropertyRegistry instance;
0113     return instance;
0114 }
0115 
0116 QVariant PropertyRegistry::parse(const QByteArray &type, const QByteArray &property, const QString &value)
0117 {
0118     auto parser = registry[type].properties[property].parser;
0119     if (parser) {
0120         return parser(value);
0121     }
0122     SinkWarningCtx(Sink::Log::Context{"PropertyRegistry"}) << "Couldn't find a parser for " << type << property;
0123     return QVariant{};
0124 }
0125 
0126     }
0127 }