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
33NetworkManager::Connection::Connection(const QString &path, QObject *parent)
34 : QObject(parent)
35 , d_ptr(new ConnectionPrivate(path, this))
36{
37 Q_D(Connection);
38
39 qDBusRegisterMetaType<NMVariantMapMap>();
40 QDBusReply<NMVariantMapMap> reply = d->iface.GetSettings();
41 if (reply.isValid()) {
42 d->updateSettings(newSettings: reply.value());
43 } else {
44 d->updateSettings();
45 }
46 d->path = path;
47 // qCDebug(NMQT) << m_connection;
48
49 connect(sender: &d->iface, signal: &OrgFreedesktopNetworkManagerSettingsConnectionInterface::Updated, context: d, slot: &ConnectionPrivate::onConnectionUpdated);
50 connect(sender: &d->iface, signal: &OrgFreedesktopNetworkManagerSettingsConnectionInterface::Removed, context: d, slot: &ConnectionPrivate::onConnectionRemoved);
51
52 QDBusConnection::systemBus().connect(service: NetworkManagerPrivate::DBUS_SERVICE,
53 path: d->path,
54 interface: NetworkManagerPrivate::FDO_DBUS_PROPERTIES,
55 name: QLatin1String("PropertiesChanged"),
56 receiver: 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
66bool NetworkManager::Connection::isValid() const
67{
68 Q_D(const Connection);
69 return d->iface.isValid();
70}
71
72QString NetworkManager::Connection::uuid() const
73{
74 Q_D(const Connection);
75 return d->uuid;
76}
77
78QString NetworkManager::Connection::name() const
79{
80 Q_D(const Connection);
81 return d->id;
82}
83
84bool NetworkManager::Connection::isUnsaved() const
85{
86 Q_D(const Connection);
87 return d->unsaved;
88}
89
90NetworkManager::ConnectionSettings::Ptr NetworkManager::Connection::settings()
91{
92 Q_D(Connection);
93
94 if (d->connection.isNull()) {
95 d->connection = ConnectionSettings::Ptr(new ConnectionSettings(d->settings));
96 }
97 return d->connection;
98}
99
100QDBusPendingReply<NMVariantMapMap> NetworkManager::Connection::secrets(const QString &setting)
101{
102 Q_D(Connection);
103 return d->iface.GetSecrets(setting_name: setting);
104}
105
106QDBusPendingReply<> NetworkManager::Connection::update(const NMVariantMapMap &settings)
107{
108 Q_D(Connection);
109 return d->iface.Update(properties: settings);
110}
111
112QDBusPendingReply<> NetworkManager::Connection::updateUnsaved(const NMVariantMapMap &settings)
113{
114 Q_D(Connection);
115 return d->iface.UpdateUnsaved(properties: settings);
116}
117
118QDBusPendingReply<> NetworkManager::Connection::save()
119{
120 Q_D(Connection);
121 return d->iface.Save();
122}
123
124QDBusPendingReply<> NetworkManager::Connection::clearSecrets()
125{
126 Q_D(Connection);
127 return d->iface.ClearSecrets();
128}
129
130QDBusPendingReply<> NetworkManager::Connection::remove()
131{
132 Q_D(Connection);
133 return d->iface.Delete();
134}
135
136QString NetworkManager::Connection::path() const
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(newSettings: 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(path: 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(key: QLatin1String(NM_SETTING_CONNECTION_SETTING_NAME))) {
192 QVariantMap connectionSetting = settings.value(key: QLatin1String(NM_SETTING_CONNECTION_SETTING_NAME));
193 if (connectionSetting.contains(key: QLatin1String(NM_SETTING_CONNECTION_UUID))) {
194 uuid = connectionSetting.value(key: QLatin1String(NM_SETTING_CONNECTION_UUID)).toString();
195 }
196 if (connectionSetting.contains(key: QLatin1String(NM_SETTING_CONNECTION_ID))) {
197 id = connectionSetting.value(key: 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"
208

source code of networkmanager-qt/src/connection.cpp