2013-06-25 17:06:51 +01:00
|
|
|
#include "device.h"
|
2013-08-12 15:09:52 +01:00
|
|
|
|
|
|
|
#include <KSharedPtr>
|
|
|
|
#include <KSharedConfig>
|
2013-07-03 02:52:44 +01:00
|
|
|
#include <KConfigGroup>
|
2013-08-13 04:07:32 +01:00
|
|
|
#include <KStandardDirs>
|
|
|
|
#include <KPluginSelector>
|
|
|
|
#include <KServiceTypeTrader>
|
|
|
|
#include <KPluginInfo>
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
#include <QDebug>
|
2013-06-25 17:06:51 +01:00
|
|
|
|
2013-08-13 04:14:46 +01:00
|
|
|
#include "plugins/kdeconnectplugin.h"
|
2013-08-13 04:07:32 +01:00
|
|
|
#include "plugins/pluginloader.h"
|
2013-08-12 15:09:52 +01:00
|
|
|
#include "devicelinks/devicelink.h"
|
|
|
|
#include "linkproviders/linkprovider.h"
|
|
|
|
#include "networkpackage.h"
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
Device::Device(const QString& id, const QString& name)
|
|
|
|
{
|
|
|
|
m_deviceId = id;
|
|
|
|
m_deviceName = name;
|
|
|
|
m_paired = true;
|
|
|
|
m_knownIdentiy = true;
|
2013-07-26 15:21:19 +01:00
|
|
|
|
|
|
|
//Register in bus
|
2013-08-13 22:23:32 +01:00
|
|
|
QDBusConnection::sessionBus().registerObject("/modules/kdeconnect/devices/"+id, this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors | QDBusConnection::ExportChildObjects);
|
2013-07-26 15:21:19 +01:00
|
|
|
|
2013-08-13 04:07:32 +01:00
|
|
|
reloadPlugins();
|
2013-06-25 17:06:51 +01:00
|
|
|
}
|
2013-07-03 02:52:44 +01:00
|
|
|
|
|
|
|
Device::Device(const QString& id, const QString& name, DeviceLink* link)
|
|
|
|
{
|
|
|
|
m_deviceId = id;
|
|
|
|
m_deviceName = name;
|
|
|
|
m_paired = false;
|
|
|
|
m_knownIdentiy = true;
|
2013-07-26 15:21:19 +01:00
|
|
|
|
|
|
|
//Register in bus
|
2013-08-13 22:23:32 +01:00
|
|
|
QDBusConnection::sessionBus().registerObject("/modules/kdeconnect/devices/"+id, this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors | QDBusConnection::ExportChildObjects);
|
2013-07-03 02:52:44 +01:00
|
|
|
|
|
|
|
addLink(link);
|
2013-08-13 04:07:32 +01:00
|
|
|
|
|
|
|
reloadPlugins();
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
Device::Device(const QString& id, const QString& name, DeviceLink* link)
|
|
|
|
{
|
|
|
|
m_deviceId = id;
|
|
|
|
m_deviceName = id; //Temporary name
|
|
|
|
m_paired = false;
|
|
|
|
m_knownIdentiy = false;
|
|
|
|
|
|
|
|
addLink(link);
|
|
|
|
|
|
|
|
NetworkPackage identityRequest;
|
|
|
|
identityRequest.setType("IDENTITY_REQUEST");
|
|
|
|
link->sendPackage(identityRequest);
|
|
|
|
|
|
|
|
QDBusConnection::sessionBus().registerObject("/modules/kdeconnect/Devices/"+id, this);
|
|
|
|
}
|
|
|
|
*/
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2013-08-13 22:23:32 +01:00
|
|
|
bool Device::hasPlugin(const QString& name)
|
|
|
|
{
|
|
|
|
return m_plugins.contains(name);
|
|
|
|
}
|
|
|
|
|
2013-08-13 04:07:32 +01:00
|
|
|
void Device::reloadPlugins()
|
|
|
|
{
|
|
|
|
|
|
|
|
qDeleteAll(m_plugins);
|
|
|
|
m_plugins.clear();
|
|
|
|
|
|
|
|
QString path = KStandardDirs().resourceDirs("config").first()+"kdeconnect/";
|
|
|
|
QMap<QString,QString> pluginStates = KSharedConfig::openConfig(path + id())->group("Plugins").entryMap();
|
|
|
|
|
|
|
|
PluginLoader* loader = PluginLoader::instance();
|
|
|
|
|
|
|
|
//Code borrowed from KWin
|
|
|
|
foreach (const QString& pluginName, loader->getPluginList()) {
|
|
|
|
|
|
|
|
const QString value = pluginStates.value(pluginName + QString::fromLatin1("Enabled"), QString());
|
2013-08-13 05:03:12 +01:00
|
|
|
KPluginInfo info = loader->getPluginInfo(pluginName);
|
|
|
|
bool enabled = (value.isNull() ? info.isPluginEnabledByDefault() : QVariant(value).toBool());
|
2013-08-13 04:07:32 +01:00
|
|
|
|
|
|
|
qDebug() << pluginName << "enabled:" << enabled;
|
|
|
|
|
|
|
|
if (enabled) {
|
2013-08-13 04:14:46 +01:00
|
|
|
KdeConnectPlugin* plugin = loader->instantiatePluginForDevice(pluginName, this);
|
2013-08-13 04:07:32 +01:00
|
|
|
|
|
|
|
connect(this, SIGNAL(receivedPackage(const NetworkPackage&)),
|
|
|
|
plugin, SLOT(receivePackage(const NetworkPackage&)));
|
2013-08-13 22:23:32 +01:00
|
|
|
// connect(plugin, SIGNAL(sendPackage(const NetworkPackage&)),
|
2013-08-13 04:07:32 +01:00
|
|
|
// device, SLOT(sendPackage(const NetworkPackage&)));
|
|
|
|
|
|
|
|
|
2013-08-13 22:23:32 +01:00
|
|
|
m_plugins[pluginName] = plugin;
|
2013-08-13 04:07:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-13 22:23:32 +01:00
|
|
|
Q_EMIT pluginsChanged();
|
2013-08-13 04:07:32 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
void Device::setPair(bool b)
|
|
|
|
{
|
|
|
|
qDebug() << "setPair" << b;
|
|
|
|
m_paired = b;
|
|
|
|
KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc");
|
|
|
|
if (b) {
|
|
|
|
qDebug() << name() << "paired";
|
|
|
|
config->group("devices").group("paired").group(id()).writeEntry("name",name());
|
|
|
|
} else {
|
|
|
|
qDebug() << name() << "unpaired";
|
|
|
|
config->group("devices").group("paired").deleteGroup(id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool lessThan(DeviceLink* p1, DeviceLink* p2)
|
|
|
|
{
|
2013-07-24 17:42:33 +01:00
|
|
|
return p1->provider()->priority() > p2->provider()->priority();
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Device::addLink(DeviceLink* link)
|
|
|
|
{
|
2013-08-07 12:40:39 +01:00
|
|
|
qDebug() << "Adding link to" << id() << "via" << link->provider();
|
2013-07-23 15:11:54 +01:00
|
|
|
|
2013-07-04 18:17:22 +01:00
|
|
|
connect(link,SIGNAL(destroyed(QObject*)),this,SLOT(linkDestroyed(QObject*)));
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
m_deviceLinks.append(link);
|
2013-08-10 04:21:55 +01:00
|
|
|
|
2013-08-13 22:23:32 +01:00
|
|
|
//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!
|
2013-08-10 04:21:55 +01:00
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
connect(link, SIGNAL(receivedPackage(NetworkPackage)), this, SLOT(privateReceivedPackage(NetworkPackage)));
|
2013-07-04 18:17:22 +01:00
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
qSort(m_deviceLinks.begin(),m_deviceLinks.end(),lessThan);
|
2013-07-28 21:00:45 +01:00
|
|
|
|
|
|
|
if (m_deviceLinks.size() == 1) {
|
2013-08-13 22:23:32 +01:00
|
|
|
Q_EMIT reachableStatusChanged();
|
2013-07-28 21:00:45 +01:00
|
|
|
}
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
|
2013-07-04 18:17:22 +01:00
|
|
|
void Device::linkDestroyed(QObject* o)
|
|
|
|
{
|
|
|
|
removeLink(static_cast<DeviceLink*>(o));
|
|
|
|
}
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
void Device::removeLink(DeviceLink* link)
|
|
|
|
{
|
2013-07-04 18:17:22 +01:00
|
|
|
m_deviceLinks.removeOne(link);
|
2013-07-28 21:00:45 +01:00
|
|
|
|
|
|
|
qDebug() << "RemoveLink"<< m_deviceLinks.size() << "links remaining";
|
|
|
|
|
|
|
|
if (m_deviceLinks.empty()) {
|
2013-08-13 22:23:32 +01:00
|
|
|
Q_EMIT reachableStatusChanged();
|
2013-07-28 21:00:45 +01:00
|
|
|
}
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
|
2013-08-10 04:21:55 +01:00
|
|
|
bool Device::sendPackage(const NetworkPackage& np) const
|
2013-07-03 02:52:44 +01:00
|
|
|
{
|
2013-08-13 04:40:39 +01:00
|
|
|
if (!m_paired) {
|
|
|
|
//qDebug() << "sendpackage disabled on untrusted device" << name();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-23 15:11:54 +01:00
|
|
|
Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
|
2013-08-13 22:23:32 +01:00
|
|
|
//TODO: Actually detect if a package is received or not, now we keep TCP
|
|
|
|
//"ESTABLISHED" connections that look legit (return true when we use them),
|
2013-08-12 15:09:52 +01:00
|
|
|
//but that are actually broken
|
2013-07-23 15:11:54 +01:00
|
|
|
if (dl->sendPackage(np)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Device::privateReceivedPackage(const NetworkPackage& np)
|
|
|
|
{
|
2013-07-04 00:09:49 +01:00
|
|
|
if (np.type() == "kdeconnect.identity" && !m_knownIdentiy) {
|
|
|
|
m_deviceName = np.get<QString>("deviceName");
|
2013-07-03 02:52:44 +01:00
|
|
|
} else if (m_paired) {
|
2013-08-13 04:40:39 +01:00
|
|
|
//qDebug() << "package received from trusted device" << name();
|
2013-08-13 04:07:32 +01:00
|
|
|
Q_EMIT receivedPackage(np);
|
2013-07-03 02:52:44 +01:00
|
|
|
} else {
|
2013-08-13 04:07:32 +01:00
|
|
|
qDebug() << "device" << name() << "not trusted, ignoring package" << np.type();
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-23 15:11:54 +01:00
|
|
|
QStringList Device::availableLinks() const
|
|
|
|
{
|
|
|
|
QStringList sl;
|
|
|
|
Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
|
2013-07-24 17:42:33 +01:00
|
|
|
sl.append(dl->provider()->name());
|
2013-07-23 15:11:54 +01:00
|
|
|
}
|
|
|
|
return sl;
|
|
|
|
}
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
void Device::sendPing()
|
|
|
|
{
|
2013-07-04 00:09:49 +01:00
|
|
|
NetworkPackage np("kdeconnect.ping");
|
|
|
|
bool success = sendPackage(np);
|
|
|
|
qDebug() << "sendPing:" << success;
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
|