diff --git a/src/notifybysnore.cpp b/src/notifybysnore.cpp index 07a9464..2fcdfc7 100644 --- a/src/notifybysnore.cpp +++ b/src/notifybysnore.cpp @@ -1,145 +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()); + + QObject::connect(server, &QLocalServer::newConnection, server, [this]() { + 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 ID = map[QStringLiteral("notificationId")]; + const auto snoreAction = SnoreToastActions::getAction(action.toStdWString()); + qDebug() << "THE ID IS : " << ID << "AND THE ACTION IS : " << action; + // if (action == QStringLiteral("clicked")) { + NotifyBySnore::notificationActionInvoked( ID.toInt() , static_cast(snoreAction)); + +// CRASHES JUST ABOUT HERE BECAUSE OF SOME LOCKED MUTEX. +}); } 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; } 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, 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. -}); 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); // 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(); 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); }