/* * NotificationDaemon.cpp * * Copyright (C) 2021 Emanoil Kotsev * * * This file is part of kdbusnotification. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation. * * 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include "NotificationDaemon.h" // path /org/freedesktop/Notifications #define NOTIFICATIONS_DBUS_SRVC "org.freedesktop.Notifications" #define DBUS_CONNECTION_TIMEOUT 4000 #define DBUS_CONNECTION_RETRY 3 NotificationDaemon::NotificationDaemon() : TDEUniqueApplication(), retryCount(0) { // init session connection to dbus if (!initDBUS()) { tqDebug("Failed to initialize the connection to DBus"); TQTimer::singleShot(DBUS_CONNECTION_TIMEOUT, this, TQ_SLOT(slotReconnect())); retryCount++; } } NotificationDaemon::~NotificationDaemon() { // close D-Bus connection dbusConnectionClose(); } bool NotificationDaemon::initDBUS(){ mConnection = TQT_DBusConnection::addConnection(TQT_DBusConnection::SessionBus, NOTIFICATIONS_DBUS_SRVC); if ( !mConnection.isConnected() ) { tqDebug("Failed to open connection to system message bus: " + mConnection.lastError().message()); return false; } mConnection.connect(this, TQ_SLOT(slotDbusSignal(const TQT_DBusMessage&))); // try to get a specific service name if (!mConnection.requestName(NOTIFICATIONS_DBUS_SRVC, TQT_DBusConnection::NoReplace)) return false; // make sure we get a reply mConnection.scheduleDispatch(); return true; } void NotificationDaemon::dbusConnectionClose() { if(rootService) { delete rootService; rootService=0; } if(orgService) { delete orgService; orgService=0; } if(freedesktopService) { delete freedesktopService; freedesktopService=0; } if(notificationNodeService) { delete notificationNodeService; notificationNodeService=0; } if(mConnection.isConnected()) { mConnection.disconnect(this, TQ_SLOT(slotDbusSignal(const TQT_DBusMessage&))); mConnection.closeConnection(NOTIFICATIONS_DBUS_SRVC); } retryCount=0; } void NotificationDaemon::slotReconnect() { dbusConnectionClose(); if (!initDBUS()) { if (DBUS_CONNECTION_RETRY > retryCount) { tqFatal("Failed to initialize the connection to DBus"); } TQTimer::singleShot(DBUS_CONNECTION_TIMEOUT, this, TQ_SLOT(slotReconnect())); retryCount++; } } void NotificationDaemon::slotDbusSignal(const TQT_DBusMessage& message) { TQString serviceName = message[0].toString(); if ( message.interface() == TQString("org.freedesktop.DBus") && message.member() == TQString("NameAcquired") && serviceName == NOTIFICATIONS_DBUS_SRVC ) { tqDebug("TDENotification unique DBus name acquired: " + serviceName); rootService = new RootNodeService(mConnection); orgService = new OrgNodeService(mConnection); freedesktopService = new FreeDesktopNodeService(mConnection); notificationNodeService = new NotificationsNodeService(mConnection); tqDebug("TDENotification service setup done."); } } #include "NotificationDaemon.moc"