NetworkManagerQt

settings.cpp
1 /*
2  SPDX-FileCopyrightText: 2011-2013 Lamarque Souza <[email protected]>
3  SPDX-FileCopyrightText: 2013 Jan Grulich <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7 
8 #include "settings.h"
9 #include "macros.h"
10 #include "manager_p.h"
11 #include "settings_p.h"
12 
13 #include <QDBusObjectPath>
14 
15 #include <nm-setting-connection.h>
16 
17 #include "nmdebug.h"
18 
19 // NM_GLOBAL_STATIC(NetworkManager::SettingsPrivate, globalSettings)
20 Q_GLOBAL_STATIC(NetworkManager::SettingsPrivate, globalSettings)
21 
22 NetworkManager::SettingsPrivate::SettingsPrivate()
23 #ifdef NMQT_STATIC
24  : iface(NetworkManagerPrivate::DBUS_SERVICE, NetworkManagerPrivate::DBUS_SETTINGS_PATH, QDBusConnection::sessionBus())
25 #else
26  : iface(NetworkManagerPrivate::DBUS_SERVICE, NetworkManagerPrivate::DBUS_SETTINGS_PATH, QDBusConnection::systemBus())
27 #endif
28  , m_canModify(true)
29 {
30  QDBusConnection::systemBus().connect(NetworkManagerPrivate::DBUS_SERVICE,
31  NetworkManagerPrivate::DBUS_SETTINGS_PATH,
32  NetworkManagerPrivate::FDO_DBUS_PROPERTIES,
33  QLatin1String("PropertiesChanged"),
34  this,
35  SLOT(dbusPropertiesChanged(QString, QVariantMap, QStringList)));
36  connect(&iface, &OrgFreedesktopNetworkManagerSettingsInterface::NewConnection, this, &SettingsPrivate::onConnectionAdded);
37  connect(&iface,
38  &OrgFreedesktopNetworkManagerSettingsInterface::ConnectionRemoved,
39  this,
40  static_cast<void (SettingsPrivate::*)(const QDBusObjectPath &)>(&SettingsPrivate::onConnectionRemoved));
41  init();
42  // This class is a friend of NetworkManagerPrivate thus initted there too
43  // because of the init chain we must follow,
44  // But if this class is used first we need to make sure the
45  // NetworkManagerPrivate also get created so we have its signals for
46  // when the daemon dies, we just can not call it directly here or
47  // we will have a constructor infinite loop
48  QTimer::singleShot(0, this, SLOT(initNotifier()));
49 }
50 
51 void NetworkManager::SettingsPrivate::init()
52 {
53  const QList<QDBusObjectPath> connectionList = iface.connections();
54  qCDebug(NMQT) << "Connections list";
55  for (const QDBusObjectPath &connection : connectionList) {
56  if (!connections.contains(connection.path())) {
57  connections.insert(connection.path(), Connection::Ptr());
58  Q_EMIT connectionAdded(connection.path());
59  qCDebug(NMQT) << " " << connection.path();
60  }
61  }
62 
63  // Get all Setting's properties at once
64  QVariantMap initialProperties = NetworkManagerPrivate::retrieveInitialProperties(iface.staticInterfaceName(), NetworkManagerPrivate::DBUS_SETTINGS_PATH);
65  if (!initialProperties.isEmpty()) {
66  propertiesChanged(initialProperties);
67  }
68 }
69 
70 NetworkManager::Connection::List NetworkManager::SettingsPrivate::listConnections()
71 {
73  QMap<QString, Connection::Ptr>::const_iterator i = connections.constBegin();
74  while (i != connections.constEnd()) {
75  NetworkManager::Connection::Ptr connection = findRegisteredConnection(i.key());
76  if (connection) {
77  list << connection;
78  }
79  ++i;
80  }
81  return list;
82 }
83 
84 NetworkManager::Connection::Ptr NetworkManager::SettingsPrivate::findConnectionByUuid(const QString &uuid)
85 {
86  QMap<QString, Connection::Ptr>::const_iterator i = connections.constBegin();
87  while (i != connections.constEnd()) {
88  NetworkManager::Connection::Ptr connection = findRegisteredConnection(i.key());
89  if (connection && connection->uuid() == uuid) {
90  return connection;
91  }
92  ++i;
93  }
94 
96 }
97 
98 QString NetworkManager::SettingsPrivate::hostname() const
99 {
100  return m_hostname;
101 }
102 
103 bool NetworkManager::SettingsPrivate::canModify() const
104 {
105  return m_canModify;
106 }
107 
108 QDBusPendingReply<QDBusObjectPath> NetworkManager::SettingsPrivate::addConnection(const NMVariantMapMap &connection)
109 {
110  return iface.AddConnection(connection);
111 }
112 
113 QDBusPendingReply<QDBusObjectPath> NetworkManager::SettingsPrivate::addConnectionUnsaved(const NMVariantMapMap &connection)
114 {
115  return iface.AddConnectionUnsaved(connection);
116 }
117 
118 QDBusPendingReply<bool, QStringList> NetworkManager::SettingsPrivate::loadConnections(const QStringList &filenames)
119 {
120  return iface.LoadConnections(filenames);
121 }
122 
123 QDBusPendingReply<bool> NetworkManager::SettingsPrivate::reloadConnections()
124 {
125  return iface.ReloadConnections();
126 }
127 
128 void NetworkManager::SettingsPrivate::initNotifier()
129 {
130  notifier();
131 }
132 
133 void NetworkManager::SettingsPrivate::saveHostname(const QString &hostname)
134 {
135  iface.SaveHostname(hostname);
136 }
137 
138 void NetworkManager::SettingsPrivate::dbusPropertiesChanged(const QString &interfaceName,
139  const QVariantMap &properties,
140  const QStringList &invalidatedProperties)
141 {
142  Q_UNUSED(invalidatedProperties);
143  if (interfaceName == QLatin1String("org.freedesktop.NetworkManager.Settings")) {
144  propertiesChanged(properties);
145  }
146 }
147 
148 void NetworkManager::SettingsPrivate::propertiesChanged(const QVariantMap &properties)
149 {
150  QVariantMap::const_iterator it = properties.constBegin();
151  while (it != properties.constEnd()) {
152  const QString property = it.key();
153  if (property == QLatin1String("CanModify")) {
154  m_canModify = it->toBool();
155  Q_EMIT canModifyChanged(m_canModify);
156  } else if (property == QLatin1String("Hostname")) {
157  m_hostname = it->toString();
158  Q_EMIT hostnameChanged(m_hostname);
159  } else if (property == QLatin1String("Connections")) {
160  // will never get here in runtime NM < 0.9.10
161  // TODO some action??
162  } else {
163  qCWarning(NMQT) << Q_FUNC_INFO << "Unhandled property" << property;
164  }
165  ++it;
166  }
167 }
168 
169 void NetworkManager::SettingsPrivate::onConnectionAdded(const QDBusObjectPath &path)
170 {
171  const QString id = path.path();
172  if (connections.contains(id)) {
173  return;
174  }
175  connections.insert(id, Connection::Ptr());
176  Q_EMIT connectionAdded(id);
177 }
178 
179 NetworkManager::Connection::Ptr NetworkManager::SettingsPrivate::findRegisteredConnection(const QString &path)
180 {
181  Connection::Ptr ret;
182  if (!path.isEmpty()) {
183  bool contains = connections.contains(path);
184  if (contains && connections.value(path)) {
185  ret = connections.value(path);
186  } else {
187  ret = Connection::Ptr(new Connection(path), &QObject::deleteLater);
188  connections[path] = ret;
189  connect(ret.data(), SIGNAL(removed(QString)), this, SLOT(onConnectionRemoved(QString)));
190  if (!contains) {
191  Q_EMIT connectionAdded(path);
192  }
193  }
194  }
195  return ret;
196 }
197 
198 void NetworkManager::SettingsPrivate::onConnectionRemoved(const QDBusObjectPath &path)
199 {
200  onConnectionRemoved(path.path());
201 }
202 
203 void NetworkManager::SettingsPrivate::onConnectionRemoved(const QString &path)
204 {
205  connections.remove(path);
206  Q_EMIT connectionRemoved(path);
207 }
208 
209 void NetworkManager::SettingsPrivate::daemonUnregistered()
210 {
211  connections.clear();
212 }
213 
215 {
216  return globalSettings->listConnections();
217 }
218 
220 {
221  return globalSettings->findConnectionByUuid(uuid);
222 }
223 
225 {
226  return globalSettings->findRegisteredConnection(path);
227 }
228 
230 {
231  return globalSettings->addConnection(connection);
232 }
233 
235 {
236  return globalSettings->addConnectionUnsaved(connection);
237 }
238 
240 {
241  return globalSettings->loadConnections(filenames);
242 }
243 
245 {
246  return globalSettings->reloadConnections();
247 }
248 
249 void NetworkManager::saveHostname(const QString &hostname)
250 {
251  globalSettings->saveHostname(hostname);
252 }
253 
255 {
256  return globalSettings->canModify();
257 }
258 
260 {
261  return globalSettings->hostname();
262 }
263 
265 {
266  return globalSettings;
267 }
bool connect(const QString &service, const QString &path, const QString &interface, const QString &name, QObject *receiver, const char *slot)
NETWORKMANAGERQT_EXPORT bool canModify()
Returns true if the user can modify the settings.
Definition: settings.cpp:254
QCA_EXPORT void init()
KGuiItem properties()
NETWORKMANAGERQT_EXPORT QDBusPendingReply< QDBusObjectPath > addConnection(const NMVariantMapMap &settings)
Add new connection and save it to disk.
Definition: settings.cpp:108
NETWORKMANAGERQT_EXPORT NetworkManager::Connection::List listConnections()
Retrieves the list of connections.
Definition: settings.cpp:70
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
NETWORKMANAGERQT_EXPORT NetworkManager::Connection::Ptr findConnectionByUuid(const QString &uuid)
Retrieves the connection for the given uuid, returns null if not found.
Definition: settings.cpp:84
void deleteLater()
NETWORKMANAGERQT_EXPORT QString hostname()
Returns hostname of the machine.
Definition: settings.cpp:259
QDBusConnection sessionBus()
This class manages provides access to connections and notify about new ones.
Definition: settings.h:26
bool isEmpty() const const
NETWORKMANAGERQT_EXPORT SettingsNotifier * settingsNotifier()
Notifier object for connecting signals.
Definition: settings.cpp:264
void insert(int i, const T &value)
NETWORKMANAGERQT_EXPORT QDBusPendingReply< QDBusObjectPath > addConnectionUnsaved(const NMVariantMapMap &settings)
Add new connection but do not save it to disk immediately.
Definition: settings.cpp:113
QDBusConnection systemBus()
NETWORKMANAGERQT_EXPORT QDBusPendingReply< bool > reloadConnections()
Tells NetworkManager to reload all connection files from disk, including noticing any added or delete...
Definition: settings.cpp:123
QString path(const QString &relativePath)
QString & insert(int position, QChar ch)
NETWORKMANAGERQT_EXPORT NetworkManager::Connection::Ptr findConnection(const QString &path)
Retrieves the connection for the given path, returns null if not found.
Definition: settings.cpp:224
NETWORKMANAGERQT_EXPORT QDBusPendingReply< bool, QStringList > loadConnections(const QStringList &filenames)
Loads or reloads the indicated connections from disk.
Definition: settings.cpp:118
NETWORKMANAGERQT_EXPORT void saveHostname(const QString &hostname)
Configure the following hostname.
Definition: settings.cpp:133
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 04:05:36 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.