diff --git a/src/notifybysnore.cpp b/src/notifybysnore.cpp index 9288d69..07a9464 100644 --- a/src/notifybysnore.cpp +++ b/src/notifybysnore.cpp @@ -1,150 +1,145 @@ /* Copyright (C) 2019 Piyush Aggarwal This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "notifybysnore.h" #include "knotification.h" #include "knotifyconfig.h" #include "debug_p.h" #include #include #include #include #include #include #include #include #include #include #include #include #include static NotifyBySnore *s_instance = nullptr; // !DOCUMENT THIS! apps must have shortcut appID same as app->applicationName() NotifyBySnore::NotifyBySnore(QObject* parent) : KNotificationPlugin(parent) { s_instance = this; app = QCoreApplication::instance(); iconDir = new QTemporaryDir(); server = new QLocalServer(); server->listen(app->applicationName()); } NotifyBySnore::~NotifyBySnore() { s_instance = nullptr; } void NotifyBySnore::notify(KNotification *notification, KNotifyConfig *config) { if (m_notifications.find(notification->id()) != m_notifications.end() || notification->id() == -1) { qDebug() << "AHAA ! Duplicate for ID: " << notification->id() << " caught!"; return; } - proc = new QProcess(); + QProcess *proc = new QProcess(); QStringList arguments; QFile file(iconDir->path() + QString::number(notification->id())); if (!notification->pixmap().isNull()) { notification->pixmap().save(&file, "PNG"); } - ACTION! - QObject::connect(server, &QLocalServer::newConnection, server, [this,notification]() { - auto sock = server->nextPendingConnection(); - sock->waitForReadyRead(); - const QByteArray rawData = sock->readAll(); - const QString data = - QString::fromWCharArray(reinterpret_cast(rawData.constData()), - rawData.size() / sizeof(wchar_t)); - QMap map; - for (const auto &str : data.split(QStringLiteral(";"))) { - const auto index = str.indexOf(QStringLiteral("=")); - map[str.mid(0, index)] = str.mid(index + 1); - } - const auto action = map[QStringLiteral("action")]; - const auto snoreAction = SnoreToastActions::getAction(action.toStdWString()); - qDebug() << "THE ID IS : " << notification->id() << "AND THE ACTION IS : " << action; - // if (action == QStringLiteral("clicked")) { - NotifyBySnore::notificationActionInvoked(notification->id(), static_cast(snoreAction)); + // ACTION! + QObject::connect(server, &QLocalServer::newConnection, notification, [this,notification]() { + auto sock = server->nextPendingConnection(); + sock->waitForReadyRead(); + const QByteArray rawData = sock->readAll(); + const QString data = + QString::fromWCharArray(reinterpret_cast(rawData.constData()), + rawData.size() / sizeof(wchar_t)); + QMap map; + for (const auto &str : data.split(QStringLiteral(";"))) { + const auto index = str.indexOf(QStringLiteral("=")); + map[str.mid(0, index)] = str.mid(index + 1); + } + const auto action = map[QStringLiteral("action")]; + const auto snoreAction = SnoreToastActions::getAction(action.toStdWString()); + qDebug() << "THE ID IS : " << notification->id() << "AND THE ACTION IS : " << action; + // if (action == QStringLiteral("clicked")) { + NotifyBySnore::notificationActionInvoked(notification->id(), static_cast(snoreAction)); -CRASHES JUST ABOUT HERE BECAUSE OF SOME LOCKED MUTEX. +// CRASHES JUST ABOUT HERE BECAUSE OF SOME LOCKED MUTEX. }); arguments << QStringLiteral("-t") << notification->title(); arguments << QStringLiteral("-m") << notification->text()+QString::number(notification->id()); arguments << QStringLiteral("-p") << file.fileName(); arguments << QStringLiteral("-appID") << app->applicationName(); arguments << QStringLiteral("-id") << QString::number(notification->id()); arguments << QStringLiteral("-pipename") << server->fullServerName(); if (!notification->actions().isEmpty()){ arguments << QStringLiteral("-b") << notification->actions().join(QStringLiteral(";")); } m_notifications.insert(notification->id(), notification); proc->start(program, arguments); - if(proc->waitForStarted(1000)) - { - qDebug() << "SnoreToast displaying notification by ID: "<< notification->id(); - } - else - { - qDebug() << "SnoreToast did not start in time for Notif-Show"; - } + // connect(proc,&QProcess::started, notification, [this,notification](){ + // qDebug() << "SnoreToast displaying notification by ID: "<< notification->id(); + // }); } void NotifyBySnore::close(KNotification* notification) { const auto it = m_notifications.find(notification->id()); if (it == m_notifications.end()) { return; } qDebug() << "SnoreToast closing notification by ID: "<< notification->id(); - proc = new QProcess(); + QProcess *proc = new QProcess(); QStringList arguments; arguments << QStringLiteral("-close") << QString::number(notification->id()) << QStringLiteral("-appID") << app->applicationName(); ; proc->start(program, arguments); arguments.clear(); m_notifications.erase(it); if (it.value()) { finish(it.value()); } } void NotifyBySnore::update(KNotification *notification, KNotifyConfig *config) { close(notification); notify(notification, config); } void NotifyBySnore::notificationActionInvoked(int id, int action) { emit actionInvoked(id, action); } diff --git a/src/notifybysnore.h b/src/notifybysnore.h index ef3401e..2d1e81a 100644 --- a/src/notifybysnore.h +++ b/src/notifybysnore.h @@ -1,54 +1,54 @@ /* Copyright (C) 2019 Piyush Aggarwal This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef NOTIFYBYSNORE_H #define NOTIFYBYSNORE_H #include "knotificationplugin.h" #include #include #include #include #include #include /** Windows notification backend - inspired by Android notification backend. */ class NotifyBySnore : public KNotificationPlugin { Q_OBJECT public: explicit NotifyBySnore(QObject *parent = nullptr); ~NotifyBySnore() override; QString optionName() override { return QStringLiteral("Popup"); } void notify(KNotification *notification, KNotifyConfig *config) override; void close(KNotification * notification) override; void update(KNotification *notification, KNotifyConfig *config) override; void notificationActionInvoked(int id, int action); private: QHash> m_notifications; QString program = QStringLiteral("SnoreToast.exe"); - QProcess *proc; + // QProcess *proc; QLocalServer *server; QTemporaryDir *iconDir; QCoreApplication *app; }; #endif // NOTIFYBYSNORE_H