1/****************************************************************************
2**
3** Copyright (C) 2018 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 "extensionloader.h"
31#include <QtVirtualKeyboard/QVirtualKeyboardExtensionPlugin>
32#include <QtCore/private/qfactoryloader_p.h>
33
34QT_BEGIN_NAMESPACE
35namespace QtVirtualKeyboard {
36
37QMutex ExtensionLoader::m_mutex;
38QMultiHash<QString, QJsonObject> ExtensionLoader::m_plugins;
39bool ExtensionLoader::m_alreadyDiscovered = false;
40
41Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
42 (QVirtualKeyboardExtensionPluginFactoryInterface_iid,
43 QLatin1String("/virtualkeyboard")))
44
45QMultiHash<QString, QJsonObject> ExtensionLoader::plugins(bool reload)
46{
47 QMutexLocker lock(&m_mutex);
48
49 if (reload == true)
50 m_alreadyDiscovered = false;
51
52 if (!m_alreadyDiscovered) {
53 loadPluginMetadata();
54 m_alreadyDiscovered = true;
55 }
56 return m_plugins;
57}
58
59QJsonObject ExtensionLoader::loadMeta(const QString &extensionName)
60{
61 QJsonObject metaData;
62 metaData = QJsonObject();
63 metaData.insert(key: QLatin1String("index"), value: -1);
64
65 QList<QJsonObject> candidates = ExtensionLoader::plugins().values(akey: extensionName);
66
67 int versionFound = -1;
68 int idx = -1;
69
70 // figure out which version of the plugin we want
71 for (int i = 0; i < candidates.size(); ++i) {
72 QJsonObject meta = candidates[i];
73 if (meta.contains(key: QLatin1String("Version"))
74 && meta.value(key: QLatin1String("Version")).isDouble()) {
75 int ver = int(meta.value(key: QLatin1String("Version")).toDouble());
76 if (ver > versionFound) {
77 versionFound = ver;
78 idx = i;
79 }
80 }
81 }
82
83 if (idx != -1) {
84 metaData = candidates[idx];
85 return metaData;
86 }
87 return QJsonObject();
88}
89
90QVirtualKeyboardExtensionPlugin *ExtensionLoader::loadPlugin(QJsonObject metaData)
91{
92 if (int(metaData.value(key: QLatin1String("index")).toDouble()) < 0) {
93 return NULL;
94 }
95 int idx = int(metaData.value(key: QLatin1String("index")).toDouble());
96 return qobject_cast<QVirtualKeyboardExtensionPlugin *>(object: loader()->instance(index: idx));
97}
98
99void ExtensionLoader::loadPluginMetadata()
100{
101 QFactoryLoader *l = loader();
102 QList<QJsonObject> meta = l->metaData();
103 for (int i = 0; i < meta.size(); ++i) {
104 QJsonObject obj = meta.at(i).value(key: QLatin1String("MetaData")).toObject();
105 QString name = obj.value(key: QLatin1String("Name")).toString();
106 if (!name.isEmpty()) {
107 obj.insert(key: QLatin1String("index"), value: i);
108 m_plugins.insert(akey: name, avalue: obj);
109 }
110 }
111}
112
113} // namespace QtVirtualKeyboard
114QT_END_NAMESPACE
115

source code of qtvirtualkeyboard/src/plugin/extensionloader.cpp