diff --git a/src/notifybysnore.cpp b/src/notifybysnore.cpp index 90fb8c8..3d75b84 100644 --- a/src/notifybysnore.cpp +++ b/src/notifybysnore.cpp @@ -1,174 +1,174 @@ /* 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")].toInt(); const auto snoreAction = SnoreToastActions::getAction(action.toStdWString()); qDebug() << "THE ID IS : " << QString::number(ID); switch(snoreAction){ case SnoreToastActions::Actions::Clicked :{ qDebug() << " User clicked on the toast."; break; } case SnoreToastActions::Actions::Hidden :{ qDebug() << "The toast got hidden."; break; } case SnoreToastActions::Actions::Dismissed :{ qDebug() << "User dismissed the toast."; break; } case SnoreToastActions::Actions::Timedout :{ qDebug() << "The toast timed out."; break; } case SnoreToastActions::Actions::ButtonClicked :{ qDebug() << " User clicked a button on the toast."; const auto button = map[QStringLiteral("button")]; QStringList s = m_notifications.value(ID)->actions(); int action_no = s.indexOf(button) + 1; // QStringList starts with index 0 but not actions NotifyBySnore::notificationActionInvoked(ID, action_no); break; } case SnoreToastActions::Actions::TextEntered :{ qDebug() << " User entered some text in the toast."; break; } default:{ qDebug() << "Something weird happened to the toast."; break; } } }); } NotifyBySnore::~NotifyBySnore() { s_instance = nullptr; } void NotifyBySnore::notify(KNotification *notification, KNotifyConfig *config) { - if (m_notifications.find(notification->id()) != m_notifications.end()) { + 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"); } arguments << QStringLiteral("-t") << notification->title(); arguments << QStringLiteral("-m") << notification->text(); 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); // I don't need to store whole proc->start(program, arguments); } 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); }