From a4208fa45c328a24b690100affd9f1735f3b98f0 Mon Sep 17 00:00:00 2001 From: Nicolas Fella Date: Sun, 24 Oct 2021 17:18:37 +0200 Subject: [PATCH] Watch for modem and messaging interface appearing When spacebar is started at boot the modem and messaging interface may not be available yet. Also the modem may re-appear at any time Watch for and react to the modem changing Needs https://invent.kde.org/frameworks/modemmanager-qt/-/merge_requests/9 Fixes https://invent.kde.org/plasma-mobile/spacebar/-/issues/25 --- lib/telephonySupport/modemcontroller.cpp | 80 +++++++++++++++--------- lib/telephonySupport/modemcontroller.h | 6 ++ 2 files changed, 56 insertions(+), 30 deletions(-) diff --git a/lib/telephonySupport/modemcontroller.cpp b/lib/telephonySupport/modemcontroller.cpp index 6639726..e9cbd1b 100644 --- a/lib/telephonySupport/modemcontroller.cpp +++ b/lib/telephonySupport/modemcontroller.cpp @@ -15,6 +15,9 @@ ModemController &ModemController::instance() ModemController::ModemController() : QObject() { + connect(ModemManager::notifier(), &ModemManager::Notifier::modemAdded, this, [this](const QString &udi) { + init(udi); + }); } std::optional> ModemController::createMessage(ModemManager::ModemMessaging::Message m) @@ -28,52 +31,69 @@ std::optional> ModemController::createMessage void ModemController::init(std::optional modemPath) { - ModemManager::ModemDevice::Ptr modem = nullptr; - if (modemPath) { - modem = ModemManager::findModemDevice(*modemPath); + m_modem = ModemManager::findModemDevice(*modemPath); } else { ModemManager::ModemDevice::List devices = ModemManager::modemDevices(); if (!devices.isEmpty()) { - modem = devices.first(); + m_modem = devices.first(); } } - if (!modem) { + if (!m_modem) { qWarning() << "Could not find modem" << modemPath.value_or(QString()); return; } - m_msgManager = modem->messagingInterface(); - - connect(m_msgManager.get(), &ModemManager::ModemMessaging::messageAdded, this, [this](const QString &uni, bool received) { - // true if the message was received from the network, as opposed to being added locally - if (!received) { - return; + connect(m_modem.get(), &ModemManager::ModemDevice::interfaceAdded, this, [this](ModemManager::ModemDevice::InterfaceType type) { + if (type == ModemManager::ModemDevice::MessagingInterface) { + initMessaging(); } + }); + + if (m_modem->hasInterface(ModemManager::ModemDevice::MessagingInterface)) { + initMessaging(); + } +} + +void ModemController::initMessaging() +{ + Q_ASSERT(m_modem); + Q_ASSERT(m_modem->hasInterface(ModemManager::ModemDevice::MessagingInterface)); + + m_msgManager = m_modem->messagingInterface(); - ModemManager::Sms::Ptr msg = m_msgManager->findMessage(uni); - Q_ASSERT(msg); + connect(m_msgManager.get(), &ModemManager::ModemMessaging::messageAdded, this, &ModemController::slotMessageAdded, Qt::UniqueConnection); +} + +void ModemController::slotMessageAdded(const QString &uni, bool received) +{ + // true if the message was received from the network, as opposed to being added locally + if (!received) { + return; + } - if (msg->state() == MMSmsState::MM_SMS_STATE_RECEIVING) { - connect(msg.get(), &ModemManager::Sms::dataChanged, this, [this, msg]() { - if (msg->state() == MMSmsState::MM_SMS_STATE_RECEIVED) { - if (!msg->data().isEmpty()) { - Q_EMIT messageAdded(msg); - } + ModemManager::Sms::Ptr msg = m_msgManager->findMessage(uni); + Q_ASSERT(msg); + + if (msg->state() == MMSmsState::MM_SMS_STATE_RECEIVING) { + connect(msg.get(), &ModemManager::Sms::dataChanged, this, [this, msg]() { + if (msg->state() == MMSmsState::MM_SMS_STATE_RECEIVED) { + if (!msg->data().isEmpty()) { + Q_EMIT messageAdded(msg); } - }); - connect(msg.get(), &ModemManager::Sms::textChanged, this, [this, msg]() { - if (msg->state() == MMSmsState::MM_SMS_STATE_RECEIVED) { - if (!msg->text().isEmpty()) { - Q_EMIT messageAdded(msg); - } + } + }); + connect(msg.get(), &ModemManager::Sms::textChanged, this, [this, msg]() { + if (msg->state() == MMSmsState::MM_SMS_STATE_RECEIVED) { + if (!msg->text().isEmpty()) { + Q_EMIT messageAdded(msg); } - }); - } else { - Q_EMIT messageAdded(msg); - } - }); + } + }); + } else { + Q_EMIT messageAdded(msg); + } } void ModemController::deleteMessage(const QString &uni) diff --git a/lib/telephonySupport/modemcontroller.h b/lib/telephonySupport/modemcontroller.h index b95bb87..0e9353a 100644 --- a/lib/telephonySupport/modemcontroller.h +++ b/lib/telephonySupport/modemcontroller.h @@ -8,6 +8,7 @@ #include +#include #include #include @@ -25,8 +26,13 @@ public: Q_SIGNALS: void messageAdded(ModemManager::Sms::Ptr message); +private Q_SLOTS: + void slotMessageAdded(const QString &uni, bool received); + private: + void initMessaging(); ModemController(); + ModemManager::ModemDevice::Ptr m_modem; ModemManager::ModemMessaging::Ptr m_msgManager; }; -- GitLab