diff --git a/src/lib/plugins/plugins.cpp b/src/lib/plugins/plugins.cpp index 6518ed258..c10cff0dd 100644 --- a/src/lib/plugins/plugins.cpp +++ b/src/lib/plugins/plugins.cpp @@ -35,6 +35,13 @@ Plugins::Plugins(QObject* parent) , m_speedDial(new SpeedDial(this)) { loadSettings(); + + m_pythonPlugin = new QLibrary(QSL("PyFalkonPrivate"), this); + if (!m_pythonPlugin->load()) { + qDebug() << "Failed to load python support plugin" << m_pythonPlugin->errorString(); + delete m_pythonPlugin; + m_pythonPlugin = nullptr; + } } QList Plugins::getAvailablePlugins() @@ -93,6 +100,27 @@ void Plugins::shutdown() } } +PluginSpec Plugins::createSpec(const DesktopFile &metaData) +{ + PluginSpec spec; + spec.name = metaData.name(mApp->currentLanguage()); + spec.description = metaData.comment(mApp->currentLanguage()); + spec.version = metaData.value(QSL("X-Falkon-Version")).toString(); + spec.author = QSL("%1 <%2>").arg(metaData.value(QSL("X-Falkon-Author")).toString(), metaData.value(QSL("X-Falkon-Email")).toString()); + spec.hasSettings = metaData.value(QSL("X-Falkon-Settings")).toBool(); + + const QString iconName = metaData.icon(); + if (!iconName.isEmpty()) { + if (QFileInfo::exists(iconName)) { + spec.icon = QIcon(iconName).pixmap(32); + } else { + spec.icon = QIcon::fromTheme(iconName).pixmap(32); + } + } + + return spec; +} + void Plugins::loadPlugins() { QDir settingsDir(DataPaths::currentProfilePath() + "/extensions/"); @@ -147,6 +175,19 @@ void Plugins::loadAvailablePlugins() registerAvailablePlugin(plugin); } } + + // PythonPlugin + if (m_pythonPlugin) { + auto f = (QVector(*)()) m_pythonPlugin->resolve("pyfalkon_load_available_plugins"); + if (!f) { + qWarning() << "Failed to resolve" << "pyfalkon_load_available_plugins"; + } else { + const auto plugins = f(); + for (const auto &plugin : plugins) { + registerAvailablePlugin(plugin); + } + } + } } void Plugins::registerAvailablePlugin(const Plugin &plugin) @@ -179,6 +220,8 @@ Plugins::Plugin Plugins::loadPlugin(const QString &id) type = Plugin::InternalPlugin; } else if (t == QL1S("lib")) { type = Plugin::SharedLibraryPlugin; + } else if (t == QL1S("python")) { + type = Plugin::PythonPlugin; } name = id.mid(colon + 1); } else { @@ -193,6 +236,9 @@ Plugins::Plugin Plugins::loadPlugin(const QString &id) case Plugin::SharedLibraryPlugin: return loadSharedLibraryPlugin(name); + case Plugin::PythonPlugin: + return loadPythonPlugin(name); + default: return Plugin(); } @@ -242,6 +288,22 @@ Plugins::Plugin Plugins::loadSharedLibraryPlugin(const QString &name) return plugin; } +Plugins::Plugin Plugins::loadPythonPlugin(const QString &name) +{ + if (!m_pythonPlugin) { + qWarning() << "Python support plugin is not loaded"; + return Plugin(); + } + + auto f = (Plugin(*)(const QString &)) m_pythonPlugin->resolve("pyfalkon_load_plugin"); + if (!f) { + qWarning() << "Failed to resolve" << "pyfalkon_load_plugin"; + return Plugin(); + } + + return f(name); +} + bool Plugins::initPlugin(PluginInterface::InitState state, Plugin *plugin) { if (!plugin) { @@ -257,6 +319,10 @@ bool Plugins::initPlugin(PluginInterface::InitState state, Plugin *plugin) initSharedLibraryPlugin(plugin); break; + case Plugin::PythonPlugin: + initPythonPlugin(plugin); + break; + default: return false; } @@ -291,23 +357,20 @@ void Plugins::initSharedLibraryPlugin(Plugin *plugin) plugin->instance = qobject_cast(plugin->pluginLoader->instance()); } -PluginSpec Plugins::createSpec(const DesktopFile &metaData) const +void Plugins::initPythonPlugin(Plugin *plugin) { - PluginSpec spec; - spec.name = metaData.name(mApp->currentLanguage()); - spec.description = metaData.comment(mApp->currentLanguage()); - spec.version = metaData.value(QSL("X-Falkon-Version")).toString(); - spec.author = QSL("%1 <%2>").arg(metaData.value(QSL("X-Falkon-Author")).toString(), metaData.value(QSL("X-Falkon-Email")).toString()); - spec.hasSettings = metaData.value(QSL("X-Falkon-Settings")).toBool(); + Q_ASSERT(plugin->type == Plugin::PythonPlugin); - const QString iconName = metaData.icon(); - if (!iconName.isEmpty()) { - if (QFileInfo::exists(iconName)) { - spec.icon = QIcon(iconName).pixmap(32); - } else { - spec.icon = QIcon::fromTheme(iconName).pixmap(32); - } + if (!m_pythonPlugin) { + qWarning() << "Python support plugin is not loaded"; + return; } - return spec; + auto f = (void(*)(Plugin *)) m_pythonPlugin->resolve("pyfalkon_init_plugin"); + if (!f) { + qWarning() << "Failed to resolve" << "pyfalkon_init_plugin"; + return; + } + + f(plugin); } diff --git a/src/lib/plugins/plugins.h b/src/lib/plugins/plugins.h index c07a46aad..02a58fba4 100644 --- a/src/lib/plugins/plugins.h +++ b/src/lib/plugins/plugins.h @@ -25,6 +25,7 @@ #include "qzcommon.h" #include "plugininterface.h" +class QLibrary; class QPluginLoader; class SpeedDial; @@ -53,7 +54,8 @@ public: enum Type { Invalid = 0, InternalPlugin, - SharedLibraryPlugin + SharedLibraryPlugin, + PythonPlugin }; Type type = Invalid; QString pluginId; @@ -67,6 +69,9 @@ public: QString libraryPath; QPluginLoader *pluginLoader = nullptr; + // Other + QVariant data; + bool isLoaded() const { return instance; } @@ -89,6 +94,8 @@ public: // SpeedDial SpeedDial* speedDial() { return m_speedDial; } + static PluginSpec createSpec(const DesktopFile &metaData); + public Q_SLOTS: void loadSettings(); @@ -101,14 +108,14 @@ Q_SIGNALS: void pluginUnloaded(PluginInterface* plugin); private: - PluginSpec createSpec(const DesktopFile &metaData) const; - Plugin loadPlugin(const QString &id); Plugin loadInternalPlugin(const QString &name); Plugin loadSharedLibraryPlugin(const QString &name); + Plugin loadPythonPlugin(const QString &name); bool initPlugin(PluginInterface::InitState state, Plugin *plugin); void initInternalPlugin(Plugin *plugin); void initSharedLibraryPlugin(Plugin *plugin); + void initPythonPlugin(Plugin *plugin); void registerAvailablePlugin(const Plugin &plugin); @@ -122,6 +129,8 @@ private: SpeedDial* m_speedDial; QList m_internalPlugins; + + QLibrary *m_pythonPlugin = nullptr; }; Q_DECLARE_METATYPE(Plugins::Plugin) diff --git a/src/plugins/CMakeLists.txt b/src/plugins/CMakeLists.txt index ba368d369..d20e3419d 100644 --- a/src/plugins/CMakeLists.txt +++ b/src/plugins/CMakeLists.txt @@ -22,3 +22,5 @@ endif() if (CMAKE_BUILD_TYPE STREQUAL "Debug") add_subdirectory(TestPlugin) endif() + +#add_subdirectory(PyFalkon) diff --git a/src/plugins/PyFalkon/CMakeLists.txt b/src/plugins/PyFalkon/CMakeLists.txt new file mode 100644 index 000000000..fd79f03ca --- /dev/null +++ b/src/plugins/PyFalkon/CMakeLists.txt @@ -0,0 +1,153 @@ +# Enable policy to run automoc on generated files. +if(POLICY CMP0071) + cmake_policy(SET CMP0071 NEW) +endif() + +# Macro to get various pyside / python include / link flags. +macro(pyside2_config option output_var) + if(${ARGC} GREATER 2) + set(is_list ${ARGV2}) + else() + set(is_list "") + endif() + + execute_process( + COMMAND python "${CMAKE_CURRENT_SOURCE_DIR}/pyside2_config.py" ${option} + OUTPUT_VARIABLE ${output_var} + OUTPUT_STRIP_TRAILING_WHITESPACE) + + if ("${${output_var}}" STREQUAL "") + message(FATAL_ERROR "Got empty string when running: pyside2_config.py ${option}") + endif() + if(is_list) + string (REPLACE " " ";" ${output_var} "${${output_var}}") + endif() +endmacro() + +set(SHIBOKEN_PATH "/usr/bin/shiboken2") +set(TYPESYSTEMS_PATH "/usr/share/PySide2/typesystems") + +if(NOT EXISTS ${SHIBOKEN_PATH}) + message(FATAL_ERROR "Shiboken executable not found at path: ${SHIBOKEN_PATH}") +endif() + +pyside2_config(--python-include PYTHON_INCLUDE_DIR) +pyside2_config(--pyside2-include PYSIDE2_INCLUDE_DIR 1) +pyside2_config(--python-link-cmake PYTHON_LINKING_DATA 1) +pyside2_config(--pyside2-shared-libraries-cmake PYSIDE2_SHARED_LIBRARIES 1) + +# Get all relevant Qt include dirs, to pass them on to shiboken. +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_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_WEBENGINEWIDGETS_INCLUDE_DIRS} +) +set(INCLUDES "") +foreach(INCLUDE_DIR ${QT_INCLUDE_DIRS}) + list(APPEND INCLUDES "-I${INCLUDE_DIR}") +endforeach() + +# Set up the options to pass to shiboken. +set(WRAPPED_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/wrappedclasses.h) +set(TYPESYSTEM_FILE ${CMAKE_CURRENT_SOURCE_DIR}/pyfalkon.xml) + +set(SHIBOKEN_OPTIONS --generator-set=shiboken --enable-parent-ctor-heuristic + --enable-pyside-extensions --enable-return-value-heuristic --use-isnull-as-nb_nonzero + --avoid-protected-hack + ${INCLUDES} + -I${CMAKE_SOURCE_DIR} + -I${CMAKE_SOURCE_DIR}/src/lib/3rdparty + -I${CMAKE_SOURCE_DIR}/src/lib/adblock + -I${CMAKE_SOURCE_DIR}/src/lib/app + -I${CMAKE_SOURCE_DIR}/src/lib/autofill + -I${CMAKE_SOURCE_DIR}/src/lib/bookmarks + -I${CMAKE_SOURCE_DIR}/src/lib/cookies + -I${CMAKE_SOURCE_DIR}/src/lib/downloads + -I${CMAKE_SOURCE_DIR}/src/lib/history + -I${CMAKE_SOURCE_DIR}/src/lib/navigation + -I${CMAKE_SOURCE_DIR}/src/lib/network + -I${CMAKE_SOURCE_DIR}/src/lib/notifications + -I${CMAKE_SOURCE_DIR}/src/lib/opensearch + -I${CMAKE_SOURCE_DIR}/src/lib/other + -I${CMAKE_SOURCE_DIR}/src/lib/plugins + -I${CMAKE_SOURCE_DIR}/src/lib/popupwindow + -I${CMAKE_SOURCE_DIR}/src/lib/preferences + -I${CMAKE_SOURCE_DIR}/src/lib/session + -I${CMAKE_SOURCE_DIR}/src/lib/sidebar + -I${CMAKE_SOURCE_DIR}/src/lib/tabwidget + -I${CMAKE_SOURCE_DIR}/src/lib/tools + -I${CMAKE_SOURCE_DIR}/src/lib/webengine + -I${CMAKE_SOURCE_DIR}/src/lib/webtab + -T${CMAKE_SOURCE_DIR} + -T${TYPESYSTEMS_PATH} + --output-directory=${CMAKE_CURRENT_BINARY_DIR} + ) + +# Specify which sources will be generated by shiboken, and their dependencies. +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} + ${TYPESYSTEM_FILE} +) + +# Add custom target to run shiboken. +add_custom_command(OUTPUT ${GENERATED_SOURCES} + COMMAND ${SHIBOKEN_PATH} + ${SHIBOKEN_OPTIONS} ${WRAPPED_HEADER} ${TYPESYSTEM_FILE} + DEPENDS ${GENERATED_SOURCES_DEPENDENCIES} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMENT "Running generator for ${TYPESYSTEM_FILE}.") + +# We need to include the headers for the module bindings that we use. +set(PYSIDE2_ADDITIONAL_INCLUDES "") +foreach(INCLUDE_DIR ${PYSIDE2_INCLUDE_DIR}) + list(APPEND PYSIDE2_ADDITIONAL_INCLUDES "${INCLUDE_DIR}/QtCore") + list(APPEND PYSIDE2_ADDITIONAL_INCLUDES "${INCLUDE_DIR}/QtGui") + list(APPEND PYSIDE2_ADDITIONAL_INCLUDES "${INCLUDE_DIR}/QtNetwork") + list(APPEND PYSIDE2_ADDITIONAL_INCLUDES "${INCLUDE_DIR}/QtWidgets") + list(APPEND PYSIDE2_ADDITIONAL_INCLUDES "${INCLUDE_DIR}/QtWebChannel") + list(APPEND PYSIDE2_ADDITIONAL_INCLUDES "${INCLUDE_DIR}/QtWebEngineWidgets") +endforeach() + +set( PyFalkon_SRCS + pythonplugin.cpp + pythonpluginobject.cpp + ${GENERATED_SOURCES} +) + +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + +add_library(PyFalkonPrivate SHARED ${PyFalkon_SRCS}) +set_target_properties(PyFalkonPrivate PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION "2") +install(TARGETS PyFalkonPrivate ${KDE_INSTALL_TARGETS_DEFAULT_ARGS} LIBRARY NAMELINK_SKIP) + +target_include_directories(PyFalkonPrivate PRIVATE ${PYTHON_INCLUDE_DIR}) +target_include_directories(PyFalkonPrivate PRIVATE ${PYSIDE2_INCLUDE_DIR}) +target_include_directories(PyFalkonPrivate PRIVATE ${PYSIDE2_ADDITIONAL_INCLUDES}) +target_include_directories(PyFalkonPrivate PRIVATE ${CMAKE_SOURCE_DIR}) +target_include_directories(PyFalkonPrivate PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/PyFalkon) + +target_link_libraries(PyFalkonPrivate PRIVATE FalkonPrivate) +target_link_libraries(PyFalkonPrivate PRIVATE Qt5::Widgets) +target_link_libraries(PyFalkonPrivate PRIVATE ${PYSIDE2_SHARED_LIBRARIES}) + +# Find and link to the python library. +list(GET PYTHON_LINKING_DATA 0 PYTHON_LIBDIR) +list(GET PYTHON_LINKING_DATA 1 PYTHON_LIB) +find_library(PYTHON_LINK_FLAGS ${PYTHON_LIB} HINTS ${PYTHON_LIBDIR}) +target_link_libraries(PyFalkonPrivate PRIVATE ${PYTHON_LINK_FLAGS}) + +# Same as CONFIG += no_keywords to avoid syntax errors in object.h due to the usage of the word Slot +target_compile_definitions(PyFalkonPrivate PRIVATE QT_NO_KEYWORDS) + diff --git a/src/plugins/PyFalkon/pyfalkon.xml b/src/plugins/PyFalkon/pyfalkon.xml new file mode 100644 index 000000000..8341af957 --- /dev/null +++ b/src/plugins/PyFalkon/pyfalkon.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/src/plugins/PyFalkon/pyside2_config.py b/src/plugins/PyFalkon/pyside2_config.py new file mode 100644 index 000000000..1453d8056 --- /dev/null +++ b/src/plugins/PyFalkon/pyside2_config.py @@ -0,0 +1,289 @@ +############################################################################# +## +## Copyright (C) 2017 The Qt Company Ltd. +## Contact: http://www.qt.io/licensing/ +## +## This file is part of the PySide examples of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## You may use this file under the terms of the BSD license as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################# + +import os, glob, re, sys, imp +from distutils import sysconfig +if sys.platform == 'win32': + import winreg + +usage = """ +Utility to determine include/link options of PySide2 and Python for qmake + +Usage: pyside2_config.py [option] +Options: + --python-include Print Python include path + --python-link Print Python link flags + --pyside2 Print PySide2 location + --pyside2-include Print PySide2 include paths + --pyside2-link Print PySide2 link flags + --pyside2-shared-libraries Print paths of PySide2 shared libraries (.so's, .dylib's, .dll's) + --clang-bin-dir Print path to the clang bin directory + -a Print all + --help/-h Print this help +""" + +def cleanPath(path): + return path if sys.platform != 'win32' else path.replace('\\', '/') + +def sharedLibrarySuffix(): + if sys.platform == 'win32': + return 'lib' + elif sys.platform == 'darwin': + return 'dylib' + return 'so' + +def sharedLibraryGlobPattern(): + glob = '*.' + sharedLibrarySuffix() + return glob + +def filterPySide2SharedLibraries(list): + def predicate(item): + basename = os.path.basename(item) + if 'Qt' in basename and 'cpython' in basename: + return True + if 'shiboken' in basename or 'pyside2' in basename: + return True + return False + result = [item for item in list if predicate(item)] + return result + +# Return qmake link option for a library file name +def linkOption(lib): + baseName = os.path.splitext(os.path.basename(lib))[0] + link = ' -l' + if sys.platform in ['linux', 'linux2', 'darwin']: # Linux: 'libfoo.so' -> '-lfoo' + link += baseName[3:] + else: + link += baseName + return link + +# Locate PySide2 via package path +def findPySide2(): + for p in sys.path: + if 'site-' in p: + pyside2 = os.path.join(p, 'PySide2') + if os.path.exists(pyside2): + return cleanPath(os.path.realpath(pyside2)) + return None + +# Return version as "3.5" +def pythonVersion(): + return str(sys.version_info[0]) + '.' + str(sys.version_info[1]) + +def pythonInclude(): + return sysconfig.get_python_inc() + +def pythonLinkQmake(): + flags = pythonLinkData() + if sys.platform == 'win32' or sys.platform == 'darwin': + return '-L{} -l{}'.format(flags['libdir'], flags['lib']) + + # Linux and anything else + return '-l{}'.format(flags['lib']) + +def pythonLinkCmake(): + flags = pythonLinkData() + libdir = flags['libdir'] + lib = re.sub(r'.dll$', '.lib', flags['lib']) + return '{} {}'.format(libdir, lib) + +def pythonLinkData(): + # @TODO Fix to work with static builds of Python + libdir = sysconfig.get_config_var('LIBDIR') + version = pythonVersion() + version_no_dots = version.replace('.', '') + + flags = {} + flags['libdir'] = libdir + if sys.platform == 'win32': + suffix = '_d' if any([tup[0].endswith('_d.pyd') for tup in imp.get_suffixes()]) else '' + flags['lib'] = 'python{}{}'.format(version_no_dots, suffix) + + elif sys.platform == 'darwin': + flags['lib'] = 'python{}'.format(version) + + # Linux and anything else + else: + if sys.version_info[0] < 3: + suffix = '_d' if any([tup[0].endswith('_d.so') for tup in imp.get_suffixes()]) else '' + flags['lib'] = 'python{}{}'.format(version, suffix) + else: + flags['lib'] = 'python{}{}'.format(version, sys.abiflags) + + return flags + +def pyside2Include(): + pySide2 = findPySide2() + if pySide2 is None: + return None + return "/usr/include/PySide2 /usr/include/shiboken2" + +def pyside2Link(): + pySide2 = findPySide2() + if pySide2 is None: + return None + link = "-L{}".format(pySide2) + glob_result = glob.glob(os.path.join(pySide2, sharedLibraryGlobPattern())) + for lib in filterPySide2SharedLibraries(glob_result): + link += ' ' + link += linkOption(lib) + return link + +def pyside2SharedLibrariesData(): + pySide2 = findPySide2() + if pySide2 is None: + return None + + glob_result = glob.glob(os.path.join(pySide2, sharedLibraryGlobPattern())) + filtered_libs = filterPySide2SharedLibraries(glob_result) + libs = [] + if sys.platform == 'win32': + for lib in filtered_libs: + libs.append(os.path.realpath(lib)) + else: + for lib in filtered_libs: + libs.append(lib) + return libs + +def pyside2SharedLibraries(): + libs = pyside2SharedLibrariesData() + if libs is None: + return None + + if sys.platform == 'win32': + if not libs: + return '' + dlls = '' + for lib in libs: + dll = os.path.splitext(lib)[0] + '.dll' + dlls += dll + ' ' + + return dlls + else: + libs_string = '' + print(libs) + for lib in libs: + libs_string += ' ' + lib + return libs_string + +def pyside2SharedLibrariesCmake(): + libs = pyside2SharedLibrariesData() + result = ' '.join(libs) + result += ' /usr/lib/libpyside2.cpython-36m-x86_64-linux-gnu.so.2.0' + result += ' /usr/lib/libshiboken2.cpython-36m-x86_64-linux-gnu.so.2.0' + return result + +def clangBinPath(): + source = 'LLVM_INSTALL_DIR' + clangDir = os.environ.get(source, None) + if not clangDir: + source = 'CLANG_INSTALL_DIR' + clangDir = os.environ.get(source, None) + if not clangDir: + source = 'llvm-config' + try: + output = run_process_output([source, '--prefix']) + if output: + clangDir = output[0] + except OSError: + pass + if clangDir: + return os.path.realpath(clangDir + os.path.sep + 'bin') + return '' + +option = sys.argv[1] if len(sys.argv) == 2 else '-a' +if option == '-h' or option == '--help': + print(usage) + sys.exit(0) + +if option == '--pyside2' or option == '-a': + pySide2 = findPySide2() + if pySide2 is None: + sys.exit('Unable to locate PySide2') + print(pySide2) + +if option == '--pyside2-link' or option == '-a': + l = pyside2Link() + if l is None: + sys.exit('Unable to locate PySide2') + print(l) + +if option == '--pyside2-include' or option == '-a': + i = pyside2Include() + if i is None: + sys.exit('Unable to locate PySide2') + print(i) + +if option == '--python-include' or option == '-a': + i = pythonInclude() + if i is None: + sys.exit('Unable to locate Python') + print(i) + +if option == '--python-link' or option == '-a': + l = pythonLinkQmake() + if l is None: + sys.exit('Unable to locate Python') + print(l) + +if option == '--python-link-cmake' or option == '-a': + l = pythonLinkCmake() + if l is None: + sys.exit('Unable to locate Python') + print(l) + +if option == '--pyside2-shared-libraries' or option == '-a': + l = pyside2SharedLibraries() + if l is None: + sys.exit('Unable to locate the PySide2 shared libraries') + print(l) + +if option == '--pyside2-shared-libraries-cmake' or option == '-a': + l = pyside2SharedLibrariesCmake() + if l is None: + sys.exit('Unable to locate the PySide2 shared libraries') + print(l) + +if option == '--clang-bin-dir' or option == '-a': + l = clangBinPath() + if l is None: + sys.exit('Unable to locate Clang') + print(l) + diff --git a/src/plugins/PyFalkon/pythonplugin.cpp b/src/plugins/PyFalkon/pythonplugin.cpp new file mode 100644 index 000000000..b246c63f2 --- /dev/null +++ b/src/plugins/PyFalkon/pythonplugin.cpp @@ -0,0 +1,199 @@ +/* ============================================================ +* Falkon - Qt web browser +* Copyright (C) 2018 David Rosca +* +* 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 . +* ============================================================ */ +#include "pythonplugin.h" +#include "pythonpluginobject.h" + +#include "plugins.h" +#include "datapaths.h" +#include "desktopfile.h" + +#include +#include + +#include + +extern "C" PyObject *PyInit_PyFalkon(); + +enum State +{ + PythonUninitialized, + PythonInitialized, + PythonError +}; + +State state = PythonUninitialized; + +PythonPluginObject *pluginObject = nullptr; +QHash pluginInstances; + +static QStringList script_paths() +{ + QStringList dirs = DataPaths::allPaths(DataPaths::Plugins); + for (int i = 0; i < dirs.count(); ++i) { + dirs[i].append(QSL("/python")); + } + return dirs; +} + +static void cleanup() +{ + if (state > PythonUninitialized) { + Py_Finalize(); + state = PythonUninitialized; + delete pluginObject; + } +} + +static void set_path(const QStringList &scriptPaths) +{ + const QString originalPath = QString::fromLocal8Bit(qgetenv("PYTHONPATH")); + + QStringList paths = scriptPaths; + paths.append(originalPath); + + qputenv("PYTHONPATH", paths.join(QL1C(':')).toLocal8Bit()); +} + +static void register_plugin_object() +{ + pluginObject = new PythonPluginObject(); + + PyTypeObject *typeObject = Shiboken::SbkType(); + PyObject *po = Shiboken::Conversions::pointerToPython(reinterpret_cast(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) { + return state; + } + + set_path(script_paths()); + + if (PyImport_AppendInittab("Falkon", PyInit_PyFalkon) != 0) { + PyErr_Print(); + qWarning() << "Failed to initialize Falkon module!"; + return state = PythonError; + } + + Py_Initialize(); + qAddPostRoutine(cleanup); + state = PythonInitialized; + + if (!PyImport_ImportModule("Falkon")) { + PyErr_Print(); + qWarning() << "Failed to import Falkon module!"; + return state = PythonError; + } + + register_plugin_object(); + + return state; +} + +Plugins::Plugin pyfalkon_load_plugin(const QString &name) +{ + QString fullPath; + if (QFileInfo(name).isAbsolute()) { + fullPath = name; + } else { + fullPath = DataPaths::locate(DataPaths::Plugins, QSL("python/") + name); + if (fullPath.isEmpty()) { + qWarning() << "Plugin" << name << "not found"; + return Plugins::Plugin(); + } + } + + Plugins::Plugin plugin; + plugin.type = Plugins::Plugin::PythonPlugin; + plugin.pluginId = QSL("python:%1").arg(QFileInfo(name).fileName()); + plugin.pluginSpec = Plugins::createSpec(DesktopFile(fullPath + QSL("/metadata.desktop"))); + return plugin; +} + +void pyfalkon_init_plugin(Plugins::Plugin *plugin) +{ + if (init() != PythonInitialized) { + return; + } + + PyObject *module = static_cast(plugin->data.value()); + if (module) { + plugin->instance = pluginInstances.value(module); + return; + } + + const QString moduleName = plugin->pluginId.mid(7); + + pluginObject->ptr = nullptr; + + module = PyImport_ImportModule(qPrintable(moduleName)); + if (!module) { + PyErr_Print(); + qWarning() << "Failed to import module" << moduleName; + return; + } + if (!pluginObject->ptr) { + qWarning() << "No plugin registered! Falkon.plugin.registerPlugin() must be called from script."; + return; + } + + pluginInstances[module] = pluginObject->ptr; + plugin->instance = pluginObject->ptr; + plugin->data = QVariant::fromValue(static_cast(module)); +} + +QVector pyfalkon_load_available_plugins() +{ + QVector plugins; + + const QStringList dirs = script_paths(); + for (const QString &dir : dirs) { + const auto modules = QDir(dir).entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot); + for (const QFileInfo &info : modules) { + Plugins::Plugin plugin = pyfalkon_load_plugin(info.absoluteFilePath()); + if (plugin.pluginSpec.name.isEmpty()) { + qWarning() << "Invalid plugin spec of" << info.absoluteFilePath() << "plugin"; + continue; + } + plugins.append(plugin); + } + } + + return plugins; +} diff --git a/src/plugins/PyFalkon/pythonplugin.h b/src/plugins/PyFalkon/pythonplugin.h new file mode 100644 index 000000000..63570ddd4 --- /dev/null +++ b/src/plugins/PyFalkon/pythonplugin.h @@ -0,0 +1,24 @@ +/* ============================================================ +* Falkon - Qt web browser +* Copyright (C) 2018 David Rosca +* +* 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 . +* ============================================================ */ +#pragma once + +#include "plugins.h" + +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 pyfalkon_load_available_plugins(); diff --git a/src/plugins/PyFalkon/pythonpluginobject.cpp b/src/plugins/PyFalkon/pythonpluginobject.cpp new file mode 100644 index 000000000..0ba482b8e --- /dev/null +++ b/src/plugins/PyFalkon/pythonpluginobject.cpp @@ -0,0 +1,30 @@ +/* ============================================================ +* Falkon - Qt web browser +* Copyright (C) 2018 David Rosca +* +* 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 . +* ============================================================ */ +#include "pythonpluginobject.h" + +PluginInterface *PythonPluginObject::ptr = nullptr; + +PythonPluginObject::PythonPluginObject(QObject *parent) + : QObject(parent) +{ +} + +void PythonPluginObject::registerPlugin(PluginInterface *plugin) +{ + ptr = plugin; +} diff --git a/src/plugins/PyFalkon/pythonpluginobject.h b/src/plugins/PyFalkon/pythonpluginobject.h new file mode 100644 index 000000000..d31807ef5 --- /dev/null +++ b/src/plugins/PyFalkon/pythonpluginobject.h @@ -0,0 +1,34 @@ +/* ============================================================ +* Falkon - Qt web browser +* Copyright (C) 2018 David Rosca +* +* 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 . +* ============================================================ */ +#pragma once + +#include + +class PluginInterface; + +class PythonPluginObject : public QObject +{ + Q_OBJECT + +public: + explicit PythonPluginObject(QObject *parent = nullptr); + + void registerPlugin(PluginInterface *plugin); + + static PluginInterface *ptr; +}; diff --git a/src/plugins/PyFalkon/wrappedclasses.h b/src/plugins/PyFalkon/wrappedclasses.h new file mode 100644 index 000000000..1b1c02859 --- /dev/null +++ b/src/plugins/PyFalkon/wrappedclasses.h @@ -0,0 +1,6 @@ +// Falkon +#include "desktopfile.h" +#include "plugininterface.h" + +// PyFalkon +#include "pythonpluginobject.h"