[OpenDocString] kdeconnect-kde (cpp)
mmtelephonyplugin.cpp
QSharedPointer _voiceInterface(const QSharedPointer modemDevice)
{
    return modemDevice->interface(ModemManager::ModemDevice::VoiceInterface).objectCast();
}
Returns a shared pointer to the voice interface.
MMTelephonyPlugin::MMTelephonyPlugin(QObject *parent, const QVariantList &args)
    : KdeConnectPlugin(parent, args)
{
    connect(ModemManager::notifier(), &ModemManager::Notifier::modemAdded, this, &MMTelephonyPlugin::onModemAdded);
}
This constructor builds a KdeConnectPlugin object and connects to the modem manager notifier.
bool MMTelephonyPlugin::receivePacket(const NetworkPacket &np)
{
    if (np.get(QStringLiteral("event")) == QLatin1String("mute")) {
        // TODO: mute code
        return true;
    }

    return true;
}
This checks if the packet is mute. If it is mute, it will mute the code if it is not mute.
void MMTelephonyPlugin::onModemAdded(const QString &path)
{
    auto modemDevice = ModemManager::findModemDevice(path);
    QSharedPointer vcm = _voiceInterface(modemDevice);
    auto voice = vcm.get();
    connect(voice, &ModemManager::ModemVoice::callAdded, this, [this, voice](const QString &uni) {
        auto call = voice->findCall(uni);
        onCallAdded(call);
    });
    connect(voice, &ModemManager::ModemVoice::callDeleted, this, [this, voice](const QString &uni) {
        auto call = voice->findCall(uni);
        onCallRemoved(call);
    });
}
This code is responsible for handling the addition and deletion of a modem call.
void MMTelephonyPlugin::onModemRemoved(const QString &path)
{
    Q_UNUSED(path);
}
This removes the modem object from the internal list of modes.
void MMTelephonyPlugin::onCallAdded(ModemManager::Call::Ptr call)
{
    qCDebug(KDECONNECT_PLUGIN_MMTELEPHONY) << "Call added" << call->number();

    connect(call.get(), &ModemManager::Call::stateChanged, this, [=](MMCallState newState, MMCallState oldState, MMCallStateReason reason) {
        onCallStateChanged(call.get(), newState, oldState, reason);
    });
}
This adds a call to the list of calls and sets the stateChanged signal on the call object.
void MMTelephonyPlugin::onCallRemoved(ModemManager::Call::Ptr call)
{
    qCDebug(KDECONNECT_PLUGIN_MMTELEPHONY) << "Call removed" << call.get()->number();
}
This removes the call object from the list of calls.
QString MMTelephonyPlugin::stateName(MMCallState state)
{
    QString event;
    switch (state) {
    case MMCallState::MM_CALL_STATE_RINGING_IN:
        event = QStringLiteral("ringing");
        break;
    case MMCallState::MM_CALL_STATE_ACTIVE:
        event = QStringLiteral("talking");
        break;
    case MMCallState::MM_CALL_STATE_TERMINATED:
        event = QStringLiteral("disconnected");
        break;
    case MMCallState::MM_CALL_STATE_UNKNOWN:
    default:
        event = QStringLiteral("Unknown");
    }
    return event;
}
Returns the string representation of the given state.
void MMTelephonyPlugin::onCallStateChanged(ModemManager::Call *call, MMCallState newState, MMCallState oldState, MMCallStateReason reason)
{
    Q_UNUSED(reason);
    auto event = stateName(newState);

    qCDebug(KDECONNECT_PLUGIN_MMTELEPHONY) << "Call state changed" << call->uni() << event;
    if (newState != MMCallState::MM_CALL_STATE_TERMINATED)
        sendMMTelephonyPacket(call, event);
    else
        sendCancelMMTelephonyPacket(call, stateName(oldState));
}
This sends a MMTelephonyPacket when the new state is not the TERMINATED state, and sends a cancel MMTelephonyPacket if the new state is not the TERMINATED state. Finally, it logs the change in the debug log.
void MMTelephonyPlugin::sendMMTelephonyPacket(ModemManager::Call *call, const QString &state)
{
    QString phoneNumber = call->number();

    qCDebug(KDECONNECT_PLUGIN_MMTELEPHONY) << "Phone number is" << phoneNumber;
    NetworkPacket np{PACKET_TYPE_TELEPHONY,
                     {
                         {QStringLiteral("event"), state},
                         {QStringLiteral("phoneNumber"), phoneNumber},
                         {QStringLiteral("contactName"), phoneNumber},
                     }};
    sendPacket(np);
}
This sends a network packet to the mmtelephony service.
void MMTelephonyPlugin::sendCancelMMTelephonyPacket(ModemManager::Call *call, const QString &lastState)
{
    QString phoneNumber = call->number();

    NetworkPacket np{PACKET_TYPE_TELEPHONY,
                     {{QStringLiteral("event"), lastState},
                      {QStringLiteral("phoneNumber"), phoneNumber},
                      {QStringLiteral("contactName"), phoneNumber},
                      {QStringLiteral("isCancel"), true}}};
    sendPacket(np);
}
This sends a network packet that cancels the call on the phone number of the given call.