2013-06-25 17:06:51 +01:00
|
|
|
#include "device.h"
|
2013-07-03 02:52:44 +01:00
|
|
|
#include <ksharedptr.h>
|
|
|
|
#include <ksharedconfig.h>
|
|
|
|
#include "devicelinks/devicelink.h"
|
2013-07-23 15:11:54 +01:00
|
|
|
#include "announcers/announcer.h"
|
2013-06-25 17:06:51 +01:00
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
#include <KConfigGroup>
|
|
|
|
#include <QDebug>
|
2013-06-25 17:06:51 +01:00
|
|
|
|
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;
|
|
|
|
QDBusConnection::sessionBus().registerObject("/modules/kdeconnect/Devices/"+id, this, QDBusConnection::ExportScriptableContents);
|
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;
|
|
|
|
QDBusConnection::sessionBus().registerObject("/modules/kdeconnect/Devices/"+id, this, QDBusConnection::ExportScriptableContents);
|
|
|
|
|
|
|
|
addLink(link);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return p1->announcer()->priority() > p2->announcer()->priority();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::addLink(DeviceLink* link)
|
|
|
|
{
|
2013-07-23 15:11:54 +01:00
|
|
|
qDebug() << "Adding link to " << id() << "via" << link->announcer();
|
|
|
|
|
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);
|
|
|
|
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-04 18:17:22 +01:00
|
|
|
void Device::linkDestroyed(QObject* o)
|
|
|
|
{
|
2013-07-23 15:11:54 +01:00
|
|
|
qDebug() << "Link destroyed";
|
2013-07-04 18:17:22 +01:00
|
|
|
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
|
|
|
qDebug() << "RemoveLink";
|
2013-07-23 15:11:54 +01:00
|
|
|
//disconnect(link, SIGNAL(receivedPackage(NetworkPackage)), this, SLOT(privateReceivedPackage(NetworkPackage)));
|
2013-07-04 18:17:22 +01:00
|
|
|
m_deviceLinks.removeOne(link);
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
|
2013-07-04 00:09:49 +01:00
|
|
|
bool Device::sendPackage(const NetworkPackage& np)
|
2013-07-03 02:52:44 +01:00
|
|
|
{
|
2013-07-23 15:11:54 +01:00
|
|
|
Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
|
|
|
|
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) {
|
|
|
|
qDebug() << "package received from paired device";
|
2013-07-04 18:17:22 +01:00
|
|
|
emit receivedPackage(*this, np);
|
2013-07-03 02:52:44 +01:00
|
|
|
} else {
|
|
|
|
qDebug() << "not paired, ignoring package";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-23 15:11:54 +01:00
|
|
|
QStringList Device::availableLinks() const
|
|
|
|
{
|
|
|
|
QStringList sl;
|
|
|
|
Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
|
|
|
|
sl.append(dl->announcer()->name());
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|