/
declarativeconnectionagent.cpp
188 lines (157 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/****************************************************************************
**
** Copyright (C) 2013 Jolla Ltd
** Contact: lorn.potter@gmail.com
**
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
****************************************************************************/
16
17
#include "declarativeconnectionagent.h"
18
#include "connectiond_interface.h"
19
20
21
22
#include <connman-qt5/networkmanager.h>
#include <connman-qt5/networktechnology.h>
#include <connman-qt5/networkservice.h>
23
24
25
26
27
28
#include <qobject.h>
#define CONND_SERVICE "com.jolla.Connectiond"
#define CONND_PATH "/Connectiond"
29
DeclarativeConnectionAgent::DeclarativeConnectionAgent(QObject *parent):
30
31
QObject(parent),
connManagerInterface(0)
32
33
34
35
36
37
38
39
40
41
{
connectiondWatcher = new QDBusServiceWatcher(CONND_SERVICE,QDBusConnection::sessionBus(),
QDBusServiceWatcher::WatchForRegistration |
QDBusServiceWatcher::WatchForUnregistration, this);
connect(connectiondWatcher, SIGNAL(serviceRegistered(QString)),
this, SLOT(connectToConnectiond(QString)));
connect(connectiondWatcher, SIGNAL(serviceUnregistered(QString)),
this, SLOT(connectiondUnregistered(QString)));
42
connectToConnectiond();
43
44
}
45
DeclarativeConnectionAgent::~DeclarativeConnectionAgent()
46
47
48
{
}
49
void DeclarativeConnectionAgent::connectToConnectiond(QString)
50
51
52
53
54
55
56
{
if (connManagerInterface) {
delete connManagerInterface;
connManagerInterface = 0;
}
if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(CONND_SERVICE)) {
57
qDebug() << Q_FUNC_INFO << QString("connection service not available").arg(CONND_SERVICE);
58
QDBusReply<void> reply = QDBusConnection::sessionBus().interface()->startService(CONND_SERVICE);
59
60
61
62
63
if (!reply.isValid()) {
qDebug() << Q_FUNC_INFO << reply.error().message();
return;
}
64
65
66
67
68
69
70
71
72
}
connManagerInterface = new com::jolla::Connectiond(CONND_SERVICE, CONND_PATH,
QDBusConnection::sessionBus(), this);
if (!connManagerInterface->isValid()) {
qDebug() << Q_FUNC_INFO << "is not valid interface";
}
connect(connManagerInterface,SIGNAL(connectionRequest()),
this,SLOT(onConnectionRequested()));
73
74
connect(connManagerInterface,SIGNAL(configurationNeeded(QString)),
this,SIGNAL(configurationNeeded(QString)));
75
76
77
78
connect(connManagerInterface,SIGNAL(userInputCanceled()),
this,SIGNAL(userInputCanceled()));
79
80
connect(connManagerInterface, SIGNAL(errorReported(QString, QString)),
this, SLOT(onErrorReported(QString, QString)));
81
82
83
84
85
86
87
88
89
connect(connManagerInterface,SIGNAL(connectionState(QString, QString)),
this,SLOT(onConnectionState(QString, QString)));
connect(connManagerInterface,SIGNAL(requestBrowser(QString)),
this,SLOT(onRequestBrowser(QString)));
connect(connManagerInterface,SIGNAL(userInputRequested(QString,QVariantMap)),
this,SLOT(onUserInputRequested(QString,QVariantMap)), Qt::UniqueConnection);
90
91
92
connect(connManagerInterface,SIGNAL(tetheringFinished(bool)),
this,SLOT(onTetheringFinished(bool)));
93
94
}
95
void DeclarativeConnectionAgent::sendUserReply(const QVariantMap &input)
96
{
97
if (!connManagerInterface || !connManagerInterface->isValid()) {
98
Q_EMIT errorReported("","ConnectionAgent not available");
99
100
return;
}
101
QDBusPendingReply<> reply = connManagerInterface->sendUserReply(input);
102
reply.waitForFinished();
103
if (reply.isError()) {
104
qDebug() << Q_FUNC_INFO << reply.error().message();
105
Q_EMIT errorReported("",reply.error().message());
106
107
108
}
}
109
void DeclarativeConnectionAgent::sendConnectReply(const QString &replyMessage, int timeout)
110
{
111
if (!connManagerInterface || !connManagerInterface->isValid()) {
112
Q_EMIT errorReported("","ConnectionAgent not available");
113
114
return;
}
115
connManagerInterface->sendConnectReply(replyMessage,timeout);
116
117
}
118
void DeclarativeConnectionAgent::connectToType(const QString &type)
119
{
120
if (!connManagerInterface || !connManagerInterface->isValid()) {
121
Q_EMIT errorReported("","ConnectionAgent not available");
122
123
124
return;
}
125
126
127
connManagerInterface->connectToType(type);
}
128
void DeclarativeConnectionAgent::onErrorReported(const QString &servicePath, const QString &error)
129
{
130
Q_EMIT errorReported(servicePath, error);
131
132
}
133
void DeclarativeConnectionAgent::onRequestBrowser(const QString &url)
134
135
{
qDebug() << Q_FUNC_INFO <<url;
136
Q_EMIT browserRequested(url);
137
138
}
139
void DeclarativeConnectionAgent::onUserInputRequested(const QString &service, const QVariantMap &fields)
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
{
// we do this as qtdbus does not understand QVariantMap very well.
// we need to manually demarshall
QVariantMap map;
QMapIterator<QString, QVariant> i(fields);
// this works for Passphrase at least. anything else?
while (i.hasNext()) {
i.next();
QDBusArgument arg = fields.value(i.key()).value<QDBusArgument>();
QVariantMap vmap = qdbus_cast<QVariantMap>(arg);
map.insert(i.key(), vmap);
}
Q_EMIT userInputRequested(service, map);
}
155
void DeclarativeConnectionAgent::onConnectionRequested()
156
{
157
qDebug() << Q_FUNC_INFO;
158
159
160
Q_EMIT connectionRequest();
}
161
void DeclarativeConnectionAgent::connectiondUnregistered(QString)
162
163
164
165
166
167
{
if (connManagerInterface) {
delete connManagerInterface;
connManagerInterface = 0;
}
}
168
169
void DeclarativeConnectionAgent::onConnectionState(const QString &state, const QString &type)
170
{
171
qDebug() << Q_FUNC_INFO << state;
172
Q_EMIT connectionState(state, type);
173
}
174
175
void DeclarativeConnectionAgent::startTethering(const QString &type)
176
177
178
179
{
connManagerInterface->startTethering(type);
}
180
void DeclarativeConnectionAgent::onTetheringFinished(bool success)
181
182
183
184
{
Q_EMIT tetheringFinished(success);
}
185
void DeclarativeConnectionAgent::stopTethering(bool keepPowered)
186
{
187
connManagerInterface->stopTethering(keepPowered);
188
}