1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
/*
* PortalServiceDaemon.cpp
*
* Copyright (C) 2021 Emanoil Kotsev <[email protected]>
*
*
* This file is part of xdg portal services.
*
* 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 "PortalServiceDaemon.h"
// path /org/freedesktop/portal/desktop
#define PORTALSERVICE_DBUS_SRVC "org.freedesktop.impl.portal.desktop.tde"
#define DBUS_CONNECTION_TIMEOUT 4000
#define DBUS_CONNECTION_RETRY 3
PortalServiceDaemon::PortalServiceDaemon()
: KUniqueApplication(),
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++;
}
}
PortalServiceDaemon::~PortalServiceDaemon()
{
// close D-Bus connection
dbusConnectionClose();
}
bool PortalServiceDaemon::initDBUS(){
mConnection = TQT_DBusConnection::addConnection(TQT_DBusConnection::SessionBus, PORTALSERVICE_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(PORTALSERVICE_DBUS_SRVC, TQT_DBusConnection::NoReplace))
return false;
// make sure we get a reply
mConnection.scheduleDispatch();
return true;
}
void PortalServiceDaemon::dbusConnectionClose() {
if(rootService)
{
delete rootService;
rootService=0;
}
if(orgService)
{
delete orgService;
orgService=0;
}
if(freedesktopService)
{
delete freedesktopService;
freedesktopService=0;
}
if(portalNodeService)
{
delete portalNodeService;
portalNodeService=0;
}
if(desktopNodeService)
{
delete desktopNodeService;
desktopNodeService=0;
}
if(sessionNodeService)
{
delete sessionNodeService;
sessionNodeService=0;
}
if(mConnection.isConnected()) {
mConnection.disconnect(this, TQ_SLOT(slotDbusSignal(const TQT_DBusMessage&)));
mConnection.closeConnection(PORTALSERVICE_DBUS_SRVC);
}
retryCount=0;
}
void PortalServiceDaemon::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 PortalServiceDaemon::slotDbusSignal(const TQT_DBusMessage& message) {
TQString serviceName = message[0].toString();
if ( message.interface() == TQString("org.freedesktop.DBus") &&
message.member() == TQString("NameAcquired") &&
serviceName == PORTALSERVICE_DBUS_SRVC )
{
tqDebug("TDEPortalService unique DBus name acquired: " + serviceName);
rootService = new RootNodeService(mConnection);
orgService = new OrgNodeService(mConnection);
freedesktopService = new FreeDesktopNodeService(mConnection);
portalNodeService = new PortalNodeService(mConnection);
desktopNodeService = new DesktopNodeService(mConnection);
//TODO: session needs clarification - perhaps it is registered here, via some app
tqDebug("TDEPortalService service setup done.");
}
}
#include "PortalServiceDaemon.moc"
|