2
* Copyright (C) 2014 Canonical Ltd.
4
* Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
6
* This file is part of online-accounts-ui
8
* This program is free software: you can redistribute it and/or modify it
9
* under the terms of the GNU General Public License version 3, as published
10
* by the Free Software Foundation.
12
* This program is distributed in the hope that it will be useful, but
13
* WITHOUT ANY WARRANTY; without even the implied warranties of
14
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
15
* PURPOSE. See the GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License along
18
* with this program. If not, see <http://www.gnu.org/licenses/>.
24
#include "request-handler.h"
25
#include "signonui-request.h"
26
#include "ui-server.h"
28
#include <QLocalSocket>
30
#include <SignOn/uisessiondata_priv.h>
32
using namespace OnlineAccountsUi;
34
static UiServer *m_instance = 0;
36
namespace OnlineAccountsUi {
38
class UiServerPrivate: public QObject
41
Q_DECLARE_PUBLIC(UiServer)
44
inline UiServerPrivate(const QString &address, UiServer *pluginServer);
45
inline ~UiServerPrivate();
49
void sendOperation(const QVariantMap &data);
52
void onDataReady(QByteArray &data);
53
void onRequestCompleted();
56
QLocalSocket m_socket;
57
OnlineAccountsUi::Ipc m_ipc;
58
mutable UiServer *q_ptr;
63
UiServerPrivate::UiServerPrivate(const QString &address,
64
UiServer *pluginServer):
65
QObject(pluginServer),
68
QObject::connect(&m_ipc, SIGNAL(dataReady(QByteArray &)),
69
this, SLOT(onDataReady(QByteArray &)));
70
QObject::connect(&m_socket, SIGNAL(disconnected()),
71
q_ptr, SIGNAL(finished()));
72
m_socket.connectToServer(address);
75
UiServerPrivate::~UiServerPrivate()
80
void UiServerPrivate::sendOperation(const QVariantMap &data)
83
QDataStream stream(&ba, QIODevice::WriteOnly);
88
void UiServerPrivate::onDataReady(QByteArray &data)
93
QDataStream stream(&data, QIODevice::ReadOnly);
98
QString code = map.value(OAU_OPERATION_CODE).toString();
99
if (code == OAU_OPERATION_CODE_PROCESS) {
100
QVariantMap parameters = map[OAU_OPERATION_DATA].toMap();
102
Request::newRequest(map[OAU_OPERATION_INTERFACE].toString(),
103
map[OAU_OPERATION_ID].toInt(),
104
map[OAU_OPERATION_CLIENT_PROFILE].toString(),
107
QObject::connect(request, SIGNAL(completed()),
108
this, SLOT(onRequestCompleted()));
110
/* Check if a RequestHandler has been setup to handle this request. If
111
* so, bing the request object to the handler and start the request
113
SignOnUi::Request *signonRequest =
114
qobject_cast<SignOnUi::Request*>(request);
116
SignOnUi::RequestHandler *handler =
117
SignOnUi::RequestHandler::findMatching(parameters);
119
signonRequest->setHandler(handler);
124
qWarning() << "Invalid operation code: " << code;
128
void UiServerPrivate::onRequestCompleted()
130
Request *request = qobject_cast<Request*>(sender());
131
request->disconnect(this);
132
request->deleteLater();
134
if (request->errorName().isEmpty()) {
135
QVariantMap operation;
136
operation.insert(OAU_OPERATION_CODE,
137
OAU_OPERATION_CODE_REQUEST_FINISHED);
138
operation.insert(OAU_OPERATION_ID, request->id());
139
operation.insert(OAU_OPERATION_DATA, request->result());
140
operation.insert(OAU_OPERATION_INTERFACE, request->interface());
141
sendOperation(operation);
143
QVariantMap operation;
144
operation.insert(OAU_OPERATION_CODE,
145
OAU_OPERATION_CODE_REQUEST_FAILED);
146
operation.insert(OAU_OPERATION_ID, request->id());
147
operation.insert(OAU_OPERATION_INTERFACE, request->interface());
148
operation.insert(OAU_OPERATION_ERROR_NAME, request->errorName());
149
operation.insert(OAU_OPERATION_ERROR_MESSAGE, request->errorMessage());
150
sendOperation(operation);
154
bool UiServerPrivate::init()
156
if (Q_UNLIKELY(!m_socket.waitForConnected())) return false;
158
m_ipc.setChannels(&m_socket, &m_socket);
162
UiServer::UiServer(const QString &address, QObject *parent):
164
d_ptr(new UiServerPrivate(address, this))
168
qmlRegisterType<SignOnUi::RequestHandler>("Ubuntu.OnlineAccounts.Plugin",
169
1, 0, "RequestHandler");
172
UiServer::~UiServer()
177
UiServer *UiServer::instance()
182
bool UiServer::init()
188
void UiServer::registerHandler(SignOnUi::RequestHandler *handler)
192
QVariantMap operation;
193
operation.insert(OAU_OPERATION_CODE,
194
OAU_OPERATION_CODE_REGISTER_HANDLER);
195
operation.insert(OAU_OPERATION_HANDLER_ID, handler->matchId());
196
d->sendOperation(operation);
199
#include "ui-server.moc"