From 24bfc9e50b35c8ab0e309fec6017abe6fb052492 Mon Sep 17 00:00:00 2001 From: Patrick von Reth Date: Mon, 13 Jul 2015 18:38:59 +0200 Subject: [PATCH] fix issues with broken backends --- src/libsnore/plugins/plugins.cpp | 5 +- src/libsnore/settingsdialog.cpp | 1 + src/libsnore/snore.cpp | 9 ++ src/libsnore/snore_p.cpp | 5 + .../backends/snoretoast/snoretoast.cpp | 110 +++++++++--------- src/plugins/backends/snoretoast/snoretoast.h | 5 +- 6 files changed, 73 insertions(+), 62 deletions(-) diff --git a/src/libsnore/plugins/plugins.cpp b/src/libsnore/plugins/plugins.cpp index c19c663..9549916 100644 --- a/src/libsnore/plugins/plugins.cpp +++ b/src/libsnore/plugins/plugins.cpp @@ -38,6 +38,9 @@ SnorePlugin::SnorePlugin() connect(this, &SnorePlugin::initialisationFinished, [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) { + disable(); + } m_initialized = b; }); } @@ -115,7 +118,7 @@ void SnorePlugin::setDefaultSettings() void SnorePlugin::setEnabled(bool enabled) { - Q_ASSERT_X(isInitialized(), Q_FUNC_INFO, "Plugin not initialized"); + Q_ASSERT_X(!enabled || isInitialized(), Q_FUNC_INFO, "Plugin not initialized"); m_enabled = enabled; } diff --git a/src/libsnore/settingsdialog.cpp b/src/libsnore/settingsdialog.cpp index 7bf63db..ef0805d 100644 --- a/src/libsnore/settingsdialog.cpp +++ b/src/libsnore/settingsdialog.cpp @@ -15,6 +15,7 @@ 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 "snore.h" diff --git a/src/libsnore/snore.cpp b/src/libsnore/snore.cpp index f31fe74..4ead36a 100644 --- a/src/libsnore/snore.cpp +++ b/src/libsnore/snore.cpp @@ -94,6 +94,15 @@ void SnoreCore::loadPlugins(SnorePlugin::PluginTypes types) snoreDebug(SNORE_WARNING) << "Plugin Cache corrupted\n" << info->file() << info->type(); continue; } + + connect(plugin, &SnorePlugin::initialisationFinished, [d, plugin](bool initialized) { + if (!initialized) { + //TODO: improve + d->m_pluginNames[plugin->type()].removeAll(plugin->name()); + d->m_plugins.remove(qMakePair(plugin->type(), plugin->name())); + } + }); + QMetaObject::invokeMethod(plugin, "slotInitialize", Qt::QueuedConnection); snoreDebug(SNORE_DEBUG) << info->name() << "is a" << info->type(); d->m_pluginNames[info->type()].append(info->name()); diff --git a/src/libsnore/snore_p.cpp b/src/libsnore/snore_p.cpp index e49fd0c..a0cfd02 100644 --- a/src/libsnore/snore_p.cpp +++ b/src/libsnore/snore_p.cpp @@ -93,6 +93,11 @@ bool SnoreCorePrivate::setBackendIfAvailible(const QString &backend) if (m_notificationBackend) { m_notificationBackend->disable(); } + connect(b, &SnoreBackend::initialisationFinished, [this, b](bool initialized) { + if (!initialized) { + slotInitPrimaryNotificationBackend(); + } + }); m_notificationBackend = b; m_notificationBackend->enable(); diff --git a/src/plugins/backends/snoretoast/snoretoast.cpp b/src/plugins/backends/snoretoast/snoretoast.cpp index 49ddbcb..5afdcd9 100644 --- a/src/plugins/backends/snoretoast/snoretoast.cpp +++ b/src/plugins/backends/snoretoast/snoretoast.cpp @@ -30,11 +30,7 @@ bool SnoreToast::canCloseNotification() const void SnoreToast::slotNotify(Notification notification) { - QProcess *p = new QProcess(this); - p->setReadChannelMode(QProcess::MergedChannels); - - connect(p, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(slotToastNotificationClosed(int,QProcess::ExitStatus))); - connect(qApp, SIGNAL(aboutToQuit()), p, SLOT(kill())); + QProcess *p = createProcess(notification); QStringList arguements; arguements << QLatin1String("-t") @@ -55,17 +51,40 @@ void SnoreToast::slotNotify(Notification notification) arguements << QLatin1String("-silent"); } snoreDebug(SNORE_DEBUG) << "SnoreToast" << arguements; - p->start(QLatin1String("SnoreToast"), arguements); - p->setProperty("SNORE_NOTIFICATION", QVariant::fromValue(notification)); + connect(p, static_cast(&QProcess::finished), [this, notification](int code) { + Notification::CloseReasons reason = Notification::NONE; + + switch (code) { + case 0: + reason = Notification::ACTIVATED; + slotNotificationActionInvoked(notification); + break; + case 1: + //hidden; + break; + case 2: + reason = Notification::DISMISSED; + break; + case 3: + reason = Notification::TIMED_OUT; + break; + case -1: + //failed + snoreDebug(SNORE_WARNING) << "SnoreToast failed to display " << notification; + break; + } + + closeNotification(notification, reason); + }); + p->start(QLatin1String("SnoreToast"), arguements); } void SnoreToast::slotRegisterApplication(const Application &application) { if (!application.constHints().contains("windows_app_id")) { - QProcess *p = new QProcess(this); - p->setReadChannelMode(QProcess::MergedChannels); + QProcess *p = createProcess(Notification()); QStringList arguements; arguements << QLatin1String("-install") << QLatin1String("SnoreNotify\\") + qApp->applicationName() @@ -73,70 +92,22 @@ void SnoreToast::slotRegisterApplication(const Application &application) << appId(application); snoreDebug(SNORE_DEBUG) << "SnoreToast" << arguements; p->start(QLatin1String("SnoreToast"), arguements); - - connect(p, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(slotPrintExitStatus(int,QProcess::ExitStatus))); - connect(qApp, SIGNAL(aboutToQuit()), p, SLOT(kill())); } } void SnoreToast::slotCloseNotification(Notification notification) { - QProcess *p = new QProcess(this); - p->setReadChannelMode(QProcess::MergedChannels); + QProcess *p = createProcess(notification); QStringList arguements; arguements << QLatin1String("-close") << QString::number(notification.id()); snoreDebug(SNORE_DEBUG) << "SnoreToast" << arguements; p->start(QLatin1String("SnoreToast"), arguements); - - connect(p, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(slotPrintExitStatus(int,QProcess::ExitStatus))); - connect(qApp, SIGNAL(aboutToQuit()), p, SLOT(kill())); -} - -void SnoreToast::slotToastNotificationClosed(int code, QProcess::ExitStatus) -{ - QProcess *p = qobject_cast(sender()); - snoreDebug(SNORE_DEBUG) << p->readAll(); - snoreDebug(SNORE_DEBUG) << "Exit code:" << code; - - Notification n = p->property("SNORE_NOTIFICATION").value(); - Notification::CloseReasons reason = Notification::NONE; - - switch (code) { - case 0: - reason = Notification::ACTIVATED; - slotNotificationActionInvoked(n); - break; - case 1: - //hidden; - break; - case 2: - reason = Notification::DISMISSED; - break; - case 3: - reason = Notification::TIMED_OUT; - break; - case -1: - //failed - snoreDebug(SNORE_WARNING) << "SnoreToast failed to display " << n; - break; - } - - closeNotification(n, reason); - p->deleteLater(); -} - -void SnoreToast::slotPrintExitStatus(int, QProcess::ExitStatus) -{ - QProcess *p = qobject_cast(sender()); - snoreDebug(SNORE_DEBUG) << p->readAll(); - p->deleteLater(); } QString SnoreToast::appId(const Application &application) { - QString appID = application.constHints().value("windows-app-id").toString(); if (appID.isEmpty()) { appID = application.constHints().value("windows_app_id").toString(); @@ -146,3 +117,26 @@ QString SnoreToast::appId(const Application &application) } return appID; } + +QProcess *SnoreToast::createProcess(Notification noti) +{ + QProcess *p = new QProcess(this); + p->setReadChannelMode(QProcess::MergedChannels); + + connect(p, static_cast(&QProcess::finished), [p](int, QProcess::ExitStatus) { + snoreDebug(SNORE_DEBUG) << p->readAll(); + p->deleteLater(); + }); + + connect(p, static_cast(&QProcess::error), [this, p, noti](QProcess::ProcessError) { + snoreDebug(SNORE_WARNING) << "SnoreToasts seems to be broken:" << p->errorString(); + snoreDebug(SNORE_DEBUG) << p->readAll(); + if (noti.isValid()) { + closeNotification(noti, Notification::NONE); + } + emit initialisationFinished(false); + p->deleteLater(); + }); + connect(qApp, &QApplication::aboutToQuit, p, &QProcess::kill); + return p; +} diff --git a/src/plugins/backends/snoretoast/snoretoast.h b/src/plugins/backends/snoretoast/snoretoast.h index 62bcdb9..a0a17fa 100644 --- a/src/plugins/backends/snoretoast/snoretoast.h +++ b/src/plugins/backends/snoretoast/snoretoast.h @@ -21,12 +21,11 @@ public Q_SLOTS: void slotRegisterApplication(const Snore::Application &application) override; void slotCloseNotification(Snore::Notification notification) override; -private Q_SLOTS: - void slotToastNotificationClosed(int code, QProcess::ExitStatus); - void slotPrintExitStatus(int code, QProcess::ExitStatus); private: QString appId(const Snore::Application &application); + QProcess *createProcess(Snore::Notification noti); + }; #endif // TOASTER_H