File: nmdbusactiveconnectionmonitor.cpp

package info (click to toggle)
plasma-widget-networkmanagement 0.1~svn1141976-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,812 kB
  • ctags: 3,700
  • sloc: cpp: 28,962; xml: 471; makefile: 21; sh: 16; python: 9
file content (252 lines) | stat: -rw-r--r-- 10,214 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
Copyright 2009 Will Stephenson <wstephenson@kde.org>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "nmdbusactiveconnectionmonitor.h"

#include <NetworkManager.h>
#include <NetworkManagerVPN.h>

#include <QMultiHash>
#include <QUuid>

#include <KDebug>
#include <solid/control/networkmanager.h>

#include <interfaceconnection.h>
#include <vpninterfaceconnection.h>

#include "activatablelist.h"

#include "nmdbussettingsservice.h"

#include "nm-active-connectioninterface.h"
#include "nm-vpn-connectioninterface.h"

NMDBusActiveConnectionProxy::NMDBusActiveConnectionProxy(Knm::InterfaceConnection * interfaceConnection,
        OrgFreedesktopNetworkManagerConnectionActiveInterface * activeConnectionIface)
: m_activeConnectionIface(activeConnectionIface), m_interfaceConnection(interfaceConnection)
{
    m_activeConnectionIface->setParent(this);

    connect(m_activeConnectionIface, SIGNAL(PropertiesChanged(const QVariantMap&)), SLOT(handlePropertiesChanged(const QVariantMap &)));

    m_interfaceConnection->setProperty("NMDBusActiveConnectionObject", m_activeConnectionIface->path());
    kDebug() << "default:" << m_activeConnectionIface->getDefault() << "state:" << m_activeConnectionIface->state();
    m_interfaceConnection->setHasDefaultRoute(m_activeConnectionIface->getDefault());
    m_interfaceConnection->setActivationState((Knm::InterfaceConnection::ActivationState)m_activeConnectionIface->state());
}

NMDBusActiveConnectionProxy::~NMDBusActiveConnectionProxy()
{
    m_interfaceConnection->setActivationState(Knm::InterfaceConnection::Unknown);
    m_interfaceConnection->setHasDefaultRoute(false);
}


Knm::InterfaceConnection * NMDBusActiveConnectionProxy::interfaceConnection() const
{
    return m_interfaceConnection;
}

void NMDBusActiveConnectionProxy::handlePropertiesChanged(const QVariantMap & changedProps)
{
    const QString defaultKey = QLatin1String("Default");
    const QString stateKey = QLatin1String("State");
    if (changedProps.contains(defaultKey)) {
        m_interfaceConnection->setHasDefaultRoute(changedProps[defaultKey].toBool());
    }
    if (changedProps.contains(stateKey)) {
        m_interfaceConnection->setActivationState((Knm::InterfaceConnection::ActivationState)changedProps[stateKey].toUInt());
    }
}

NMDBusVPNConnectionProxy::NMDBusVPNConnectionProxy(Knm::InterfaceConnection * interfaceConnection,
        OrgFreedesktopNetworkManagerConnectionActiveInterface * activeConnectionIface)
: NMDBusActiveConnectionProxy(interfaceConnection, activeConnectionIface)
{
    m_vpnConnectionIface = new OrgFreedesktopNetworkManagerVPNConnectionInterface(
            activeConnectionIface->service(),
            activeConnectionIface->path(),
            QDBusConnection::systemBus(),
            this);
    connect(m_vpnConnectionIface, SIGNAL(PropertiesChanged(const QVariantMap&)), this, SLOT(handleVPNPropertiesChanged(const QVariantMap&)));
    setState(m_vpnConnectionIface->vpnState());
}

void NMDBusVPNConnectionProxy::handleVPNPropertiesChanged(const QVariantMap & changedProps)
{
    const QString stateKey = QLatin1String("VpnState");
    if (changedProps.contains(stateKey)) {
        setState(changedProps[stateKey].toUInt());
    }
}

void NMDBusVPNConnectionProxy::setState(uint vpnState)
{
    Knm::InterfaceConnection::ActivationState aState = Knm::InterfaceConnection::Unknown;
    switch (vpnState) {
        case NM_VPN_CONNECTION_STATE_UNKNOWN:
        case NM_VPN_CONNECTION_STATE_FAILED:
        case NM_VPN_CONNECTION_STATE_DISCONNECTED:
            break;
        case NM_VPN_CONNECTION_STATE_PREPARE:
        case NM_VPN_CONNECTION_STATE_NEED_AUTH:
        case NM_VPN_CONNECTION_STATE_CONNECT:
        case NM_VPN_CONNECTION_STATE_IP_CONFIG_GET:
            aState = Knm::InterfaceConnection::Activating;
            break;
        case NM_VPN_CONNECTION_STATE_ACTIVATED:
            aState = Knm::InterfaceConnection::Activated;
    }
    kDebug() << "state:" << aState;
    m_interfaceConnection->setActivationState(aState);
}

class NMDBusActiveConnectionMonitorPrivate
{
public:
    ActivatableList * activatableList;
    QHash<QString, NMDBusActiveConnectionProxy *> activeConnections;
};

NMDBusActiveConnectionMonitor::NMDBusActiveConnectionMonitor(ActivatableList * activatables, QObject * parent)
: QObject(parent), d_ptr(new NMDBusActiveConnectionMonitorPrivate)
{
    Q_D(NMDBusActiveConnectionMonitor);
    d->activatableList = activatables;

    connect(Solid::Control::NetworkManager::notifier(),
            SIGNAL(activeConnectionsChanged()),
            this, SLOT(activeConnectionListChanged()));

    connect(Solid::Control::NetworkManager::notifier(),
            SIGNAL(statusChanged(Solid::Networking::Status)),
            this, SLOT(networkingStatusChanged(Solid::Networking::Status)));

    activeConnectionListChanged();
}

NMDBusActiveConnectionMonitor::~NMDBusActiveConnectionMonitor()
{
    delete d_ptr;
}

void NMDBusActiveConnectionMonitor::activeConnectionListChanged()
{
    // update all InterfaceConnections we know about
    Q_D(NMDBusActiveConnectionMonitor);

    QStringList currentActiveConnections = Solid::Control::NetworkManager::activeConnections();

    // delete any stale interfaces
    foreach (const QString &key, d->activeConnections.keys()) {
        if (!currentActiveConnections.contains(key)) {
            NMDBusActiveConnectionProxy * stale = d->activeConnections.take(key);
            kDebug() << "removing stale active connection" << key;
            delete stale;
        }
    }

    // create an interface to any active connections we're not already tracking
    // and update their interfaceconnections
    foreach (const QString &activeConnectionPath, currentActiveConnections) {
        if (!d->activeConnections.contains(activeConnectionPath)) {
            kDebug() << "Adding active connection interface for " << activeConnectionPath;

            OrgFreedesktopNetworkManagerConnectionActiveInterface * active = new OrgFreedesktopNetworkManagerConnectionActiveInterface("org.freedesktop.NetworkManager", activeConnectionPath, QDBusConnection::systemBus(), 0);

            Knm::InterfaceConnection * ic = interfaceConnectionForConnectionActive(active);
            if (ic) {
                NMDBusActiveConnectionProxy* proxy;
                if (active->vpn()) {
                    proxy = new NMDBusVPNConnectionProxy(ic, active);
                } else {
                    proxy = new NMDBusActiveConnectionProxy(ic, active);
                }
                d->activeConnections.insert(activeConnectionPath, proxy);
            }
            // put the service and the object path into a list of active connections
            kDebug() << "Connection active at" << active->serviceName() << active->connection().path() << (active->vpn() ? "is" : "is not") << "a VPN connection";
        }
    }
}

Knm::InterfaceConnection * NMDBusActiveConnectionMonitor::interfaceConnectionForConnectionActive(OrgFreedesktopNetworkManagerConnectionActiveInterface * connectionActive)
{
    Q_D(NMDBusActiveConnectionMonitor);
    Knm::InterfaceConnection * ic = 0;
    QList<Knm::Activatable *> activatables = d->activatableList->activatables();
    // check whether each interfaceconnection is for the changed active connection
    foreach (Knm::Activatable * activatable, activatables) {
        Knm::InterfaceConnection * candidate = qobject_cast<Knm::InterfaceConnection*>(activatable);
        // ignore HiddenWICs, we don't set status on these
        if (candidate && candidate->activatableType() != Knm::Activatable::HiddenWirelessInterfaceConnection) {
            if (candidate->property("NMDBusService") == connectionActive->serviceName()
                    && candidate->property("NMDBusObjectPath") == connectionActive->connection().path()
                    && (candidate->activatableType() == Knm::Activatable::VpnInterfaceConnection
                        || connectionActive->devices().contains(QDBusObjectPath(candidate->deviceUni())))
                    ) {
                ic = candidate;
                break;
            }
        }
    }

    return ic;
}

void NMDBusActiveConnectionMonitor::handleAdd(Knm::Activatable *)
{
}

void NMDBusActiveConnectionMonitor::handleUpdate(Knm::Activatable *)
{
}

void NMDBusActiveConnectionMonitor::handleRemove(Knm::Activatable * removed)
{
    Q_D(NMDBusActiveConnectionMonitor);
    // we remove proxies if their interfaceconnection is removed so that we don't need a fixed order
    // ActiveConnection removal and InterfaceConnection removal events
    QMutableHashIterator<QString, NMDBusActiveConnectionProxy*> i(d->activeConnections);
    while (i.hasNext()) {
        i.next();
        NMDBusActiveConnectionProxy * candidate = i.value();
        if (candidate->interfaceConnection() == removed) {
            i.remove();
            kDebug() << "removing active connection because its connection was removed";
            delete candidate;
        }
    }
}

void NMDBusActiveConnectionMonitor::networkingStatusChanged(Solid::Networking::Status status)
{
    if (status == Solid::Networking::Unknown) {
        Q_D(NMDBusActiveConnectionMonitor);
        if (status == Solid::Networking::Unknown) {
            // the manager probably exited, clean our state
            qDeleteAll(d->activeConnections);
            d->activeConnections.clear();
        }
    }
}

// vim: sw=4 sts=4 et tw=100