include(ECMAddAppIcon)

find_package(KDSingleApplication-qt6 1.0.0 REQUIRED)

if (WIN32)
    find_package(LibSnoreToast)
    set_package_properties(LibSnoreToast PROPERTIES
            URL https://invent.kde.org/libraries/snoretoast
            DESCRIPTION "Command-line application capable of creating Windows Toast notifications"
            TYPE OPTIONAL
    )
endif()


add_library(QSferaGui SHARED)

target_sources(QSferaGui PRIVATE
    aboutdialog.ui
    accountsettings.ui
    generalsettings.ui
    ignorelisteditor.ui
    networksettings.ui
    protocolwidget.ui
    issueswidget.ui
    settingsdialog.ui
    tlserrordialog.ui
    logbrowser.ui
)

target_sources(QSferaGui PRIVATE
    aboutdialog.cpp
    accountmanager.cpp
    accountsettings.cpp

    accountmodalwidget.cpp
    accountmodalwidget.ui

    application.cpp
    fetchserversettings.cpp
    commonstrings.cpp
    connectionvalidator.cpp
    folder.cpp
    folderdefinition.cpp
    folderman.cpp
    folderstatusmodel.cpp
    folderwatcher.cpp
    generalsettings.cpp
    ignorelisteditor.cpp
    lockwatcher.cpp
    logbrowser.cpp
    fonticonmessagebox.cpp
    networkinformation.cpp
    networksettings.cpp
    notifications.cpp
    openfilemanager.cpp
    protocolwidget.cpp
    protocolitem.cpp
    issueswidget.cpp
    activitywidget.cpp
    selectivesyncwidget.cpp
    settingsdialog.cpp
    tlserrordialog.cpp
    syncrunfilelog.cpp
    systray.cpp
    accountstate.cpp
    guiutility.cpp
    elidedlabel.cpp
    translations.cpp
    creds/httpcredentialsgui.cpp
    creds/qmlcredentials.cpp
    updateurldialog.cpp
    updatenotifier.cpp

    models/expandingheaderview.cpp
    models/models.cpp
    models/protocolitemmodel.cpp

    scheduling/syncscheduler.cpp
    scheduling/etagwatcher.cpp

    notifications/systemnotification.cpp
    notifications/systemnotificationmanager.cpp
    notifications/systemnotificationbackend.cpp

    qmlutils.cpp
)

# 3rd party code
target_sources(QSferaGui PRIVATE ../3rdparty/QProgressIndicator/QProgressIndicator.cpp)

add_subdirectory(newwizard)
add_subdirectory(folderwizard)

set_target_properties(QSferaGui PROPERTIES
    OUTPUT_NAME "QSferaGui"
    AUTOUIC ON
    AUTORCC ON
)
# for the generated qml module
target_include_directories(QSferaGui PRIVATE models spaces creds)
target_link_libraries(QSferaGui
    PUBLIC
        Qt::Widgets Qt::Network Qt::Xml Qt::Quick Qt::QuickWidgets Qt::QuickControls2
        libsync
        Qt6Keychain::Qt6Keychain
)

apply_common_target_settings(QSferaGui)
ecm_add_qml_module(QSferaGui
        URI eu.QSfera.gui
        VERSION 1.0
        NAMESPACE OCC
        GENERATE_PLUGIN_SOURCE
        QML_FILES
            qml/AccountBar.qml
            qml/AccountButton.qml
            qml/FolderDelegate.qml
            qml/FolderError.qml

            qml/UpdateUrlDialog.qml

            qml/credentials/Credentials.qml
            qml/credentials/OAuthCredentials.qml

            spaces/qml/SpaceDelegate.qml
            spaces/qml/SpacesView.qml
)

generate_export_header(QSferaGui
        EXPORT_MACRO_NAME QSFERA_GUI_EXPORT
        EXPORT_FILE_NAME qsferaguilib.h
        STATIC_DEFINE QSFERA_BUILT_AS_STATIC
)
add_subdirectory(spaces)


add_subdirectory(socketapi)

target_include_directories(QSferaGui PUBLIC
    ${CMAKE_SOURCE_DIR}/src/3rdparty/QProgressIndicator
    ${CMAKE_CURRENT_BINARY_DIR}
)

IF( APPLE )
    target_sources(QSferaGui PRIVATE
            notifications/macnotifications.mm
            settingsdialog_mac.mm
            guiutility_mac.mm
            folderwatcher_mac.cpp)
    set_source_files_properties(guiutility_mac.mm PROPERTIES COMPILE_DEFINITIONS SOCKETAPI_TEAM_IDENTIFIER_PREFIX="${SOCKETAPI_TEAM_IDENTIFIER_PREFIX}")
elseif( WIN32 )
    target_sources(QSferaGui PRIVATE
            guiutility_win.cpp
            folderwatcher_win.cpp
            navigationpanehelper.cpp
    )
    if (TARGET SnoreToast::SnoreToastActions)
        target_sources(QSferaGui PRIVATE
                notifications/snoretoast.cpp)
        target_compile_definitions(QSferaGui PRIVATE WITH_SNORE_TOAST)
        target_link_libraries(QSferaGui PRIVATE SnoreToast::SnoreToastActions)
    endif()
elseif(UNIX AND NOT APPLE)
    ## handle DBUS for Fdo notifications
    target_link_libraries(QSferaGui PUBLIC Qt::DBus)
    target_sources(QSferaGui PRIVATE
            folderwatcher_linux.cpp
            guiutility_unix.cpp
            notifications/dbusnotifications.cpp
    )
    qt_add_dbus_interface(notifications_dbus_SRCS notifications/org.freedesktop.Notifications.xml dbusnotifications_interface)
    target_sources(QSferaGui PRIVATE ${notifications_dbus_SRCS})
endif()

if(WITH_AUTO_UPDATER)
    add_subdirectory(updater)
    target_compile_definitions(QSferaGui PUBLIC $<BUILD_INTERFACE:WITH_AUTO_UPDATER>)
endif()

add_executable(qsfera main.cpp)
set_target_properties(qsfera PROPERTIES
    OUTPUT_NAME "${APPLICATION_EXECUTABLE}"
    AUTOUIC ON
    AUTORCC ON
)
apply_common_target_settings(qsfera)
target_link_libraries(qsfera PUBLIC QSferaGui QSferaResources KDAB::kdsingleapplication )

MESSAGE(STATUS "QSFERA_SIDEBAR_ICONS: ${APPLICATION_ICON_NAME}: ${QSFERA_SIDEBAR_ICONS}")

ecm_add_app_icon(appIcons ICONS "${QSFERA_ICONS}" SIDEBAR_ICONS "${QSFERA_SIDEBAR_ICONS}" OUTFILE_BASENAME "${APPLICATION_ICON_NAME}")
target_sources(qsfera PRIVATE ${appIcons})

if(NOT APPLE)
    if(WIN32)
        target_sources(qsfera PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/qsfera.exe.manifest)
    else()
        file(GLOB _icons "${OEM_THEME_DIR}/theme/colored/*-${APPLICATION_ICON_NAME}-icon.png")
        foreach(_file ${_icons})
            string(REPLACE "${OEM_THEME_DIR}/theme/colored/" "" _res ${_file})
            string(REPLACE "-${APPLICATION_ICON_NAME}-icon.png" "" _res ${_res})
            install(FILES ${_file} RENAME ${APPLICATION_ICON_NAME}.png DESTINATION ${KDE_INSTALL_DATADIR}/icons/hicolor/${_res}x${_res}/apps)
        endforeach(_file)
    endif()

else()
    target_sources(qsfera PRIVATE ${QSFERA_BUNDLED_RESOURCES})

    set_source_files_properties(
      ${QSFERA_BUNDLED_RESOURCES}
      PROPERTIES
      MACOSX_PACKAGE_LOCATION Resources
      )
  set_target_properties(qsfera PROPERTIES OUTPUT_NAME "${APPLICATION_SHORTNAME}" MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/MacOSXBundleInfo.plist)
endif()

install(TARGETS qsfera QSferaGui ${KDE_INSTALL_TARGETS_DEFAULT_ARGS})
ecm_finalize_qml_module(QSferaGui DESTINATION ${KDE_INSTALL_QMLDIR})

if(UNIX AND NOT APPLE)
    configure_file(${CMAKE_SOURCE_DIR}/qsfera.desktop.in
                   ${CMAKE_CURRENT_BINARY_DIR}/${APPLICATION_EXECUTABLE}.desktop)
    install(FILES  ${CMAKE_CURRENT_BINARY_DIR}/${APPLICATION_EXECUTABLE}.desktop DESTINATION ${KDE_INSTALL_DATADIR}/applications )
endif()
