device.cpp 16 KB
Newer Older
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**
 * Copyright 2013 Albert Vaca <albertvaka@gmail.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/>.
 */

21
#include "device.h"
22

Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
23 24 25 26 27 28 29
#ifdef interface // MSVC language extension, QDBusConnection uses this as a variable name
#undef interface
#endif

#include <QDBusConnection>
#include <QFile>

30
#include <KSharedConfig>
31
#include <KConfigGroup>
32 33 34
#include <KStandardDirs>
#include <KPluginSelector>
#include <KServiceTypeTrader>
35 36
#include <KNotification>
#include <KIcon>
37

38
#include "kdebugnamespace.h"
39 40
#include "kdeconnectplugin.h"
#include "pluginloader.h"
41 42
#include "backends/devicelink.h"
#include "backends/linkprovider.h"
43 44
#include "networkpackage.h"

45 46 47
Device::Device(QObject* parent, const QString& id)
    : QObject(parent)
    , m_deviceId(id)
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
48 49
    , m_pairStatus(Device::Paired)
    , m_protocolVersion(NetworkPackage::ProtocolVersion) //We don't know it yet
50
{
51
    KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
52
    KConfigGroup data = config->group("trusted_devices").group(id);
53

54
    m_deviceName = data.readEntry<QString>("deviceName", QLatin1String("unnamed"));
55
    m_deviceType = str2type(data.readEntry<QString>("deviceType", QLatin1String("unknown")));
56

57
    const QString& key = data.readEntry<QString>("publicKey", QString());
58
    m_publicKey = QCA::RSAPublicKey::fromPEM(key);
59
    
60
    initPrivateKey();
61

62
    //Register in bus
63
    QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors);
64
}
65

66 67 68
Device::Device(QObject* parent, const NetworkPackage& identityPackage, DeviceLink* dl)
    : QObject(parent)
    , m_deviceId(identityPackage.get<QString>("deviceId"))
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
69
    , m_deviceName(identityPackage.get<QString>("deviceName"))
70
    , m_deviceType(str2type(identityPackage.get<QString>("deviceType")))
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
71 72
    , m_pairStatus(Device::NotPaired)
    , m_protocolVersion(identityPackage.get<int>("protocolVersion"))
73
{
74
    initPrivateKey();
Samoilenko Yuri's avatar
Samoilenko Yuri committed
75 76

    addLink(identityPackage, dl);
Samoilenko Yuri's avatar
Samoilenko Yuri committed
77
    
78
    //Register in bus
79
    QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors);
80 81
}

82 83 84 85 86 87 88 89 90
void Device::initPrivateKey()
{
    //TODO: It is redundant to have our own private key in every instance of Device, move this to a singleton somewhere (Daemon?)
    const QString privateKeyPath = KStandardDirs::locateLocal("appdata", "key.pem", true, KComponentData("kdeconnect", "kdeconnect"));
    QFile privKey(privateKeyPath);
    privKey.open(QIODevice::ReadOnly);
    m_privateKey = QCA::PrivateKey::fromPEM(privKey.readAll());
}

Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
91 92
Device::~Device()
{
93 94

}
95

96
bool Device::hasPlugin(const QString& name) const
97 98 99 100
{
    return m_plugins.contains(name);
}

101
QStringList Device::loadedPlugins() const
102 103 104 105
{
    return m_plugins.keys();
}

106 107
void Device::reloadPlugins()
{
108
    QMap<QString, KdeConnectPlugin*> newPluginMap;
109 110
    QMultiMap<QString, KdeConnectPlugin*> newPluginsByIncomingInterface;
    QMultiMap<QString, KdeConnectPlugin*> newPluginsByOutgoingInterface;
111

112
    if (isPaired() && isReachable()) { //Do not load any plugin for unpaired devices, nor useless loading them for unreachable devices
113

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
114 115
        QString path = KGlobal::dirs()->findResource("config", "kdeconnect/"+id());
        KConfigGroup pluginStates = KSharedConfig::openConfig(path)->group("Plugins");
116

117
        PluginLoader* loader = PluginLoader::instance();
118

119 120
        //Code borrowed from KWin
        foreach (const QString& pluginName, loader->getPluginList()) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
121
            QString enabledKey = pluginName + QString::fromLatin1("Enabled");
122

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
123 124
            bool isPluginEnabled = (pluginStates.hasKey(enabledKey) ? pluginStates.readEntry(enabledKey, false)
                                                            : loader->getPluginInfo(pluginName).isPluginEnabledByDefault());
125

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
126
            if (isPluginEnabled) {
127
                KdeConnectPlugin* plugin = m_plugins.take(pluginName);
128
                QStringList incomingInterfaces, outgoingInterfaces;
129
                if (plugin) {
130 131
                    incomingInterfaces = m_pluginsByIncomingInterface.keys(plugin);
                    outgoingInterfaces = m_pluginsByOutgoingInterface.keys(plugin);
132
                } else {
133 134
                    PluginData data = loader->instantiatePluginForDevice(pluginName, this);
                    plugin = data.plugin;
135 136
                    incomingInterfaces = data.incomingInterfaces;
                    outgoingInterfaces = data.outgoingInterfaces;
137
                }
138 139 140 141 142
                foreach(const QString& interface, incomingInterfaces) {
                    newPluginsByIncomingInterface.insert(interface, plugin);
                }
                foreach(const QString& interface, outgoingInterfaces) {
                    newPluginsByOutgoingInterface.insert(interface, plugin);
143
                }
144
                newPluginMap[pluginName] = plugin;
145
            }
146 147 148
        }
    }

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
149 150
    //Erase all left plugins in the original map (meaning that we don't want
    //them anymore, otherwise they would have been moved to the newPluginMap)
151 152
    qDeleteAll(m_plugins);
    m_plugins = newPluginMap;
153 154
    m_pluginsByIncomingInterface = newPluginsByIncomingInterface;
    m_pluginsByOutgoingInterface = newPluginsByOutgoingInterface;
155

156
    Q_FOREACH(KdeConnectPlugin* plugin, m_plugins) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
157
        plugin->connected();
158 159
    }

160
    Q_EMIT pluginsChanged();
161 162 163

}

164
void Device::requestPair()
165
{
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
166 167 168 169 170 171 172 173 174 175 176 177 178
    switch(m_pairStatus) {
        case Device::Paired:
            Q_EMIT pairingFailed(i18n("Already paired"));
            return;
        case Device::Requested:
            Q_EMIT pairingFailed(i18n("Pairing already requested for this device"));
            return;
        default:
            if (!isReachable()) {
                Q_EMIT pairingFailed(i18n("Device not reachable"));
                return;
            }
            break;
179 180
    }

181 182
    m_pairStatus = Device::Requested;

183
    //Send our own public key
184
    bool success = sendOwnPublicKey();
185 186

    if (!success) {
187
        m_pairStatus = Device::NotPaired;
188 189
        Q_EMIT pairingFailed(i18n("Error contacting device"));
        return;
190
    }
191

192 193 194 195
    if (m_pairStatus == Device::Paired) {
        return;
    }

196 197 198
    m_pairingTimeut.setSingleShot(true);
    m_pairingTimeut.start(30 * 1000); //30 seconds of timeout
    connect(&m_pairingTimeut, SIGNAL(timeout()),
199 200 201 202 203 204
            this, SLOT(pairingTimeout()));

}

void Device::unpair()
{
205
    m_pairStatus = Device::NotPaired;
206 207

    KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
208
    config->group("trusted_devices").deleteGroup(id());
209

210 211 212
    NetworkPackage np(PACKAGE_TYPE_PAIR);
    np.set("pair", false);
    sendPackage(np);
213 214 215

    reloadPlugins(); //Will unload the plugins

216 217
    Q_EMIT unpaired();

218 219 220 221
}

void Device::pairingTimeout()
{
222 223 224 225
    NetworkPackage np(PACKAGE_TYPE_PAIR);
    np.set("pair", false);
    sendPackage(np);
    m_pairStatus = Device::NotPaired;
Albert Vaca Cintora's avatar
i18n  
Albert Vaca Cintora committed
226
    Q_EMIT pairingFailed(i18n("Timed out"));
227 228 229 230
}

static bool lessThan(DeviceLink* p1, DeviceLink* p2)
{
231
    return p1->provider()->priority() > p2->provider()->priority();
232 233
}

234
void Device::addLink(const NetworkPackage& identityPackage, DeviceLink* link)
235
{
236
    //kDebug(kdeconnect_kded()) << "Adding link to" << id() << "via" << link->provider();
237

238 239 240 241 242
    m_protocolVersion = identityPackage.get<int>("protocolVersion");
    if (m_protocolVersion != NetworkPackage::ProtocolVersion) {
        qWarning() << m_deviceName << "- warning, device uses a different protocol version" << m_protocolVersion << "expected" << NetworkPackage::ProtocolVersion;
    }

243 244
    connect(link, SIGNAL(destroyed(QObject*)),
            this, SLOT(linkDestroyed(QObject*)));
245

246
    m_deviceLinks.append(link);
247

248 249
    //re-read the device name from the identityPackage because it could have changed
    m_deviceName = identityPackage.get<QString>("deviceName");
250
    m_deviceType = str2type(identityPackage.get<QString>("deviceType"));
251

Samoilenko Yuri's avatar
Samoilenko Yuri committed
252
    Q_ASSERT(!m_privateKey.isNull());
253
    link->setPrivateKey(m_privateKey);
254

255 256 257
    //Theoretically we will never add two links from the same provider (the provider should destroy
    //the old one before this is called), so we do not have to worry about destroying old links.
    //Actually, we should not destroy them or the provider will store an invalid ref!
258

259 260
    connect(link, SIGNAL(receivedPackage(NetworkPackage)),
            this, SLOT(privateReceivedPackage(NetworkPackage)));
261

262
    qSort(m_deviceLinks.begin(), m_deviceLinks.end(), lessThan);
263 264

    if (m_deviceLinks.size() == 1) {
265
        reloadPlugins(); //Will load the plugins
266
        Q_EMIT reachableStatusChanged();
267 268 269 270
    } else {
        Q_FOREACH(KdeConnectPlugin* plugin, m_plugins) {
            plugin->connected();
        }
271
    }
272 273
}

274 275 276 277 278
void Device::linkDestroyed(QObject* o)
{
    removeLink(static_cast<DeviceLink*>(o));
}

279 280
void Device::removeLink(DeviceLink* link)
{
281
    m_deviceLinks.removeOne(link);
282

283
    //kDebug(kdeconnect_kded()) << "RemoveLink" << m_deviceLinks.size() << "links remaining";
284

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
285
    if (m_deviceLinks.isEmpty()) {
286
        reloadPlugins();
287
        Q_EMIT reachableStatusChanged();
288
    }
289 290
}

291
QString Device::privateKeyPath() const
292
{
293
    return KSharedConfig::openConfig("kdeconnectrc")->group("myself").readEntry("privateKeyPath");
294 295
}

296
bool Device::sendPackage(NetworkPackage& np)
297
{
298
    if (np.type() != PACKAGE_TYPE_PAIR && isPaired()) {
299 300 301
        Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
            if (dl->sendPackageEncrypted(m_publicKey, np)) return true;
        }
302 303
    } else {
        //Maybe we could block here any package that is not an identity or a pairing package to prevent sending non encrypted data
304 305 306
        Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
            if (dl->sendPackage(np)) return true;
        }
307 308
    }

309
    return false;
310 311 312 313
}

void Device::privateReceivedPackage(const NetworkPackage& np)
{
314 315
    if (np.type() == PACKAGE_TYPE_PAIR) {

316
        //kDebug(kdeconnect_kded()) << "Pair package";
317

318
        bool wantsPair = np.get<bool>("pair");
319

320
        if (wantsPair == isPaired()) {
321
            kDebug(kdeconnect_kded()) << "Already" << (wantsPair? "paired":"unpaired");
322
            if (m_pairStatus == Device::Requested) {
323
                m_pairStatus = Device::NotPaired;
324
                m_pairingTimeut.stop();
325 326
                Q_EMIT pairingFailed(i18n("Canceled by other peer"));
            }
327 328 329
            return;
        }

330
        if (wantsPair) {
331

332 333 334 335
            //Retrieve their public key
            const QString& key = np.get<QString>("publicKey");
            m_publicKey = QCA::RSAPublicKey::fromPEM(key);
            if (m_publicKey.isNull()) {
336
                kDebug(kdeconnect_kded()) << "ERROR decoding key";
337 338
                if (m_pairStatus == Device::Requested) {
                    m_pairStatus = Device::NotPaired;
339
                    m_pairingTimeut.stop();
340 341 342 343
                }
                Q_EMIT pairingFailed(i18n("Received incorrect key"));
                return;
            }
344

345
            if (m_pairStatus == Device::Requested)  { //We started pairing
346

347
                kDebug(kdeconnect_kded()) << "Pair answer";
348
                setAsPaired();
349 350 351

            } else {

352
                kDebug(kdeconnect_kded()) << "Pair request";
353 354 355 356 357 358 359 360 361 362 363

                KNotification* notification = new KNotification("pingReceived"); //KNotification::Persistent
                notification->setPixmap(KIcon("dialog-information").pixmap(48, 48));
                notification->setComponentData(KComponentData("kdeconnect", "kdeconnect"));
                notification->setTitle("KDE Connect");
                notification->setText(i18n("Pairing request from %1", m_deviceName));
                notification->setActions(QStringList() << i18n("Accept") << i18n("Reject"));
                connect(notification, SIGNAL(action1Activated()), this, SLOT(acceptPairing()));
                connect(notification, SIGNAL(action2Activated()), this, SLOT(rejectPairing()));
                notification->sendEvent();

364 365
                m_pairStatus = Device::RequestedByPeer;

366 367 368 369
            }

        } else {

370
            kDebug(kdeconnect_kded()) << "Unpair request";
371

372 373 374 375
            PairStatus prevPairStatus = m_pairStatus;
            m_pairStatus = Device::NotPaired;

            if (prevPairStatus == Device::Requested) {
376
                m_pairingTimeut.stop();
377
                Q_EMIT pairingFailed(i18n("Canceled by other peer"));
378
            } else if (prevPairStatus == Device::Paired) {
379 380 381
                KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
                config->group("trusted_devices").deleteGroup(id());
                reloadPlugins();
382
                Q_EMIT unpaired();
383 384 385 386
            }

        }

387
    } else if (isPaired()) {
388
        QList<KdeConnectPlugin*> plugins = m_pluginsByIncomingInterface.values(np.type());
389 390 391 392
        foreach(KdeConnectPlugin* plugin, plugins) {
            plugin->receivePackage(np);
        }
    } else {
393
        kDebug(kdeconnect_kded()) << "device" << name() << "not paired, ignoring package" << np.type();
394 395
        unpair();

396
    }
397 398 399

}

400
bool Device::sendOwnPublicKey()
401 402 403
{
    NetworkPackage np(PACKAGE_TYPE_PAIR);
    np.set("pair", true);
Samoilenko Yuri's avatar
Samoilenko Yuri committed
404
    np.set("publicKey", m_privateKey.toPublicKey().toPEM());
405
    bool success = sendPackage(np);
406
    return success;
407 408 409 410
}

void Device::rejectPairing()
{
411
    kDebug(kdeconnect_kded()) << "Rejected pairing";
412

413 414
    m_pairStatus = Device::NotPaired;

415 416 417 418
    NetworkPackage np(PACKAGE_TYPE_PAIR);
    np.set("pair", false);
    sendPackage(np);

419
    Q_EMIT pairingFailed(i18n("Canceled by the user"));
420

421 422
}

423 424 425 426
void Device::acceptPairing()
{
    if (m_pairStatus != Device::RequestedByPeer) return;

427
    kDebug(kdeconnect_kded()) << "Accepted pairing";
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

    bool success = sendOwnPublicKey();

    if (!success) {
        m_pairStatus = Device::NotPaired;
        return;
    }

    setAsPaired();

}

void Device::setAsPaired()
{

    m_pairStatus = Device::Paired;

445
    m_pairingTimeut.stop(); //Just in case it was started
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460

    storeAsTrusted(); //Save to the config as trusted

    reloadPlugins(); //Will actually load the plugins

    Q_EMIT pairingSuccesful();

}

void Device::storeAsTrusted()
{
    KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
    config->group("trusted_devices").group(id()).writeEntry("publicKey", m_publicKey.toPEM());
    config->group("trusted_devices").group(id()).writeEntry("deviceName", name());
    config->group("trusted_devices").group(id()).writeEntry("deviceType", type2str(m_deviceType));
461
    config->sync();
462 463
}

464 465 466 467
QStringList Device::availableLinks() const
{
    QStringList sl;
    Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
468
        sl.append(dl->provider()->name());
469 470 471 472
    }
    return sl;
}

473
Device::DeviceType Device::str2type(QString deviceType) {
474 475 476 477 478 479 480
    if (deviceType == "desktop") return Desktop;
    if (deviceType == "laptop") return Laptop;
    if (deviceType == "phone") return Phone;
    if (deviceType == "tablet") return Tablet;
    return Unknown;
}

481
QString Device::type2str(Device::DeviceType deviceType) {
482 483 484 485 486
    if (deviceType == Desktop) return "desktop";
    if (deviceType == Laptop) return "laptop";
    if (deviceType == Phone) return "phone";
    if (deviceType == Tablet) return "tablet";
    return "unknown";
487
}
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

QString Device::iconName() const
{
    switch(m_deviceType) {
        case Device::Desktop:
            return "computer";
        case Device::Laptop:
            return "computer-laptop";
        case Device::Phone:
            return "smartphone";
        case Device::Tablet:
            return "tablet";
        case Device::Unknown:
            return "unknown";
    }
    return QString();
}