smsplugin.cpp 4.48 KB
Newer Older
1 2
/**
 * Copyright 2013 Albert Vaca <albertvaka@gmail.com>
3
 * Copyright 2018 Simon Redman <simon@ergotech.com>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 21 22 23 24 25 26 27 28
 */

#include "smsplugin.h"

#include <KLocalizedString>
#include <KPluginFactory>

#include <QDebug>
#include <QDBusConnection>
29
#include <QProcess>
30 31 32 33

#include <core/device.h>
#include <core/daemon.h>

34
#include "plugin_sms_debug.h"
35

Nicolas Fella's avatar
Nicolas Fella committed
36
K_PLUGIN_CLASS_WITH_JSON(SmsPlugin, "kdeconnect_sms.json")
37 38 39 40 41 42 43 44 45 46

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()
{
47
    // m_conversationInterface is self-deleting, see ~ConversationsDbusInterface for more information
48 49 50 51
}

bool SmsPlugin::receivePacket(const NetworkPacket& np)
{
52
    if (np.type() == PACKET_TYPE_SMS_MESSAGES) {
53 54 55 56 57 58
        return handleBatchMessages(np);
    }

    return true;
}

59
void SmsPlugin::sendSms(const QVariantList& addresses, const QString& messageBody, const qint64 subID)
60
{
61
    QVariantList addressMapList;
62 63
    for (const QVariant& address : addresses) {
        QVariantMap addressMap({{QStringLiteral("address"), qdbus_cast<ConversationAddress>(address).address()}});
64 65 66
        addressMapList.append(addressMap);
    }

67
    QVariantMap packetMap({
68
        {QStringLiteral("sendSms"), true},
69
        {QStringLiteral("addresses"), addressMapList},
70
        {QStringLiteral("messageBody"), messageBody}
71
    });
72 73 74 75
    if (subID != -1) {
        packetMap[QStringLiteral("subID")] = subID;
    }
    NetworkPacket np(PACKET_TYPE_SMS_REQUEST, packetMap);
76
    qCDebug(KDECONNECT_PLUGIN_SMS) << "Dispatching SMS send request to remote";
77 78 79 80 81
    sendPacket(np);
}

void SmsPlugin::requestAllConversations()
{
82
    NetworkPacket np(PACKET_TYPE_SMS_REQUEST_CONVERSATIONS);
83 84 85 86

    sendPacket(np);
}

87
void SmsPlugin::requestConversation (const qint64& conversationID) const
88
{
89
    NetworkPacket np(PACKET_TYPE_SMS_REQUEST_CONVERSATION);
90
    np.set(QStringLiteral("threadID"), conversationID);
91 92 93 94 95 96 97 98 99 100 101 102 103

    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
104
    const QString phoneNumber = message.addresses()[0].address();
105 106 107 108 109
    m_telepathyInterface.call(QDBus::NoBlock, QStringLiteral("sendMessage"), phoneNumber, contactName, messageBody);
}

bool SmsPlugin::handleBatchMessages(const NetworkPacket& np)
{
110
    const auto messages = np.get<QVariantList>(QStringLiteral("messages"));
111 112
    QList<ConversationMessage> messagesList;
    messagesList.reserve(messages.count());
113

114
    for (const QVariant& body : messages) {
115
        ConversationMessage message(body.toMap());
116 117 118
        if (message.containsTextBody()) {
            forwardToTelepathy(message);
        }
119
        messagesList.append(message);
120 121
    }

122 123
    m_conversationInterface->addMessages(messagesList);

124 125 126 127 128
    return true;
}

QString SmsPlugin::dbusPath() const
{
129
    return QStringLiteral("/modules/kdeconnect/devices/") + device()->id() + QStringLiteral("/sms");
130 131
}

132 133 134 135 136
void SmsPlugin::launchApp()
{
    QProcess::startDetached(QLatin1String("kdeconnect-sms"), { QStringLiteral("--device"), device()->id() });
}

137 138
#include "smsplugin.moc"