# SPDX-License-Identifier: BSD-3-Clause
# SPDX-FileCopyrightText: 2021 Mathis Brüchert <mbb-mail@gmx.de>

add_library(marknote_static STATIC)

qt_policy(SET QTP0004 NEW)

option(USE_UNITY_CMAKE_SUPPORT "Use UNITY CMake support" OFF)

if(USE_UNITY_CMAKE_SUPPORT)
    message(STATUS "UNITY CMake support is enabled")
    set_target_properties(marknote_static PROPERTIES UNITY_BUILD ON)
endif()

target_compile_options(marknote_static PRIVATE
    $<$<CXX_COMPILER_ID:MSVC>:/bigobj>
)

target_include_directories(marknote_static PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/mdoptions
    ${CMAKE_CURRENT_SOURCE_DIR}/mdtreemodel
    ${CMAKE_CURRENT_SOURCE_DIR}/commands
    ${CMAKE_CURRENT_SOURCE_DIR}/mdinlineparser
)

set_source_files_properties(qml/EditorActions.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)

ecm_add_qml_module(marknote_static
    URI org.kde.marknote
    GENERATE_PLUGIN_SOURCE
    QML_FILES
        qml/Main.qml
        qml/MainEditor.qml
        qml/NotesPage.qml
        qml/NoteInlineStatusBar.qml
        qml/NoteLinkDialog.qml
        qml/NoteMetadataDialog.qml
        qml/WelcomePage.qml
        # Notebook
        qml/NotebookContextMenu.qml
        qml/NotebookDelegate.qml
        qml/NotebookDeleteAction.qml
        qml/NotebookMetadataDialog.qml
        qml/NotebookDeleteDialog.qml
        qml/ImportMaildirDialog.qml
        # Editor
        qml/EditPage.qml
        qml/RichEditPage.qml
        qml/RawEditPage.qml
        qml/LinkDialog.qml
        qml/TableDialog.qml
        qml/SketchDialog.qml
        qml/TextFieldContextMenu.qml
        qml/TocDrawer.qml
        # Components
        qml/components/EmojierPopup.qml
        qml/components/SlashMenuPopup.qml
        # Native menu
        qml/NativeHelpMenu.qml
        qml/NativeWindowMenu.qml
        qml/NativeFileMenu.qml
        qml/NativeEditMenu.qml
        qml/GlobalMenuBar.qml
        # Block Editor
        qml/EditorActions.qml
        qml/BlockContextMenu.qml
        qml/BlockView.qml
        qml/BlockTemplate.qml
        qml/BlockChooser.qml
        qml/BlockHeading.qml
        qml/BlockParagraph.qml
        qml/BlockQuote.qml
        qml/BlockList.qml
        qml/BlockListItem.qml
        qml/BlockHorizontalLine.qml
        qml/BlockCode.qml
        qml/BlockTable.qml
        qml/BlockText.qml
        qml/BlockTableCell.qml
    SOURCES
        documenthandler.cpp documenthandler.h
        rich_documenthandler.cpp rich_documenthandler.h
        raw_documenthandler.cpp raw_documenthandler.h
        notesmodel.cpp notesmodel.h
        notebooksmodel.cpp notebooksmodel.h
        navigationcontroller.cpp navigationcontroller.h
        application.cpp application.h
        colorschemer.cpp colorschemer.h
        maildirimport.cpp maildirimport.h
        sketchhistory.cpp sketchhistory.h
        tocmodel.cpp tocmodel.h
        emojiermodel.h emojiermodel.cpp
        emojierproxymodel.h emojierproxymodel.cpp
        commandbarhelper.cpp commandbarhelper.h
        asyncdocbuilder/asyncdocbuilder.h asyncdocbuilder/asyncdocbuilder.cpp
        mdoptions/mdoptions.h
        mdtreemodel/treeitem.h mdtreemodel/treeitem.cpp
        mdtreemodel/mdtreemodel.h mdtreemodel/mdtreemodel.cpp
        mdtreemodel/mddatagenerator.h mdtreemodel/mddatagenerator.cpp
        mdserializer/mdserializer.h mdserializer/mdserializer.cpp
        mdinlineparser/mdinlineparser.h mdinlineparser/mdinlineparser.cpp
        commands/commandmanager.h commands/commandmanager.cpp
        commands/mergewithpreviousblock.h commands/mergewithpreviousblock.cpp
        commands/edittext.h commands/edittext.cpp
        commands/editcelltext.h commands/editcelltext.cpp
        commands/editcode.h commands/editcode.cpp
        commands/parseblock.h commands/parseblock.cpp
        commands/splitblock.h commands/splitblock.cpp
        commands/splitlistitem.h commands/splitlistitem.cpp
        commands/deindentlistitem.h commands/deindentlistitem.cpp
        commands/indentlistitem.h commands/indentlistitem.cpp
        commands/removeblockquote.h commands/removeblockquote.cpp
        commands/removefromlist.h commands/removefromlist.cpp
        commands/moveoutsideblockquote.h commands/moveoutsideblockquote.cpp
        commands/transformtoblockquote.h commands/transformtoblockquote.cpp
        commands/transformtolist.h commands/transformtolist.cpp
        commands/transformtochecklist.h commands/transformtochecklist.cpp
        commands/changelisttypecommand.h commands/changelisttypecommand.cpp
        commands/moveblockcommand.h commands/moveblockcommand.cpp
        commands/splitcode.h commands/splitcode.cpp
        commands/insertparagraphbelow.h commands/insertparagraphbelow.cpp
        commands/insertrowintablecommand.h commands/insertrowintablecommand.cpp
        commands/insertcolumnintablecommand.h commands/insertcolumnintablecommand.cpp
        commands/removerowfromtablecommand.h commands/removerowfromtablecommand.cpp
        commands/removecolumnfromtablecommand.h commands/removecolumnfromtablecommand.cpp
        commands/removeblockscommand.h commands/removeblockscommand.cpp
)

kconfig_target_kcfg_file(marknote_static
    FILE marknotesettings.kcfg
    CLASS_NAME Config
    MUTATORS
    GENERATE_PROPERTIES
    GENERATE_MOC
    DEFAULT_VALUE_GETTERS
    PARENT_IN_CONSTRUCTOR
    QML_REGISTRATION
    SINGLETON
)

add_subdirectory(settings)

target_link_libraries(marknote_static
    PUBLIC
        Qt::Core
        Qt::Gui
        Qt::Qml
        Qt::Quick
        Qt::QuickControls2
        Qt::Svg
	md4qt::md4qt
        KF6::I18n
        KF6::ConfigCore
        KF6::ConfigGui
        KF6::CoreAddons
        KF6::IconThemes
        KF6::ColorScheme
        KF6::Mime
        KF6::ItemModels
        KirigamiApp
        KirigamiAddonsStatefulApp
        settingsplugin
)

option(USE_PRECOMPILED_HEADERS "Use precompiled headers" OFF)

if(USE_PRECOMPILED_HEADERS)
    message(STATUS "PCH is enabled")
    target_precompile_headers(marknote_static PRIVATE
        <QObject>
        <QQmlEngine>
        <QQuickItem>
        <QAbstractListModel>
        <QString>
        <QList>
        <QUrl>
        <KLocalizedString>
        <QVariant>
        <QDateTime>
        <QPointer>
        <QHash>
        <QTextDocument>
        <QTextCursor>
        <QTextTableCell>
        <QTextBlock>
        <QQuickTextDocument>
    )
endif()

if (TARGET KF6::I18nQml)
    target_link_libraries(marknote_static PUBLIC KF6::I18nQml)
endif()

if (WITH_KRUNNER)
    target_sources(marknote_static PRIVATE runner.cpp runner.h)
    target_link_libraries(marknote_static
        PUBLIC
            Qt6::DBus
            KF6::WindowSystem
    )
    target_compile_definitions(marknote_static PUBLIC HAVE_KRUNNER)
endif()

if (TARGET md4c::md4c-html)
    target_link_libraries(marknote_static PUBLIC md4c::md4c-html)
endif()

add_executable(marknote main.cpp)

target_link_libraries(marknote
    PRIVATE
        marknote_static
        marknote_staticplugin
)

if (NOT ANDROID)
    target_link_libraries(marknote PRIVATE Qt::Widgets)
endif()

# Icons
file(GLOB ICONS_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/../icons/*-apps-org.kde.marknote.png")
ecm_add_app_icon(marknote ICONS
    "${CMAKE_CURRENT_SOURCE_DIR}/../icons/sc-apps-org.kde.marknote.svg"
    ${ICONS_SRCS}
)

# Platform Specific
if (APPLE)
    set_target_properties(marknote PROPERTIES
        MACOSX_BUNDLE_DISPLAY_NAME "Marknote"
        MACOSX_BUNDLE_BUNDLE_NAME "Marknote"
        MACOSX_BUNDLE_LONG_VERSION_STRING "Marknote ${RELEASE_SERVICE_VERSION}"
        MACOSX_BUNDLE_SHORT_VERSION_STRING "${RELEASE_SERVICE_VERSION_MAJOR}.${RELEASE_SERVICE_VERSION_MINOR}"
        MACOSX_BUNDLE_BUNDLE_VERSION "${RELEASE_SERVICE_VERSION}"
        MACOSX_BUNDLE_GUI_IDENTIFIER "org.kde.marknote"
        MACOSX_BUNDLE_COPYRIGHT "2002-2024 The Marknote Developers")
endif()

if (WITH_KCRASH)
   target_link_libraries(marknote PRIVATE KF6::Crash)
    target_compile_definitions(marknote_static PUBLIC HAVE_KCRASH)
endif()

if (BUILD_TESTING)
    add_subdirectory(autotests)
endif()

install(TARGETS marknote ${KDE_INSTALL_TARGETS_DEFAULT_ARGS})
install(FILES marknotesettings.kcfg DESTINATION ${KDE_INSTALL_KCFGDIR})
