# linscp, the Qt shell (P2). Qt's translation units and core's never meet:
# CoreBridge.cpp is the only file that includes the VCL shim, and the only one
# the Qt sources reach core through.

# The Qt side, moc's output included. On Linux Qt is built with a 4-byte
# wchar_t and its headers inline wide conversions (QString::fromWCharArray,
# std::wstring), so these units must have Qt's wchar_t, not the -fshort-wchar
# every core unit has. Nothing wide crosses CoreBridge.h. Windows' wchar_t is
# 2 bytes either way. See ADR 0001.
add_library(linscp-qt-ui STATIC EXCLUDE_FROM_ALL MainWindow.cpp AboutDialog.cpp EditorWindow.cpp QtFrontend.cpp QueryDialog.cpp
  PromptDialog.cpp Language.cpp SiteManager.cpp SiteAdvanced.cpp ColorMenu.cpp
  FileList.cpp RemotePanel.cpp LocalPanel.cpp
  TransferDialog.cpp QueuePanel.cpp SynchronizeDialog.cpp PreferencesDialog.cpp TransferSettingsDialog.cpp
  RightsFrame.cpp PropertiesDialog.cpp CreateDirectoryDialog.cpp LinkDialog.cpp RemoteTransferDialog.cpp OpenDirectoryDialog.cpp LocationProfilesDialog.cpp FindFilesDialog.cpp EditMaskDialog.cpp ServerInfoDialog.cpp RtfText.cpp GenerateUrlDialog.cpp ConsoleDialog.cpp SiteRawDialog.cpp ImportSitesDialog.cpp CustomCommandDialog.cpp KeepUpToDateDialog.cpp LicenseDialog.cpp CleanupDialog.cpp TagDialog.cpp ShortCutDialog.cpp EditorPreferencesDialog.cpp Integration.cpp SaveWorkspaceDialog.cpp
  CopyLocalDialog.cpp KeyVerify.cpp SaveSiteDialog.cpp IconSet.cpp PanelBands.cpp WindowParams.cpp HintButton.cpp SingleInstance.cpp MasterPasswordDialog.cpp SshHostCADialog.cpp DirectoryTree.cpp Theme.cpp ExtensionOptionsDialog.cpp DragExtClient.cpp ExplorerInterface.cpp CoreBridge.h)
set_target_properties(linscp-qt-ui PROPERTIES AUTOMOC ON)
# Its own folder an include directory, so that moc includes the headers by
# name (CMAKE_AUTOMOC_PATH_PREFIX, cmake/reproducible.cmake).
target_include_directories(linscp-qt-ui PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(linscp-qt-ui PUBLIC Qt6::Widgets Qt6::Svg Qt6::Network)
# Update checks (Help > Check for Updates, the Updates button, the periodic
# check against linscp's GitHub releases): on for linscp's own packages, off
# for distributions' and Flathub's, whose package manager updates linscp.
# Integration::UpdateChecksAllowed() also turns them off at run time in a
# Flatpak. PUBLIC: the tests see which way the shell was built.
option(LINSCP_UPDATE_CHECK "Check linscp's releases for updates (off in distribution and Flatpak builds)" ON)
if(LINSCP_UPDATE_CHECK)
  target_compile_definitions(linscp-qt-ui PUBLIC LINSCP_UPDATE_CHECK=1)
else()
  target_compile_definitions(linscp-qt-ui PUBLIC LINSCP_UPDATE_CHECK=0)
endif()
if(WIN32)
  # Integration's shortcuts (IShellLink), local files' properties (the shell's).
  target_link_libraries(linscp-qt-ui PUBLIC ole32 uuid shell32 advapi32 user32 propsys)
  # The drag & drop shell extension's protocol (DragExt.h), as WinSCP's.
  target_include_directories(linscp-qt-ui PRIVATE "${CMAKE_SOURCE_DIR}/3rd-party/winscp/source/dragext")
else()
  # Local files' properties: the desktop's file manager (FileManager1).
  find_package(Qt6 REQUIRED COMPONENTS DBus)
  target_link_libraries(linscp-qt-ui PUBLIC Qt6::DBus)
endif()
if(NOT WIN32)
  target_compile_options(linscp-qt-ui PRIVATE -fno-short-wchar)
endif()

# The core side: the bridge (CoreBridge.cpp, Session.cpp) and the shell's
# half of core's Interface.h (QtInterface.cpp), in place of the headless one.
# WinSCP's custom commands, compiled as they are from slices of
# windows/WinConfiguration.cpp and GUITools.cpp, and its keep-up-to-date
# controller (ADR 0014).
set(LINSCP_SLICE_SOURCES "${LINSCP_GEN_SLICES}/windows/CustomCommandType.cpp"
  "${LINSCP_GEN_SLICES}/windows/LocalCustomCommand.cpp" "${LINSCP_GEN_SLICES}/windows/CustomShortCuts.cpp"
  "${LINSCP_GEN_SLICES}/windows/SynchronizeController.cpp" "${LINSCP_GEN_SLICES}/windows/PuttyTools.cpp"
  "${LINSCP_GEN_SLICES}/windows/EditorPreferences.cpp")
# Windows only: the protocols' registration and the search path (on POSIX
# the shell's XDG integration stands in).
if(WIN32)
  list(APPEND LINSCP_SLICE_SOURCES "${LINSCP_GEN_SLICES}/windows/SetupIntegration.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/SetupSlice.cpp")
endif()
set_source_files_properties(${LINSCP_SLICE_SOURCES} PROPERTIES GENERATED TRUE)
add_library(linscp-qt-core STATIC EXCLUDE_FROM_ALL CoreBridge.cpp QtInterface.cpp Session.cpp Sites.cpp
  Keys.cpp ShellConfiguration.cpp TransferSettings.cpp GenerateCode.cpp CustomCommandsSlice.cpp CustomCommands.cpp DiscMon.cpp PuttySlice.cpp ${LINSCP_SLICE_SOURCES})
add_dependencies(linscp-qt-core transform)
target_link_libraries(linscp-qt-core PUBLIC linscp-runtime-base linscp-gui)
# The generated GUIConfiguration.h before core's include path, which has
# upstream's own windows/ (ADR 0014).
target_include_directories(linscp-qt-core BEFORE PUBLIC "${LINSCP_GEN}/source/windows")
target_include_directories(linscp-qt-core PUBLIC "${LINSCP_GEN_SLICES}/windows" "${CMAKE_CURRENT_SOURCE_DIR}")
# linscp's own version (the project's); core keeps WinSCP's.
target_compile_definitions(linscp-qt-core PRIVATE LINSCP_VERSION="${PROJECT_VERSION}")

add_executable(linscp WIN32 EXCLUDE_FROM_ALL main.cpp)
if(WIN32)
  target_sources(linscp PRIVATE linscp.rc)
endif()
set_target_properties(linscp PROPERTIES AUTOMOC ON)
target_include_directories(linscp PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
if(NOT WIN32)
  target_compile_options(linscp PRIVATE -fno-short-wchar)
endif()
target_link_libraries(linscp PRIVATE linscp-qt-ui linscp-qt-core)

# Translations (P2-09, doc/qt/translations.md).
#
# Core's texts: WinSCP's own translations, one file per language generated
# into Translations/ beside the executable, where upstream's GUI looks for its
# own (port/tools/gen_translations.py),
# which CoreBridge::SetLanguage loads into the shim.
file(GLOB _winscp_translations CONFIGURE_DEPENDS "${LINSCP_UPSTREAM}/translations/*.ini")
set(_translations_dir "${CMAKE_CURRENT_BINARY_DIR}/Translations")
set(_translations_stamp "${CMAKE_CURRENT_BINARY_DIR}/translations.stamp")
add_custom_command(
  OUTPUT "${_translations_stamp}"
  COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/port/tools/gen_translations.py"
          --src "${LINSCP_UPSTREAM}/source" --translations "${LINSCP_UPSTREAM}/translations"
          --out "${_translations_dir}"
  COMMAND "${CMAKE_COMMAND}" -E touch "${_translations_stamp}"
  DEPENDS ${_winscp_translations} "${CMAKE_SOURCE_DIR}/port/tools/gen_translations.py"
          "${CMAKE_SOURCE_DIR}/port/tools/gen_resources.py"
  COMMENT "Generating core's translations from WinSCP's"
  VERBATIM)
add_custom_target(core-translations DEPENDS "${_translations_stamp}")
add_dependencies(linscp core-translations)
set(LINSCP_TRANSLATIONS_DIR "${_translations_dir}" PARENT_SCOPE)

# The shell's own texts: Qt Linguist's .ts files, one per language
# (translations/linscp_<locale>.ts), compiled into the UI library -- so the
# tests that link it have them too. The build machine's lupdate and lrelease
# run them, the same for both targets: MSYS2 has Qt's Linguist CMake package
# only in its qt6-tools. Run the update_translations target after changing
# the shell's texts; ctest translations_current fails while a .ts lags behind.
# The names: Fedora's (-qt6), openSUSE's (6), and Qt's own.
find_program(LINSCP_LUPDATE NAMES lupdate-qt6 lupdate6 lupdate HINTS /usr/lib/qt6/bin NO_CMAKE_FIND_ROOT_PATH REQUIRED)
find_program(LINSCP_LRELEASE NAMES lrelease-qt6 lrelease6 lrelease HINTS /usr/lib/qt6/bin NO_CMAKE_FIND_ROOT_PATH REQUIRED)
file(GLOB _shell_ts CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/translations/linscp_*.ts")
file(GLOB _shell_sources CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
set(_shell_qm)
foreach(_ts ${_shell_ts})
  get_filename_component(_name "${_ts}" NAME_WE)
  set(_qm "${CMAKE_CURRENT_BINARY_DIR}/i18n/${_name}.qm")
  add_custom_command(OUTPUT "${_qm}"
    COMMAND "${LINSCP_LRELEASE}" -silent "${_ts}" -qm "${_qm}"
    DEPENDS "${_ts}" VERBATIM)
  list(APPEND _shell_qm "${_qm}")
endforeach()
qt_add_resources(linscp-qt-ui shell_translations
  PREFIX /i18n BASE "${CMAKE_CURRENT_BINARY_DIR}/i18n" FILES ${_shell_qm})
# linscp's own icon (assets/app/linscp.svg, rendered by port/tools/make_icon.py).
qt_add_resources(linscp-qt-ui shell_icons
  PREFIX /icons BASE "${CMAKE_CURRENT_SOURCE_DIR}/icons" FILES "${CMAKE_CURRENT_SOURCE_DIR}/icons/linscp.ico")
# The icon sets (P3-17, port/tools/import_icons.py): one SVG per role, each
# set's license beside them.
file(GLOB _icon_set_files LIST_DIRECTORIES false CONFIGURE_DEPENDS
  "${CMAKE_SOURCE_DIR}/assets/icons/*/*.svg" "${CMAKE_SOURCE_DIR}/assets/icons/*/LICENSE")
qt_add_resources(linscp-qt-ui icon_sets
  PREFIX /iconsets BASE "${CMAKE_SOURCE_DIR}/assets/icons" FILES ${_icon_set_files})
# The License dialog's texts: linscp's own, and Expat's as WinSCP ships it.
set_source_files_properties("${CMAKE_SOURCE_DIR}/LICENSE" PROPERTIES QT_RESOURCE_ALIAS LICENSE)
set_source_files_properties("${LINSCP_UPSTREAM}/source/resource/LicenseExpat.txt" PROPERTIES QT_RESOURCE_ALIAS LicenseExpat.txt)
qt_add_resources(linscp-qt-ui shell_licenses
  PREFIX /licenses FILES "${CMAKE_SOURCE_DIR}/LICENSE" "${LINSCP_UPSTREAM}/source/resource/LicenseExpat.txt")
add_custom_target(update_translations
  COMMAND "${LINSCP_LUPDATE}" -no-obsolete -locations none ${_shell_sources} -ts ${_shell_ts}
  WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  COMMENT "Updating the shell's .ts files from its sources"
  VERBATIM)
set(LINSCP_LUPDATE "${LINSCP_LUPDATE}" PARENT_SCOPE)
set(LINSCP_SHELL_SOURCES ${_shell_sources} PARENT_SCOPE)
set(LINSCP_SHELL_TS ${_shell_ts} PARENT_SCOPE)

# A folder that runs on Windows as it is (port/tools/deploy_windows.py):
#   cmake --build <build> --target deploy   ->   <build>/deploy
# With the build host's .NET SDK (port/tools/fetch_dotnet.py), WinSCP's .NET
# assembly too, for WinSCP's PowerShell extensions (the dotnet folder).
if(WIN32 AND LINSCP_QT_PREFIX)
  set(LINSCP_HOST_DOTNET "${CMAKE_BINARY_DIR}/tools/dotnet/dotnet" CACHE FILEPATH
      "the build host's .NET SDK, to build WinSCP's .NET assembly into the deploy folder")
  set(_deploy_winscpnet)
  set(_deploy_depends linscp linscp-cli core-translations DragExt64)
  if(EXISTS "${LINSCP_HOST_DOTNET}")
    set(_winscpnet "${CMAKE_BINARY_DIR}/winscpnet-net40/bin/WinSCPnet.dll")
    add_custom_command(OUTPUT "${_winscpnet}"
      COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/port/tools/build_winscpnet.py"
              --dotnet "${LINSCP_HOST_DOTNET}" --upstream "${CMAKE_SOURCE_DIR}/3rd-party/winscp"
              --dest "${CMAKE_BINARY_DIR}/winscpnet-net40" --framework net40
      DEPENDS "${CMAKE_SOURCE_DIR}/port/tools/build_winscpnet.py"
      COMMENT "Building WinSCP's .NET assembly (net40)"
      VERBATIM)
    add_custom_target(winscpnet-net40 DEPENDS "${_winscpnet}")
    set(_deploy_winscpnet --winscpnet "${_winscpnet}" --cli "$<TARGET_FILE:linscp-cli>")
    list(APPEND _deploy_depends winscpnet-net40)
  else()
    message(STATUS "No .NET SDK at ${LINSCP_HOST_DOTNET}: the deploy folder goes without WinSCP's .NET assembly")
  endif()
  add_custom_target(deploy
    COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/port/tools/deploy_windows.py"
            --objdump "${LINSCP_TARGET_OBJDUMP}" --qt-prefix "${LINSCP_QT_PREFIX}"
            --dest "${CMAKE_BINARY_DIR}/deploy" --version "${PROJECT_VERSION}"
            --translations "${_translations_dir}"
            ${_deploy_winscpnet}
            --extra "$<TARGET_FILE:DragExt64>"
            "$<TARGET_FILE:linscp>" "$<TARGET_FILE:linscp-cli>"
    DEPENDS ${_deploy_depends}
    COMMENT "Assembling a runnable Windows folder in ${CMAKE_BINARY_DIR}/deploy"
    VERBATIM)
endif()
