# Copyright © 2013 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

project(lomiri-content-hub-plugin)

find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS
  Quick
)

set(CMAKE_AUTOMOC TRUE)

set(QT_IMPORTS_DIR "${CMAKE_INSTALL_LIBDIR}/qt${QT_VERSION_MAJOR}/qml")
set(CONTENT_HUB_IMPORTS_DIR "${QT_IMPORTS_DIR}/Lomiri/Content")

set(PLUGIN lomiri-content-hub-plugin)

add_definitions(-DQT_NO_KEYWORDS)

if (ENABLE_UBUNTU_COMPAT)
    add_definitions(-DENABLE_UBUNTU_COMPAT)
endif ()

include_directories(
    ${CMAKE_SOURCE_DIR}
    ${LOMIRI_API_INCLUDE_DIRS}
    ${DBUS_INCLUDE_DIRS}
    ${GLIB_INCLUDE_DIRS}
    ${LOMIRI_LAUNCH_INCLUDE_DIRS}
)

set(PLUGIN_HDRS
    contenthandler.h
    contenthub.h
    contenthubplugin.h
    contenticonprovider.h
    contentitem.h
    contentpeer.h
    contentpeermodel.h
    contentscope.h
    contentstore.h
    contenttransfer.h
    contenttype.h
    qmlimportexporthandler.h
    )

set(PLUGIN_SRC
    contenthandler.cpp
    contenthub.cpp
    contenthubplugin.cpp
    contenticonprovider.cpp
    contentitem.cpp
    contentpeer.cpp
    contentpeermodel.cpp
    contentscope.cpp
    contentstore.cpp
    contenttransfer.cpp
    contenttype.cpp
    qmlimportexporthandler.cpp
    ../../../src/com/lomiri/content/debug.cpp
    )

add_library(${PLUGIN} MODULE ${PLUGIN_SRC} ${PLUGIN_HDRS})

target_link_libraries(${PLUGIN} Qt::Core Qt::Qml Qt::Quick Qt::DBus)
target_link_libraries(
    ${PLUGIN}
    lomiri-content-hub${QT_SUFFIX}
    ${LOMIRI_API_LIBRARIES}
    ${GLIB_LIBRARIES}
    ${LOMIRI_LAUNCH_LDFLAGS}
)

install(TARGETS ${PLUGIN} DESTINATION ${CONTENT_HUB_IMPORTS_DIR})

if(NOT CMAKE_CROSSCOMPILING)
    set(CHECK_DEFAULT_PATH "")
    if (EXISTS "/etc/debian_version")
        set(CHECK_DEFAULT_PATH "NO_DEFAULT_PATH")
    endif()
    find_program(QML_PLUGIN_DUMP qmlplugindump REQUIRED
                     PATHS /usr/lib/qt${QT_VERSION_MAJOR}/bin
                     ${CHECK_DEFAULT_PATH}
    )

    if (ENABLE_QT6)
        set(LOMIRI_CONTENT_VERSION 2.0)
    else()
        set(LOMIRI_CONTENT_VERSION 1.1)
    endif()
    add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/plugins.qmltypes
        COMMAND ${QML_PLUGIN_DUMP} -noinstantiate -notrelocatable Lomiri.Content ${LOMIRI_CONTENT_VERSION} ../../ > ${CMAKE_CURRENT_BINARY_DIR}/plugins.qmltypes
        DEPENDS ${PLUGIN}
        WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
    )

    add_custom_target(Content_generated_files ALL SOURCES ${CMAKE_CURRENT_BINARY_DIR}/plugins.qmltypes)
    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/plugins.qmltypes DESTINATION ${CONTENT_HUB_IMPORTS_DIR})
endif()

add_subdirectory(qml${QT_VERSION_MAJOR})
