Home · All Classes · All Namespaces · Modules · Functions · Files

method-invocation-context.h

00001 
00023 #ifndef _TelepathyQt4_method_invocation_context_h_HEADER_GUARD_
00024 #define _TelepathyQt4_method_invocation_context_h_HEADER_GUARD_
00025 
00026 #ifndef IN_TELEPATHY_QT4_HEADER
00027 #error IN_TELEPATHY_QT4_HEADER
00028 #endif
00029 
00030 #include <QtDBus>
00031 #include <QtCore>
00032 
00033 namespace Tp
00034 {
00035 
00036 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00037 
00038 namespace MethodInvocationContextTypes
00039 {
00040 
00041 struct Nil
00042 {
00043 };
00044 
00045 template<int Index,
00046          typename T1, typename T2, typename T3, typename T4,
00047          typename T5, typename T6, typename T7, typename T8>
00048 struct Select
00049 {
00050     typedef Select<Index - 1, T2, T3, T4, T5, T6, T7, T8, Nil> Next;
00051     typedef typename Next::Type Type;
00052 };
00053 template<typename T1, typename T2, typename T3, typename T4,
00054          typename T5, typename T6, typename T7, typename T8>
00055 struct Select<0, T1, T2, T3, T4, T5, T6, T7, T8>
00056 {
00057     typedef T1 Type;
00058 };
00059 
00060 template<typename T1, typename T2, typename T3, typename T4,
00061          typename T5, typename T6, typename T7, typename T8>
00062 struct ForEach
00063 {
00064     typedef ForEach<T2, T3, T4, T5, T6, T7, T8, Nil> Next;
00065     enum { Total = Next::Total + 1 };
00066 };
00067 template<>
00068 struct ForEach<Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil>
00069 {
00070     enum { Total = 0 };
00071 };
00072 
00073 }
00074 
00075 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
00076 
00077 template<typename T1 = MethodInvocationContextTypes::Nil, typename T2 = MethodInvocationContextTypes::Nil,
00078          typename T3 = MethodInvocationContextTypes::Nil, typename T4 = MethodInvocationContextTypes::Nil,
00079          typename T5 = MethodInvocationContextTypes::Nil, typename T6 = MethodInvocationContextTypes::Nil,
00080          typename T7 = MethodInvocationContextTypes::Nil, typename T8 = MethodInvocationContextTypes::Nil>
00081 class MethodInvocationContext : public RefCounted
00082 {
00083 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00084 
00085     template<int Index>
00086     struct Select : MethodInvocationContextTypes::Select<Index, T1, T2, T3, T4, T5, T6, T7, T8>
00087     {
00088     };
00089 
00090     typedef MethodInvocationContextTypes::ForEach<T1, T2, T3, T4, T5, T6, T7, T8> ForEach;
00091     enum { Count = ForEach::Total };
00092 
00093 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
00094 
00095 public:
00096     MethodInvocationContext(const QDBusConnection &bus, const QDBusMessage &message)
00097         : mBus(bus), mMessage(message), mFinished(false)
00098     {
00099         mMessage.setDelayedReply(true);
00100     }
00101 
00102     virtual ~MethodInvocationContext()
00103     {
00104         if (!mFinished) {
00105             setFinishedWithError(QString(), QString());
00106         }
00107     }
00108 
00109     bool isFinished() const { return mFinished; }
00110     bool isError() const { return !mErrorName.isEmpty(); }
00111     QString errorName() const { return mErrorName; }
00112     QString errorMessage() const { return mErrorMessage; }
00113 
00114     void setFinished(const T1 &t1 = T1(), const T2 &t2 = T2(), const T3 &t3 = T3(),
00115                      const T4 &t4 = T4(), const T5 &t5 = T5(), const T6 &t6 = T6(),
00116                      const T7 &t7 = T7(), const T8 &t8 = T8())
00117     {
00118         if (mFinished) {
00119             return;
00120         }
00121 
00122         mFinished = true;
00123 
00124         setReplyValue(0, qVariantFromValue(t1));
00125         setReplyValue(1, qVariantFromValue(t2));
00126         setReplyValue(2, qVariantFromValue(t3));
00127         setReplyValue(3, qVariantFromValue(t4));
00128         setReplyValue(4, qVariantFromValue(t5));
00129         setReplyValue(5, qVariantFromValue(t6));
00130         setReplyValue(6, qVariantFromValue(t7));
00131         setReplyValue(7, qVariantFromValue(t8));
00132 
00133         if (mReply.isEmpty()) {
00134             mBus.send(mMessage.createReply());
00135         } else {
00136             mBus.send(mMessage.createReply(mReply));
00137         }
00138         onFinished();
00139     }
00140 
00141     void setFinishedWithError(const QString &errorName,
00142             const QString &errorMessage)
00143     {
00144         if (mFinished) {
00145             return;
00146         }
00147 
00148         mFinished = true;
00149 
00150         if (errorName.isEmpty()) {
00151             mErrorName = QLatin1String("org.freedesktop.Telepathy.Qt4.ErrorHandlingError");
00152         } else {
00153             mErrorName = errorName;
00154         }
00155         mErrorMessage = errorMessage;
00156 
00157         mBus.send(mMessage.createErrorReply(mErrorName, mErrorMessage));
00158         onFinished();
00159     }
00160 
00161     template<int Index> inline
00162     typename Select<Index>::Type argumentAt() const
00163     {
00164         Q_ASSERT(Index >= 0 && Index < Count);
00165         return qdbus_cast<typename Select<Index>::Type>(mReply.value(Index));
00166     }
00167 
00168 protected:
00169     virtual void onFinished() {}
00170 
00171 private:
00172     Q_DISABLE_COPY(MethodInvocationContext)
00173 
00174     void setReplyValue(int index, const QVariant &value)
00175     {
00176         if (index >= Count) {
00177             return;
00178         }
00179         mReply.insert(index, value);
00180     }
00181 
00182     QDBusConnection mBus;
00183     QDBusMessage mMessage;
00184     bool mFinished;
00185     QList<QVariant> mReply;
00186     QString mErrorName;
00187     QString mErrorMessage;
00188 };
00189 
00190 } // Tp
00191 
00192 Q_DECLARE_METATYPE(Tp::MethodInvocationContextTypes::Nil)
00193 
00194 #endif


Copyright © 2008-2011 Collabora Ltd. and Nokia Corporation
Telepathy-Qt4 0.5.14.1