KI18n

ki18nlocaledataqmlplugin.cpp
1/*
2 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include <KCountry>
8#include <KCountrySubdivision>
9#include <KTimeZone>
10
11#include <QCoreApplication>
12#include <QQmlContext>
13#include <QQmlEngine>
14#include <QQmlExtensionPlugin>
15
16#include <cstring>
17
18class KI18nLocaleDataQmlPlugin : public QQmlExtensionPlugin
19{
21 Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
22public:
23 void registerTypes(const char *uri) override;
24};
25
26// return "undefined" for invalid objects, so JS conditionals work as expected
27template<typename T>
28static QJSValue toJsValue(T value, QJSEngine *engine)
29{
30 return value.isValid() ? engine->toScriptValue(value) : QJSValue(QJSValue::UndefinedValue);
31}
32
33class KCountryFactory
34{
35 Q_GADGET
36 Q_PROPERTY(QList<KCountry> allCountries READ allCountries)
37public:
38 Q_INVOKABLE QJSValue fromAlpha2(const QString &code) const
39 {
40 return toJsValue(KCountry::fromAlpha2(code), m_engine);
41 }
42 Q_INVOKABLE QJSValue fromAlpha3(const QString &code) const
43 {
44 return toJsValue(KCountry::fromAlpha3(code), m_engine);
45 }
46 Q_INVOKABLE QJSValue fromName(const QString &name) const
47 {
48 return toJsValue(KCountry::fromName(name), m_engine);
49 }
50 Q_INVOKABLE QJSValue fromLocation(double latitude, double longitude) const
51 {
52 return toJsValue(KCountry::fromLocation(latitude, longitude), m_engine);
53 }
54
55 QJSEngine *m_engine = nullptr;
56
57private:
58 QList<KCountry> allCountries() const
59 {
61 }
62};
63
64class KCountrySubdivisionFactory
65{
66 Q_GADGET
67public:
68 Q_INVOKABLE QJSValue fromCode(const QString &code) const
69 {
70 return toJsValue(KCountrySubdivision::fromCode(code), m_engine);
71 }
72 Q_INVOKABLE QJSValue fromLocation(double latitude, double longitude) const
73 {
74 return toJsValue(KCountrySubdivision::fromLocation(latitude, longitude), m_engine);
75 }
76
77 QJSEngine *m_engine = nullptr;
78};
79
80class KTimeZoneWrapper
81{
82 Q_GADGET
83public:
84 Q_INVOKABLE QJSValue fromLocation(double latitude, double longitude) const
85 {
86 const auto tzId = KTimeZone::fromLocation(latitude, longitude);
87 return tzId ? QString::fromUtf8(tzId) : QJSValue(QJSValue::UndefinedValue);
88 }
89
90 Q_INVOKABLE QJSValue country(const QString &tzId) const
91 {
92 return toJsValue(KTimeZone::country(tzId.toUtf8()), m_engine);
93 }
94
95 QJSEngine *m_engine = nullptr;
96};
97
98void KI18nLocaleDataQmlPlugin::registerTypes(const char *uri)
99{
100 Q_ASSERT(std::strcmp(uri, "org.kde.i18n.localeData") == 0);
101
102 qRegisterMetaType<KCountry>();
103 qRegisterMetaType<KCountrySubdivision>();
104 qRegisterMetaType<QList<KCountrySubdivision>>();
105
106 // HACK qmlplugindump chokes on gadget singletons, to the point of breaking ecm_find_qmlmodule()
107 if (QCoreApplication::applicationName() != QLatin1String("qmlplugindump")) {
108 qmlRegisterSingletonType(uri, 1, 0, "Country", [](QQmlEngine *, QJSEngine *engine) -> QJSValue {
109 KCountryFactory factory;
110 factory.m_engine = engine;
111 return engine->toScriptValue(factory);
112 });
113 qmlRegisterSingletonType(uri, 1, 0, "CountrySubdivision", [](QQmlEngine *, QJSEngine *engine) -> QJSValue {
114 KCountrySubdivisionFactory factory;
115 factory.m_engine = engine;
116 return engine->toScriptValue(factory);
117 });
118 qmlRegisterSingletonType(uri, 1, 0, "TimeZone", [](QQmlEngine *, QJSEngine *engine) -> QJSValue {
119 KTimeZoneWrapper wrapper;
120 wrapper.m_engine = engine;
121 return engine->toScriptValue(wrapper);
122 });
123 }
124}
125
126#include "ki18nlocaledataqmlplugin.moc"
static KCountrySubdivision fromCode(QStringView code)
Create a KCountrySubdivision instance from an ISO 3166-2 code.
static KCountrySubdivision fromLocation(float latitude, float longitude)
Looks up the country subdivision at the given geographic coordinate.
static KCountry fromLocation(float latitude, float longitude)
Looks up the country at the given geographic coordinate.
Definition kcountry.cpp:264
static QList< KCountry > allCountries()
List all countries.
Definition kcountry.cpp:403
static KCountry fromName(QStringView name)
Attempts to identify the country from the given name.
Definition kcountry.cpp:347
static KCountry fromAlpha2(QStringView alpha2Code)
Create a KCountry instance from an ISO 3166-1 alpha 2 code.
Definition kcountry.cpp:215
static KCountry fromAlpha3(QStringView alpha3Code)
Create a KCountry instance from an ISO 3166-1 alpha 3 code.
Definition kcountry.cpp:247
KI18NLOCALEDATA_EXPORT const char * fromLocation(float latitude, float longitude)
Returns the timezone at the given geo coordinate.
Definition ktimezone.cpp:16
KI18NLOCALEDATA_EXPORT KCountry country(const char *)
Returns the country a timezone is in.
Definition ktimezone.cpp:22
QJSValue toScriptValue(const T &value)
Q_OBJECTQ_OBJECT
QString fromUtf8(QByteArrayView str)
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:05:34 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.