find_package(Qt6 COMPONENTS WebEngineCore WebEngineWidgets)
# 检查系统编译环境是否有webengine库
if(Qt6_WebEngineCore_FOUND AND Qt6_WebEngineWidgets_FOUND)
    message(STATUS "Target could not found webengine" )
else()
    add_definitions(-DARCHITECTUREISSW64)
    message(STATUS "Target found webengine" )
endif()
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets DBus Xml Network Svg Multimedia MultimediaWidgets Sql)
find_package(PkgConfig REQUIRED)
find_package(KF6NetworkManagerQt REQUIRED)
pkg_check_modules(KWAYLANDCLIENT REQUIRED KWaylandClient)

pkg_check_modules(GIOUNIX2 REQUIRED gio-unix-2.0)
pkg_check_modules(GLIB2 REQUIRED glib-2.0 gio-2.0)

find_package(X11 REQUIRED)
pkg_check_modules(KYSDKSYSTIME REQUIRED kysdk-systime)
pkg_check_modules(KYSDKWINDOWHELPER REQUIRED kysdk-ukuiwindowhelper)
if (KYSDK_UKUIWINDOWHELPER_FOUND)
    include_directories(${KYSDK_UKUIWINDOWHELPER_INCLUDE_DIRS})
endif()
pkg_check_modules(KYSDKQTWIDGETS kysdk-qtwidgets)
pkg_check_modules(XTST REQUIRED xtst)
pkg_check_modules(XCB REQUIRED xcb)
pkg_check_modules(QGS REQUIRED gsettings-qt6)
pkg_check_modules(MMIX REQUIRED libmatemixer)
pkg_check_modules(kylin-nm-base REQUIRED kylin-nm-base)
pkg_check_modules(LIGHTDM-GOBJ-1 REQUIRED liblightdm-gobject-1)
pkg_check_modules(LIBSYSTEMD REQUIRED libsystemd)
pkg_check_modules(LIBXRANDR REQUIRED xrandr)

# 查找pam动态库全路径并缓存到PAM_LIBRARIES变量
find_library(PAM_LIBRARIES pam)

include_directories(${PROJECT_BINARY_DIR})
include_directories(${PROJECT_SOURCE_DIR}/src)
include_directories(${PROJECT_SOURCE_DIR}/src/common)
include_directories(${PROJECT_SOURCE_DIR}/src/widgets)
include_directories(${PROJECT_SOURCE_DIR}/src/QtSingleApplication)
include_directories(${PROJECT_SOURCE_DIR}/src/dbusifs)
include_directories(${PROJECT_SOURCE_DIR}/VirtualKeyboard/src)
include_directories(${PROJECT_SOURCE_DIR}/screensaver)
include_directories(${PROJECT_SOURCE_DIR}/src/liblightdm-qt)

aux_source_directory(${PROJECT_SOURCE_DIR}/src/common COMMON)
aux_source_directory(${PROJECT_SOURCE_DIR}/src/widgets WIDGEGS)
aux_source_directory(${PROJECT_SOURCE_DIR}/src/lock-dialog LOCK_DIALOG)
aux_source_directory(${PROJECT_SOURCE_DIR}/src/lock-backend LOCK_BACKEND)
aux_source_directory(${PROJECT_SOURCE_DIR}/src/lock-command LOCK_COMMAND)
aux_source_directory(${PROJECT_SOURCE_DIR}/src/lock-checkpasswd LOCK_CHECKPASSWD)
aux_source_directory(${PROJECT_SOURCE_DIR}/src/lock-focushelper LOCK_FOCUSHELPER)
aux_source_directory(${PROJECT_SOURCE_DIR}/src/liblightdm-qt LIGHTDM_QT)

add_definitions(-DRESOURCE_PATH="/usr/share/ukui-screensaver/")
add_definitions(-DENABLEPQ)

set(SINGLE_GUI_APPLICATION_SRCS
    ${PROJECT_SOURCE_DIR}/src/QtSingleApplication/qtsingleapplication.cpp
    ${PROJECT_SOURCE_DIR}/src/QtSingleApplication/qtlocalpeer.cpp
    )
set(SINGLE_CORE_APPLICATION_SRCS
    ${PROJECT_SOURCE_DIR}/src/QtSingleApplication/qtsinglecoreapplication.cpp
    ${PROJECT_SOURCE_DIR}/src/QtSingleApplication/qtlocalpeer.cpp
    )

include_directories(
    ${X11_INCLUDE_DIRS}
    ${XTST_INCLUDE_DIRS}
    ${XCB_INCLUDE_DIRS}
    ${QGS_INCLUDE_DIRS}
    ${GIOUNIX2_INCLUDE_DIRS}
    ${GLIB2_INCLUDE_DIRS}
    ${MMIX_INCLUDE_DIRS}
    ${kylin-nm-base_INCLUDE_DIRS}
    ${LIGHTDM-GOBJ-1_INCLUDE_DIRS}
    ${LIBSYSTEMD_INCLUDE_DIRS}
    ${LIBXRANDR_INCLUDE_DIRS}
    )

set(EXTRA_LIBS
    ${EXTRA_LIBS}
    ${X11_LIBRARIES}
    ${XTST_LIBRARIES}
    ${XCB_LIBRARIES}
    ${QGS_LIBRARIES}
    ${GIOUNIX2_LIBRARIES}
    ${GLIB2_LIBRARIES}
    ${MMIX_LIBRARIES}
    ${LIBXRANDR_LIBRARIES}
    -lrt
    -lpthread
    )

option(SUPPORT_X11_BACKGROUND "Support draw x11 background" OFF)
if (SUPPORT_X11_BACKGROUND)
    pkg_check_modules(IMLIB2 REQUIRED imlib2)
    include_directories(
        ${IMLIB2_INCLUDE_DIRS}
        )
    set(EXTRA_LIBS
        ${EXTRA_LIBS}
        ${IMLIB2_LIBRARIES}
        )
    add_definitions(-DSUPPORT_X11_BACKGROUND)
endif()

option(SUPPORT_NET_ICON "Support show network icon from library" ON)
if (SUPPORT_NET_ICON)
    set(EXTRA_LIBS
        ${EXTRA_LIBS}
        -llibnm-icon-kylin
        )
    add_definitions(-DSUPPORT_NET_ICON)
endif()

option(SUPPORT_TABLET_MODE "Support tablet mode check" OFF)
if (SUPPORT_TABLET_MODE)
    set(BACKEND_EXTRA_LIBS
        ${BACKEND_EXTRA_LIBS}
        -lukuiinputgatherclient
        )
    add_definitions(-DSUPPORT_TABLET_MODE)
endif()

#qt5_wrap_ui(dialog_SRC
#    lockwidget.ui
#    surewindow.ui
#    )

qt6_add_resources(dialog_SRC
	screensaver/default.qrc
        assets.qrc
    )

qt6_wrap_cpp(dialog_SRC
    userinfo.h
    agreementinfo.h
    dbusifs/freedesktophelper.h
    dbusifs/greeterconfig.h
    )

set(dialog_SRC
    ${dialog_SRC}
    ${COMMON}
    ${WIDGEGS}
    ${SINGLE_GUI_APPLICATION_SRCS}
    ${LOCK_DIALOG}
    userinfo.cpp
    agreementinfo.cpp
    dbusifs/uniauthservice.cpp
    dbusifs/biometrichelper.cpp
    dbusifs/freedesktophelper.cpp
    dbusifs/greeterconfig.cpp
    dbusifs/giodbus.cpp
    common/configuration.cpp
    common/displayservice.cpp
    common/capslockstatehelper.cpp
    )

add_executable(ukui-screensaver-dialog ${dialog_SRC})
add_definitions(-DAPP_API_MAJOR=0
                -DAPP_API_MINOR=11
                -DAPP_API_FUNC=0
                )

target_include_directories(ukui-screensaver-dialog
    PRIVATE
    ${KYSDKSYSTIME_INCLUDE_DIRS}
    ${KYSDKWINDOWHELPER_INCLUDE_DIRS}
    ${KYSDKQTWIDGETS_INCLUDE_DIRS}
    ${KWAYLANDCLIENT_INCLUDE_DIRS})
target_link_directories(ukui-screensaver-dialog
    PRIVATE ${KYSDKSYSTIME_LIBRARY_DIRS}
    ${KYSDKWINDOWHELPER_LIBRARY_DIRS}
    ${KYSDKQTWIDGETS_LIBRARY_DIRS}
    ${KWAYLANDCLIENT_LIBRARY_DIRS})
target_link_libraries(ukui-screensaver-dialog
	Qt6::Core
	Qt6::Widgets
	Qt6::DBus
	Qt6::Svg
        Qt6::Network
        Qt6::Widgets
        ${KWAYLANDCLIENT_LIBRARIES}
        ${KYSDKSYSTIME_LIBRARIES}
        ${KYSDKWINDOWHELPER_LIBRARIES}
        ${KYSDKQTWIDGETS_LIBRARIES}
        KF6::NetworkManagerQt
	${EXTRA_LIBS}
        VirtualKeyboard
        ukui-log4qt
        -ldl
	-lcrypto
        -lqrencode
        pwquality
        )

qt6_add_dbus_adaptor(backend_SRC
    lock-backend/org.ukui.ScreenSaver.xml
    lock-backend/dbusupperinterface.h
    DbusUpperInterface
    )

qt6_wrap_cpp(backend_SRC
       userinfo.h
       agreementinfo.h
       dbusifs/freedesktophelper.h
       dbusifs/greeterconfig.h
       dbusifs/upowerdbus.h
       dbusifs/upowerbatterydbus.h
       dbusifs/upowerlinepowerdbus.h
       dbusifs/upm_upowerdbus.h
       ukccplugins/sessiondbus/screensaverinterface.h
       ukccplugins/sessiondbus/screenlockinterface.h
       liblightdm-qt/QLightDM/greeter.h
       liblightdm-qt/QLightDM/power.h
       liblightdm-qt/QLightDM/usersmodel.h
       liblightdm-qt/QLightDM/sessionsmodel.h
    )

set(backend_SRC
    ${backend_SRC}
    ${LOCK_BACKEND}
    ${SINGLE_CORE_APPLICATION_SRCS}
    ${LIGHTDM_QT}
    dbusifs/login1helper.cpp
    dbusifs/usdhelper.cpp
    dbusifs/upowerhelper.cpp
    dbusifs/device.cpp
    dbusifs/accountshelper.cpp
    dbusifs/sessionhelper.cpp
    dbusifs/uniauthservice.cpp
    dbusifs/biometrichelper.cpp
    dbusifs/freedesktophelper.cpp
    dbusifs/giodbus.cpp
    dbusifs/kglobalaccelhelper.cpp
    dbusifs/machinemodel.cpp
    dbusifs/libinputswitchevent.cpp
    dbusifs/systemupgradehelper.cpp
    dbusifs/dbusservermanager.cpp
    dbusifs/networkmanagerhelper.cpp
    dbusifs/filemanagerhelper.cpp
    dbusifs/greeterconfig.cpp
    dbusifs/upowerdbus.cpp
    dbusifs/upowerbatterydbus.cpp
    dbusifs/upowerlinepowerdbus.cpp
    dbusifs/upm_upowerdbus.cpp
    userinfo.cpp
    agreementinfo.cpp
    common/global_utils.cpp
    common/configuration.cpp
    common/biodefines.cpp
    common/kyrsac.cpp
    ukccplugins/sessiondbus/screensaverinterface.cpp
    ukccplugins/sessiondbus/screenlockinterface.cpp
    )

add_executable(ukui-screensaver-backend ${backend_SRC})
target_link_libraries(ukui-screensaver-backend
    Qt6::Core
    Qt6::DBus
    Qt6::Network
    ${PAM_LIBRARIES}
    ${LIGHTDM-GOBJ-1_LIBRARIES}
    ${QGS_LIBRARIES}
    ${LIBSYSTEMD_LIBRARIES}
    ${GLIB2_LIBRARIES}
    ukui-log4qt
    -ldl
    -lcrypto
    ${BACKEND_EXTRA_LIBS}
    )

set(command_SRC
    ${LOCK_COMMAND}
    )
add_executable(ukui-screensaver-command ${command_SRC})
target_link_libraries(ukui-screensaver-command Qt6::Core Qt6::Gui Qt6::DBus ukui-log4qt)

set(checkpass_SRC
    ${LOCK_CHECKPASSWD}
    )
add_executable(ukui-screensaver-checkpass ${checkpass_SRC})
target_link_libraries(ukui-screensaver-checkpass ${PAM_LIBRARIES})

set(focushelper_SRCS
    ${focushelper_SRCS}
    ${LOCK_FOCUSHELPER}
    widgets/screen-manager.h
    widgets/screen-manager.cpp
    )

add_executable(screensaver-focus-helper ${focushelper_SRCS})
target_link_libraries(screensaver-focus-helper Qt6::Core  Qt6::Widgets ${X11_LIBRARIES} ${XCB_LIBRARIES})

add_subdirectory(VirtualKeyboard)
add_subdirectory(screensaver)
add_subdirectory(ukccplugins)

add_dependencies(ukui-screensaver-dialog VirtualKeyboard ukui-screensaver-default screensaver-default Screensaver)

install(TARGETS
    ukui-screensaver-dialog
    ukui-screensaver-backend
    ukui-screensaver-command
#    ukui-screensaver-checkpass
    DESTINATION bin)

#install(TARGETS
#        screensaver-focus-helper
#        DESTINATION lib/ukui-screensaver)

install(DIRECTORY assets/badges DESTINATION /usr/share/ukui-screensaver/images/)
