File indexing completed on 2024-04-14 03:52:16
0001 /* 0002 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include <KCountry> 0008 #include <KCountrySubdivision> 0009 #include <KTimeZone> 0010 0011 #include <QCoreApplication> 0012 #include <QQmlContext> 0013 #include <QQmlEngine> 0014 #include <QQmlExtensionPlugin> 0015 0016 #include <cstring> 0017 0018 class KI18nLocaleDataQmlPlugin : public QQmlExtensionPlugin 0019 { 0020 Q_OBJECT 0021 Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface") 0022 public: 0023 void registerTypes(const char *uri) override; 0024 }; 0025 0026 // return "undefined" for invalid objects, so JS conditionals work as expected 0027 template<typename T> 0028 static QJSValue toJsValue(T value, QJSEngine *engine) 0029 { 0030 return value.isValid() ? engine->toScriptValue(value) : QJSValue(QJSValue::UndefinedValue); 0031 } 0032 0033 class KCountryFactory 0034 { 0035 Q_GADGET 0036 Q_PROPERTY(QList<KCountry> allCountries READ allCountries) 0037 public: 0038 Q_INVOKABLE QJSValue fromAlpha2(const QString &code) const 0039 { 0040 return toJsValue(KCountry::fromAlpha2(code), m_engine); 0041 } 0042 Q_INVOKABLE QJSValue fromAlpha3(const QString &code) const 0043 { 0044 return toJsValue(KCountry::fromAlpha3(code), m_engine); 0045 } 0046 Q_INVOKABLE QJSValue fromName(const QString &name) const 0047 { 0048 return toJsValue(KCountry::fromName(name), m_engine); 0049 } 0050 Q_INVOKABLE QJSValue fromLocation(double latitude, double longitude) const 0051 { 0052 return toJsValue(KCountry::fromLocation(latitude, longitude), m_engine); 0053 } 0054 0055 QJSEngine *m_engine = nullptr; 0056 0057 private: 0058 QList<KCountry> allCountries() const 0059 { 0060 return KCountry::allCountries(); 0061 } 0062 }; 0063 0064 class KCountrySubdivisionFactory 0065 { 0066 Q_GADGET 0067 public: 0068 Q_INVOKABLE QJSValue fromCode(const QString &code) const 0069 { 0070 return toJsValue(KCountrySubdivision::fromCode(code), m_engine); 0071 } 0072 Q_INVOKABLE QJSValue fromLocation(double latitude, double longitude) const 0073 { 0074 return toJsValue(KCountrySubdivision::fromLocation(latitude, longitude), m_engine); 0075 } 0076 0077 QJSEngine *m_engine = nullptr; 0078 }; 0079 0080 class KTimeZoneWrapper 0081 { 0082 Q_GADGET 0083 public: 0084 Q_INVOKABLE QJSValue fromLocation(double latitude, double longitude) const 0085 { 0086 const auto tzId = KTimeZone::fromLocation(latitude, longitude); 0087 return tzId ? QString::fromUtf8(tzId) : QJSValue(QJSValue::UndefinedValue); 0088 } 0089 0090 Q_INVOKABLE QJSValue country(const QString &tzId) const 0091 { 0092 return toJsValue(KTimeZone::country(tzId.toUtf8()), m_engine); 0093 } 0094 0095 QJSEngine *m_engine = nullptr; 0096 }; 0097 0098 void KI18nLocaleDataQmlPlugin::registerTypes(const char *uri) 0099 { 0100 Q_ASSERT(std::strcmp(uri, "org.kde.i18n.localeData") == 0); 0101 0102 qRegisterMetaType<KCountry>(); 0103 qRegisterMetaType<KCountrySubdivision>(); 0104 qRegisterMetaType<QList<KCountrySubdivision>>(); 0105 0106 // HACK qmlplugindump chokes on gadget singletons, to the point of breaking ecm_find_qmlmodule() 0107 if (QCoreApplication::applicationName() != QLatin1String("qmlplugindump")) { 0108 qmlRegisterSingletonType(uri, 1, 0, "Country", [](QQmlEngine *, QJSEngine *engine) -> QJSValue { 0109 KCountryFactory factory; 0110 factory.m_engine = engine; 0111 return engine->toScriptValue(factory); 0112 }); 0113 qmlRegisterSingletonType(uri, 1, 0, "CountrySubdivision", [](QQmlEngine *, QJSEngine *engine) -> QJSValue { 0114 KCountrySubdivisionFactory factory; 0115 factory.m_engine = engine; 0116 return engine->toScriptValue(factory); 0117 }); 0118 qmlRegisterSingletonType(uri, 1, 0, "TimeZone", [](QQmlEngine *, QJSEngine *engine) -> QJSValue { 0119 KTimeZoneWrapper wrapper; 0120 wrapper.m_engine = engine; 0121 return engine->toScriptValue(wrapper); 0122 }); 0123 } 0124 } 0125 0126 #include "ki18nlocaledataqmlplugin.moc"