diff --git a/interfaces/conversationmessage.cpp b/interfaces/conversationmessage.cpp index 8fada475..08510675 100644 --- a/interfaces/conversationmessage.cpp +++ b/interfaces/conversationmessage.cpp @@ -1,131 +1,146 @@ /** * 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 "conversationmessage.h" #include #include ConversationMessage::ConversationMessage(const QVariantMap& args, QObject* parent) : QObject(parent), + m_eventField(args["event"].toInt()), 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()), m_uID(args["_id"].toInt()) { } -ConversationMessage::ConversationMessage (const QString& body, const QString& address, const qint64& date, - const qint32& type, const qint32& read, const qint32& threadID, +ConversationMessage::ConversationMessage (const qint32& eventField, const QString& body, + const QString& address, const qint64& date, + const qint32& type, const qint32& read, + const qint32& threadID, const qint32& uID, QObject* parent) : QObject(parent) + , m_eventField(eventField) , m_body(body) , m_address(address) , m_date(date) , m_type(type) , m_read(read) , m_threadID(threadID) , m_uID(uID) { } ConversationMessage::ConversationMessage(const ConversationMessage& other, QObject* parent) : QObject(parent) + , m_eventField(other.m_eventField) , m_body(other.m_body) , m_address(other.m_address) , m_date(other.m_date) , m_type(other.m_type) , m_read(other.m_read) , m_threadID(other.m_threadID) , m_uID(other.m_uID) { } ConversationMessage::~ConversationMessage() { } ConversationMessage& ConversationMessage::operator=(const ConversationMessage& other) { + this->m_eventField = other.m_eventField; this->m_body = other.m_body; this->m_address = other.m_address; this->m_date = other.m_date; this->m_type = other.m_type; this->m_read = other.m_read; this->m_threadID = other.m_threadID; this->m_uID = other.m_uID; return *this; } QVariantMap ConversationMessage::toVariant() const { return { + {"event", m_eventField}, {"body", m_body}, {"address", m_address}, {"date", m_date}, {"type", m_type}, {"read", m_read}, {"thread_id", m_threadID}, {"_id", m_uID}, }; } QDBusArgument &operator<<(QDBusArgument &argument, const ConversationMessage &message) { argument.beginStructure(); - argument << message.body() << message.address() << message.date() << message.type() - << message.read() << message.threadID() << message.uID(); + argument << message.eventField() + << message.body() + << message.address() + << message.date() + << message.type() + << message.read() + << message.threadID() + << message.uID(); argument.endStructure(); return argument; } const QDBusArgument &operator>>(const QDBusArgument &argument, ConversationMessage &message) { + qint32 event; QString body; QString address; qint64 date; qint32 type; qint32 read; qint32 threadID; qint32 uID; argument.beginStructure(); + argument >> event; argument >> body; argument >> address; argument >> date; argument >> type; argument >> read; argument >> threadID; argument >> uID; argument.endStructure(); - message = ConversationMessage(body, address, date, type, read, threadID, uID); + message = ConversationMessage(event, body, address, date, type, read, threadID, uID); return argument; } void ConversationMessage::registerDbusType() { qDBusRegisterMetaType(); qRegisterMetaType(); } diff --git a/interfaces/conversationmessage.h b/interfaces/conversationmessage.h index aad56608..bec1442e 100644 --- a/interfaces/conversationmessage.h +++ b/interfaces/conversationmessage.h @@ -1,122 +1,142 @@ /** * 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_CONVERSATIONMESSAGE_H_ #define PLUGINS_TELEPHONY_CONVERSATIONMESSAGE_H_ #include #include #include #include #include "interfaces/kdeconnectinterfaces_export.h" class KDECONNECTINTERFACES_EXPORT ConversationMessage : public QObject { Q_OBJECT Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device.telephony.messages") + Q_PROPERTY(qint32 eventField READ eventField) Q_PROPERTY(QString body READ body) Q_PROPERTY(QString address READ address) Q_PROPERTY(qint64 date READ date) Q_PROPERTY(qint32 type READ type) Q_PROPERTY(qint32 read READ read) Q_PROPERTY(qint32 threadID READ threadID) Q_PROPERTY(qint32 uID READ uID) 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); + /** + * Values describing the possible type of events contained in a message + * A message's eventField is constructed as a bitwise-OR of events + * Any events which are unsupported should be ignored + */ + enum Events { + EventTextMessage = 0x1, // This message has a body field which contains pure, human-readable text + }; + Q_ENUM(Events) + /** * Build a new message from a keyword argument dictionary * * @param args mapping of field names to values as might be contained in a network packet containing a message */ ConversationMessage(const QVariantMap& args = QVariantMap(), QObject* parent = Q_NULLPTR); - ConversationMessage(const QString& body, const QString& address, const qint64& date, - const qint32& type, const qint32& read, const qint32& threadID, - const qint32& uID, + ConversationMessage(const qint32& eventField, const QString& body, const QString& address, + const qint64& date, const qint32& type, const qint32& read, + const qint32& threadID, const qint32& uID, QObject* parent = Q_NULLPTR); ConversationMessage(const ConversationMessage& other, QObject* parent = Q_NULLPTR); ~ConversationMessage(); ConversationMessage& operator=(const ConversationMessage& other); static void registerDbusType(); + qint32 eventField() const { return m_eventField; } QString body() const { return m_body; } QString address() const { return m_address; } qint64 date() const { return m_date; } qint32 type() const { return m_type; } qint32 read() const { return m_read; } qint32 threadID() const { return m_threadID; } qint32 uID() const { return m_uID; } QVariantMap toVariant() const; + bool containsTextBody() const { return (eventField() & ConversationMessage::EventTextMessage); } + protected: + /** + * Bitwise OR of event flags + * Unsupported flags shall cause the message to be ignored + */ + qint32 m_eventField; + /** * 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; /** * Date stamp (Unix epoch millis) associated with the message */ qint64 m_date; /** * Type of the message. See the message.type enum */ qint32 m_type; /** * Whether we have a read report for this message */ qint32 m_read; /** * Tag which binds individual messages into a thread */ qint32 m_threadID; /** * Value which uniquely identifies a message */ qint32 m_uID; }; Q_DECLARE_METATYPE(ConversationMessage); #endif /* PLUGINS_TELEPHONY_CONVERSATIONMESSAGE_H_ */ diff --git a/plugins/sms/smsplugin.cpp b/plugins/sms/smsplugin.cpp index 351d4e9f..c4025304 100644 --- a/plugins/sms/smsplugin.cpp +++ b/plugins/sms/smsplugin.cpp @@ -1,119 +1,121 @@ /** * 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 * 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 "smsplugin.h" #include #include #include #include #include #include #include #include "sendreplydialog.h" K_PLUGIN_FACTORY_WITH_JSON( KdeConnectPluginFactory, "kdeconnect_sms.json", registerPlugin< SmsPlugin >(); ) Q_LOGGING_CATEGORY(KDECONNECT_PLUGIN_SMS, "kdeconnect.plugin.sms") SmsPlugin::SmsPlugin(QObject* parent, const QVariantList& args) : KdeConnectPlugin(parent, args) , m_telepathyInterface(QStringLiteral("org.freedesktop.Telepathy.ConnectionManager.kdeconnect"), QStringLiteral("/kdeconnect")) , m_conversationInterface(new ConversationsDbusInterface(this)) { } SmsPlugin::~SmsPlugin() { } bool SmsPlugin::receivePacket(const NetworkPacket& np) { if (np.type() == PACKET_TYPE_SMS_MESSAGES) { return handleBatchMessages(np); } return true; } void SmsPlugin::sendSms(const QString& phoneNumber, const QString& messageBody) { NetworkPacket np(PACKET_TYPE_SMS_REQUEST, { {"sendSms", true}, {"phoneNumber", phoneNumber}, {"messageBody", messageBody} }); qCDebug(KDECONNECT_PLUGIN_SMS) << "Dispatching SMS send request to remote"; sendPacket(np); } void SmsPlugin::requestAllConversations() { NetworkPacket np(PACKET_TYPE_SMS_REQUEST_CONVERSATIONS); sendPacket(np); } void SmsPlugin::requestConversation (const QString& conversationID) const { NetworkPacket np(PACKET_TYPE_SMS_REQUEST_CONVERSATION); np.set("threadID", conversationID.toInt()); sendPacket(np); } void SmsPlugin::forwardToTelepathy(const ConversationMessage& message) { // If we don't have a valid Telepathy interface, bail out if (!(m_telepathyInterface.isValid())) return; qCDebug(KDECONNECT_PLUGIN_SMS) << "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.body(); const QString contactName; // TODO: When telepathy support is improved, look up the contact with KPeople const QString phoneNumber = message.address(); m_telepathyInterface.call(QDBus::NoBlock, QStringLiteral("sendMessage"), phoneNumber, contactName, messageBody); } bool SmsPlugin::handleBatchMessages(const NetworkPacket& np) { const auto messages = np.get("messages"); for (const QVariant& body : messages) { ConversationMessage message(body.toMap()); - forwardToTelepathy(message); - m_conversationInterface->addMessage(message); + if (message.containsTextBody()) { + forwardToTelepathy(message); + m_conversationInterface->addMessage(message); + } } return true; } QString SmsPlugin::dbusPath() const { return "/modules/kdeconnect/devices/" + device()->id() + "/sms"; } #include "smsplugin.moc" diff --git a/plugins/sms/smsplugin.h b/plugins/sms/smsplugin.h index 276b5feb..83558d0a 100644 --- a/plugins/sms/smsplugin.h +++ b/plugins/sms/smsplugin.h @@ -1,132 +1,135 @@ /** * 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 * 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 SMSPLUGIN_H #define SMSPLUGIN_H #include #include #include "conversationsdbusinterface.h" #include "interfaces/conversationmessage.h" #include "sendreplydialog.h" /** * 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" - * }, - * { ... }, - * ... - * ] - * } + * { "event" : 1, // 32-bit field containing a bitwise-or of event flags + * // See constants declared in SMSHelper.Message for defined + * // values and explanations + * "body" : "Hello", // Text message body + * "address" : "2021234567", // Sending or receiving address of the message + * "date" : "1518846484880", // Timestamp of the message + * "type" : "2", // Compare with Android's + * // Telephony.TextBasedSmsColumns.MESSAGE_TYPE_* + * "thread_id" : "132" // Thread to which the message belongs + * "read" : true // Boolean representing whether a message is read or unread + * }, + * { ... }, + * ... + * ] */ #define PACKET_TYPE_SMS_MESSAGES QStringLiteral("kdeconnect.sms.messages") /** * Packet sent to request a message be sent * * This will almost certainly need to be replaced or augmented to support MMS, * but be sure the Android side remains compatible with old desktop apps! * * The body should look like so: * { "sendSms": true, * "phoneNumber": "542904563213", * "messageBody": "Hi mom!" * } */ #define PACKET_TYPE_SMS_REQUEST QStringLiteral("kdeconnect.sms.request") /** * Packet sent to request the most-recent message in each conversations on the device * * The request packet shall contain no body */ #define PACKET_TYPE_SMS_REQUEST_CONVERSATIONS QStringLiteral("kdeconnect.sms.request_conversations") /** * Packet sent to request all the messages in a particular conversation * * The body should contain the key "threadID" mapping to the threadID being requested * For example: * { "threadID": 203 } */ #define PACKET_TYPE_SMS_REQUEST_CONVERSATION QStringLiteral("kdeconnect.sms.request_conversation") Q_DECLARE_LOGGING_CATEGORY(KDECONNECT_PLUGIN_SMS) class Q_DECL_EXPORT SmsPlugin : public KdeConnectPlugin { Q_OBJECT Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device.sms") public: explicit SmsPlugin(QObject* parent, const QVariantList& args); ~SmsPlugin() override; bool receivePacket(const NetworkPacket& np) override; void connected() override {} QString dbusPath() const override; 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(); /** * Send a request to the remote for a particular conversation */ Q_SCRIPTABLE void requestConversation(const QString& conversationID) const; private: /** * Send to the telepathy plugin if it is available */ void forwardToTelepathy(const ConversationMessage& message); /** * Handle a packet which contains many messages, such as PACKET_TYPE_TELEPHONY_MESSAGE */ bool handleBatchMessages(const NetworkPacket& np); QDBusInterface m_telepathyInterface; ConversationsDbusInterface* m_conversationInterface; }; #endif