summaryrefslogtreecommitdiffstats
path: root/dcop/dcopsignals.cpp
blob: 46b1dcd14d6cd46aa733688c39a2619b78b01341 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
Copyright (c) 2000 Waldo Bastian <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <dcopserver.h>
#include <dcopsignals.h>

template class TQPtrList<DCOPSignalConnection>;

DCOPSignals::DCOPSignals()
{
   connections.setAutoDelete(true);
}

/**
 * Client "conn" emits the signal "fun" with "data" as arguments.
 *
 * If "excludeSelf" is true, signal is never sent to "conn" itself.
 */
void
DCOPSignals::emitSignal( DCOPConnection *conn, const TQCString &_fun, const TQByteArray &data, bool excludeSelf)
{
   TQCString senderObj;
   TQCString fun = _fun;
   int i = fun.find('#');
   if (i > -1)
   {
      senderObj = fun.left(i);
      fun = fun.mid(i+1);
   }

   DCOPSignalConnectionList *list = connections.find(fun);
   if (!list) return;
   for(DCOPSignalConnection *current = list->first(); current; current = list->next())
   {
      bool doSend = false;
      if (current->senderConn)
      {
         if (current->senderConn == conn)
	    doSend = true;
      }
      else if (!current->sender.isEmpty())
      {
         if ((conn && current->sender == conn->appId) || (current->sender == "DCOPServer"))
	    doSend = true;
      }
      else
      {
         doSend = true;
      }

      if (!current->senderObj.isEmpty() &&
          (current->senderObj != senderObj))
      {
         doSend = false;
      }

      if (excludeSelf && (conn == current->recvConn))
         doSend = false;
      if (doSend)
      {
         the_server->sendMessage(current->recvConn, conn ? conn->appId : TQCString("DCOPServer"),
                                 current->recvConn->appId, current->recvObj,
                                 current->slot, data);
      }
   }
}

/**
 * Connects "signal" of the client named "sender" with the "slot" of
 * "receiverObj" in the "conn" client.
 *
 * If "Volatile" is true the connection will be removed when either
 * "sender" or "conn" unregisters.
 *
 * If "Volatile" is false the connection will only be removed when
 * "conn" unregisters.
 */
bool
DCOPSignals::connectSignal( const TQCString &sender, const TQCString &senderObj,
                       const TQCString &signal,
                       DCOPConnection *conn, const TQCString &receiverObj,
                       const TQCString &slot, bool Volatile)
{
   // TODO: Check if signal and slot match
   TQCString signalArgs, slotArgs;
   int i,j;
   i = signal.find('(');
   if (i < 0) return false;
   signalArgs = signal.mid(i+1);
   j = signalArgs.find(')');
   if (j < 0) return false;
   signalArgs.truncate(j);
   i = slot.find('(');
   if (i < 0) return false;
   slotArgs = slot.mid(i+1);
   j = slotArgs.find(')');
   if (j < 0) return false;
   slotArgs.truncate(j);

   if(signalArgs != slotArgs)
   {
      // Maybe the signal has more arguments than the slot...
      if (signalArgs.length() <= slotArgs.length())
         return false;
      if ((slotArgs.length() > 0) && (signalArgs[slotArgs.length()] != ','))
         return false;
      if (signalArgs.left(slotArgs.length()) != slotArgs)
         return false;
   }

   DCOPConnection *senderConn = 0;
   if (Volatile)
   {
      senderConn = the_server->findApp(sender);
      if (!senderConn)
         return false; // Sender does not exist.
   }
   DCOPSignalConnection *current = new DCOPSignalConnection;
   current->sender = sender;
   current->senderObj = senderObj;
   current->senderConn = senderConn;
   current->signal = signal;
   current->recvConn = conn;
   current->recvObj = receiverObj;
   current->slot = slot;

   DCOPSignalConnectionList *list = connections.find(signal);
   if (!list)
   {
      list = new DCOPSignalConnectionList;
      connections.insert(signal, list);
   }

   list->append( current );
   conn->signalConnectionList()->append(current);
   if (senderConn && senderConn != conn)
      senderConn->signalConnectionList()->append(current);
   return true;
}

/**
 * Disconnects "signal" of the client named "sender" from the "slot" of
 * "receiverObj" in the "conn" client.
 *
 * Special case:
 *   If sender & signal are empty all connections that involve
 *       conn & receiverObj (either as receiver or as sender)
 *   are disconnected.
 */
bool
DCOPSignals::disconnectSignal( const TQCString &sender, const TQCString &senderObj,
                       const TQCString &signal,
                       DCOPConnection *conn, const TQCString &receiverObj,
                       const TQCString &slot)
{
   if (sender.isEmpty() && signal.isEmpty())
   {
      removeConnections(conn, receiverObj);
      return true;
   }

   DCOPSignalConnectionList *list = connections.find(signal);
   if (!list)
      return false; // Not found...

   DCOPSignalConnection *next = 0;
   bool result = false;

   for(DCOPSignalConnection *current = list->first(); current; current = next)
   {
      next = list->next();

      if (current->recvConn != conn)
         continue;

      if (current->senderConn)
      {
         if (current->senderConn->appId != sender)
	    continue;
      }
      else if (current->sender != sender)
	    continue;

      if (!senderObj.isEmpty() &&
          (current->senderObj != senderObj))
         continue;

      if (!receiverObj.isEmpty() &&
          (current->recvObj != receiverObj))
         continue;

      if (!slot.isEmpty() &&
          (current->slot != slot))
         continue;

      result = true;
      list->removeRef(current);
      conn->signalConnectionList()->removeRef(current);
      if (current->senderConn)
         current->senderConn->signalConnectionList()->removeRef(current);
      delete current;
   }
   return result;
}

/**
 * Removes all connections related to "obj" in the "conn" client
 * This means:
 *   All connections for which "conn"/"obj" is the receiver.
 *   All volatile connections for which "conn"/"obj" is the sender.
 */
void
DCOPSignals::removeConnections(DCOPConnection *conn, const TQCString &obj)
{
   DCOPSignalConnectionList *list = conn->_signalConnectionList;
   if (!list)
      return; // Nothing to do...

   DCOPSignalConnection *next = 0;

   for(DCOPSignalConnection *current = list->first(); current; current = next)
   {
      next = list->next();

      if (!obj.isEmpty())
      {
         if ((current->senderConn == conn) && (current->senderObj != obj))
            continue;

         if ((current->recvConn == conn) && (current->recvObj != obj))
            continue;
      }

      if (current->senderConn && (current->senderConn != conn))
         current->senderConn->signalConnectionList()->removeRef(current);

      if (current->recvConn != conn)
         current->recvConn->signalConnectionList()->removeRef(current);

      DCOPSignalConnectionList *signalList = connections.find(current->signal);
      if (signalList)
      {
         signalList->removeRef(current);
         if (signalList->isEmpty())
            connections.remove(current->signal);
      }
      else
      {
         tqDebug("Error: Signal Connection was not in signalList!\n");
      }
      list->removeRef(current);
      delete current;
   }
}