From 3844e88488b66c2699d59a3fe0623b5bb58dbb79 Mon Sep 17 00:00:00 2001 From: Volodymyr Kozieiev Date: Tue, 26 Dec 2017 17:42:22 +0200 Subject: [PATCH] Added desktop implementation --- desktop/CMakeLists.txt | 7 ++++ desktop/rni18ndesktop.cpp | 81 +++++++++++++++++++++++++++++++++++++++ desktop/rni18ndesktop.h | 43 +++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 desktop/CMakeLists.txt create mode 100644 desktop/rni18ndesktop.cpp create mode 100644 desktop/rni18ndesktop.h diff --git a/desktop/CMakeLists.txt b/desktop/CMakeLists.txt new file mode 100644 index 0000000..f39e53e --- /dev/null +++ b/desktop/CMakeLists.txt @@ -0,0 +1,7 @@ +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_TYPE_NAMES ${REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_TYPE_NAMES} + \"RNI18n\" PARENT_SCOPE) + +set(REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_SRC ${REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_SRC} + ${CMAKE_CURRENT_SOURCE_DIR}/rni18ndesktop.cpp PARENT_SCOPE) diff --git a/desktop/rni18ndesktop.cpp b/desktop/rni18ndesktop.cpp new file mode 100644 index 0000000..adf9f3c --- /dev/null +++ b/desktop/rni18ndesktop.cpp @@ -0,0 +1,81 @@ +/** + * Copyright (C) 2016, Canonical Ltd. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * Author: Justin McPherson + * + */ + +#include + +#include "bridge.h" +#include "rni18ndesktop.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { +struct RegisterQMLMetaType { + RegisterQMLMetaType() { + qRegisterMetaType(); + } +} registerMetaType; +} // namespace + +class RNI18nPrivate { + +public: + RNI18nPrivate() {} + + Bridge* bridge = nullptr; + + QVariantList languages() { + QStringList languages = QLocale().uiLanguages(); + QVariantList variantLanguages; + for (QString l : languages) { + variantLanguages.push_back(l); + } + return variantLanguages; + } +}; + +RNI18n::RNI18n(QObject* parent) : QObject(parent), d_ptr(new RNI18nPrivate) {} + +RNI18n::~RNI18n() {} + +void RNI18n::getLanguages(double successCallback, double errorCallback) { + Q_D(RNI18n); + QVariantList args; + args.push_back(d->languages()); + + if (d->bridge) { + d->bridge->invokePromiseCallback(successCallback, args); + } +} + +QString RNI18n::moduleName() { + return "RNI18n"; +} + +QList RNI18n::methodsToExport() { + return QList{}; +} + +QVariantMap RNI18n::constantsToExport() { + return QVariantMap{{"languages", d_ptr->languages()}}; +} + +void RNI18n::setBridge(Bridge* bridge) { + Q_D(RNI18n); + d->bridge = bridge; +} diff --git a/desktop/rni18ndesktop.h b/desktop/rni18ndesktop.h new file mode 100644 index 0000000..5ca228f --- /dev/null +++ b/desktop/rni18ndesktop.h @@ -0,0 +1,43 @@ + +/** + * Copyright (C) 2016, Canonical Ltd. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * Author: Justin McPherson + * + */ + +#ifndef RNI18NDESKTOP_H +#define RNI18NDESKTOP_H + +#include + +#include "moduleinterface.h" + +class RNI18nPrivate; +class RNI18n : public QObject, public ModuleInterface { + Q_OBJECT + + Q_INTERFACES(ModuleInterface) + + Q_DECLARE_PRIVATE(RNI18n) + +public: + Q_INVOKABLE RNI18n(QObject* parent = 0); + ~RNI18n(); + + Q_INVOKABLE REACT_PROMISE void getLanguages(double successCallback, double errorCallback); + virtual QString moduleName() override; + virtual QList methodsToExport() override; + virtual QVariantMap constantsToExport() override; + virtual void setBridge(Bridge* bridge) override; + +private: + QScopedPointer d_ptr; +}; + +#endif // RNI18NDESKTOP_H