From 910d4e64828da26e9cb1c7d58a7c07f2713a0ff8 Mon Sep 17 00:00:00 2001 From: Patrick von Reth Date: Mon, 2 Feb 2015 20:28:00 +0100 Subject: [PATCH] make it possible to disable backends --- src/core/plugins/plugincontainer.cpp | 8 ++-- src/core/plugins/plugins.cpp | 2 +- src/core/plugins/plugins.h | 2 +- src/core/snore.cpp | 58 ++++++++++++++++++---------- src/core/snore.h | 19 +++------ src/core/snore_p.cpp | 2 +- src/core/snore_p.h | 3 +- src/daemon/CMakeLists.txt | 3 +- src/daemon/settingsdialog.cpp | 34 ++++++++++++++-- src/daemon/settingsdialog.h | 17 ++++++++ src/daemon/settingsdialog.ui | 8 +--- src/daemon/trayicon.cpp | 2 +- 12 files changed, 104 insertions(+), 54 deletions(-) diff --git a/src/core/plugins/plugincontainer.cpp b/src/core/plugins/plugincontainer.cpp index 89b2db4..c6a8ccb 100644 --- a/src/core/plugins/plugincontainer.cpp +++ b/src/core/plugins/plugincontainer.cpp @@ -137,6 +137,7 @@ void PluginContainer::updatePluginCache() const QHash PluginContainer::pluginCache(SnorePlugin::PluginTypes type) { + snoreDebug(SNORE_DEBUG) << type; if (s_pluginCache.isEmpty()) { QTime time; time.start(); @@ -145,12 +146,11 @@ const QHash PluginContainer::pluginCache(SnorePlugin } QHash out; - if (type == SnorePlugin::ALL) { - foreach(const SnorePlugin::PluginTypes & t, types()) { + for(auto t:types()){ + if(t & type) + { out.unite(s_pluginCache.value(t)); } - } else { - out = s_pluginCache[type]; } return out; } diff --git a/src/core/plugins/plugins.cpp b/src/core/plugins/plugins.cpp index cc104bc..1151fd4 100644 --- a/src/core/plugins/plugins.cpp +++ b/src/core/plugins/plugins.cpp @@ -58,7 +58,7 @@ bool SnorePlugin::initialize(SnoreCore *snore) return true; } -bool SnorePlugin::isInitialized() +bool SnorePlugin::isInitialized() const { return m_initialized; } diff --git a/src/core/plugins/plugins.h b/src/core/plugins/plugins.h index aab8952..dd28496 100644 --- a/src/core/plugins/plugins.h +++ b/src/core/plugins/plugins.h @@ -51,7 +51,7 @@ public: virtual ~SnorePlugin(); virtual bool initialize(SnoreCore *snore); virtual bool deinitialize(); - bool isInitialized(); + bool isInitialized() const; SnoreCore *snore(); const SnoreCore *snore() const; diff --git a/src/core/snore.cpp b/src/core/snore.cpp index bd09a1f..9bf4e28 100644 --- a/src/core/snore.cpp +++ b/src/core/snore.cpp @@ -73,15 +73,16 @@ void SnoreCore::loadPlugins(SnorePlugin::PluginTypes types) continue; } snoreDebug(SNORE_DEBUG) << info->name() << "is a" << info->type(); - d->m_plugins[info->type()].append(info->name()); + d->m_pluginNames[info->type()].append(info->name()); + d->m_plugins[info->name()] = info->load(); } - if (d->m_plugins.contains(type)) { - qSort(d->m_plugins[type]); + if (d->m_pluginNames.contains(type)) { + qSort(d->m_pluginNames[type]); } } } - snoreDebug(SNORE_INFO) << "Loaded Plugins:" << d->m_plugins; + snoreDebug(SNORE_INFO) << "Loaded Plugins:" << d->m_pluginNames; } void SnoreCore::broadcastNotification(Notification notification) @@ -120,22 +121,17 @@ const QHash &SnoreCore::aplications() const return d->m_applications; } -const QStringList SnoreCore::notificationBackends() const +const QStringList SnoreCore::pluginNames(SnorePlugin::PluginTypes type) const { Q_D(const SnoreCore); - return d->m_plugins.value(SnorePlugin::BACKEND); -} - -const QStringList SnoreCore::notificationFrontends() const -{ - Q_D(const SnoreCore); - return d->m_plugins.value(SnorePlugin::FRONTEND); -} - -const QStringList SnoreCore::secondaryNotificationBackends() const -{ - Q_D(const SnoreCore); - return d->m_plugins.value(SnorePlugin::SECONDARY_BACKEND); + QStringList out; + for(auto t:PluginContainer::types()){ + if(t & type) + { + out.append(d->m_pluginNames.value(t)); + } + } + return out; } bool SnoreCore::setPrimaryNotificationBackend(const QString &backend) @@ -228,11 +224,12 @@ bool SnoreCore::primaryBackendSupportsRichtext() QList SnoreCore::settingWidgets() { + Q_D(SnoreCore); QList list; - for(auto p:PluginContainer::pluginCache(SnorePlugin::ALL)) + for(auto p:d->m_plugins) { //TODO: mem leak? - PluginSettingsWidget *w = p->load()->settingsWidget(); + PluginSettingsWidget *w = p->settingsWidget(); if(w) { list.append(w); } @@ -257,6 +254,27 @@ const QHash &SnoreCore::settingsDescription() const return d->m_help; } + +bool SnoreCore::setPluginEnabled(const QString &pluginName, bool enable) +{ + Q_D(SnoreCore); + SnorePlugin *plugin = d->m_plugins.value(pluginName); + if(!plugin->isInitialized() && enable) + { + return plugin->initialize(this); + } else if(plugin->isInitialized() && !enable) + { + return plugin->deinitialize(); + } + return false; +} + +bool SnoreCore::pluginIsEnabled(const QString &pluginName) const +{ + Q_D(const SnoreCore); + return d->m_plugins.value(pluginName)->isInitialized(); +} + const SnoreCorePrivate *SnoreCore::d() { Q_D(SnoreCore); diff --git a/src/core/snore.h b/src/core/snore.h index baf1c7f..92f76fb 100644 --- a/src/core/snore.h +++ b/src/core/snore.h @@ -106,21 +106,9 @@ public: /** * - * @return a list of all known notification backends + * @return a list of plugins */ - const QStringList notificationBackends() const; - - /** - * - * @return a list of all known notification frontends - */ - const QStringList notificationFrontends() const; - - /** - * - * @return a list of all known notification secondary backends - */ - const QStringList secondaryNotificationBackends() const; + const QStringList pluginNames(SnorePlugin::PluginTypes type) const; /** * Sets the primary backend. @@ -180,6 +168,9 @@ public: */ const QHash &settingsDescription() const; + bool setPluginEnabled(const QString& pluginName,bool enable); + bool pluginIsEnabled(const QString& pluginName) const; + /** * @return a pointer to the private class, for internal use only. */ diff --git a/src/core/snore_p.cpp b/src/core/snore_p.cpp index 9516294..a17a858 100644 --- a/src/core/snore_p.cpp +++ b/src/core/snore_p.cpp @@ -68,7 +68,7 @@ void SnoreCorePrivate::notificationActionInvoked(Notification notification) cons bool SnoreCorePrivate::setBackendIfAvailible(const QString &backend) { Q_Q(SnoreCore); - if (m_plugins[SnorePlugin::BACKEND].contains(backend)) { + if (m_pluginNames[SnorePlugin::BACKEND].contains(backend)) { return q->setPrimaryNotificationBackend(backend); } return false; diff --git a/src/core/snore_p.h b/src/core/snore_p.h index abd3f8e..9a680d0 100644 --- a/src/core/snore_p.h +++ b/src/core/snore_p.h @@ -78,7 +78,8 @@ private: QHash m_applications; - QHash m_plugins; + QHash m_pluginNames; + QHash m_plugins; QPointer m_notificationBackend; diff --git a/src/daemon/CMakeLists.txt b/src/daemon/CMakeLists.txt index 792dbfb..fae870f 100644 --- a/src/daemon/CMakeLists.txt +++ b/src/daemon/CMakeLists.txt @@ -3,10 +3,9 @@ if(WITH_SNORE_DAEMON) include(ECMInstallIcons) ecm_add_app_icon(SNORENOTIFY_DAEMON_DEPS ICONS ${PROJECT_SOURCE_DIR}/data/128-apps-snore.png) - qt5_wrap_ui(UI settingsdialog.ui) - add_executable( snorenotify WIN32 main.cpp snorenotify.cpp trayicon.cpp settingsdialog.cpp ${SNORENOTIFY_DAEMON_DEPS} ${UI}) + add_executable( snorenotify WIN32 main.cpp snorenotify.cpp trayicon.cpp settingsdialog.cpp ${UI} ${SNORENOTIFY_DAEMON_DEPS}) target_link_libraries( snorenotify libsnore Qt5::Gui Qt5::Widgets ) install(TARGETS snorenotify ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) diff --git a/src/daemon/settingsdialog.cpp b/src/daemon/settingsdialog.cpp index 1a3f58c..dea61a3 100644 --- a/src/daemon/settingsdialog.cpp +++ b/src/daemon/settingsdialog.cpp @@ -1,9 +1,28 @@ +/* + 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 "settingsdialog.h" #include "ui_settingsdialog.h" -#include - #include "snore.h" +#include +#include +#include + using namespace Snore; SettingsDialog::SettingsDialog(SnoreCore *snore, QWidget *parent) : @@ -12,7 +31,16 @@ SettingsDialog::SettingsDialog(SnoreCore *snore, QWidget *parent) : m_snore(snore) { ui->setupUi(this); - ui->tabWidget->clear(); + for(const QString &plugin : snore->pluginNames(SnorePlugin::FRONTEND|SnorePlugin::SECONDARY_BACKEND|SnorePlugin::PLUGIN)) + { + QCheckBox *box = new QCheckBox(plugin); + box->setChecked(snore->pluginIsEnabled(plugin)); + connect(box,QCheckBox::toggled,[plugin,snore,box](bool checked){ + snore->setPluginEnabled(plugin,checked); + box->setChecked(snore->pluginIsEnabled(plugin)); + }); + ui->verticalLayout2->addWidget(box); + } for(auto widget : snore->settingWidgets()) { ui->tabWidget->addTab(widget,widget->name()); diff --git a/src/daemon/settingsdialog.h b/src/daemon/settingsdialog.h index f2561f2..abb682c 100644 --- a/src/daemon/settingsdialog.h +++ b/src/daemon/settingsdialog.h @@ -1,3 +1,20 @@ +/* + 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 SETTINGSDIALOG_H #define SETTINGSDIALOG_H diff --git a/src/daemon/settingsdialog.ui b/src/daemon/settingsdialog.ui index ee07138..c53167f 100644 --- a/src/daemon/settingsdialog.ui +++ b/src/daemon/settingsdialog.ui @@ -21,13 +21,9 @@ - Tab 1 - - - - - Tab 2 + Active Backends + diff --git a/src/daemon/trayicon.cpp b/src/daemon/trayicon.cpp index 07dea76..a5341f9 100644 --- a/src/daemon/trayicon.cpp +++ b/src/daemon/trayicon.cpp @@ -56,7 +56,7 @@ void TrayIcon::initConextMenu(SnoreCore *snore) m_trayMenu->addSeparator(); m_backendActions = new QActionGroup(m_trayMenu); m_backendActions->setExclusive(true); - foreach(const QString & back, m_snore->notificationBackends()) { + for(const QString & back : m_snore->pluginNames(SnorePlugin::BACKEND)) { QAction *b = m_trayMenu->addAction(back, this, SLOT(setPrimaryBackend())); b->setCheckable(true); if (back == m_snore->primaryNotificationBackend()) {