# SPDX-FileCopyrightText: 2026 Jérôme SONRIER <jsid@emor3j.fr.eu.org>
# SPDX-License-Identifier: GPL-2.0-or-later

# taylet-kde: the KDE user interface. The audio output and the controller of
# the emulation core, which need Qt only, are in ../qt.

set(QT_MIN_VERSION "6.5.0")
set(KF_MIN_VERSION "6.0.0")

find_package(ECM ${KF_MIN_VERSION} REQUIRED NO_MODULE)
list(APPEND CMAKE_MODULE_PATH ${ECM_MODULE_PATH})

# GNUInstallDirs has already been included (by the top-level CMakeLists.txt,
# and by find_package(Qt6)), so CMAKE_INSTALL_<DIR> are set. KDEInstallDirs
# then warns for each directory that it took the value from there; saying
# that we mean it, with KDE_INSTALL_<DIR>, is what it recommends. The values
# do not change.
foreach(dir BINDIR SBINDIR LIBDIR LIBEXECDIR INCLUDEDIR LOCALSTATEDIR
            SHAREDSTATEDIR DATAROOTDIR DATADIR LOCALEDIR MANDIR INFODIR
            SYSCONFDIR)
  if(NOT DEFINED KDE_INSTALL_${dir} AND DEFINED CMAKE_INSTALL_${dir})
    set(KDE_INSTALL_${dir} "${CMAKE_INSTALL_${dir}}")
  endif()
endforeach()

# These modules apply to this directory only: the rest of the project is
# not KDE software and keeps its own settings.
include(KDEInstallDirs)
include(KDECMakeSettings)
include(KDECompilerSettings NO_POLICY_SCOPE)
include(FeatureSummary)

find_package(Qt6 ${QT_MIN_VERSION} REQUIRED COMPONENTS Widgets DBus)
find_package(KF6 ${KF_MIN_VERSION} REQUIRED COMPONENTS
  Config
  ConfigWidgets
  CoreAddons
  DBusAddons
  I18n
  WidgetsAddons
  WindowSystem
  XmlGui)

# The settings (taylet.kcfg, described by tayletsettings.kcfgc) become the
# TayletSettings class, generated in the build directory. The generated files
# are named after the .kcfgc file: tayletsettings.h and tayletsettings.cpp.
kconfig_add_kcfg_files(taylet_kde_settings tayletsettings.kcfgc)

# tayletui.rc (menus and toolbar of the window) is embedded in the executable
# (taylet.qrc): it is found whatever the install prefix, and the program
# works from the build directory.
add_executable(taylet-kde
  main.cpp
  mainwindow.cpp
  mainwindow.h
  mpris.cpp
  mpris.h
  settingsdialog.cpp
  settingsdialog.h
  taylet.qrc
  ${taylet_kde_settings})

target_include_directories(taylet-kde PRIVATE ${CMAKE_CURRENT_BINARY_DIR})

target_compile_definitions(taylet-kde PRIVATE
  TRANSLATION_DOMAIN="taylet"
  TAYLET_VERSION="${PROJECT_VERSION}"
  TAYLET_APP_ID="${TAYLET_APP_ID}")

target_link_libraries(taylet-kde PRIVATE
  taylet-qt
  Qt6::DBus
  Qt6::Widgets
  KF6::ConfigCore
  KF6::ConfigGui
  KF6::ConfigWidgets
  KF6::CoreAddons
  KF6::DBusAddons
  KF6::I18n
  KF6::WidgetsAddons
  KF6::WindowSystem
  KF6::XmlGui)

install(TARGETS taylet-kde ${KDE_INSTALL_TARGETS_DEFAULT_ARGS})
# The description of the settings (used by tools which show or edit them).
install(FILES taylet.kcfg DESTINATION ${KDE_INSTALL_KCFGDIR})
# The menu entry, and the MIME type which lets the desktop offer taylet-kde
# for .ay files (shared-mime-info does not have one).
install(PROGRAMS org.eu.fr.emor3j.taylet.desktop DESTINATION ${KDE_INSTALL_APPDIR})
install(FILES org.eu.fr.emor3j.taylet.xml DESTINATION ${KDE_INSTALL_MIMEDIR})
# The description of the application for the software centers (AppStream).
install(FILES org.eu.fr.emor3j.taylet.metainfo.xml DESTINATION ${KDE_INSTALL_METAINFODIR})

# The menu entry and the AppStream file are checked by the tests when the
# tools are installed (desktop-file-utils, appstream); without them these two
# tests do not exist.
if(TAYLET_BUILD_TESTS)
  find_program(TAYLET_DESKTOP_FILE_VALIDATE desktop-file-validate)
  if(TAYLET_DESKTOP_FILE_VALIDATE)
    add_test(NAME desktop-file
      COMMAND ${TAYLET_DESKTOP_FILE_VALIDATE} ${CMAKE_CURRENT_SOURCE_DIR}/org.eu.fr.emor3j.taylet.desktop)
  endif()
  find_program(TAYLET_APPSTREAMCLI appstreamcli)
  if(TAYLET_APPSTREAMCLI)
    add_test(NAME appstream-metainfo
      COMMAND ${TAYLET_APPSTREAMCLI} validate --no-net ${CMAKE_CURRENT_SOURCE_DIR}/org.eu.fr.emor3j.taylet.metainfo.xml)
  endif()
endif()
# The icon (also embedded in the executable, see main.cpp). Its name is the
# application ID, as the Icon of the menu entry.
install(FILES org.eu.fr.emor3j.taylet.svg DESTINATION ${KDE_INSTALL_ICONDIR}/hicolor/scalable/apps)

# The translations: po/<language>/taylet.po become taylet.mo, installed with
# the other locale data. The template (po/taylet.pot) is made again by
# po/extract-messages.sh. A missing translation is not an error: the
# messages are in English then.
ki18n_install(${PROJECT_SOURCE_DIR}/po)

feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES)
