diff --git a/plugins/telephony/CMakeLists.txt b/plugins/telephony/CMakeLists.txt --- a/plugins/telephony/CMakeLists.txt +++ b/plugins/telephony/CMakeLists.txt @@ -1,8 +1,13 @@ +set(kdeconnect_telephony_SRCS + telephonyplugin.cpp + telephonyMessage.cpp +) + include_directories(${CMAKE_BINARY_DIR}) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../notifications/) # needed for the sendreplydialog ki18n_wrap_ui(kdeconnect_telephony_SRCS ../notifications/sendreplydialog.ui) -kdeconnect_add_plugin(kdeconnect_telephony JSON kdeconnect_telephony.json SOURCES telephonyplugin.cpp ../notifications/sendreplydialog.cpp ${kdeconnect_telephony_SRCS}) +kdeconnect_add_plugin(kdeconnect_telephony JSON kdeconnect_telephony.json SOURCES ../notifications/sendreplydialog.cpp ${kdeconnect_telephony_SRCS}) target_link_libraries(kdeconnect_telephony kdeconnectcore diff --git a/plugins/telephony/kdeconnect_telephony.json b/plugins/telephony/kdeconnect_telephony.json --- a/plugins/telephony/kdeconnect_telephony.json +++ b/plugins/telephony/kdeconnect_telephony.json @@ -89,9 +89,11 @@ }, "X-KdeConnect-OutgoingPacketType": [ "kdeconnect.telephony.request", - "kdeconnect.sms.request" + "kdeconnect.sms.request", + "kdeconnect.telephony.request_conversations" ], "X-KdeConnect-SupportedPacketType": [ - "kdeconnect.telephony" + "kdeconnect.telephony", + "kdeconnect.telephony.message" ] } diff --git a/plugins/telephony/telephonyMessage.h b/plugins/telephony/telephonyMessage.h new file mode 100644 --- /dev/null +++ b/plugins/telephony/telephonyMessage.h @@ -0,0 +1,41 @@ +/** + * Copyright 2018 Simon Redman + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef PLUGINS_TELEPHONY_MESSAGE_H_ +#define PLUGINS_TELEPHONY_MESSAGE_H_ + +#include +#include + +class telephonyMessage: public QMap { +public: + // Field names as copied from Android's Telephony.Sms class + static const QString ADDRESS; + static const QString BODY; + + static void registerDBus(); + + QString getBody() const; + QString getAddress() const; +}; + +Q_DECLARE_METATYPE(telephonyMessage) + +#endif /* PLUGINS_TELEPHONY_MESSAGE_H_ */ diff --git a/plugins/telephony/telephonyMessage.cpp b/plugins/telephony/telephonyMessage.cpp new file mode 100644 --- /dev/null +++ b/plugins/telephony/telephonyMessage.cpp @@ -0,0 +1,43 @@ +/** + * Copyright 2018 Simon Redman + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "telephonyMessage.h" + +const QString telephonyMessage::ADDRESS = "address"; +const QString telephonyMessage::BODY = "body"; + +void telephonyMessage::registerDBus() +{ + // Register custom types with dbus + qRegisterMetaType("telephonyMessage"); + qDBusRegisterMetaType(); +} + +QString telephonyMessage::getBody() const +{ + return this->operator [](BODY); +} + +QString telephonyMessage::getAddress() const +{ + return this->operator [](ADDRESS); +} diff --git a/plugins/telephony/telephonyplugin.h b/plugins/telephony/telephonyplugin.h --- a/plugins/telephony/telephonyplugin.h +++ b/plugins/telephony/telephonyplugin.h @@ -1,5 +1,6 @@ /** * Copyright 2013 Albert Vaca + * Copyright 2018 Simon Redman * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -21,16 +22,41 @@ #ifndef TELEPHONYPLUGIN_H #define TELEPHONYPLUGIN_H +#include "telephonyMessage.h" + #include #include -#include - #include +/** + * Packet used to indicate a message has been pushed from the remote device + * + * The body should contain a mapping of all fields of the message to their values + * + * For example: + * { "event" : "sms", + * "messageBody" : "Hello", + * "phoneNumber" : "2021234567", + * "messageDate" : "20150321434", + * "messageType" : "-1", + * "threadID" : "132" + * } + */ +#define PACKET_TYPE_TELEPHONY_MESSAGE QStringLiteral("kdeconnect.telephony.message") + +#define PACKET_TYPE_TELEPHONY QStringLiteral("kdeconnect.telephony") + #define PACKET_TYPE_TELEPHONY_REQUEST QStringLiteral("kdeconnect.telephony.request") #define PACKET_TYPE_SMS_REQUEST QStringLiteral("kdeconnect.sms.request") +/** + * Packet sent to request all the most-recent messages in all conversations on the device + * + * The request packet shall contain no body + */ +#define PACKET_TYPE_TELEPHONY_REQUEST_CONVERSATIONS QStringLiteral("kdeconnect.telephony.request_conversations") + Q_DECLARE_LOGGING_CATEGORY(KDECONNECT_PLUGIN_TELEPHONY) class TelephonyPlugin @@ -48,14 +74,36 @@ public Q_SLOTS: Q_SCRIPTABLE void sendSms(const QString& phoneNumber, const QString& messageBody); + /** + * Send a request to the remote for all of its conversations + */ + Q_SCRIPTABLE void sendAllConversationsRequest(); + +public: +Q_SIGNALS: + /** + * Emitted to indicated a message has arrived for handling + */ + Q_SCRIPTABLE void incomingMessage(const telephonyMessage& message); private Q_SLOTS: void sendMutePacket(); void showSendSmsDialog(); -private: - KNotification* createNotification(const NetworkPacket& np); +protected: + telephonyMessage convertPacketToMessage(const NetworkPacket& np); + + /** + * Send to the telepathy plugin if it is available + */ + bool forwardToTelepathy(const telephonyMessage& message); + /** + * Report that a message has arrived + */ + bool emitIncomingMessage(const telephonyMessage& message); + +private: QDBusInterface m_telepathyInterface; }; diff --git a/plugins/telephony/telephonyplugin.cpp b/plugins/telephony/telephonyplugin.cpp --- a/plugins/telephony/telephonyplugin.cpp +++ b/plugins/telephony/telephonyplugin.cpp @@ -1,5 +1,6 @@ /** * Copyright 2013 Albert Vaca + * Copyright 2018 Simon Redman * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -20,10 +21,10 @@ #include "telephonyplugin.h" +#include "telephonyMessage.h" #include "sendreplydialog.h" #include -#include #include #include @@ -37,88 +38,7 @@ : KdeConnectPlugin(parent, args) , m_telepathyInterface(QStringLiteral("org.freedesktop.Telepathy.ConnectionManager.kdeconnect"), QStringLiteral("/kdeconnect")) { -} - -KNotification* 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"), "")); - - // In case telepathy can handle the message, don't do anything else - if (event == QLatin1String("sms") && m_telepathyInterface.isValid()) { - qCDebug(KDECONNECT_PLUGIN_TELEPHONY) << "Passing a text message to the telepathy interface"; - connect(&m_telepathyInterface, SIGNAL(messageReceived(QString,QString)), SLOT(sendSms(QString,QString)), Qt::UniqueConnection); - const QString messageBody = np.get(QStringLiteral("messageBody"),QLatin1String("")); - QDBusReply reply = m_telepathyInterface.call(QStringLiteral("sendMessage"), phoneNumber, contactName, messageBody); - if (reply) { - return nullptr; - } else { - qCDebug(KDECONNECT_PLUGIN_TELEPHONY) << "Telepathy failed, falling back to the default handling"; - } - } - - QString content, type, icon; - KNotification::NotificationFlags flags = KNotification::CloseOnTimeout; - - const QString title = device()->name(); - - 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); - flags |= KNotification::Persistent; //Note that in Unity this generates a message box! - } else if (event == QLatin1String("sms")) { - type = QStringLiteral("smsReceived"); - icon = QStringLiteral("mail-receive"); - QString messageBody = np.get(QStringLiteral("messageBody"), QLatin1String("")); - content = i18n("SMS from %1
%2", contactName, messageBody); - flags |= KNotification::Persistent; //Note that in Unity this generates a message box! - } else if (event == QLatin1String("talking")) { - return nullptr; - } else { -#ifndef NDEBUG - return nullptr; -#else - type = QStringLiteral("callReceived"); - icon = QStringLiteral("phone"); - content = i18n("Unknown telephony event: %1", event); -#endif - } - - qCDebug(KDECONNECT_PLUGIN_TELEPHONY) << "Creating notification with type:" << type; - - KNotification* notification = new KNotification(type, flags, this); - if (!phoneThumbnail.isEmpty()) { - QPixmap photo; - photo.loadFromData(phoneThumbnail, "JPEG"); - notification->setPixmap(photo); - } else { - notification->setIconName(icon); - } - notification->setComponentName(QStringLiteral("kdeconnect")); - notification->setTitle(title); - notification->setText(content); - - if (event == QLatin1String("ringing")) { - notification->setActions( QStringList(i18n("Mute Call")) ); - connect(notification, &KNotification::action1Activated, this, &TelephonyPlugin::sendMutePacket); - } else if (event == QLatin1String("sms")) { - const QString messageBody = np.get(QStringLiteral("messageBody"),QLatin1String("")); - notification->setActions( QStringList(i18n("Reply")) ); - notification->setProperty("phoneNumber", phoneNumber); - notification->setProperty("contactName", contactName); - notification->setProperty("originalMessage", messageBody); - connect(notification, &KNotification::action1Activated, this, &TelephonyPlugin::showSendSmsDialog); - } - - return notification; - + telephonyMessage::registerDBus(); } bool TelephonyPlugin::receivePacket(const NetworkPacket& np) @@ -129,8 +49,14 @@ return true; } - KNotification* n = createNotification(np); - if (n != nullptr) n->sendEvent(); + const QString& event = np.get(QStringLiteral("event"), QStringLiteral("unknown")); + + if (np.type() == PACKET_TYPE_TELEPHONY_MESSAGE || event == QLatin1String("sms")) + { + const telephonyMessage& message = convertPacketToMessage(np); + this->forwardToTelepathy(message); + this->emitIncomingMessage(message); + } return true; } @@ -163,6 +89,53 @@ dialog->raise(); } +void TelephonyPlugin::sendAllConversationsRequest() +{ + NetworkPacket np(PACKET_TYPE_TELEPHONY_REQUEST_CONVERSATIONS); + + sendPacket(np); +} + +telephonyMessage TelephonyPlugin::convertPacketToMessage(const NetworkPacket& np) +{ + telephonyMessage message; + + for (const QString& key : np.body().keys()) + { + const QString& field = np.body()[key].toString(); + message[key] = field; + } + + return message; +} + +bool TelephonyPlugin::forwardToTelepathy(const telephonyMessage& message) +{ + // In case telepathy can handle the message, don't do anything else + if (m_telepathyInterface.isValid()) { + qCDebug(KDECONNECT_PLUGIN_TELEPHONY) << "Passing a text message to the telepathy interface"; + connect(&m_telepathyInterface, SIGNAL(messageReceived(QString,QString)), SLOT(sendSms(QString,QString)), Qt::UniqueConnection); + const QString messageBody = message.getBody(); + const QString contactName = ""; + const QString phoneNumber = message.getAddress(); + QDBusReply reply = m_telepathyInterface.call(QStringLiteral("sendMessage"), phoneNumber, contactName, messageBody); + if (reply) { + return true; + } else { + return false; + } + } + + return false; +} + +bool TelephonyPlugin::emitIncomingMessage(const telephonyMessage& message) +{ + Q_EMIT incomingMessage(message); + + return true; +} + QString TelephonyPlugin::dbusPath() const { return "/modules/kdeconnect/devices/" + device()->id() + "/telephony";