diff --git a/interfaces/CMakeLists.txt b/interfaces/CMakeLists.txt --- a/interfaces/CMakeLists.txt +++ b/interfaces/CMakeLists.txt @@ -45,6 +45,7 @@ geninterface(${CMAKE_SOURCE_DIR}/plugins/remotecommands/remotecommandsplugin.h remotecommandsinterface) geninterface(${CMAKE_SOURCE_DIR}/plugins/remotekeyboard/remotekeyboardplugin.h remotekeyboardinterface) geninterface(${CMAKE_SOURCE_DIR}/plugins/telephony/telephonyplugin.h telephonyinterface) +geninterface(${CMAKE_SOURCE_DIR}/plugins/telephony/message.h messageinterface) add_library(kdeconnectinterfaces SHARED ${libkdeconnect_SRC}) diff --git a/interfaces/dbusinterfaces.h b/interfaces/dbusinterfaces.h --- a/interfaces/dbusinterfaces.h +++ b/interfaces/dbusinterfaces.h @@ -36,6 +36,7 @@ #include "interfaces/remotecommandsinterface.h" #include "interfaces/remotekeyboardinterface.h" #include "interfaces/telephonyinterface.h" +#include "interfaces/messageinterface.h" /** * Using these "proxy" classes just in case we need to rename the @@ -115,6 +116,19 @@ const QString id; }; +class KDECONNECTINTERFACES_EXPORT MessageDbusInterface + : public OrgKdeKdeconnectDeviceTelephonyMessagesInterface +{ + Q_OBJECT +public: + MessageDbusInterface(const QString& deviceId, const QString& messageId, QObject* parent = nullptr); + ~MessageDbusInterface() override; + + QString messageId() { return id; } +private: + const QString id; +}; + class KDECONNECTINTERFACES_EXPORT SftpDbusInterface : public OrgKdeKdeconnectDeviceSftpInterface diff --git a/interfaces/dbusinterfaces.cpp b/interfaces/dbusinterfaces.cpp --- a/interfaces/dbusinterfaces.cpp +++ b/interfaces/dbusinterfaces.cpp @@ -100,6 +100,18 @@ } +MessageDbusInterface::MessageDbusInterface(const QString& deviceId, const QString& messageId, QObject* parent) + : OrgKdeKdeconnectDeviceTelephonyMessagesInterface(DaemonDbusInterface::activatedService(), "/modules/kdeconnect/devices/" + deviceId + "/telephony/messages/" + messageId, QDBusConnection::sessionBus(), parent) + , id (messageId) +{ + +} + +MessageDbusInterface::~MessageDbusInterface() +{ + +} + SftpDbusInterface::SftpDbusInterface(const QString& id, QObject* parent) : OrgKdeKdeconnectDeviceSftpInterface(DaemonDbusInterface::activatedService(), "/modules/kdeconnect/devices/" + id + "/sftp", QDBusConnection::sessionBus(), parent) { 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 + message.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/message.h b/plugins/telephony/message.h new file mode 100644 --- /dev/null +++ b/plugins/telephony/message.h @@ -0,0 +1,97 @@ +/** + * 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 + +class Message: public QObject { + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device.telephony.messages") + Q_PROPERTY(QString body READ getBody) + Q_PROPERTY(QString address READ getAddress) + Q_PROPERTY(qint64 date READ getDate) + Q_PROPERTY(qint32 type READ getType) + Q_PROPERTY(qint32 read READ getRead) + Q_PROPERTY(qint32 threadID READ getThreadID) + +public: + // TYPE field values from Android + enum Types + { + messageTypeAll = 0, + messageTypeInbox = 1, + messageTypeSent = 2, + messageTypeDraft = 3, + messageTypeOutbox = 4, + messageTypeFailed = 5, + messageTypeQueued = 6, + }; + Q_ENUM(Types); + + /** + * Build a new message + * + * @param args mapping of field names to values as might be contained in a network packet containing a message + */ + Message(const QVariantMap& args, QObject* parent = Q_NULLPTR); + ~Message(); + + QString getBody() const { return m_body; } + QString getAddress() const { return m_address; } + qint64 getDate() const { return m_date; } + qint32 getType() const { return m_type; } + qint32 getRead() const { return m_read; } + qint32 getThreadID() const { return m_threadID; } + +protected: + /** + * Body of the message + */ + const QString m_body; + + /** + * Remote-side address of the message. Most likely a phone number, but may be an email address + */ + const QString m_address; + + /** + * Date stamp (Unix epoch millis) associated with the message + */ + const qint64 m_date; + + /** + * Type of the message. See the message.type enum + */ + const qint32 m_type; + + /** + * Whether we have a read report for this message + */ + const qint32 m_read; + + /** + * Tag which binds individual messages into a thread + */ + const qint32 m_threadID; +}; + +#endif /* PLUGINS_TELEPHONY_MESSAGE_H_ */ diff --git a/plugins/telephony/message.cpp b/plugins/telephony/message.cpp new file mode 100644 --- /dev/null +++ b/plugins/telephony/message.cpp @@ -0,0 +1,37 @@ +/** + * 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 + +#include "message.h" + +Message::Message(const QVariantMap& args, QObject* parent) + : QObject(parent), + m_body(args["body"].toString()), + m_address(args["address"].toString()), + m_date(args["date"].toLongLong()), + m_type(args["type"].toInt()), + m_read(args["read"].toInt()), + m_threadID(args["thread_id"].toInt()) + { +} + +Message::~Message() { } 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,60 @@ #ifndef TELEPHONYPLUGIN_H #define TELEPHONYPLUGIN_H +#include "message.h" + #include #include #include #include +/** + * Packet used to indicate a batch of messages has been pushed from the remote device + * + * The body should contain the key "messages" mapping to an array of messages + * + * For example: + * { "messages" : [ + * { "event" : "sms", + * "messageBody" : "Hello", + * "phoneNumber" : "2021234567", + * "messageDate" : "1518846484880", + * "messageType" : "2", + * "threadID" : "132" + * }, + * { ... }, + * ... + * ] + * } + */ +#define PACKET_TYPE_TELEPHONY_MESSAGE QStringLiteral("kdeconnect.telephony.message") + +/** + * Packet used for simple telephony events + * + * It contains the key "event" which maps to a string indicating the type of event: + * - "ringing" - A phone call is incoming + * - "missedCall" - An incoming call was not answered + * - "sms" - An incoming SMS message + * - Note: As of this writing (15 May 2018) the SMS interface is being improved and this type of event + * is no longer the preferred way of retrieving SMS. Use PACKET_TYPE_TELEPHONY_MESSAGE instead. + * + * Depending on the event, other fields may be defined + */ +#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 @@ -49,11 +94,38 @@ 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 requestAllConversations(); + +public: +Q_SIGNALS: + private Q_SLOTS: void sendMutePacket(); void showSendSmsDialog(); +protected: + /** + * Send to the telepathy plugin if it is available + */ + void forwardToTelepathy(const Message& message); + + /** + * Handle a packet which contains many messages, such as PACKET_TYPE_TELEPHONY_MESSAGE + */ + bool handleBatchMessages(const NetworkPacket& np); + private: + /** + * Create a notification for: + * - Incoming call (with the option to mute the ringing) + * - Missed call + * - Incoming SMS (with the option to reply) + * - This comment is being written while SMS handling is in the process of being improved. + * As such, any code in createNotification which deals with SMS is legacy support + */ KNotification* createNotification(const NetworkPacket& np); 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 @@ -23,11 +24,12 @@ #include "sendreplydialog.h" #include -#include #include #include #include +#include +#include "message.h" K_PLUGIN_FACTORY_WITH_JSON( KdeConnectPluginFactory, "kdeconnect_telephony.json", registerPlugin< TelephonyPlugin >(); ) @@ -45,18 +47,14 @@ 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"), "")); + const QString messageBody = np.get(QStringLiteral("messageBody"),QLatin1String("")); // 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"; - } + // Telepathy has already been tried (in receivePacket) + // There is a chance that it somehow failed, but since nobody uses Telepathy anyway... + // TODO: When upgrading telepathy, handle failure case (in case m_telepathyInterface.call returns false) + return nullptr; } QString content, type, icon; @@ -109,7 +107,6 @@ 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); @@ -129,8 +126,27 @@ return true; } - KNotification* n = createNotification(np); - if (n != nullptr) n->sendEvent(); + const QString& event = np.get(QStringLiteral("event"), QStringLiteral("unknown")); + + // Handle old-style packets + if (np.type() == PACKET_TYPE_TELEPHONY) + { + if (event == QLatin1String("sms")) + { + // New-style packets should be a PACKET_TYPE_TELEPHONY_MESSAGE (15 May 2018) + qCDebug(KDECONNECT_PLUGIN_TELEPHONY) << "Handled an old-style Telephony sms packet. You should update your Android app to get the latest features!"; + Message message(np.body()); + forwardToTelepathy(message); + } + KNotification* n = createNotification(np); + if (n != nullptr) n->sendEvent(); + return true; + } + + if (np.type() == PACKET_TYPE_TELEPHONY_MESSAGE) + { + return handleBatchMessages(np); + } return true; } @@ -163,6 +179,39 @@ dialog->raise(); } +void TelephonyPlugin::requestAllConversations() +{ + NetworkPacket np(PACKET_TYPE_TELEPHONY_REQUEST_CONVERSATIONS); + + sendPacket(np); +} + +void TelephonyPlugin::forwardToTelepathy(const Message& 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; // TODO: When telepathy support is improved, look up the contact with KPeople + const QString phoneNumber = message.getAddress(); + m_telepathyInterface.call(QDBus::NoBlock, QStringLiteral("sendMessage"), phoneNumber, contactName, messageBody); + } +} + +bool TelephonyPlugin::handleBatchMessages(const NetworkPacket& np) +{ + const auto messages = np.get("messages"); + + for (const QVariant& body : messages) + { + Message message(body.toMap()); + forwardToTelepathy(message); + } + + return true; +} + QString TelephonyPlugin::dbusPath() const { return "/modules/kdeconnect/devices/" + device()->id() + "/telephony";