fix issues with broken backends

This commit is contained in:
Patrick von Reth 2015-07-13 18:38:59 +02:00
parent 753dfc9109
commit 24bfc9e50b
6 changed files with 73 additions and 62 deletions

View File

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

View File

@ -15,6 +15,7 @@
You should have received a copy of the GNU Lesser General Public License
along with SnoreNotify. If not, see <http://www.gnu.org/licenses/>.
*/
#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include "snore.h"

View File

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

View File

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

View File

@ -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<void (QProcess::*)(int, QProcess::ExitStatus)>(&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<QProcess *>(sender());
snoreDebug(SNORE_DEBUG) << p->readAll();
snoreDebug(SNORE_DEBUG) << "Exit code:" << code;
Notification n = p->property("SNORE_NOTIFICATION").value<Notification>();
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<QProcess *>(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<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), [p](int, QProcess::ExitStatus) {
snoreDebug(SNORE_DEBUG) << p->readAll();
p->deleteLater();
});
connect(p, static_cast<void (QProcess::*)(QProcess::ProcessError)>(&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;
}

View File

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