File indexing completed on 2024-04-28 08:49:13

0001 /**
0002  * SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
0003  * SPDX-FileCopyrightText: 2018 Simon Redman <simon@ergotech.com>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006  */
0007 
0008 #include "telephonyplugin.h"
0009 
0010 #include <KLocalizedString>
0011 #include <QDBusReply>
0012 #include <QDebug>
0013 
0014 #include "plugin_telephony_debug.h"
0015 #include <KNotification>
0016 #include <KPluginFactory>
0017 
0018 K_PLUGIN_CLASS_WITH_JSON(TelephonyPlugin, "kdeconnect_telephony.json")
0019 
0020 void TelephonyPlugin::createNotification(const NetworkPacket &np)
0021 {
0022     const QString event = np.get<QString>(QStringLiteral("event"));
0023     const QString phoneNumber = np.get<QString>(QStringLiteral("phoneNumber"), i18n("unknown number"));
0024     const QString contactName = np.get<QString>(QStringLiteral("contactName"), phoneNumber);
0025     const QByteArray phoneThumbnail = QByteArray::fromBase64(np.get<QByteArray>(QStringLiteral("phoneThumbnail"), ""));
0026 
0027     QString content, type, icon;
0028 
0029     if (event == QLatin1String("ringing")) {
0030         type = QStringLiteral("callReceived");
0031         icon = QStringLiteral("call-start");
0032         content = i18n("Incoming call from %1", contactName);
0033     } else if (event == QLatin1String("missedCall")) {
0034         type = QStringLiteral("missedCall");
0035         icon = QStringLiteral("call-start");
0036         content = i18n("Missed call from %1", contactName);
0037     } else if (event == QLatin1String("talking")) {
0038         if (m_currentCallNotification) {
0039             m_currentCallNotification->close();
0040         }
0041         return;
0042     }
0043 
0044     Q_EMIT callReceived(type, phoneNumber, contactName);
0045 
0046     qCDebug(KDECONNECT_PLUGIN_TELEPHONY) << "Creating notification with type:" << type;
0047 
0048     if (!m_currentCallNotification) {
0049         m_currentCallNotification = new KNotification(type, KNotification::Persistent);
0050     }
0051 
0052     if (!phoneThumbnail.isEmpty()) {
0053         QPixmap photo;
0054         photo.loadFromData(phoneThumbnail, "JPEG");
0055         m_currentCallNotification->setPixmap(photo);
0056     } else {
0057         m_currentCallNotification->setIconName(icon);
0058     }
0059     m_currentCallNotification->setComponentName(QStringLiteral("kdeconnect"));
0060     m_currentCallNotification->setTitle(device()->name());
0061     m_currentCallNotification->setText(content);
0062 
0063     if (event == QLatin1String("ringing")) {
0064 #if QT_VERSION_MAJOR == 6
0065         m_currentCallNotification->clearActions();
0066         KNotificationAction *muteAction = m_currentCallNotification->addAction(i18n("Mute Call"));
0067         connect(muteAction, &KNotificationAction::activated, this, &TelephonyPlugin::sendMutePacket);
0068 #else
0069         m_currentCallNotification->setActions(QStringList(i18n("Mute Call")));
0070         connect(m_currentCallNotification, &KNotification::action1Activated, this, &TelephonyPlugin::sendMutePacket);
0071 #endif
0072     }
0073 
0074     m_currentCallNotification->sendEvent();
0075 }
0076 
0077 void TelephonyPlugin::receivePacket(const NetworkPacket &np)
0078 {
0079     if (np.get<bool>(QStringLiteral("isCancel"))) {
0080         if (m_currentCallNotification) {
0081             m_currentCallNotification->close();
0082         }
0083         return;
0084     }
0085 
0086     // ignore old style sms packet
0087     if (np.get<QString>(QStringLiteral("event")) != QLatin1String("sms")) {
0088         createNotification(np);
0089     }
0090 }
0091 
0092 void TelephonyPlugin::sendMutePacket()
0093 {
0094     NetworkPacket packet(PACKET_TYPE_TELEPHONY_REQUEST_MUTE, {{QStringLiteral("action"), QStringLiteral("mute")}});
0095     sendPacket(packet);
0096 }
0097 
0098 QString TelephonyPlugin::dbusPath() const
0099 {
0100     return QLatin1String("/modules/kdeconnect/devices/%1/telepony").arg(device()->id());
0101 }
0102 
0103 #include "moc_telephonyplugin.cpp"
0104 #include "telephonyplugin.moc"