File: nmdbussettingsservice.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 (280 lines) | stat: -rw-r--r-- 10,403 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
Copyright 2008,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/>.
*/

#ifndef NMDBUSSETTINGSSERVICE_CPP
#define NMDBUSSETTINGSSERVICE_CPP

#include "nmdbussettingsservice.h"

#include <NetworkManager.h>

#include <QHash>
#include <QUuid>

#include <QDBusArgument>
#include <QDBusConnection>
#include <QDBusConnectionInterface>

#include <KDebug>
#include <KLocale>

#include <solid/control/networkmanager.h>

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

#include "busconnection.h"
#include "exportedconnection.h"
#include "exportedconnectionsecrets.h"

#include "nm-active-connectioninterface.h"

class NMDBusSettingsServicePrivate
{
public:
    NMDBusSettingsService::ServiceStatus status;
    uint nextConnectionId;
    QHash<QDBusObjectPath, BusConnection *> pathToConnections;
    QHash<QUuid, BusConnection *> uuidToConnections;
    QHash<QUuid, QDBusObjectPath> uuidToPath;
};

const QString NMDBusSettingsService::SERVICE_USER_SETTINGS = QLatin1String(NM_DBUS_SERVICE_USER_SETTINGS);
const QString NMDBusSettingsService::SERVICE_SYSTEM_SETTINGS = QLatin1String(NM_DBUS_SERVICE_SYSTEM_SETTINGS);

NMDBusSettingsService::NMDBusSettingsService(QObject * parent)
: QObject(parent), d_ptr(new NMDBusSettingsServicePrivate)
{
    Q_D(NMDBusSettingsService);
    d->status = Available;
    d->nextConnectionId = 0;

    QDBusReply<QDBusConnectionInterface::RegisterServiceReply> reply = QDBusConnection::systemBus().interface()->registerService(SERVICE_USER_SETTINGS);

    if (reply.value() != QDBusConnectionInterface::ServiceRegistered) {
        // trouble;
        // TODO use QDBusConnectionInterface to get the error and handle AccessDenied as well as the
        // owner error
        kDebug() << "Unable to register service" << QDBusConnection::systemBus().lastError();
        if (reply.error().type() == QDBusError::AccessDenied) {
            d->status = AccessDenied;
        } else if (reply.value() == QDBusConnectionInterface::ServiceNotRegistered) {
            d->status = AlreadyRunning;
        } else {
            d->status = UnknownError;
        }
    }


    //declare types
    qDBusRegisterMetaType<QList<QDBusObjectPath> >();

    QDBusConnection dbus = QDBusConnection::systemBus();
    if (!dbus.registerObject(QLatin1String(NM_DBUS_PATH_SETTINGS), this, QDBusConnection::ExportScriptableContents)) {
        kDebug() << "Unable to register settings object " << NM_DBUS_PATH_SETTINGS;
        d->status = UnknownError;
    }
}

NMDBusSettingsService::~NMDBusSettingsService()
{
    Q_D(const NMDBusSettingsService);
    if ((d->status == Available) && !QDBusConnection::systemBus().unregisterService( "org.freedesktop.NetworkManagerUserSettings" ) ) {
        // trouble;
        kDebug() << "Unable to unregister service";
    }
}

QUuid NMDBusSettingsService::uuidForPath(const QDBusObjectPath& path) const
{
    Q_D(const NMDBusSettingsService);
    BusConnection * busConn = 0;
    if (d->pathToConnections.contains(path)) {
        busConn = d->pathToConnections[path];
        return busConn->connection()->uuid();
    }
    return QUuid();
}

void NMDBusSettingsService::handleAdd(Knm::Connection * added)
{
    Q_D(NMDBusSettingsService);

    // only handle connections that come from local storage, not those from the system settings
    // service
    if ((d->status == Available) && added->origin() == QLatin1String("ConnectionListPersistence")) {
        // put it on our bus 
        QDBusObjectPath objectPath;
        BusConnection * busConn = new BusConnection(added, this);
        new ConnectionAdaptor(busConn);
        new SecretsAdaptor(busConn);

        objectPath = QDBusObjectPath(nextObjectPath());
        // important - make sure all 3 hashes are up to date!
        d->pathToConnections.insert(objectPath, busConn);
        d->uuidToConnections.insert(added->uuid(), busConn);
        d->uuidToPath.insert(added->uuid(), objectPath);

        QDBusConnection::systemBus().registerObject(objectPath.path(), busConn, QDBusConnection::ExportAdaptors);
        emit NewConnection(objectPath);
    }
}

void NMDBusSettingsService::handleUpdate(Knm::Connection * updated)
{
    Q_D(NMDBusSettingsService);

    if ((d->status == Available) && d->uuidToConnections.contains(updated->uuid())) {
        BusConnection * busConn = d->uuidToConnections[updated->uuid()];
        if (busConn) {
            busConn->updateInternal(updated);
        }
    }
}

void NMDBusSettingsService::handleRemove(Knm::Connection * removed)
{
    Q_D(NMDBusSettingsService);
    if (d->status == Available) {
        BusConnection * busConn = d->uuidToConnections.take(removed->uuid());
        if (busConn) {
            QDBusObjectPath key = d->pathToConnections.key(busConn);
            d->uuidToPath.remove(removed->uuid());
            d->pathToConnections.remove(key);
            busConn->Delete();
        }
    }
}

void NMDBusSettingsService::handleAdd(Knm::Activatable * added)
{
    Q_D(NMDBusSettingsService);
    if (d->status == Available) {
        Knm::InterfaceConnection * ic = qobject_cast<Knm::InterfaceConnection*>(added);
        if (ic && ic->activatableType() != Knm::Activatable::HiddenWirelessInterfaceConnection) {
            // listen to the IC
            kDebug() << ic->connectionUuid();
            //if (ic->activatableType() != Knm::Activatable::VpnInterfaceConnection) {
            connect(ic, SIGNAL(activated()), this, SLOT(interfaceConnectionActivated()));
            connect(ic, SIGNAL(deactivated()), this, SLOT(interfaceConnectionDeactivated()));
            //}

            // if derived from one of our connections, tag it with the service and object path of the
            // connection.  The system settings monitor NMDBusSettingsConnectionProvider does this for
            // its connections.
            if (d->uuidToPath.contains(ic->connectionUuid())) {
                kDebug() << "tagging local InterfaceConnection " << ic->connectionName() << SERVICE_USER_SETTINGS << d->uuidToPath[ic->connectionUuid()].path();
                ic->setProperty("NMDBusService", SERVICE_USER_SETTINGS);
                ic->setProperty("NMDBusObjectPath", d->uuidToPath[ic->connectionUuid()].path());
            }
        }
    }
}

void NMDBusSettingsService::interfaceConnectionActivated()
{
    Knm::InterfaceConnection * ic = qobject_cast<Knm::InterfaceConnection*>(sender());

    if (ic) {
        QString deviceToActivateOn;
        QVariantMap extraArguments;

        Knm::VpnInterfaceConnection * vpn = qobject_cast<Knm::VpnInterfaceConnection*>(ic);
        if (vpn) {
            // look up the active connection (a real connection, not this vpn that is being activated)
            // because NM needs its details to bring up the VPN
            QString activeConnPath;
            foreach (const QString &activeConnectionPath, Solid::Control::NetworkManager::activeConnections()) {
                OrgFreedesktopNetworkManagerConnectionActiveInterface activeConnection("org.freedesktop.NetworkManager", activeConnectionPath, QDBusConnection::systemBus());

                if ( activeConnection.getDefault() && activeConnection.state() == NM_ACTIVE_CONNECTION_STATE_ACTIVATED) {
                    activeConnPath = activeConnection.path();
                    QList<QDBusObjectPath> devs = activeConnection.devices();
                    if (!devs.isEmpty()) {
                        deviceToActivateOn = devs.first().path();
                    }
                }
            }

            kDebug() << "active" << activeConnPath << "device" << deviceToActivateOn;

            if ( activeConnPath.isEmpty() || deviceToActivateOn.isEmpty() )
                return;

            extraArguments.insert( "extra_connection_parameter", activeConnPath );
        } else {
            deviceToActivateOn = ic->deviceUni();
        }

        Solid::Control::NetworkManager::activateConnection(deviceToActivateOn,
                QString::fromLatin1("%1 %2")
                .arg(ic->property("NMDBusService").toString(), ic->property("NMDBusObjectPath").toString()),
                extraArguments);
    }
}

void NMDBusSettingsService::interfaceConnectionDeactivated()
{
    Knm::InterfaceConnection * ic = qobject_cast<Knm::InterfaceConnection*>(sender());
    Solid::Control::NetworkManager::deactivateConnection(ic->property("NMDBusActiveConnectionObject").toString());
}

void NMDBusSettingsService::handleUpdate(Knm::Activatable *)
{

}
void NMDBusSettingsService::handleRemove(Knm::Activatable *)
{

}

QString NMDBusSettingsService::nextObjectPath()
{
    Q_D(NMDBusSettingsService);
    return QString::fromLatin1("%1/%2").arg(QLatin1String(NM_DBUS_PATH_SETTINGS)).arg(d->nextConnectionId++);
}

QList<QDBusObjectPath> NMDBusSettingsService::ListConnections() const
{
    Q_D(const NMDBusSettingsService);
    QList<QDBusObjectPath> pathList = d->pathToConnections.keys();
    kDebug() << "There are " << pathList.count() << " known connections";
    return pathList;
}

NMDBusSettingsService::ServiceStatus NMDBusSettingsService::serviceStatus() const
{
    Q_D(const NMDBusSettingsService);
    return d->status;
}

#if QT_VERSION < 0x040500
inline bool operator==(const QDBusObjectPath &lhs, const QDBusObjectPath &rhs)
{ return lhs.path() == rhs.path(); }

inline bool operator!=(const QDBusObjectPath &lhs, const QDBusObjectPath &rhs)
{ return lhs.path() != rhs.path(); }

inline bool operator<(const QDBusObjectPath &lhs, const QDBusObjectPath &rhs)
{ return lhs.path() < rhs.path(); }
#endif

#endif // NMDBUSSETTINGSSERVICE_CPP