File indexing completed on 2025-01-26 04:24:11
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 "growlbackend.h" 0020 #include "growlconstants.h" 0021 0022 #include "libsnore/snore.h" 0023 #include "libsnore/snore_p.h" 0024 #include "libsnore/utils.h" 0025 0026 #include <functional> 0027 0028 0029 SnorePlugin::Growl *SnorePlugin::Growl::s_instance = nullptr; 0030 0031 SnorePlugin::Growl::Growl() 0032 { 0033 s_instance = this; 0034 0035 auto func = [](growl_callback_data * data)->void { 0036 qCDebug(SNORE) << data->id << QString::fromUtf8(data->reason) << QString::fromUtf8(data->data); 0037 Snore::Notification n = Snore::SnoreCore::instance().getActiveNotificationByID(data->id); 0038 if (!n.isValid()) 0039 { 0040 return; 0041 } 0042 Snore::Notification::CloseReasons r = Snore::Notification::None; 0043 std::string reason(data->reason); 0044 if (reason == "TIMEDOUT") 0045 { 0046 r = Snore::Notification::TimedOut; 0047 } else if (reason == "CLOSED") 0048 { 0049 r = Snore::Notification::Dismissed; 0050 } else if (reason == "CLICK") 0051 { 0052 r = Snore::Notification::Activated; 0053 s_instance->slotNotificationActionInvoked(n); 0054 } 0055 s_instance->closeNotification(n, r); 0056 }; 0057 ::Growl::init((GROWL_CALLBACK)static_cast<void(*)(growl_callback_data *)>(func)); 0058 } 0059 0060 SnorePlugin::Growl::~Growl() 0061 { 0062 ::Growl::shutdown(); 0063 } 0064 0065 bool SnorePlugin::Growl::isReady() 0066 { 0067 bool running = ::Growl::isRunning(GROWL_TCP, settingsValue(GrowlConstants::Host).toString().toUtf8().constData()); 0068 if (!running) { 0069 setErrorString(tr("%1 is not running.").arg(name())); 0070 } 0071 return running; 0072 } 0073 0074 void SnorePlugin::Growl::slotRegisterApplication(const Snore::Application &application) 0075 { 0076 qCDebug(SNORE) << application.name(); 0077 std::vector<std::string> alerts; 0078 alerts.reserve(application.alerts().size()); 0079 foreach(const Snore::Alert & a, application.alerts()) { 0080 qCDebug(SNORE) << a.name(); 0081 alerts.push_back(a.name().toUtf8().constData()); 0082 } 0083 0084 ::Growl *growl = new ::Growl(GROWL_TCP, settingsValue(GrowlConstants::Host).toString().toUtf8().constData(), 0085 settingsValue(GrowlConstants::Password).toString().toUtf8().constData(), 0086 application.name().toUtf8().constData()); 0087 m_applications.insert(application.name(), growl); 0088 growl->Register(alerts, application.icon().localUrl(QSize(128, 128)).toUtf8().constData()); 0089 0090 } 0091 0092 void SnorePlugin::Growl::slotDeregisterApplication(const Snore::Application &application) 0093 { 0094 ::Growl *growl = m_applications.take(application.name()); 0095 if (growl == nullptr) { 0096 return; 0097 } 0098 delete growl; 0099 } 0100 0101 void SnorePlugin::Growl::slotNotify(Snore::Notification notification) 0102 { 0103 ::Growl *growl = m_applications.value(notification.application().name()); 0104 QString alert = notification.alert().name(); 0105 qCDebug(SNORE) << "Notify Growl:" << notification.application() << alert << notification.title(); 0106 0107 GrowlNotificationData data(alert.toUtf8().constData(), notification.id(), 0108 notification.title().toUtf8().constData(), 0109 notification.text().toUtf8().constData()); 0110 0111 data.setIcon(notification.icon().localUrl(QSize(128, 128)).toUtf8().constData()); 0112 data.setCallbackData("1"); 0113 growl->Notify(data); 0114 0115 slotNotificationDisplayed(notification); 0116 } 0117 0118 void SnorePlugin::Growl::setDefaultSettings() 0119 { 0120 SnoreBackend::setDefaultSettings(); 0121 setDefaultSettingsValue(GrowlConstants::Host, QLatin1String("localhost")); 0122 setDefaultSettingsValue(GrowlConstants::Password, QString()); 0123 } 0124