declarativeconnectionagent.cpp 5.69 KB
Newer Older
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.
**
****************************************************************************/
Lorn Potter's avatar
Lorn Potter committed
16

17
#include "declarativeconnectionagent.h"
18
#include "connectiond_interface.h"
lpotter's avatar
lpotter committed
19

20 21 22
#include <connman-qt5/networkmanager.h>
#include <connman-qt5/networktechnology.h>
#include <connman-qt5/networkservice.h>
23

lpotter's avatar
lpotter committed
24 25 26 27 28
#include <qobject.h>

#define CONND_SERVICE "com.jolla.Connectiond"
#define CONND_PATH "/Connectiond"

29
DeclarativeConnectionAgent::DeclarativeConnectionAgent(QObject *parent):
30
    QObject(parent),
31
    connManagerInterface(nullptr)
lpotter's avatar
lpotter committed
32 33 34 35 36
{
    connectiondWatcher = new QDBusServiceWatcher(CONND_SERVICE,QDBusConnection::sessionBus(),
            QDBusServiceWatcher::WatchForRegistration |
            QDBusServiceWatcher::WatchForUnregistration, this);

37 38 39 40
    connect(connectiondWatcher, &QDBusServiceWatcher::serviceRegistered,
            this, &DeclarativeConnectionAgent::connectToConnectiond);
    connect(connectiondWatcher, &QDBusServiceWatcher::serviceUnregistered,
            this, &DeclarativeConnectionAgent::connectiondUnregistered);
lpotter's avatar
lpotter committed
41

42
    connectToConnectiond();
lpotter's avatar
lpotter committed
43 44
}

45
DeclarativeConnectionAgent::~DeclarativeConnectionAgent()
lpotter's avatar
lpotter committed
46 47 48
{
}

49
void DeclarativeConnectionAgent::connectToConnectiond()
50
{
51 52
    delete connManagerInterface;
    connManagerInterface = nullptr;
53 54

    if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(CONND_SERVICE)) {
55
        qDebug() << Q_FUNC_INFO << QString("connection service not available").arg(CONND_SERVICE);
56
        QDBusReply<void> reply = QDBusConnection::sessionBus().interface()->startService(CONND_SERVICE);
57

58 59 60 61
        if (!reply.isValid()) {
            qDebug() << Q_FUNC_INFO << reply.error().message();
            return;
        }
62 63 64 65 66 67 68
    }

    connManagerInterface = new com::jolla::Connectiond(CONND_SERVICE, CONND_PATH,
                                                       QDBusConnection::sessionBus(), this);
    if (!connManagerInterface->isValid()) {
        qDebug() << Q_FUNC_INFO << "is not valid interface";
    }
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    connect(connManagerInterface, &com::jolla::Connectiond::connectionRequest,
            this, &DeclarativeConnectionAgent::connectionRequest);
    connect(connManagerInterface, &com::jolla::Connectiond::configurationNeeded,
            this, &DeclarativeConnectionAgent::configurationNeeded);
    connect(connManagerInterface, &com::jolla::Connectiond::userInputCanceled,
            this, &DeclarativeConnectionAgent::userInputCanceled);
    connect(connManagerInterface, &com::jolla::Connectiond::errorReported,
            this, &DeclarativeConnectionAgent::errorReported);
    connect(connManagerInterface, &com::jolla::Connectiond::connectionState,
            this, &DeclarativeConnectionAgent::connectionState);
    connect(connManagerInterface, &com::jolla::Connectiond::requestBrowser,
            this, &DeclarativeConnectionAgent::browserRequested);
    connect(connManagerInterface, &com::jolla::Connectiond::userInputRequested,
            this, &DeclarativeConnectionAgent::onUserInputRequested, Qt::UniqueConnection);
    connect(connManagerInterface, &com::jolla::Connectiond::tetheringFinished,
            this, &DeclarativeConnectionAgent::tetheringFinished);
85 86
}

87
void DeclarativeConnectionAgent::sendUserReply(const QVariantMap &input)
88
{
89
    if (!connManagerInterface || !connManagerInterface->isValid()) {
90
        Q_EMIT errorReported("","ConnectionAgent not available");
91 92
        return;
    }
lpotter's avatar
lpotter committed
93
    QDBusPendingReply<> reply = connManagerInterface->sendUserReply(input);
94
    reply.waitForFinished();
lpotter's avatar
lpotter committed
95
    if (reply.isError()) {
96
        qDebug() << Q_FUNC_INFO << reply.error().message();
97
        Q_EMIT errorReported("",reply.error().message());
lpotter's avatar
lpotter committed
98 99 100
    }
}

101
void DeclarativeConnectionAgent::sendConnectReply(const QString &replyMessage, int timeout)
lpotter's avatar
lpotter committed
102
{
103
    if (!connManagerInterface || !connManagerInterface->isValid()) {
104
        Q_EMIT errorReported("","ConnectionAgent not available");
105 106
        return;
    }
107
    connManagerInterface->sendConnectReply(replyMessage,timeout);
lpotter's avatar
lpotter committed
108 109
}

110
void DeclarativeConnectionAgent::connectToType(const QString &type)
111
{
112
    if (!connManagerInterface || !connManagerInterface->isValid()) {
113
        Q_EMIT errorReported("","ConnectionAgent not available");
114 115 116
        return;
    }

117 118 119
    connManagerInterface->connectToType(type);
}

120
void DeclarativeConnectionAgent::onUserInputRequested(const QString &service, const QVariantMap &fields)
lpotter's avatar
lpotter committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
{
    // 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);
}

136
void DeclarativeConnectionAgent::connectiondUnregistered()
lpotter's avatar
lpotter committed
137
{
138 139
    delete connManagerInterface;
    connManagerInterface = nullptr;
lpotter's avatar
lpotter committed
140
}
141

142
void DeclarativeConnectionAgent::startTethering(const QString &type)
143 144 145 146
{
    connManagerInterface->startTethering(type);
}

147
void DeclarativeConnectionAgent::stopTethering(bool keepPowered)
148
{
149
    connManagerInterface->stopTethering(keepPowered);
150
}