From cb9f5521ed2fe01e04d2adf1da9ebb4fd36c84ee Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Mon, 24 Jun 2013 20:19:20 +0200 Subject: [PATCH] Initial implementation of auto-start function on Linux. --- CMakeLists.txt | 13 +++++---- src/core/defs.h.in | 2 ++ src/core/systemfactory.cpp | 57 +++++++++++++++++++++++--------------- src/core/systemfactory.h | 4 +++ 4 files changed, 48 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c8824bf0d..84bf4b170 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,12 +47,6 @@ endif(EXISTS "${PROJECT_SOURCE_DIR}/.git") message(STATUS "[${APP_LOW_NAME}] Revision number obtained: " ${APP_REVISION} ".") -# Configure internal C++ defines. -configure_file ( - ${PROJECT_SOURCE_DIR}/src/core/defs.h.in - ${CMAKE_CURRENT_BINARY_DIR}/src/core/defs.h -) - # Configure executable "properties" for Windows. if(WIN32) message(STATUS "[${APP_LOW_NAME}] Generating executable file properties.") @@ -69,8 +63,15 @@ if(UNIX) ${PROJECT_SOURCE_DIR}/resources/desktop/rssguard.desktop.in ${CMAKE_CURRENT_BINARY_DIR}/resources/desktop/rssguard.desktop ) + set(DESKTOP_ENTRY ${CMAKE_INSTALL_PREFIX}/share/applications) endif(UNIX) +# Configure internal C++ defines. +configure_file ( + ${PROJECT_SOURCE_DIR}/src/core/defs.h.in + ${CMAKE_CURRENT_BINARY_DIR}/src/core/defs.h +) + # Define some useful DEBUG for, ehrm, debug build. if(CMAKE_BUILD_TYPE STREQUAL "release" OR CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RELEASE") message(STATUS "[${APP_LOW_NAME}] A release build (non-debug) is chosen. Debugging outputs are silently ignored.") diff --git a/src/core/defs.h.in b/src/core/defs.h.in index 346443814..77ff894ff 100644 --- a/src/core/defs.h.in +++ b/src/core/defs.h.in @@ -25,6 +25,8 @@ #define APP_IS_RUNNING "app_is_running" #if defined(Q_OS_LINUX) +#define APP_DESKTOP_ENTRY_PATH "@DESKTOP_ENTRY@" +#define APP_DESKTOP_ENTRY_FILE "@APP_LOW_NAME@.desktop" #define APP_LANG_PATH APP_PREFIX + QString("/share/rssguard/l10n") #define APP_SKIN_PATH APP_PREFIX + QString("/share/rssguard/skins") #define APP_INFO_PATH APP_PREFIX + QString("/share/rssguard/information") diff --git a/src/core/systemfactory.cpp b/src/core/systemfactory.cpp index 98c47f4d1..a1ebd4e21 100644 --- a/src/core/systemfactory.cpp +++ b/src/core/systemfactory.cpp @@ -33,26 +33,12 @@ SystemFactory::AutoStartStatus SystemFactory::getAutoStartStatus() { // Use proper freedesktop.org way to auto-start the application on Linux. // INFO: http://standards.freedesktop.org/autostart-spec/latest/ #elif defined(Q_OS_LINUX) - QString xdg_config_path(qgetenv("XDG_CONFIG_HOME")); - QString desktop_file_location; + QString desktop_file_location = SystemFactory::getAutostartDesktopFileLocation(); - if (!xdg_config_path.isEmpty()) { - // XDG_CONFIG_HOME variable is specified. Look for .desktop file - // in 'autostart' subdirectory. - desktop_file_location = xdg_config_path + "/autostart/rssguard.desktop"; - } - else { - // Desired variable is not set, look for the default 'autostart' subdirectory. - QString home_directory(qgetenv("HOME")); - if (!home_directory.isEmpty()) { - // Home directory exists. Check if target .desktop file exists and - // return according status. - desktop_file_location = home_directory + "/.config/autostart/rssguard.desktop"; - } - else { - qDebug("Searching for auto-start function status failed. HOME variable not found."); - return SystemFactory::Unavailable; - } + // No correct path was found. + if (desktop_file_location.isEmpty()) { + qDebug("Searching for auto-start function status failed. HOME variable not found."); + return SystemFactory::Unavailable; } // We found correct path, now check if file exists and return correct status. @@ -69,6 +55,29 @@ SystemFactory::AutoStartStatus SystemFactory::getAutoStartStatus() { #endif } +QString SystemFactory::getAutostartDesktopFileLocation() { + QString xdg_config_path(qgetenv("XDG_CONFIG_HOME")); + QString desktop_file_location; + + if (!xdg_config_path.isEmpty()) { + // XDG_CONFIG_HOME variable is specified. Look for .desktop file + // in 'autostart' subdirectory. + desktop_file_location = xdg_config_path + "/autostart/" + APP_DESKTOP_ENTRY_FILE; + } + else { + // Desired variable is not set, look for the default 'autostart' subdirectory. + QString home_directory(qgetenv("HOME")); + if (!home_directory.isEmpty()) { + // Home directory exists. Check if target .desktop file exists and + // return according status. + desktop_file_location = home_directory + "/.config/autostart/" + APP_DESKTOP_ENTRY_FILE; + } + } + + // No location found, return empty string. + return desktop_file_location; +} + // TODO: Finish implementation of SystemFactory auto-start methods. bool SystemFactory::setAutoStartStatus(const AutoStartStatus &new_status) { SystemFactory::AutoStartStatus current_status = SystemFactory::getAutoStartStatus(); @@ -93,12 +102,16 @@ bool SystemFactory::setAutoStartStatus(const AutoStartStatus &new_status) { return false; } #elif defined(Q_OS_LINUX) + // Note that we expect here that no other program uses + // "rssguard.desktop" desktop file. switch (new_status) { case SystemFactory::Enabled: - - break; + QFile::link(QString(APP_DESKTOP_ENTRY_PATH) + "/" + APP_DESKTOP_ENTRY_FILE, + getAutostartDesktopFileLocation()); + return true; case SystemFactory::Disabled: - break; + QFile::remove(getAutostartDesktopFileLocation()); + return true; default: return false; } diff --git a/src/core/systemfactory.h b/src/core/systemfactory.h index 78fd53fec..ec145164a 100644 --- a/src/core/systemfactory.h +++ b/src/core/systemfactory.h @@ -20,6 +20,10 @@ class SystemFactory { // Function returns false if setting of // new status failed. static bool setAutoStartStatus(const SystemFactory::AutoStartStatus &new_status); + + // Returns standard location where auto-start .desktop files + // should be placed. + static QString getAutostartDesktopFileLocation(); }; #endif // SYSTEMFACTORY_H