diff --git a/src/notifybysnore.cpp b/src/notifybysnore.cpp index 6c03bbf..9288d69 100644 --- a/src/notifybysnore.cpp +++ b/src/notifybysnore.cpp @@ -1,124 +1,150 @@ /* 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 #include static NotifyBySnore *s_instance = nullptr; -// !IMPORTANT! apps must have shortcut appID same as app->applicationName() - +// !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(); - QTemporaryFile iconFile; 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)); + +CRASHES JUST ABOUT HERE BECAUSE OF SOME LOCKED MUTEX. +}); + arguments << QStringLiteral("-t") << notification->title(); - arguments << QStringLiteral("-m") << notification->text(); + arguments << QStringLiteral("-m") << notification->text()+QString::number(notification->id()); arguments << QStringLiteral("-p") << file.fileName(); - arguments << QStringLiteral("-appID") << app->applicationName(); + 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"; } } 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(); 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); }