File indexing completed on 2024-05-12 15:48:10

0001 /*
0002     SPDX-FileCopyrightText: 2015 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 "fakemodem.h"
0008 #include "dbus/fakedbus.h"
0009 
0010 #include <QDBusConnection>
0011 
0012 FakeModem::FakeModem(QObject *parent)
0013     : QObject(parent)
0014     , m_bearerCounter(0)
0015     , m_modemCounter(0)
0016     , m_objectManager(new ObjectManager(this))
0017 {
0018     qDBusRegisterMetaType<ModemManager::MMVariantMapMap>();
0019     qDBusRegisterMetaType<ModemManager::UIntList>();
0020     qDBusRegisterMetaType<ModemManager::CurrentModesType>();
0021     qDBusRegisterMetaType<ModemManager::SupportedModesType>();
0022     qDBusRegisterMetaType<ModemManager::SignalQualityPair>();
0023     qDBusRegisterMetaType<ModemManager::PortList>();
0024     qDBusRegisterMetaType<ModemManager::UnlockRetriesMap>();
0025     qDBusRegisterMetaType<ModemManager::Port>();
0026     qDBusRegisterMetaType<ModemManager::LocationInformationMap>();
0027 
0028     QDBusConnection::sessionBus().registerService(QLatin1String(MMQT_DBUS_SERVICE));
0029     QDBusConnection::sessionBus().registerObject(QLatin1String(MMQT_DBUS_PATH),
0030                                                  this,
0031                                                  QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors);
0032 }
0033 
0034 FakeModem::~FakeModem()
0035 {
0036     QDBusConnection::sessionBus().unregisterObject(QLatin1String(MMQT_DBUS_SERVICE));
0037     QDBusConnection::sessionBus().unregisterService(QLatin1String(MMQT_DBUS_SERVICE));
0038 }
0039 
0040 void FakeModem::addModem(Modem *modem)
0041 {
0042     QString newModemPath = QStringLiteral("/org/kde/fakemodem/Modem/") + QString::number(m_modemCounter++);
0043     modem->setModemPath(newModemPath);
0044     // Start monitoring property changes
0045     modem->setEnableNotifications(true);
0046     m_modems.insert(QDBusObjectPath(newModemPath), modem);
0047     QDBusConnection::sessionBus().registerObject(newModemPath, modem, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors);
0048 
0049     ModemManager::MMVariantMapMap interfaces;
0050     interfaces.insert(QLatin1String(MMQT_DBUS_INTERFACE_MODEM), modem->toMap());
0051 
0052     m_objectManager->addInterfaces(QDBusObjectPath(newModemPath), interfaces);
0053 }
0054 
0055 void FakeModem::removeModem(Modem *modem)
0056 {
0057     m_modems.remove(QDBusObjectPath(modem->modemPath()));
0058     QDBusConnection::sessionBus().unregisterObject(modem->modemPath());
0059 
0060     m_objectManager->removeInterfaces(QDBusObjectPath(modem->modemPath()), {QLatin1String(MMQT_DBUS_INTERFACE_MODEM)});
0061 }
0062 
0063 void FakeModem::addBearer(Bearer *bearer)
0064 {
0065     QString newBearerPath = QStringLiteral("/org/kde/fakemodem/Bearer/") + QString::number(m_bearerCounter++);
0066     bearer->setBearerPath(newBearerPath);
0067     bearer->setEnableNotifications(true);
0068     m_bearers.insert(QDBusObjectPath(newBearerPath), bearer);
0069     QDBusConnection::sessionBus().registerObject(newBearerPath, bearer, QDBusConnection::ExportScriptableContents);
0070     Q_FOREACH (Modem *modem, m_modems.values()) {
0071         modem->addBearer(QDBusObjectPath(newBearerPath));
0072         QVariantMap map;
0073         map.insert(QLatin1String("Bearers"), QVariant::fromValue<QList<QDBusObjectPath>>(modem->bearers()));
0074         QDBusMessage message =
0075             QDBusMessage::createSignal(modem->modemPath(), QLatin1String("org.freedesktop.DBus.Properties"), QLatin1String("PropertiesChanged"));
0076         message << QLatin1String("org.kde.fakemodem.Modem") << map << QStringList();
0077         QDBusConnection::sessionBus().send(message);
0078     }
0079 }
0080 
0081 void FakeModem::removeBearer(Bearer *bearer)
0082 {
0083     m_bearers.remove(QDBusObjectPath(bearer->bearerPath()));
0084     QDBusConnection::sessionBus().unregisterObject(bearer->bearerPath());
0085     Q_FOREACH (Modem *modem, m_modems.values()) {
0086         modem->removeBearer(QDBusObjectPath(bearer->bearerPath()));
0087         QVariantMap map;
0088         map.insert(QLatin1String("Bearers"), QVariant::fromValue<QList<QDBusObjectPath>>(modem->bearers()));
0089         QDBusMessage message =
0090             QDBusMessage::createSignal(modem->modemPath(), QLatin1String("org.freedesktop.DBus.Properties"), QLatin1String("PropertiesChanged"));
0091         message << QLatin1String("org.kde.fakemodem.Modem") << map << QStringList();
0092         QDBusConnection::sessionBus().send(message);
0093     }
0094 }
0095 
0096 void FakeModem::addInterfaces(const QDBusObjectPath &object_path, const ModemManager::MMVariantMapMap &interfaces_and_properties)
0097 {
0098     m_objectManager->addInterfaces(object_path, interfaces_and_properties);
0099 }
0100 
0101 void FakeModem::removeInterfaces(const QDBusObjectPath &object_path, const QStringList &interfaces)
0102 {
0103     m_objectManager->removeInterfaces(object_path, interfaces);
0104 }
0105 
0106 void FakeModem::ScanDevices()
0107 {
0108 }
0109 
0110 void FakeModem::SetLogging(const QString &level)
0111 {
0112     Q_UNUSED(level);
0113 }
0114 
0115 #include "moc_fakemodem.cpp"