File indexing completed on 2024-04-14 03:57:32

0001 /*
0002     SPDX-FileCopyrightText: 2014 Jan Grulich <jgrulich@redhat.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "settings.h"
0008 
0009 #include <QDBusConnection>
0010 #include <QDBusMetaType>
0011 
0012 Settings::Settings(QObject *parent)
0013     : QObject(parent)
0014     , m_canModify(true)
0015     , m_hostname(QLatin1String("fake-hostname"))
0016     , m_connectionCounter(0)
0017 {
0018     qDBusRegisterMetaType<NMVariantMapMap>();
0019 }
0020 
0021 Settings::~Settings()
0022 {
0023     for (auto it = m_connections.cbegin(); it != m_connections.cend(); ++it) {
0024         const QDBusObjectPath &connection = it.key();
0025         QDBusConnection::sessionBus().unregisterObject(connection.path());
0026         Q_EMIT ConnectionRemoved(connection);
0027     }
0028 
0029     qDeleteAll(m_connections);
0030 }
0031 
0032 bool Settings::canModify() const
0033 {
0034     return m_canModify;
0035 }
0036 
0037 QList<QDBusObjectPath> Settings::connections() const
0038 {
0039     return m_connections.keys();
0040 }
0041 
0042 QString Settings::hostname() const
0043 {
0044     return m_hostname;
0045 }
0046 
0047 QDBusObjectPath Settings::AddConnection(const NMVariantMapMap &connection)
0048 {
0049     Connection *newConnection = new Connection(this, connection);
0050     QString newConnectionPath = QString("/org/kde/fakenetwork/Settings/") + QString::number(m_connectionCounter++);
0051     newConnection->setConnectionPath(newConnectionPath);
0052     m_connections.insert(QDBusObjectPath(newConnectionPath), newConnection);
0053     QDBusConnection::sessionBus().registerObject(newConnectionPath, newConnection, QDBusConnection::ExportScriptableContents);
0054 
0055     connect(newConnection, &Connection::connectionRemoved, this, &Settings::onConnectionRemoved);
0056 
0057     Q_EMIT NewConnection(QDBusObjectPath(newConnectionPath));
0058     // Send it for FakeNetwork separately to get AvailableConnections signal after NewConnection
0059     Q_EMIT connectionAdded(QDBusObjectPath(newConnectionPath));
0060 
0061     return QDBusObjectPath(newConnectionPath);
0062 }
0063 
0064 QDBusObjectPath Settings::AddConnectionUnsaved(const NMVariantMapMap &connection)
0065 {
0066     Q_UNUSED(connection)
0067     // TODO
0068     return QDBusObjectPath();
0069 }
0070 
0071 QDBusObjectPath Settings::GetConnectionByUuid(const QString &uuid)
0072 {
0073     Q_UNUSED(uuid)
0074     // TODO
0075     return QDBusObjectPath();
0076 }
0077 
0078 QList<QDBusObjectPath> Settings::ListConnections()
0079 {
0080     return m_connections.keys();
0081 }
0082 
0083 void Settings::SaveHostname(const QString &hostname)
0084 {
0085     m_hostname = hostname;
0086 }
0087 
0088 void Settings::onConnectionRemoved(const QDBusObjectPath &connectionPath)
0089 {
0090     Connection *connection = m_connections.value(connectionPath);
0091 
0092     if (connection) {
0093         QDBusConnection::sessionBus().unregisterObject(connectionPath.path());
0094         Q_EMIT ConnectionRemoved(connectionPath);
0095         // Send it for FakeNetwork separately to get AvailableConnections signal after ConnectionRemoved
0096         Q_EMIT connectionRemoved(connectionPath);
0097         m_connections.remove(QDBusObjectPath(connectionPath));
0098         delete connection;
0099     }
0100 }
0101 
0102 #include "moc_settings.cpp"