1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 02:36:34 +01:00

PyFalkon: Remove PythonPluginObject

Instead just export global registerPlugin function.
This commit is contained in:
David Rosca 2018-02-25 19:52:24 +01:00
parent b9cedf45a1
commit 1cf47bfdb5
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
7 changed files with 44 additions and 115 deletions

View File

@ -40,11 +40,13 @@ pyside2_config(--pyside2-shared-libraries-cmake PYSIDE2_SHARED_LIBRARIES 1)
get_property(QT_CORE_INCLUDE_DIRS TARGET Qt5::Core PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
get_property(QT_GUI_INCLUDE_DIRS TARGET Qt5::Gui PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
get_property(QT_WIDGETS_INCLUDE_DIRS TARGET Qt5::Widgets PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
get_property(QT_WEBENGINECORE_INCLUDE_DIRS TARGET Qt5::WebEngineCore PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
get_property(QT_WEBENGINEWIDGETS_INCLUDE_DIRS TARGET Qt5::WebEngineWidgets PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
set(QT_INCLUDE_DIRS
${QT_CORE_INCLUDE_DIRS}
${QT_GUI_INCLUDE_DIRS}
${QT_WIDGETS_INCLUDE_DIRS}
${QT_WEBENGINECORE_INCLUDE_DIRS}
${QT_WEBENGINEWIDGETS_INCLUDE_DIRS}
)
set(INCLUDES "")
@ -93,7 +95,6 @@ set(GENERATED_SOURCES
${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/pyfalkon_module_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/desktopfile_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/plugininterface_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/PyFalkon/pythonpluginobject_wrapper.cpp
)
set(GENERATED_SOURCES_DEPENDENCIES
${WRAPPED_HEADER}
@ -121,7 +122,6 @@ endforeach()
set( PyFalkon_SRCS
pythonplugin.cpp
pythonpluginobject.cpp
${GENERATED_SOURCES}
)

View File

@ -2,17 +2,20 @@
<typesystem package="PyFalkon">
<load-typesystem name="typesystem_webenginewidgets.xml" generate="no"/>
<inject-code class="native" position="beginning">
#include &lt;pythonplugin.h&gt;
</inject-code>
<value-type name="DesktopFile"/>
<object-type name="PluginInterface">
<enum-type name="InitState"/>
</object-type>
<object-type name="PythonPluginObject">
<modify-function signature="registerPlugin(PluginInterface*)">
<modify-argument index="1">
<define-ownership owner="c++"/>
</modify-argument>
</modify-function>
</object-type>
<add-function signature="registerPlugin(PluginInterface*)">
<inject-code class="target" position="beginning">
Py_INCREF(%PYARG_1);
pyfalkon_register_plugin(%1);
</inject-code>
</add-function>
</typesystem>

View File

@ -16,7 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "pythonplugin.h"
#include "pythonpluginobject.h"
#include "plugins.h"
#include "datapaths.h"
@ -38,7 +37,7 @@ enum State
State state = PythonUninitialized;
PythonPluginObject *pluginObject = nullptr;
PluginInterface *pluginInterface = nullptr;
QHash<PyObject*, PluginInterface*> pluginInstances;
static QStringList script_paths()
@ -55,7 +54,6 @@ static void cleanup()
if (state > PythonUninitialized) {
Py_Finalize();
state = PythonUninitialized;
delete pluginObject;
}
}
@ -69,34 +67,6 @@ static void set_path(const QStringList &scriptPaths)
qputenv("PYTHONPATH", paths.join(QL1C(':')).toLocal8Bit());
}
static void register_plugin_object()
{
pluginObject = new PythonPluginObject();
PyTypeObject *typeObject = Shiboken::SbkType<PythonPluginObject>();
PyObject *po = Shiboken::Conversions::pointerToPython(reinterpret_cast<const SbkObjectType *>(typeObject), pluginObject);
if (!po) {
qWarning() << "Failed to create wrapper for" << pluginObject;
return;
}
Py_INCREF(po);
PyObject *module = PyImport_AddModule("Falkon");
if (!module) {
Py_DECREF(po);
PyErr_Print();
qWarning() << "Failed to locate Falkon module!";
return;
}
if (PyModule_AddObject(module, "plugin", po) < 0) {
Py_DECREF(po);
PyErr_Print();
qWarning() << "Failed to add plugin object to Falkon module!";
return;
}
}
static State init()
{
if (state > PythonUninitialized) {
@ -121,11 +91,14 @@ static State init()
return state = PythonError;
}
register_plugin_object();
return state;
}
void pyfalkon_register_plugin(PluginInterface *plugin)
{
pluginInterface = plugin;
}
Plugins::Plugin pyfalkon_load_plugin(const QString &name)
{
QString fullPath;
@ -160,7 +133,7 @@ void pyfalkon_init_plugin(Plugins::Plugin *plugin)
const QString moduleName = plugin->pluginId.mid(7);
pluginObject->ptr = nullptr;
pluginInterface = nullptr;
module = PyImport_ImportModule(qPrintable(moduleName));
if (!module) {
@ -168,13 +141,13 @@ void pyfalkon_init_plugin(Plugins::Plugin *plugin)
qWarning() << "Failed to import module" << moduleName;
return;
}
if (!pluginObject->ptr) {
qWarning() << "No plugin registered! Falkon.plugin.registerPlugin() must be called from script.";
if (!pluginInterface) {
qWarning() << "No plugin registered! Falkon.registerPlugin() must be called from script.";
return;
}
pluginInstances[module] = pluginObject->ptr;
plugin->instance = pluginObject->ptr;
pluginInstances[module] = pluginInterface;
plugin->instance = pluginInterface;
plugin->data = QVariant::fromValue(static_cast<void*>(module));
}

View File

@ -19,6 +19,8 @@
#include "plugins.h"
void pyfalkon_register_plugin(PluginInterface *plugin);
extern "C" Q_DECL_EXPORT Plugins::Plugin pyfalkon_load_plugin(const QString &name);
extern "C" Q_DECL_EXPORT void pyfalkon_init_plugin(Plugins::Plugin *plugin);
extern "C" Q_DECL_EXPORT QVector<Plugins::Plugin> pyfalkon_load_available_plugins();

View File

@ -1,30 +0,0 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2018 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "pythonpluginobject.h"
PluginInterface *PythonPluginObject::ptr = nullptr;
PythonPluginObject::PythonPluginObject(QObject *parent)
: QObject(parent)
{
}
void PythonPluginObject::registerPlugin(PluginInterface *plugin)
{
ptr = plugin;
}

View File

@ -1,34 +0,0 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2018 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#pragma once
#include <QObject>
class PluginInterface;
class PythonPluginObject : public QObject
{
Q_OBJECT
public:
explicit PythonPluginObject(QObject *parent = nullptr);
void registerPlugin(PluginInterface *plugin);
static PluginInterface *ptr;
};

View File

@ -1,6 +1,21 @@
// Falkon
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2018 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "webview.h"
#include "desktopfile.h"
#include "plugininterface.h"
// PyFalkon
#include "pythonpluginobject.h"