Falkon Develop
Cross-platform Qt-based web browser
qmlpluginloader.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "qmlpluginloader.h"
19#include "qmlengine.h"
20#include <QQmlContext>
21#include <QDir>
22#include "../config.h"
23
24#if HAVE_LIBINTL
26#endif
27
28QmlPluginLoader::QmlPluginLoader(const QString &name, const QString &path)
29{
30 m_name = name;
31 m_path = path;
32 initEngineAndComponent();
33}
34
36{
37 m_interface = qobject_cast<QmlPluginInterface*>(m_component->create(m_component->creationContext()));
38
39 if (!m_interface) {
40 return;
41 }
42
43 m_interface->setEngine(m_engine);
44 m_interface->setName(m_name);
45 connect(m_interface, &QmlPluginInterface::qmlPluginUnloaded, this, [this] {
46 delete m_component;
47 delete m_engine;
48 initEngineAndComponent();
49 });
50}
51
52QQmlComponent *QmlPluginLoader::component() const
53{
54 return m_component;
55}
56
58{
59 return m_interface;
60}
61
62void QmlPluginLoader::initEngineAndComponent()
63{
64 m_engine = new QmlEngine();
65 m_component = new QQmlComponent(m_engine, QDir(m_path).filePath(QStringLiteral("main.qml")));
66 m_engine->setExtensionPath(m_path);
67 m_engine->setExtensionName(m_name);
68#if HAVE_LIBINTL
69 auto i18n = new QmlI18n(m_name);
70 m_engine->globalObject().setProperty(QSL("__falkon_i18n"), m_engine->newQObject(i18n));
71 m_engine->evaluate(QSL("i18n = function (s) { return __falkon_i18n.i18n(s) };"));
72 m_engine->evaluate(QSL("i18np = function (s1, s2) { return __falkon_i18n.i18np(s1, s2) }"));
73#else
74 m_engine->evaluate(QSL("i18n = function (s) { return s; };"));
75 m_engine->evaluate(QSL("i18np = function (s1, s2) { return s1; }"));
76#endif
77}
void setExtensionPath(const QString &path)
Definition: qmlengine.cpp:40
void setExtensionName(const QString &name)
Definition: qmlengine.cpp:30
The class exposing GNU Gettext to QML.
Definition: qmli18n.h:30
void setName(const QString &name)
void setEngine(QQmlEngine *engine)
QmlPluginLoader(const QString &name, const QString &path)
QmlPluginInterface * instance() const
QQmlComponent * component() const
Definition: i18n.py:1
#define QSL(x)
Definition: qzcommon.h:40