|
| 1 | +#include "connection.hpp" |
| 2 | + |
| 3 | +#include <qcontainerfwd.h> |
| 4 | +#include <qdbusconnection.h> |
| 5 | +#include <qlogging.h> |
| 6 | +#include <qloggingcategory.h> |
| 7 | +#include <qobject.h> |
| 8 | +#include <qstring.h> |
| 9 | +#include <qtypes.h> |
| 10 | + |
| 11 | +#include "../../dbus/properties.hpp" |
| 12 | +#include "nm/dbus_nm_connection.h" |
| 13 | + |
| 14 | +using namespace qs::dbus; |
| 15 | + |
| 16 | +namespace qs::network { |
| 17 | + |
| 18 | +namespace { |
| 19 | +Q_LOGGING_CATEGORY(logNetworkManager, "quickshell.network.networkmanager", QtWarningMsg); |
| 20 | +} |
| 21 | + |
| 22 | +NMConnectionAdapter::NMConnectionAdapter(const QString& path, QObject* parent): QObject(parent) { |
| 23 | + this->proxy = new DBusNMConnectionProxy( |
| 24 | + "org.freedesktop.NetworkManager", |
| 25 | + path, |
| 26 | + QDBusConnection::systemBus(), |
| 27 | + this |
| 28 | + ); |
| 29 | + |
| 30 | + if (!this->proxy->isValid()) { |
| 31 | + qCWarning(logNetworkManager) << "Cannot create DBus interface for connection at" << path; |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + this->connectionProperties.setInterface(this->proxy); |
| 36 | + this->connectionProperties.updateAllViaGetAll(); |
| 37 | +} |
| 38 | + |
| 39 | +bool NMConnectionAdapter::isValid() const { return this->proxy && this->proxy->isValid(); } |
| 40 | +QString NMConnectionAdapter::address() const { |
| 41 | + return this->proxy ? this->proxy->service() : QString(); |
| 42 | +} |
| 43 | +QString NMConnectionAdapter::path() const { return this->proxy ? this->proxy->path() : QString(); } |
| 44 | + |
| 45 | +NMSettingsAdapter::NMSettingsAdapter(const QString& path, QObject* parent): QObject(parent) { |
| 46 | + this->proxy = new DBusNMSettingsProxy( |
| 47 | + "org.freedesktop.NetworkManager", |
| 48 | + path, |
| 49 | + QDBusConnection::systemBus(), |
| 50 | + this |
| 51 | + ); |
| 52 | + |
| 53 | + if (!this->proxy->isValid()) { |
| 54 | + qCWarning(logNetworkManager) << "Cannot create DBus interface for connection at" << path; |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + QObject::connect( |
| 59 | + this->proxy, |
| 60 | + &DBusNMSettingsProxy::NewConnection, |
| 61 | + this, |
| 62 | + &NMSettingsAdapter::onNewConnection |
| 63 | + ); |
| 64 | + |
| 65 | + QObject::connect( |
| 66 | + this->proxy, |
| 67 | + &DBusNMSettingsProxy::ConnectionRemoved, |
| 68 | + this, |
| 69 | + &NMSettingsAdapter::onConnectionRemoved |
| 70 | + ); |
| 71 | + |
| 72 | + this->settingsProperties.setInterface(this->proxy); |
| 73 | + this->settingsProperties.updateAllViaGetAll(); |
| 74 | + |
| 75 | + this->registerConnections(); |
| 76 | +} |
| 77 | + |
| 78 | +void NMSettingsAdapter::onNewConnection(const QDBusObjectPath& path) { |
| 79 | + this->registerConnection(path.path()); |
| 80 | +} |
| 81 | + |
| 82 | +void NMSettingsAdapter::onConnectionRemoved(const QDBusObjectPath& path) { |
| 83 | + auto* connection = this->mConnectionMap.take(path.path()); |
| 84 | + |
| 85 | + if (!connection) { |
| 86 | + qCDebug(logNetworkManager) << "NetworkManager backend sent removal signal for" << path.path() |
| 87 | + << "which is not registered."; |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + delete connection; |
| 92 | +} |
| 93 | + |
| 94 | +void NMSettingsAdapter::registerConnections() { |
| 95 | + auto pending = this->proxy->ListConnections(); |
| 96 | + auto* call = new QDBusPendingCallWatcher(pending, this); |
| 97 | + |
| 98 | + auto responseCallback = [this](QDBusPendingCallWatcher* call) { |
| 99 | + const QDBusPendingReply<QList<QDBusObjectPath>> reply = *call; |
| 100 | + |
| 101 | + if (reply.isError()) { |
| 102 | + qCWarning(logNetworkManager) << "Failed to get connections: " << reply.error().message(); |
| 103 | + } else { |
| 104 | + for (const QDBusObjectPath& devicePath: reply.value()) { |
| 105 | + this->registerConnection(devicePath.path()); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + delete call; |
| 110 | + }; |
| 111 | + |
| 112 | + QObject::connect(call, &QDBusPendingCallWatcher::finished, this, responseCallback); |
| 113 | +} |
| 114 | + |
| 115 | +void NMSettingsAdapter::registerConnection(const QString& path) { |
| 116 | + auto* connection = new NMConnectionAdapter(path, this); |
| 117 | + |
| 118 | + if (!connection->isValid()) { |
| 119 | + qCWarning(logNetworkManager) << "Ignoring invalid registration of" << path; |
| 120 | + delete connection; |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + this->mConnectionMap.insert(path, connection); |
| 125 | + qCDebug(logNetworkManager) << "Registered connection" << connection; |
| 126 | +} |
| 127 | + |
| 128 | +bool NMSettingsAdapter::isValid() const { return this->proxy && this->proxy->isValid(); } |
| 129 | +QString NMSettingsAdapter::address() const { |
| 130 | + return this->proxy ? this->proxy->service() : QString(); |
| 131 | +} |
| 132 | +QString NMSettingsAdapter::path() const { return this->proxy ? this->proxy->path() : QString(); } |
| 133 | + |
| 134 | +} // namespace qs::network |
0 commit comments