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 QmlPlugin()27QmlPlugin::QmlPlugin() 28 { 29 } 30 loadPlugin(const QString & name)31Plugins::Plugin QmlPlugin::loadPlugin(const QString &name) 32 { 33 static bool qmlSupportLoaded = false; 34 if (!qmlSupportLoaded) { 35 QmlPlugins::registerQmlTypes(); 36 qmlSupportLoaded = true; 37 } 38 39 QString fullPath; 40 if (QFileInfo(name).isAbsolute()) { 41 fullPath = name; 42 } else { 43 fullPath = DataPaths::locate(DataPaths::Plugins, name); 44 if (fullPath.isEmpty()) { 45 qWarning() << "QML plugin" << name << "not found"; 46 return Plugins::Plugin(); 47 } 48 } 49 50 Plugins::Plugin plugin; 51 plugin.type = Plugins::Plugin::QmlPlugin; 52 plugin.pluginId = QSL("qml:%1").arg(QFileInfo(name).fileName()); 53 plugin.pluginPath = fullPath; 54 DesktopFile desktopFile(fullPath + QSL("/metadata.desktop")); 55 plugin.pluginSpec = Plugins::createSpec(desktopFile); 56 plugin.data = QVariant::fromValue(new QmlPluginLoader(plugin.pluginSpec.name, fullPath)); 57 return plugin; 58 } 59 initPlugin(Plugins::Plugin * plugin)60void QmlPlugin::initPlugin(Plugins::Plugin *plugin) 61 { 62 Q_ASSERT(plugin->type == Plugins::Plugin::QmlPlugin); 63 64 const QString name = plugin->pluginSpec.name; 65 66 auto qmlPluginLoader = plugin->data.value<QmlPluginLoader*>(); 67 if (!qmlPluginLoader) { 68 qWarning() << "Failed to cast from data"; 69 return; 70 } 71 qmlPluginLoader->createComponent(); 72 if (!qmlPluginLoader->instance()) { 73 qWarning().noquote() << "Failed to create component for" << name << "plugin:" << qmlPluginLoader->component()->errorString(); 74 return; 75 } 76 77 plugin->instance = qobject_cast<PluginInterface*>(qmlPluginLoader->instance()); 78 } 79