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,67 @@ +/** + * 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) +public: + // TYPE field values from Android + enum types + { + MESSAGE_TYPE_ALL = 0, + MESSAGE_TYPE_INBOX = 1, + MESSAGE_TYPE_SENT = 2, + MESSAGE_TYPE_DRAFT = 3, + MESSAGE_TYPE_OUTBOX = 4, + MESSAGE_TYPE_FAILED = 5, + MESSAGE_TYPE_QUEUED = 6, + }; + + /** + * 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; } + +protected: + /** + * Body of the message + */ + QString m_body; + + /** + * Remote-side address of the message. Most likely a phone number, but may be an email address + */ + QString m_address; +}; + +#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,33 @@ +/** + * 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()) + { +} + +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,46 @@ #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") + +#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 +79,30 @@ 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: private Q_SLOTS: void sendMutePacket(); void showSendSmsDialog(); -private: - KNotification* createNotification(const NetworkPacket& np); +protected: + /** + * Send to the telepathy plugin if it is available + */ + bool forwardToTelepathy(const Message& message); + /** + * Handle a packet which contains many messages, such as PACKET_TYPE_TELEPHONY_MESSAGE + */ + bool handleBatchMessages(const NetworkPacket& np); + +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 @@ -23,11 +24,11 @@ #include "sendreplydialog.h" #include -#include #include #include #include +#include "message.h" K_PLUGIN_FACTORY_WITH_JSON( KdeConnectPluginFactory, "kdeconnect_telephony.json", registerPlugin< TelephonyPlugin >(); ) @@ -39,98 +40,27 @@ { } -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; - -} - bool TelephonyPlugin::receivePacket(const NetworkPacket& np) { if (np.get(QStringLiteral("isCancel"))) { //TODO: Clear the old notification 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 && event == QLatin1String("sms")) + { + Message message(np.body()); + this->forwardToTelepathy(message); + } + + if (np.type() == PACKET_TYPE_TELEPHONY_MESSAGE) + { + return this->handleBatchMessages(np); + } return true; } @@ -163,6 +93,46 @@ dialog->raise(); } +void TelephonyPlugin::sendAllConversationsRequest() +{ + NetworkPacket np(PACKET_TYPE_TELEPHONY_REQUEST_CONVERSATIONS); + + sendPacket(np); +} + +bool 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 = ""; + 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::handleBatchMessages(const NetworkPacket& np) +{ + auto messages = np.get("messages"); + + for (QVariant body : messages) + { + Message message(body.toMap()); + this->forwardToTelepathy(message); + } + + return true; +} + QString TelephonyPlugin::dbusPath() const { return "/modules/kdeconnect/devices/" + device()->id() + "/telephony";