File indexing completed on 2024-04-21 13:10:59

0001 /*
0002     SPDX-FileCopyrightText: 2013-2014 Jan Grulich <jgrulich@redhat.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #ifndef PLASMA_NM_HANDLER_H
0008 #define PLASMA_NM_HANDLER_H
0009 
0010 #include <QDBusInterface>
0011 #include <QPointer>
0012 #include <QTimer>
0013 
0014 #include <ModemManagerQt/GenericTypes>
0015 #include <NetworkManagerQt/Connection>
0016 #include <NetworkManagerQt/ConnectionSettings>
0017 #include <NetworkManagerQt/Settings>
0018 #include <NetworkManagerQt/Utils>
0019 
0020 class Q_DECL_EXPORT Handler : public QObject
0021 {
0022     Q_OBJECT
0023 
0024 public:
0025     enum HandlerAction {
0026         ActivateConnection,
0027         AddAndActivateConnection,
0028         AddConnection,
0029         DeactivateConnection,
0030         RemoveConnection,
0031         RequestScan,
0032         UpdateConnection,
0033         CreateHotspot,
0034     };
0035 
0036     explicit Handler(QObject *parent = nullptr);
0037     ~Handler() override;
0038 
0039     Q_PROPERTY(bool hotspotSupported READ hotspotSupported NOTIFY hotspotSupportedChanged)
0040 
0041     // Not scientifically correct, but a good estimation of whether a scanning is currently in progress.
0042     Q_PROPERTY(bool scanning READ isScanning NOTIFY scanningChanged)
0043 
0044 public:
0045     bool hotspotSupported() const
0046     {
0047         return m_hotspotSupported;
0048     }
0049 
0050     bool isScanning() const
0051     {
0052         return m_ongoingScansCount != 0;
0053     }
0054 
0055 public Q_SLOTS:
0056     /**
0057      * Activates given connection
0058      * @connection - d-bus path of the connection you want to activate
0059      * @device - d-bus path of the device where the connection should be activated
0060      * @specificParameter - d-bus path of the specific object you want to use for this activation, i.e access point
0061      */
0062     void activateConnection(const QString &connection, const QString &device, const QString &specificParameter);
0063     /**
0064      * Adds and activates a new wireless connection
0065      * @device - d-bus path of the wireless device where the connection should be activated
0066      * @specificParameter - d-bus path of the accesspoint you want to connect to
0067      * @password - pre-filled password which should be used for the new wireless connection
0068      * @autoConnect - boolean value whether this connection should be activated automatically when it's available
0069      *
0070      * Works automatically for wireless connections with WEP/WPA security, for wireless connections with WPA/WPA
0071      * it will open the connection editor for advanced configuration.
0072      * */
0073     void addAndActivateConnection(const QString &device, const QString &specificParameter, const QString &password = QString());
0074 
0075     /**
0076      * Request a code that includes the credentials to a said wifi connection
0077      * Here's some information on how this information is created, it's generally used to put in QR codes to share.
0078      * https://github.com/zxing/zxing/wiki/Barcode-Contents#wi-fi-network-config-android-ios-11
0079      *
0080      * @param connectionPath the d-bus path to the connection we want to read
0081      * @param ssid the name of the network being displayed
0082      * @param securityType the authentication protocol used for this specific ssid
0083      * @param connectionName the name of the connection
0084      * @see wifiCodeReceived
0085      */
0086     void requestWifiCode(const QString &connectionPath,
0087                          const QString &ssid,
0088                          /*NetworkManager::WirelessSecurityType*/ int securityType,
0089                          const QString &connectionName);
0090 
0091     /**
0092      * Adds a new connection
0093      * @map - NMVariantMapMap with connection settings
0094      */
0095     void addConnection(const NMVariantMapMap &map);
0096 
0097     void addConnection(NMConnection *connection);
0098 
0099     /**
0100      * Deactivates given connection
0101      * @connection - d-bus path of the connection you want to deactivate
0102      * @device - d-bus path of the connection where the connection is activated
0103      */
0104     void deactivateConnection(const QString &connection, const QString &device);
0105     /**
0106      * Disconnects all connections
0107      */
0108     void disconnectAll();
0109     void enableAirplaneMode(bool enable);
0110     void enableNetworking(bool enable);
0111     void enableWireless(bool enable);
0112 
0113     void enableWwan(bool enable);
0114 
0115     /**
0116      * Removes given connection
0117      * @connection - d-bus path of the connection you want to edit
0118      */
0119     void removeConnection(const QString &connection);
0120     /**
0121      * Updates given connection
0122      * @connection - connection which should be updated
0123      * @map - NMVariantMapMap with new connection settings
0124      */
0125     void updateConnection(const NetworkManager::Connection::Ptr &connection, const NMVariantMapMap &map);
0126     void requestScan(const QString &interface = QString());
0127 
0128     void createHotspot();
0129     void stopHotspot();
0130 
0131 private Q_SLOTS:
0132     void secretAgentError(const QString &connectionPath, const QString &message);
0133     void replyFinished(QDBusPendingCallWatcher *watcher);
0134     void hotspotCreated(QDBusPendingCallWatcher *watcher);
0135     void primaryConnectionTypeChanged(NetworkManager::ConnectionSettings::ConnectionType type);
0136     void unlockRequiredChanged(MMModemLock modemLock);
0137     void slotRequestWifiCode(QDBusPendingCallWatcher *watcher);
0138 
0139 Q_SIGNALS:
0140     void connectionActivationFailed(const QString &connectionPath, const QString &message);
0141     void hotspotCreated();
0142     void hotspotDisabled();
0143     void hotspotSupportedChanged(bool hotspotSupported);
0144     void scanningChanged();
0145     void wifiCodeReceived(const QString &data, const QString &connectionName);
0146 
0147 private:
0148     void addAndActivateConnectionDBus(const NMVariantMapMap &map, const QString &device, const QString &specificObject);
0149 
0150     bool m_hotspotSupported;
0151     bool m_tmpWirelessEnabled;
0152     bool m_tmpWwanEnabled;
0153     QString m_tmpConnectionPath;
0154     QString m_tmpConnectionUuid;
0155     QString m_tmpDevicePath;
0156     QString m_tmpSpecificPath;
0157     QMap<QString, bool> m_bluetoothAdapters;
0158     QMap<QString, QTimer *> m_wirelessScanRetryTimer;
0159     short m_ongoingScansCount = 0;
0160 
0161     void enableBluetooth(bool enable);
0162     void scanRequestFailed(const QString &interface);
0163     bool checkRequestScanRateLimit(const NetworkManager::WirelessDevice::Ptr &wifiDevice);
0164     bool checkHotspotSupported();
0165     void scheduleRequestScan(const QString &interface, int timeout);
0166     void incrementScansCount();
0167     void decrementScansCount();
0168 
0169     QPointer<QDBusPendingCallWatcher> m_requestWifiCodeWatcher;
0170 };
0171 
0172 #endif // PLASMA_NM_HANDLER_H