| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Virtual Keyboard module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 or (at your option) any later version |
| 20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
| 21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
| 22 | ** included in the packaging of this file. Please review the following |
| 23 | ** information to ensure the GNU General Public License requirements will |
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 25 | ** |
| 26 | ** $QT_END_LICENSE$ |
| 27 | ** |
| 28 | ****************************************************************************/ |
| 29 | |
| 30 | #include "plugin.h" |
| 31 | #include "extensionloader.h" |
| 32 | #include <QtVirtualKeyboard/private/platforminputcontext_p.h> |
| 33 | #include <QtVirtualKeyboard/private/plaininputmethod_p.h> |
| 34 | #include <QtVirtualKeyboard/private/qvirtualkeyboard_staticplugin_p.h> |
| 35 | #include <QLoggingCategory> |
| 36 | #include <QtQml> |
| 37 | #if defined(Q_OS_WIN) |
| 38 | #include <qt_windows.h> |
| 39 | #endif |
| 40 | |
| 41 | QT_BEGIN_NAMESPACE |
| 42 | |
| 43 | using namespace QtVirtualKeyboard; |
| 44 | |
| 45 | Q_LOGGING_CATEGORY(qlcVirtualKeyboard, "qt.virtualkeyboard" ) |
| 46 | |
| 47 | static const char pluginsUri[] = "QtQuick.VirtualKeyboard.Plugins" ; |
| 48 | static const char pluginName[] = "qtvirtualkeyboard" ; |
| 49 | static const char inputMethodEnvVarName[] = "QT_IM_MODULE" ; |
| 50 | |
| 51 | QStringList QVirtualKeyboardPlugin::keys() const |
| 52 | { |
| 53 | return QStringList(QLatin1String(pluginName)); |
| 54 | } |
| 55 | |
| 56 | QPlatformInputContext *QVirtualKeyboardPlugin::create(const QString &system, const QStringList ¶mList) |
| 57 | { |
| 58 | Q_UNUSED(paramList); |
| 59 | |
| 60 | #if defined(QT_STATICPLUGIN) |
| 61 | #if !defined(QT_VIRTUALKEYBOARD_DISABLE_LAYOUTS) |
| 62 | Q_INIT_RESOURCE(qmake_virtualkeyboard_layouts); |
| 63 | #endif |
| 64 | Q_INIT_RESOURCE(virtualkeyboard_content); |
| 65 | #if !defined(QT_VIRTUALKEYBOARD_NO_BUILTIN_STYLES) |
| 66 | Q_INIT_RESOURCE(virtualkeyboard_default_style); |
| 67 | Q_INIT_RESOURCE(virtualkeyboard_retro_style); |
| 68 | #endif |
| 69 | Q_VKB_IMPORT_PLUGIN(QtQuickVirtualKeyboardPlugin) |
| 70 | Q_VKB_IMPORT_PLUGIN(QtQuickVirtualKeyboardSettingsPlugin) |
| 71 | Q_VKB_IMPORT_PLUGIN(QtQuickVirtualKeyboardStylesPlugin) |
| 72 | #endif |
| 73 | |
| 74 | if (!qEnvironmentVariableIsSet(varName: inputMethodEnvVarName) || qgetenv(varName: inputMethodEnvVarName) != pluginName) |
| 75 | return Q_NULLPTR; |
| 76 | |
| 77 | if (system.compare(s1: system, s2: QLatin1String(pluginName), cs: Qt::CaseInsensitive) != 0) |
| 78 | return Q_NULLPTR; |
| 79 | |
| 80 | #if defined(Q_OS_WIN) |
| 81 | // QTBUG-93042 |
| 82 | ImmDisableIME(0); |
| 83 | #endif |
| 84 | |
| 85 | PlatformInputContext *platformInputContext = new PlatformInputContext(); |
| 86 | |
| 87 | QStringList inputMethodList; |
| 88 | inputMethodList.append(t: QLatin1String("PlainInputMethod" )); |
| 89 | qRegisterMetaType<PlainInputMethod *>(typeName: "PlainInputMethod*" ); |
| 90 | qmlRegisterType<PlainInputMethod>(uri: pluginsUri, versionMajor: 1, versionMinor: 0, qmlName: "PlainInputMethod" ); |
| 91 | qmlRegisterType<PlainInputMethod>(uri: pluginsUri, versionMajor: 2, versionMinor: 0, qmlName: "PlainInputMethod" ); |
| 92 | qmlRegisterType<PlainInputMethod>(uri: pluginsUri, versionMajor: 2, versionMinor: 3, qmlName: "PlainInputMethod" ); |
| 93 | |
| 94 | QMultiHash<QString, QJsonObject> extensions = ExtensionLoader::plugins(); |
| 95 | for (const QString &extensionName : extensions.uniqueKeys()) { |
| 96 | QJsonObject metaData = ExtensionLoader::loadMeta(extensionName); |
| 97 | if (metaData.isEmpty()) { |
| 98 | qCWarning(qlcVirtualKeyboard) << "Error loading extension - metadata not found!" ; |
| 99 | continue; |
| 100 | } |
| 101 | const QString inputMethod = metaData.value(key: QLatin1String("InputMethod" )).toString(); |
| 102 | if (!inputMethod.isEmpty() && inputMethodList.contains(str: inputMethod)) { |
| 103 | qCWarning(qlcVirtualKeyboard) << "Ignored extension" << extensionName << |
| 104 | "by" << metaData.value(key: QLatin1String("Provider" )).toString() << |
| 105 | "-" << inputMethod << "is already registered!" ; |
| 106 | continue; |
| 107 | } |
| 108 | qCDebug(qlcVirtualKeyboard) << "Loading extension" << extensionName; |
| 109 | QVirtualKeyboardExtensionPlugin *extensionPlugin = ExtensionLoader::loadPlugin(metaData); |
| 110 | if (extensionPlugin && !inputMethod.isEmpty()) { |
| 111 | extensionPlugin->registerTypes(uri: pluginsUri); |
| 112 | inputMethodList.append(t: inputMethod); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Auto-increment the import to stay in sync with ALL future QtQuick minor versions |
| 117 | qmlRegisterModule(uri: pluginsUri, versionMajor: 2, QT_VERSION_MINOR); |
| 118 | |
| 119 | platformInputContext->setInputMethods(inputMethodList); |
| 120 | |
| 121 | return platformInputContext; |
| 122 | } |
| 123 | |
| 124 | QT_END_NAMESPACE |
| 125 | |