1 /**
2  * SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
3  *
4  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5  */
6 
7 #ifndef DEVICE_H
8 #define DEVICE_H
9 
10 #include <QObject>
11 #include <QString>
12 #include <QHostAddress>
13 
14 #include "networkpacket.h"
15 #include "backends/devicelink.h"
16 
17 class DeviceLink;
18 class KdeConnectPlugin;
19 
20 class KDECONNECTCORE_EXPORT Device
21     : public QObject
22 {
23     Q_OBJECT
24     Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device")
25     Q_PROPERTY(QString type READ type NOTIFY typeChanged)
26     Q_PROPERTY(QString name READ name NOTIFY nameChanged)
27     Q_PROPERTY(QString iconName READ iconName CONSTANT)
28     Q_PROPERTY(QString statusIconName READ statusIconName NOTIFY statusIconNameChanged)
29     Q_PROPERTY(bool isReachable READ isReachable NOTIFY reachableChanged)
30     Q_PROPERTY(bool isTrusted READ isTrusted NOTIFY trustedChanged)
31     Q_PROPERTY(QStringList supportedPlugins READ supportedPlugins NOTIFY pluginsChanged)
32     Q_PROPERTY(bool hasPairingRequests READ hasPairingRequests NOTIFY hasPairingRequestsChanged)
33 
34 public:
35 
36     enum DeviceType {
37         Unknown,
38         Desktop,
39         Laptop,
40         Phone,
41         Tablet,
42         Tv,
43     };
44 
45     /**
46      * Restores the @p device from the saved configuration
47      *
48      * We already know it but we need to wait for an incoming DeviceLink to communicate
49      */
50     Device(QObject* parent, const QString& id);
51 
52     /**
53      * Device known via an incoming connection sent to us via a devicelink.
54      *
55      * We know everything but we don't trust it yet
56      */
57     Device(QObject* parent, const NetworkPacket& np, DeviceLink* dl);
58 
59     ~Device() override;
60 
61     QString id() const;
62     QString name() const;
dbusPath()63     QString dbusPath() const { return QStringLiteral("/modules/kdeconnect/devices/") + id(); }
64     QString type() const;
65     QString iconName() const;
66     QString statusIconName() const;
67     Q_SCRIPTABLE QByteArray verificationKey() const;
68     Q_SCRIPTABLE QString encryptionInfo() const;
69 
70     //Add and remove links
71     void addLink(const NetworkPacket& identityPacket, DeviceLink*);
72     void removeLink(DeviceLink*);
73 
74     Q_SCRIPTABLE bool isTrusted() const;
75 
76     Q_SCRIPTABLE QStringList availableLinks() const;
77     virtual bool isReachable() const;
78 
79     Q_SCRIPTABLE QStringList loadedPlugins() const;
80     Q_SCRIPTABLE bool hasPlugin(const QString& name) const;
81 
82     Q_SCRIPTABLE QString pluginsConfigFile() const;
83 
84     KdeConnectPlugin* plugin(const QString& pluginName) const;
85     Q_SCRIPTABLE void setPluginEnabled(const QString& pluginName, bool enabled);
86     Q_SCRIPTABLE bool isPluginEnabled(const QString& pluginName) const;
87 
88     void cleanUnneededLinks();
89 
90     int protocolVersion();
91     QStringList supportedPlugins() const;
92 
93     QHostAddress getLocalIpAddress() const;
94 
95 public Q_SLOTS:
96     ///sends a @p np packet to the device
97     ///virtual for testing purposes.
98     virtual bool sendPacket(NetworkPacket& np);
99 
100     //Dbus operations
101 public Q_SLOTS:
102     Q_SCRIPTABLE void requestPair(); //to all links
103     Q_SCRIPTABLE void unpair(); //from all links
104     Q_SCRIPTABLE void reloadPlugins(); //from kconf
105 
106     Q_SCRIPTABLE void acceptPairing();
107     Q_SCRIPTABLE void rejectPairing();
108     Q_SCRIPTABLE bool hasPairingRequests() const;
109 
110     Q_SCRIPTABLE QString pluginIconName(const QString& pluginName);
111 private Q_SLOTS:
112     void privateReceivedPacket(const NetworkPacket& np);
113     void linkDestroyed(QObject* o);
114     void pairStatusChanged(DeviceLink::PairStatus current);
115     void addPairingRequest(PairingHandler* handler);
116     void removePairingRequest(PairingHandler* handler);
117 
118 Q_SIGNALS:
119     Q_SCRIPTABLE void pluginsChanged();
120     Q_SCRIPTABLE void reachableChanged(bool reachable);
121     Q_SCRIPTABLE void trustedChanged(bool trusted);
122     Q_SCRIPTABLE void pairingError(const QString& error);
123     Q_SCRIPTABLE void nameChanged(const QString& name);
124     Q_SCRIPTABLE void typeChanged(const QString& type);
125     Q_SCRIPTABLE void statusIconNameChanged();
126 
127     Q_SCRIPTABLE void hasPairingRequestsChanged(bool hasPairingRequests);
128 
129 private: //Methods
130     static DeviceType str2type(const QString& deviceType);
131     static QString type2str(DeviceType deviceType);
132 
133     void setName(const QString& name);
134     void setType(const QString& strtype);
135     QString iconForStatus(bool reachable, bool paired) const;
136     QSslCertificate certificate() const;
137 
138 private:
139     class DevicePrivate;
140     DevicePrivate *d;
141 };
142 
143 Q_DECLARE_METATYPE(Device*)
144 
145 #endif // DEVICE_H
146