From 36fd285568bd8894638e8a65d5ff37c6bf6ae1a36d1d5b748e92152eb14f62e6 Mon Sep 17 00:00:00 2001
From: Martin Pluskal <mpluskal@suse.com>
Date: Fri, 16 Feb 2024 12:48:54 +0000
Subject: [PATCH] Accepting request 1147076 from
 home:badshah400:branches:Publishing

* Add texworks-python-plugin-buildfix.patch to fix building python scripting plugin (gh#TeXworks/texworks#1038); patch taken from upstream commit.
* Refresh texworks-cmake-find-python.patch with version committed upstream.
* Enable python scripting plugin; Requires at least Python 3.8, disable plugin for openSUSE < 1650.
* Require gcc9 for openSUSE < 1650 as GCC >= 8 is required by Lua scripting plugin for filesystem support.

OBS-URL: https://build.opensuse.org/request/show/1147076
OBS-URL: https://build.opensuse.org/package/show/Publishing/texworks?expand=0&rev=27
---
 texworks-cmake-find-python.patch      |  60 +++++++++-----
 texworks-python-plugin-buildfix.patch | 111 ++++++++++++++++++++++++++
 texworks.changes                      |  13 +++
 texworks.spec                         |  15 +++-
 4 files changed, 179 insertions(+), 20 deletions(-)
 create mode 100644 texworks-python-plugin-buildfix.patch

diff --git a/texworks-cmake-find-python.patch b/texworks-cmake-find-python.patch
index 59ba8a7..30092ac 100644
--- a/texworks-cmake-find-python.patch
+++ b/texworks-cmake-find-python.patch
@@ -1,8 +1,19 @@
-Index: texworks-release-0.6.9/CMakeLists.txt
-===================================================================
---- texworks-release-0.6.9.orig/CMakeLists.txt
-+++ texworks-release-0.6.9/CMakeLists.txt
-@@ -326,17 +326,14 @@ ENDIF()
+From dae1586af7a218e9bbe9ce3031a97e8efcac980a Mon Sep 17 00:00:00 2001
+From: Atri Bhattacharya <A.Bhattacharya@uliege.be>
+Date: Thu, 15 Feb 2024 20:03:50 +0100
+Subject: [PATCH] Fix finding Python with CMake >= 3.27 (fixes #1039)
+
+---
+ CMakeLists.txt                            | 38 ++++++++++++++---------
+ plugins-src/TWPythonPlugin/CMakeLists.txt |  4 +--
+ src/CMakeLists.txt                        |  4 +--
+ 3 files changed, 28 insertions(+), 18 deletions(-)
+
+diff --git a/CMakeLists.txt b/CMakeLists.txt
+index f245e5192..9682c3933 100644
+--- a/CMakeLists.txt
++++ b/CMakeLists.txt
+@@ -326,17 +326,27 @@ ENDIF()
  
  IF ( WITH_PYTHON )
    IF ( USE_SYSTEM_PYTHON )
@@ -23,11 +34,24 @@ Index: texworks-release-0.6.9/CMakeLists.txt
 -    # In order to find the correct version of 'PythonInterp', we need to set 'PYTHONHOME' environment variable
 -    FIND_PACKAGE(PythonInterp)
 -    FIND_PACKAGE(PythonLibs)
-+    FIND_PACKAGE(Python COMPONENTS Interpreter Development)
++    IF (CMAKE_VERSION VERSION_LESS "3.12")
++      # **NOTE**
++      # In order to find the correct version of 'PythonLibs', it seems that we need to run 'FIND_PACKAGE(PythonInterp)' firstly.
++      # In order to find the correct version of 'PythonInterp', we need to set 'PYTHONHOME' environment variable
++      FIND_PACKAGE(PythonInterp)
++      FIND_PACKAGE(PythonLibs)
++      SET(Python_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS})
++      SET(Python_LIBRARIES ${PYTHON_LIBRARIES})
++      SET(Python_VERSION ${PYTHON_VERSION_STRING})
++      SET(Python_Interpreter_FOUND ${PYTHONINTERP_FOUND})
++      SET(Python_Development_FOUND ${PYTHONLIBS_FOUND})
++    ELSE ()
++      FIND_PACKAGE(Python COMPONENTS Interpreter Development)
++    ENDIF ()
    ENDIF ()
  ENDIF()
  
-@@ -344,7 +341,7 @@ IF ( LUA_FOUND AND WITH_LUA AND NOT ${BU
+@@ -344,7 +354,7 @@ IF ( LUA_FOUND AND WITH_LUA AND NOT ${BUILD_SHARED_PLUGINS})
    ADD_DEFINITIONS(-DQT_STATICPLUGIN -DSTATIC_LUA_SCRIPTING_PLUGIN)
  ENDIF ()
  
@@ -36,7 +60,7 @@ Index: texworks-release-0.6.9/CMakeLists.txt
    ADD_DEFINITIONS(-DQT_STATICPLUGIN -DSTATIC_PYTHON_SCRIPTING_PLUGIN)
  ENDIF ()
  
-@@ -406,7 +403,7 @@ IF ( LUA_FOUND AND WITH_LUA )
+@@ -406,7 +416,7 @@ IF ( LUA_FOUND AND WITH_LUA )
    ADD_SUBDIRECTORY(${TeXworks_SOURCE_DIR}/plugins-src/TWLuaPlugin)
  ENDIF ()
  
@@ -45,7 +69,7 @@ Index: texworks-release-0.6.9/CMakeLists.txt
    ADD_SUBDIRECTORY(${TeXworks_SOURCE_DIR}/plugins-src/TWPythonPlugin)
  ENDIF ()
  
-@@ -509,7 +506,7 @@ IF ( WITH_LUA )
+@@ -509,7 +519,7 @@ IF ( WITH_LUA )
    CONFIG_VERSION("Lua" "${LUA_VERSION_STRING}")
  ENDIF()
  if (WITH_PYTHON)
@@ -54,7 +78,7 @@ Index: texworks-release-0.6.9/CMakeLists.txt
  endif()
  CONFIG_VERSION("Qt" ${QT_VERSION_MAJOR}.${QT_VERSION_MINOR}.${QT_VERSION_PATCH})
  CONFIG_VERSION("SyncTeX" "${SYNCTEX_VERSION_STRING}")
-@@ -520,7 +517,7 @@ message("  Scripting")
+@@ -520,7 +530,7 @@ message("  Scripting")
  CONFIG_YESNO("  ECMA scripting" ON)
  CONFIG_YESNO("  QtScript scripting" WITH_QTSCRIPT)
  CONFIG_YESNO("  Lua scripting plugin" LUA_FOUND)
@@ -63,10 +87,10 @@ Index: texworks-release-0.6.9/CMakeLists.txt
  message("")
  
  CONFIG_INFO("Build ID" ${TW_BUILD_ID})
-Index: texworks-release-0.6.9/plugins-src/TWPythonPlugin/CMakeLists.txt
-===================================================================
---- texworks-release-0.6.9.orig/plugins-src/TWPythonPlugin/CMakeLists.txt
-+++ texworks-release-0.6.9/plugins-src/TWPythonPlugin/CMakeLists.txt
+diff --git a/plugins-src/TWPythonPlugin/CMakeLists.txt b/plugins-src/TWPythonPlugin/CMakeLists.txt
+index 255937f17..6d99c5a68 100644
+--- a/plugins-src/TWPythonPlugin/CMakeLists.txt
++++ b/plugins-src/TWPythonPlugin/CMakeLists.txt
 @@ -37,13 +37,13 @@ if (NOT MSVC)
  	target_compile_options(TWPythonPlugin PRIVATE -Wno-old-style-cast)
  endif ()
@@ -83,10 +107,10 @@ Index: texworks-release-0.6.9/plugins-src/TWPythonPlugin/CMakeLists.txt
  IF (${BUILD_SHARED_PLUGINS})
    INSTALL(TARGETS TWPythonPlugin
      LIBRARY DESTINATION ${TeXworks_PLUGIN_DIR}
-Index: texworks-release-0.6.9/src/CMakeLists.txt
-===================================================================
---- texworks-release-0.6.9.orig/src/CMakeLists.txt
-+++ texworks-release-0.6.9/src/CMakeLists.txt
+diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
+index ba2f40351..3f91ab887 100644
+--- a/src/CMakeLists.txt
++++ b/src/CMakeLists.txt
 @@ -207,8 +207,8 @@ IF (NOT ${BUILD_SHARED_PLUGINS})
    IF (WITH_LUA AND LUA_FOUND)
      LIST(INSERT TeXworks_LIBS 0 TWLuaPlugin ${LUA_LIBRARIES})
diff --git a/texworks-python-plugin-buildfix.patch b/texworks-python-plugin-buildfix.patch
new file mode 100644
index 0000000..cb4efeb
--- /dev/null
+++ b/texworks-python-plugin-buildfix.patch
@@ -0,0 +1,111 @@
+From f8962bca2db2cae3183cad201a4726e7726caccb Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Stefan=20L=C3=B6ffler?= <st.loeffler@gmail.com>
+Date: Thu, 15 Feb 2024 20:05:39 +0100
+Subject: [PATCH] Fix building the python scripting plugin (fixes #1038)
+
+---
+ plugins-src/TWPythonPlugin/PythonScript.cpp | 31 +++++++++++++--------
+ plugins-src/TWPythonPlugin/PythonScript.h   |  4 ++-
+ 2 files changed, 22 insertions(+), 13 deletions(-)
+
+diff --git a/plugins-src/TWPythonPlugin/PythonScript.cpp b/plugins-src/TWPythonPlugin/PythonScript.cpp
+index 752783b60..5c218e66e 100644
+--- a/plugins-src/TWPythonPlugin/PythonScript.cpp
++++ b/plugins-src/TWPythonPlugin/PythonScript.cpp
+@@ -21,6 +21,9 @@
+ 
+ #include "PythonScript.h"
+ 
++// https://docs.python.org/3/c-api/arg.html#strings-and-buffers
++#define PY_SSIZE_T_CLEAN
++
+ // Python uses the name "slots", which Qt hijacks. So we temporarily undefine
+ // it, then include the python headers, then redefine it
+ #undef slots
+@@ -376,22 +379,26 @@ PyObject * PythonScript::VariantToPython(const QVariant & v)
+ {
+ 	if (v.isNull()) Py_RETURN_NONE;
+ 
++#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
+ 	switch (static_cast<int>(v.type())) {
+-		case QVariant::Double:
++#else
++	switch (v.metaType().id()) {
++#endif
++		case QMetaType::Double:
+ 			return Py_BuildValue("d", v.toDouble());
+-		case QVariant::Bool:
++		case QMetaType::Bool:
+ 			if (v.toBool()) Py_RETURN_TRUE;
+ 			else Py_RETURN_FALSE;
+-		case QVariant::Int:
++		case QMetaType::Int:
+ 			return Py_BuildValue("i", v.toInt());
+-		case QVariant::LongLong:
++		case QMetaType::LongLong:
+ 			return Py_BuildValue("L", v.toLongLong());
+-		case QVariant::UInt:
++		case QMetaType::UInt:
+ 			return Py_BuildValue("I", v.toUInt());
+-		case QVariant::ULongLong:
++		case QMetaType::ULongLong:
+ 			return Py_BuildValue("K", v.toULongLong());
+-		case QVariant::Char:
+-		case QVariant::String:
++		case QMetaType::Char:
++		case QMetaType::QString:
+ #ifdef Py_UNICODE_WIDE
+ 			{
+ 				QVector<uint> tmp = v.toString().toUcs4();
+@@ -400,8 +407,8 @@ PyObject * PythonScript::VariantToPython(const QVariant & v)
+ #else
+ 			return Py_BuildValue("u", v.toString().constData());
+ #endif
+-		case QVariant::List:
+-		case QVariant::StringList:
++		case QMetaType::QVariantList:
++		case QMetaType::QStringList:
+ 		{
+ 			QVariantList list = v.toList();
+ 
+@@ -412,7 +419,7 @@ PyObject * PythonScript::VariantToPython(const QVariant & v)
+ 			}
+ 			return pyList;
+ 		}
+-		case QVariant::Hash:
++		case QMetaType::QVariantHash:
+ 		{
+ 			QVariantHash hash = v.toHash();
+ 
+@@ -422,7 +429,7 @@ PyObject * PythonScript::VariantToPython(const QVariant & v)
+ 			}
+ 			return pyDict;
+ 		}
+-		case QVariant::Map:
++		case QMetaType::QVariantMap:
+ 		{
+ 			QVariantMap map = v.toMap();
+ 
+diff --git a/plugins-src/TWPythonPlugin/PythonScript.h b/plugins-src/TWPythonPlugin/PythonScript.h
+index 35d420aa9..658d4330a 100644
+--- a/plugins-src/TWPythonPlugin/PythonScript.h
++++ b/plugins-src/TWPythonPlugin/PythonScript.h
+@@ -26,6 +26,8 @@
+ #include "scripting/Script.h"
+ #include "scripting/ScriptAPIInterface.h"
+ 
++#include <QCoreApplication>
++
+ // Forward declaration taken from the Python headers to avoid having to include
+ // Python in this header file
+ struct _object;
+@@ -37,8 +39,8 @@ namespace Scripting {
+ /** \brief Class for handling python scripts */
+ class PythonScript : public Script
+ {
+-	Q_OBJECT
+ 	Q_INTERFACES(Tw::Scripting::Script)
++	Q_DECLARE_TR_FUNCTIONS(Tw::Scripting::ECMAScript)
+ 
+ public:
+ 	/** \brief Constructor
diff --git a/texworks.changes b/texworks.changes
index 57451aa..6a32bbf 100644
--- a/texworks.changes
+++ b/texworks.changes
@@ -1,3 +1,16 @@
+-------------------------------------------------------------------
+Thu Feb 15 19:27:12 UTC 2024 - Atri Bhattacharya <badshah400@gmail.com>
+
+- Add texworks-python-plugin-buildfix.patch to fix building python
+  scripting plugin (gh#TeXworks/texworks#1038); patch taken from
+  upstream commit.
+- Refresh texworks-cmake-find-python.patch with version committed
+  upstream.
+- Enable python scripting plugin; Requires at least Python 3.8,
+  disable plugin for openSUSE < 1650.
+- Require gcc9 for openSUSE < 1650 as GCC >= 8 is required by Lua
+  scripting plugin for filesystem support.
+
 -------------------------------------------------------------------
 Mon Feb 12 10:35:26 UTC 2024 - Atri Bhattacharya <badshah400@gmail.com>
 
diff --git a/texworks.spec b/texworks.spec
index 1c5f829..bf9f4b5 100644
--- a/texworks.spec
+++ b/texworks.spec
@@ -18,7 +18,14 @@
 
 
 %define __builder ninja
+%if 0%{?suse_version} < 1650
+# Lua plugin requires GCC >= 8 for filesystem support
+%define gcc_ver 9
+# Python plugin requires Python >= 3.8
 %bcond_with python
+%else
+%bcond_without python
+%endif
 Name:           texworks
 Version:        0.6.9
 Release:        0
@@ -28,10 +35,13 @@ Group:          Productivity/Publishing/TeX/Frontends
 URL:            https://www.tug.org/texworks/
 Source0:        https://github.com/TeXworks/texworks/archive/release-%{version}.tar.gz#/%{name}-%{version}.tar.gz
 # PATCH-FIX-UPSTREAM texworks-cmake-find-python.patch gh#TeXworks/texworks#1039 badshah400@gmail.com -- cmake has dropped support for PythonInterp and PythonLibs, use FindPython instead
-Patch0:         texworks-cmake-find-python.patch
+Patch0:         https://github.com/TeXworks/texworks/commit/dae1586af7a218e9bbe9ce3031a97e8efcac980a.patch#/texworks-cmake-find-python.patch
+# PATCH-FIX-UPSTREAM texworks-python-plugin-buildfix.patch gh#/TeXworks/texworks#1038 badshah400@gmail.com -- Fix building the python scripting plugin
+Patch1:         https://github.com/TeXworks/texworks/commit/f8962bca2db2cae3183cad201a4726e7726caccb.patch#/texworks-python-plugin-buildfix.patch
 BuildRequires:  cmake
 BuildRequires:  dbus-1-devel
 BuildRequires:  desktop-file-utils
+BuildRequires:  gcc%{?gcc_ver}-c++
 BuildRequires:  hicolor-icon-theme
 BuildRequires:  hunspell-devel
 BuildRequires:  libpoppler-devel >= 0.24
@@ -93,7 +103,8 @@ This package adds lua scripting abitilies to TeXworks.
 %autosetup -p1 -n texworks-release-%{version}
 
 %build
-%cmake -DCMAKE_INSTALL_PREFIX=%{_prefix} \
+%cmake -DCMAKE_CXX_COMPILER=g++%{?gcc_ver:-%{gcc_ver}} \
+       -DCMAKE_INSTALL_PREFIX=%{_prefix} \
        -DTW_BUILD_ID="openSUSE" \
        -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=%{_lib} \
        -DQT_DEFAULT_MAJOR_VERSION=6 \