Falkon Develop
Cross-platform Qt-based web browser
qmlplugin.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 "qmlplugin.h"
19#include "qmlplugins.h"
20#include "qmlpluginloader.h"
21#include "datapaths.h"
22#include "desktopfile.h"
23
24#include <QFileInfo>
25#include <QDir>
26
28= default;
29
31{
32 static bool qmlSupportLoaded = false;
33 if (!qmlSupportLoaded) {
35 qmlSupportLoaded = true;
36 }
37
38 QString fullPath;
39 if (QFileInfo(name).isAbsolute()) {
40 fullPath = name;
41 } else {
42 fullPath = DataPaths::locate(DataPaths::Plugins, name);
43 if (fullPath.isEmpty()) {
44 qWarning() << "QML plugin" << name << "not found";
45 return {};
46 }
47 }
48
49 Plugins::Plugin plugin;
51 plugin.pluginId = QSL("qml:%1").arg(QFileInfo(name).fileName());
52 plugin.pluginPath = fullPath;
53 DesktopFile desktopFile(fullPath + QSL("/metadata.desktop"));
54 plugin.pluginSpec = Plugins::createSpec(desktopFile);
55 plugin.data = QVariant::fromValue(new QmlPluginLoader(plugin.pluginSpec.name, fullPath));
56 return plugin;
57}
58
60{
61 Q_ASSERT(plugin->type == Plugins::Plugin::QmlPlugin);
62
63 const QString name = plugin->pluginSpec.name;
64
65 auto qmlPluginLoader = plugin->data.value<QmlPluginLoader*>();
66 if (!qmlPluginLoader) {
67 qWarning() << "Failed to cast from data";
68 return;
69 }
70 qmlPluginLoader->createComponent();
71 if (!qmlPluginLoader->instance()) {
72 qWarning().noquote() << "Failed to create component for" << name << "plugin:" << qmlPluginLoader->component()->errorString();
73 return;
74 }
75
76 plugin->instance = qobject_cast<PluginInterface*>(qmlPluginLoader->instance());
77}
static QString locate(Path type, const QString &file)
Definition: datapaths.cpp:82
static PluginSpec createSpec(const QJsonObject &metaData)
Definition: plugins.cpp:172
static void initPlugin(Plugins::Plugin *plugin)
Definition: qmlplugin.cpp:59
static Plugins::Plugin loadPlugin(const QString &name)
Definition: qmlplugin.cpp:30
static void registerQmlTypes()
Definition: qmlplugins.cpp:57
#define QSL(x)
Definition: qzcommon.h:40
QString name
Definition: plugins.h:35
QVariant data
Definition: plugins.h:75
QString pluginId
Definition: plugins.h:63
PluginSpec pluginSpec
Definition: plugins.h:65
QString pluginPath
Definition: plugins.h:64
PluginInterface * instance
Definition: plugins.h:66