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

method-invocation-context.h
1
23 #ifndef _TelepathyQt_method_invocation_context_h_HEADER_GUARD_
24 #define _TelepathyQt_method_invocation_context_h_HEADER_GUARD_
25
26 #ifndef IN_TP_QT_HEADER
27 #error IN_TP_QT_HEADER
28 #endif
29
30 #include <QtDBus>
31 #include <QtCore>
32
33 namespace Tp
34 {
35
36 #ifndef DOXYGEN_SHOULD_SKIP_THIS
37
38 namespace MethodInvocationContextTypes
39 {
40
41 struct Nil
42 {
43 };
44
45 template<int Index,
46 typename T1, typename T2, typename T3, typename T4,
47 typename T5, typename T6, typename T7, typename T8>
48 struct Select
49 {
50 typedef Select<Index - 1, T2, T3, T4, T5, T6, T7, T8, Nil> Next;
51 typedef typename Next::Type Type;
52 };
53 template<typename T1, typename T2, typename T3, typename T4,
54 typename T5, typename T6, typename T7, typename T8>
55 struct Select<0, T1, T2, T3, T4, T5, T6, T7, T8>
56 {
57 typedef T1 Type;
58 };
59
60 template<typename T1, typename T2, typename T3, typename T4,
61 typename T5, typename T6, typename T7, typename T8>
62 struct ForEach
63 {
64 typedef ForEach<T2, T3, T4, T5, T6, T7, T8, Nil> Next;
65 enum { Total = Next::Total + 1 };
66 };
67 template<>
68 struct ForEach<Nil, Nil, Nil, Nil, Nil, Nil, Nil, Nil>
69 {
70 enum { Total = 0 };
71 };
72
73 }
74
75 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
76
77 template<typename T1 = MethodInvocationContextTypes::Nil, typename T2 = MethodInvocationContextTypes::Nil,
78 typename T3 = MethodInvocationContextTypes::Nil, typename T4 = MethodInvocationContextTypes::Nil,
79 typename T5 = MethodInvocationContextTypes::Nil, typename T6 = MethodInvocationContextTypes::Nil,
80 typename T7 = MethodInvocationContextTypes::Nil, typename T8 = MethodInvocationContextTypes::Nil>
81 class MethodInvocationContext : public RefCounted
82 {
83 #ifndef DOXYGEN_SHOULD_SKIP_THIS
84 template<int Index>
85 struct Select : MethodInvocationContextTypes::Select<Index, T1, T2, T3, T4, T5, T6, T7, T8>
86 {
87 };
88
89 typedef MethodInvocationContextTypes::ForEach<T1, T2, T3, T4, T5, T6, T7, T8> ForEach;
90 enum { Count = ForEach::Total };
91 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
92
93 public:
94 MethodInvocationContext(const QDBusConnection &bus, const QDBusMessage &message)
95 : mBus(bus), mMessage(message), mFinished(false)
96 {
97 mMessage.setDelayedReply(true);
98 }
99
100 virtual ~MethodInvocationContext()
101 {
102 if (!mFinished) {
103 setFinishedWithError(QString(), QString());
104 }
105 }
106
107 bool isFinished() const { return mFinished; }
108 bool isError() const { return !mErrorName.isEmpty(); }
109 QString errorName() const { return mErrorName; }
110 QString errorMessage() const { return mErrorMessage; }
111
112 void setFinished(const T1 &t1 = T1(), const T2 &t2 = T2(), const T3 &t3 = T3(),
113 const T4 &t4 = T4(), const T5 &t5 = T5(), const T6 &t6 = T6(),
114 const T7 &t7 = T7(), const T8 &t8 = T8())
115 {
116 if (mFinished) {
117 return;
118 }
119
120 mFinished = true;
121
122 setReplyValue(0, qVariantFromValue(t1));
123 setReplyValue(1, qVariantFromValue(t2));
124 setReplyValue(2, qVariantFromValue(t3));
125 setReplyValue(3, qVariantFromValue(t4));
126 setReplyValue(4, qVariantFromValue(t5));
127 setReplyValue(5, qVariantFromValue(t6));
128 setReplyValue(6, qVariantFromValue(t7));
129 setReplyValue(7, qVariantFromValue(t8));
130
131 if (mReply.isEmpty()) {
132 mBus.send(mMessage.createReply());
133 } else {
134 mBus.send(mMessage.createReply(mReply));
135 }
136 onFinished();
137 }
138
139 void setFinishedWithError(const QString &errorName,
140 const QString &errorMessage)
141 {
142 if (mFinished) {
143 return;
144 }
145
146 mFinished = true;
147
148 if (errorName.isEmpty()) {
149 mErrorName = QLatin1String("org.freedesktop.Telepathy.Qt.ErrorHandlingError");
150 } else {
151 mErrorName = errorName;
152 }
153 mErrorMessage = errorMessage;
154
155 mBus.send(mMessage.createErrorReply(mErrorName, mErrorMessage));
156 onFinished();
157 }
158
159 template<int Index> inline
160 typename Select<Index>::Type argumentAt() const
161 {
162 Q_ASSERT(Index >= 0 && Index < Count);
163 return qdbus_cast<typename Select<Index>::Type>(mReply.value(Index));
164 }
165
166 protected:
167 virtual void onFinished() {}
168
169 private:
170 Q_DISABLE_COPY(MethodInvocationContext)
171
172 void setReplyValue(int index, const QVariant &value)
173 {
174 if (index >= Count) {
175 return;
176 }
177 mReply.insert(index, value);
178 }
179
180 QDBusConnection mBus;
181 QDBusMessage mMessage;
182 bool mFinished;
183 QList<QVariant> mReply;
184 QString mErrorName;
185 QString mErrorMessage;
186 };
187
188 } // Tp
189
190 Q_DECLARE_METATYPE(Tp::MethodInvocationContextTypes::Nil)
191
192 #endif
Tp::MethodInvocationContext::isFinished
bool isFinished() const
Definition: method-invocation-context.h:107
Tp::MethodInvocationContext::errorName
QString errorName() const
Definition: method-invocation-context.h:109
Tp::MethodInvocationContext::isError
bool isError() const
Definition: method-invocation-context.h:108
QDBusConnection
Tp::MethodInvocationContext::onFinished
virtual void onFinished()
Definition: method-invocation-context.h:167
QString::isEmpty
bool isEmpty() const
Tp::MethodInvocationContext::errorMessage
QString errorMessage() const
Definition: method-invocation-context.h:110
QString
QList< QVariant >
Tp::MethodInvocationContext::MethodInvocationContext
MethodInvocationContext(const QDBusConnection &bus, const QDBusMessage &message)
Definition: method-invocation-context.h:94
Tp::RefCounted
The RefCounted class is a base class for shared objects used by SharedPtr.
Definition: shared-ptr.h:42
Tp::MethodInvocationContext::setFinishedWithError
void setFinishedWithError(const QString &errorName, const QString &errorMessage)
Definition: method-invocation-context.h:139
Tp::MethodInvocationContext
The MethodInvocationContext class provides a way for the service implementation to respond to method ...
Definition: method-invocation-context.h:81
QDBusMessage
QLatin1String
Tp::MethodInvocationContext::~MethodInvocationContext
virtual ~MethodInvocationContext()
Definition: method-invocation-context.h:100
Tp::MethodInvocationContext::argumentAt
Select< Index >::Type argumentAt() const
Definition: method-invocation-context.h:160
Tp::MethodInvocationContext::setFinished
void setFinished(const T1 &t1=T1(), const T2 &t2=T2(), const T3 &t3=T3(), const T4 &t4=T4(), const T5 &t5=T5(), const T6 &t6=T6(), const T7 &t7=T7(), const T8 &t8=T8())
Definition: method-invocation-context.h:112
Tp
Definition: abstract-adaptor.cpp:31
QVariant