NetworkManagerQt

connection.cpp
1/*
2 SPDX-FileCopyrightText: 2008, 2009 Will Stephenson <wstephenson@kde.org>
3 SPDX-FileCopyrightText: 2011 Ilia Kats <ilia-kats@gmx.net>
4 SPDX-FileCopyrightText: 2011-2013 Lamarque V. Souza <lamarque@kde.org>
5 SPDX-FileCopyrightText: 2013 Jan Grulich <jgrulich@redhat.com>
6
7 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
8*/
9
10#include "connection_p.h"
11
12#undef signals
13#include <libnm/NetworkManager.h>
14#define signals Q_SIGNALS
15
16#include <QDBusConnection>
17#include <QDBusMetaType>
18#include <QDBusPendingCallWatcher>
19#include <QDBusReply>
20
21#include "nmdebug.h"
22
23NetworkManager::ConnectionPrivate::ConnectionPrivate(const QString &path, Connection *q)
24#ifdef NMQT_STATIC
25 : iface(NetworkManagerPrivate::DBUS_SERVICE, path, QDBusConnection::sessionBus())
26#else
27 : iface(NetworkManagerPrivate::DBUS_SERVICE, path, QDBusConnection::systemBus())
28#endif
29 , q_ptr(q)
30{
31}
32
34 : QObject(parent)
35 , d_ptr(new ConnectionPrivate(path, this))
36{
38
39 qDBusRegisterMetaType<NMVariantMapMap>();
40 QDBusReply<NMVariantMapMap> reply = d->iface.GetSettings();
41 if (reply.isValid()) {
42 d->updateSettings(reply.value());
43 } else {
44 d->updateSettings();
45 }
46 d->path = path;
47 // qCDebug(NMQT) << m_connection;
48
49 connect(&d->iface, &OrgFreedesktopNetworkManagerSettingsConnectionInterface::Updated, d, &ConnectionPrivate::onConnectionUpdated);
50 connect(&d->iface, &OrgFreedesktopNetworkManagerSettingsConnectionInterface::Removed, d, &ConnectionPrivate::onConnectionRemoved);
51
52 QDBusConnection::systemBus().connect(NetworkManagerPrivate::DBUS_SERVICE,
53 d->path,
54 NetworkManagerPrivate::FDO_DBUS_PROPERTIES,
55 QLatin1String("PropertiesChanged"),
56 d,
57 SLOT(dbusPropertiesChanged(QString, QVariantMap, QStringList)));
58 d->unsaved = d->iface.unsaved();
59}
60
61NetworkManager::Connection::~Connection()
62{
63 delete d_ptr;
64}
65
67{
68 Q_D(const Connection);
69 return d->iface.isValid();
70}
71
73{
74 Q_D(const Connection);
75 return d->uuid;
76}
77
79{
80 Q_D(const Connection);
81 return d->id;
82}
83
85{
86 Q_D(const Connection);
87 return d->unsaved;
88}
89
91{
93
94 if (d->connection.isNull()) {
95 d->connection = ConnectionSettings::Ptr(new ConnectionSettings(d->settings));
96 }
97 return d->connection;
98}
99
101{
103 return d->iface.GetSecrets(setting);
104}
105
107{
109 return d->iface.Update(settings);
110}
111
113{
115 return d->iface.UpdateUnsaved(settings);
116}
117
119{
121 return d->iface.Save();
122}
123
125{
127 return d->iface.ClearSecrets();
128}
129
131{
133 return d->iface.Delete();
134}
135
137{
138 Q_D(const Connection);
139 return d->path;
140}
141
142void NetworkManager::ConnectionPrivate::onConnectionUpdated()
143{
144 Q_Q(Connection);
145 QDBusReply<NMVariantMapMap> reply = iface.GetSettings();
146 if (reply.isValid()) {
147 updateSettings(reply.value());
148 } else {
149 updateSettings();
150 }
151 Q_EMIT q->updated();
152}
153
154void NetworkManager::ConnectionPrivate::onConnectionRemoved()
155{
156 Q_Q(Connection);
157 QString tmpPath = path;
158 updateSettings();
159 Q_EMIT q->removed(tmpPath);
160}
161
162void NetworkManager::ConnectionPrivate::dbusPropertiesChanged(const QString &interfaceName,
163 const QVariantMap &properties,
164 const QStringList &invalidatedProperties)
165{
166 Q_UNUSED(invalidatedProperties);
167 if (interfaceName == QLatin1String("org.freedesktop.NetworkManager.Settings.Connection")) {
168 onPropertiesChanged(properties);
169 }
170}
171
172void NetworkManager::ConnectionPrivate::onPropertiesChanged(const QVariantMap &properties)
173{
174 Q_Q(Connection);
175 QVariantMap::const_iterator it = properties.constBegin();
176 while (it != properties.constEnd()) {
177 const QString property = it.key();
178 if (property == QLatin1String("Unsaved")) {
179 unsaved = it->toBool();
180 Q_EMIT q->unsavedChanged(unsaved);
181 } else {
182 qCWarning(NMQT) << Q_FUNC_INFO << "Unhandled property" << property;
183 }
184 ++it;
185 }
186}
187
188void NetworkManager::ConnectionPrivate::updateSettings(const NMVariantMapMap &newSettings)
189{
190 settings = newSettings;
191 if (settings.contains(QLatin1String(NM_SETTING_CONNECTION_SETTING_NAME))) {
192 QVariantMap connectionSetting = settings.value(QLatin1String(NM_SETTING_CONNECTION_SETTING_NAME));
193 if (connectionSetting.contains(QLatin1String(NM_SETTING_CONNECTION_UUID))) {
194 uuid = connectionSetting.value(QLatin1String(NM_SETTING_CONNECTION_UUID)).toString();
195 }
196 if (connectionSetting.contains(QLatin1String(NM_SETTING_CONNECTION_ID))) {
197 id = connectionSetting.value(QLatin1String(NM_SETTING_CONNECTION_ID)).toString();
198 }
199 } else if (newSettings.isEmpty()) {
200 uuid.clear();
201 id.clear();
202 }
203 connection.clear();
204}
205
206#include "moc_connection.cpp"
207#include "moc_connection_p.cpp"
Represents collection of all connection settings.
This class represents a single network connection configuration.
Definition connection.h:28
QString name() const
Returns the name of this connection.
bool isValid() const
Returns if this connection is valid.
QDBusPendingReply< NMVariantMapMap > secrets(const QString &setting)
Retrieves this connections's secrets (passwords and / or encryption keys).
QDBusPendingReply clearSecrets()
Clear the secrets belonging to this network connection profile.
QDBusPendingReply save()
Saves a "dirty" connection (that had previously been updated with updateUnsaved()) to persistent stor...
bool isUnsaved() const
If set, indicates that the in-memory state of the connection does not match the on-disk state.
ConnectionSettings::Ptr settings()
Returns the settings of this connection.
QDBusPendingReply updateUnsaved(const NMVariantMapMap &settings)
Update the connection with new settings and properties (replacing all previous settings and propertie...
QDBusPendingReply remove()
Removes the connection from NetworkManager database, this operation does not ask for confirmation but...
QString uuid() const
Returns the unique identifier of this connection.
Connection(const QString &path, QObject *parent=nullptr)
Constructs a connection object for the given path.
QString path() const
Returns the path (DBus) of this connection.
QDBusPendingReply update(const NMVariantMapMap &settings)
Update the connection with new settings and properties, replacing all previous settings and propertie...
QString path(const QString &relativePath)
KGuiItem properties()
bool connect(const QString &service, const QString &path, const QString &interface, const QString &name, QObject *receiver, const char *slot)
QDBusConnection sessionBus()
QDBusConnection systemBus()
bool isValid() const const
void clear()
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:01:05 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.