From 11a36a50558bdbc9c42253b937c8a832d896248f Mon Sep 17 00:00:00 2001
From: Andreas Sturmlechner <asturm@gentoo.org>
Date: Fri, 21 Feb 2025 22:28:27 +0100
Subject: [PATCH] Make it possible to build against system kdsingleapplication

Bundling must be avoided wherever possible.

Signed-off-by: Andreas Sturmlechner <asturm@gentoo.org>
---
 CMakeLists.txt     |  7 +++++++
 src/CMakeLists.txt | 23 ++++++++++++++++++++---
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index ac49e01f..990ff871 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -112,6 +112,13 @@ set_package_properties(Ytdlp PROPERTIES TYPE RUNTIME
     URL "https://github.com/yt-dlp/yt-dlp"
     DESCRIPTION "needed to play videos from youtube and other online sources")
 
+find_package(KDSingleApplication-qt6 QUIET)
+if(KDSingleApplication-qt6_FOUND)
+    message(STATUS "Using system KDSingleApplication")
+else()
+    message(STATUS "Using bundled KDSingleApplication")
+endif()
+
 feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES)
 
 include(KDEInstallDirs)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ef334a52..da9dd493 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -16,8 +16,6 @@ target_sources(haruna
     application.h application.cpp
     framedecoder.h framedecoder.cpp
     global.h global.cpp
-    KDSingleApplication/kdsingleapplication_localsocket_p.h KDSingleApplication/kdsingleapplication_localsocket.cpp
-    KDSingleApplication/kdsingleapplication.h KDSingleApplication/kdsingleapplication.cpp
     main.cpp
     models/actionsmodel.h models/actionsmodel.cpp
     models/chaptersmodel.h models/chaptersmodel.cpp
@@ -34,6 +32,14 @@ target_sources(haruna
     ${ICONS_SRCS}
 )
 
+if (NOT KDSingleApplication-qt6_FOUND)
+    target_sources(haruna
+        PRIVATE
+        KDSingleApplication/kdsingleapplication_localsocket_p.h KDSingleApplication/kdsingleapplication_localsocket.cpp
+        KDSingleApplication/kdsingleapplication.h KDSingleApplication/kdsingleapplication.cpp
+    )
+endif()
+
 if (CMAKE_SYSTEM_NAME IN_LIST DBUS_PLATFORMS)
     qt_add_dbus_interface(haruna_screensaver_inhibit_SRCS ../org.freedesktop.ScreenSaver.xml screensaverdbusinterface)
     target_sources(haruna
@@ -126,12 +132,18 @@ qt_add_qml_module(haruna
 
 target_include_directories(haruna
     PRIVATE
-        KDSingleApplication
         models
         mpv
         ${FFMPEG_INCLUDE_DIRS}
 )
 
+if (NOT KDSingleApplication-qt6_FOUND)
+    target_include_directories(haruna
+        PRIVATE
+        KDSingleApplication
+    )
+endif()
+
 target_compile_definitions(haruna PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
 
 target_link_libraries(haruna PRIVATE
@@ -161,6 +173,11 @@ target_link_libraries(haruna PRIVATE
     ${AVCODEC_LIBRARIES}
     ${SWSCALE_LIBRARIES}
 )
+if(KDSingleApplication-qt6_FOUND)
+    target_link_libraries(haruna PRIVATE
+        KDAB::kdsingleapplication
+    )
+endif()
 
 if (CMAKE_SYSTEM_NAME IN_LIST DBUS_PLATFORMS)
     target_link_libraries(haruna PRIVATE
-- 
2.48.1

