[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kdeconnect-kde/sms-history] plugins/telephony: Add custom Message type
From:       Simon Redman <null () kde ! org>
Date:       2018-04-01 3:20:58
Message-ID: E1f2TYA-0005kJ-1L () code ! kde ! org
[Download RAW message or body]

Git commit 321a6a99c03959b71999d3cbaab7f9067a3c0c68 by Simon Redman.
Committed on 01/04/2018 at 03:20.
Pushed by sredman into branch 'sms-history'.

Add custom Message type

M  +6    -1    plugins/telephony/CMakeLists.txt
A  +43   -0    plugins/telephony/telephonyMessage.cpp     [License: GPL (v2/3)]
A  +41   -0    plugins/telephony/telephonyMessage.h     [License: GPL (v2/3)]
M  +21   -4    plugins/telephony/telephonyplugin.cpp
M  +6    -1    plugins/telephony/telephonyplugin.h

https://commits.kde.org/kdeconnect-kde/321a6a99c03959b71999d3cbaab7f9067a3c0c68

diff --git a/plugins/telephony/CMakeLists.txt b/plugins/telephony/CMakeLists.txt
index 933c4591..2e348e18 100644
--- 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/telephonyMessage.cpp \
b/plugins/telephony/telephonyMessage.cpp new file mode 100644
index 00000000..51d52f38
--- /dev/null
+++ b/plugins/telephony/telephonyMessage.cpp
@@ -0,0 +1,43 @@
+/**
+ * Copyright 2018 Simon Redman <simon@ergotech.com>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <QtDBus>
+
+#include "telephonyMessage.h"
+
+const QString telephonyMessage::ADDRESS = "address";
+const QString telephonyMessage::BODY = "body";
+
+void telephonyMessage::registerDBus()
+{
+    // Register custom types with dbus
+    qRegisterMetaType<telephonyMessage>("telephonyMessage");
+    qDBusRegisterMetaType<telephonyMessage>();
+}
+
+QString telephonyMessage::getBody() const
+{
+    return this->operator [](BODY);
+}
+
+QString telephonyMessage::getAddress() const
+{
+    return this->operator [](ADDRESS);
+}
diff --git a/plugins/telephony/telephonyMessage.h \
b/plugins/telephony/telephonyMessage.h new file mode 100644
index 00000000..41406a23
--- /dev/null
+++ b/plugins/telephony/telephonyMessage.h
@@ -0,0 +1,41 @@
+/**
+ * Copyright 2018 Simon Redman <simon@ergotech.com>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef PLUGINS_TELEPHONY_MESSAGE_H_
+#define PLUGINS_TELEPHONY_MESSAGE_H_
+
+#include <QtDBus>
+#include <QMap>
+
+class telephonyMessage: public QMap<QString, QString> {
+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/telephonyplugin.cpp \
b/plugins/telephony/telephonyplugin.cpp index d128ab69..4d3b91b0 100644
--- a/plugins/telephony/telephonyplugin.cpp
+++ b/plugins/telephony/telephonyplugin.cpp
@@ -1,5 +1,6 @@
 /**
  * Copyright 2013 Albert Vaca <albertvaka@gmail.com>
+ * Copyright 2018 Simon Redman <simon@ergotech.com>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -20,6 +21,7 @@
 
 #include "telephonyplugin.h"
 
+#include "telephonyMessage.h"
 #include "sendreplydialog.h"
 
 #include <KLocalizedString>
@@ -36,6 +38,7 @@ TelephonyPlugin::TelephonyPlugin(QObject* parent, const \
QVariantList& args)  : KdeConnectPlugin(parent, args)
     , m_telepathyInterface(QStringLiteral("org.freedesktop.Telepathy.ConnectionManager.kdeconnect"), \
QStringLiteral("/kdeconnect"))  {
+    telephonyMessage::registerDBus();
 }
 
 bool TelephonyPlugin::receivePacket(const NetworkPacket& np)
@@ -50,7 +53,8 @@ bool TelephonyPlugin::receivePacket(const NetworkPacket& np)
 
     if (np.type() == PACKET_TYPE_TELEPHONY_MESSAGE || event == QLatin1String("sms"))
     {
-        this->forwardToTelepathy(np);
+        const telephonyMessage& message = convertPacketToMessage(np);
+        this->forwardToTelepathy(message);
     }
 
     return true;
@@ -91,15 +95,28 @@ void TelephonyPlugin::sendAllConversationsRequest()
     sendPacket(np);
 }
 
-bool TelephonyPlugin::forwardToTelepathy(const NetworkPacket& 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 = \
np.get<QString>(QStringLiteral("messageBody"),QLatin1String("")); +        const \
QString messageBody = message.getBody();  const QString contactName = "";
-        const QString phoneNumber = np.get<QString>(QStringLiteral("phoneNumber"), \
i18n("unknown number")); +        const QString phoneNumber = message.getAddress();
         QDBusReply<bool> reply = \
m_telepathyInterface.call(QStringLiteral("sendMessage"), phoneNumber, contactName, \
messageBody);  if (reply) {
             return true;
diff --git a/plugins/telephony/telephonyplugin.h \
b/plugins/telephony/telephonyplugin.h index 2fc57549..cd3ed07b 100644
--- a/plugins/telephony/telephonyplugin.h
+++ b/plugins/telephony/telephonyplugin.h
@@ -1,5 +1,6 @@
 /**
  * Copyright 2013 Albert Vaca <albertvaka@gmail.com>
+ * Copyright 2018 Simon Redman <simon@ergotech.com>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -21,6 +22,8 @@
 #ifndef TELEPHONYPLUGIN_H
 #define TELEPHONYPLUGIN_H
 
+#include "telephonyMessage.h"
+
 #include <QLoggingCategory>
 #include <QDBusInterface>
 
@@ -81,10 +84,12 @@ private Q_SLOTS:
     void showSendSmsDialog();
 
 protected:
+    telephonyMessage convertPacketToMessage(const NetworkPacket& np);
+
     /**
      * Send to the telepathy plugin if it is available
      */
-    bool forwardToTelepathy(const NetworkPacket& np);
+    bool forwardToTelepathy(const telephonyMessage& message);
 
 private:
     QDBusInterface m_telepathyInterface;


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic