diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index b0f97f9..7d217ee 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -27,6 +27,7 @@ list(APPEND SnoreNotify_SRCS hint.cpp log.cpp settingsdialog.cpp + setting.cpp ${UI} ${CMAKE_CURRENT_BINARY_DIR}/version.cpp ${SNORENOTIFY_RCS} @@ -40,6 +41,7 @@ list(APPEND SnoreNotify_HDR hint.h log.h settingsdialog.h + setting.h ${CMAKE_CURRENT_BINARY_DIR}/snore_exports.h version.h ) diff --git a/src/core/plugins/plugins.cpp b/src/core/plugins/plugins.cpp index 308ed1b..054b612 100644 --- a/src/core/plugins/plugins.cpp +++ b/src/core/plugins/plugins.cpp @@ -35,7 +35,7 @@ SnorePlugin::SnorePlugin(const QString &name) : if (thread() != qApp->thread()) { moveToThread(qApp->thread()); } - setDefaultValue("Enabled", true); + setDefaultValue("Enabled", Setting(true,true)); } SnorePlugin::~SnorePlugin() @@ -61,20 +61,17 @@ bool SnorePlugin::isInitialized() const QVariant SnorePlugin::value(const QString &key) const { - return SnoreCore::instance().settings()->value(normaliseKey(key)); + return SnoreCore::instance().value(normaliseKey(key)); } void SnorePlugin::setValue(const QString &key, const QVariant &value) { - SnoreCore::instance().settings()->setValue(normaliseKey(key), value); + SnoreCore::instance().setValue(normaliseKey(key), value); } void SnorePlugin::setDefaultValue(const QString &key, const QVariant &value) { - QString pk(normaliseKey(key)); - if (!SnoreCore::instance().settings()->contains(pk)) { - SnoreCore::instance().settings()->setValue(normaliseKey(key), value); - } + SnoreCore::instance().setDefaultValue(normaliseKey(key),value); } Snore::PluginSettingsWidget *SnorePlugin::settingsWidget() diff --git a/src/core/plugins/pluginsettingswidget.cpp b/src/core/plugins/pluginsettingswidget.cpp index c17cec8..0d60a64 100644 --- a/src/core/plugins/pluginsettingswidget.cpp +++ b/src/core/plugins/pluginsettingswidget.cpp @@ -57,7 +57,7 @@ void PluginSettingsWidget::addRow(const QString &label, QWidget *widget) void PluginSettingsWidget::loadSettings() { if (m_snorePlugin->type() != SnorePlugin::BACKEND) { - m_enabled->setChecked(m_snorePlugin->value("Enabled").toBool()); + m_enabled->setChecked(m_snorePlugin->value("Enabled").value().value().toBool()); } load(); } @@ -65,11 +65,21 @@ void PluginSettingsWidget::loadSettings() void PluginSettingsWidget::saveSettings() { if (m_snorePlugin->type() != SnorePlugin::BACKEND) { - m_snorePlugin->setValue("Enabled", m_enabled->isChecked()); + m_snorePlugin->setValue("Enabled", Setting(m_enabled->isChecked(),true)); } save(); } +QVariant PluginSettingsWidget::value(const QString &key) const +{ + return m_snorePlugin->value(key); +} + +void PluginSettingsWidget::setValue(const QString &key, const QVariant &value) +{ + m_snorePlugin->setValue(key,value); +} + void PluginSettingsWidget::load() { diff --git a/src/core/plugins/pluginsettingswidget.h b/src/core/plugins/pluginsettingswidget.h index 011c4b1..2294a14 100644 --- a/src/core/plugins/pluginsettingswidget.h +++ b/src/core/plugins/pluginsettingswidget.h @@ -43,12 +43,14 @@ public: void saveSettings(); protected: - SnorePlugin *m_snorePlugin; + QVariant value(const QString &key) const; + void setValue(const QString &key, const QVariant &value); virtual void load(); virtual void save(); -private: +private: + SnorePlugin *m_snorePlugin; QFormLayout *m_layout; QCheckBox *m_enabled; diff --git a/src/core/setting.cpp b/src/core/setting.cpp new file mode 100644 index 0000000..f8f734d --- /dev/null +++ b/src/core/setting.cpp @@ -0,0 +1,68 @@ +/* + SnoreNotify is a Notification Framework based on Qt + Copyright (C) 2013-2015 Patrick von Reth + + SnoreNotify is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + SnoreNotify 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with SnoreNotify. If not, see . +*/ +#include "setting.h" + +using namespace Snore; + +Setting::Setting() +{ + +} + +Setting::Setting(const QVariant &value, bool specific): + m_value(value),m_specific(specific) +{ + +} + +Setting::~Setting() +{ + +} + +bool Setting::isSpecific() const +{ + return m_specific; +} + +QVariant Setting::value() const +{ + return m_value; +} + +Snore::Setting::operator QVariant() +{ + return QVariant::fromValue(*this); +} + + +QDataStream &operator<<(QDataStream &out, const Snore::Setting &myObj) +{ + out << myObj.value() << myObj.isSpecific(); + return out; +} + +QDataStream &operator>>(QDataStream &in, Snore::Setting &myObj) +{ + in >> myObj.m_value; + in >> myObj.m_specific; + return in; +} + + + diff --git a/src/core/setting.h b/src/core/setting.h new file mode 100644 index 0000000..e0e1924 --- /dev/null +++ b/src/core/setting.h @@ -0,0 +1,60 @@ +/* + SnoreNotify is a Notification Framework based on Qt + Copyright (C) 2013-2015 Patrick von Reth + + SnoreNotify is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + SnoreNotify 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with SnoreNotify. If not, see . +*/ + +#ifndef SETTING_H +#define SETTING_H + +#include "snore_exports.h" + +#include + +namespace Snore{ + class Setting; +} + +SNORE_EXPORT QDataStream &operator<<(QDataStream &out, const Snore::Setting &myObj); +SNORE_EXPORT QDataStream &operator>>(QDataStream &in, Snore::Setting &myObj); + +namespace Snore{ + +class SNORE_EXPORT Setting +{ +public: + Setting(); + Setting(const QVariant &value, bool specific = false); + + ~Setting(); + + bool isSpecific() const; + + QVariant value() const; + + operator QVariant(); + +private: + QVariant m_value; + bool m_specific; + + friend SNORE_EXPORT QDataStream &(::operator<<)(QDataStream &out, const Snore::Setting &myObj); + friend SNORE_EXPORT QDataStream &(::operator>>)(QDataStream &in, Snore::Setting &myObj); +}; +} + +Q_DECLARE_METATYPE(Snore::Setting) + +#endif // SETTING_H diff --git a/src/core/snore.cpp b/src/core/snore.cpp index 727d422..ec1c75f 100644 --- a/src/core/snore.cpp +++ b/src/core/snore.cpp @@ -197,16 +197,41 @@ QList SnoreCore::settingWidgets() return list; } -QSettings *SnoreCore::settings() -{ - Q_D(SnoreCore); - return d->m_settings; -} - -const QSettings *SnoreCore::settings() const +QVariant SnoreCore::value(const QString &key) const { Q_D(const SnoreCore); - return d->m_settings; + QString nk = d->normalizeKey(key); + if(d->m_settings->contains(nk)) + { + return d->m_settings->value(nk); + } + return d->m_settings->value(key); + +} + +void SnoreCore::setValue(const QString &key, const QVariant &value) +{ + Q_D(SnoreCore); + QString nk = key; + if(value.canConvert() && value.value().isSpecific()) + { + nk = d->normalizeKey(nk); + } + d->m_settings->setValue(nk,value); +} + + +void SnoreCore::setDefaultValue(const QString &key, const QVariant &value) +{ + Q_D(SnoreCore); + QString nk = key; + if(value.canConvert() && value.value().isSpecific()) + { + nk = d->normalizeKey(nk); + } + if (!d->m_settings->contains(nk)) { + d->m_settings->setValue(nk, value); + } } void SnoreCore::setSettingsPrefix(const QString &prefix) diff --git a/src/core/snore.h b/src/core/snore.h index cf2eff1..58a1ddc 100644 --- a/src/core/snore.h +++ b/src/core/snore.h @@ -25,6 +25,7 @@ #include "notification/notification.h" #include "plugins/plugins.h" #include "hint.h" +#include "setting.h" #include #include @@ -150,11 +151,11 @@ public: */ QList settingWidgets(); - /** - * @return a pointer to the global settings - */ - QSettings *settings(); - const QSettings *settings() const; + + QVariant value(const QString &key) const; + void setValue(const QString &key, const QVariant &value); + void setDefaultValue(const QString &key, const QVariant &value); + /** * Some settings can be uniqe to your application. diff --git a/src/core/snore_p.cpp b/src/core/snore_p.cpp index 5eac148..3cec763 100644 --- a/src/core/snore_p.cpp +++ b/src/core/snore_p.cpp @@ -90,7 +90,7 @@ bool SnoreCorePrivate::setBackendIfAvailible(const QString &backend) } m_notificationBackend = b; - m_settings->setValue(primaryBackendSettingsName(), backend); + q->setValue("PrimaryBackend", Setting(backend,true)); return true; } return false; @@ -98,7 +98,9 @@ bool SnoreCorePrivate::setBackendIfAvailible(const QString &backend) bool SnoreCorePrivate::initPrimaryNotificationBackend() { - if (setBackendIfAvailible(m_settings->value(primaryBackendSettingsName()).toString())) { + Q_Q(SnoreCore); + snoreDebug(SNORE_DEBUG)<value("PrimaryBackend").value().value().toString(); + if (setBackendIfAvailible(q->value("PrimaryBackend").value().value().toString())) { return true; } #ifdef Q_OS_WIN @@ -131,7 +133,7 @@ bool SnoreCorePrivate::initPrimaryNotificationBackend() void SnoreCorePrivate::syncSettings() { - setBackendIfAvailible(m_settings->value(primaryBackendSettingsName()).toString()); + setBackendIfAvailible(m_settings->value("PrimaryBackend").value().value().toString()); for (auto pluginName : m_pluginNames[SnorePlugin::SECONDARY_BACKEND]) { SnorePlugin *plugin = m_plugins.value(pluginName); bool enable = m_plugins[pluginName]->value("Enabled").toBool(); @@ -147,6 +149,8 @@ void SnoreCorePrivate::registerMetaTypes() { qRegisterMetaType(); qRegisterMetaType(); + qRegisterMetaType(); + qRegisterMetaTypeStreamOperators("Snore::Settings"); } QString SnoreCorePrivate::tempPath() diff --git a/src/core/snore_p.h b/src/core/snore_p.h index 609c434..3fc0644 100644 --- a/src/core/snore_p.h +++ b/src/core/snore_p.h @@ -65,6 +65,11 @@ public: void syncSettings(); + QString normalizeKey(const QString &key) const{ + return QString("AppSpecificSettings/%1/%2").arg(m_hints.value("app_specific_settings","SnoreNotify").toString(),key); + } + + signals: void applicationRegistered(const Snore::Application &); void applicationDeregistered(const Snore::Application &); @@ -75,9 +80,6 @@ private slots: void slotAboutToQuit(); private: - QString primaryBackendSettingsName(){ - return QString("AppSpecificSettings/%1/PrimaryBackend").arg(m_hints.value("app_specific_settings","SnoreNotify").toString()); - } SnoreCorePrivate(); SnoreCore *q_ptr; diff --git a/src/plugins/backends/growl/CMakeLists.txt b/src/plugins/backends/growl/CMakeLists.txt index 4b4845f..6e17cc6 100644 --- a/src/plugins/backends/growl/CMakeLists.txt +++ b/src/plugins/backends/growl/CMakeLists.txt @@ -6,7 +6,7 @@ set_package_properties(SnoreGrowl++ PROPERTIES TYPE RECOMMENDED) if(SnoreGrowl++_FOUND) - set( GROWL_SRC growlbackend.cpp) + set( GROWL_SRC growlbackend.cpp growlsettings.cpp) add_library(libsnore_backend_growl MODULE ${GROWL_SRC} ) target_link_libraries(libsnore_backend_growl Snore::Libsnore Snore::SnoreGrowl++ ${CMAKE_THREAD_LIBS_INIT}) diff --git a/src/plugins/backends/growl/growlbackend.cpp b/src/plugins/backends/growl/growlbackend.cpp index 5cc2dcd..5cb4346 100644 --- a/src/plugins/backends/growl/growlbackend.cpp +++ b/src/plugins/backends/growl/growlbackend.cpp @@ -17,6 +17,7 @@ */ #include "growlbackend.h" +#include "growlsettings.h" #include "core/snore.h" #include "core/snore_p.h" @@ -30,6 +31,8 @@ GrowlBackend::GrowlBackend(): m_id(0) { s_instance = this; + setDefaultValue("Host","localhost"); + setDefaultValue("Password",""); } GrowlBackend::~GrowlBackend() @@ -63,7 +66,9 @@ void GrowlBackend::slotRegisterApplication(const Application &application) alerts.push_back(a.name().toUtf8().constData()); } - Growl *growl = new Growl(GROWL_TCP, "", application.name().toUtf8().constData()); + Growl *growl = new Growl(GROWL_TCP, value("Host").toString().toUtf8().constData(), + value("Password").toString().toUtf8().constData(), + application.name().toUtf8().constData()); growl->Register(alerts, application.icon().localUrl().toUtf8().constData()); m_applications.insert(application.name(), growl); @@ -97,6 +102,11 @@ void GrowlBackend::slotNotify(Notification notification) startTimeout(notification); } +PluginSettingsWidget *GrowlBackend::settingsWidget() +{ + return new GrowlSettings(this); +} + void GrowlBackend::gntpCallback(growl_callback_data *data) { if (s_instance) { diff --git a/src/plugins/backends/growl/growlbackend.h b/src/plugins/backends/growl/growlbackend.h index 28ead88..6ca4353 100644 --- a/src/plugins/backends/growl/growlbackend.h +++ b/src/plugins/backends/growl/growlbackend.h @@ -47,6 +47,11 @@ public slots: void slotRegisterApplication(const Snore::Application &application) override; void slotDeregisterApplication(const Snore::Application &application) override; void slotNotify(Snore::Notification notification) override; + + +public: + Snore::PluginSettingsWidget *settingsWidget() override; + }; #endif // GROWL_BACKEND_H diff --git a/src/plugins/backends/growl/growlsettings.cpp b/src/plugins/backends/growl/growlsettings.cpp new file mode 100644 index 0000000..22f08dc --- /dev/null +++ b/src/plugins/backends/growl/growlsettings.cpp @@ -0,0 +1,50 @@ +/* + SnoreNotify is a Notification Framework based on Qt + Copyright (C) 2015 Patrick von Reth + + SnoreNotify is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + SnoreNotify 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with SnoreNotify. If not, see . +*/ +#include "growlsettings.h" + +#include + +using namespace Snore; + +GrowlSettings::GrowlSettings(SnorePlugin *plugin, QWidget *parent): + PluginSettingsWidget(plugin,parent), + m_host(new QLineEdit), + m_password(new QLineEdit) +{ + m_password->setEchoMode(QLineEdit::Password); + addRow("Host:",m_host); + addRow("Password:", m_password); +} + +GrowlSettings::~GrowlSettings() +{ + +} + +void GrowlSettings::load() +{ + m_host->setText(value("Host").toString()); + m_password->setText(value("Password").toString()); +} + +void GrowlSettings::save() +{ + setValue("Host",m_host->text()); + setValue("Password",m_password->text()); +} + diff --git a/src/plugins/backends/growl/growlsettings.h b/src/plugins/backends/growl/growlsettings.h new file mode 100644 index 0000000..9764dfa --- /dev/null +++ b/src/plugins/backends/growl/growlsettings.h @@ -0,0 +1,41 @@ +/* + SnoreNotify is a Notification Framework based on Qt + Copyright (C) 2015 Patrick von Reth + + SnoreNotify is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + SnoreNotify 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with SnoreNotify. If not, see . +*/ +#ifndef GROWLSETTINGS_H +#define GROWLSETTINGS_H + +#include "plugins/pluginsettingswidget.h" + +class QLineEdit; + + +class GrowlSettings : public Snore::PluginSettingsWidget +{ +public: + explicit GrowlSettings(Snore::SnorePlugin *plugin, QWidget *parent = 0); + ~GrowlSettings(); + +protected: + void load() override; + void save() override; + +private: + QLineEdit *m_host; + QLineEdit *m_password; +}; + +#endif // GROWLSETTINGS_H diff --git a/src/plugins/backends/snore/snorenotifiersettings.cpp b/src/plugins/backends/snore/snorenotifiersettings.cpp index d271b34..b2a47d2 100644 --- a/src/plugins/backends/snore/snorenotifiersettings.cpp +++ b/src/plugins/backends/snore/snorenotifiersettings.cpp @@ -40,10 +40,10 @@ SnoreNotifierSettings::~SnoreNotifierSettings() void SnoreNotifierSettings::load() { - m_comboBox->setCurrentIndex(m_snorePlugin->value("Position").toInt()); + m_comboBox->setCurrentIndex(value("Position").toInt()); } void SnoreNotifierSettings::save() { - m_snorePlugin->setValue("Position", m_comboBox->currentIndex()); + setValue("Position", m_comboBox->currentIndex()); } diff --git a/src/plugins/secondary_backends/sound/soundsettings.cpp b/src/plugins/secondary_backends/sound/soundsettings.cpp index c990f17..6206608 100644 --- a/src/plugins/secondary_backends/sound/soundsettings.cpp +++ b/src/plugins/secondary_backends/sound/soundsettings.cpp @@ -48,11 +48,11 @@ SoundSettings::~SoundSettings() void SoundSettings::load() { - m_lineEdit->setText(m_snorePlugin->value("Sound").toString()); + m_lineEdit->setText(value("Sound").toString()); } void SoundSettings::save() { - m_snorePlugin->setValue("Sound", m_lineEdit->text()); + setValue("Sound", m_lineEdit->text()); } diff --git a/src/plugins/secondary_backends/toasty/toastysettings.cpp b/src/plugins/secondary_backends/toasty/toastysettings.cpp index 5fe7a86..1bf9800 100644 --- a/src/plugins/secondary_backends/toasty/toastysettings.cpp +++ b/src/plugins/secondary_backends/toasty/toastysettings.cpp @@ -34,10 +34,10 @@ ToastySettings::~ToastySettings() void ToastySettings::load() { - m_lineEdit->setText(m_snorePlugin->value("DeviceID").toString()); + m_lineEdit->setText(value("DeviceID").toString()); } void ToastySettings::save() { - m_snorePlugin->setValue("DeviceID", m_lineEdit->text()); + setValue("DeviceID", m_lineEdit->text()); }