~michihenning/storage-framework/add-mcloud-scope

« back to all changes in this revision

Viewing changes to src/qt/client/internal/remote_client/dbusmarshal.cpp

  • Committer: Michi Henning
  • Date: 2016-08-11 04:39:59 UTC
  • mfrom: (45.1.2 devel)
  • Revision ID: michi.henning@canonical.com-20160811043959-oqthzfqqa6lrrz42
Merged devel and resolved conflict.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
#include <unity/storage/qt/client/internal/remote_client/dbusmarshal.h>
20
20
 
 
21
#include <unity/storage/internal/dbus_error.h>
 
22
#include <unity/storage/qt/client/Exceptions.h>
 
23
#include <unity/storage/qt/client/internal/make_future.h>
 
24
 
 
25
#include <QDBusPendingCallWatcher>
 
26
#include <QDBusPendingReply>
21
27
#include <QDebug>
22
28
 
 
29
#include <cassert>
 
30
#include <functional>
 
31
#include <map>
 
32
 
 
33
using namespace unity::storage::internal;
23
34
using namespace std;
24
35
 
25
36
namespace unity
117
128
{
118
129
namespace remote_client
119
130
{
120
 
 
121
 
}  // remote_client
122
 
}  // internal
123
 
}  // client
124
 
}  // qt
125
 
}  // storage
126
 
}  // unity
 
131
namespace
 
132
{
 
133
 
 
134
template<typename T>
 
135
exception_ptr make_exception(QDBusPendingCallWatcher const& call)
 
136
{
 
137
    QDBusPendingReply<QString> reply = call;
 
138
    auto msg = reply.argumentAt<0>();
 
139
    return make_exception_ptr(T(msg));
 
140
}
 
141
 
 
142
template<>
 
143
exception_ptr make_exception<NotExistsException>(QDBusPendingCallWatcher const& call)
 
144
{
 
145
    QDBusPendingReply<QString, QString> reply = call;
 
146
    auto msg = reply.argumentAt<0>();
 
147
    auto key = reply.argumentAt<1>();
 
148
    return make_exception_ptr(NotExistsException(msg, key));
 
149
}
 
150
 
 
151
template<>
 
152
exception_ptr make_exception<ExistsException>(QDBusPendingCallWatcher const& call)
 
153
{
 
154
    QDBusPendingReply<QString, QString, QString> reply = call;
 
155
    auto msg = reply.argumentAt<0>();
 
156
    auto id = reply.argumentAt<1>();
 
157
    auto name = reply.argumentAt<2>();
 
158
    return make_exception_ptr(ExistsException(msg, id, name));
 
159
}
 
160
 
 
161
template<>
 
162
exception_ptr make_exception<ResourceException>(QDBusPendingCallWatcher const& call)
 
163
{
 
164
    QDBusPendingReply<QString, int> reply = call;
 
165
    auto msg = reply.argumentAt<0>();
 
166
    auto error_code = reply.argumentAt<1>();
 
167
    return make_exception_ptr(ResourceException(msg, error_code));
 
168
}
 
169
 
 
170
static const map<QString, function<exception_ptr(QDBusPendingCallWatcher const& call)>> exception_factories =
 
171
{
 
172
    { "NotExistsException",       make_exception<NotExistsException> },
 
173
    { "ExistsException",          make_exception<ExistsException> },
 
174
    { "ResourceException",        make_exception<ResourceException> },
 
175
    { "RemoteCommsException",     make_exception<RemoteCommsException> },
 
176
    { "ConflictException",        make_exception<ConflictException> },
 
177
    { "PermissionException",      make_exception<PermissionException> },
 
178
    { "QuotaException",           make_exception<QuotaException> },
 
179
    { "CancelledException",       make_exception<CancelledException> },
 
180
    { "LogicException",           make_exception<LogicException> },
 
181
    { "InvalidArgumentException", make_exception<InvalidArgumentException> },
 
182
    { "UnknownException",         make_exception<LocalCommsException> }  // Yes, LocalCommsException is intentional
 
183
};
 
184
 
 
185
}  // namespace
 
186
 
 
187
std::exception_ptr unmarshal_exception(QDBusPendingCallWatcher const& call)
 
188
{
 
189
    assert(call.isError());
 
190
 
 
191
    int err = call.error().type();
 
192
    if (err != QDBusError::Other)
 
193
    {
 
194
        return make_exception_ptr(LocalCommsException(call.error().message()));
 
195
    }
 
196
 
 
197
    auto exception_type = call.error().name();
 
198
    if (!exception_type.startsWith(DBUS_ERROR_PREFIX))
 
199
    {
 
200
        QString msg = "unmarshal_exception(): unknown exception type received from server: " + exception_type
 
201
                      + ": " + call.error().message();
 
202
        return make_exception_ptr(LocalCommsException(msg));
 
203
    }
 
204
    exception_type = exception_type.remove(0, strlen(DBUS_ERROR_PREFIX));
 
205
 
 
206
    auto factory_it = exception_factories.find(exception_type);
 
207
    if (factory_it == exception_factories.end())
 
208
    {
 
209
        QString msg = "unmarshal_exception(): unknown exception type received from server: " + exception_type
 
210
                      + ": " + call.error().message();
 
211
        return make_exception_ptr(LocalCommsException(msg));
 
212
    }
 
213
    return factory_it->second(call);
 
214
}
 
215
 
 
216
}  // namespace remote_client
 
217
}  // namespace internal
 
218
}  // namespace client
 
219
}  // namespace qt
 
220
}  // namespace storage
 
221
}  // namespace unity