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


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