summaryrefslogtreecommitdiffstats
path: root/tqdbusmessage.h
blob: 0a097e0b3591f20c2c563f7709293480fddf62fb (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/* qdbusmessage.h TQT_DBusMessage object
 *
 * Copyright (C) 2005 Harald Fernengel <harry@tdevelop.org>
 * Copyright (C) 2005-2007 Kevin Krammer <kevin.krammer@gmx.at>
 *
 * Licensed under the Academic Free License version 2.1
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA.
 *
 */

#ifndef TQDBUSMESSAGE_H
#define TQDBUSMESSAGE_H

#include "tqdbusmacros.h"
#include "tqdbusdata.h"

#include <tqvaluelist.h>

#include <limits.h>

class TQT_DBusError;
class TQT_DBusMessagePrivate;
struct DBusMessage;

/**
 * @brief A message converts and transports data over D-Bus
 *
 * A TQT_DBusMessage is implicitly shared, similar to a TQString, i.e. copying
 * a message creates just a shallow copy.
 *
 * The TQT_DBusMessage is the TQt3 bindings means of encapsulating data for a
 * method call, a method reply or an error.
 *
 * Data specifying the sender and receipient is directly accessible through
 * getter methods, while data, e.g. method parameters or return values, are
 * managed as a list of TQT_DBusData.
 *
 * To create a message suitable for sending use one of the static factory
 * methods:
 * - signal() for creating a D-Bus signal message
 *
 * - methodCall() for creating a D-Bus method calls to a service object
 *
 * - methodReply() for creating a method reply on success
 *
 * - methodError() for creating a method reply on error
 *
 * @note for applications that just want to perform method calls and/or receive
 *       signals, it is usually more convenient to use TQT_DBusProxy instead.
 *
 * Message sending is achieved through TQT_DBusConnection
 *
 * Example:
 * @code
 *   TQT_DBusConnection con = TQT_DBusConnection::sessionBus();
 *
 *   // receipient service is the bus' main interface
 *
 *   TQString service   = "org.freedesktop.DBus";
 *   TQString path      = "/org/freedesktop/DBus";
 *   TQString interface = "org.freedesktop.DBus";
 *
 *   TQT_DBusMessage msg = TQBusMessage::methodCall(service, path, interface, "ListNames");
 *
 *   TQT_DBusMessage reply = con.sendWithReply(msg);
 *
 *   // awaiting for a message list
 *
 *   if (reply.type() != TQT_DBusMessage::ReplyMessage || reply.count() != 2 ||
 *       reply[0].type() != TQT_DBusData::List)
 *   {
 *       // error handling here
 *   }
 *   else
 *   {
 *       TQStringList list = reply[0].toTQStringList();
 *
 *       // reply handling here
 *   }
 * @endcode
 *
 * A service returning such a reply would do something like this
 * @code
 *   bool Service::handleMethodCall(const TQT_DBusMessage& call)
 *   {
 *       // checks for correctness, i.e. correct interface, member,
 *       // would usually haven been placed here
 *
 *       TQStringList result;
 *       result << "Foo" << "Bar";
 *
 *       TQT_DBusMessage reply = TQT_DBusMessage::methodReply(call);
 *       reply << TQT_DBusData::fromList(result);
 *
 *       connection.send(reply);
 *
 *       return true;
 *   }
 * @endcode
 */
class TQDBUS_EXPORT TQT_DBusMessage: public TQValueList<TQT_DBusData>
{
    friend class TQT_DBusConnection;
public:
    /**
     * @brief Anonymous enum for timeout constants
     *
     * @see timeout()
     * @see setTimeout()
     */
    enum
    {
        /**
         * Use whatever D-Bus has as default timeout
         */
        DefaultTimeout = -1,

        /**
         * Use no timeout at all, i.e. wait as long as necessary
         */
        NoTimeout = INT_MAX
    };

    /**
     * @brief D-Bus message types
     *
     * A message of a specific type can be created using the respective factory
     * method. A message created by the default constructor becomes an
     * InvalidMessage
     *
     * @see type()
     * @see signal()
     * @see methodCall()
     * @see methodReply()
     * @see methodError()
     */
    enum MessageType
    {
        /**
         * An invalid message cannot be sent over D-Bus. This type serves for
         * initializing message variables without requiring a "real" message
         */
        InvalidMessage,

        /**
         * A message for doing method calls on remote service objects
         *
         * @see methodCall()
         */
        MethodCallMessage,

        /**
         * A message for replying to a method call in case of success
         *
         * @see methodReply()
         */
        ReplyMessage,

        /**
         * A message for replying to a method call in case of failure
         *
         * @see methodError()
         */
        ErrorMessage,

        /**
         * A message for emitting D-Bus signals
         *
         * @see signal()
         */
        SignalMessage
    };

    /**
     * @brief Creates an empty and invalid message
     *
     * To create a message suitable for sending through D-Bus see the factory
     * methods signal(), methodCall(), methodReply() and methodError()
     *
     * @see #InvalidMessage
     */
    TQT_DBusMessage();


    /**
     * @brief Creates a shallow copy of the given message
     *
     * This instance will become a handle to the same message data the other
     * message is using, including #MessageType
     *
     * @param other the message to copy
     */
    TQT_DBusMessage(const TQT_DBusMessage &other);

    /**
     * @brief Destroys a message
     *
     * If this message handle is the last one using this respective message
     * content, the message content will be deleted as well
     */
    ~TQT_DBusMessage();

    /**
     * @brief Creates a shallow copy of the given message
     *
     * This instance will become a handle to the same message data the other
     * message is usingm including #MessageType
     *
     * Any content used in this instance will be deleted if this instance was
     * the last handle using that content
     *
     * @param other the message to copy
     *
     * @return a reference to this instance as required by assignment operator
     *         semantics
     */
    TQT_DBusMessage &operator=(const TQT_DBusMessage &other);

    /**
     * @brief Creates a message for sending a D-Bus signal
     *
     * Sending/emitting a signal over D-Bus requires a message of type
     * #SignalMessage as well as the information where it is coming from, i.e.
     * which interface of which object is sending it.
     * See @ref dbusconventions for recommendations on those parameters.
     *
     * @param path the object path of the service object
     * @param interface the object's interface to which the signal belongs
     * @param member the signal's name
     *
     * @return a message suitable for appending arguments and for sending
     *
     * @see TQT_DBusConnection::send()
     */
    static TQT_DBusMessage signal(const TQString &path, const TQString &interface,
                               const TQString &member);

    /**
     * @brief Creates a message for sending a D-Bus method call
     *
     * Invoking a method over D-Bus requires a message of type
     * #MethodCallMessage as well as the information where it should be sent
     * to, e.g which interface of which object in which service.
     * See @ref dbusconventions for recommendations on those parameters.
     *
     * @param service the D-Bus name of the application hosting the service
     *                object
     * @param path the object path of the service object
     * @param interface the object's interface to which the method belongs
     * @param method the method's name
     *
     * @return a message suitable for appending arguments and for sending
     *
     * @see methodReply()
     * @see methodError()
     * @see TQT_DBusConnection::send()
     */
    static TQT_DBusMessage methodCall(const TQString &service, const TQString &path,
                                   const TQString &interface, const TQString &method);

    /**
     * @brief Creates a message for replying to a D-Bus method call
     *
     * Replying to a D-Bus method call in the case of success requires a message
     * of type #ReplyMessage as well as the information to which method call it
     * is replying to.
     *
     * @param other the method call message it is replying to
     *
     * @return a message suitable for appending arguments and for sending
     *
     * @see methodCall()
     * @see methodError()
     * @see TQT_DBusConnection::send()
     */
    static TQT_DBusMessage methodReply(const TQT_DBusMessage &other);

    /**
     * @brief Creates a message for replying to a D-Bus method call
     *
     * Replying to a D-Bus method call in the case of failure requires a message
     * of type #ErrorMessage as well as the information to which method call it
     * is replying to and which error occured.
     *
     * @param other the method call message it is replying to
     * @param error the error which occured during during the method call
     *
     * @return a message suitable for appending arguments and for sending
     *
     * @see methodCall()
     * @see methodReply()
     * @see TQT_DBusConnection::send()
     */
    static TQT_DBusMessage methodError(const TQT_DBusMessage &other, const TQT_DBusError& error);

    /**
     * @brief Returns the message's object path
     *
     * See section @ref dbusconventions-objectpath for details.
     *
     * The context of the object path depends on the message type:
     * - #SignalMessage: the path of the service object which emits the signal
     * - #MethodCallMessage: the path of the service object the call is sent to
     * - #ReplyMessage: the path of the object which is replying
     * - #ErrorMessage: the path of the object which is replying
     *
     * @return a non-empty object path or @c TQString()
     *
     * @see interface()
     * @see member()
     * @see sender()
     */
    TQString path() const;

    /**
     * @brief Returns the message's interface name
     *
     * See section @ref dbusconventions-interfacename for details.
     *
     * The context of the interface name depends on the message type:
     * - #SignalMessage: the name of the interface which emits the signal
     * - #MethodCallMessage: the name of the interface the call is sent to
     * - #ReplyMessage: the name of the interface to which the method belonged
     * - #ErrorMessage: the name of the interface to which the method belonged
     *
     * @return a non-empty interface name or @c TQString()
     *
     * @see path()
     * @see member()
     * @see sender()
     */
    TQString interface() const;

    /**
     * @brief Returns the message's member name
     *
     * See section @ref dbusconventions-membername for details.
     *
     * The context of the member name depends on the message type:
     * - #SignalMessage: the name of the emitted signal
     * - #MethodCallMessage: the name of the method to call
     * - #ReplyMessage: the name of the method which replies
     * - #ErrorMessage: the name of the method which replies
     *
     * @return a non-empty member name or @c TQString()
     *
     * @see path()
     * @see interface()
     * @see sender()
     */
    TQString member() const;

    /**
     * @brief Returns the name of the message sender
     *
     * The message sender name or address used on the D-Bus message bus
     * to refer to the application which sent this message.
     *
     * See section @ref dbusconventions-servicename for details.
     *
     * This can either be a unique name as handed out by the bus, see
     * TQT_DBusConnection::uniqueName() or a name registered with
     * TQT_DBusConnection::requestName()
     *
     * @return a non-empty D-Bus sender name or @c TQString()
     *
     * @see path()
     * @see interface()
     * @see member()
     */
    TQString sender() const;

    /**
     * @brief Returns the error of an error message
     *
     * If this message is of type #ErrorMessage, this method can be used
     * to retrieve the respective error object
     *
     * @return the transported error object. Will be empty if this is not an
     *         error message
     *
     * @see type()
     */
    TQT_DBusError error() const;

    /**
     * @brief Returns which kind of message this is
     *
     * @return the message's type
     */
    MessageType type() const;

    /**
     * @brief Returns the message's timeout
     *
     * @return the asynchronous wait timeout in milliseconds
     *
     * @see setTimeout()
     */
    int timeout() const;

    /**
     * @brief Sets the message's timeout
     *
     * The timeout is the number of milliseconds the D-Bus connection will
     * wait for the reply of an asynchronous call.
     *
     * If no reply is received in time, an error message will be delivered to
     * the asynchronous reply receiver.
     *
     * If no timeout is set explicitly, #DefaultTimeout is assumed, which is
     * usually the best option
     *
     * @return the asynchronous wait timeout in milliseconds
     *
     * @param ms timeout in milliseconds
     *
     * @see timeout()
     * @see #DefaultTimeout
     * @see #NoTimeout
     */
    void setTimeout(int ms);

    /**
     * @brief Returns the message's serial number
     *
     * The serial number is some kind of short term identifier for messages
     * travelling the same connection.
     *
     * It can be used to associate a reply or error message with a method
     * call message.
     *
     * @return the message's serial number or @c 0 if the message hasn't been
     *         send yets
     *
     * @see replySerialNumber()
     */
    int serialNumber() const;

    /**
     * @brief Returns the message's reply serial number
     *
     * The reply serial number is the serial number of the method call message
     * this message is a reply to.
     *
     * If this is neither a message of type #ReplyMessage or #ErrorMessage, the
     * returned value will be @c 0
     *
     * It can be used to associate a reply or error message with a method
     * call message.
     *
     * @return the serial number of the associated method call message or @c 0
     *         if this message is not a reply message
     *
     * @see serialNumber()
     * @see methodReply()
     * @see methodError()
     * @see TQT_DBusConnection::sendWithAsyncReply()
     * @see TQT_DBusProxy::sendWithAsyncReply()
     */
    int replySerialNumber() const;

//protected:
    /**
     * @brief Creates a raw D-Bus message from this TQt3-bindings message
     *
     * Marshalls data contained in the message's value list into D-Bus data
     * format and creates a low level API D-Bus message for it.
     *
     * @note ownership of the returned message is transferred to the caller,
     *       i.e. it has to be deleted using dbus_message_unref()
     *
     * @return a C API D-Bus message or @c 0 if this is an #InvalidMessage
     *         or marshalling failed
     */
    DBusMessage *toDBusMessage() const;

    /**
     * @brief Creates a TQt3-bindings message from the given raw D-Bus message
     *
     * De-marshalls data contained in the message to a list of TQT_DBusData.
     *
     * @note ownership of the given message is shared between the caller and
     *       the returned message, i.e. the message as increased the reference
     *       counter and will still have access to the raw message even if the
     *       caller "deleted" it using dbus_message_unref()
     *
     * @param dmsg a C API D-Bus message
     *
     * @return a TQt3 bindings message. Can be an #InvalidMessage if the given
     *         message was @c 0 or if de-marshalling failed
     */
    static TQT_DBusMessage fromDBusMessage(DBusMessage *dmsg);

private:
    TQT_DBusMessagePrivate *d;
};

#endif