source: trunk/doc/src/qdbusadaptors.qdoc@ 321

Last change on this file since 321 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 22.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the documentation of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/** -*- mode: C++ -*-
43** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
44** Contact: Qt Software Information ([email protected])
45**
46** This file is part of the documentation of the Qt Toolkit.
47**
48** $QT_BEGIN_LICENSE:LGPL$
49** Commercial Usage
50** Licensees holding valid Qt Commercial licenses may use this file in
51** accordance with the Qt Commercial License Agreement provided with the
52** Software or, alternatively, in accordance with the terms contained in
53** a written agreement between you and Nokia.
54**
55** GNU Lesser General Public License Usage
56** Alternatively, this file may be used under the terms of the GNU Lesser
57** General Public License version 2.1 as published by the Free Software
58** Foundation and appearing in the file LICENSE.LGPL included in the
59** packaging of this file. Please review the following information to
60** ensure the GNU Lesser General Public License version 2.1 requirements
61** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
62**
63** In addition, as a special exception, Nokia gives you certain
64** additional rights. These rights are described in the Nokia Qt LGPL
65** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
66** package.
67**
68** GNU General Public License Usage
69** Alternatively, this file may be used under the terms of the GNU
70** General Public License version 3.0 as published by the Free Software
71** Foundation and appearing in the file LICENSE.GPL included in the
72** packaging of this file. Please review the following information to
73** ensure the GNU General Public License version 3.0 requirements will be
74** met: http://www.gnu.org/copyleft/gpl.html.
75**
76** If you are unsure which license is appropriate for your use, please
77** contact the sales department at [email protected].
78** $QT_END_LICENSE$
79**
80****************************************************************************/
81
82/*!
83 \page usingadaptors.html
84 \title Using QtDBus Adaptors
85
86 Adaptors are special classes that are attached to any QObject-derived class
87 and provide the interface to the external world using D-Bus. Adaptors are
88 intended to be lightweight classes whose main purpose is to relay calls to
89 and from the real object, possibly validating or converting the input from
90 the external world and, thus, protecting the real object.
91
92 Unlike multiple inheritance, adaptors can be added at any time to any object
93 (but not removed), which allows for greater flexibility when exporting
94 existing classes. Another advantage of adaptors is to provide similar but not
95 identical functionality in methods of the same name in different interfaces,
96 a case which can be quite common when adding a new version of a standard
97 interface to an object.
98
99 In order to use an adaptor, one must create a class which inherits
100 QDBusAbstractAdaptor. Since that is a standard QObject-derived class, the
101 Q_OBJECT macro must appear in the declaration and the source file must be
102 processed with the \l {moc} tool. The class must also contain one
103 Q_CLASSINFO entry with the \c {"D-Bus Interface"} name, declaring which
104 interface it is exporting. Only one entry per class is supported.
105
106 Any public slot in the class will be accessible through the bus over messages
107 of the MethodCall type. (See \l {Declaring Slots in D-Bus Adaptors} for more
108 information). Signals in the class will be automatically relayed over D-Bus.
109 However, not all types are allowed signals or slots' parameter lists: see
110 \l {The QtDBus Type System} for more information.
111
112 Also, any property declared with Q_PROPERTY will be automatically exposed
113 over the Properties interface on D-Bus. Since the QObject property system
114 does not allow for non-readable properties, it is not possible to declare
115 write-only properties using adaptors.
116
117 More information:
118 \list
119 \o \l{Declaring Slots in D-Bus Adaptors}
120 \o \l{Declaring Signals in D-Bus Adaptors}
121 \o \l{The QtDBus Type System}
122 \o \l{D-Bus Adaptor Example}
123 \endlist
124
125 \sa QDBusAbstractAdaptor
126*/
127
128/*!
129 \page qdbusadaptorexample.html
130 \title D-Bus Adaptor Example
131
132 The following example code shows how a D-Bus interface can be implemented
133 using an adaptor.
134
135 A sample usage of QDBusAbstractAdaptor is as follows:
136 \snippet doc/src/snippets/code/doc_src_qdbusadaptors.qdoc 0
137
138 The code above would create an interface that could be represented more or less in the following
139 canonical representation:
140 \snippet doc/src/snippets/code/doc_src_qdbusadaptors.qdoc 1
141
142 This adaptor could be used in the application's main function as follows
143 \snippet doc/src/snippets/code/doc_src_qdbusadaptors.qdoc 2
144
145 Break-down analysis:
146 \tableofcontents
147
148 \section1 The header
149
150 The header of the example is:
151 \snippet doc/src/snippets/code/doc_src_qdbusadaptors.qdoc 3
152
153 The code does the following:
154 \list
155 \o it declares the adaptor MainApplicationAdaptor, which descends from QDBusAbstractAdaptor
156 \o it declares the Qt meta-object data using the Q_OBJECT macro
157 \o it declares the name of the D-Bus interface it implements.
158 \endlist
159
160 \section1 The properties
161
162 The properties are declared as follows:
163 \snippet doc/src/snippets/code/doc_src_qdbusadaptors.qdoc 4
164
165 And are implemented as follows:
166 \snippet doc/src/snippets/code/doc_src_qdbusadaptors.qdoc 5
167
168 The code declares three properties: one of them is a read-write property called "caption" of
169 string type. The other two are read-only, also of the string type.
170
171 The properties organizationName and organizationDomain are simple relays of the app object's
172 organizationName and organizationDomain properties. However, the caption property requires
173 verifying if the application has a main window associated with it: if there isn't any, the
174 caption property is empty. Note how it is possible to access data defined in other objects
175 through the getter/setter functions.
176
177 \section1 The constructor
178
179 The constructor:
180 \snippet doc/src/snippets/code/doc_src_qdbusadaptors.qdoc 6
181
182 The constructor does the following:
183 \list
184 \o it initialises its base class (QDBusAbstractAdaptor) with the parent object it is related to.
185 \o it stores the app pointer in a member variable. Note that it would be possible to access the
186 same object using the QDBusAbstractAdaptor::object() function, but it would be necessary to
187 use \a static_cast<> to properly access the methods in QApplication that are not part of
188 QObject.
189 \o it connects the application's signal \a aboutToQuit to its own signal \a aboutToQuit.
190 \o it connects the application's signal \a focusChanged to a private slot to do some further
191 processing before emitting a D-Bus signal.
192 \endlist
193
194 Note that there is no destructor in the example. An eventual destructor could be used to emit
195 one last signal before the object is destroyed, for instance.
196
197 \section1 Slots/methods
198
199 The public slots in the example (which will be exported as D-Bus methods) are the following:
200 \snippet doc/src/snippets/code/doc_src_qdbusadaptors.qdoc 7
201
202 This snippet of code defines 4 methods with different properties each:
203 \list 1
204 \o \c quit: this method takes no parameters and is defined to be asynchronous. That is, callers
205 are expected to use "fire-and-forget" mechanism when calling this method, since it provides no
206 useful reply. This is represented in D-Bus by the use of the
207 org.freedesktop.DBus.Method.NoReply annotation. See \l Q_NOREPLY for more information on
208 asynchronous methods
209
210 \o \c reparseConfiguration: this simple method, with no input or output arguments simply relays
211 the call to the application's reparseConfiguration member function.
212
213 \o \c mainWindowObject: this method takes no input parameter, but returns one string output
214 argument, containing the path to the main window object (if the application has a main
215 window), or an empty string if it has no main window. Note that this method could have also
216 been written: void mainWindowObject(QString &path).
217
218 \o \c setSessionManagement: this method takes one input argument (a boolean) and, depending on
219 its value, it calls one function or another in the application.
220 \endlist
221
222 See also: \l Q_NOREPLY.
223
224 \section1 Signals
225
226 The signals in this example are defined as follows:
227 \snippet doc/src/snippets/code/doc_src_qdbusadaptors.qdoc 8
228
229 However, signal definition isn't enough: signals have to be emitted. One simple way of emitting
230 signals is to connect another signal to them, so that Qt's signal handling system chains them
231 automatically. This is what is done for the \a aboutToQuit signal.
232
233 When this is the case, one can use the QDBusAbstractAdaptor::setAutoRelaySignals to
234 automatically connect every signal from the real object to the adaptor.
235
236 When simple signal-to-signal connection isn't enough, one can use a private slot do do some
237 work. This is what was done for the mainWindowHasFocus signal:
238 \snippet doc/src/snippets/code/doc_src_qdbusadaptors.qdoc 9
239
240 This private slot (which will not be exported as a method via D-Bus) was connected to the
241 \c focusChanged signal in the adaptor's constructor. It is therefore able to shape the
242 application's signal into what the interface expects it to be.
243*/
244
245/*!
246 \page qdbusdeclaringslots.html
247 \title Declaring Slots in D-Bus Adaptors
248
249 Slots in D-Bus adaptors are declared just like normal, public slots, but their
250 parameters must follow certain rules (see \l{The QtDBus Type System} for more
251 information). Slots whose parameters do not follow those rules or that are not
252 public will not be accessible via D-Bus.
253
254 Slots can have one parameter of type \c{const QDBusMessage &}, which must
255 appear at the end of the input parameter list, before any output parameters.
256 This parameter, if present, will be initialized with a copy of the
257 current message being processed, which allows the callee to obtain
258 information about the caller, such as its connection name.
259
260 Slots can be of three kinds:
261 \list 1
262 \o Asynchronous
263 \o Input-only
264 \o Input-and-output
265 \endlist
266
267 \section1 Asynchronous Slots
268 Asynchronous slots are those that do not normally return any reply to the
269 caller. For that reason, they cannot take any output parameters. In most
270 cases, by the time the first line of the slot is run, the caller function
271 has already resumed working.
272
273 However, slots must not rely on that behavior. Scheduling and message-dispatching
274 issues could change the order in which the slot is run. Code intending to
275 synchronize with the caller should provide its own method of synchronization.
276
277 Asynchronous slots are marked by the keyword \l Q_NOREPLY in the method
278 signature, before the \c void return type and the slot name. (See the
279 \c quit() slot in the \l{D-Bus Adaptor Example}).
280
281 \section1 Input-Only Slots
282
283 Input-only slots are normal slots that take parameters passed by value or
284 by constant reference. However, unlike asynchronous slots, the caller is
285 usually waiting for completion of the callee before resuming operation.
286 Therefore, non-asynchronous slots should not block or should state it its
287 documentation that they may do so.
288
289 Input-only slots have no special marking in their signature, except that
290 they take only parameters passed by value or by constant reference.
291 Optionally, slots can take a QDBusMessage parameter as a last parameter,
292 which can be used to perform additional analysis of the method call message.
293
294 \section1 Input and Output Slots
295
296 Like input-only slots, input-and-output slots are those that the caller is
297 waiting for a reply. Unlike input-only ones, though, this reply will contain
298 data. Slots that output data may contain non-constant references and may
299 return a value as well. However, the output parameters must all appear at
300 the end of the argument list and may not have input arguments interleaved.
301 Optionally, a QDBusMessage argument may appear between the input and the
302 output arguments.
303
304 \section1 Automatic Replies
305
306 Method replies are generated automatically with the contents of the output
307 parameters (if there were any) by the QtDBus implementation. Slots need not
308 worry about constructing proper QDBusMessage objects and sending them over
309 the connection.
310
311 However, the possibility of doing so remains there. Should the slot find out
312 it needs to send a special reply or even an error, it can do so by using
313 QDBusMessage::createReply() or QDBusMessage::createErrorReply() on the
314 QDBusMessage parameter and send it with QDBusConnection::send(). The
315 QtDBus implementation will not generate any reply if the slot did so.
316
317 \warning When a caller places a method call and waits for a reply, it will
318 only wait for a limited amount of time. Slots intending to take a long time
319 to complete should make that fact clear in documentation so that callers
320 properly set higher timeouts.
321
322 \section1 Delayed Replies
323
324 In some circumstances, the called slot may not be able to process
325 the request immediately. This is frequently the case when the
326 request involves an I/O or networking operation which may block.
327
328 If this is the case, the slot should return control to the
329 application's main loop to avoid freezing the user interface, and
330 resume the process later. To accomplish this, it should make use
331 of the extra \c QDBusMessage parameter at the end of the input
332 parameter list and request a delayed reply.
333
334 We do this by writing a slot that stores the request data in a
335 persistent structure, indicating to the caller using
336 \l{QDBusMessage::setDelayedReply()}{QDBusMessage::setDelayedReply(true)}
337 that the response will be sent later.
338
339 \snippet doc/src/snippets/code/doc_src_qdbusadaptors.qdoc 10
340
341 The use of
342 \l{QDBusConnection::send()}{QDBusConnection::sessionBus().send(data->reply)}
343 is needed to explicitly inform the caller that the response will be delayed.
344 In this case, the return value is unimportant; we return an arbitrary value
345 to satisfy the compiler.
346
347 When the request is processed and a reply is available, it should be sent
348 using the \c QDBusMessage object that was obtained. In our example, the
349 reply code could be something as follows:
350
351 \snippet doc/src/snippets/code/doc_src_qdbusadaptors.qdoc 11
352
353 As can be seen in the example, when a delayed reply is in place,
354 the return value(s) from the slot will be ignored by QtDBus. They
355 are used only to determine the slot's signature when communicating
356 the adaptor's description to remote applications, or in case the
357 code in the slot decides not to use a delayed reply.
358
359 The delayed reply itself is requested from QtDBus by calling
360 QDBusMessage::reply() on the original message. It then becomes the
361 resposibility of the called code to eventually send a reply to the
362 caller.
363
364 \warning When a caller places a method call and waits for a reply, it will
365 only wait for a limited amount of time. Slots intending to take a long time
366 to complete should make that fact clear in documentation so that callers
367 properly set higher timeouts.
368
369 \sa {Using QtDBus Adaptors}, {Declaring Signals in D-Bus Adaptors},
370 {The QtDBus Type System}, QDBusConnection, QDBusMessage
371*/
372
373/*!
374 \page qdbusdeclaringsignals.html
375 \title Declaring Signals in D-Bus Adaptors
376
377 Any signal in a class derived from QDBusAbstractAdaptor will be automatically
378 relayed into D-Bus, provided that the signal's parameters conform to certain
379 rules (see \l{The QtDBus Type System} for more information). No special code
380 is necessary to make this relay.
381
382 However, signals must still be emitted. The easiest way to emit an adaptor
383 signal is to connect another signal to it, so that Qt's signals and slots
384 mechanism automatically emits the adaptor signal, too. This can be done in
385 the adaptor's constructor, as has been done in the
386 \l{D-Bus Adaptor Example}{D-Bus Adaptor example}.
387
388 The QDBusAbstractAdaptor::setAutoRelaySignals() convenience function can also
389 be used to make and break connections between signals in the real object and
390 the corresponding signals in the adaptor. It will inspect the list of signals
391 in both classes and connect those whose parameters match exactly.
392
393 \sa {Using QtDBus Adaptors},
394 {Declaring Slots in D-Bus Adaptors},
395 {The QtDBus Type System}, QDBusAbstractAdaptor
396*/
397
398/*!
399 \page qdbustypesystem.html
400 \title The QtDBus Type System
401
402 D-Bus has an extensible type system based on a few primitives and
403 composition of the primitives in arrays and structures. QtDBus
404 implements the interface to that type system through the
405 QDBusArgument class, allowing user programs to send and receive
406 practically every C++ type over the bus.
407
408 \section1 Primitive Types
409
410 The primitive types are supported natively by QDBusArgument and
411 need no special customization to be sent or received. They are
412 listed below, along with the C++ class they relate to:
413
414 \table
415 \header
416 \o Qt type
417 \o D-Bus equivalent type
418 \row
419 \o uchar
420 \o BYTE
421 \row
422 \o bool
423 \o BOOLEAN
424 \row
425 \o short
426 \o INT16
427 \row
428 \o ushort
429 \o UINT16
430 \row
431 \o int
432 \o INT32
433 \row
434 \o uint
435 \o UINT32
436 \row
437 \o qlonglong
438 \o INT64
439 \row
440 \o qulonglong
441 \o UINT64
442 \row
443 \o double
444 \o DOUBLE
445 \row
446 \o QString
447 \o STRING
448 \row
449 \o QDBusVariant
450 \o VARIANT
451 \row
452 \o QDBusObjectPath
453 \o OBJECT_PATH
454 \row
455 \o QDBusSignature
456 \o SIGNATURE
457 \endtable
458
459 Aside from the primitive types, QDBusArgument also supports two
460 non-primitive types natively, due to their widespread use in Qt
461 applications: QStringList and QByteArray.
462
463 \section1 Compound Types
464
465 D-Bus specifies three types of aggregations of primitive types
466 that allow one to create compound types. They are \c ARRAY, \c
467 STRUCT and maps/dictionaries.
468
469 Arrays are sets of zero or more elements of the same type, while
470 structures are a set of a fixed number of elements, each of any
471 type. Maps or dictionaries are implemented as arrays of a pair of
472 elements, so there can be zero or more elements in one map.
473
474 \section1 Extending the Type System
475
476 In order to use one's own type with QtDBus, the type has to be
477 declared as a Qt meta-type with the Q_DECLARE_METATYPE() macro and
478 registered with the qDBusRegisterMetaType() function. The
479 streaming operators \c{operator>>} and \c{operator<<} will be
480 automatically found by the registration system.
481
482 QtDBus provides template specializations for arrays and maps for
483 use with Qt's \l{Container classes}{container classes}, such as
484 QMap and QList, so it is not necessary to write the streaming
485 operator functions for those. For other types, and specially for
486 types implementing structures, the operators have to be explicitly
487 implemented.
488
489 See the documentation for QDBusArgument for examples for
490 structures, arrays and maps.
491
492 \section1 The Type System in Use
493
494 All of the QtDBus types (primitives and user-defined alike) can be
495 used to send and receive messages of all types over the bus.
496
497 \warning You may not use any type that is not on the list above,
498 including \a typedefs to the types listed. This also includes
499 QList<QVariant> and QMap<QString,QVariant>.
500*/
501
502/*!
503 \macro Q_NOREPLY
504 \relates QDBusAbstractAdaptor
505 \since 4.2
506
507 The Q_NOREPLY macro can be used to mark a method to be called and not wait for it to finish
508 processing before returning from QDBusInterface::call(). The called method cannot return any
509 output arguments and, if it does, any such arguments will be discarded.
510
511 You can use this macro in your own adaptors by placing it before your method's return value
512 (which must be "void") in the class declaration, as shown in the example:
513 \snippet doc/src/snippets/code/doc_src_qdbusadaptors.qdoc 12
514
515 Its presence in the method implementation (outside the class declaration) is optional.
516
517 \sa {Using QtDBus Adaptors}
518*/
Note: See TracBrowser for help on using the repository browser.