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