[OpenDocString] kdeconnect-kde (cpp)
telephonyplugin.cpp
TelephonyPlugin::TelephonyPlugin(QObject *parent, const QVariantList &args)
    : KdeConnectPlugin(parent, args)
{
}
Constructs a telephony plugin object and assigns its id to its parent object. The args parameter is a list of arguments to the plugin and the constructor.
void TelephonyPlugin::createNotification(const NetworkPacket &np)
{
    const QString event = np.get(QStringLiteral("event"));
    const QString phoneNumber = np.get(QStringLiteral("phoneNumber"), i18n("unknown number"));
    const QString contactName = np.get(QStringLiteral("contactName"), phoneNumber);
    const QByteArray phoneThumbnail = QByteArray::fromBase64(np.get(QStringLiteral("phoneThumbnail"), ""));

    QString content, type, icon;

    if (event == QLatin1String("ringing")) {
        type = QStringLiteral("callReceived");
        icon = QStringLiteral("call-start");
        content = i18n("Incoming call from %1", contactName);
    } else if (event == QLatin1String("missedCall")) {
        type = QStringLiteral("missedCall");
        icon = QStringLiteral("call-start");
        content = i18n("Missed call from %1", contactName);
    } else if (event == QLatin1String("talking")) {
        if (m_currentCallNotification) {
            m_currentCallNotification->close();
        }
        return;
    }

    Q_EMIT callReceived(type, phoneNumber, contactName);

    qCDebug(KDECONNECT_PLUGIN_TELEPHONY) << "Creating notification with type:" << type;

    if (!m_currentCallNotification) {
        m_currentCallNotification = new KNotification(type, KNotification::Persistent);
    }

    if (!phoneThumbnail.isEmpty()) {
        QPixmap photo;
        photo.loadFromData(phoneThumbnail, "JPEG");
        m_currentCallNotification->setPixmap(photo);
    } else {
        m_currentCallNotification->setIconName(icon);
    }
    m_currentCallNotification->setComponentName(QStringLiteral("kdeconnect"));
    m_currentCallNotification->setTitle(device()->name());
    m_currentCallNotification->setText(content);

    if (event == QLatin1String("ringing")) {
        m_currentCallNotification->setActions(QStringList(i18n("Mute Call")));
        connect(m_currentCallNotification, &KNotification::action1Activated, this, &TelephonyPlugin::sendMutePacket);
    }

    m_currentCallNotification->sendEvent();
}
This creates a notification object from an existing network packet. It takes the event, phone number, contact name, and thumbnail data, and sets the title, icon and content of the current call notification.
bool TelephonyPlugin::receivePacket(const NetworkPacket &np)
{
    if (np.get(QStringLiteral("isCancel"))) {
        if (m_currentCallNotification) {
            m_currentCallNotification->close();
        }
        return true;
    }

    // ignore old style sms packet
    if (np.get(QStringLiteral("event")) == QLatin1String("sms")) {
        return false;
    }

    createNotification(np);

    return true;
}
It receives a NetworkPacket object np and creates a TelephonyPlugin::Notification object. It returns true if the packet is accepted. If the packet is cancelled, the function closes the current call notification object, and returns true. Otherwise, it creates a new notification object and returns true.
void TelephonyPlugin::sendMutePacket()
{
    NetworkPacket packet(PACKET_TYPE_TELEPHONY_REQUEST_MUTE, {{QStringLiteral("action"), QStringLiteral("mute")}});
    sendPacket(packet);
}
This sends a mute packet.
QString TelephonyPlugin::dbusPath() const
{
    return QStringLiteral("/modules/kdeconnect/devices/") + device()->id() + QStringLiteral("/telephony");
}
Returns the dbus path as a QString.