diff options
Diffstat (limited to 'src/daemon/NotificationDaemon.cpp')
-rw-r--r-- | src/daemon/NotificationDaemon.cpp | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/src/daemon/NotificationDaemon.cpp b/src/daemon/NotificationDaemon.cpp new file mode 100644 index 0000000..81e5d08 --- /dev/null +++ b/src/daemon/NotificationDaemon.cpp @@ -0,0 +1,127 @@ +/* + * NotificationDaemon.cpp + * + * Created on: May 11, 2021 + * Author: emanoil + * + * kdbusnotification Copyright (C) 2009 kdbusnotification development team + * + * 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 <tqtimer.h> +#include <tqdbusmessage.h> +#include <tqdbuserror.h> + +#include "NotificationDaemon.h" + +#define NOTIFICATIONS_DBUS_PATH "/org/freedesktop/Notifications" +#define NOTIFICATIONS_DBUS_SRVC "org.freedesktop.Notifications" +#define DBUS_PATH "/org/freedesktop/DBus" +#define DBUS_SRVC "org.freedesktop.DBus" +#define DBUS_CONNECTION_TIMEOUT 4000 +#define DBUS_CONNECTION_RETRY 3 + +NotificationDaemon::NotificationDaemon() : KUniqueApplication() +{ + // TODO Auto-generated constructor stub + retryCount=0; + // init session connection to dbus + if (!initDBUS()) { + tqDebug("Failed to initialize the connection to DBus"); + TQTimer::singleShot(DBUS_CONNECTION_TIMEOUT, this, TQT_SLOT(slotReconnect())); + retryCount++; + } +} + +NotificationDaemon::~NotificationDaemon() +{ + // close D-Bus connection + close(); + + delete notificationService; + delete freedesktopService; + delete orgService; + delete rootService; +// delete receiver; +} + +bool NotificationDaemon::isConnectedToDBUS(){ + return mConnection.isConnected(); +} + +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; + } + + // try to get a specific service name + if (!mConnection.requestName(NOTIFICATIONS_DBUS_SRVC, TQT_DBusConnection::NoReplace)) + return false; + + mConnection.scheduleDispatch(); + mConnection.connect(this, TQT_SLOT(slotDbusSignal(const TQT_DBusMessage&))); + + TQTimer::singleShot(10, this, TQT_SLOT(slotConnectionCheck())); + + return true; +} + +void NotificationDaemon::close() { + if(mConnection.isConnected()) { + mConnection.disconnect(this, TQT_SLOT(slotDbusSignal(const TQT_DBusMessage&))); + mConnection.closeConnection(NOTIFICATIONS_DBUS_SRVC); + } +} + +void NotificationDaemon::slotReconnect() { + + close(); + + if (!initDBUS()) { + if (DBUS_CONNECTION_RETRY > retryCount) { + tqFatal("Failed to initialize the connection to DBus"); + } + TQTimer::singleShot(DBUS_CONNECTION_TIMEOUT, this, TQT_SLOT(slotReconnect())); + retryCount++; + } +} + +void NotificationDaemon::slotDbusSignal(const TQT_DBusMessage& message) { + if (message.interface() != TQString("org.freedesktop.DBus")) + return; + if (message.member() != TQString("NameAcquired")) + return; + tqDebug("Name acquired: " + message[0].toString()); + serviceName = message[0].toString(); +} + +void NotificationDaemon::slotConnectionCheck() { + + if (serviceName != NOTIFICATIONS_DBUS_SRVC) { + tqFatal("TDE Notification service already running or no unique name possible."); + } + + rootService = new RootNodeService(mConnection); + orgService = new OrgNodeService(mConnection); + freedesktopService = new FreeDesktopNodeService(mConnection); + notificationService = new NotificationsNodeService(mConnection); + + tqDebug("TDE Notification service setup done."); +} + +#include "NotificationDaemon.moc" |