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

# method-invocation-context.h

    00001 /*
    00002  * This file is part of TelepathyQt4
    00003  *
    00004  * Copyright (C) 2009 Collabora Ltd. <http://www.collabora.co.uk/>
    00005  * Copyright (C) 2009 Nokia Corporation
    00006  *
    00007  * This library is free software; you can redistribute it and/or
    00008  * modify it under the terms of the GNU Lesser General Public
    00009  * License as published by the Free Software Foundation; either
    00010  * version 2.1 of the License, or (at your option) any later version.
    00011  *
    00012  * This library is distributed in the hope that it will be useful,
    00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00015  * Lesser General Public License for more details.
    00016  *
    00017  * You should have received a copy of the GNU Lesser General Public
    00018  * License along with this library; if not, write to the Free Software
    00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    00020  */
    00021
    00022 #ifndef _TelepathyQt4_method_invocation_context_h_HEADER_GUARD_
    00023 #define _TelepathyQt4_method_invocation_context_h_HEADER_GUARD_
    00024
    00025 #ifndef IN_TELEPATHY_QT4_HEADER
    00026 #error IN_TELEPATHY_QT4_HEADER
    00027 #endif
    00028
    00029 #include <QtDBus>
    00030 #include <QtCore>
    00031
    00032 namespace Tp
    00033 {
    00034
    00035 namespace MethodInvocationContextTypes
    00036 {
    00037
    00038 struct Nil
    00039 {
    00040 };
    00041
    00042 template<int Index,
    00043          typename T1, typename T2, typename T3, typename T4,
    00044          typename T5, typename T6, typename T7, typename T8>
    00045 struct Select
    00046 {
    00047     typedef Select<Index - 1, T2, T3, T4, T5, T6, T7, T8, Nil> Next;
    00048     typedef typename Next::Type Type;
    00049 };
    00050 template<typename T1, typename T2, typename T3, typename T4,
    00051          typename T5, typename T6, typename T7, typename T8>
    00052 struct Select<0, T1, T2, T3, T4, T5, T6, T7, T8>
    00053 {
    00054     typedef T1 Type;
    00055 };
    00056
    00057 template<typename T1, typename T2, typename T3, typename T4,
    00058          typename T5, typename T6, typename T7, typename T8>
    00059 struct ForEach
    00060 {
    00061     typedef ForEach<T2, T3, T4, T5, T6, T7, T8, Nil> Next;
    00062     enum { Total = Next::Total + 1 };
    00063 };
    00064 template<>
    00065 struct ForEach<Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil>
    00066 {
    00067     enum { Total = 0 };
    00068 };
    00069
    00070 }
    00071
    00072 template<typename T1 = MethodInvocationContextTypes::Nil, typename T2 = MethodInvocationContextTypes::Nil,
    00073          typename T3 = MethodInvocationContextTypes::Nil, typename T4 = MethodInvocationContextTypes::Nil,
    00074          typename T5 = MethodInvocationContextTypes::Nil, typename T6 = MethodInvocationContextTypes::Nil,
    00075          typename T7 = MethodInvocationContextTypes::Nil, typename T8 = MethodInvocationContextTypes::Nil>
    00076 class MethodInvocationContext : public RefCounted
    00077 {
    00078     template<int Index>
    00079     struct Select : MethodInvocationContextTypes::Select<Index, T1, T2, T3, T4, T5, T6, T7, T8>
    00080     {
    00081     };
    00082
    00083     typedef MethodInvocationContextTypes::ForEach<T1, T2, T3, T4, T5, T6, T7, T8> ForEach;
    00084     enum { Count = ForEach::Total };
    00085
    00086 public:
    00087     MethodInvocationContext(const QDBusConnection &bus, const QDBusMessage &message)
    00088         : mBus(bus), mMessage(message), mFinished(false)
    00089     {
    00090     }
    00091
    00092     virtual ~MethodInvocationContext()
    00093     {
    00094         if (!mFinished) {
    00095             setFinishedWithError(QString(), QString());
    00096         }
    00097     }
    00098
    00099     bool isFinished() const { return mFinished; }
    00100     bool isError() const { return !mErrorName.isEmpty(); }
    00101     QString errorName() const { return mErrorName; }
    00102     QString errorMessage() const { return mErrorMessage; }
    00103
    00104     void setFinished(const T1 &t1 = T1(), const T2 &t2 = T2(), const T3 &t3 = T3(),
    00105                      const T4 &t4 = T4(), const T5 &t5 = T5(), const T6 &t6 = T6(),
    00106                      const T7 &t7 = T7(), const T8 &t8 = T8())
    00107     {
    00108         if (mFinished) {
    00109             return;
    00110         }
    00111
    00112         mFinished = true;
    00113
    00114         setReplyValue(0, qVariantFromValue(t1));
    00115         setReplyValue(1, qVariantFromValue(t2));
    00116         setReplyValue(2, qVariantFromValue(t3));
    00117         setReplyValue(3, qVariantFromValue(t4));
    00118         setReplyValue(4, qVariantFromValue(t5));
    00119         setReplyValue(5, qVariantFromValue(t6));
    00120         setReplyValue(6, qVariantFromValue(t7));
    00121         setReplyValue(7, qVariantFromValue(t8));
    00122
    00123         if (mReply.isEmpty()) {
    00124             mBus.send(mMessage.createReply());
    00125         } else {
    00126             mBus.send(mMessage.createReply(mReply));
    00127         }
    00128         onFinished();
    00129     }
    00130
    00131     void setFinishedWithError(const QString &errorName,
    00132             const QString &errorMessage)
    00133     {
    00134         if (mFinished) {
    00135             return;
    00136         }
    00137
    00138         mFinished = true;
    00139
    00140         if (errorName.isEmpty()) {
    00141             mErrorName = "org.freedesktop.Telepathy.Qt4.ErrorHandlingError";
    00142         } else {
    00143             mErrorName = errorName;
    00144         }
    00145         mErrorMessage = errorMessage;
    00146
    00147         mBus.send(mMessage.createErrorReply(mErrorName, mErrorMessage));
    00148         onFinished();
    00149     }
    00150
    00151     template<int Index> inline
    00152     typename Select<Index>::Type argumentAt() const
    00153     {
    00154         Q_ASSERT(Index >= 0 && Index < Count);
    00155         return qdbus_cast<typename Select<Index>::Type>(mReply.value(Index));
    00156     }
    00157
    00158 protected:
    00159     virtual void onFinished() {}
    00160
    00161 private:
    00162     Q_DISABLE_COPY(MethodInvocationContext)
    00163
    00164     void setReplyValue(int index, const QVariant &value)
    00165     {
    00166         if (index >= Count) {
    00167             return;
    00168         }
    00169         mReply.insert(index, value);
    00170     }
    00171
    00172     QDBusConnection mBus;
    00173     QDBusMessage mMessage;
    00174     bool mFinished;
    00175     QList<QVariant> mReply;
    00176     QString mErrorName;
    00177     QString mErrorMessage;
    00178 };
    00179
    00180 } // Tp
    00181
    00182 Q_DECLARE_METATYPE(Tp::MethodInvocationContextTypes::Nil)
    00183
    00184 #endif