Including d-pointer in Device class

Summary:
Including d-pointer pattern in Device class.

Related to this task: https://phabricator.kde.org/T6805

Reviewers: #kde_connect, nicolasfella

Reviewed By: #kde_connect, nicolasfella

Subscribers: nicolasfella, kdeconnect

Tags: #kde_connect

Differential Revision: https://phabricator.kde.org/D12863
This commit is contained in:
Caio Carvalho 2018-05-15 19:15:05 -03:00
parent 0e023346a3
commit 4097b6f71f
2 changed files with 127 additions and 80 deletions

View file

@ -21,6 +21,8 @@
#include "device.h" #include "device.h"
#include <QDBusConnection> #include <QDBusConnection>
#include <QVector>
#include <QSet>
#include <QSslCertificate> #include <QSslCertificate>
#include <KSharedConfig> #include <KSharedConfig>
@ -37,6 +39,34 @@
#include "kdeconnectconfig.h" #include "kdeconnectconfig.h"
#include "daemon.h" #include "daemon.h"
class Device::DevicePrivate
{
public:
DevicePrivate(const QString &id)
: m_deviceId(id)
{
}
~DevicePrivate()
{
qDeleteAll(m_deviceLinks);
m_deviceLinks.clear();
}
const QString m_deviceId;
QString m_deviceName;
DeviceType m_deviceType;
int m_protocolVersion;
QVector<DeviceLink *> m_deviceLinks;
QHash<QString, KdeConnectPlugin *> m_plugins;
QMultiMap<QString, KdeConnectPlugin *> m_pluginsByIncomingCapability;
QSet<QString> m_supportedPlugins;
QSet<PairingHandler *> m_pairRequests;
};
static void warn(const QString& info) static void warn(const QString& info)
{ {
qWarning() << "Device pairing error" << info; qWarning() << "Device pairing error" << info;
@ -44,28 +74,28 @@ static void warn(const QString& info)
Device::Device(QObject* parent, const QString& id) Device::Device(QObject* parent, const QString& id)
: QObject(parent) : QObject(parent)
, m_deviceId(id) , d(new Device::DevicePrivate(id))
, m_protocolVersion(NetworkPacket::s_protocolVersion) //We don't know it yet
{ {
KdeConnectConfig::DeviceInfo info = KdeConnectConfig::instance()->getTrustedDevice(id); d->m_protocolVersion = NetworkPacket::s_protocolVersion;
KdeConnectConfig::DeviceInfo info = KdeConnectConfig::instance()->getTrustedDevice(d->m_deviceId);
m_deviceName = info.deviceName; d->m_deviceName = info.deviceName;
m_deviceType = str2type(info.deviceType); d->m_deviceType = str2type(info.deviceType);
//Register in bus //Register in bus
QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors); QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors);
//Assume every plugin is supported until addLink is called and we can get the actual list //Assume every plugin is supported until addLink is called and we can get the actual list
m_supportedPlugins = PluginLoader::instance()->getPluginList().toSet(); d->m_supportedPlugins = PluginLoader::instance()->getPluginList().toSet();
connect(this, &Device::pairingError, this, &warn); connect(this, &Device::pairingError, this, &warn);
} }
Device::Device(QObject* parent, const NetworkPacket& identityPacket, DeviceLink* dl) Device::Device(QObject* parent, const NetworkPacket& identityPacket, DeviceLink* dl)
: QObject(parent) : QObject(parent)
, m_deviceId(identityPacket.get<QString>(QStringLiteral("deviceId"))) , d(new Device::DevicePrivate(identityPacket.get<QString>(QStringLiteral("deviceId"))))
, m_deviceName(identityPacket.get<QString>(QStringLiteral("deviceName")))
{ {
d->m_deviceName = identityPacket.get<QString>(QStringLiteral("deviceName"));
addLink(identityPacket, dl); addLink(identityPacket, dl);
//Register in bus //Register in bus
@ -76,37 +106,66 @@ Device::Device(QObject* parent, const NetworkPacket& identityPacket, DeviceLink*
Device::~Device() Device::~Device()
{ {
qDeleteAll(m_deviceLinks); delete d;
m_deviceLinks.clear(); }
QString Device::id() const
{
return d->m_deviceId;
}
QString Device::name() const
{
return d->m_deviceName;
}
QString Device::type() const
{
return type2str(d->m_deviceType);
}
bool Device::isReachable() const
{
return !d->m_deviceLinks.isEmpty();
}
int Device::protocolVersion()
{
return d->m_protocolVersion;
}
QStringList Device::supportedPlugins() const
{
return d->m_supportedPlugins.toList();
} }
bool Device::hasPlugin(const QString& name) const bool Device::hasPlugin(const QString& name) const
{ {
return m_plugins.contains(name); return d->m_plugins.contains(name);
} }
QStringList Device::loadedPlugins() const QStringList Device::loadedPlugins() const
{ {
return m_plugins.keys(); return d->m_plugins.keys();
} }
void Device::reloadPlugins() void Device::reloadPlugins()
{ {
QHash<QString, KdeConnectPlugin*> newPluginMap, oldPluginMap = m_plugins; QHash<QString, KdeConnectPlugin*> newPluginMap, oldPluginMap = d->m_plugins;
QMultiMap<QString, KdeConnectPlugin*> newPluginsByIncomingCapability; QMultiMap<QString, KdeConnectPlugin*> newPluginsByIncomingCapability;
if (isTrusted() && isReachable()) { //Do not load any plugin for unpaired devices, nor useless loading them for unreachable devices if (isTrusted() && isReachable()) { //Do not load any plugin for unpaired devices, nor useless loading them for unreachable devices
PluginLoader* loader = PluginLoader::instance(); PluginLoader* loader = PluginLoader::instance();
for (const QString& pluginName : qAsConst(m_supportedPlugins)) { for (const QString& pluginName : qAsConst(d->m_supportedPlugins)) {
const KPluginMetaData service = loader->getPluginInfo(pluginName); const KPluginMetaData service = loader->getPluginInfo(pluginName);
const bool pluginEnabled = isPluginEnabled(pluginName); const bool pluginEnabled = isPluginEnabled(pluginName);
const QSet<QString> incomingCapabilities = KPluginMetaData::readStringList(service.rawData(), QStringLiteral("X-KdeConnect-SupportedPacketType")).toSet(); const QSet<QString> incomingCapabilities = KPluginMetaData::readStringList(service.rawData(), QStringLiteral("X-KdeConnect-SupportedPacketType")).toSet();
if (pluginEnabled) { if (pluginEnabled) {
KdeConnectPlugin* plugin = m_plugins.take(pluginName); KdeConnectPlugin* plugin = d->m_plugins.take(pluginName);
if (!plugin) { if (!plugin) {
plugin = loader->instantiatePluginForDevice(pluginName, this); plugin = loader->instantiatePluginForDevice(pluginName, this);
@ -126,12 +185,12 @@ void Device::reloadPlugins()
//Erase all left plugins in the original map (meaning that we don't want //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) //them anymore, otherwise they would have been moved to the newPluginMap)
qDeleteAll(m_plugins); qDeleteAll(d->m_plugins);
m_plugins = newPluginMap; d->m_plugins = newPluginMap;
m_pluginsByIncomingCapability = newPluginsByIncomingCapability; d->m_pluginsByIncomingCapability = newPluginsByIncomingCapability;
QDBusConnection bus = QDBusConnection::sessionBus(); QDBusConnection bus = QDBusConnection::sessionBus();
for (KdeConnectPlugin* plugin : qAsConst(m_plugins)) { for (KdeConnectPlugin* plugin : qAsConst(d->m_plugins)) {
//TODO: see how it works in Android (only done once, when created) //TODO: see how it works in Android (only done once, when created)
plugin->connected(); plugin->connected();
@ -162,14 +221,14 @@ void Device::requestPair()
return; return;
} }
for (DeviceLink* dl : qAsConst(m_deviceLinks)) { for (DeviceLink* dl : qAsConst(d->m_deviceLinks)) {
dl->userRequestsPair(); dl->userRequestsPair();
} }
} }
void Device::unpair() void Device::unpair()
{ {
for (DeviceLink* dl : qAsConst(m_deviceLinks)) { for (DeviceLink* dl : qAsConst(d->m_deviceLinks)) {
dl->userRequestsUnpair(); dl->userRequestsUnpair();
} }
KdeConnectConfig::instance()->removeTrustedDevice(id()); KdeConnectConfig::instance()->removeTrustedDevice(id());
@ -181,7 +240,7 @@ void Device::pairStatusChanged(DeviceLink::PairStatus status)
if (status == DeviceLink::NotPaired) { if (status == DeviceLink::NotPaired) {
KdeConnectConfig::instance()->removeTrustedDevice(id()); KdeConnectConfig::instance()->removeTrustedDevice(id());
for (DeviceLink* dl : qAsConst(m_deviceLinks)) { for (DeviceLink* dl : qAsConst(d->m_deviceLinks)) {
if (dl != sender()) { if (dl != sender()) {
dl->setPairStatus(DeviceLink::NotPaired); dl->setPairStatus(DeviceLink::NotPaired);
} }
@ -207,20 +266,20 @@ void Device::addLink(const NetworkPacket& identityPacket, DeviceLink* link)
//qCDebug(KDECONNECT_CORE) << "Adding link to" << id() << "via" << link->provider(); //qCDebug(KDECONNECT_CORE) << "Adding link to" << id() << "via" << link->provider();
setName(identityPacket.get<QString>(QStringLiteral("deviceName"))); setName(identityPacket.get<QString>(QStringLiteral("deviceName")));
m_deviceType = str2type(identityPacket.get<QString>(QStringLiteral("deviceType"))); d->m_deviceType = str2type(identityPacket.get<QString>(QStringLiteral("deviceType")));
if (m_deviceLinks.contains(link)) if (d->m_deviceLinks.contains(link))
return; return;
m_protocolVersion = identityPacket.get<int>(QStringLiteral("protocolVersion"), -1); d->m_protocolVersion = identityPacket.get<int>(QStringLiteral("protocolVersion"), -1);
if (m_protocolVersion != NetworkPacket::s_protocolVersion) { if (d->m_protocolVersion != NetworkPacket::s_protocolVersion) {
qCWarning(KDECONNECT_CORE) << m_deviceName << "- warning, device uses a different protocol version" << m_protocolVersion << "expected" << NetworkPacket::s_protocolVersion; qCWarning(KDECONNECT_CORE) << d->m_deviceName << "- warning, device uses a different protocol version" << d->m_protocolVersion << "expected" << NetworkPacket::s_protocolVersion;
} }
connect(link, &QObject::destroyed, connect(link, &QObject::destroyed,
this, &Device::linkDestroyed); this, &Device::linkDestroyed);
m_deviceLinks.append(link); d->m_deviceLinks.append(link);
//Theoretically we will never add two links from the same provider (the provider should destroy //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. //the old one before this is called), so we do not have to worry about destroying old links.
@ -229,22 +288,22 @@ void Device::addLink(const NetworkPacket& identityPacket, DeviceLink* link)
connect(link, &DeviceLink::receivedPacket, connect(link, &DeviceLink::receivedPacket,
this, &Device::privateReceivedPacket); this, &Device::privateReceivedPacket);
std::sort(m_deviceLinks.begin(), m_deviceLinks.end(), lessThan); std::sort(d->m_deviceLinks.begin(), d->m_deviceLinks.end(), lessThan);
const bool capabilitiesSupported = identityPacket.has(QStringLiteral("incomingCapabilities")) || identityPacket.has(QStringLiteral("outgoingCapabilities")); const bool capabilitiesSupported = identityPacket.has(QStringLiteral("incomingCapabilities")) || identityPacket.has(QStringLiteral("outgoingCapabilities"));
if (capabilitiesSupported) { if (capabilitiesSupported) {
const QSet<QString> outgoingCapabilities = identityPacket.get<QStringList>(QStringLiteral("outgoingCapabilities")).toSet() const QSet<QString> outgoingCapabilities = identityPacket.get<QStringList>(QStringLiteral("outgoingCapabilities")).toSet()
, incomingCapabilities = identityPacket.get<QStringList>(QStringLiteral("incomingCapabilities")).toSet(); , incomingCapabilities = identityPacket.get<QStringList>(QStringLiteral("incomingCapabilities")).toSet();
m_supportedPlugins = PluginLoader::instance()->pluginsForCapabilities(incomingCapabilities, outgoingCapabilities); d->m_supportedPlugins = PluginLoader::instance()->pluginsForCapabilities(incomingCapabilities, outgoingCapabilities);
//qDebug() << "new plugins for" << m_deviceName << m_supportedPlugins << incomingCapabilities << outgoingCapabilities; //qDebug() << "new plugins for" << m_deviceName << m_supportedPlugins << incomingCapabilities << outgoingCapabilities;
} else { } else {
m_supportedPlugins = PluginLoader::instance()->getPluginList().toSet(); d->m_supportedPlugins = PluginLoader::instance()->getPluginList().toSet();
} }
reloadPlugins(); reloadPlugins();
if (m_deviceLinks.size() == 1) { if (d->m_deviceLinks.size() == 1) {
Q_EMIT reachableChanged(true); Q_EMIT reachableChanged(true);
} }
@ -256,45 +315,45 @@ void Device::addLink(const NetworkPacket& identityPacket, DeviceLink* link)
void Device::addPairingRequest(PairingHandler* handler) void Device::addPairingRequest(PairingHandler* handler)
{ {
const bool wasEmpty = m_pairRequests.isEmpty(); const bool wasEmpty = d->m_pairRequests.isEmpty();
m_pairRequests.insert(handler); d->m_pairRequests.insert(handler);
if (wasEmpty != m_pairRequests.isEmpty()) if (wasEmpty != d->m_pairRequests.isEmpty())
Q_EMIT hasPairingRequestsChanged(!m_pairRequests.isEmpty()); Q_EMIT hasPairingRequestsChanged(!d->m_pairRequests.isEmpty());
} }
void Device::removePairingRequest(PairingHandler* handler) void Device::removePairingRequest(PairingHandler* handler)
{ {
const bool wasEmpty = m_pairRequests.isEmpty(); const bool wasEmpty = d->m_pairRequests.isEmpty();
m_pairRequests.remove(handler); d->m_pairRequests.remove(handler);
if (wasEmpty != m_pairRequests.isEmpty()) if (wasEmpty != d->m_pairRequests.isEmpty())
Q_EMIT hasPairingRequestsChanged(!m_pairRequests.isEmpty()); Q_EMIT hasPairingRequestsChanged(!d->m_pairRequests.isEmpty());
} }
bool Device::hasPairingRequests() const bool Device::hasPairingRequests() const
{ {
return !m_pairRequests.isEmpty(); return !d->m_pairRequests.isEmpty();
} }
void Device::acceptPairing() void Device::acceptPairing()
{ {
if (m_pairRequests.isEmpty()) if (d->m_pairRequests.isEmpty())
qWarning() << "no pair requests to accept!"; qWarning() << "no pair requests to accept!";
//copying because the pairing handler will be removed upon accept //copying because the pairing handler will be removed upon accept
const auto prCopy = m_pairRequests; const auto prCopy = d->m_pairRequests;
for (auto ph: prCopy) for (auto ph: prCopy)
ph->acceptPairing(); ph->acceptPairing();
} }
void Device::rejectPairing() void Device::rejectPairing()
{ {
if (m_pairRequests.isEmpty()) if (d->m_pairRequests.isEmpty())
qWarning() << "no pair requests to reject!"; qWarning() << "no pair requests to reject!";
//copying because the pairing handler will be removed upon reject //copying because the pairing handler will be removed upon reject
const auto prCopy = m_pairRequests; const auto prCopy = d->m_pairRequests;
for (auto ph: prCopy) for (auto ph: prCopy)
ph->rejectPairing(); ph->rejectPairing();
} }
@ -306,11 +365,11 @@ void Device::linkDestroyed(QObject* o)
void Device::removeLink(DeviceLink* link) void Device::removeLink(DeviceLink* link)
{ {
m_deviceLinks.removeAll(link); d->m_deviceLinks.removeAll(link);
//qCDebug(KDECONNECT_CORE) << "RemoveLink" << m_deviceLinks.size() << "links remaining"; //qCDebug(KDECONNECT_CORE) << "RemoveLink" << m_deviceLinks.size() << "links remaining";
if (m_deviceLinks.isEmpty()) { if (d->m_deviceLinks.isEmpty()) {
reloadPlugins(); reloadPlugins();
Q_EMIT reachableChanged(false); Q_EMIT reachableChanged(false);
} }
@ -322,7 +381,7 @@ bool Device::sendPacket(NetworkPacket& np)
Q_ASSERT(isTrusted()); Q_ASSERT(isTrusted());
//Maybe we could block here any packet that is not an identity or a pairing packet to prevent sending non encrypted data //Maybe we could block here any packet that is not an identity or a pairing packet to prevent sending non encrypted data
for (DeviceLink* dl : qAsConst(m_deviceLinks)) { for (DeviceLink* dl : qAsConst(d->m_deviceLinks)) {
if (dl->sendPacket(np)) return true; if (dl->sendPacket(np)) return true;
} }
@ -333,7 +392,7 @@ void Device::privateReceivedPacket(const NetworkPacket& np)
{ {
Q_ASSERT(np.type() != PACKET_TYPE_PAIR); Q_ASSERT(np.type() != PACKET_TYPE_PAIR);
if (isTrusted()) { if (isTrusted()) {
const QList<KdeConnectPlugin*> plugins = m_pluginsByIncomingCapability.values(np.type()); const QList<KdeConnectPlugin*> plugins = d->m_pluginsByIncomingCapability.values(np.type());
if (plugins.isEmpty()) { if (plugins.isEmpty()) {
qWarning() << "discarding unsupported packet" << np.type() << "for" << name(); qWarning() << "discarding unsupported packet" << np.type() << "for" << name();
} }
@ -355,8 +414,8 @@ bool Device::isTrusted() const
QStringList Device::availableLinks() const QStringList Device::availableLinks() const
{ {
QStringList sl; QStringList sl;
sl.reserve(m_deviceLinks.size()); sl.reserve(d->m_deviceLinks.size());
for (DeviceLink* dl : qAsConst(m_deviceLinks)) { for (DeviceLink* dl : qAsConst(d->m_deviceLinks)) {
sl.append(dl->provider()->name()); sl.append(dl->provider()->name());
} }
return sl; return sl;
@ -366,11 +425,11 @@ void Device::cleanUnneededLinks() {
if (isTrusted()) { if (isTrusted()) {
return; return;
} }
for(int i = 0; i < m_deviceLinks.size(); ) { for(int i = 0; i < d->m_deviceLinks.size(); ) {
DeviceLink* dl = m_deviceLinks[i]; DeviceLink* dl = d->m_deviceLinks[i];
if (!dl->linkShouldBeKeptAlive()) { if (!dl->linkShouldBeKeptAlive()) {
dl->deleteLater(); dl->deleteLater();
m_deviceLinks.remove(i); d->m_deviceLinks.remove(i);
} else { } else {
i++; i++;
} }
@ -379,7 +438,7 @@ void Device::cleanUnneededLinks() {
QHostAddress Device::getLocalIpAddress() const QHostAddress Device::getLocalIpAddress() const
{ {
for (DeviceLink* dl : m_deviceLinks) { for (DeviceLink* dl : d->m_deviceLinks) {
LanDeviceLink* ldl = dynamic_cast<LanDeviceLink*>(dl); LanDeviceLink* ldl = dynamic_cast<LanDeviceLink*>(dl);
if (ldl) { if (ldl) {
return ldl->hostAddress(); return ldl->hostAddress();
@ -419,7 +478,7 @@ QString Device::iconName() const
QString Device::iconForStatus(bool reachable, bool trusted) const QString Device::iconForStatus(bool reachable, bool trusted) const
{ {
Device::DeviceType deviceType = m_deviceType; Device::DeviceType deviceType = d->m_deviceType;
if (deviceType == Device::Unknown) { if (deviceType == Device::Unknown) {
deviceType = Device::Phone; //Assume phone if we don't know the type deviceType = Device::Phone; //Assume phone if we don't know the type
} else if (deviceType == Device::Desktop) { } else if (deviceType == Device::Desktop) {
@ -434,15 +493,15 @@ QString Device::iconForStatus(bool reachable, bool trusted) const
void Device::setName(const QString& name) void Device::setName(const QString& name)
{ {
if (m_deviceName != name) { if (d->m_deviceName != name) {
m_deviceName = name; d->m_deviceName = name;
Q_EMIT nameChanged(name); Q_EMIT nameChanged(name);
} }
} }
KdeConnectPlugin* Device::plugin(const QString& pluginName) const KdeConnectPlugin* Device::plugin(const QString& pluginName) const
{ {
return m_plugins[pluginName]; return d->m_plugins[pluginName];
} }
void Device::setPluginEnabled(const QString& pluginName, bool enabled) void Device::setPluginEnabled(const QString& pluginName, bool enabled)

View file

@ -23,8 +23,6 @@
#include <QObject> #include <QObject>
#include <QString> #include <QString>
#include <QVector>
#include <QSet>
#include <QHostAddress> #include <QHostAddress>
#include "networkpacket.h" #include "networkpacket.h"
@ -74,10 +72,10 @@ public:
~Device() override; ~Device() override;
QString id() const { return m_deviceId; } QString id() const;
QString name() const { return m_deviceName; } QString name() const;
QString dbusPath() const { return "/modules/kdeconnect/devices/"+id(); } QString dbusPath() const { return "/modules/kdeconnect/devices/"+id(); }
QString type() const { return type2str(m_deviceType); } QString type() const;
QString iconName() const; QString iconName() const;
QString statusIconName() const; QString statusIconName() const;
Q_SCRIPTABLE QString encryptionInfo() const; Q_SCRIPTABLE QString encryptionInfo() const;
@ -89,7 +87,7 @@ public:
Q_SCRIPTABLE bool isTrusted() const; Q_SCRIPTABLE bool isTrusted() const;
Q_SCRIPTABLE QStringList availableLinks() const; Q_SCRIPTABLE QStringList availableLinks() const;
bool isReachable() const { return !m_deviceLinks.isEmpty(); } bool isReachable() const;
Q_SCRIPTABLE QStringList loadedPlugins() const; Q_SCRIPTABLE QStringList loadedPlugins() const;
Q_SCRIPTABLE bool hasPlugin(const QString& name) const; Q_SCRIPTABLE bool hasPlugin(const QString& name) const;
@ -102,8 +100,8 @@ public:
void cleanUnneededLinks(); void cleanUnneededLinks();
int protocolVersion() { return m_protocolVersion; } int protocolVersion();
QStringList supportedPlugins() const { return m_supportedPlugins.toList(); } QStringList supportedPlugins() const;
QHostAddress getLocalIpAddress() const; QHostAddress getLocalIpAddress() const;
@ -145,19 +143,9 @@ private: //Methods
void setName(const QString& name); void setName(const QString& name);
QString iconForStatus(bool reachable, bool paired) const; QString iconForStatus(bool reachable, bool paired) const;
private: //Fields (TODO: dPointer!) private:
const QString m_deviceId; class DevicePrivate;
QString m_deviceName; DevicePrivate *d;
DeviceType m_deviceType;
int m_protocolVersion;
QVector<DeviceLink*> m_deviceLinks;
QHash<QString, KdeConnectPlugin*> m_plugins;
//Capabilities stuff
QMultiMap<QString, KdeConnectPlugin*> m_pluginsByIncomingCapability;
QSet<QString> m_supportedPlugins;
QSet<PairingHandler*> m_pairRequests;
}; };
Q_DECLARE_METATYPE(Device*) Q_DECLARE_METATYPE(Device*)