src/dbus/qdbuspendingcall.cpp
changeset 30 5dc02b23752f
parent 29 b72c6db6890b
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
    45 #include "qdbusconnection_p.h"
    45 #include "qdbusconnection_p.h"
    46 #include "qdbusmetatype_p.h"
    46 #include "qdbusmetatype_p.h"
    47 #include "qcoreapplication.h"
    47 #include "qcoreapplication.h"
    48 #include "qcoreevent.h"
    48 #include "qcoreevent.h"
    49 #include <private/qobject_p.h>
    49 #include <private/qobject_p.h>
       
    50 
       
    51 #ifndef QT_NO_DBUS
    50 
    52 
    51 QT_BEGIN_NAMESPACE
    53 QT_BEGIN_NAMESPACE
    52 
    54 
    53 /*!
    55 /*!
    54     \class QDBusPendingCall
    56     \class QDBusPendingCall
   206     expectedReplySignature = QString::fromLatin1(sig);
   208     expectedReplySignature = QString::fromLatin1(sig);
   207 }
   209 }
   208 
   210 
   209 void QDBusPendingCallPrivate::checkReceivedSignature()
   211 void QDBusPendingCallPrivate::checkReceivedSignature()
   210 {
   212 {
   211     // MUST BE CALLED WITH A LOCKED MUTEX!
       
   212 
       
   213     if (replyMessage.type() == QDBusMessage::InvalidMessage)
   213     if (replyMessage.type() == QDBusMessage::InvalidMessage)
   214         return;                 // not yet finished - no message to
   214         return;                 // not yet finished - no message to
   215                                 // validate against
   215                                 // validate against
   216     if (replyMessage.type() == QDBusMessage::ErrorMessage)
   216     if (replyMessage.type() == QDBusMessage::ErrorMessage)
   217         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
   230     }
   230     }
   231 }
   231 }
   232 
   232 
   233 void QDBusPendingCallPrivate::waitForFinished()
   233 void QDBusPendingCallPrivate::waitForFinished()
   234 {
   234 {
   235     QMutexLocker locker(&mutex);
       
   236 
       
   237     if (replyMessage.type() != QDBusMessage::InvalidMessage)
   235     if (replyMessage.type() != QDBusMessage::InvalidMessage)
   238         return;                 // already finished
   236         return;                 // already finished
   239 
   237 
   240     connection->waitForFinished(this);
   238     connection->waitForFinished(this);
   241 }
   239 }
   312     \sa QDBusPendingCallWatcher::isFinished()
   310     \sa QDBusPendingCallWatcher::isFinished()
   313 */
   311 */
   314 
   312 
   315 bool QDBusPendingCall::isFinished() const
   313 bool QDBusPendingCall::isFinished() const
   316 {
   314 {
   317     if (!d)
   315     return !d || (d->replyMessage.type() != QDBusMessage::InvalidMessage);
   318         return true; // considered finished
       
   319 
       
   320     QMutexLocker locker(&d->mutex);
       
   321     return d->replyMessage.type() != QDBusMessage::InvalidMessage;
       
   322 }
   316 }
   323 
   317 
   324 void QDBusPendingCall::waitForFinished()
   318 void QDBusPendingCall::waitForFinished()
   325 {
   319 {
   326     if (d) d->waitForFinished();
   320     if (d) d->waitForFinished();
   335     If the pending call has not finished processing, this function
   329     If the pending call has not finished processing, this function
   336     return false.
   330     return false.
   337 */
   331 */
   338 bool QDBusPendingCall::isValid() const
   332 bool QDBusPendingCall::isValid() const
   339 {
   333 {
   340     if (!d)
   334     return d ? d->replyMessage.type() == QDBusMessage::ReplyMessage : false;
   341         return false;
       
   342     QMutexLocker locker(&d->mutex);
       
   343     return d->replyMessage.type() == QDBusMessage::ReplyMessage;
       
   344 }
   335 }
   345 
   336 
   346 /*!
   337 /*!
   347     \fn bool QDBusPendingReply::isError() const
   338     \fn bool QDBusPendingReply::isError() const
   348 
   339 
   352     If the pending call has not finished processing, this function
   343     If the pending call has not finished processing, this function
   353     also returns true.
   344     also returns true.
   354 */
   345 */
   355 bool QDBusPendingCall::isError() const
   346 bool QDBusPendingCall::isError() const
   356 {
   347 {
   357     if (!d)
   348     return d ? d->replyMessage.type() == QDBusMessage::ErrorMessage : true;
   358         return true; // considered finished and an error
       
   359     QMutexLocker locker(&d->mutex);
       
   360     return d->replyMessage.type() == QDBusMessage::ErrorMessage;
       
   361 }
   349 }
   362 
   350 
   363 /*!
   351 /*!
   364     \fn QDBusError QDBusPendingReply::error() const
   352     \fn QDBusError QDBusPendingReply::error() const
   365 
   353 
   368     processing or if it contains a normal reply message (non-error),
   356     processing or if it contains a normal reply message (non-error),
   369     this function returns an invalid QDBusError.
   357     this function returns an invalid QDBusError.
   370 */
   358 */
   371 QDBusError QDBusPendingCall::error() const
   359 QDBusError QDBusPendingCall::error() const
   372 {
   360 {
   373     if (d) {
   361     if (d)
   374         QMutexLocker locker(&d->mutex);
       
   375         return d->replyMessage;
   362         return d->replyMessage;
   376     }
       
   377 
   363 
   378     // not connected, return an error
   364     // not connected, return an error
   379     QDBusError err = QDBusError(QDBusError::Disconnected,
   365     QDBusError err = QDBusError(QDBusError::Disconnected,
   380                                 QLatin1String("Not connected to D-Bus server"));
   366                                 QLatin1String("Not connected to D-Bus server"));
   381     return err;
   367     return err;
   392     After it has finished processing, the message type will either be
   378     After it has finished processing, the message type will either be
   393     an error message or a normal method reply message.
   379     an error message or a normal method reply message.
   394 */
   380 */
   395 QDBusMessage QDBusPendingCall::reply() const
   381 QDBusMessage QDBusPendingCall::reply() const
   396 {
   382 {
   397     if (!d)
   383     return d ? d->replyMessage : QDBusMessage::createError(error());
   398         return QDBusMessage::createError(error());
       
   399     QMutexLocker locker(&d->mutex);
       
   400     return d->replyMessage;
       
   401 }
   384 }
   402 
   385 
   403 #if 0
   386 #if 0
   404 /*!
   387 /*!
   405     Sets the slot \a member in object \a target to be called when the
   388     Sets the slot \a member in object \a target to be called when the
   456 QDBusPendingCall QDBusPendingCall::fromCompletedCall(const QDBusMessage &msg)
   439 QDBusPendingCall QDBusPendingCall::fromCompletedCall(const QDBusMessage &msg)
   457 {
   440 {
   458     QDBusPendingCallPrivate *d = 0;
   441     QDBusPendingCallPrivate *d = 0;
   459     if (msg.type() == QDBusMessage::ErrorMessage ||
   442     if (msg.type() == QDBusMessage::ErrorMessage ||
   460         msg.type() == QDBusMessage::ReplyMessage) {
   443         msg.type() == QDBusMessage::ReplyMessage) {
   461         d = new QDBusPendingCallPrivate(QDBusMessage(), 0);
   444         d = new QDBusPendingCallPrivate;
   462         d->replyMessage = msg;
   445         d->replyMessage = msg;
       
   446         d->connection = 0;
   463     }
   447     }
   464 
   448 
   465     return QDBusPendingCall(d);
   449     return QDBusPendingCall(d);
   466 }
   450 }
   467 
   451 
   487 */
   471 */
   488 QDBusPendingCallWatcher::QDBusPendingCallWatcher(const QDBusPendingCall &call, QObject *parent)
   472 QDBusPendingCallWatcher::QDBusPendingCallWatcher(const QDBusPendingCall &call, QObject *parent)
   489     : QObject(*new QDBusPendingCallWatcherPrivate, parent), QDBusPendingCall(call)
   473     : QObject(*new QDBusPendingCallWatcherPrivate, parent), QDBusPendingCall(call)
   490 {
   474 {
   491     if (d) {                    // QDBusPendingCall::d
   475     if (d) {                    // QDBusPendingCall::d
   492         QMutexLocker locker(&d->mutex);
       
   493         if (!d->watcherHelper) {
   476         if (!d->watcherHelper) {
   494             d->watcherHelper = new QDBusPendingCallWatcherHelper;
   477             d->watcherHelper = new QDBusPendingCallWatcherHelper;
   495             if (d->replyMessage.type() != QDBusMessage::InvalidMessage) {
   478             if (isFinished()) {
   496                 // cause a signal emission anyways
   479                 // cause a signal emission anyways
   497                 QMetaObject::invokeMethod(d->watcherHelper, "finished", Qt::QueuedConnection);
   480                 QMetaObject::invokeMethod(d->watcherHelper, "finished", Qt::QueuedConnection);
   498             }
   481             }
   499         }
   482         }
   500         d->watcherHelper->add(this);
   483         d->watcherHelper->add(this);
   530         QCoreApplication::sendPostedEvents(this, QEvent::MetaCall);
   513         QCoreApplication::sendPostedEvents(this, QEvent::MetaCall);
   531     }
   514     }
   532 }
   515 }
   533 QT_END_NAMESPACE
   516 QT_END_NAMESPACE
   534 
   517 
       
   518 #endif // QT_NO_DBUS
       
   519 
   535 #include "moc_qdbuspendingcall.cpp"
   520 #include "moc_qdbuspendingcall.cpp"