File indexing completed on 2024-09-22 04:20:27
0001 /* 0002 SnoreNotify is a Notification Framework based on Qt 0003 Copyright (C) 2013-2015 Hannah von Reth <vonreth@kde.org> 0004 0005 SnoreNotify is free software: you can redistribute it and/or modify 0006 it under the terms of the GNU Lesser General Public License as published by 0007 the Free Software Foundation, either version 3 of the License, or 0008 (at your option) any later version. 0009 0010 SnoreNotify is distributed in the hope that it will be useful, 0011 but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 GNU Lesser General Public License for more details. 0014 0015 You should have received a copy of the GNU Lesser General Public License 0016 along with SnoreNotify. If not, see <http://www.gnu.org/licenses/>. 0017 */ 0018 0019 #include "parser.h" 0020 #include "snarlnetwork.h" 0021 0022 #include "libsnore/snore.h" 0023 #include "libsnore/notification/notification.h" 0024 #include "libsnore/notification/notification_p.h" 0025 0026 #include <QObject> 0027 #include <QTcpSocket> 0028 #include <QPointer> 0029 0030 using namespace Snore; 0031 0032 Parser::Parser(SnarlNetworkFrontend *snarl): 0033 QObject(snarl), 0034 snarl(snarl) 0035 { 0036 getSnpType.insert("type", TYPE); 0037 getSnpType.insert("app", APP); 0038 getSnpType.insert("version", VERSION); 0039 getSnpType.insert("action", ACTION); 0040 getSnpType.insert("register", REGISTER); 0041 getSnpType.insert("add_class", ADD_CLASS); 0042 getSnpType.insert("notification", NOTIFICATION); 0043 getSnpType.insert("unregister", UNREGISTER); 0044 getSnpType.insert("class", CLASS); 0045 getSnpType.insert("title", TITLE); 0046 getSnpType.insert("text", TEXT); 0047 getSnpType.insert("icon", ICON); 0048 getSnpType.insert("timeout", TIMEOUT); 0049 } 0050 0051 void Parser::parse(Notification &sNotification, const QString &msg, QTcpSocket *client) 0052 { 0053 qCDebug(SNORE) << msg; 0054 QStringList splitted(msg.split(QStringLiteral("#?"))); 0055 snpTypes action(ERROR); 0056 0057 QString appName; 0058 QString alertName; 0059 QString title; 0060 QString text; 0061 Icon icon = Icon::defaultIcon(); 0062 int timeout = Notification::defaultTimeout(); 0063 0064 QByteArray key; 0065 QString value; 0066 0067 foreach(const QString & s, splitted) { 0068 key = s.mid(0, s.indexOf(QLatin1String("="))).toLower().toLatin1(); 0069 value = s.mid(s.indexOf(QLatin1String("=")) + 1); 0070 switch (getSnpType.value(key)) { 0071 case APP: 0072 appName = value; 0073 break; 0074 case ACTION: 0075 action = getSnpType.value(value.toLatin1()); 0076 break; 0077 case TITLE: 0078 title = value; 0079 break; 0080 case TEXT: 0081 text = value; 0082 break; 0083 case ICON: 0084 icon = Icon(value); 0085 break; 0086 case CLASS: 0087 alertName = value; 0088 break; 0089 case TIMEOUT: 0090 timeout = value.toInt(); 0091 break; 0092 default: 0093 break; 0094 } 0095 } 0096 0097 Application app; 0098 Alert alert; 0099 0100 if (snarl->m_applications.contains(client)) { 0101 app = snarl->m_applications.value(client); 0102 } 0103 0104 if (!alertName.isEmpty() && app.isValid()) { 0105 if (app.alerts().contains(alertName)) { 0106 alert = app.alerts().value(alertName); 0107 } 0108 } 0109 0110 switch (action) { 0111 case NOTIFICATION: { 0112 if (!SnoreCore::instance().aplications().contains(app.name())) { 0113 SnoreCore::instance().registerApplication(app); 0114 } 0115 0116 sNotification = Notification(app, alert, title, text, icon, timeout); 0117 sNotification.hints().setPrivateValue(snarl, "clientSocket", QVariant::fromValue(QPointer<QTcpSocket>(client))); 0118 break; 0119 } 0120 case ADD_CLASS: 0121 if (alertName.isEmpty()) { 0122 qCDebug(SNORE) << "Error registering alert with empty name"; 0123 break; 0124 } 0125 alert = Alert(alertName, icon); 0126 app.addAlert(alert); 0127 break; 0128 case REGISTER: 0129 if (!snarl->m_applications.contains(client)) { 0130 snarl->m_applications[client] = Application(appName, icon); 0131 } else { 0132 qCDebug(SNORE) << appName << "already registered"; 0133 } 0134 break; 0135 case UNREGISTER: 0136 SnoreCore::instance().deregisterApplication(app); 0137 snarl->m_applications.take(client); 0138 break; 0139 case ERROR: 0140 default: 0141 break; 0142 } 0143 } 0144