PageRenderTime 27ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/dbus/qdbuscontext.cpp

https://bitbucket.org/kasimling/qt
C++ | 208 lines | 60 code | 20 blank | 128 comment | 2 complexity | 4f4e6fd3b7d96f26299d1f7b77ebe50a MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the QtDBus module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "qdbusmessage.h"
  42. #include "qdbusconnection.h"
  43. #include "qdbusabstractadaptor.h"
  44. #include "qdbuscontext.h"
  45. #include "qdbuscontext_p.h"
  46. #ifndef QT_NO_DBUS
  47. QT_BEGIN_NAMESPACE
  48. QDBusContextPrivate *QDBusContextPrivate::set(QObject *obj, QDBusContextPrivate *newContext)
  49. {
  50. // determine if this is an adaptor or not
  51. if (qobject_cast<QDBusAbstractAdaptor *>(obj))
  52. obj = obj->parent();
  53. Q_ASSERT(obj);
  54. void *ptr = obj->qt_metacast("QDBusContext");
  55. QDBusContext *q_ptr = reinterpret_cast<QDBusContext *>(ptr);
  56. if (q_ptr) {
  57. QDBusContextPrivate *old = q_ptr->d_ptr;
  58. q_ptr->d_ptr = newContext;
  59. return old;
  60. }
  61. return 0;
  62. }
  63. /*!
  64. \since 4.3
  65. \class QDBusContext
  66. \inmodule QtDBus
  67. \brief The QDBusContext class allows slots to determine the D-Bus context of the calls.
  68. When a slot is called in an object due to a signal delivery or due
  69. to a remote method call, it is sometimes necessary to know the
  70. context in which that happened. In particular, if the slot
  71. determines that it wants to send the reply at a later opportunity
  72. or if it wants to reply with an error, the context is needed.
  73. The QDBusContext class is an alternative to accessing the context
  74. that doesn't involve modifying the code generated by the \l
  75. {QtDBus XML Compiler (qdbusxml2cpp)}.
  76. QDBusContext is used by subclassing it from the objects being
  77. exported using QDBusConnection::registerObject(). The following
  78. example illustrates the usage:
  79. \snippet doc/src/snippets/code/src_qdbus_qdbuscontext.cpp 0
  80. The example illustrates the two typical uses, that of sending
  81. error replies and that of delayed replies.
  82. Note: do not subclass QDBusContext and QDBusAbstractAdaptor at the
  83. same time. QDBusContext should appear in the real object, not the
  84. adaptor. If it's necessary from the adaptor code to determine the
  85. context, use a public inheritance and access the functions via
  86. QObject::parent().
  87. */
  88. /*!
  89. Constructs an empty QDBusContext.
  90. */
  91. QDBusContext::QDBusContext()
  92. : d_ptr(0)
  93. {
  94. }
  95. /*!
  96. An empty destructor.
  97. */
  98. QDBusContext::~QDBusContext()
  99. {
  100. }
  101. /*!
  102. Returns true if we are processing a D-Bus call. If this function
  103. returns true, the rest of the functions in this class are
  104. available.
  105. Accessing those functions when this function returns false is
  106. undefined and may lead to crashes.
  107. */
  108. bool QDBusContext::calledFromDBus() const
  109. {
  110. return d_ptr;
  111. }
  112. /*!
  113. Returns the connection from which this call was received.
  114. */
  115. QDBusConnection QDBusContext::connection() const
  116. {
  117. return d_ptr->connection;
  118. }
  119. /*!
  120. Returns the message that generated this call.
  121. */
  122. const QDBusMessage &QDBusContext::message() const
  123. {
  124. return d_ptr->message;
  125. }
  126. /*!
  127. Returns true if this call will have a delayed reply.
  128. \sa setDelayedReply()
  129. */
  130. bool QDBusContext::isDelayedReply() const
  131. {
  132. return message().isDelayedReply();
  133. }
  134. /*!
  135. Sets whether this call will have a delayed reply or not.
  136. If \a enable is false, QtDBus will automatically generate a reply
  137. back to the caller, if needed, as soon as the called slot returns.
  138. If \a enable is true, QtDBus will not generate automatic
  139. replies. It will also ignore the return value from the slot and
  140. any output parameters. Instead, the called object is responsible
  141. for storing the incoming message and send a reply or error at a
  142. later time.
  143. Failing to send a reply will result in an automatic timeout error
  144. being generated by D-Bus.
  145. */
  146. void QDBusContext::setDelayedReply(bool enable) const
  147. {
  148. message().setDelayedReply(enable);
  149. }
  150. /*!
  151. Sends an error \a name as a reply to the caller. The optional \a
  152. msg parameter is a human-readable text explaining the failure.
  153. If an error is sent, the return value and any output parameters
  154. from the called slot will be ignored by QtDBus.
  155. */
  156. void QDBusContext::sendErrorReply(const QString &name, const QString &msg) const
  157. {
  158. setDelayedReply(true);
  159. connection().send(message().createErrorReply(name, msg));
  160. }
  161. /*!
  162. \overload
  163. Sends an error \a type as a reply to the caller. The optional \a
  164. msg parameter is a human-readable text explaining the failure.
  165. If an error is sent, the return value and any output parameters
  166. from the called slot will be ignored by QtDBus.
  167. */
  168. void QDBusContext::sendErrorReply(QDBusError::ErrorType type, const QString &msg) const
  169. {
  170. setDelayedReply(true);
  171. connection().send(message().createErrorReply(type, msg));
  172. }
  173. QT_END_NAMESPACE
  174. #endif // QT_NO_DBUS