# SPDX-FileCopyrightText: 2022-2025 Laurent Montel <montel@kde.org>
# SPDX-License-Identifier: BSD-3-Clause
cmake_minimum_required(VERSION 3.16 FATAL_ERROR)
set(KTEXTADDONS_CURRENT_VERSION "1.9.0")
project(KTextAddons VERSION ${KTEXTADDONS_CURRENT_VERSION})

set(QT_REQUIRED_VERSION "6.9.0")
set(KF_MIN_VERSION "6.19.0")
set(QT6KEYCHAIN_LIB_VERSION "0.15.0")
set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
include(FeatureSummary)
find_package(ECM ${KF_MIN_VERSION} NO_MODULE)
set_package_properties(
    ECM
    PROPERTIES
        TYPE
            REQUIRED
        DESCRIPTION
            "Extra CMake Modules."
        URL
            "https://invent.kde.org/frameworks/extra-cmake-modules"
)

set(CMAKE_MODULE_PATH
    ${CMAKE_CURRENT_SOURCE_DIR}/cmake/
    ${ECM_MODULE_PATH}
)

include(KDEInstallDirs)
include(KDEFrameworkCompilerSettings NO_POLICY_SCOPE)
include(KDECMakeSettings)
include(ECMGenerateQDoc)

include(ECMGenerateExportHeader)
include(ECMSetupVersion)
include(ECMGenerateHeaders)
include(CMakePackageConfigHelpers)
include(ECMAddTests)
include(ECMDeprecationSettings)
include(CMakeDependentOption)
include(ECMQtDeclareLoggingCategory)
include(ECMCheckOutboundLicense)

file(
    GLOB_RECURSE ALL_SOURCE_FILES
    textaddonswidgets/*.cpp
    textaddonswidgets/*.h
    textedittexttospeech/*.cpp
    textedittexttospeech/*.h
    textspeechtotext/*.cpp
    textspeechtotext/*.h
    textautocorrection/*.cpp
    textautocorrection/*.h
    textemoticons/*.cpp
    textemoticons/*.h
    texttranslator/*.cpp
    texttranslator/*.h
    textcustomeditor/*.cpp
    textcustomeditor/*.h
    textgrammarcheck/*.cpp
    textgrammarcheck/*.h
    textutils/*.cpp
    textutils/*.h
    textautogeneratetext/*.h
    textautogeneratetext/*.cpp
)
ecm_check_outbound_license(LICENSES GPL-2.0-only  FILES ${ALL_SOURCE_FILES})

set(PRE_COMMIT_INSTALLED FALSE)
if(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR})
    if(EXISTS "${PROJECT_SOURCE_DIR}/.git/hooks/pre-commit")
        file(READ "${PROJECT_SOURCE_DIR}/.git/hooks/pre-commit" FILE_CONTENTS)
        string(FIND "${FILE_CONTENTS}" "File generated by pre-commit" INDEX)
        if(${INDEX} GREATER_EQUAL 0)
            set(PRE_COMMIT_INSTALLED TRUE)
        endif()
    endif()
endif()
if(NOT ${PRE_COMMIT_INSTALLED})
    find_program(PRE_COMMIT_EXECUTABLE pre-commit)
    if(PRE_COMMIT_EXECUTABLE)
        message(STATUS "This module has pre-commit support. pre-commit binary was found. Installing pre-commit hook")
        execute_process(
            COMMAND
                "${PRE_COMMIT_EXECUTABLE}" install -f
            ERROR_QUIET
            RESULT_VARIABLE _precommitresult
            WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
        )
        if(_precommitresult EQUAL 0)
            message(STATUS "Pre-commit installed.")
        else()
            message(
                WARNING
                "Error during installing pre-commit hook: ${PRE_COMMIT_EXECUTABLE} project source dir ${PROJECT_SOURCE_DIR}"
            )
            include(KDEGitCommitHooks)
            include(KDEClangFormat)
            kde_clang_format(${ALL_CLANG_FORMAT_SOURCE_FILES})
        endif()
    else()
        message(WARNING "This module has pre-commit support. pre-commit binary was not found.")
        include(KDEGitCommitHooks)
        include(KDEClangFormat)
        kde_clang_format(${ALL_CLANG_FORMAT_SOURCE_FILES})
    endif()
endif()

option(BUILD_QUICK "Build with QtQuick support (experimental)" OFF)
if(BUILD_QUICK)
    include(ECMQmlModule)
endif()

find_package(
    Qt6
    ${QT_REQUIRED_VERSION}
    CONFIG
    REQUIRED
        Widgets
        Core
        Network
        Xml
        Sql
)

if(NOT WIN32 AND NOT APPLE)
    find_package(Qt6DBus ${QT_REQUIRED_VERSION} CONFIG REQUIRED)
endif()

find_package(
    KF6
    ${KF_MIN_VERSION}
    REQUIRED
    COMPONENTS
        I18n
        ColorScheme
        Archive
        CoreAddons
        Sonnet
        Service
        SyntaxHighlighting
        WidgetsAddons
        TextWidgets
        ItemViews
        IconThemes
)
option(BUILD_KIO_SUPPORT "Build with KIO support" ON)
add_feature_info(
    KIO_SUPPORT
    ${BUILD_KIO_SUPPORT}
    "Build with KIO support"
)

if(${BUILD_KIO_SUPPORT})
    find_package(KF6 ${KF_MIN_VERSION} REQUIRED COMPONENTS KIO)
endif()

option(
    ENABLE_WARN_OUTDATED
    "Warn users if using an old version; if you long term supported distribution, you might want to set this to off"
    ON
)

option(BUILD_DESIGNERPLUGIN "Build plugin for Qt Designer" ON)
add_feature_info(
    DESIGNERPLUGIN
    ${BUILD_DESIGNERPLUGIN}
    "Build plugin for Qt Designer"
)

option(ENABLE_PCH "Enable Precompiled Headers support" ON)

find_package(Qt6Keychain ${QT6KEYCHAIN_LIB_VERSION} CONFIG)
set_package_properties(
    Qt6Keychain
    PROPERTIES
        DESCRIPTION
            "Provides support for secure credentials storage"
        URL
            "https://github.com/frankosterfeld/qtkeychain"
        TYPE
            REQUIRED
)

if(BUILD_TESTING)
    find_package(Qt6 ${REQUIRED_QT_VERSION} REQUIRED COMPONENTS Test)
    add_definitions(-DBUILD_TESTING)
endif()

option(USE_UNITY_CMAKE_SUPPORT "Use UNITY cmake support (speedup compile time)" OFF)

set(COMPILE_WITH_UNITY_CMAKE_SUPPORT OFF)
if(USE_UNITY_CMAKE_SUPPORT)
    set(COMPILE_WITH_UNITY_CMAKE_SUPPORT ON)
endif()

option(BERGAMOT_TRANSLATOR_PLUGIN "Build bergamot translator plugin" OFF)
add_feature_info(
    BERGAMOT_TRANSLATOR_PLUGIN
    ${BERGAMOT_TRANSLATOR_PLUGIN}
    "Bergamot translator plugin (local translation) (Experimental. Need to download bergamot source code first)"
)

option(SPEAK_TO_TEXT_VOSK_PLUGIN "Build vosk speak to text support" OFF)
add_feature_info(
    SPEAK_TO_TEXT_VOSK_PLUGIN
    ${SPEAK_TO_TEXT_VOSK_PLUGIN}
    "Vosk speak to text plugin (local text to speak) (Experimental)"
)

option(DISABLE_AUTOTEST_BROKEN_CI "Disable autotest broken on CI (work locally)" OFF)

add_definitions(-DQT_NO_CONTEXTLESS_CONNECT)
ecm_set_disabled_deprecation_versions(
    QT 6.11.0
    KF 6.21.0
)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    # Not setting for GNU due to too many warnings related to private members of base classes or around lambdas
    # see e.g. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56556 or https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79328
    string(APPEND CMAKE_CXX_FLAGS " -Wshadow")
endif()

if(BERGAMOT_TRANSLATOR_PLUGIN)
    find_package(slimt)
    set_package_properties(
        slimt
        PROPERTIES
            DESCRIPTION
                "Add local translation based on Bergamot"
            TYPE
                OPTIONAL
    )
endif()

find_package(Qt6TextToSpeech ${QT_REQUIRED_VERSION} CONFIG)
set_package_properties(
    Qt6TextToSpeech
    PROPERTIES
        DESCRIPTION
            "Add support for text to speech"
        TYPE
            OPTIONAL
)
if(TARGET Qt6::TextToSpeech)
    add_subdirectory(textedittexttospeech)
endif()

add_subdirectory(textaddonswidgets)
add_subdirectory(textautocorrection)
add_subdirectory(textgrammarcheck)
add_subdirectory(texttranslator)
add_subdirectory(textemoticons)
add_subdirectory(textutils)
find_package(Qt6 ${REQUIRED_QT_VERSION} CONFIG REQUIRED MultimediaWidgets)
add_subdirectory(textspeechtotext)
add_subdirectory(textautogeneratetext)

add_subdirectory(textcustomeditor)
ecm_qt_install_logging_categories(
        EXPORT KTEXTADDONS
        FILE ktextaddons.categories
        DESTINATION ${KDE_INSTALL_LOGGINGCATEGORIESDIR}
)

if(DEFINED kde_configure_git_pre_commit_hook)
    kde_configure_git_pre_commit_hook(CHECKS CLANG_FORMAT)
endif()
ki18n_install(po)
feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES)
