TelepathyQt4: method-invocation-context.h Source File

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