Initial implementation of auto-start functionality.

This commit is contained in:
Martin Rotter 2013-06-24 16:18:13 +02:00
parent 1706a36b66
commit 3cf6a90d50
8 changed files with 194 additions and 6 deletions

View File

@ -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

View File

@ -0,0 +1,94 @@
#include <QString>
#include <QFile>
#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;
}
}

25
src/core/systemfactory.h Normal file
View File

@ -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

View File

@ -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);
}

View File

@ -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()) {

View File

@ -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;

View File

@ -45,9 +45,31 @@
<item row="0" column="1">
<widget class="QStackedWidget" name="m_stackedSettings">
<property name="currentIndex">
<number>1</number>
<number>0</number>
</property>
<widget class="QWidget" name="m_pageGeneral"/>
<widget class="QWidget" name="m_pageGeneral">
<layout class="QFormLayout" name="formLayout_5">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QCheckBox" name="m_checkAutostart">
<property name="text">
<string>Launch RSS Guard on operating system startup</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="m_pageUi">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="leftMargin">

View File

@ -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);