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     template<int Index>
00085     struct Select : MethodInvocationContextTypes::Select<Index, T1, T2, T3, T4, T5, T6, T7, T8>
00086     {
00087     };
00088 
00089     typedef MethodInvocationContextTypes::ForEach<T1, T2, T3, T4, T5, T6, T7, T8> ForEach;
00090     enum { Count = ForEach::Total };
00091 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
00092 
00093 public:
00094     MethodInvocationContext(const QDBusConnection &bus, const QDBusMessage &message)
00095         : mBus(bus), mMessage(message), mFinished(false)
00096     {
00097         mMessage.setDelayedReply(true);
00098     }
00099 
00100     virtual ~MethodInvocationContext()
00101     {
00102         if (!mFinished) {
00103             setFinishedWithError(QString(), QString());
00104         }
00105     }
00106 
00107     bool isFinished() const { return mFinished; }
00108     bool isError() const { return !mErrorName.isEmpty(); }
00109     QString errorName() const { return mErrorName; }
00110     QString errorMessage() const { return mErrorMessage; }
00111 
00112     void setFinished(const T1 &t1 = T1(), const T2 &t2 = T2(), const T3 &t3 = T3(),
00113                      const T4 &t4 = T4(), const T5 &t5 = T5(), const T6 &t6 = T6(),
00114                      const T7 &t7 = T7(), const T8 &t8 = T8())
00115     {
00116         if (mFinished) {
00117             return;
00118         }
00119 
00120         mFinished = true;
00121 
00122         setReplyValue(0, qVariantFromValue(t1));
00123         setReplyValue(1, qVariantFromValue(t2));
00124         setReplyValue(2, qVariantFromValue(t3));
00125         setReplyValue(3, qVariantFromValue(t4));
00126         setReplyValue(4, qVariantFromValue(t5));
00127         setReplyValue(5, qVariantFromValue(t6));
00128         setReplyValue(6, qVariantFromValue(t7));
00129         setReplyValue(7, qVariantFromValue(t8));
00130 
00131         if (mReply.isEmpty()) {
00132             mBus.send(mMessage.createReply());
00133         } else {
00134             mBus.send(mMessage.createReply(mReply));
00135         }
00136         onFinished();
00137     }
00138 
00139     void setFinishedWithError(const QString &errorName,
00140             const QString &errorMessage)
00141     {
00142         if (mFinished) {
00143             return;
00144         }
00145 
00146         mFinished = true;
00147 
00148         if (errorName.isEmpty()) {
00149             mErrorName = QLatin1String("org.freedesktop.Telepathy.Qt4.ErrorHandlingError");
00150         } else {
00151             mErrorName = errorName;
00152         }
00153         mErrorMessage = errorMessage;
00154 
00155         mBus.send(mMessage.createErrorReply(mErrorName, mErrorMessage));
00156         onFinished();
00157     }
00158 
00159     template<int Index> inline
00160     typename Select<Index>::Type argumentAt() const
00161     {
00162         Q_ASSERT(Index >= 0 && Index < Count);
00163         return qdbus_cast<typename Select<Index>::Type>(mReply.value(Index));
00164     }
00165 
00166 protected:
00167     virtual void onFinished() {}
00168 
00169 private:
00170     Q_DISABLE_COPY(MethodInvocationContext)
00171 
00172     void setReplyValue(int index, const QVariant &value)
00173     {
00174         if (index >= Count) {
00175             return;
00176         }
00177         mReply.insert(index, value);
00178     }
00179 
00180     QDBusConnection mBus;
00181     QDBusMessage mMessage;
00182     bool mFinished;
00183     QList<QVariant> mReply;
00184     QString mErrorName;
00185     QString mErrorMessage;
00186 };
00187 
00188 } // Tp
00189 
00190 Q_DECLARE_METATYPE(Tp::MethodInvocationContextTypes::Nil)
00191 
00192 #endif


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