File indexing completed on 2024-11-24 04:44:41
0001 /* 0002 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include <KHealthCertificate> 0008 #include <KHealthCertificateParser> 0009 #include <KRecoveryCertificate> 0010 #include <KTestCertificate> 0011 #include <KVaccinationCertificate> 0012 0013 #include <QCoreApplication> 0014 #include <QQmlEngine> 0015 #include <QQmlExtensionPlugin> 0016 0017 class KHealthCertificateQmlPlugin : public QQmlExtensionPlugin 0018 { 0019 Q_OBJECT 0020 Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface") 0021 public: 0022 void registerTypes(const char* uri) override; 0023 }; 0024 0025 0026 class KHealthCertificateParserWrapper 0027 { 0028 Q_GADGET 0029 public: 0030 Q_INVOKABLE QVariant parse(const QVariant &data) const; 0031 }; 0032 0033 QVariant KHealthCertificateParserWrapper::parse(const QVariant &data) const 0034 { 0035 if (data.userType() == QMetaType::QByteArray) { 0036 return KHealthCertificateParser::parse(data.toByteArray()); 0037 } 0038 if (data.userType() == QMetaType::QString) { 0039 return KHealthCertificateParser::parse(data.toString().toUtf8()); 0040 } 0041 return {}; 0042 } 0043 0044 void KHealthCertificateQmlPlugin::registerTypes(const char*) 0045 { 0046 qRegisterMetaType<KRecoveryCertificate>(); 0047 qRegisterMetaType<KTestCertificate>(); 0048 qRegisterMetaType<KVaccinationCertificate>(); 0049 0050 qmlRegisterUncreatableMetaObject(KHealthCertificate::staticMetaObject, "org.kde.khealthcertificate", 1, 0, "HealthCertificate", {}); 0051 qmlRegisterUncreatableMetaObject(KTestCertificate::staticMetaObject, "org.kde.khealthcertificate", 1, 0, "TestCertificate", {}); 0052 qmlRegisterUncreatableMetaObject(KVaccinationCertificate::staticMetaObject, "org.kde.khealthcertificate", 1, 0, "VaccinationCertificate", {}); 0053 0054 // HACK qmlplugindump chokes on gadget singletons, to the point of breaking ecm_find_qmlmodule() 0055 if (QCoreApplication::applicationName() != QLatin1String("qmlplugindump")) { 0056 qmlRegisterSingletonType("org.kde.khealthcertificate", 1, 0, "HealthCertificateParser", [](QQmlEngine*, QJSEngine *engine) -> QJSValue { 0057 return engine->toScriptValue(KHealthCertificateParserWrapper()); 0058 }); 0059 } 0060 } 0061 0062 #include "khealthcertificateqmlplugin.moc"