From 7a979c732a936852cf6d8f720b61d2e4cd28b74f Mon Sep 17 00:00:00 2001
From: Matthieu Gallien <matthieu.gallien@nextcloud.com>
Date: Mon, 26 Aug 2024 19:11:33 +0200
Subject: [PATCH] properly detect and enable Qt6 WebEngine integration

Signed-off-by: Matthieu Gallien <matthieu.gallien@nextcloud.com>
---
 CMakeLists.txt                             | 39 +++++++++++++++++++---
 config.h.in                                |  2 ++
 src/CMakeLists.txt                         | 32 +-----------------
 src/gui/CMakeLists.txt                     |  6 ++--
 src/gui/creds/webflowcredentialsdialog.cpp |  8 +++--
 src/gui/creds/webflowcredentialsdialog.h   |  6 ++--
 src/gui/wizard/owncloudwizardcommon.h      |  2 ++
 src/gui/wizard/webview.cpp                 | 11 +++---
 src/libsync/networkjobs.h                  |  2 ++
 9 files changed, 62 insertions(+), 46 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3c278aa199430..635ba0dfd7276 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -243,10 +243,41 @@ if(BUILD_CLIENT)
    endif()
 endif()
 
-if (APPLE)
-    option(BUILD_WITH_WEBENGINE "BUILD_WITH_WEBENGINE" OFF)
-else ()
-    option(BUILD_WITH_WEBENGINE "BUILD_WITH_WEBENGINE" ON)
+option(BUILD_WITH_WEBENGINE "BUILD_WITH_WEBENGINE" ON)
+if (BUILD_WITH_WEBENGINE)
+    find_package(Qt${QT_VERSION_MAJOR}WebEngineCore ${REQUIRED_QT_VERSION} CONFIG QUIET)
+    if(APPLE)
+        set_package_properties(Qt${QT_VERSION_MAJOR}WebEngineCore PROPERTIES
+            DESCRIPTION "Qt${QT_VERSION_MAJOR} WebEngineCore component."
+            TYPE RECOMMENDED
+        )
+    else()
+        set_package_properties(Qt${QT_VERSION_MAJOR}WebEngineCore PROPERTIES
+            DESCRIPTION "Qt${QT_VERSION_MAJOR} WebEngine component."
+            TYPE REQUIRED
+        )
+    endif()
+
+    find_package(Qt${QT_MAJOR_VERSION}WebEngineWidgets ${REQUIRED_QT_VERSION} CONFIG QUIET)
+    if(APPLE)
+        set_package_properties(Qt${QT_MAJOR_VERSION}WebEngineWidgets PROPERTIES
+            DESCRIPTION "Qt${QT_MAJOR_VERSION} WebEngineWidgets component."
+            TYPE RECOMMENDED
+        )
+    else()
+        set_package_properties(Qt${QT_MAJOR_VERSION}WebEngineWidgets PROPERTIES
+            DESCRIPTION "Qt${QT_MAJOR_VERSION} WebEngineWidgets component."
+            TYPE REQUIRED
+        )
+    endif()
+
+    if(Qt${QT_MAJOR_VERSION}WebEngineCore_FOUND AND Qt${QT_MAJOR_VERSION}WebEngineWidgets_FOUND)
+        message(STATUS "Enable use of Qt6 WebEngine module")
+        set(WITH_WEBENGINE 1)
+    else()
+        unset(WITH_WEBENGINE)
+        message(STATUS "Disable use of Qt6 WebEngine module")
+    endif()
 endif()
 
 if (NOT DEFINED APPLICATION_ICON_NAME)
diff --git a/config.h.in b/config.h.in
index d7f561525beb6..fd2b177661992 100644
--- a/config.h.in
+++ b/config.h.in
@@ -63,4 +63,6 @@
 
 #cmakedefine01 NEXTCLOUD_DEV
 
+#cmakedefine WITH_WEBENGINE
+
 #endif
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 75c6f458295cf..fe9d4d9affab3 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,6 +1,6 @@
 include(ECMEnableSanitizers)
 
-set(REQUIRED_QT_VERSION "5.15.0")
+set(REQUIRED_QT_VERSION "6.5.0")
 
 find_package(Qt${QT_MAJOR_VERSION}Core ${REQUIRED_QT_VERSION} CONFIG QUIET)
 set_package_properties(Qt${QT_MAJOR_VERSION}Core PROPERTIES
@@ -38,36 +38,6 @@ find_package(Qt${QT_VERSION_MAJOR}Core5Compat ${REQUIRED_QT_VERSION} CONFIG QUIE
      TYPE REQUIRED
  )
 
-find_package(Qt${QT_MAJOR_VERSION}WebEngineWidgets ${REQUIRED_QT_VERSION} CONFIG QUIET)
-if(NOT BUILD_WITH_WEBENGINE)
-    set_package_properties(Qt${QT_MAJOR_VERSION}WebEngineWidgets PROPERTIES
-        DESCRIPTION "Qt${QT_MAJOR_VERSION} WebEngineWidgets component."
-        TYPE RECOMMENDED
-    )
-else()
-    set_package_properties(Qt${QT_MAJOR_VERSION}WebEngineWidgets PROPERTIES
-        DESCRIPTION "Qt${QT_MAJOR_VERSION} WebEngineWidgets component."
-        TYPE REQUIRED
-    )
-endif()
-
-find_package(Qt${QT_VERSION_MAJOR}WebEngineCore ${REQUIRED_QT_VERSION} CONFIG QUIET)
-if(APPLE)
-    set_package_properties(Qt${QT_VERSION_MAJOR}WebEngineCore PROPERTIES
-        DESCRIPTION "Qt${QT_VERSION_MAJOR} WebEngineCore component."
-        TYPE RECOMMENDED
-    )
-else()
-    set_package_properties(Qt${QT_VERSION_MAJOR}WebEngine PROPERTIES
-        DESCRIPTION "Qt${QT_VERSION_MAJOR} WebEngine component."
-        TYPE REQUIRED
-    )
-endif()
-
-if(Qt${QT_MAJOR_VERSION}WebEngine_FOUND AND Qt${QT_MAJOR_VERSION}WebEngineWidgets_FOUND)
-  add_compile_definitions(WITH_WEBENGINE=1)
-endif()
-
 get_target_property (QT_QMAKE_EXECUTABLE Qt::qmake IMPORTED_LOCATION)
 message(STATUS "Using Qt ${Qt${QT_MAJOR_VERSION}Core_VERSION} (${QT_QMAKE_EXECUTABLE})")
 
diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt
index f26bfaa8e4fd0..845066977d403 100644
--- a/src/gui/CMakeLists.txt
+++ b/src/gui/CMakeLists.txt
@@ -251,7 +251,7 @@ set(client_SRCS
     wizard/linklabel.cpp
     )
 
-if (Qt${QT_MAJOR_VERSION}WebEngine_FOUND AND Qt${QT_MAJOR_VERSION}WebEngineWidgets_FOUND)
+if (WITH_WEBENGINE)
   list(APPEND client_SRCS
     wizard/webviewpage.h
     wizard/webviewpage.cpp
@@ -581,8 +581,8 @@ foreach(FILE IN LISTS client_UI_SRCS)
 	set_property(SOURCE ${FILE} PROPERTY SKIP_UNITY_BUILD_INCLUSION ON)
 endforeach()
 
-if(Qt6WebEngine_FOUND AND Qt6WebEngineWidgets_FOUND)
-  target_link_libraries(nextcloudCore PUBLIC Qt::WebEngineWidgets)
+if(WITH_WEBENGINE)
+    target_link_libraries(nextcloudCore PUBLIC Qt::WebEngineWidgets Qt::WebEngineCore)
 endif()
 
 set_target_properties(nextcloudCore
diff --git a/src/gui/creds/webflowcredentialsdialog.cpp b/src/gui/creds/webflowcredentialsdialog.cpp
index 9ce37d7f0a1d9..4178b0bd73586 100644
--- a/src/gui/creds/webflowcredentialsdialog.cpp
+++ b/src/gui/creds/webflowcredentialsdialog.cpp
@@ -1,17 +1,21 @@
 #include "webflowcredentialsdialog.h"
 
-#include <QVBoxLayout>
-#include <QLabel>
+#include "config.h"
 
 #include "theme.h"
 #include "application.h"
 #include "owncloudgui.h"
 #include "wizard/owncloudwizardcommon.h"
+
 #ifdef WITH_WEBENGINE
 #include "wizard/webview.h"
 #endif // WITH_WEBENGINE
+
 #include "wizard/flow2authwidget.h"
 
+#include <QVBoxLayout>
+#include <QLabel>
+
 namespace OCC {
 
 WebFlowCredentialsDialog::WebFlowCredentialsDialog(Account *account, bool useFlow2, QWidget *parent)
diff --git a/src/gui/creds/webflowcredentialsdialog.h b/src/gui/creds/webflowcredentialsdialog.h
index 86abe8af3fd71..2ed7e7a7b572f 100644
--- a/src/gui/creds/webflowcredentialsdialog.h
+++ b/src/gui/creds/webflowcredentialsdialog.h
@@ -1,12 +1,14 @@
 #ifndef WEBFLOWCREDENTIALSDIALOG_H
 #define WEBFLOWCREDENTIALSDIALOG_H
 
-#include <QDialog>
-#include <QUrl>
+#include "config.h"
 
 #include "accountfwd.h"
 #include "creds/flow2auth.h"
 
+#include <QDialog>
+#include <QUrl>
+
 class QLabel;
 class QVBoxLayout;
 
diff --git a/src/gui/wizard/owncloudwizardcommon.h b/src/gui/wizard/owncloudwizardcommon.h
index fb118d84f6fd2..f5d7ba530f436 100644
--- a/src/gui/wizard/owncloudwizardcommon.h
+++ b/src/gui/wizard/owncloudwizardcommon.h
@@ -16,6 +16,8 @@
 #ifndef MIRALL_OWNCLOUD_WIZARD_COMMON_H
 #define MIRALL_OWNCLOUD_WIZARD_COMMON_H
 
+#include "config.h"
+
 #include <QString>
 
 class QVariant;
diff --git a/src/gui/wizard/webview.cpp b/src/gui/wizard/webview.cpp
index 5baf82d1cf7f6..0eb0bdb0d1f46 100644
--- a/src/gui/wizard/webview.cpp
+++ b/src/gui/wizard/webview.cpp
@@ -51,7 +51,7 @@ class WebEnginePage : public QWebEnginePage {
     void setUrl(const QUrl &url);
 
 protected:
-    bool certificateError(const QWebEngineCertificateError &certificateError) override;
+    bool slotCertificateError(const QWebEngineCertificateError &certificateError);
 
     bool acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) override;
 
@@ -202,8 +202,11 @@ void WebViewPageUrlSchemeHandler::requestStarted(QWebEngineUrlRequestJob *reques
 }
 
 
-WebEnginePage::WebEnginePage(QWebEngineProfile *profile, QObject* parent) : QWebEnginePage(profile, parent) {
-
+WebEnginePage::WebEnginePage(QWebEngineProfile *profile, QObject* parent)
+    : QWebEnginePage(profile, parent)
+{
+    connect(this, &QWebEnginePage::certificateError,
+            this, &WebEnginePage::slotCertificateError);
 }
 
 QWebEnginePage * WebEnginePage::createWindow(QWebEnginePage::WebWindowType type) {
@@ -218,7 +221,7 @@ void WebEnginePage::setUrl(const QUrl &url)
     _enforceHttps = url.scheme() == QStringLiteral("https");
 }
 
-bool WebEnginePage::certificateError(const QWebEngineCertificateError &certificateError)
+bool WebEnginePage::slotCertificateError(const QWebEngineCertificateError &certificateError)
 {
     /**
      * TODO properly improve this.
diff --git a/src/libsync/networkjobs.h b/src/libsync/networkjobs.h
index c4968157a8508..d013e7be83ac9 100644
--- a/src/libsync/networkjobs.h
+++ b/src/libsync/networkjobs.h
@@ -16,6 +16,8 @@
 #ifndef NETWORKJOBS_H
 #define NETWORKJOBS_H
 
+#include "config.h"
+
 #include "abstractnetworkjob.h"
 
 #include "common/result.h"