IF(POLICY CMP0017)
   CMAKE_POLICY(SET CMP0017 NEW)
ENDIF(POLICY CMP0017)

IF(POLICY CMP0022)
   CMAKE_POLICY(SET CMP0022 NEW)
ENDIF(POLICY CMP0022)

IF(POLICY CMP0063)
   CMAKE_POLICY(SET CMP0063 NEW)
ENDIF(POLICY CMP0063)

# set(CMAKE_AUTOMOC ON)

SET(KF5_DEP_VERSION "5.6.0")
SET(QT_MIN_VERSION  "5.2.0")

FIND_PACKAGE ( KF5 REQUIRED )

FIND_PACKAGE (ECM 1.1.0 REQUIRED NO_MODULE)

ADD_DEFINITIONS(
   ${KF5_DEFINITIONS}
   ${QT_DEFINITIONS}
   -fexceptions
   -Wno-error
   -Wno-reorder
   -Wno-error=deprecated-declarations
)

# It doesn't work in sandboxes
IF (NOT ENABLE_SINGLE_INSTANCE)
   ADD_DEFINITIONS(-DDISABLE_KDBUS_SERVICE=1)
ENDIF()

# Enable some useful warnings
ADD_DEFINITIONS(
   -Wall
   -Wextra
   -Wmissing-declarations
   -Wmissing-noreturn
   -Wpointer-arith
   -Wcast-align
   -Wwrite-strings
   -Wformat-nonliteral
   -Wformat-security
   -Wswitch-enum
   -Winit-self
   -Wmissing-include-dirs
   -Wundef
   -Wmissing-format-attribute
   -Wno-reorder
   -Wunused
   -Wuninitialized
   -Woverloaded-virtual
   -Wunused-value
   -pedantic
   -Wnonnull
   -Wsequence-point
   #-Wsystem-headers
   -Wsizeof-pointer-memaccess
   #-Wuseless-cast
   -Wvarargs
)

#Add more warnings for compilers that support it. I used this command:
#curl https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/Warning-Options.html | \
#grep -E "^[\t ]+<br><dt><code>-W[a-zA-Z=-]*" -o | grep -E "\-W[a-zA-Z=-]*" -o >
#cat /tmp/48 /tmp/49 | sort | uniq -u
# IF (CMAKE_COMPILER_IS_GNUCC)

IF (CMAKE_COMPILER_IS_GNUCC)
   IF (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9 OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 4.9)
      ADD_DEFINITIONS(
         -Wunused-but-set-parameter
         -Wconditionally-supported
         -Wno-cpp
#          -Wdouble-promotion
         -Wdate-time
         -Wdelete-incomplete
#          -Wfloat-conversion
      )
   ENDIF()

   IF (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 5.0 OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 5.0)
      ADD_DEFINITIONS(
         -Wsuggest-override
         -Wsuggest-final-types
         -Wsuggest-final-methods
         -Wbool-compare
         -Wformat-signedness
         -Wlogical-not-parentheses
         -Wnormalized
         -Wshift-count-negative
         -Wshift-count-overflow
         -Wsized-deallocation
         -Wsizeof-array-argument
      )
   ENDIF()

   IF (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 6.0 OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 6.0)
      ADD_DEFINITIONS(
         -Wnull-dereference
         -Wtautological-compare
         -Wduplicated-cond
         -Wmisleading-indentation
      )
   ENDIF()

   IF (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0 OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 7.0)
      ADD_DEFINITIONS(
         -Wimplicit-fallthrough
         -Wduplicated-branches
         -Wswitch-unreachable
         -Wformat-overflow
         -Wformat-truncation
         -Wnonnull
      )
   ENDIF()

   IF (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 8.0 OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 8.0)
      ADD_DEFINITIONS(
        -Wmultistatement-macros
        -Wstringop-truncation
        -Wif-not-aligned
        -Wmissing-attributes
      )
   ENDIF()
ENDIF()

IF (CMAKE_COMPILER_IS_GNUCC)
   EXECUTE_PROCESS(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE CMAKE_CXX_COMPILER_VERSION)
   IF (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6.9 OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 4.6)
      #GCC 4.6 version of those warnings does detect valid C++0x/C++11 as invalid.
      ADD_DEFINITIONS(
         -Wno-error=pragmas
         -Wno-pragmas
         -Wno-error
      )
   ENDIF()
   IF (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.7 OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 4.7)
      ADD_DEFINITIONS(
         -Wmaybe-uninitialized
         -Wunused-local-typedefs
         -pedantic
      )
   ENDIF()
   IF (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.8 OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 4.8)
#       ADD_DEFINITIONS("-Wzero-as-null-pointer-constant")
#       ADD_DEFINITIONS( -DENABLE_IGNORE_NULL=true )
   ENDIF()
ELSE()
   ADD_DEFINITIONS(-Wno-unknown-pragmas -Wno-unknown-warning-option)
ENDIF()

ADD_DEFINITIONS("-std=c++0x")

#Make sure it can access DBUS autogenerated files
INCLUDE_DIRECTORIES(SYSTEM
   ${KF5_INCLUDES}
   ${Qt5Widgets_INCLUDES}
   ${Qt5PrintSupport_INCLUDES}
   ${Qt5Core_INCLUDES}
   ${CMAKE_BINARY_DIR}
   ${ringqt_INCLUDE_DIR}
   ${LIB_TIP_INCLUDE}
   ${LIB_CONFIG_INCLUDE}
   ${LIB_CONFIG_WIDGETS}
)

# Shared C++ QML components
ADD_SUBDIRECTORY( qmlwidgets )

# Pack the icons shared by various components
ADD_SUBDIRECTORY( sharedassets )

#Build KDE specific files
ADD_SUBDIRECTORY( klib )

# Build the QML based first run Wizard
ADD_SUBDIRECTORY( wizard )

# Build the QML based interactive video canvas
ADD_SUBDIRECTORY( callview )

# Build the advanced phone dialer
ADD_SUBDIRECTORY( dialview )

# Build the views and widgets related to the social timeline feature
ADD_SUBDIRECTORY( timeline )

# Build the contact and profile photo editor dialog
ADD_SUBDIRECTORY( photoselector )

# Build the elements used to notify the users of events
ADD_SUBDIRECTORY( canvasindicators )

# Build the contact manager subsystem
ADD_SUBDIRECTORY( contactview )

# Build the account manager subsystem
ADD_SUBDIRECTORY( accountview )

# Build the main desktop component
ADD_SUBDIRECTORY( desktopview )

IF(${CMAKE_BUILD_TYPE} MATCHES Release)
   MESSAGE("NO DEBUG OUTPUT")
   ADD_DEFINITIONS( -DQT_NO_DEBUG_OUTPUT)
ELSE()
#     ADD_DEFINITIONS(-Werror)
ENDIF(${CMAKE_BUILD_TYPE} MATCHES Release)

# SET ( KF5_KABC_LIBS  -lkabc )

# Delegates
SET(
   ring_kde_DELEGATES
#    delegates/conferencedelegate.cpp
#    delegates/contactdelegate.cpp
#    delegates/historydelegate.cpp
#    delegates/phonenumberdelegate.cpp
#    delegates/delegatedropoverlay.cpp
#    delegates/dialpaddelegate.cpp
   delegates/autocompletiondelegate.cpp
   delegates/autocompletiondelegate2.cpp
   delegates/toolbardelegate.cpp
   delegates/accountinfodelegate.cpp
   delegates/kdepixmapmanipulation.cpp
   delegates/categorizeddelegate.cpp
   delegates/righticondelegate.cpp
   proxies/simplerotateproxy.cpp
   proxies/deduplicateproxy.cpp
   proxies/filtertoplevelproxy.cpp
   proxies/roletransformationproxy.cpp
   proxies/flagsfilterproxy.cpp
   proxies/pollingproxy.cpp
   #proxies/valuefilterproxy.cpp
)

# Configuration pages
SET(
   ring_kde_CONF
   conf/accountserializationadapter.cpp
   conf/configurationdialog.cpp
   conf/dlggeneral.cpp
   conf/dlgdisplay.cpp
   conf/dlgaudio.cpp
   conf/dlgaddressbook.cpp
   conf/dlgaccessibility.cpp
   conf/dlgpresence.cpp
   conf/dlgaccount.cpp
   configurator/localhistoryconfigurator.cpp
   configurator/peerprofileconfigurator.cpp
   configurator/bookmarkconfigurator.cpp
   configurator/audiorecordingconfigurator.cpp
   configurator/fallbackpersonconfigurator.cpp
)

# Account pages
SET(
   ring_kde_ACCOUNT
   conf/accountpages/advanced.cpp
   conf/accountpages/credentials.cpp
   conf/accountpages/basic.cpp
   conf/accountpages/network.cpp
   conf/accountpages/codecs.cpp
   conf/accountpages/ringtone.cpp
   conf/accountpages/security.cpp
   conf/accountpages/pagebase.cpp
   conf/accountpages/account.cpp
   conf/accountpages/devices.cpp
   conf/accountpages/dlgprofiles.cpp
)

# Widgets
SET(
   ring_kde_WIDGETS
   widgets/systray.cpp
   widgets/categorizedtreeview.cpp
   widgets/fileselector.cpp
   widgets/filterlineedit.cpp
   widgets/kphonenumberselector.cpp
   widgets/directoryview.cpp
   widgets/menumodelview.cpp
   widgets/accountstatusviewer.cpp
   widgets/autocombobox.cpp
   widgets/categorizedtree.cpp
   widgets/certificateviewer.cpp
   widgets/contextlistview.cpp
   widgets/groupedtoolbutton.cpp
   widgets/securityprogress.cpp
)

# Video Widgets
IF(NOT (${ENABLE_VIDEO} MATCHES false))
   SET(
      ring_kde_VIDEO
      conf/dlgvideo.cpp
   )
ENDIF(NOT (${ENABLE_VIDEO} MATCHES false))

SET(
   ring_kde_SRCS
   main.cpp
   cmd.cpp
   notification.cpp
   ringapplication.cpp
   errormessage.cpp
#    canvasobjectmanager.cpp
#    eventmanager.cpp
   actioncollection.cpp
   implementation.cpp
   model/extendedprotocolmodel.cpp
   ${ring_kde_DELEGATES}
   ${ring_kde_CONF}
   ${ring_kde_WIDGETS}
   ${ring_kde_ACCOUNT}
   ${ring_kde_VIDEO}
)


IF (Qt5TextToSpeech_FOUND)

SET(
   ring_kde_SRCS
   ${ring_kde_SRCS}
   accessibility.cpp
)
ENDIF()

# generate rules for building source files from the resources
SET(QtApp_RCCS
   qrc/resources.qrc
   conf/confresources.qrc
   ../data/appicon.qrc
)
QT5_ADD_RESOURCES(QtApp_RCC_SRCS ${QtApp_RCCS})


# kde4_automoc(${ring_kde_SRCS})
SET(
   config_ui_files
   conf/dlggeneralbase.ui
   conf/dlgdisplaybase.ui
   conf/dlgaudiobase.ui
   conf/dlgaddressbookbase.ui
   conf/dlgaccessibility.ui
   conf/dlgaudiorecording.ui
   conf/dlgpresence.ui
   conf/dlgfallbackperson.ui
   conf/accountdlg.ui
   conf/accountpages/advanced.ui
   conf/accountpages/credentials.ui
   conf/accountpages/basic.ui
   conf/accountpages/network.ui
   conf/accountpages/codecs.ui
   conf/accountpages/ringtone.ui
   conf/accountpages/security.ui
   conf/accountpages/tabs.ui
   widgets/ui/directoryview.ui
   widgets/ui/accountcreator.ui
   widgets/ui/fileselect.ui
   configurator/peerprofile.ui
   configurator/bookmark.ui
   configurator/recording.ui
)

# add_subdirectory( test   ) #Enable again some day, it cause compile problems for some users

IF(NOT (${ENABLE_VIDEO} MATCHES false))
   SET(ENABLE_VIDEO 1 CACHE BOOLEAN "Enable video")
   ADD_DEFINITIONS( -DENABLE_VIDEO=true )
   SET (
      config_ui_files
      ${config_ui_files}
      conf/dlgvideobase.ui
   )
ENDIF(NOT (${ENABLE_VIDEO} MATCHES false))

IF(${DISABLE_UNIQUE_APPLICATION} MATCHES true)
   MESSAGE("KUniqueApplication disabled")
   SET(DISABLE_UNIQUE_APPLICATION 1 CACHE BOOLEAN "Disable KUniqueApplication")
   ADD_DEFINITIONS( -DDISABLE_UNIQUE_APPLICATION=true )
ENDIF(${DISABLE_UNIQUE_APPLICATION} MATCHES true)

ki18n_wrap_ui(ring_kde_SRCS ${config_ui_files} )

IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  ADD_EXECUTABLE(ring-kde MACOSX_BUNDLE ${ring_kde_SRCS} ${QtApp_RCC_SRCS})

  set_source_files_properties( ../data/ring-kdeui.rc PROPERTIES MACOSX_PACKAGE_LOCATION Resources )
  file( COPY ../data/ring-kdeui.rc DESTINATION "ring-kde.app/Contents/Resources/" )
ELSE()
  ADD_EXECUTABLE(ring-kde ${ring_kde_SRCS} ${QtApp_RCC_SRCS})
ENDIF()

IF(NOT ${ENABLE_STATIC} MATCHES false)
  FIND_PACKAGE(Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED DBus)
  INCLUDE_DIRECTORIES(SYSTEM ${Qt5DBus_INCLUDE_DIRS} )
  LINK_DIRECTORIES(${Qt5DBus_LIBRARY_DIRS} )
  ADD_DEFINITIONS(${Qt5DBus_CFLAGS})
  TARGET_LINK_LIBRARIES(ring-kde
    ${ringqt_LIBRARY_STATIC}
    -lpthread
  )

  IF(NOT ${ENABLE_LIBWRAP})
    TARGET_LINK_LIBRARIES(ring-kde
        ${Qt5DBus_LIBRARIES}
    )
  ENDIF()

ELSE()
   TARGET_LINK_LIBRARIES(ring-kde ${ringqt_LIBRARY_DYNAMIC})
ENDIF()

QT5_ADD_DBUS_ADAPTOR(ring_kde_SRCS ../data/org.kde.kuiserver.xml cmd.h Cmd )

TARGET_LINK_LIBRARIES(ring-kde
  libkring
  libwizard
  libcallview
  dialview
  libtimeline
  libphotoselector
  libcanvasindicator
  desktopview
  contactview
  accountview
  libqmlwidgets

  # Qt5
  Qt5::Widgets
  Qt5::Core
  Qt5::Gui
  Qt5::Svg
  Qt5::PrintSupport

  # KF5
  KF5::I18n
  KF5::WidgetsAddons
  KF5::ConfigCore
  KF5::ConfigGui
  KF5::DBusAddons
  KF5::XmlGui
  KF5::Notifications
  KF5::IconThemes
  KF5::Crash
  KF5::NotifyConfig
  KF5::GlobalAccel
  KF5::Declarative
  KF5::Kirigami2
)

IF (Qt5TextToSpeech_FOUND)
   TARGET_LINK_LIBRARIES(ring-kde
      Qt5::TextToSpeech
   )
ENDIF()

########### install files ###############

INSTALL( TARGETS ring-kde                     DESTINATION ${BIN_INSTALL_DIR}           )
INSTALL( FILES wizard/assets/beta_logo.svg    DESTINATION ${DATA_INSTALL_DIR}/ring-kde )
