# SPDX-License-Identifier: CC0-1.0
# SPDX-FileCopyrightText: none
cmake_minimum_required(VERSION 3.29)
set(PIM_VERSION "6.8.0")

project(Messagelib VERSION ${PIM_VERSION})

set(KF_MIN_VERSION "6.25.0")
set(QT_REQUIRED_VERSION "6.9.0")

set(MESSAGELIB_LIB_VERSION ${PIM_VERSION})
set(AKONADIMIME_LIB_VERSION "6.8.0")

set(AKONADICONTACT_LIB_VERSION "6.8.0")
set(AKONADI_VERSION "6.8.0")
set(GRANTLEETHEME_LIB_VERSION "6.8.0")
set(GRAVATAR_LIB_VERSION "6.8.0")
set(IDENTITYMANAGEMENT_LIB_VERSION "6.8.0")
set(KMAILTRANSPORT_LIB_VERSION "6.8.0")
set(KMBOX_LIB_VERSION "6.8.0")
set(KMIME_LIB_VERSION "6.8.0")
set(KPIMTEXTEDIT_LIB_VERSION "6.8.0")
set(LIBKDEPIM_LIB_VERSION "6.8.0")
set(LIBKLEO_LIB_VERSION "6.8.0")
set(PIMCOMMON_LIB_VERSION "6.8.0")
set(GPGME_REQUIRED_VERSION "2.0.0")
set(AKONADI_SEARCH_VERSION "6.8.0")
set(MIMETREEPARSER_LIB_VERSION "6.8.0")

set(OPENSSL_VERSION "3.0.0")

set(ECM_MIN_VERSION ${KF_MIN_VERSION})

find_package(ECM ${ECM_MIN_VERSION} CONFIG REQUIRED)
set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH})
set(CMAKE_MODULE_PATH
    ${CMAKE_CURRENT_SOURCE_DIR}/cmake/ecmcopy
    ${ECM_MODULE_PATH}
)
include(KDEInstallDirs)
include(KDECMakeSettings)
include(KDECompilerSettings NO_POLICY_SCOPE)
include(ECMInstalledLibraryCheck)
include(ECMGenerateExportHeader)
include(ECMSetupVersion)
include(ECMGenerateHeaders)

file(
    GLOB_RECURSE ALL_CLANG_FORMAT_SOURCE_FILES
    messagecomposer/*.cpp
    messagecomposer/*.h
    messagecore/*.cpp
    messagecore/*.h
    messagelist/*.cpp
    messagelist/*.h
    messageviewer/*.cpp
    messageviewer/*.h
    mimetreeparser/*.cpp
    mimetreeparser/*.h
    templateparser/*.cpp
    templateparser/*.h
    webengineviewer/*.cpp
    webengineviewer/*.h
)
if(EXISTS "${PROJECT_SOURCE_DIR}/.git/")
    set(GIT_SOURCE_TARBALL TRUE)
endif()

if(GIT_SOURCE_TARBALL)
    set(PRE_COMMIT_INSTALLED FALSE)
    include(KDEGitCommitHooks)
    include(KDEClangFormat)
    kde_clang_format(${ALL_CLANG_FORMAT_SOURCE_FILES})
    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})
        function(install_hook executable hook_name)
            message(
                STATUS
                "This module has ${hook_name} support. ${hook_name} binary was found. Installing ${hook_name} hook"
            )
            execute_process(
                COMMAND
                    "${executable}" install -f
                ERROR_QUIET
                RESULT_VARIABLE _precommitresult
                WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
            )
            if(_precommitresult EQUAL 0)
                message(STATUS "${hook_name} installed.")
            else()
                message(
                    WARNING
                    "Error during installing ${hook_name} hook: ${executable} project source dir ${PROJECT_SOURCE_DIR}"
                )
            endif()
        endfunction()

        find_program(PREK_EXECUTABLE prek)
        if(PREK_EXECUTABLE)
            install_hook(${PREK_EXECUTABLE} "Prek")
        else()
            find_program(PRE_COMMIT_EXECUTABLE pre-commit)
            if(PRE_COMMIT_EXECUTABLE)
                install_hook(${PRE_COMMIT_EXECUTABLE} "pre-commit")
            else()
                message(WARNING "This module has pre-commit support. pre-commit binary was not found.")
            endif()
        endif()
    endif()
endif()

include(ECMQtDeclareLoggingCategory)
include(ECMDeprecationSettings)
include(ECMFeatureSummary)
include(ECMAddTests)
include(ECMGenerateQDoc)

include(ECMCheckOutboundLicense)
file(
    GLOB_RECURSE ALL_SOURCE_FILES
    messagecomposer/*.cpp
    messagecomposer/*.h
    messagecore/*.cpp
    messagecore/*.h
    messagelist/*.cpp
    messagelist/*.h
    messageviewer/*.cpp
    messageviewer/*.h
    mimetreeparser/*.cpp
    mimetreeparser/*.h
    templateparser/*.cpp
    templateparser/*.h
    webengineviewer/*.cpp
    webengineviewer/*.h
)
ecm_check_outbound_license(LICENSES GPL-2.0-only  FILES ${ALL_SOURCE_FILES})

set(KTEXTADDONS_MIN_VERSION "2.0.2")

find_package(
    Qt6
    ${QT_REQUIRED_VERSION}
    CONFIG
    REQUIRED
        Gui
        Test
)
find_package(
    Qt6
    ${QT_REQUIRED_VERSION}
    CONFIG
    REQUIRED
        Widgets
        Network
        PrintSupport
        WebEngineWidgets
)

find_package(KF6CoreAddons ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6Codecs ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6I18n ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6NewStuff ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(Gpgmepp ${GPGME_REQUIRED_VERSION} CONFIG REQUIRED)
find_package(QGpgmeQt6 ${GPGME_REQUIRED_VERSION} CONFIG REQUIRED)
find_package(KF6Archive ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6Completion ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6Config ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6Contacts ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6IconThemes ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6ItemViews ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6JobWidgets ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6KIO ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6Service ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6Sonnet ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6TextWidgets ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6WidgetsAddons ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6XmlGui ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6SyntaxHighlighting ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6GuiAddons ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6Notifications ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6ColorScheme ${KF_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6TextTemplate CONFIG REQUIRED)

find_package(KF6CalendarCore ${KF_MIN_VERSION} CONFIG REQUIRED)

find_package(KPim6Akonadi ${AKONADI_VERSION} CONFIG REQUIRED)
find_package(KPim6AkonadiMime ${AKONADIMIME_LIB_VERSION} CONFIG REQUIRED)
find_package(KPim6AkonadiContactWidgets ${AKONADICONTACT_LIB_VERSION} CONFIG REQUIRED)
find_package(KPim6GrantleeTheme ${GRANTLEETHEME_LIB_VERSION} CONFIG REQUIRED)
find_package(KPim6Gravatar ${GRAVATAR_LIB_VERSION} CONFIG REQUIRED)
find_package(KPim6IdentityManagementWidgets ${IDENTITYMANAGEMENT_LIB_VERSION} CONFIG REQUIRED)
find_package(KPim6Libkleo ${LIBKLEO_LIB_VERSION} CONFIG REQUIRED)
find_package(KPim6MailTransport ${KMAILTRANSPORT_LIB_VERSION} CONFIG REQUIRED)
find_package(KPim6Mbox ${KMBOX_LIB_VERSION} CONFIG REQUIRED)
find_package(KF6Mime ${KF_MIN_VERSION} CONFIG)
if(NOT TARGET KF6::Mime)
    find_package(KPim6Mime ${KMIME_LIB_VERSION} CONFIG REQUIRED)
    add_library(KF6::Mime ALIAS KPim6::Mime)
endif()
find_package(KPim6PimCommonAkonadi ${PIMCOMMON_LIB_VERSION} CONFIG REQUIRED)
find_package(KPim6TextEdit ${KPIMTEXTEDIT_LIB_VERSION} CONFIG REQUIRED)
find_package(KPim6Libkdepim ${LIBKDEPIM_LIB_VERSION} CONFIG REQUIRED)

find_package(KF6TextEditTextToSpeech ${KTEXTADDONS_MIN_VERSION} CONFIG)
if(TARGET KF6::TextEditTextToSpeech)
    set(HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT TRUE)
endif()

find_package(KF6TextAddonsWidgets ${KTEXTADDONS_MIN_VERSION} CONFIG)

find_package(KF6TextAutoCorrectionWidgets ${KTEXTADDONS_MIN_VERSION} CONFIG REQUIRED)

find_package(KF6TextUtils ${KTEXTADDONS_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6TextCustomEditor ${KTEXTADDONS_MIN_VERSION} CONFIG REQUIRED)
find_package(KF6TextAddonsWidgets ${KTEXTADDONS_MIN_VERSION} CONFIG REQUIRED)

if(${KF6TextAddonsWidgets_VERSION} VERSION_GREATER_EQUAL 2.0.65)
    set(HAVE_TEXTADDONSWIDGETS_OPENSAVEDFILEFOLDERWIDGET TRUE)
endif()

option(
    FORCE_DISABLE_AKONADI_SEARCH
    "Disable features and API that require akonadi-search, e.g. because xapian isn't available"
    OFF
)
if(UNIX)
    if(NOT FORCE_DISABLE_AKONADI_SEARCH)
        find_package(KPim6AkonadiSearch ${AKONADI_SEARCH_VERSION} CONFIG REQUIRED)
        set_package_properties(
            KPim6AkonadiSearch
            PROPERTIES
                DESCRIPTION
                    "The Akonadi Search libraries"
                URL
                    "https://invent.kde.org/pim/akonadi-search"
                TYPE
                    REQUIRED
                PURPOSE
                    "Provides search capabilities in KMail and Akonadi"
        )
    endif()
else()
    if(NOT FORCE_DISABLE_AKONADI_SEARCH)
        find_package(KPim6AkonadiSearch ${AKONADI_SEARCH_VERSION} CONFIG)
        set_package_properties(
            KPim6AkonadiSearch
            PROPERTIES
                DESCRIPTION
                    "The Akonadi Search libraries"
                URL
                    "https://invent.kde.org/pim/akonadi-search"
                TYPE
                    OPTIONAL
                PURPOSE
                    "Provides search capabilities in KMail and Akonadi"
        )
    endif()
endif()

if(NOT KPim6AkonadiSearch_FOUND)
    set(FORCE_DISABLE_AKONADI_SEARCH TRUE)
endif()

find_package(OpenSSL ${OPENSSL_VERSION} REQUIRED)

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()
ecm_set_disabled_deprecation_versions(QT 6.12.0 KF 6.28.0 )
add_definitions(-DQT_NO_CONTEXTLESS_CONNECT)
if(BUILD_TESTING)
    set(ECM_TEST_NAME_PREFIX_STRATEGY "PATH")
    add_definitions(-DBUILD_TESTING)
endif()

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

option(ENABLE_PCH "Enable Precompiled Headers support" ON)

set(COMPILE_WITH_UNITY_CMAKE_SUPPORT OFF)
if(USE_UNITY_CMAKE_SUPPORT)
    set(COMPILE_WITH_UNITY_CMAKE_SUPPORT ON)
    add_definitions(-DCOMPILE_WITH_UNITY_CMAKE_SUPPORT)
endif()

include_directories(
    ${CMAKE_CURRENT_BINARY_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}
)

add_subdirectory(mimetreeparser)
add_subdirectory(messageviewer)
add_subdirectory(templateparser)
add_subdirectory(messagecomposer)
add_subdirectory(messagecore)
add_subdirectory(messagelist)
add_subdirectory(webengineviewer)

ecm_qt_install_logging_categories(
    EXPORT MESSAGELIB
    FILE messagelib.categories
    DESTINATION ${KDE_INSTALL_LOGGINGCATEGORIESDIR}
)

if(GIT_SOURCE_TARBALL)
    if(DEFINED kde_configure_git_pre_commit_hook)
        kde_configure_git_pre_commit_hook(CHECKS CLANG_FORMAT)
    endif()
endif()

ki18n_install(po)
ecm_feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES)
