improve SnorePlugin::setEnabled

This commit is contained in:
Patrick von Reth 2015-07-13 21:49:33 +02:00
parent 24bfc9e50b
commit 7d0f7584fd
26 changed files with 136 additions and 169 deletions

View File

@ -35,7 +35,7 @@ SnorePlugin::SnorePlugin()
if (thread() != qApp->thread()) {
snoreDebug(SNORE_WARNING) << "Plugin initialized in wrong thread.";
}
connect(this, &SnorePlugin::initialisationFinished, [this](bool b) {
connect(this, &SnorePlugin::initializeChanged, [this](bool b) {
snoreDebug(SNORE_DEBUG) << "Plugin:" << name() << "initialized" << b;
Q_ASSERT_X(!(b && m_initialized), Q_FUNC_INFO, "Plugin initialized multiple times.");
if (!b) {
@ -119,6 +119,10 @@ void SnorePlugin::setDefaultSettings()
void SnorePlugin::setEnabled(bool enabled)
{
Q_ASSERT_X(!enabled || isInitialized(), Q_FUNC_INFO, "Plugin not initialized");
if (enabled != m_enabled) {
emit enabledChanged(enabled);
}
m_enabled = enabled;
}

View File

@ -53,7 +53,7 @@ public:
SnorePlugin();
virtual ~SnorePlugin();
virtual void setEnabled(bool enabled);
void setEnabled(bool enabled);
void enable();
void disable();
@ -84,7 +84,8 @@ public:
virtual PluginSettingsWidget *settingsWidget();
Q_SIGNALS:
void initialisationFinished(bool initialized);
void initializeChanged(bool initialized);
void enabledChanged(bool enabled);
public Q_SLOTS:
virtual void slotInitialize() = 0;

View File

@ -29,40 +29,37 @@
using namespace Snore;
SnoreBackend::SnoreBackend()
{
connect(this, &SnoreSecondaryBackend::enabledChanged, [this](bool enabled) {
if (enabled) {
connect(SnoreCorePrivate::instance(), &SnoreCorePrivate::applicationRegistered, this, &SnoreBackend::slotRegisterApplication, Qt::QueuedConnection);
connect(SnoreCorePrivate::instance(), &SnoreCorePrivate::applicationDeregistered, this, &SnoreBackend::slotDeregisterApplication, Qt::QueuedConnection);
connect(this, &SnoreBackend::notificationClosed, SnoreCorePrivate::instance(), &SnoreCorePrivate::slotNotificationClosed, Qt::QueuedConnection);
connect(SnoreCorePrivate::instance(), &SnoreCorePrivate::notify, this, &SnoreBackend::slotNotify, Qt::QueuedConnection);
for (const Application &a : SnoreCore::instance().aplications()) {
slotRegisterApplication(a);
}
} else {
for (const Application &a : SnoreCore::instance().aplications()) {
slotDeregisterApplication(a);
}
disconnect(SnoreCorePrivate::instance(), &SnoreCorePrivate::applicationRegistered, this, &SnoreBackend::slotRegisterApplication);
disconnect(SnoreCorePrivate::instance(), &SnoreCorePrivate::applicationDeregistered, this, &SnoreBackend::slotDeregisterApplication);
disconnect(this, &SnoreBackend::notificationClosed, SnoreCorePrivate::instance(), &SnoreCorePrivate::slotNotificationClosed);
disconnect(SnoreCorePrivate::instance(), &SnoreCorePrivate::notify, this, &SnoreBackend::slotNotify);
}
});
}
SnoreBackend::~SnoreBackend()
{
snoreDebug(SNORE_DEBUG) << "Deleting" << name();
}
void SnoreBackend::setEnabled(bool enabled)
{
if (enabled == isEnabled()) {
return;
}
SnorePlugin::setEnabled(enabled);
if (enabled) {
connect(SnoreCorePrivate::instance(), &SnoreCorePrivate::applicationRegistered, this, &SnoreBackend::slotRegisterApplication, Qt::QueuedConnection);
connect(SnoreCorePrivate::instance(), &SnoreCorePrivate::applicationDeregistered, this, &SnoreBackend::slotDeregisterApplication, Qt::QueuedConnection);
connect(this, &SnoreBackend::notificationClosed, SnoreCorePrivate::instance(), &SnoreCorePrivate::slotNotificationClosed, Qt::QueuedConnection);
connect(SnoreCorePrivate::instance(), &SnoreCorePrivate::notify, this, &SnoreBackend::slotNotify, Qt::QueuedConnection);
for (const Application &a : SnoreCore::instance().aplications()) {
slotRegisterApplication(a);
}
} else {
for (const Application &a : SnoreCore::instance().aplications()) {
slotDeregisterApplication(a);
}
disconnect(SnoreCorePrivate::instance(), &SnoreCorePrivate::applicationRegistered, this, &SnoreBackend::slotRegisterApplication);
disconnect(SnoreCorePrivate::instance(), &SnoreCorePrivate::applicationDeregistered, this, &SnoreBackend::slotDeregisterApplication);
disconnect(this, &SnoreBackend::notificationClosed, SnoreCorePrivate::instance(), &SnoreCorePrivate::slotNotificationClosed);
disconnect(SnoreCorePrivate::instance(), &SnoreCorePrivate::notify, this, &SnoreBackend::slotNotify);
}
}
void SnoreBackend::requestCloseNotification(Notification notification, Notification::CloseReasons reason)
{
if (canCloseNotification() && notification.isValid()) {
@ -90,26 +87,24 @@ void SnoreBackend::slotCloseNotification(Notification notification)
Q_UNUSED(notification)
}
SnoreSecondaryBackend::SnoreSecondaryBackend()
{
connect(this, &SnoreSecondaryBackend::enabledChanged, [this](bool enabled) {
if (enabled) {
connect(SnoreCorePrivate::instance(), &SnoreCorePrivate::notify, this, &SnoreSecondaryBackend::slotNotify, Qt::QueuedConnection);
connect(SnoreCorePrivate::instance(), &SnoreCorePrivate::notificationDisplayed, this, &SnoreSecondaryBackend::slotNotificationDisplayed, Qt::QueuedConnection);
} else {
disconnect(SnoreCorePrivate::instance(), &SnoreCorePrivate::notify, this, &SnoreSecondaryBackend::slotNotify);
disconnect(SnoreCorePrivate::instance(), &SnoreCorePrivate::notificationDisplayed, this, &SnoreSecondaryBackend::slotNotificationDisplayed);
}
});
}
SnoreSecondaryBackend::~SnoreSecondaryBackend()
{
snoreDebug(SNORE_DEBUG) << "Deleting" << name();
}
void SnoreSecondaryBackend::setEnabled(bool enabled)
{
if (enabled == isEnabled()) {
return;
}
SnorePlugin::setEnabled(enabled);
if (enabled) {
connect(SnoreCorePrivate::instance(), &SnoreCorePrivate::notify, this, &SnoreSecondaryBackend::slotNotify, Qt::QueuedConnection);
connect(SnoreCorePrivate::instance(), &SnoreCorePrivate::notificationDisplayed, this, &SnoreSecondaryBackend::slotNotificationDisplayed, Qt::QueuedConnection);
} else {
disconnect(SnoreCorePrivate::instance(), &SnoreCorePrivate::notify, this, &SnoreSecondaryBackend::slotNotify);
disconnect(SnoreCorePrivate::instance(), &SnoreCorePrivate::notificationDisplayed, this, &SnoreSecondaryBackend::slotNotificationDisplayed);
}
}
void SnoreSecondaryBackend::slotNotify(Notification)
{

View File

@ -31,11 +31,9 @@ class SNORE_EXPORT SnoreBackend : public SnorePlugin
Q_OBJECT
Q_INTERFACES(Snore::SnorePlugin)
public:
SnoreBackend() = default;
SnoreBackend();
virtual ~SnoreBackend();
virtual void setEnabled(bool enabled) override;
void requestCloseNotification(Snore::Notification notification, Notification::CloseReasons reason);
virtual bool canCloseNotification() const;
@ -72,11 +70,9 @@ class SNORE_EXPORT SnoreSecondaryBackend : public SnorePlugin
Q_OBJECT
Q_INTERFACES(Snore::SnorePlugin Snore::SnorePlugin)
public:
SnoreSecondaryBackend() = default;
SnoreSecondaryBackend();
virtual ~SnoreSecondaryBackend();
virtual void setEnabled(bool enabled) override;
public Q_SLOTS:
virtual void slotNotify(Snore::Notification notification);
virtual void slotNotificationDisplayed(Snore::Notification notification);

View File

@ -21,26 +21,24 @@
using namespace Snore;
SnoreFrontend::SnoreFrontend()
{
connect(this, &SnoreFrontend::enabledChanged, [this](bool enabled) {
if (enabled) {
connect(&SnoreCore::instance(), &SnoreCore::notificationClosed, this, &SnoreFrontend::slotNotificationClosed, Qt::QueuedConnection);
connect(&SnoreCore::instance(), &SnoreCore::actionInvoked, this, &SnoreFrontend::slotActionInvoked, Qt::QueuedConnection);
} else {
disconnect(&SnoreCore::instance(), &SnoreCore::notificationClosed, this, &SnoreFrontend::slotNotificationClosed);
disconnect(&SnoreCore::instance(), &SnoreCore::actionInvoked, this, &SnoreFrontend::slotActionInvoked);
}
});
}
SnoreFrontend::~SnoreFrontend()
{
snoreDebug(SNORE_DEBUG) << "Deleting" << name();
}
void SnoreFrontend::setEnabled(bool enabled)
{
if (enabled == isEnabled()) {
return;
}
SnorePlugin::setEnabled(enabled);
if (enabled) {
connect(&SnoreCore::instance(), &SnoreCore::notificationClosed, this, &SnoreFrontend::slotNotificationClosed, Qt::QueuedConnection);
connect(&SnoreCore::instance(), &SnoreCore::actionInvoked, this, &SnoreFrontend::slotActionInvoked, Qt::QueuedConnection);
} else {
disconnect(&SnoreCore::instance(), &SnoreCore::notificationClosed, this, &SnoreFrontend::slotNotificationClosed);
disconnect(&SnoreCore::instance(), &SnoreCore::actionInvoked, this, &SnoreFrontend::slotActionInvoked);
}
}
void SnoreFrontend::slotActionInvoked(Notification)
{

View File

@ -31,11 +31,9 @@ class SNORE_EXPORT SnoreFrontend: public SnorePlugin
Q_OBJECT
Q_INTERFACES(Snore::SnorePlugin)
public:
SnoreFrontend() = default;
SnoreFrontend();
virtual ~SnoreFrontend();
virtual void setEnabled(bool enabled) override;
public Q_SLOTS:
virtual void slotActionInvoked(Snore::Notification notification);
virtual void slotNotificationClosed(Snore::Notification notification);

View File

@ -84,7 +84,7 @@ void SnoreCore::loadPlugins(SnorePlugin::PluginTypes types)
case SnorePlugin::SECONDARY_BACKEND:
case SnorePlugin::FRONTEND:
case SnorePlugin::PLUGIN:
connect(plugin, &SnorePlugin::initialisationFinished, [plugin](bool initialized) {
connect(plugin, &SnorePlugin::initializeChanged, [plugin](bool initialized) {
if (initialized) {
plugin->setEnabled(plugin->settingsValue(QLatin1String("Enabled"), LOCAL_SETTING).toBool());
}
@ -95,7 +95,7 @@ void SnoreCore::loadPlugins(SnorePlugin::PluginTypes types)
continue;
}
connect(plugin, &SnorePlugin::initialisationFinished, [d, plugin](bool initialized) {
connect(plugin, &SnorePlugin::initializeChanged, [d, plugin](bool initialized) {
if (!initialized) {
//TODO: improve
d->m_pluginNames[plugin->type()].removeAll(plugin->name());

View File

@ -93,7 +93,7 @@ bool SnoreCorePrivate::setBackendIfAvailible(const QString &backend)
if (m_notificationBackend) {
m_notificationBackend->disable();
}
connect(b, &SnoreBackend::initialisationFinished, [this, b](bool initialized) {
connect(b, &SnoreBackend::initializeChanged, [this, b](bool initialized) {
if (!initialized) {
slotInitPrimaryNotificationBackend();
}

View File

@ -10,6 +10,20 @@
using namespace Snore;
FreedesktopBackend::FreedesktopBackend()
{
connect(this, &SnoreSecondaryBackend::enabledChanged, [this](bool enabled) {
if (enabled) {
connect(m_interface, &org::freedesktop::Notifications::ActionInvoked, this, &FreedesktopBackend::slotActionInvoked);
connect(m_interface, &org::freedesktop::Notifications::NotificationClosed, this , &FreedesktopBackend::slotNotificationClosed);
} else {
disconnect(m_interface, &org::freedesktop::Notifications::ActionInvoked, this, &FreedesktopBackend::slotActionInvoked);
disconnect(m_interface, &org::freedesktop::Notifications::NotificationClosed, this , &FreedesktopBackend::slotNotificationClosed);
}
});
}
void FreedesktopBackend::slotInitialize()
{
m_interface = new org::freedesktop::Notifications(QLatin1String("org.freedesktop.Notifications"),
@ -19,23 +33,7 @@ void FreedesktopBackend::slotInitialize()
reply.waitForFinished();
QStringList caps = reply.value();
m_supportsRichtext = caps.contains(QLatin1String("body-markup"));
emit initialisationFinished(true);
}
void FreedesktopBackend::setEnabled(bool enabled)
{
if (enabled == isEnabled()) {
return;
}
SnoreBackend::setEnabled(enabled);
if (enabled) {
connect(m_interface, &org::freedesktop::Notifications::ActionInvoked, this, &FreedesktopBackend::slotActionInvoked);
connect(m_interface, &org::freedesktop::Notifications::NotificationClosed, this , &FreedesktopBackend::slotNotificationClosed);
} else {
disconnect(m_interface, &org::freedesktop::Notifications::ActionInvoked, this, &FreedesktopBackend::slotActionInvoked);
disconnect(m_interface, &org::freedesktop::Notifications::NotificationClosed, this , &FreedesktopBackend::slotNotificationClosed);
}
emit initializeChanged(true);
}
bool FreedesktopBackend::canCloseNotification() const

View File

@ -9,11 +9,9 @@ class FreedesktopBackend: public Snore::SnoreBackend
Q_INTERFACES(Snore::SnoreBackend)
Q_PLUGIN_METADATA(IID "org.Snore.NotificationBackend/1.0" FILE "plugin.json")
public:
FreedesktopBackend() = default;
FreedesktopBackend();
~FreedesktopBackend() = default;
void setEnabled(bool enabled) override;
bool canCloseNotification() const override;
bool canUpdateNotification() const override;

View File

@ -65,7 +65,7 @@ GrowlBackend::~GrowlBackend()
void GrowlBackend::slotInitialize()
{
emit initialisationFinished(Growl::isRunning(GROWL_TCP, settingsValue(QLatin1String("Host")).toString().toUtf8().constData()));
emit initializeChanged(Growl::isRunning(GROWL_TCP, settingsValue(QLatin1String("Host")).toString().toUtf8().constData()));
}
void GrowlBackend::slotRegisterApplication(const Application &application)

View File

@ -32,6 +32,7 @@ public:
~OSXNotificationCenter();
public Q_SLOTS:
void slotInitialize() override;
void slotNotify(Snore::Notification notification) override;
};

View File

@ -125,7 +125,7 @@ void SnarlBackend::slotInitialize()
{
SnarlInterface *snarlInterface = new SnarlInterface();
emit initialisationFinished(snarlInterface->IsSnarlRunning());
emit initializeChanged(snarlInterface->IsSnarlRunning());
delete snarlInterface;
}

View File

@ -123,7 +123,7 @@ void SnoreNotifier::slotInitialize()
slotCloseNotification(notification);
});
}
emit initialisationFinished(true);
emit initializeChanged(true);
}
bool SnoreNotifier::canCloseNotification() const

View File

@ -18,9 +18,9 @@ void SnoreToast::slotInitialize()
{
if (QSysInfo::windowsVersion() < QSysInfo::WV_WINDOWS8) {
snoreDebug(SNORE_DEBUG) << "SnoreToast does not work on windows" << QSysInfo::windowsVersion();
emit initialisationFinished(false);
emit initializeChanged(false);
}
emit initialisationFinished(true);
emit initializeChanged(true);
}
bool SnoreToast::canCloseNotification() const
@ -134,7 +134,7 @@ QProcess *SnoreToast::createProcess(Notification noti)
if (noti.isValid()) {
closeNotification(noti, Notification::NONE);
}
emit initialisationFinished(false);
emit initializeChanged(false);
p->deleteLater();
});
connect(qApp, &QApplication::aboutToQuit, p, &QProcess::kill);

View File

@ -9,7 +9,7 @@ using namespace Snore;
void TrayIconNotifer::slotInitialize()
{
m_currentlyDisplaying = false;
emit initialisationFinished(true);
emit initializeChanged(true);
}
bool TrayIconNotifer::canCloseNotification() const

View File

@ -29,36 +29,34 @@
using namespace Snore;
void FreedesktopFrontend::slotInitialize()
FreedesktopFrontend::FreedesktopFrontend()
{
emit initialisationFinished(true);
}
void FreedesktopFrontend::setEnabled(bool enabled)
{
if (enabled == isEnabled()) {
return;
}
SnoreFrontend::setEnabled(enabled);
if (enabled) {
m_adaptor = new NotificationsAdaptor(this);
QDBusConnection dbus = QDBusConnection::sessionBus();
if (dbus.registerService(QLatin1String("org.freedesktop.Notifications"))) {
if (!dbus.registerObject(QLatin1String("/org/freedesktop/Notifications"), this)) {
snoreDebug(SNORE_WARNING) << "Failed to initialize" << name() << "failed to register object";
emit initialisationFinished(false);
connect(this, &FreedesktopFrontend::enabledChanged, [this](bool enabled) {
if (enabled) {
m_adaptor = new NotificationsAdaptor(this);
QDBusConnection dbus = QDBusConnection::sessionBus();
if (dbus.registerService(QLatin1String("org.freedesktop.Notifications"))) {
if (!dbus.registerObject(QLatin1String("/org/freedesktop/Notifications"), this)) {
snoreDebug(SNORE_WARNING) << "Failed to initialize" << name() << "failed to register object";
emit initializeChanged(false);
}
} else {
snoreDebug(SNORE_WARNING) << "Failed to initialize" << name() << "failed to register service";
emit initializeChanged(false);
}
} else {
snoreDebug(SNORE_WARNING) << "Failed to initialize" << name() << "failed to register service";
emit initialisationFinished(false);
QDBusConnection dbus = QDBusConnection::sessionBus();
dbus.unregisterService(QLatin1String("org.freedesktop.Notifications"));
dbus.unregisterObject(QLatin1String("/org/freedesktop/Notifications"));
m_adaptor->deleteLater();
m_adaptor = nullptr;
}
} else {
QDBusConnection dbus = QDBusConnection::sessionBus();
dbus.unregisterService(QLatin1String("org.freedesktop.Notifications"));
dbus.unregisterObject(QLatin1String("/org/freedesktop/Notifications"));
m_adaptor->deleteLater();
m_adaptor = nullptr;
}
});
}
void FreedesktopFrontend::slotInitialize()
{
emit initializeChanged(true);
}
void FreedesktopFrontend::slotActionInvoked(Notification notification)

View File

@ -29,11 +29,9 @@ class FreedesktopFrontend : public Snore::SnoreFrontend
Q_INTERFACES(Snore::SnoreFrontend)
Q_PLUGIN_METADATA(IID "org.Snore.NotificationFrontend/1.0" FILE "plugin.json")
public:
FreedesktopFrontend() = default;
FreedesktopFrontend();
~FreedesktopFrontend() = default;
void setEnabled(bool enabled) override;
uint Notify(const QString &app_name, uint replaces_id, const QString &app_icon, const QString &summary, const QString &body, const QStringList &actions, const QVariantMap &hints, int timeout);
void CloseNotification(uint id);

View File

@ -42,24 +42,18 @@ PushoverFrontend::PushoverFrontend()
connect(this, &PushoverFrontend::error, [this](QString error) {
m_errorMessage = error;
});
connect(this, &PushoverFrontend::enabledChanged, [this](bool enabled) {
if (enabled) {
connectToService();
} else {
disconnectService();
}
});
}
void PushoverFrontend::slotInitialize()
{
emit initialisationFinished(true);
}
void PushoverFrontend::setEnabled(bool enabled)
{
if (enabled == isEnabled()) {
return;
}
SnoreFrontend::setEnabled(enabled);
if (enabled) {
connectToService();
} else {
disconnectService();
}
emit initializeChanged(true);
}
PluginSettingsWidget *PushoverFrontend::settingsWidget()

View File

@ -35,8 +35,6 @@ public:
PushoverFrontend();
~PushoverFrontend() = default;
void setEnabled(bool enabled) override;
Snore::PluginSettingsWidget *settingsWidget() override;
void login(const QString &email, const QString &password, const QString &deviceName);

View File

@ -30,7 +30,13 @@ using namespace Snore;
SnarlNetworkFrontend::SnarlNetworkFrontend():
parser(new Parser(this))
{
connect(this, &SnarlNetworkFrontend::enabledChanged, [this](bool enabled) {
if (enabled) {
connect(tcpServer, &QTcpServer::newConnection, this, SnarlNetworkFrontend::handleConnection);
} else {
disconnect(tcpServer, &QTcpServer::newConnection, this, SnarlNetworkFrontend::handleConnection);
}
});
}
SnarlNetworkFrontend::~SnarlNetworkFrontend()
@ -43,25 +49,11 @@ void SnarlNetworkFrontend::slotInitialize()
tcpServer = new QTcpServer(this);
if (!tcpServer->listen(QHostAddress::Any, port)) {
snoreDebug(SNORE_DEBUG) << "The port is already used";
emit initialisationFinished(false);
emit initializeChanged(false);
} else {
std::cout << "The Snarl Network Protokoll is developed for Snarl <http://www.fullphat.net/>" << std::endl;
}
emit initialisationFinished(true);
}
void SnarlNetworkFrontend::setEnabled(bool enabled)
{
if (enabled == isEnabled()) {
return;
}
SnoreFrontend::setEnabled(enabled);
if (enabled) {
connect(tcpServer, &QTcpServer::newConnection, this, SnarlNetworkFrontend::handleConnection);
} else {
disconnect(tcpServer, &QTcpServer::newConnection, this, SnarlNetworkFrontend::handleConnection);
}
emit initializeChanged(true);
}
void SnarlNetworkFrontend::slotActionInvoked(Snore::Notification notification)

View File

@ -38,8 +38,6 @@ public:
SnarlNetworkFrontend();
~SnarlNetworkFrontend();
void setEnabled(bool enabled) override;
public Q_SLOTS:
void slotInitialize() override;
void slotActionInvoked(Snore::Notification notification) override;

View File

@ -61,7 +61,7 @@ void NotifyMyAndroid::slotNotify(Notification notification)
void NotifyMyAndroid::slotInitialize()
{
emit initialisationFinished(true);
emit initializeChanged(true);
}
PluginSettingsWidget *NotifyMyAndroid::settingsWidget()

View File

@ -122,7 +122,7 @@ void Pushover::slotNotify(Notification notification)
void Pushover::slotInitialize()
{
emit initialisationFinished(true);
emit initializeChanged(true);
}
PluginSettingsWidget *Pushover::settingsWidget()

View File

@ -37,7 +37,7 @@ Sound::Sound():
void Sound::slotInitialize()
{
m_player->setVolume(settingsValue(QLatin1String("Volume")).toInt());
emit initialisationFinished(true);
emit initializeChanged(true);
}
PluginSettingsWidget *Sound::settingsWidget()

View File

@ -79,7 +79,7 @@ void Toasty::slotNotify(Notification notification)
void Toasty::slotInitialize()
{
emit initialisationFinished(true);
emit initializeChanged(true);
}
PluginSettingsWidget *Toasty::settingsWidget()