src/dbus/qdbuspendingcall.cpp
changeset 29 b72c6db6890b
parent 18 2f34d5167611
child 30 5dc02b23752f
equal deleted inserted replaced
25:e24348a560a6 29:b72c6db6890b
   206     expectedReplySignature = QString::fromLatin1(sig);
   206     expectedReplySignature = QString::fromLatin1(sig);
   207 }
   207 }
   208 
   208 
   209 void QDBusPendingCallPrivate::checkReceivedSignature()
   209 void QDBusPendingCallPrivate::checkReceivedSignature()
   210 {
   210 {
       
   211     // MUST BE CALLED WITH A LOCKED MUTEX!
       
   212 
   211     if (replyMessage.type() == QDBusMessage::InvalidMessage)
   213     if (replyMessage.type() == QDBusMessage::InvalidMessage)
   212         return;                 // not yet finished - no message to
   214         return;                 // not yet finished - no message to
   213                                 // validate against
   215                                 // validate against
   214     if (replyMessage.type() == QDBusMessage::ErrorMessage)
   216     if (replyMessage.type() == QDBusMessage::ErrorMessage)
   215         return;                 // we don't have to check the signature of an error reply
   217         return;                 // we don't have to check the signature of an error reply
   228     }
   230     }
   229 }
   231 }
   230 
   232 
   231 void QDBusPendingCallPrivate::waitForFinished()
   233 void QDBusPendingCallPrivate::waitForFinished()
   232 {
   234 {
       
   235     QMutexLocker locker(&mutex);
       
   236 
   233     if (replyMessage.type() != QDBusMessage::InvalidMessage)
   237     if (replyMessage.type() != QDBusMessage::InvalidMessage)
   234         return;                 // already finished
   238         return;                 // already finished
   235 
   239 
   236     connection->waitForFinished(this);
   240     connection->waitForFinished(this);
   237 }
   241 }
   308     \sa QDBusPendingCallWatcher::isFinished()
   312     \sa QDBusPendingCallWatcher::isFinished()
   309 */
   313 */
   310 
   314 
   311 bool QDBusPendingCall::isFinished() const
   315 bool QDBusPendingCall::isFinished() const
   312 {
   316 {
   313     return !d || (d->replyMessage.type() != QDBusMessage::InvalidMessage);
   317     if (!d)
       
   318         return true; // considered finished
       
   319 
       
   320     QMutexLocker locker(&d->mutex);
       
   321     return d->replyMessage.type() != QDBusMessage::InvalidMessage;
   314 }
   322 }
   315 
   323 
   316 void QDBusPendingCall::waitForFinished()
   324 void QDBusPendingCall::waitForFinished()
   317 {
   325 {
   318     if (d) d->waitForFinished();
   326     if (d) d->waitForFinished();
   327     If the pending call has not finished processing, this function
   335     If the pending call has not finished processing, this function
   328     return false.
   336     return false.
   329 */
   337 */
   330 bool QDBusPendingCall::isValid() const
   338 bool QDBusPendingCall::isValid() const
   331 {
   339 {
   332     return d ? d->replyMessage.type() == QDBusMessage::ReplyMessage : false;
   340     if (!d)
       
   341         return false;
       
   342     QMutexLocker locker(&d->mutex);
       
   343     return d->replyMessage.type() == QDBusMessage::ReplyMessage;
   333 }
   344 }
   334 
   345 
   335 /*!
   346 /*!
   336     \fn bool QDBusPendingReply::isError() const
   347     \fn bool QDBusPendingReply::isError() const
   337 
   348 
   341     If the pending call has not finished processing, this function
   352     If the pending call has not finished processing, this function
   342     also returns true.
   353     also returns true.
   343 */
   354 */
   344 bool QDBusPendingCall::isError() const
   355 bool QDBusPendingCall::isError() const
   345 {
   356 {
   346     return d ? d->replyMessage.type() == QDBusMessage::ErrorMessage : true;
   357     if (!d)
       
   358         return true; // considered finished and an error
       
   359     QMutexLocker locker(&d->mutex);
       
   360     return d->replyMessage.type() == QDBusMessage::ErrorMessage;
   347 }
   361 }
   348 
   362 
   349 /*!
   363 /*!
   350     \fn QDBusError QDBusPendingReply::error() const
   364     \fn QDBusError QDBusPendingReply::error() const
   351 
   365 
   354     processing or if it contains a normal reply message (non-error),
   368     processing or if it contains a normal reply message (non-error),
   355     this function returns an invalid QDBusError.
   369     this function returns an invalid QDBusError.
   356 */
   370 */
   357 QDBusError QDBusPendingCall::error() const
   371 QDBusError QDBusPendingCall::error() const
   358 {
   372 {
   359     if (d)
   373     if (d) {
       
   374         QMutexLocker locker(&d->mutex);
   360         return d->replyMessage;
   375         return d->replyMessage;
       
   376     }
   361 
   377 
   362     // not connected, return an error
   378     // not connected, return an error
   363     QDBusError err = QDBusError(QDBusError::Disconnected,
   379     QDBusError err = QDBusError(QDBusError::Disconnected,
   364                                 QLatin1String("Not connected to D-Bus server"));
   380                                 QLatin1String("Not connected to D-Bus server"));
   365     return err;
   381     return err;
   376     After it has finished processing, the message type will either be
   392     After it has finished processing, the message type will either be
   377     an error message or a normal method reply message.
   393     an error message or a normal method reply message.
   378 */
   394 */
   379 QDBusMessage QDBusPendingCall::reply() const
   395 QDBusMessage QDBusPendingCall::reply() const
   380 {
   396 {
   381     return d ? d->replyMessage : QDBusMessage::createError(error());
   397     if (!d)
       
   398         return QDBusMessage::createError(error());
       
   399     QMutexLocker locker(&d->mutex);
       
   400     return d->replyMessage;
   382 }
   401 }
   383 
   402 
   384 #if 0
   403 #if 0
   385 /*!
   404 /*!
   386     Sets the slot \a member in object \a target to be called when the
   405     Sets the slot \a member in object \a target to be called when the
   437 QDBusPendingCall QDBusPendingCall::fromCompletedCall(const QDBusMessage &msg)
   456 QDBusPendingCall QDBusPendingCall::fromCompletedCall(const QDBusMessage &msg)
   438 {
   457 {
   439     QDBusPendingCallPrivate *d = 0;
   458     QDBusPendingCallPrivate *d = 0;
   440     if (msg.type() == QDBusMessage::ErrorMessage ||
   459     if (msg.type() == QDBusMessage::ErrorMessage ||
   441         msg.type() == QDBusMessage::ReplyMessage) {
   460         msg.type() == QDBusMessage::ReplyMessage) {
   442         d = new QDBusPendingCallPrivate;
   461         d = new QDBusPendingCallPrivate(QDBusMessage(), 0);
   443         d->replyMessage = msg;
   462         d->replyMessage = msg;
   444         d->connection = 0;
       
   445     }
   463     }
   446 
   464 
   447     return QDBusPendingCall(d);
   465     return QDBusPendingCall(d);
   448 }
   466 }
   449 
   467 
   469 */
   487 */
   470 QDBusPendingCallWatcher::QDBusPendingCallWatcher(const QDBusPendingCall &call, QObject *parent)
   488 QDBusPendingCallWatcher::QDBusPendingCallWatcher(const QDBusPendingCall &call, QObject *parent)
   471     : QObject(*new QDBusPendingCallWatcherPrivate, parent), QDBusPendingCall(call)
   489     : QObject(*new QDBusPendingCallWatcherPrivate, parent), QDBusPendingCall(call)
   472 {
   490 {
   473     if (d) {                    // QDBusPendingCall::d
   491     if (d) {                    // QDBusPendingCall::d
       
   492         QMutexLocker locker(&d->mutex);
   474         if (!d->watcherHelper) {
   493         if (!d->watcherHelper) {
   475             d->watcherHelper = new QDBusPendingCallWatcherHelper;
   494             d->watcherHelper = new QDBusPendingCallWatcherHelper;
   476             if (isFinished()) {
   495             if (d->replyMessage.type() != QDBusMessage::InvalidMessage) {
   477                 // cause a signal emission anyways
   496                 // cause a signal emission anyways
   478                 QMetaObject::invokeMethod(d->watcherHelper, "finished", Qt::QueuedConnection);
   497                 QMetaObject::invokeMethod(d->watcherHelper, "finished", Qt::QueuedConnection);
   479             }
   498             }
   480         }
   499         }
   481         d->watcherHelper->add(this);
   500         d->watcherHelper->add(this);