From 3cf6a90d502b5e74f8d565eebdea5ec9dd24e5ad Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Mon, 24 Jun 2013 16:18:13 +0200 Subject: [PATCH] Initial implementation of auto-start functionality. --- CMakeLists.txt | 1 + src/core/systemfactory.cpp | 94 ++++++++++++++++++++++++++++++++++++++ src/core/systemfactory.h | 25 ++++++++++ src/gui/formmain.cpp | 6 +++ src/gui/formsettings.cpp | 42 +++++++++++++++-- src/gui/formsettings.h | 3 ++ src/gui/formsettings.ui | 26 ++++++++++- src/main.cpp | 3 +- 8 files changed, 194 insertions(+), 6 deletions(-) create mode 100644 src/core/systemfactory.cpp create mode 100644 src/core/systemfactory.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 988586f1f..c8824bf0d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -166,6 +166,7 @@ set(APP_SOURCES # CORE sources. src/core/debugging.cpp src/core/settings.cpp + src/core/systemfactory.cpp # Basic application sources. src/main.cpp diff --git a/src/core/systemfactory.cpp b/src/core/systemfactory.cpp new file mode 100644 index 000000000..006a96c40 --- /dev/null +++ b/src/core/systemfactory.cpp @@ -0,0 +1,94 @@ +#include +#include + +#include "core/systemfactory.h" +#include "core/defs.h" + + +SystemFactory::SystemFactory() { +} + +SystemFactory::AutoStartStatus SystemFactory::getAutoStartStatus() { + // User registry way to auto-start the application on Windows. +#if defined(Q_OS_WIN) + QSettings sett("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); + bool autostart_enabled = sett.value(APP_LOW_NAME, + "").toString().replace('\\', '/') == QApplication::applicationFilePath(); + + if (autostart_enabled) { + return SystemFactory::Enabled; + } + else { + return SystemFactory::Disabled; + } + + // 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; + + 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; + } + } + + // We found correct path, now check if file exists and return correct status. + if (QFile::exists(desktop_file_location)) { + return SystemFactory::Enabled; + } + else { + return SystemFactory::Disabled; + } + + // Disable auto-start functionality on unsupported platforms. +#else + return SystemFactory::Unavailable; +#endif +} + +// TODO: Finish implementation of SystemFactory auto-start methods. +bool SystemFactory::setAutoStartStatus(const AutoStartStatus &new_status) { + SystemFactory::AutoStartStatus current_status = SystemFactory::getAutoStartStatus(); + + if (current_status == SystemFactory::Unavailable) { + return false; + } + + switch (new_status) { + case SystemFactory::Enabled: +#if defined(Q_OS_WIN) + +#elif defined(Q_OS_LINUX) + +#else + +#endif + break; + case SystemFactory::Disabled: +#if defined(Q_OS_WIN) + +#elif defined(Q_OS_LINUX) + +#else + +#endif + break; + default: + return false; + } +} diff --git a/src/core/systemfactory.h b/src/core/systemfactory.h new file mode 100644 index 000000000..78fd53fec --- /dev/null +++ b/src/core/systemfactory.h @@ -0,0 +1,25 @@ +#ifndef SYSTEMFACTORY_H +#define SYSTEMFACTORY_H + + +class SystemFactory { + private: + SystemFactory(); + + public: + enum AutoStartStatus { + Enabled, + Disabled, + Unavailable + }; + + // Returns current status of auto-start function. + static SystemFactory::AutoStartStatus getAutoStartStatus(); + + // Sets new status for auto-start function. + // Function returns false if setting of + // new status failed. + static bool setAutoStartStatus(const SystemFactory::AutoStartStatus &new_status); +}; + +#endif // SYSTEMFACTORY_H diff --git a/src/gui/formmain.cpp b/src/gui/formmain.cpp index abbf662f0..ab43e5e35 100644 --- a/src/gui/formmain.cpp +++ b/src/gui/formmain.cpp @@ -36,6 +36,7 @@ void FormMain::processExecutionMessage(const QString &message) { // TODO: Implement proper reaction when application is launched more than once. qDebug("Received '%s' execution message from another application instance.", qPrintable(message)); + display(); } void FormMain::quit() { @@ -44,10 +45,15 @@ void FormMain::quit() { } void FormMain::display() { + // Make sure window is not minimized. setWindowState(windowState() & ~Qt::WindowMinimized); + + // Display the window and make sure it is raised on top. show(); activateWindow(); raise(); + + // Raise alert event. Check the documentation for more info on this. QtSingleApplication::alert(this); } diff --git a/src/gui/formsettings.cpp b/src/gui/formsettings.cpp index 0458ad801..31303c070 100644 --- a/src/gui/formsettings.cpp +++ b/src/gui/formsettings.cpp @@ -4,6 +4,7 @@ #include "gui/formmain.h" #include "core/settings.h" #include "core/defs.h" +#include "core/systemfactory.h" FormSettings::FormSettings(QWidget *parent) : QDialog(parent), m_ui(new Ui::FormSettings) { @@ -16,6 +17,7 @@ FormSettings::FormSettings(QWidget *parent) : QDialog(parent), m_ui(new Ui::Form connect(this, &FormSettings::accepted, this, &FormSettings::saveSettings); // Load all settings. + loadGeneral(); loadInterface(); } @@ -24,15 +26,49 @@ FormSettings::~FormSettings() { } void FormSettings::saveSettings() { - // Save all categories. - //saveGeneral(); + // Save all settings. + saveGeneral(); saveInterface(); - //saveLanguages(); // Make sure that settings is synced. Settings::getInstance()->checkSettings(); } +void FormSettings::loadGeneral() { + // Load auto-start status. + SystemFactory::AutoStartStatus autostart_status = SystemFactory::getAutoStartStatus(); + switch (autostart_status) { + case SystemFactory::Enabled: + m_ui->m_checkAutostart->setChecked(true); + break; + case SystemFactory::Disabled: + m_ui->m_checkAutostart->setChecked(false); + break; + default: + m_ui->m_checkAutostart->setEnabled(false); + m_ui->m_checkAutostart->setText(m_ui->m_checkAutostart->text() + + tr(" (not supported on this platform)")); + break; + } +} + +void FormSettings::saveGeneral() { + // Save auto-start on Windows. +#if defined(Q_OS_WIN) + QSettings sett("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); + if (m_ui->m_checkAutostart->isChecked()) { + sett.setValue(APP_LOW_NAME, QApplication::applicationFilePath().replace('/', '\\')); + } + else { + sett.remove(APP_LOW_NAME); + } + + // Save auto-start on Linux. +#elif defined(Q_OS_LINUX) + +#endif +} + void FormSettings::loadInterface() { // Load settings of tray icon. if (SystemTrayIcon::isSystemTrayAvailable()) { diff --git a/src/gui/formsettings.h b/src/gui/formsettings.h index 63131ab18..0658e9389 100644 --- a/src/gui/formsettings.h +++ b/src/gui/formsettings.h @@ -24,6 +24,9 @@ class FormSettings : public QDialog { // Load/save GUI settings. void loadInterface(); void saveInterface(); + + void loadGeneral(); + void saveGeneral(); private: Ui::FormSettings *m_ui; diff --git a/src/gui/formsettings.ui b/src/gui/formsettings.ui index 736a39ec6..2d1a01217 100644 --- a/src/gui/formsettings.ui +++ b/src/gui/formsettings.ui @@ -45,9 +45,31 @@ - 1 + 0 - + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Launch RSS Guard on operating system startup + + + + + diff --git a/src/main.cpp b/src/main.cpp index 7fe4ec649..b6584853d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -80,9 +80,11 @@ int main(int argc, char *argv[]) { if (Settings::getInstance()->value(APP_CFG_GUI, "start_hidden", false).toBool() && SystemTrayIcon::isSystemTrayActivated()) { + qDebug("Hiding the main window when the application is starting."); window.hide(); } else { + qDebug("Showing the main window when the application is starting."); window.show(); } @@ -92,7 +94,6 @@ int main(int argc, char *argv[]) { } // Setup single-instance behavior. - application.setActivationWindow(&window, true); QObject::connect(&application, &QtSingleApplication::messageReceived, &window, &FormMain::processExecutionMessage);