diff options
author | Teemu Holappa <teemu.holappa@theqtcompany.com> | 2016-02-11 11:50:55 +0200 |
---|---|---|
committer | Teemu Holappa <teemu.holappa@theqtcompany.com> | 2016-02-17 11:57:20 +0000 |
commit | f1d884b6dad5a93d7a3077b6b05d3ec7fcd9a6ea (patch) | |
tree | 9d48669bdf1e8877b19c3a98cd8bbd8c90df5290 /src/networksettings | |
parent | b4088adc7f2666d468a478e379b94c5cb4494c1b (diff) |
Refactored Qml plugins into modules.
Separated C++ and Qml interfaces. All the UI's from plugins are moved to the settingsui folder.
Change-Id: Id6a6623346b18321357bc42d24121c4d9cdfd098
Reviewed-by: Kimmo Ollila <kimmo.ollila@theqtcompany.com>
Diffstat (limited to 'src/networksettings')
45 files changed, 5288 insertions, 0 deletions
diff --git a/src/networksettings/connman.pri b/src/networksettings/connman.pri new file mode 100644 index 0000000..957f2f0 --- /dev/null +++ b/src/networksettings/connman.pri @@ -0,0 +1,22 @@ +QT += core network dbus + +INCLUDEPATH += $${PWD}/connman + +DBUS_INTERFACES = \ + connman/connman_manager.xml \ + connman/connman_service.xml \ + connman/connman_technology.xml + +SOURCES += \ + $$PWD/connman/qnetworksettingsinterface_p.cpp \ + $$PWD/connman/qnetworksettingsservice_p.cpp \ + $$PWD/connman/qnetworksettingsuseragent_p.cpp \ + $$PWD/connman/qnetworksettingsmanager_p.cpp \ + $$PWD/connman/connmancommon.cpp + +HEADERS += \ + $$PWD/connman/qnetworksettingsinterface_p.h \ + $$PWD/connman/qnetworksettingsservice_p.h \ + $$PWD/connman/qnetworksettingsuseragent_p.h \ + $$PWD/connman/qnetworksettingsmanager_p.h \ + $$PWD/connman/connmancommon.h diff --git a/src/networksettings/connman/connman_manager.xml b/src/networksettings/connman/connman_manager.xml new file mode 100644 index 0000000..5c52b27 --- /dev/null +++ b/src/networksettings/connman/connman_manager.xml @@ -0,0 +1,108 @@ +<?xml version="1.0" encoding="utf-8"?> +<node name="/Manager" xmlns:tp="http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0"> + <interface name="net.connman.Manager"> + + <method name="GetProperties" tp:name-for-bindings="Get_Properties"> + <arg name="properties" type="a{sv}" direction="out"/> + <annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="QVariantMap"/> + </method> + + <method name="SetProperty" tp:name-for-bindings="Set_Property"> + <arg name="name" type="s"/> + <arg name="value" type="v"/> + </method> + + <method name="GetTechnologies" tp:name-for-bindings="Get_Technologies"> + <arg name="technologies" type="a(oa{sv})" direction="out"/> + <annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="ConnmanMapList"/> + </method> + + <method name="GetServices" tp:name-for-bindings="Get_Services"> + <arg name="services" type="a(oa{sv})" direction="out"/> + <annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="ConnmanMapList"/> + </method> + + <method name="GetSavedServices" tp:name-for-bindings="Get_Saved_Services"> + <arg name="services" type="a(oa{sv})" direction="out"/> + <annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="ConnmanMapList"/> + </method> + + <method name="RemoveSavedService" tp:name-for-bindings="Remove_Saved_Services"> + <arg name="identifier" type="s" direction="in"/> + </method> + + <method name="RegisterAgent" tp:name-for-bindings="Register_Agent"> + <arg name="path" type="o"/> + </method> + + <method name="UnregisterAgent" tp:name-for-bindings="Unregister_Agent"> + <arg name="path" type="o"/> + </method> + + <method name="RegisterCounter" tp:name-for-bindings="Register_Counter"> + <arg name="path" type="o"/> + <arg name="accuracy" type="u"/> + <arg name="period" type="u"/> + </method> + + <method name="UnregisterCounter" tp:name-for-bindings="Unregister_Counter"> + <arg name="path" type="o"/> + </method> + + <method name="ResetCounters" tp:name-for-bindings="Reset_Counters"> + <arg name="type" type="s"/> + </method> + + <method name="CreateSession" tp:name-for-bindings="Create_Session"> + <arg name="settings" type="a{sv}"/> + <annotation name="org.qtproject.QtDBus.QtTypeName.In0" value="QVariantMap"/> + <arg name="notifier" type="o"/> + <arg name="session" type="o" direction="out"/> + </method> + + <method name="DestroySession" tp:name-for-bindings="Destroy_Session"> + <arg name="session" type="o"/> + </method> + + <method name="RequestPrivateNetwork" tp:name-for-bindings="Request_Private_Network"> + <arg name="options" type="a{sv}"/> + <annotation name="org.qtproject.QtDBus.QtTypeName.In0" value="QVariantMap"/> + <arg name="path" type="o" direction="out"/> + <arg name="dict" type="a{sv}" direction="out"/> + <annotation name="org.qtproject.QtDBus.QtTypeName.Out1" value="QVariantMap"/> + <arg name="fd" type="i" direction="out"/> + <annotation name="org.qtproject.QtDBus.QtTypeName.In0" value="QVariantMap"/> + </method> + + <method name="ReleasePrivateNetwork" tp:name-for-bindings="Release_Private_Network"> + <arg name="path" type="o"/> + </method> + + <signal name="NetworkTypeAdded" tp:name-for-bindings="NetworkType_Added"> + <arg name="technology" type="o"/> + <arg name="properties" type="a{sv}"/> + <annotation name="org.qtproject.QtDBus.QtTypeName.In1" value="QVariantMap"/> + </signal> + + <signal name="NetworkTypeRemoved" tp:name-for-bindings="NetworkType_Removed"> + <arg name="technology" type="o"/> + </signal> + + <signal name="ServicesChanged" tp:name-for-bindings="Services_Changed"> + <arg name="changed" type="a(oa{sv})"/> + <annotation name="org.qtproject.QtDBus.QtTypeName.In0" value="ConnmanMapList"/> + <arg name="removed" type="ao"/> + </signal> + + <signal name="SavedServicesChanged" tp:name-for-bindings="SavedServices_Changed"> + <arg name="changed" type="a(oa{sv})"/> + <annotation name="org.qtproject.QtDBus.QtTypeName.In0" value="ConnmanMapList"/> + </signal> + + <signal name="PropertyChanged" tp:name-for-bindings="Property_Changed"> + <arg name="name" type="s"/> + <arg name="value" type="v"/> + </signal> + + </interface> +</node> diff --git a/src/networksettings/connman/connman_service.xml b/src/networksettings/connman/connman_service.xml new file mode 100644 index 0000000..c41aca4 --- /dev/null +++ b/src/networksettings/connman/connman_service.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="utf-8"?> +<node name="/Service" xmlns:tp="http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0"> + <interface name="net.connman.Service"> + + <method name="GetProperties" tp:name-for-bindings="Get_Properties"> + <arg name="properties" type="a{sv}" direction="out"/> + <annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="QVariantMap"/> + </method> + + <method name="SetProperty" tp:name-for-bindings="Set_Property"> + <arg name="name" type="s"/> + <arg name="value" type="v"/> + </method> + + <method name="ClearProperty" tp:name-for-bindings="Clear_Property"> + <arg name="name" type="s" /> + </method> + + <method name="Connect" tp:name-for-bindings="Connect" /> + + <method name="Disconnect" tp:name-for-bindings="Disconnect" /> + + <method name="Remove" tp:name-for-bindings="Remove" /> + + <method name="MoveBefore" tp:name-for-bindings="Move_Before"> + <arg name="service" type="o"/> + </method> + + <method name="MoveAfter" tp:name-for-bindings="Move_After"> + <arg name="service" type="o"/> + </method> + + <method name="ResetCounters" tp:name-for-bindings="Reset_Counters" /> + + <signal name="PropertyChanged" tp:name-for-bindings="Property_Changed"> + <arg name="name" type="s"/> + <arg name="value" type="v"/> + </signal> + + </interface> +</node> diff --git a/src/networksettings/connman/connman_technology.xml b/src/networksettings/connman/connman_technology.xml new file mode 100644 index 0000000..08d780f --- /dev/null +++ b/src/networksettings/connman/connman_technology.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="utf-8"?> +<node name="/Service" xmlns:tp="http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0"> + <interface name="net.connman.Technology"> + + <method name="GetProperties" tp:name-for-bindings="Get_Properties"> + <arg name="properties" type="a{sv}" direction="out"/> + <annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="QVariantMap"/> + </method> + + <method name="SetProperty" tp:name-for-bindings="Set_Property"> + <arg name="name" type="s"/> + <arg name="value" type="v"/> + </method> + + <method name="Scan" tp:name-for-bindings="Scan"/> + + <signal name="PropertyChanged" tp:name-for-bindings="Property_Changed"> + <arg name="name" type="s"/> + <arg name="value" type="v"/> + </signal> + + </interface> +</node> diff --git a/src/networksettings/connman/connmancommon.cpp b/src/networksettings/connman/connmancommon.cpp new file mode 100644 index 0000000..94a1fa1 --- /dev/null +++ b/src/networksettings/connman/connmancommon.cpp @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "connmancommon.h" + +//Conversion operations for datatypes +const QDBusArgument &operator>>(const QDBusArgument &argument, ConnmanMap &obj) +{ + argument.beginStructure(); + argument >> obj.objectPath >> obj.propertyMap; + argument.endStructure(); + return argument; +} + +QDBusArgument &operator<<(QDBusArgument &argument, const ConnmanMap &obj) +{ + argument.beginStructure(); + argument << obj.objectPath << obj.propertyMap; + argument.endStructure(); + return argument; +} + +const QString &operator>>(const QString &argument, QNetworkSettingsType &obj) +{ + if (argument == AttributeEthernet) { + obj.setType(QNetworkSettingsType::Wired); + } + else if (argument == AttributeWifi) { + obj.setType(QNetworkSettingsType::Wifi); + } + else { + obj.setType(QNetworkSettingsType::Unknown); + } + + return argument; +} + +const QString &operator>>(const QString &argument, QNetworkSettingsState &obj) +{ + if (argument == AttributeIdle) { + obj.setState(QNetworkSettingsState::Idle); + } + else if (argument == AttributeFailure) { + obj.setState(QNetworkSettingsState::Failure); + } + else if (argument == AttributeAssociation) { + obj.setState(QNetworkSettingsState::Association); + } + else if (argument == AttributeConfiguration) { + obj.setState(QNetworkSettingsState::Configuration); + } + else if (argument == AttributeReady) { + obj.setState(QNetworkSettingsState::Ready); + } + else if (argument == AttributeDisconnect) { + obj.setState(QNetworkSettingsState::Disconnect); + } + else if (argument == AttributeOneline) { + obj.setState(QNetworkSettingsState::Online); + } + else { + obj.setState(QNetworkSettingsState::Undefined); + } + return argument; +} diff --git a/src/networksettings/connman/connmancommon.h b/src/networksettings/connman/connmancommon.h new file mode 100644 index 0000000..16850ea --- /dev/null +++ b/src/networksettings/connman/connmancommon.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef CONNMANCOMMON_H +#define CONNMANCOMMON_H + +#include <QtCore/QMetaType> +#include <QtDBus> +#include <qnetworksettings.h> + +#define AgentPath "/ConnmanAgent" +#define PropertyState QStringLiteral("State") +#define PropertyName QStringLiteral("Name") +#define PropertyType QStringLiteral("Type") +#define PropertyConnected QStringLiteral("Connected") +#define PropertyPowered QStringLiteral("Powered") + +#define AttributeWifi QStringLiteral("wifi") +#define AttributeEthernet QStringLiteral("ethernet") +#define AttributeIdle QStringLiteral("idle") +#define AttributeFailure QStringLiteral("failure") +#define AttributeAssociation QStringLiteral("association") +#define AttributeConfiguration QStringLiteral("configuration") +#define AttributeReady QStringLiteral("ready") +#define AttributeDisconnect QStringLiteral("disconnect") +#define AttributeOneline QStringLiteral("online") + +struct ConnmanMap { + QDBusObjectPath objectPath; + QVariantMap propertyMap; +}; + +QDBusArgument &operator<<(QDBusArgument &argument, const ConnmanMap &obj); +const QDBusArgument &operator>>(const QDBusArgument &argument, ConnmanMap &obj); + +const QString &operator>>(const QString &argument, QNetworkSettingsType &obj); +const QString &operator>>(const QString &argument, QNetworkSettingsState &obj); + +typedef QList<ConnmanMap> ConnmanMapList; + +Q_DECLARE_METATYPE( ConnmanMap ) +Q_DECLARE_METATYPE( ConnmanMapList ) + + +#endif // CONNMANCOMMON_H diff --git a/src/networksettings/connman/qnetworksettingsinterface_p.cpp b/src/networksettings/connman/qnetworksettingsinterface_p.cpp new file mode 100644 index 0000000..dda05be --- /dev/null +++ b/src/networksettings/connman/qnetworksettingsinterface_p.cpp @@ -0,0 +1,105 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qnetworksettingsinterface_p.h" +#include "qnetworksettingsinterface.h" +#include "connman_technology_interface.h" +#include "connmancommon.h" + +QNetworkSettingsInterfacePrivate::QNetworkSettingsInterfacePrivate(QNetworkSettingsInterface* parent) + : QObject(parent) + ,q_ptr(parent) +{ + +} + +void QNetworkSettingsInterfacePrivate::initialize(const QString& path, const QVariantMap& properties) +{ + m_technology = new NetConnmanTechnologyInterface(QStringLiteral("net.connman"), path, + QDBusConnection::systemBus(), this); + connect(m_technology, SIGNAL(PropertyChanged(QString,QDBusVariant)), + this, SLOT(updateProperty(QString,QDBusVariant))); + + updateProperty(PropertyName, properties[PropertyName]); + updateProperty(PropertyType, properties[PropertyType]); + updateProperty(PropertyConnected, properties[PropertyConnected]); + updateProperty(PropertyPowered, properties[PropertyPowered]); +} + +void QNetworkSettingsInterfacePrivate::updateProperty(const QString &name, const QDBusVariant &value) +{ + updateProperty(name, value.variant()); +} + +void QNetworkSettingsInterfacePrivate::updateProperty(const QString &name, const QVariant &value) +{ + Q_Q(QNetworkSettingsInterface); + if (name == PropertyName) { + m_name = qdbus_cast<QString>(value); + } + else if (name == PropertyType) { + qdbus_cast<QString>(value) >> m_type; + emit q->typeChanged(); + } + else if (name == PropertyConnected) { + bool connected = qdbus_cast<bool>(value); + if (connected) + m_state.setState(QNetworkSettingsState::Online); + else + m_state.setState(QNetworkSettingsState::Disconnect); + emit q->stateChanged(); + } + else if (name == PropertyPowered) { + m_powered = qdbus_cast<bool>(value); + emit q->poweredChanged(); + } +} + +void QNetworkSettingsInterfacePrivate::setState(QNetworkSettingsState::States aState) +{ + Q_Q(QNetworkSettingsInterface); + m_state.setState(aState); + emit q->stateChanged(); +} + +void QNetworkSettingsInterfacePrivate::setPowered(const bool aPowered) +{ + m_technology->SetProperty(PropertyPowered, QDBusVariant(QVariant(aPowered))); +} + +void QNetworkSettingsInterfacePrivate::scan() +{ + m_technology->Scan(); +} diff --git a/src/networksettings/connman/qnetworksettingsinterface_p.h b/src/networksettings/connman/qnetworksettingsinterface_p.h new file mode 100644 index 0000000..6e1d5ca --- /dev/null +++ b/src/networksettings/connman/qnetworksettingsinterface_p.h @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QNETWORKSETTINGSINTERFACEPRIVATE_H +#define QNETWORKSETTINGSINTERFACEPRIVATE_H + +#include <QObject> +#include <QtDBus> +#include "qnetworksettings.h" +#include "qnetworksettingsinterface.h" + +class NetConnmanTechnologyInterface; + +class QNetworkSettingsInterfacePrivate : public QObject +{ + Q_OBJECT + Q_DECLARE_PUBLIC(QNetworkSettingsInterface) +public: + explicit QNetworkSettingsInterfacePrivate(QNetworkSettingsInterface* parent); + void initialize(const QString& path, const QVariantMap& properties); + void setPowered(const bool power); + void setState(QNetworkSettingsState::States aState); + void scan(); + +public slots: + void updateProperty(const QString &name, const QDBusVariant &value); +protected: + void updateProperty(const QString &name, const QVariant &value); + +protected: + NetConnmanTechnologyInterface *m_technology; + QString m_name; + QNetworkSettingsType m_type; + QNetworkSettingsState m_state; + bool m_powered; + QNetworkSettingsInterface *q_ptr; +}; + + +class ConnmanSettingsInterface : public QNetworkSettingsInterface +{ + Q_OBJECT +public: + ConnmanSettingsInterface(const QString& path, const QVariantMap& properties, QObject *parent=0) + :QNetworkSettingsInterface(parent) + { + if (d_ptr) + d_ptr->initialize(path, properties); + } + + void setState(QNetworkSettingsState::States aState) { + Q_D(QNetworkSettingsInterface); + d->setState(aState); + } + + virtual ~ConnmanSettingsInterface() { + + } +}; +#endif // QNETWORKSETTINGSINTERFACEPRIVATE_H diff --git a/src/networksettings/connman/qnetworksettingsmanager_p.cpp b/src/networksettings/connman/qnetworksettingsmanager_p.cpp new file mode 100644 index 0000000..64b4341 --- /dev/null +++ b/src/networksettings/connman/qnetworksettingsmanager_p.cpp @@ -0,0 +1,171 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qnetworksettingsmanager_p.h" +#include "connman_manager_interface.cpp" +#include "moc_connman_manager_interface.cpp" +#include "qnetworksettingsinterface.h" +#include "qnetworksettingsinterface_p.h" +#include "qnetworksettingsservicemodel.h" + +QNetworkSettingsManagerPrivate::QNetworkSettingsManagerPrivate(QNetworkSettingsManager *parent) + :QObject(parent) + ,q_ptr(parent) +{ + m_serviceModel = new QNetworkSettingsServiceModel(this); + m_serviceFilter = new QNetworkSettingsServiceFilter(this); + m_serviceFilter->setSourceModel(m_serviceModel); + m_manager = new NetConnmanManagerInterface(QStringLiteral("net.connman"), QStringLiteral("/"), + QDBusConnection::systemBus(), this); + + if (m_manager->isValid()) { + //List technologies + QDBusPendingReply<ConnmanMapList> reply = m_manager->GetTechnologies(); + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this); + connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), + this, SLOT(getTechnologiesFinished(QDBusPendingCallWatcher*))); + + reply = m_manager->GetServices(); + watcher = new QDBusPendingCallWatcher(reply, this); + connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), + this, SLOT(getServicesFinished(QDBusPendingCallWatcher*))); + + connect(m_manager, &NetConnmanManagerInterface::ServicesChanged, this, &QNetworkSettingsManagerPrivate::servicesChanged); + + m_manager->RegisterAgent(QDBusObjectPath(AgentPath)); + } + else { + delete m_manager; + m_manager = NULL; + } +} + +void QNetworkSettingsManagerPrivate::requestInput(const QString& service, const QString& type) +{ + Q_UNUSED(type); + foreach (QNetworkSettingsService* servicePtr, m_serviceModel->getModel()) { + if (servicePtr->id() == service) + emit servicePtr->showCrendentialInput(); + } +} + +void QNetworkSettingsManagerPrivate::getServicesFinished(QDBusPendingCallWatcher *watcher) +{ + Q_Q(QNetworkSettingsManager); + QDBusPendingReply<ConnmanMapList> reply = *watcher; + watcher->deleteLater(); + if (reply.isError()) + return; + + foreach (const ConnmanMap &object, reply.value()) { + const QString servicePath = object.objectPath.path(); + QNetworkSettingsService *service = new QNetworkSettingsService(servicePath, this); + m_serviceModel->append(service); + + //Update status property + QString n = qdbus_cast<QString>(object.propertyMap[PropertyName]); + QString t = qdbus_cast<QString>(object.propertyMap[PropertyType]); + QString s = qdbus_cast<QString>(object.propertyMap[PropertyState]); + + QNetworkSettingsType type; + t >> type; + QNetworkSettingsState state; + s >> state; + + foreach (QNetworkSettingsInterface* item, m_interfaceModel.getModel()) { + ConnmanSettingsInterface* technology = qobject_cast<ConnmanSettingsInterface*>(item); + if (technology->name() == n && technology->type() == type.type()) { + technology->setState(state.state()); + } + } + } + emit q->servicesChanged(); +} + +void QNetworkSettingsManagerPrivate::getTechnologiesFinished(QDBusPendingCallWatcher *watcher) +{ + Q_Q(QNetworkSettingsManager); + + QDBusPendingReply<ConnmanMapList> reply = *watcher; + watcher->deleteLater(); + if (reply.isError()) + return; + + foreach (const ConnmanMap &object, reply.value()) { + ConnmanSettingsInterface *item = new ConnmanSettingsInterface(object.objectPath.path(), object.propertyMap, this); + + if (item->type() == QNetworkSettingsType::Wired) { + m_interfaceModel.insert(0, item); + } + else if (item->type() == QNetworkSettingsType::Wifi) { + m_interfaceModel.append(item); + } + emit q->interfacesChanged(); + } +} + +void QNetworkSettingsManagerPrivate::servicesChanged(ConnmanMapList changed, const QList<QDBusObjectPath> &removed) +{ + foreach (QDBusObjectPath path, removed) { + QList<QNetworkSettingsService*> serviceList = m_serviceModel->getModel(); + QMutableListIterator<QNetworkSettingsService*> i(serviceList); + while (i.hasNext()) { + if (i.next()->id() == path.path()) + i.remove(); + } + } + + QStringList newServices; + foreach (ConnmanMap map, changed) { + bool found = false; + foreach (QNetworkSettingsService* service, m_serviceModel->getModel()) { + if (service->id() == map.objectPath.path()) { + found =true; + break; + } + } + if (!found) + newServices.append(map.objectPath.path()); + } + foreach (QString newService, newServices) { + QNetworkSettingsService *service = new QNetworkSettingsService(newService, this); + m_serviceModel->append(service); + } +} + +void QNetworkSettingsManagerPrivate::setUserAgent(QNetworkSettingsUserAgent *agent) +{ + Q_UNUSED(agent); +} diff --git a/src/networksettings/connman/qnetworksettingsmanager_p.h b/src/networksettings/connman/qnetworksettingsmanager_p.h new file mode 100644 index 0000000..3176c98 --- /dev/null +++ b/src/networksettings/connman/qnetworksettingsmanager_p.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QNETWORKSETTINGSMANAGERPRIVATE_H +#define QNETWORKSETTINGSMANAGERPRIVATE_H + +#include <QObject> +#include <QtDBus> +#include "connmancommon.h" +#include "qnetworksettingsmanager.h" +#include "qnetworksettingsinterfacemodel.h" + +class NetConnmanManagerInterface; +class QNetworkSettingsServiceModel; +class QNetworkSettingsServiceFilter; + +class QNetworkSettingsManagerPrivate : public QObject +{ + Q_OBJECT + Q_DECLARE_PUBLIC(QNetworkSettingsManager) +public: + explicit QNetworkSettingsManagerPrivate(QNetworkSettingsManager *parent); + QNetworkSettingsManager *q_ptr; + void setUserAgent(QNetworkSettingsUserAgent *agent); + +public slots: + void getServicesFinished(QDBusPendingCallWatcher *watcher); + void getTechnologiesFinished(QDBusPendingCallWatcher *watcher); + void requestInput(const QString& service, const QString& type); + void servicesChanged(ConnmanMapList changed, const QList<QDBusObjectPath> &removed); + +protected: + QNetworkSettingsInterfaceModel m_interfaceModel; + QNetworkSettingsServiceModel *m_serviceModel; + QNetworkSettingsServiceFilter *m_serviceFilter; +private: + NetConnmanManagerInterface *m_manager; +}; + +#endif // QNETWORKSETTINGSMANAGERPRIVATE_H diff --git a/src/networksettings/connman/qnetworksettingsservice_p.cpp b/src/networksettings/connman/qnetworksettingsservice_p.cpp new file mode 100644 index 0000000..b4e33a3 --- /dev/null +++ b/src/networksettings/connman/qnetworksettingsservice_p.cpp @@ -0,0 +1,402 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qnetworksettingsservice_p.h" +#include "connman_service_interface.h" +#include "connmancommon.h" + +const QString PropertyIPv4(QStringLiteral("IPv4")); +const QString PropertyQNetworkSettingsIPv4(QStringLiteral("IPv4.Configuration")); +const QString PropertyIPv6(QStringLiteral("IPv6")); +const QString PropertyQNetworkSettingsIPv6(QStringLiteral("IPv6.Configuration")); +const QString PropertyNameservers(QStringLiteral("Nameservers")); +const QString PropertyNameserversConfig(QStringLiteral("Nameservers.Configuration")); +const QString PropertyDomains(QStringLiteral("Domains")); +const QString PropertyDomainsConfig(QStringLiteral("Domains.Configuration")); +const QString PropertyProxy(QStringLiteral("Proxy")); +const QString PropertyQNetworkSettingsProxy(QStringLiteral("Proxy.Configuration")); +const QString PropertyAddress(QStringLiteral("Address")); +const QString PropertyNetMask(QStringLiteral("Netmask")); +const QString PropertyGateway(QStringLiteral("Gateway")); +const QString PropertyPrefixLength(QStringLiteral("PrefixLength")); +const QString PropertyMethod(QStringLiteral("Method")); +const QString PropertyPrivacy(QStringLiteral("Privacy")); +const QString PropertyUrl(QStringLiteral("Url")); +const QString PropertyServers(QStringLiteral("Servers")); +const QString PropertyExcludes(QStringLiteral("Excludes")); +const QString PropertyStrength(QStringLiteral("Strength")); +const QString PropertySecurity(QStringLiteral("Security")); + +const QString AttributeAuto(QStringLiteral("auto")); +const QString AttributeDhcp(QStringLiteral("dhcp")); +const QString AttributeManual(QStringLiteral("manual")); +const QString AttributeOff(QStringLiteral("off")); +const QString AttributeDisabled(QStringLiteral("disabled")); +const QString AttributeEnabled(QStringLiteral("enabled")); +const QString AttributePreferred(QStringLiteral("preferred")); +const QString AttributeDirect(QStringLiteral("direct")); +const QString AttributeNone(QStringLiteral("none")); +const QString AttributeWep(QStringLiteral("wep")); +const QString AttributePsk(QStringLiteral("psk")); +const QString AttributeIeee(QStringLiteral("ieee8021x")); +const QString AttributeWps(QStringLiteral("wps")); +const QString AttributeInvalidKey(QStringLiteral("invalid-key")); + +const QVariantMap &operator>>(const QVariantMap &argument, QNetworkSettingsIPv4 &obj) +{ + obj.setAddress(argument[PropertyAddress].toString()); + obj.setMask(argument[PropertyNetMask].toString()); + obj.setGateway(argument[PropertyGateway].toString()); + + QString method = argument[PropertyMethod].toString(); + + if (method == AttributeDhcp) { + obj.setMethod(QNetworkSettingsIPv4::Dhcp); + } + else if (method == AttributeManual) { + obj.setMethod(QNetworkSettingsIPv4::Manual); + } + else if (method == AttributeOff) { + obj.setMethod(QNetworkSettingsIPv4::Off); + } + else { + obj.setMethod(QNetworkSettingsIPv4::Off); + } + return argument; +} + +const QVariantMap &operator<<(QVariantMap &argument, const QNetworkSettingsIPv4 &obj) +{ + argument.insert(PropertyAddress, QVariant::fromValue(obj.address())); + + argument.insert(PropertyNetMask, QVariant::fromValue(obj.mask())); + + argument.insert(PropertyGateway, QVariant::fromValue(obj.gateway())); + + QString method; + switch (obj.method()) { + case QNetworkSettingsIPv4::Dhcp: + method = AttributeDhcp; + break; + case QNetworkSettingsIPv4::Manual: + method = AttributeManual; + break; + case QNetworkSettingsIPv4::Off: + method = AttributeOff; + break; + default: + break; + } + argument.insert(PropertyMethod, QVariant::fromValue(method)); + return argument; +} + +const QVariantMap &operator>>(const QVariantMap &argument, QNetworkSettingsIPv6 &obj) +{ + obj.setAddress(argument[PropertyAddress].toString()); + obj.setGateway(argument[PropertyGateway].toString()); + obj.setPrefixLength(argument[PropertyPrefixLength].toInt()); + QString method = argument[PropertyMethod].toString(); + if (method == AttributeAuto) { + obj.setMethod(QNetworkSettingsIPv6::Auto); + } + else if (method == AttributeManual) { + obj.setMethod(QNetworkSettingsIPv6::Manual); + } + else if (method == AttributeOff) { + obj.setMethod(QNetworkSettingsIPv6::Off); + } + QString privacy = argument[PropertyPrivacy].toString(); + + if (privacy == AttributeDisabled) { + obj.setPrivacy(QNetworkSettingsIPv6::Disabled); + } + else if (privacy == AttributeEnabled) { + obj.setPrivacy(QNetworkSettingsIPv6::Enabled); + } + else if (privacy == AttributePreferred) { + obj.setPrivacy(QNetworkSettingsIPv6::Preferred); + } + return argument; +} + + +const QVariantMap &operator<<(QVariantMap &argument, const QNetworkSettingsIPv6 &obj) +{ + argument.insert(PropertyAddress, QVariant::fromValue(obj.address())); + argument.insert(PropertyGateway, QVariant::fromValue(obj.address())); + argument.insert(PropertyPrefixLength, QVariant::fromValue(obj.prefixLength())); + + QString method; + switch (obj.method()) { + case QNetworkSettingsIPv6::Auto: + method = AttributeAuto; + break; + case QNetworkSettingsIPv6::Manual: + method = AttributeManual; + break; + case QNetworkSettingsIPv6::Off: + method = AttributeOff; + break; + default: + break; + } + argument.insert(PropertyMethod, QVariant::fromValue(method)); + + QString privacy; + switch (obj.privacy()) { + case QNetworkSettingsIPv6::Disabled: + method = AttributeDisabled; + break; + case QNetworkSettingsIPv6::Enabled: + method = AttributeEnabled; + break; + case QNetworkSettingsIPv6::Preferred: + method = AttributePreferred; + break; + default: + break; + } + argument.insert(PropertyPrivacy, QVariant::fromValue(privacy)); + + return argument; +} + + +const QVariantMap &operator>>(const QVariantMap &argument, QNetworkSettingsProxy &obj) +{ + QString method = argument[PropertyMethod].toString(); + if (method == AttributeDirect) { + obj.setMethod(QNetworkSettingsProxy::Direct); + } + else if (method == AttributeAuto) { + obj.setMethod(QNetworkSettingsProxy::Auto); + } + else if (method == AttributeManual) { + obj.setMethod(QNetworkSettingsProxy::Manual); + } + + QString urlStr = argument[PropertyUrl].toString(); + obj.setUrl(QUrl(urlStr)); + + obj.setServers(argument[PropertyServers].toStringList()); + obj.setExcludes(argument[PropertyExcludes].toStringList()); + + return argument; +} + +const QVariantMap &operator<<(QVariantMap &argument, const QNetworkSettingsProxy &obj) +{ + QString method; + switch (obj.method()) { + case QNetworkSettingsProxy::Direct: + method = AttributeDirect; + break; + case QNetworkSettingsProxy::Auto: + method = AttributeAuto; + break; + case QNetworkSettingsProxy::Manual: + method = AttributeManual; + break; + default: + break; + } + argument.insert(PropertyMethod, QVariant::fromValue(method)); + argument.insert(PropertyUrl, QVariant::fromValue(obj.url().toString())); + argument.insert(PropertyServers, QVariant::fromValue(obj.servers())); + argument.insert(PropertyServers, QVariant::fromValue(obj.excludes())); + + return argument; +} + +QNetworkSettingsServicePrivate::QNetworkSettingsServicePrivate(const QString& id, QNetworkSettingsService *parent) : + QObject(parent) + ,q_ptr(parent) + ,m_id(id) +{ + qDBusRegisterMetaType<ConnmanMap>(); + qDBusRegisterMetaType<ConnmanMapList>(); + + m_service = new NetConnmanServiceInterface(QStringLiteral("net.connman"), m_id, + QDBusConnection::systemBus(), this); + + connect(m_service, SIGNAL(PropertyChanged(QString,QDBusVariant)), + this, SLOT(updateProperty(QString,QDBusVariant))); + + QDBusPendingReply<QVariantMap> reply = m_service->GetProperties(); + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this); + + connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), + this, SLOT(propertiesUpdated(QDBusPendingCallWatcher*))); +} + +void QNetworkSettingsServicePrivate::setupIpv6Config() +{ + QVariantMap param; + param << m_ipv6config; + if (m_service) + m_service->SetProperty(PropertyQNetworkSettingsIPv6, QDBusVariant(QVariant(param))); +} + +void QNetworkSettingsServicePrivate::setupNameserversConfig() +{ + QVariant param = QVariant::fromValue(m_nameserverConfig.stringList()); + if (m_service) + m_service->SetProperty(PropertyNameserversConfig, QDBusVariant(QVariant(param))); +} + +void QNetworkSettingsServicePrivate::setupDomainsConfig() +{ + QVariant param = QVariant::fromValue(m_domainsConfig.stringList()); + if (m_service) + m_service->SetProperty(PropertyDomainsConfig, QDBusVariant(QVariant(param))); +} + +void QNetworkSettingsServicePrivate::setupQNetworkSettingsProxy() +{ + QVariantMap param; + param << m_proxyConfig; + if (m_service) + m_service->SetProperty(PropertyQNetworkSettingsProxy, QDBusVariant(QVariant(param))); +} + +void QNetworkSettingsServicePrivate::setupIpv4Config() +{ + QVariantMap param; + param << m_ipv4config; + if (m_service) + m_service->SetProperty(PropertyQNetworkSettingsIPv4, QDBusVariant(QVariant(param))); +} + +void QNetworkSettingsServicePrivate::connectService() +{ + if (m_service) { + m_service->Connect(); + } +} + +void QNetworkSettingsServicePrivate::disconnectService() +{ + if (m_service) + m_service->Disconnect(); + +} + +void QNetworkSettingsServicePrivate::propertiesUpdated(QDBusPendingCallWatcher *call) +{ + QDBusPendingReply<QVariantMap> reply = *call; + call->deleteLater(); + + if (!reply.isError()) + setupConfiguration(reply.value()); +} + +void QNetworkSettingsServicePrivate::updateProperty(const QString &name, const QDBusVariant &value) +{ + updateProperty(name, value.variant()); +} + +void QNetworkSettingsServicePrivate::updateProperty(const QString& key, const QVariant& val) +{ + Q_Q(QNetworkSettingsService); + + if (key == PropertyIPv4) { + QVariantMap value = qdbus_cast<QVariantMap>(val); + value >> m_ipv4config; + emit q->ipv4Changed(); + } + else if (key == PropertyIPv6) { + QVariantMap value = qdbus_cast<QVariantMap>(val); + value >> m_ipv6config; + emit q->ipv6Changed(); + } + else if (key == PropertyNameservers) { + QStringList value = qdbus_cast<QStringList>(val); + m_nameserverConfig.setStringList(value); + emit q->nameserversChanged(); + } + else if (key == PropertyDomains) { + QStringList value = qdbus_cast<QStringList>(val); + m_domainsConfig.setStringList(value); + emit q->domainsChanged(); + } + else if (key == PropertyProxy) { + QVariantMap value = qdbus_cast<QVariantMap>(val); + value >> m_proxyConfig; + emit q->proxyChanged(); + } + else if (key == PropertyState) { + QString value = qdbus_cast<QString>(val); + value >> m_state; + emit q->stateChanged(); + } + else if (key == PropertyName) { + m_name = qdbus_cast<QString>(val); + emit q->nameChanged(); + } + else if (key == PropertyType) { + QString value = qdbus_cast<QString>(val); + value >> m_type; + } + else if (key == PropertyStrength) { + m_wifiConfig.setSignalStrength(val.toInt()); + } + else if (key == PropertySecurity) { + QStringList value = qdbus_cast<QStringList>(val); + foreach (const QString str, value) { + if (str == AttributeNone || str == AttributeWps) { + m_wifiConfig.setSecurity(QNetworkSettingsWireless::None); + } + else if (str == AttributeWep) { + m_wifiConfig.setSecurity(QNetworkSettingsWireless::WEP); + } + else if (str == AttributePsk) { + m_wifiConfig.setSecurity(QNetworkSettingsWireless::WPA); + } + else if (str == AttributeIeee) { + m_wifiConfig.setSecurity(QNetworkSettingsWireless::WPA2); + } + } + } +} + +void QNetworkSettingsServicePrivate::setupConfiguration(const QVariantMap &properties) +{ + QVariantMap::const_iterator it = properties.constBegin(), end = properties.constEnd(); + for ( ; it != end; ++it) { + updateProperty(it.key(), it.value()); + } +} + diff --git a/src/networksettings/connman/qnetworksettingsservice_p.h b/src/networksettings/connman/qnetworksettingsservice_p.h new file mode 100644 index 0000000..39a04bd --- /dev/null +++ b/src/networksettings/connman/qnetworksettingsservice_p.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QNETWORKSETTINGSSERVICEPRIVATE_H +#define QNETWORKSETTINGSSERVICEPRIVATE_H + +#include <QtDBus> +#include "qnetworksettingsservice.h" +#include "qnetworksettings.h" + +class NetConnmanServiceInterface; + +class QNetworkSettingsServicePrivate : public QObject +{ + Q_OBJECT + Q_DECLARE_PUBLIC(QNetworkSettingsService) +public: + QNetworkSettingsServicePrivate(const QString& aPath, QNetworkSettingsService *parent=0); + + QNetworkSettingsService *q_ptr; +private slots: + void propertiesUpdated(QDBusPendingCallWatcher *call); + void updateProperty(const QString &name, const QDBusVariant &value); + +private: + void setupConfiguration(const QVariantMap &properties); + void updateProperty(const QString& key, const QVariant& value); + +protected: + void setAutoConnect(const bool autoconnect); + void setupIpv4Config(); + void setupIpv6Config(); + void setupNameserversConfig(); + void setupDomainsConfig(); + void setupQNetworkSettingsProxy(); + void connectService(); + void disconnectService(); + + QString m_id; + QString m_name; + QNetworkSettingsState m_state; + QNetworkSettingsIPv4 m_ipv4config; + QNetworkSettingsIPv6 m_ipv6config; + QNetworkSettingsAddressModel m_domainsConfig; + QNetworkSettingsAddressModel m_nameserverConfig; + QNetworkSettingsProxy m_proxyConfig; + QNetworkSettingsWireless m_wifiConfig; + QNetworkSettingsType m_type; + NetConnmanServiceInterface *m_service; +}; + +#endif // QNETWORKSETTINGSSERVICEPRIVATE_H diff --git a/src/networksettings/connman/qnetworksettingsuseragent_p.cpp b/src/networksettings/connman/qnetworksettingsuseragent_p.cpp new file mode 100644 index 0000000..514745e --- /dev/null +++ b/src/networksettings/connman/qnetworksettingsuseragent_p.cpp @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qnetworksettingsuseragent_p.h" +#include "connmancommon.h" + +const QString PropertyPassphrase(QStringLiteral("Passphrase")); + +QNetworkSettingsUserAgentPrivate::QNetworkSettingsUserAgentPrivate(QNetworkSettingsUserAgent *parent) + :QDBusAbstractAdaptor(parent) + ,q_ptr(parent) + ,m_pendingReply(false) +{ + + setAutoRelaySignals(true); + QMetaObject::invokeMethod(this, "registerAgent", Qt::QueuedConnection); +} + +void QNetworkSettingsUserAgentPrivate::cancel() +{ + // handle method call net.connman.Agent.Cancel + m_pendingReply = false; + QMetaObject::invokeMethod(parent(), "Cancel"); +} + +void QNetworkSettingsUserAgentPrivate::release() +{ + // handle method call net.connman.Agent.Release + QMetaObject::invokeMethod(parent(), "Release"); +} + +void QNetworkSettingsUserAgentPrivate::ReportError(const QDBusObjectPath &path, const QString ¶m) +{ + Q_Q(QNetworkSettingsUserAgent); + Q_UNUSED(path); + Q_UNUSED(param); + emit q->error(); +} + +void QNetworkSettingsUserAgentPrivate::registerAgent() +{ + Q_Q(QNetworkSettingsUserAgent); + QDBusConnection::systemBus().registerObject(QStringLiteral(AgentPath), q); +} + +QVariantMap QNetworkSettingsUserAgentPrivate::RequestInput(const QDBusObjectPath &path, const QVariantMap ¶ms, const QDBusMessage &msg) +{ + Q_Q(QNetworkSettingsUserAgent); + Q_UNUSED(path); + Q_UNUSED(params); + msg.setDelayedReply(true); + m_reply = msg.createReply(); + m_pendingReply = true; + emit q->showUserCredentialsInput(); + return QVariantMap(); +} + +void QNetworkSettingsUserAgentPrivate::setUserCredentials(const QString& username, + const QString& password) +{ + m_username = username; + m_passphrase = password; + QVariantMap response; + response[PropertyName] = m_username; + response[PropertyPassphrase] = m_passphrase; + m_reply << response; + m_pendingReply = false; + QDBusConnection::systemBus().send(m_reply); +} diff --git a/src/networksettings/connman/qnetworksettingsuseragent_p.h b/src/networksettings/connman/qnetworksettingsuseragent_p.h new file mode 100644 index 0000000..efde5b5 --- /dev/null +++ b/src/networksettings/connman/qnetworksettingsuseragent_p.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QNETWORKSETTINGSUSERAGENTPRIVATE_H +#define QNETWORKSETTINGSUSERAGENTPRIVATE_H + +#include <QtCore/QObject> +#include <QtDBus/QtDBus> +#include "qnetworksettingsuseragent.h" + +QT_BEGIN_NAMESPACE +class QByteArray; +template<class T> class QList; +template<class Key, class Value> class QMap; +class QString; +class QStringList; +class QVariant; +QT_END_NAMESPACE + +const QString AgentPath(QStringLiteral("/ConnmanAgent")); + +class QNetworkSettingsUserAgentPrivate : public QDBusAbstractAdaptor +{ + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "net.connman.Agent") + Q_DECLARE_PUBLIC(QNetworkSettingsUserAgent) +public: + QNetworkSettingsUserAgentPrivate(QNetworkSettingsUserAgent *parent=0); + void setUserCredentials(const QString& aUsername, const QString& aPassword); + void cancel(); + void release(); +public Q_SLOTS: // Dbus methods + void ReportError(const QDBusObjectPath &path, const QString ¶m); + QVariantMap RequestInput(const QDBusObjectPath &path, const QVariantMap ¶ms, + const QDBusMessage &message); + void registerAgent(); +private: + QNetworkSettingsUserAgent *q_ptr; + QDBusMessage m_reply; + bool m_pendingReply; + QString m_passphrase; + QString m_username; +}; + +#endif // QNETWORKSETTINGSUSERAGENTPRIVATE_H diff --git a/src/networksettings/networksettings.pro b/src/networksettings/networksettings.pro new file mode 100644 index 0000000..9ae1bb3 --- /dev/null +++ b/src/networksettings/networksettings.pro @@ -0,0 +1,37 @@ +load(qt_build_config) + +TARGET = QtNetworkSettings +VERSION = 1.0 +CONFIG += dll warn_on + +QT = core network + +MODULE = networksettings +load(qt_module) + +wpasupplicant { + include(wpasupplicant.pri) +} +else { + include(connman.pri) +} + +# Input +SOURCES += \ + qnetworksettingsinterfacemodel.cpp \ + qnetworksettingsmanager.cpp \ + qnetworksettingsaddressmodel.cpp \ + qnetworksettingsservicemodel.cpp \ + qnetworksettingsservice.cpp \ + qnetworksettingsuseragent.cpp \ + qnetworksettingsinterface.cpp \ + +HEADERS += \ + qnetworksettingsinterfacemodel.h \ + qnetworksettings.h \ + qnetworksettingsmanager.h \ + qnetworksettingsaddressmodel.h \ + qnetworksettingsservicemodel.h \ + qnetworksettingsservice.h \ + qnetworksettingsuseragent.h \ + qnetworksettingsinterface.h \ diff --git a/src/networksettings/qnetworksettings.h b/src/networksettings/qnetworksettings.h new file mode 100644 index 0000000..839d5a9 --- /dev/null +++ b/src/networksettings/qnetworksettings.h @@ -0,0 +1,411 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QNETWORKSETTINGS_H +#define QNETWORKSETTINGS_H + +#include <QObject> +#include <QUrl> +#include <QVariantMap> +#include "qnetworksettingsaddressmodel.h" + +class Q_DECL_EXPORT QNetworkSettingsState : public QObject +{ + Q_OBJECT + Q_ENUMS(States) + Q_PROPERTY(States state READ state WRITE setState NOTIFY stateChanged) + +public: + enum States {Idle, Failure, Association, Configuration, Ready, Disconnect, Online, Undefined}; + + explicit QNetworkSettingsState(States state, QObject *parent = 0) + : QObject(parent) + { + m_state = state; + } + + explicit QNetworkSettingsState(QObject *parent = 0) + : QObject(parent) + { + m_state = Undefined; + } + + States state() const { + return m_state; + } + + void setState(const States state) { + m_state = state; + emit stateChanged(); + } + +signals: + void stateChanged(); + +private: + States m_state; +}; + +class Q_DECL_EXPORT QNetworkSettingsType : public QObject +{ + Q_OBJECT + Q_ENUMS(Types) + Q_PROPERTY(Types type READ type WRITE setType NOTIFY typeChanged) + +public: + enum Types { + Wired, + Wifi, + Bluetooth, + Unknown + }; + + explicit QNetworkSettingsType(Types type, QObject *parent = 0) + : QObject(parent) + { + m_type = type; + } + + explicit QNetworkSettingsType(QObject *parent = 0) + : QObject(parent) + { + m_type = Unknown; + } + + + Types type() const { + return m_type; + } + + void setType(const Types type) { + m_type = type; + emit typeChanged(); + } + +signals: + void typeChanged(); + +private: + Types m_type; +}; + +class Q_DECL_EXPORT QNetworkSettingsIPv4 : public QObject +{ + Q_OBJECT + Q_ENUMS(MethodType) + Q_PROPERTY(QString address READ address WRITE setAddress NOTIFY addressChanged) + Q_PROPERTY(QString gateway READ gateway WRITE setGateway NOTIFY gatewayChanged) + Q_PROPERTY(MethodType method READ method WRITE setMethod NOTIFY methodChanged) + Q_PROPERTY(QString mask READ mask WRITE setMask NOTIFY maskChanged) + +public: + explicit QNetworkSettingsIPv4(QObject *parent = 0) + : QObject(parent) + { + + } + + enum MethodType {Dhcp, Manual, Off}; + + QString address() const { + return m_address; + } + void setAddress(const QString& adderess) { + m_address = adderess; + emit addressChanged(); + } + + QString gateway() const { + return m_gateway; + } + + void setGateway(const QString& gateway) { + m_gateway = gateway; + emit gatewayChanged(); + } + + MethodType method() const { + return m_method; + } + + void setMethod(const MethodType method) { + m_method = method; + emit methodChanged(); + } + + QString mask() const { + return m_mask; + } + + void setMask(const QString& mask) { + m_mask = mask; + emit maskChanged(); + } + +signals: + void addressChanged(); + void gatewayChanged(); + void methodChanged(); + void maskChanged(); + +private: + QString m_address; + QString m_gateway; + MethodType m_method; + QString m_mask; +}; + + +class Q_DECL_EXPORT QNetworkSettingsIPv6 : public QObject +{ + Q_OBJECT + Q_ENUMS(MethodType PrivacyType) + Q_PROPERTY(QString address READ address WRITE setAddress NOTIFY addressChanged) + Q_PROPERTY(QString gateway READ gateway WRITE setGateway NOTIFY gatewayChanged) + Q_PROPERTY(MethodType method READ method WRITE setMethod NOTIFY methodChanged) + Q_PROPERTY(PrivacyType privacy READ privacy WRITE setPrivacy NOTIFY privacyChanged) + Q_PROPERTY(int prefixLength READ prefixLength WRITE setPrefixLength NOTIFY prefixLengthChanged) + +public: + explicit QNetworkSettingsIPv6(QObject *parent = 0) + : QObject(parent) + { + + } + + enum MethodType {Auto, Manual, Off}; + enum PrivacyType {Disabled, Enabled, Preferred}; + + QString address() const { + return m_address; + } + void setAddress(const QString& adderess) { + m_address = adderess; + emit addressChanged(); + } + + QString gateway() const { + return m_gateway; + } + + void setGateway(const QString& gateway) { + m_gateway = gateway; + emit gatewayChanged(); + } + + MethodType method() const { + return m_method; + } + + void setMethod(const MethodType method) { + m_method = method; + emit methodChanged(); + } + + PrivacyType privacy() const { + return m_privacy; + } + + void setPrivacy(const PrivacyType privacy) { + m_privacy = privacy; + emit privacyChanged(); + } + + int prefixLength() const { + return m_prefixLength; + } + + void setPrefixLength(const int& prefixLength) { + m_prefixLength = prefixLength; + emit prefixLengthChanged(); + } + +signals: + void addressChanged(); + void gatewayChanged(); + void methodChanged(); + void privacyChanged(); + void prefixLengthChanged(); + +private: + QString m_address; + QString m_gateway; + MethodType m_method; + PrivacyType m_privacy; + int m_prefixLength; +}; + +class Q_DECL_EXPORT QNetworkSettingsProxy : public QObject +{ + Q_OBJECT + Q_ENUMS(MethodType) +public: + Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged) + Q_PROPERTY(QAbstractItemModel* servers READ servers NOTIFY serversChanged) + Q_PROPERTY(QAbstractItemModel* excludes READ excludes NOTIFY excludesChanged) + Q_PROPERTY(MethodType method READ method WRITE setMethod NOTIFY methodChanged) + + explicit QNetworkSettingsProxy(QObject *parent=NULL) + : QObject(parent) + { + + } + + enum MethodType {Direct, Auto, Manual}; + + QUrl url() const { + return m_url; + } + + void setUrl(const QUrl& url) { + m_url = url; + emit urlChanged(); + } + + QAbstractItemModel* servers(void) { + return &m_servers; + } + + void setServers(const QStringList& servers) { + m_servers.setStringList(servers); + emit serversChanged(); + } + + QStringList servers() const { + return m_servers.stringList(); + } + + QAbstractItemModel* excludes(void) { + return &m_excludes; + } + + QStringList excludes() const { + return m_excludes.stringList(); + } + + void setExcludes(const QStringList& excludes) { + m_excludes.setStringList(excludes); + emit excludesChanged(); + } + + MethodType method(void) const { + return m_method; + } + + void setMethod(const MethodType& method) { + m_method = method; + emit methodChanged(); + } + +signals: + void urlChanged(); + void serversChanged(); + void excludesChanged(); + void methodChanged(); +private: + QUrl m_url; + QNetworkSettingsAddressModel m_servers; + QNetworkSettingsAddressModel m_excludes; + MethodType m_method; +}; + +class Q_DECL_EXPORT QNetworkSettingsWireless : public QObject +{ + Q_OBJECT + Q_PROPERTY(int signalStrength READ signalStrength WRITE setSignalStrength NOTIFY signalStrengthChanged) + Q_PROPERTY(bool hidden READ hidden NOTIFY hiddenChanged) + Q_PROPERTY(bool isOutOfRange READ outOfRange WRITE setOutOfRange NOTIFY outOfRangeChanged) +public: + explicit QNetworkSettingsWireless(QObject* parent = 0) : + QObject(parent) { + } + + enum SecurityFlags {None=1, WEP=2, WPA=4, WPA2=8}; + + Q_INVOKABLE bool supportsSecurity(SecurityFlags security) { + if (m_securityFlags & security) { + return true; + } + return false; + } + + bool hidden() const { + return m_hidden; + } + + void setHidden(const bool hidden) { + m_hidden = hidden; + emit hiddenChanged(); + } + + int signalStrength() const { + return m_signalStrength; + } + + void setSignalStrength(const int signalStrength) { + m_signalStrength = signalStrength; + emit signalStrengthChanged(); + } + + void setSecurity(const SecurityFlags security) { + if (security == None) { + m_securityFlags = None; + } + else { + m_securityFlags |= security; + } + } + + void setOutOfRange(const bool aOutOfRange) { + m_isOutOfRange = aOutOfRange; + emit outOfRangeChanged(); + } + + bool outOfRange() const { + return m_isOutOfRange; + } + +signals: + void hiddenChanged(); + void signalStrengthChanged(); + void passwordChanged(); + void outOfRangeChanged(); +private: + quint16 m_securityFlags; + bool m_hidden; + int m_signalStrength; + bool m_isOutOfRange; +}; +#endif //QNETWORKSETTINGS_H diff --git a/src/networksettings/qnetworksettingsaddressmodel.cpp b/src/networksettings/qnetworksettingsaddressmodel.cpp new file mode 100644 index 0000000..b70278b --- /dev/null +++ b/src/networksettings/qnetworksettingsaddressmodel.cpp @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qnetworksettingsaddressmodel.h" + +QNetworkSettingsAddressModel::QNetworkSettingsAddressModel(QObject *parent) + :QStringListModel(parent) +{ +} + +QNetworkSettingsAddressModel::QNetworkSettingsAddressModel(const QStringList &strings, QObject *parent) + :QStringListModel(parent) +{ + setStringList(strings); +} + +void QNetworkSettingsAddressModel::setStringList(const QStringList &addresses) +{ + m_addresses = addresses; + QStringListModel::setStringList(m_addresses); + emit countChanged(); +} + +void QNetworkSettingsAddressModel::append(const QString& address) +{ + int row = rowCount(); + + bool succeed = insertRows(row, 1); + if (succeed) + succeed = setData(index(row), QVariant::fromValue(address)); + + Q_ASSERT(succeed == true); + + emit countChanged(); +} + +void QNetworkSettingsAddressModel::remove(int index) +{ + removeRows(index, 1); + emit countChanged(); +} + +int QNetworkSettingsAddressModel::count() const +{ + return rowCount(); +} + +void QNetworkSettingsAddressModel::resetChanges() +{ + QStringListModel::setStringList(m_addresses); +} diff --git a/src/networksettings/qnetworksettingsaddressmodel.h b/src/networksettings/qnetworksettingsaddressmodel.h new file mode 100644 index 0000000..429443a --- /dev/null +++ b/src/networksettings/qnetworksettingsaddressmodel.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QNETWORKSETTINGSADDRESSMODEL_H +#define QNETWORKSETTINGSADDRESSMODEL_H + +#include <QStringListModel> + +class Q_DECL_EXPORT QNetworkSettingsAddressModel : public QStringListModel +{ + Q_OBJECT + Q_PROPERTY(int count READ count NOTIFY countChanged) +public: + explicit QNetworkSettingsAddressModel(QObject *parent = 0); + explicit QNetworkSettingsAddressModel(const QStringList &strings, QObject *parent = 0); + Q_INVOKABLE void append(const QString& address); + Q_INVOKABLE void remove(int index); + Q_INVOKABLE void resetChanges(); + void setStringList(const QStringList &addresses); + int count() const; + +signals: + void countChanged(); +private: + QStringList m_addresses; +}; + +#endif // QNETWORKSETTINGSADDRESSMODEL_H diff --git a/src/networksettings/qnetworksettingsinterface.cpp b/src/networksettings/qnetworksettingsinterface.cpp new file mode 100644 index 0000000..893311a --- /dev/null +++ b/src/networksettings/qnetworksettingsinterface.cpp @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qnetworksettingsinterface.h" +#include "qnetworksettingsinterface_p.h" +#include "qnetworksettings.h" + +QNetworkSettingsInterface::QNetworkSettingsInterface(QObject *parent) : + QObject(parent) + ,d_ptr(new QNetworkSettingsInterfacePrivate(this)) +{ + +} + +QString QNetworkSettingsInterface::name() const +{ + Q_D(const QNetworkSettingsInterface); + return d->m_name; +} + +QNetworkSettingsState::States QNetworkSettingsInterface::state() +{ + Q_D(QNetworkSettingsInterface); + return d->m_state.state(); +} + +QNetworkSettingsType::Types QNetworkSettingsInterface::type() +{ + Q_D(QNetworkSettingsInterface); + return d->m_type.type(); +} + +bool QNetworkSettingsInterface::powered() const +{ + Q_D(const QNetworkSettingsInterface); + return d->m_powered; +} + +void QNetworkSettingsInterface::setPowered(const bool powered) +{ + Q_D(QNetworkSettingsInterface); + d->setPowered(powered); +} + +void QNetworkSettingsInterface::scanServices() +{ + Q_D(QNetworkSettingsInterface); + d->scan(); +} diff --git a/src/networksettings/qnetworksettingsinterface.h b/src/networksettings/qnetworksettingsinterface.h new file mode 100644 index 0000000..cf52ae3 --- /dev/null +++ b/src/networksettings/qnetworksettingsinterface.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QNETWORKSETTINGSINTERFACE_H +#define QNETWORKSETTINGSINTERFACE_H + +#include <QObject> +#include "qnetworksettings.h" + +QT_FORWARD_DECLARE_CLASS(QNetworkSettingsInterfacePrivate) + +class Q_DECL_EXPORT QNetworkSettingsInterface : public QObject { + Q_OBJECT + Q_PROPERTY(QString name READ name NOTIFY nameChanged) + Q_PROPERTY(QNetworkSettingsState::States state READ state NOTIFY stateChanged) + Q_PROPERTY(QNetworkSettingsType::Types type READ type NOTIFY typeChanged) + Q_PROPERTY(bool powered READ powered WRITE setPowered NOTIFY poweredChanged) + +public: + explicit QNetworkSettingsInterface(QObject* parent=0); + QString name() const; + QNetworkSettingsState::States state(); + QNetworkSettingsType::Types type(); + bool powered() const; + void setPowered(const bool powered); + Q_INVOKABLE void scanServices(); + +signals: + void nameChanged(); + void stateChanged(); + void typeChanged(); + void poweredChanged(); + +protected: + friend class NetworksInterfaceModel; + QNetworkSettingsInterfacePrivate *d_ptr; + + Q_DISABLE_COPY(QNetworkSettingsInterface) + Q_DECLARE_PRIVATE(QNetworkSettingsInterface) +}; + +#endif // QNETWORKSETTINGSINTERFACE_H diff --git a/src/networksettings/qnetworksettingsinterfacemodel.cpp b/src/networksettings/qnetworksettingsinterfacemodel.cpp new file mode 100644 index 0000000..b25787d --- /dev/null +++ b/src/networksettings/qnetworksettingsinterfacemodel.cpp @@ -0,0 +1,113 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qnetworksettingsinterfacemodel.h" +#include "qnetworksettingsinterface.h" + +QNetworkSettingsInterfaceModel::QNetworkSettingsInterfaceModel(QObject *parent) + : QAbstractListModel(parent) +{ + m_roleNames.insert(Qt::UserRole, "modelData"); + m_roleNames.insert(Type, "type"); + m_roleNames.insert(Status, "status"); + m_roleNames.insert(Name, "name"); +} + +QHash<int, QByteArray> QNetworkSettingsInterfaceModel::roleNames() const +{ + return m_roleNames; +} + + +int QNetworkSettingsInterfaceModel::rowCount(const QModelIndex & parent) const +{ + Q_UNUSED(parent); + return m_items.count(); +} + +QVariant QNetworkSettingsInterfaceModel::data(const QModelIndex & index, int role) const +{ + if (!index.isValid()) return QVariant(); + + QNetworkSettingsInterface *item = m_items[index.row()]; + if (role == Qt::UserRole) { + return QVariant::fromValue(static_cast<QObject*>(item)); + } + + switch (role) { + case Type: + return item->type(); + break; + case Name: + return item->name(); + break; + case Status: + return item->state(); + break; + default: + return QStringLiteral(""); + } + +} + +void QNetworkSettingsInterfaceModel::append(QNetworkSettingsInterface* item) +{ + item->setParent(this); + + beginInsertRows(QModelIndex(), rowCount(), rowCount()); + m_items.append(item); + endInsertRows(); +} + +void QNetworkSettingsInterfaceModel::insert(int row, QNetworkSettingsInterface* item) +{ + item->setParent(this); + + beginInsertRows(QModelIndex(), row, row); + m_items.insert(row, item); + endInsertRows(); +} + +void QNetworkSettingsInterfaceModel::remove(int row) +{ + beginRemoveRows(QModelIndex(), row, row); + m_items.removeFirst(); + endRemoveRows(); +} + +QList<QNetworkSettingsInterface*> QNetworkSettingsInterfaceModel::getModel() +{ + return m_items; +} diff --git a/src/networksettings/qnetworksettingsinterfacemodel.h b/src/networksettings/qnetworksettingsinterfacemodel.h new file mode 100644 index 0000000..fb7be45 --- /dev/null +++ b/src/networksettings/qnetworksettingsinterfacemodel.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QNETWORKSETTINGSINTERFACEMODEL_H +#define QNETWORKSETTINGSINTERFACEMODEL_H + +#include <QAbstractListModel> +#include "qnetworksettings.h" + +class QNetworkSettingsInterface; + +class Q_DECL_EXPORT QNetworkSettingsInterfaceModel : public QAbstractListModel +{ + Q_OBJECT + +public: + explicit QNetworkSettingsInterfaceModel(QObject *parent=0); + // from QAbstractItemModel + int rowCount(const QModelIndex & parent = QModelIndex()) const; + QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; + QHash<int, QByteArray> roleNames() const; + + void append(QNetworkSettingsInterface* networkInterface); + void insert(int row, QNetworkSettingsInterface* networkInterface); + void remove(int row); + QList<QNetworkSettingsInterface*> getModel(); + + enum Roles { + Type = Qt::UserRole + 1, + Status, + Name + }; + +private: + QList<QNetworkSettingsInterface*> m_items; + QHash<int, QByteArray> m_roleNames; +}; + +#endif // QNETWORKSETTINGSINTERFACEMODEL_H diff --git a/src/networksettings/qnetworksettingsmanager.cpp b/src/networksettings/qnetworksettingsmanager.cpp new file mode 100644 index 0000000..095d1a0 --- /dev/null +++ b/src/networksettings/qnetworksettingsmanager.cpp @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qnetworksettingsmanager.h" +#include "qnetworksettingsservice.h" +#include "qnetworksettingsservicemodel.h" +#include "qnetworksettingsinterfacemodel.h" +#include "qnetworksettingsmanager_p.h" +#include <QStringListModel> + +QNetworkSettingsManager::QNetworkSettingsManager(QObject *parent) : + QObject(parent) + ,d_ptr(new QNetworkSettingsManagerPrivate(this)) +{ +} + +QAbstractItemModel* QNetworkSettingsManager::services() +{ + Q_D(QNetworkSettingsManager); + return d->m_serviceFilter; +} + +QAbstractItemModel* QNetworkSettingsManager::interfaces() +{ + Q_D(QNetworkSettingsManager); + return &d->m_interfaceModel; +} + +QNetworkSettingsService* QNetworkSettingsManager::getService(const QString& name, const int type) +{ + Q_D(QNetworkSettingsManager); + + foreach (QNetworkSettingsService* service, d->m_serviceModel->getModel()) { + if (service->name() == name && service->type() == type) { + return service; + } + } + return NULL; +} + +void QNetworkSettingsManager::setUserAgent(QNetworkSettingsUserAgent *agent) +{ + Q_D(QNetworkSettingsManager); + d->setUserAgent(agent); +} diff --git a/src/networksettings/qnetworksettingsmanager.h b/src/networksettings/qnetworksettingsmanager.h new file mode 100644 index 0000000..00980f4 --- /dev/null +++ b/src/networksettings/qnetworksettingsmanager.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QNETWORKSETTINGSMANAGER_H +#define QNETWORKSETTINGSMANAGER_H + +#include "qnetworksettings.h" +#include <QObject> +#include <QStringListModel> + +QT_FORWARD_DECLARE_CLASS(QNetworkSettingsManagerPrivate) +QT_FORWARD_DECLARE_CLASS(QNetworkSettingsService) +QT_FORWARD_DECLARE_CLASS(QNetworkSettingsUserAgent) + +class Q_DECL_EXPORT QNetworkSettingsManager : public QObject +{ + Q_OBJECT + Q_ENUMS(StateTypes NetworkTypeTypes) + Q_PROPERTY(QAbstractItemModel* services READ services NOTIFY servicesChanged) + Q_PROPERTY(QAbstractItemModel* interfaces READ interfaces NOTIFY interfacesChanged) +public: + explicit QNetworkSettingsManager(QObject* parent = 0); + QAbstractItemModel* services(); + QAbstractItemModel* interfaces(); + void setUserAgent(QNetworkSettingsUserAgent *agent); + + Q_INVOKABLE QNetworkSettingsService* getService(const QString& name, const int type); + +Q_SIGNALS: + void servicesChanged(); + void interfacesChanged(); + +protected: + QNetworkSettingsManagerPrivate *d_ptr; + +private: + Q_DISABLE_COPY(QNetworkSettingsManager) + Q_DECLARE_PRIVATE(QNetworkSettingsManager) +}; + +#endif // QNETWORKSETTINGSMANAGER_H diff --git a/src/networksettings/qnetworksettingsservice.cpp b/src/networksettings/qnetworksettingsservice.cpp new file mode 100644 index 0000000..727c929 --- /dev/null +++ b/src/networksettings/qnetworksettingsservice.cpp @@ -0,0 +1,152 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qnetworksettingsservice.h" +#include "qnetworksettingsservice_p.h" + +QNetworkSettingsService::QNetworkSettingsService(const QString& aServiceId, QObject* parent) : + QObject(parent) + ,d_ptr(new QNetworkSettingsServicePrivate(aServiceId, this)) +{ + +} + +QString QNetworkSettingsService::id() const +{ + Q_D(const QNetworkSettingsService); + return d->m_id; +} + +QString QNetworkSettingsService::name() const +{ + Q_D(const QNetworkSettingsService); + return d->m_name; +} + +void QNetworkSettingsService::setAutoConnect(const bool autoconnect) +{ + Q_UNUSED(autoconnect); +} + +QNetworkSettingsState::States QNetworkSettingsService::state() +{ + Q_D(QNetworkSettingsService); + return d->m_state.state(); +} + +QNetworkSettingsType::Types QNetworkSettingsService::type() +{ + Q_D(QNetworkSettingsService); + return d->m_type.type(); +} + +QNetworkSettingsIPv4* QNetworkSettingsService::ipv4() +{ + Q_D(QNetworkSettingsService); + return &d->m_ipv4config; +} + +QNetworkSettingsIPv6* QNetworkSettingsService::ipv6() +{ + Q_D(QNetworkSettingsService); + return &d->m_ipv6config; +} + +QNetworkSettingsProxy* QNetworkSettingsService::proxy() +{ + Q_D(QNetworkSettingsService); + return &d->m_proxyConfig; +} + +QNetworkSettingsWireless* QNetworkSettingsService::wirelessConfig() +{ + Q_D(QNetworkSettingsService); + return &d->m_wifiConfig; +} + +QAbstractItemModel* QNetworkSettingsService::domains() +{ + Q_D(QNetworkSettingsService); + return &d->m_domainsConfig; +} + +QAbstractItemModel* QNetworkSettingsService::nameservers() +{ + Q_D(QNetworkSettingsService); + return &d->m_nameserverConfig; +} + +void QNetworkSettingsService::setupIpv4Config() +{ + Q_D(QNetworkSettingsService); + d->setupIpv4Config(); +} + +void QNetworkSettingsService::setupIpv6Config() +{ + Q_D(QNetworkSettingsService); + d->setupIpv6Config(); +} + +void QNetworkSettingsService::setupNameserversConfig() +{ + Q_D(QNetworkSettingsService); + d->setupNameserversConfig(); +} + +void QNetworkSettingsService::setupDomainsConfig() +{ + Q_D(QNetworkSettingsService); + d->setupDomainsConfig(); +} + +void QNetworkSettingsService::setupNetworkSettingsProxy() +{ + Q_D(QNetworkSettingsService); + d->setupQNetworkSettingsProxy(); +} + +void QNetworkSettingsService::connectService() +{ + Q_D(QNetworkSettingsService); + d->connectService(); +} + +void QNetworkSettingsService::disconnectService() +{ + Q_D(QNetworkSettingsService); + d->disconnectService(); +} diff --git a/src/networksettings/qnetworksettingsservice.h b/src/networksettings/qnetworksettingsservice.h new file mode 100644 index 0000000..aa0be2c --- /dev/null +++ b/src/networksettings/qnetworksettingsservice.h @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QNETWORKSETTINGSSERVICE_H +#define QNETWORKSETTINGSSERVICE_H + +#include <QObject> +#include "qnetworksettings.h" + +QT_FORWARD_DECLARE_CLASS(QNetworkSettingsServicePrivate) + +class Q_DECL_EXPORT QNetworkSettingsService : public QObject +{ + Q_OBJECT + Q_ENUMS(StateTypes) + Q_PROPERTY(QString id READ id CONSTANT) + Q_PROPERTY(QString name READ name NOTIFY nameChanged) + Q_PROPERTY(QNetworkSettingsState::States state READ state NOTIFY stateChanged) + Q_PROPERTY(QNetworkSettingsType::Types type READ type NOTIFY typeChanged) + Q_PROPERTY(QNetworkSettingsIPv4* ipv4 READ ipv4 NOTIFY ipv4Changed) + Q_PROPERTY(QNetworkSettingsIPv6* ipv6 READ ipv6 NOTIFY ipv6Changed) + Q_PROPERTY(QNetworkSettingsProxy* proxy READ proxy NOTIFY proxyChanged) + Q_PROPERTY(QNetworkSettingsWireless* wirelessConfig READ wirelessConfig NOTIFY wirelessChanged) + Q_PROPERTY(QAbstractItemModel* domains READ domains NOTIFY domainsChanged) + Q_PROPERTY(QAbstractItemModel* nameservers READ nameservers NOTIFY nameserversChanged) +public: + explicit QNetworkSettingsService(const QString& aServiceId, QObject* parent = 0); + + QString id() const; + QString name() const; + QNetworkSettingsState::States state(); + QNetworkSettingsType::Types type(); + QNetworkSettingsIPv4* ipv4(); + QNetworkSettingsIPv6* ipv6(); + QNetworkSettingsProxy* proxy(); + QAbstractItemModel* domains(); + QAbstractItemModel* nameservers(); + QNetworkSettingsWireless* wirelessConfig(); + + Q_INVOKABLE void setAutoConnect(const bool autoconnect); + Q_INVOKABLE void setupIpv4Config(); + Q_INVOKABLE void setupIpv6Config(); + Q_INVOKABLE void setupNameserversConfig(); + Q_INVOKABLE void setupDomainsConfig(); + Q_INVOKABLE void setupNetworkSettingsProxy(); + //Wireless config + Q_INVOKABLE void connectService(); + Q_INVOKABLE void disconnectService(); +Q_SIGNALS: + void nameChanged(); + void stateChanged(); + void typeChanged(); + void proxyChanged(); + void ipv4Changed(); + void ipv6Changed(); + void domainsChanged(); + void nameserversChanged(); + void wirelessChanged(); + void showCrendentialInput(); +protected: + QNetworkSettingsServicePrivate *d_ptr; + + Q_DISABLE_COPY(QNetworkSettingsService) + Q_DECLARE_PRIVATE(QNetworkSettingsService) +}; + +#endif // QNETWORKSETTINGSSERVICE_H diff --git a/src/networksettings/qnetworksettingsservicemodel.cpp b/src/networksettings/qnetworksettingsservicemodel.cpp new file mode 100644 index 0000000..af84a3e --- /dev/null +++ b/src/networksettings/qnetworksettingsservicemodel.cpp @@ -0,0 +1,177 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qnetworksettingsservicemodel.h" +#include "qnetworksettings.h" + +QNetworkSettingsServiceModel::QNetworkSettingsServiceModel(QObject *parent) + : QAbstractListModel(parent) +{ + m_roleNames.insert(Qt::UserRole, "modelData"); + m_roleNames.insert(Name, "name"); + m_roleNames.insert(SignalStrength, "signalStrength"); + m_roleNames.insert(Connected, "connected"); +} + +QNetworkSettingsServiceModel::~QNetworkSettingsServiceModel() +{ + +} + +QHash<int, QByteArray> QNetworkSettingsServiceModel::roleNames() const +{ + return m_roleNames; +} + + +int QNetworkSettingsServiceModel::rowCount(const QModelIndex & parent) const +{ + Q_UNUSED(parent); + return m_items.count(); +} + +QVariant QNetworkSettingsServiceModel::data(const QModelIndex & index, int role) const +{ + if (!index.isValid()) return QVariant(); + + QNetworkSettingsService *item = m_items[index.row()]; + if (role == Qt::UserRole) { + return QVariant::fromValue(static_cast<QObject*>(item)); + } + else if (role == Name) { + return item->name(); + } + else if (role == SignalStrength) { + return item->wirelessConfig()->signalStrength(); + } + else if (role == Connected) { + return item->state() == QNetworkSettingsState::Online || item->state() == QNetworkSettingsState::Ready; + } + return QVariant(); +} + +void QNetworkSettingsServiceModel::append(QNetworkSettingsService* item) +{ + item->setParent(this); + + beginInsertRows(QModelIndex(), rowCount(), rowCount()); + m_items.append(item); + endInsertRows(); +} + +void QNetworkSettingsServiceModel::insert(int row, QNetworkSettingsService* item) +{ + item->setParent(this); + + beginInsertRows(QModelIndex(), row, row); + m_items.insert(row, item); + endInsertRows(); +} + +void QNetworkSettingsServiceModel::remove(int row) +{ + beginRemoveRows(QModelIndex(), row, row); + m_items.removeAt(row); + endRemoveRows(); +} + +void QNetworkSettingsServiceModel::updated(int row) +{ + dataChanged(createIndex(row, 0), createIndex(row, 0)); +} + +QList<QNetworkSettingsService*> QNetworkSettingsServiceModel::getModel() +{ + return m_items; +} + +//Filter model + +QNetworkSettingsServiceFilter::QNetworkSettingsServiceFilter(QObject* parent) + :QSortFilterProxyModel(parent) +{ + connect(this, &QNetworkSettingsServiceFilter::typeChanged, this, &QNetworkSettingsServiceFilter::invalidate); +} + +QNetworkSettingsServiceFilter::~QNetworkSettingsServiceFilter() +{ + +} + +QNetworkSettingsType::Types QNetworkSettingsServiceFilter::type() const +{ + return m_type; +} + +void QNetworkSettingsServiceFilter::setType(const QNetworkSettingsType::Types type) +{ + m_type = type; + emit typeChanged(); +} + +bool QNetworkSettingsServiceFilter::filterAcceptsRow( int source_row, const QModelIndex& source_parent ) const +{ + if (this->sourceModel()) + { + QModelIndex index = this->sourceModel()->index( source_row, 0, source_parent ); + if (index.isValid()) + { + if (index.isValid()) + { + QObject * obj = qvariant_cast<QObject *>(index.data(Qt::UserRole)); + QNetworkSettingsService * service = qobject_cast<QNetworkSettingsService *>(obj); + if (service->type() == m_type) + return true; + } + } + } + return false; +} + +QVariant QNetworkSettingsServiceFilter::itemFromRow(const int row) const +{ + QModelIndex idx = index(row, 0); + QModelIndex mapped = mapToSource(idx); + if (mapped.isValid()) + { + QVariant serviceItem = mapped.data(Qt::UserRole); + if (serviceItem.isValid()) + { + return serviceItem; + } + } + return QVariant::fromValue(QStringLiteral("")); +} + diff --git a/src/networksettings/qnetworksettingsservicemodel.h b/src/networksettings/qnetworksettingsservicemodel.h new file mode 100644 index 0000000..566018d --- /dev/null +++ b/src/networksettings/qnetworksettingsservicemodel.h @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QNETWORKSETTINGSSERVICEMODEL_H +#define QNETWORKSETTINGSSERVICEMODEL_H + +#include <QSortFilterProxyModel> +#include "qnetworksettings.h" +#include "qnetworksettingsservice.h" + +class Q_DECL_EXPORT QNetworkSettingsServiceModel : public QAbstractListModel +{ + Q_OBJECT +public: + explicit QNetworkSettingsServiceModel(QObject *parent=0); + virtual ~QNetworkSettingsServiceModel(); + // from QAbstractItemModel + int rowCount(const QModelIndex & parent = QModelIndex()) const; + QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; + QHash<int, QByteArray> roleNames() const; + + void append(QNetworkSettingsService* networkService); + void insert(int row, QNetworkSettingsService* networkInterface); + void remove(int row); + void updated(int row); + QList<QNetworkSettingsService*> getModel(); + + enum Roles { + Type = Qt::UserRole + 1, + Name, + SignalStrength, + Connected + }; + +private: + QList<QNetworkSettingsService*> m_items; + QHash<int, QByteArray> m_roleNames; +}; + +class QNetworkSettingsServiceFilter : public QSortFilterProxyModel +{ + Q_OBJECT + Q_PROPERTY(QNetworkSettingsType::Types type READ type WRITE setType NOTIFY typeChanged) +public: + explicit QNetworkSettingsServiceFilter(QObject* parent=0); + virtual ~QNetworkSettingsServiceFilter(); + bool filterAcceptsRow( int source_row, const QModelIndex& source_parent ) const override; + QNetworkSettingsType::Types type() const; + void setType(QNetworkSettingsType::Types type); + Q_INVOKABLE QVariant itemFromRow(const int row) const; +signals: + void typeChanged(); +private: + QNetworkSettingsType::Types m_type; +}; + +#endif // QNETWORKSETTINGSSERVICEMODEL_H diff --git a/src/networksettings/qnetworksettingsuseragent.cpp b/src/networksettings/qnetworksettingsuseragent.cpp new file mode 100644 index 0000000..f1b0590 --- /dev/null +++ b/src/networksettings/qnetworksettingsuseragent.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qnetworksettingsuseragent.h" +#include "qnetworksettingsuseragent_p.h" + +QNetworkSettingsUserAgent::QNetworkSettingsUserAgent(QObject *parent) + :QObject(parent) + ,d_ptr(new QNetworkSettingsUserAgentPrivate(this)) +{ +} + +void QNetworkSettingsUserAgent::setUserCredentials(const QString& username, const QString& passphrase) +{ + Q_D(QNetworkSettingsUserAgent); + d->setUserCredentials(username, passphrase); +} + +void QNetworkSettingsUserAgent::cancelInput() +{ + Q_D(QNetworkSettingsUserAgent); + d->cancel(); +} + +QString QNetworkSettingsUserAgent::passPhrase() const +{ + Q_D(const QNetworkSettingsUserAgent); + return d->m_passphrase; +} + +QString QNetworkSettingsUserAgent::userName() const +{ + Q_D(const QNetworkSettingsUserAgent); + return d->m_username; +} diff --git a/src/networksettings/qnetworksettingsuseragent.h b/src/networksettings/qnetworksettingsuseragent.h new file mode 100644 index 0000000..c0e0b2f --- /dev/null +++ b/src/networksettings/qnetworksettingsuseragent.h @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QNETWORKSETTINGSUSERAGENT_H +#define QNETWORKSETTINGSUSERAGENT_H + +#include <QObject> + +QT_FORWARD_DECLARE_CLASS(QNetworkSettingsUserAgentPrivate) + +class Q_DECL_EXPORT QNetworkSettingsUserAgent : public QObject +{ + Q_OBJECT +public: + explicit QNetworkSettingsUserAgent(QObject *parent = 0); + Q_INVOKABLE void setUserCredentials(const QString& username, const QString& passphrase); + Q_INVOKABLE void cancelInput(); + QString passPhrase() const; + QString userName() const; + +signals: + void showUserCredentialsInput(); + void error(); + void ready(bool cancel); +private: + QNetworkSettingsUserAgentPrivate *d_ptr; + + Q_DISABLE_COPY(QNetworkSettingsUserAgent) + Q_DECLARE_PRIVATE(QNetworkSettingsUserAgent) +}; +#endif // QNETWORKSETTINGSUSERAGENT_H diff --git a/src/networksettings/wpasupplicant.pri b/src/networksettings/wpasupplicant.pri new file mode 100644 index 0000000..db89315 --- /dev/null +++ b/src/networksettings/wpasupplicant.pri @@ -0,0 +1,28 @@ +QT += core network + +INCLUDEPATH += $${PWD}/wpasupplicant + +HEADERS += \ + $$PWD/wpasupplicant/qnetworksettingsinterface_p.h \ + $$PWD/wpasupplicant/qnetworksettingsmanager_p.h \ + $$PWD/wpasupplicant/qwifisupplicant_p.h \ + $$PWD/wpasupplicant/qwificontroller_p.h \ + $$PWD/wpasupplicant/qnetworksettingsservice_p.h \ + $$PWD/wpasupplicant/qnetworksettingsuseragent_p.h \ + $$PWD/wpasupplicant/qwifidevice.h + +SOURCES += \ + $$PWD/wpasupplicant/qnetworksettingsinterface_p.cpp \ + $$PWD/wpasupplicant/qnetworksettingsmanager_p.cpp \ + $$[QT_SYSROOT]/usr/include/wpa-supplicant/wpa_ctrl.c \ + $$[QT_SYSROOT]/usr/include/wpa-supplicant/os_unix.c \ + $$PWD/wpasupplicant/qwifisupplicant.cpp \ + $$PWD/wpasupplicant/qwificontroller.cpp \ + $$PWD/wpasupplicant/qnetworksettingsservice_p.cpp \ + $$PWD/wpasupplicant/qnetworksettingsuseragent_p.cpp \ + $$PWD/wpasupplicant/qwifidevice.cpp + +DEFINES += \ + CONFIG_CTRL_IFACE \ + CONFIG_CTRL_IFACE_UNIX + diff --git a/src/networksettings/wpasupplicant/qnetworksettingsinterface_p.cpp b/src/networksettings/wpasupplicant/qnetworksettingsinterface_p.cpp new file mode 100644 index 0000000..7370421 --- /dev/null +++ b/src/networksettings/wpasupplicant/qnetworksettingsinterface_p.cpp @@ -0,0 +1,71 @@ + +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qnetworksettingsinterface_p.h" +#include "qnetworksettingsmanager_p.h" +#include "qwifidevice.h" + +QNetworkSettingsInterfacePrivate::QNetworkSettingsInterfacePrivate(QNetworkSettingsInterface* parent) + :QObject(parent) + ,q_ptr(parent) +{ + m_name = QWifiDevice::wifiInterfaceName(); + m_type.setType(QNetworkSettingsType::Wifi); + m_powered = true; //We don't really know +} + +void QNetworkSettingsInterfacePrivate::setManager(QNetworkSettingsManagerPrivate *manager) +{ + m_manager = manager; +} + +void QNetworkSettingsInterfacePrivate::setPowered(const bool power) +{ + //Not supported + Q_UNUSED(power); +} + +void QNetworkSettingsInterfacePrivate::setState(QNetworkSettingsState::States aState) +{ + Q_Q(QNetworkSettingsInterface); + m_state.setState(aState); + emit q->stateChanged(); +} + +void QNetworkSettingsInterfacePrivate::scan() +{ + m_manager->call(QStringLiteral("SCAN")); +} diff --git a/src/networksettings/wpasupplicant/qnetworksettingsinterface_p.h b/src/networksettings/wpasupplicant/qnetworksettingsinterface_p.h new file mode 100644 index 0000000..d8964c3 --- /dev/null +++ b/src/networksettings/wpasupplicant/qnetworksettingsinterface_p.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QNETWORKSETTINGSINTERFACEPRIVATE_H +#define QNETWORKSETTINGSINTERFACEPRIVATE_H + +#include <QObject> +#include "qnetworksettings.h" +#include "qnetworksettingsinterface.h" +#include "qnetworksettingsmanager_p.h" + +class QNetworkSettingsInterfacePrivate : public QObject +{ + Q_OBJECT + Q_DECLARE_PUBLIC(QNetworkSettingsInterface) +public: + explicit QNetworkSettingsInterfacePrivate(QNetworkSettingsInterface* parent); + void initialize(const QString& path, const QVariantMap& properties); + void setPowered(const bool power); + void setState(QNetworkSettingsState::States aState); + void setManager(QNetworkSettingsManagerPrivate *manager); + void scan(); + +signals: + +protected: + QString m_name; + QNetworkSettingsType m_type; + QNetworkSettingsState m_state; + bool m_powered; + QNetworkSettingsManagerPrivate *m_manager; //not owned + QNetworkSettingsInterface *q_ptr; +}; + +class WpaSupplicantSettingsInterface : public QNetworkSettingsInterface +{ + Q_OBJECT +public: + WpaSupplicantSettingsInterface(QNetworkSettingsManagerPrivate* manager) + :QNetworkSettingsInterface(manager) + { + Q_D(QNetworkSettingsInterface); + d->setManager(manager); + } + + void setState(QNetworkSettingsState::States aState) { + Q_D(QNetworkSettingsInterface); + d->setState(aState); + } + + virtual ~WpaSupplicantSettingsInterface() { + } +}; + +#endif // QNETWORKSETTINGSINTERFACEPRIVATE_H + diff --git a/src/networksettings/wpasupplicant/qnetworksettingsmanager_p.cpp b/src/networksettings/wpasupplicant/qnetworksettingsmanager_p.cpp new file mode 100644 index 0000000..17a1f2f --- /dev/null +++ b/src/networksettings/wpasupplicant/qnetworksettingsmanager_p.cpp @@ -0,0 +1,433 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QProcess> +#include "qnetworksettingsmanager_p.h" +#include "qwificontroller_p.h" +#include "qnetworksettingsinterface_p.h" +#include "qnetworksettingsservice_p.h" +#include "qnetworksettingsuseragent.h" +#include "qwifisupplicant_p.h" + +QNetworkSettingsManagerPrivate::QNetworkSettingsManagerPrivate(QNetworkSettingsManager *parent) + :QObject(parent) + ,q_ptr(parent) +{ + m_serviceFilter.setSourceModel(&m_serviceModel); + m_wifiController = new QWifiController(this); + m_wifiController->asyncCall(QWifiController::InitializeBackend); + + QObject::connect(m_wifiController, &QWifiController::backendStateChanged, + this, &QNetworkSettingsManagerPrivate::handleBackendStateChanged); + QObject::connect(m_wifiController, &QWifiController::dhcpRequestFinished, + this, &QNetworkSettingsManagerPrivate::handleDhcpRequestFinished); + + QObject::connect(m_wifiController, &QWifiController::raiseError, this, &QNetworkSettingsManagerPrivate::updateLastError); + m_wifiController->start(); + + updateWifiState(); +} + +QNetworkSettingsManagerPrivate::~QNetworkSettingsManagerPrivate() +{ + m_wifiController->asyncCall(QWifiController::ExitEventLoop); + m_wifiController->wait(); + delete m_wifiController; +} + +bool QNetworkSettingsManagerPrivate::event(QEvent *event) +{ + switch ((int) event->type()) { + case WIFI_SCAN_RESULTS: + parseScanResults(call(QStringLiteral("SCAN_RESULTS"))); + return true; + case WIFI_CONNECTED: + handleConnected(); + return true; + case WIFI_DISCONNECTED: + handleDisconneced(); + return true; + case WIFI_AUTHENTICATING: + handleAuthenticating(static_cast<QWifiEvent *>(event)); + return true; + case WIFI_HANDSHAKE_FAILED: + updateNetworkState(QNetworkSettingsState::Failure); + return true; + } + return QObject::event(event); +} + +void QNetworkSettingsManagerPrivate::connectNetwork(const QString& ssid) +{ + if (m_backendState != QWifiController::Running) { + qCWarning(B2QT_WIFI) << "start wifi backend before calling connect()"; + return; + } + + call(QStringLiteral("DISABLE_NETWORK all")); + m_currentSSID = ssid; + emit m_agent->showUserCredentialsInput(); +} + +void QNetworkSettingsManagerPrivate::userInteractionReady(bool cancel) +{ + if (cancel) { + m_currentSSID = ""; + return; + } + bool networkKnown = false; + QString id; + const QStringList configuredNetworks = call(QStringLiteral("LIST_NETWORKS")).split('\n'); + for (int i = 1; i < configuredNetworks.length(); ++i) { + const QStringList networkFields = configuredNetworks.at(i).split('\t'); + const QString ssid = QWifiSupplicant::decodeSsid(networkFields.at(1)); + if (ssid == m_currentSSID) { + id = networkFields.at(0); + networkKnown = true; + break; + } + } + + if (!networkKnown) { + bool ok; + id = call(QStringLiteral("ADD_NETWORK")); + id.toInt(&ok); + if (!ok) { + updateLastError(QStringLiteral("failed to add network")); + return; + } + } + + bool ok = true; + QChar q = QLatin1Char('"'); + QString setNetworkCommand = QLatin1String("SET_NETWORK ") + id; + if (!networkKnown) { + ok = ok && checkedCall(setNetworkCommand + QLatin1String(" ssid ") + q + m_currentSSID + q); + } + + QString key_mgmt; + WpaSupplicantService *service = networkForSSID(m_currentSSID); + if (!service) { + return; + } + QString psk = m_agent->passPhrase(); + + // --------------------- configure network ------------------------------ + // ref: http://w1.fi/cgit/hostap/plain/wpa_supplicant/wpa_supplicant.conf + // ref: https://www.freebsd.org/cgi/man.cgi?wpa_supplicant.conf + // ---------------------------------------------------------------------- + if (service->wirelessConfig()->supportsSecurity(QNetworkSettingsWireless::WPA) || + service->wirelessConfig()->supportsSecurity(QNetworkSettingsWireless::WPA2)) { + // ### TODO - password length has limits (see IEEE 802.11), we need to check + // for those limits here. Supplicant gives only a meaningless "fail" message. + ok = ok && checkedCall(setNetworkCommand + QLatin1String(" psk ") + q + psk + q); + key_mgmt = QLatin1String("WPA-PSK"); + } else if (service->wirelessConfig()->supportsSecurity(QNetworkSettingsWireless::WEP)) { + ok = ok && checkedCall(setNetworkCommand + QLatin1String(" wep_key0 ") + q + psk + q); + ok = ok && checkedCall(setNetworkCommand + QLatin1String(" auth_alg OPEN SHARED")); + key_mgmt = QLatin1String("NONE"); + } else if (service->wirelessConfig()->supportsSecurity(QNetworkSettingsWireless::None)) { + // open network + key_mgmt = QLatin1String("NONE"); + } + + if (service->wirelessConfig()->hidden()) + ok = ok && checkedCall(setNetworkCommand + QLatin1String(" scan_ssid 1")); + + ok = ok && checkedCall(setNetworkCommand + QLatin1String(" key_mgmt ") + key_mgmt); + if (!ok) { + if (!networkKnown) + call(QLatin1String("REMOVE_NETWORK ") + id); + updateLastError(QLatin1String("failed to set properties on network: ") + id); + return; + } + + call(QLatin1String("SELECT_NETWORK ") + id); + call(QStringLiteral("RECONNECT")); +} + +void QNetworkSettingsManagerPrivate::disconnectNetwork() +{ + call(QStringLiteral("DISCONNECT")); + m_wifiController->asyncCall(QWifiController::StopDhcp); +} + +void QNetworkSettingsManagerPrivate::handleBackendStateChanged(QWifiController::BackendState backendState) +{ + switch (backendState) { + case QWifiController::NotRunning: + updateNetworkState(QNetworkSettingsState::Disconnect); + break; + default: + break; + } + updateBackendState(backendState); +} + +void QNetworkSettingsManagerPrivate::handleDhcpRequestFinished(const QString &status) +{ + qCDebug(B2QT_WIFI) << "handleDhcpRequestFinished: " << status << " for " << m_currentSSID; + if (status == QLatin1String("success")) { + updateNetworkState(QNetworkSettingsState::Online); + call(QStringLiteral("SAVE_CONFIG")); + } else { + updateNetworkState(QNetworkSettingsState::Failure); + } +} + +void QNetworkSettingsManagerPrivate::setUserAgent(QNetworkSettingsUserAgent *agent) +{ + m_agent = agent; + connect(m_agent, &QNetworkSettingsUserAgent::ready, this, &QNetworkSettingsManagerPrivate::userInteractionReady); +} + +void QNetworkSettingsManagerPrivate::setCurrentSSID(const QString &ssid) +{ + qCDebug(B2QT_WIFI) << "current SSID: " << m_currentSSID << " -> " << ssid; + if (m_currentSSID == ssid) + return; + + m_currentSSID = ssid; +} + +void QNetworkSettingsManagerPrivate::handleAuthenticating(QWifiEvent *event) +{ + QString data = event->data().trimmed(); + QString ssid = data.mid(data.indexOf(QLatin1String("SSID")) + 6); + ssid = ssid.left(ssid.lastIndexOf(QLatin1Char('\''))); + + setCurrentSSID(QWifiSupplicant::decodeSsid(ssid)); + updateNetworkState(QNetworkSettingsState::Association); +} + +void QNetworkSettingsManagerPrivate::handleConnected() +{ + qCDebug(B2QT_WIFI) << "connected network: " << m_currentSSID; + updateNetworkState(QNetworkSettingsState::Ready); + m_wifiController->asyncCall(QWifiController::AcquireIPAddress); +} + +void QNetworkSettingsManagerPrivate::handleDisconneced() +{ + updateNetworkState(QNetworkSettingsState::Disconnect); +} + +void QNetworkSettingsManagerPrivate::updateNetworkState(QNetworkSettingsState::States networkState) +{ + //Update interface + if (!m_interfaceModel.getModel().isEmpty()) { + WpaSupplicantSettingsInterface* interface = qobject_cast<WpaSupplicantSettingsInterface*>(m_interfaceModel.getModel().first()); + if (interface && interface->state() != networkState) { + interface->setState(networkState); + } + } + + //Update service state + WpaSupplicantService *service = networkForSSID(m_currentSSID); + if (service) { + service->setState(networkState); + } +} + +void QNetworkSettingsManagerPrivate::updateBackendState(QWifiController::BackendState backendState) +{ + if (m_backendState == backendState) + return; + + m_backendState = backendState; + + if (m_backendState == QWifiController::Running && m_interfaceModel.getModel().isEmpty()) { + WpaSupplicantSettingsInterface *interface = new WpaSupplicantSettingsInterface(this); + m_interfaceModel.append(interface); + } else if (m_backendState == QWifiController::NotRunning && m_interfaceModel.getModel().size() > 0){ + m_interfaceModel.remove(0); + } +} + +void QNetworkSettingsManagerPrivate::updateWifiState() +{ + QProcess ps; + ps.start(QStringLiteral("ps")); + if (!ps.waitForStarted()) { + updateLastError(ps.program() + QLatin1String(": ") + ps.errorString()); + return; + } + + ps.waitForFinished(); + bool supplicantRunning = ps.readAll().contains("wpa_supplicant"); + if (supplicantRunning && m_wifiController->resetSupplicantSocket()) + m_backendState = QWifiController::Running; +} + +QString QNetworkSettingsManagerPrivate::call(const QString &command) +{ + if (m_backendState != QWifiController::Running) + return QString(); + + QByteArray reply; + bool success = m_wifiController->supplicant()->sendCommand(command, &reply); + if (!success) { + qCDebug(B2QT_WIFI) << "call to supplicant failed"; + return QString(); + } + + return QLatin1String(reply.trimmed()); +} + +bool QNetworkSettingsManagerPrivate::checkedCall(const QString &command) +{ + return call(command).toUpper() == QLatin1String("OK"); +} + +void QNetworkSettingsManagerPrivate::updateLastError(const QString &error) +{ + qCWarning(B2QT_WIFI) << error; + if (!m_interfaceModel.getModel().isEmpty()) { + WpaSupplicantSettingsInterface* interface = qobject_cast<WpaSupplicantSettingsInterface*>(m_interfaceModel.getModel().first()); + if (interface) { + interface->setState(QNetworkSettingsState::Failure); + } + } +} + +WpaSupplicantService* QNetworkSettingsManagerPrivate::networkForSSID(const QString& ssid) +{ + int pos = 0; + return networkForSSID(ssid, pos); +} + +WpaSupplicantService* QNetworkSettingsManagerPrivate::networkForSSID(const QString& ssid, int& pos) +{ + QList<QNetworkSettingsService*> services = m_serviceModel.getModel(); + pos = 0; + foreach (QNetworkSettingsService *service, services) { + if (service->name() == ssid) { + return qobject_cast<WpaSupplicantService*>(service); + } + pos++; + } + pos = -1; + return NULL; +} + +WpaSupplicantService* QNetworkSettingsManagerPrivate::outOfRangeListContains(const QString& ssid) +{ + QList<QNetworkSettingsService*> services = m_outOfRangeServiceModel.getModel(); + foreach (QNetworkSettingsService *service, services) { + if (service->name() == ssid) { + return qobject_cast<WpaSupplicantService*>(service); + } + } + return NULL; +} + +void QNetworkSettingsManagerPrivate::parseScanResults(const QString &results) +{ + QStringList lines = results.split('\n'); + QSet<QString> sensibleNetworks; + + for (int i = 1; i < lines.size(); ++i) { + QStringList info = lines.at(i).split('\t'); + if (info.size() < 5 || info.at(4).isEmpty() || info.at(0).isEmpty()) + continue; + int pos = 0; + + QString ssid = QWifiSupplicant::decodeSsid(info.at(4)); + if (ssid.isEmpty()) + continue; + + sensibleNetworks.insert(ssid); + WpaSupplicantService *knownNetwork = networkForSSID(ssid, pos); + + if (!knownNetwork) { + knownNetwork = outOfRangeListContains(ssid); + m_outOfRangeServiceModel.getModel().removeOne(knownNetwork); + } + + int signalStrength = info.at(2).trimmed().toInt(); + if (signalStrength < 0) { + // signal is reported in dBm, rough conversion: best = -40, worst = -100 + int val = qAbs(qMax(-100, qMin(signalStrength, -40)) + 40); // clamp and normalize to 0 + signalStrength = 100 - (int) ((100.0 * (double) val) / 60.0); + } else if (signalStrength > 100) { + qCWarning(B2QT_WIFI) << "unexpected value for a signal level: " << signalStrength; + } + + if (!knownNetwork) { + WpaSupplicantService *network = new WpaSupplicantService(this); + network->setId(info.at(0)); + network->setFlags(info.at(3)); + network->wirelessConfig()->setSignalStrength(signalStrength); + network->setName(ssid); + m_serviceModel.append(network); + } else { + if (knownNetwork->wirelessConfig()->outOfRange()) { + // known network has come back into a range + knownNetwork->wirelessConfig()->setOutOfRange(false); + m_serviceModel.append(knownNetwork); + pos = m_serviceModel.getModel().size() - 1; + } + // ssids are the same, compare bssids.. + if (knownNetwork->id() == info.at(0)) { + // same access point, simply update the signal strength + knownNetwork->wirelessConfig()->setSignalStrength(signalStrength); + knownNetwork->wirelessConfig()->setOutOfRange(false); + m_serviceModel.updated(pos); + } else if (knownNetwork->wirelessConfig()->signalStrength() < signalStrength) { + // replace with a stronger access point within the same network + knownNetwork->wirelessConfig()->setOutOfRange(false); + knownNetwork->setId(info.at(0)); + knownNetwork->setFlags(info.at(3)); + knownNetwork->wirelessConfig()->setSignalStrength(signalStrength); + knownNetwork->setName(ssid); + m_serviceModel.updated(pos); + } + } + } + // remove out-of-range networks from the data model + QList<QNetworkSettingsService*> networks; + for (int i = 0; i < networks.size();) { + if (!sensibleNetworks.contains(networks.at(i)->name())) { + WpaSupplicantService *n = qobject_cast<WpaSupplicantService*>(networks.at(i)); + m_serviceModel.remove(i); + if (n) { + n->wirelessConfig()->setOutOfRange(true); + m_outOfRangeServiceModel.append(n); + } + } else { + ++i; + } + } +} diff --git a/src/networksettings/wpasupplicant/qnetworksettingsmanager_p.h b/src/networksettings/wpasupplicant/qnetworksettingsmanager_p.h new file mode 100644 index 0000000..fa20fb4 --- /dev/null +++ b/src/networksettings/wpasupplicant/qnetworksettingsmanager_p.h @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QNETWORKSETTINGSMANAGERPRIVATE_H +#define QNETWORKSETTINGSMANAGERPRIVATE_H + +#include <QObject> +#include "qnetworksettings.h" +#include "qnetworksettingsmanager.h" +#include "qnetworksettingsinterfacemodel.h" +#include "qnetworksettingsservicemodel.h" +#include "qwificontroller_p.h" + +class WpaSupplicantService; + +class QNetworkSettingsManagerPrivate : public QObject +{ + Q_OBJECT + Q_DECLARE_PUBLIC(QNetworkSettingsManager) +public: + explicit QNetworkSettingsManagerPrivate(QNetworkSettingsManager *parent); + virtual ~QNetworkSettingsManagerPrivate(); + QNetworkSettingsManager *q_ptr; + void setUserAgent(QNetworkSettingsUserAgent *agent); + void connectNetwork(const QString& ssid); + void disconnectNetwork(); + QString call(const QString &command); + bool checkedCall(const QString &command); +protected: + bool event(QEvent *event); + +private slots: + void handleBackendStateChanged(QWifiController::BackendState backendState); + void handleDhcpRequestFinished(const QString &status); + void userInteractionReady(bool cancel); + void updateLastError(const QString &error); +private: + void setCurrentSSID(const QString &ssid); + void handleConnected(); + void handleDisconneced(); + void handleAuthenticating(QWifiEvent *event); + void updateNetworkState(QNetworkSettingsState::States networkState); + void updateBackendState(QWifiController::BackendState backendState); + void updateWifiState(); + void parseScanResults(const QString &results); + WpaSupplicantService* networkForSSID(const QString& ssid); + WpaSupplicantService* networkForSSID(const QString& ssid, int& pos); + WpaSupplicantService* outOfRangeListContains(const QString& ssid); + + QNetworkSettingsInterfaceModel m_interfaceModel; + QNetworkSettingsServiceModel m_serviceModel; + QNetworkSettingsServiceModel m_outOfRangeServiceModel; + QNetworkSettingsServiceFilter m_serviceFilter; + QWifiController *m_wifiController; + QNetworkSettingsUserAgent *m_agent; //Not owned + + QWifiController::BackendState m_backendState; + QString m_currentSSID; +}; + + +#endif // QNETWORKSETTINGSMANAGERPRIVATE_H diff --git a/src/networksettings/wpasupplicant/qnetworksettingsservice_p.cpp b/src/networksettings/wpasupplicant/qnetworksettingsservice_p.cpp new file mode 100644 index 0000000..b5d4545 --- /dev/null +++ b/src/networksettings/wpasupplicant/qnetworksettingsservice_p.cpp @@ -0,0 +1,127 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qnetworksettingsservice_p.h" +#include "qnetworksettingsmanager_p.h" + +QNetworkSettingsServicePrivate::QNetworkSettingsServicePrivate(const QString& bssid, QNetworkSettingsService *parent) : + QObject(parent) + ,q_ptr(parent) + ,m_id(bssid) +{ + m_type.setType(QNetworkSettingsType::Wifi); +} + +void QNetworkSettingsServicePrivate::setManager(QNetworkSettingsManagerPrivate *manager) +{ + m_manager = manager; +} + +void QNetworkSettingsServicePrivate::connectService() +{ + m_manager->connectNetwork(m_name); +} + +void QNetworkSettingsServicePrivate::disconnectService() +{ + m_manager->disconnectNetwork(); +} + +void QNetworkSettingsServicePrivate::setupIpv6Config() +{ + //Not supported +} + +void QNetworkSettingsServicePrivate::setupNameserversConfig() +{ + //Not supported +} + +void QNetworkSettingsServicePrivate::setupDomainsConfig() +{ + //Not supported +} + +void QNetworkSettingsServicePrivate::setupQNetworkSettingsProxy() +{ + //Not supported +} + +void QNetworkSettingsServicePrivate::setupIpv4Config() +{ + //Not supported +} + +WpaSupplicantService::WpaSupplicantService(QNetworkSettingsManagerPrivate* manager, QObject* parent) + :QNetworkSettingsService("", parent) +{ + Q_D(QNetworkSettingsService); + d->setManager(manager); +} + +void WpaSupplicantService::setId(const QString& aId) +{ + Q_D(QNetworkSettingsService); + d->m_id = aId; +} + +void WpaSupplicantService::setName(const QString& aName) +{ + Q_D(QNetworkSettingsService); + d->m_name = aName; + emit nameChanged(); +} + +void WpaSupplicantService::setFlags(const QString& aFlags) +{ + Q_D(QNetworkSettingsService); + + if (aFlags.contains("WPA-")) { + d->m_wifiConfig.setSecurity(QNetworkSettingsWireless::WPA); + } + if (aFlags.contains("WPA2-")) { + d->m_wifiConfig.setSecurity(QNetworkSettingsWireless::WPA2); + } + if (aFlags.contains("WEP-")) { + d->m_wifiConfig.setSecurity(QNetworkSettingsWireless::WEP); + } +} + +void WpaSupplicantService::setState(QNetworkSettingsState::States aState) +{ + Q_D(QNetworkSettingsService); + d->m_state.setState(aState); + emit stateChanged(); +} diff --git a/src/networksettings/wpasupplicant/qnetworksettingsservice_p.h b/src/networksettings/wpasupplicant/qnetworksettingsservice_p.h new file mode 100644 index 0000000..12cc193 --- /dev/null +++ b/src/networksettings/wpasupplicant/qnetworksettingsservice_p.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QNETWORKSETTINGSSERVICEPRIVATE_H +#define QNETWORKSETTINGSSERVICEPRIVATE_H + +#include <QObject> +#include "qnetworksettings.h" +#include "qnetworksettingsservice.h" + +class QNetworkSettingsManagerPrivate; + +class QNetworkSettingsServicePrivate : public QObject +{ + Q_OBJECT + Q_DECLARE_PUBLIC(QNetworkSettingsService) +public: + QNetworkSettingsServicePrivate(const QString& aServiceId, QNetworkSettingsService *parent=0); + void setManager(QNetworkSettingsManagerPrivate *manager); + QNetworkSettingsService *q_ptr; + + void setAutoConnect(const bool autoconnect); + void setupIpv4Config(); + void setupIpv6Config(); + void setupNameserversConfig(); + void setupDomainsConfig(); + void setupQNetworkSettingsProxy(); + void connectService(); + void disconnectService(); + + QString m_id; + QString m_name; + QNetworkSettingsState m_state; + QNetworkSettingsIPv4 m_ipv4config; + QNetworkSettingsIPv6 m_ipv6config; + QNetworkSettingsAddressModel m_domainsConfig; + QNetworkSettingsAddressModel m_nameserverConfig; + QNetworkSettingsProxy m_proxyConfig; + QNetworkSettingsWireless m_wifiConfig; + QNetworkSettingsType m_type; + QNetworkSettingsManagerPrivate *m_manager; //Not owned +}; + +class WpaSupplicantService : public QNetworkSettingsService +{ + Q_OBJECT +public: + explicit WpaSupplicantService(QNetworkSettingsManagerPrivate* manager, QObject* parent=0); + void setId(const QString& aId); + void setName(const QString& aName); + void setFlags(const QString& aFlags); + void setState(QNetworkSettingsState::States aState); +}; + +#endif // QNETWORKSETTINGSSERVICEPRIVATE_H diff --git a/src/networksettings/wpasupplicant/qnetworksettingsuseragent_p.cpp b/src/networksettings/wpasupplicant/qnetworksettingsuseragent_p.cpp new file mode 100644 index 0000000..4e636c0 --- /dev/null +++ b/src/networksettings/wpasupplicant/qnetworksettingsuseragent_p.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qnetworksettingsuseragent_p.h" + +QNetworkSettingsUserAgentPrivate::QNetworkSettingsUserAgentPrivate(QNetworkSettingsUserAgent* parent) + :QObject(parent) + ,q_ptr(parent) +{ + +} + +void QNetworkSettingsUserAgentPrivate::setUserCredentials(const QString& aUsername, const QString& aPassword) +{ + Q_Q(QNetworkSettingsUserAgent); + m_passphrase = aPassword; + m_username = aUsername; + emit q->ready(false); +} + +void QNetworkSettingsUserAgentPrivate::cancel() +{ + Q_Q(QNetworkSettingsUserAgent); + emit q->ready(true); +} diff --git a/src/networksettings/wpasupplicant/qnetworksettingsuseragent_p.h b/src/networksettings/wpasupplicant/qnetworksettingsuseragent_p.h new file mode 100644 index 0000000..fc717dc --- /dev/null +++ b/src/networksettings/wpasupplicant/qnetworksettingsuseragent_p.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QNETWORKSETTINGSUSERAGENTPRIVATE_H +#define QNETWORKSETTINGSUSERAGENTPRIVATE_H + +#include <QObject> +#include "qnetworksettingsuseragent.h" + +class QNetworkSettingsUserAgentPrivate : public QObject +{ + Q_OBJECT + Q_DECLARE_PUBLIC(QNetworkSettingsUserAgent) +public: + explicit QNetworkSettingsUserAgentPrivate(QNetworkSettingsUserAgent* parent); + virtual ~QNetworkSettingsUserAgentPrivate() {} + void setUserCredentials(const QString& aUsername, const QString& aPassword); + void cancel(); + QString m_passphrase; + QString m_username; + QNetworkSettingsUserAgent *q_ptr; +}; + +#endif // QNETWORKSETTINGSUSERAGENTPRIVATE_H diff --git a/src/networksettings/wpasupplicant/qwificontroller.cpp b/src/networksettings/wpasupplicant/qwificontroller.cpp new file mode 100644 index 0000000..8e6cfff --- /dev/null +++ b/src/networksettings/wpasupplicant/qwificontroller.cpp @@ -0,0 +1,285 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc +** All rights reserved. +** For any questions to Digia, please use the contact form at +** http://www.qt.io +** +** This file is part of Qt Enterprise Embedded. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** the contact form at http://www.qt.io +** +****************************************************************************/ +#include "qwificontroller_p.h" +#include "qnetworksettingsmanager_p.h" +#include "qwifisupplicant_p.h" +#include "qwifidevice.h" + +#include <sys/types.h> +#include <signal.h> + +#include <QtCore/QCoreApplication> +#include <QtCore/QProcess> +#include <QtCore/QByteArray> +#include <QtCore/QFile> + +QT_BEGIN_NAMESPACE + +Q_LOGGING_CATEGORY(B2QT_WIFI, "qt.b2qt.wifi") + +class QWifiEventThread : public QThread +{ +public: + QWifiEventThread(QWifiController *controller) + : m_controller(controller) + { + } + + void run() { + qCDebug(B2QT_WIFI) << "running wifi event thread"; + QWifiEvent *event = 0; + char buffer[2048]; + forever { + int size = m_controller->supplicant()->waitForEvent(buffer, sizeof(buffer) - 1); + if (size > 0) { + buffer[size] = 0; + event = 0; + qCDebug(B2QT_WIFI) << "[event]: " << buffer; + + if (m_controller->isWifiThreadExitRequested()) { + qCDebug(B2QT_WIFI) << "exit wifi event thread"; + return; + } + + if (strstr(buffer, "SCAN-RESULTS")) { + event = new QWifiEvent(WIFI_SCAN_RESULTS); + } else if (strstr(buffer, "CTRL-EVENT-CONNECTED")) { + event = new QWifiEvent(WIFI_CONNECTED); + } else if (strstr(buffer, "CTRL-EVENT-DISCONNECTED")) { + event = new QWifiEvent(WIFI_DISCONNECTED); + } else if (strstr(buffer, "Trying to associate")) { + event = new QWifiEvent(WIFI_AUTHENTICATING, QLatin1String(buffer)); + } else if (strstr(buffer, "Handshake failed")) { + event = new QWifiEvent(WIFI_HANDSHAKE_FAILED); + } + + if (event) { + QCoreApplication::postEvent(m_controller->manager(), event); + } + } + } + } + +private: + QWifiController *m_controller; +}; + + +QWifiController::QWifiController(QNetworkSettingsManagerPrivate *manager) : + m_manager(manager), + m_exitEventThread(false), + m_interface(QWifiDevice::wifiInterfaceName()), + m_eventThread(new QWifiEventThread(this)), + m_supplicant(new QWifiSupplicant(this)) +{ + qRegisterMetaType<QWifiController::BackendState>("BackendState"); + connect(m_supplicant, &QWifiSupplicant::raiseError, this, &QWifiController::raiseError); +} + +QWifiController::~QWifiController() +{ + exitWifiEventThread(); + delete m_eventThread; +} + +void QWifiController::run() +{ + qCDebug(B2QT_WIFI) << "running wifi backend controller thread"; + Method method; + forever { + m_methodsMutex.lock(); + if (m_methods.isEmpty()) + methodCallRequested.wait(&m_methodsMutex); + method = m_methods.takeFirst(); + m_methodsMutex.unlock(); + switch (method) { + case InitializeBackend: + initializeBackend(); + break; + case TerminateBackend: + terminateBackend(); + break; + case AcquireIPAddress: + acquireIPAddress(); + break; + case StopDhcp: + stopDhcp(); + break; + case ExitEventLoop: + qCDebug(B2QT_WIFI) << "exit wifi backend controller thread"; + return; + } + } +} + +void QWifiController::asyncCall(Method method) +{ + QMutexLocker locker(&m_methodsMutex); + m_methods.append(method); + methodCallRequested.wakeOne(); +} + +void QWifiController::initializeBackend() +{ + qCDebug(B2QT_WIFI) << "initializing wifi backend"; + emit backendStateChanged(QWifiController::Initializing); + + QProcess rfkill; + rfkill.start(QStringLiteral("rfkill"), + QStringList() << QStringLiteral("unblock") << QStringLiteral("wifi")); + rfkill.waitForFinished(); + + QProcess ifconfig; + ifconfig.start(QStringLiteral("ifconfig"), + QStringList() << QLatin1String(m_interface) << QStringLiteral("up")); + if (!ifconfig.waitForStarted()) { + emit raiseError(ifconfig.program() + QLatin1String(": ") + ifconfig.errorString()); + return; + } + + ifconfig.waitForFinished(); + bool initFailed = false; + QByteArray error = ifconfig.readAllStandardError(); + if (!error.isEmpty()) { + emit raiseError(QLatin1String("failed to bring up wifi interface: " + error)); + initFailed = true; + } + if (!initFailed && resetSupplicantSocket()) + emit backendStateChanged(QWifiController::Running); + else + emit backendStateChanged(QWifiController::NotRunning); +} + +bool QWifiController::resetSupplicantSocket() +{ + qCDebug(B2QT_WIFI) << "reset supplicant socket"; + exitWifiEventThread(); + m_supplicant->stopSupplicant(); + m_supplicant->closeSupplicantConnection(); + if (!m_supplicant->startSupplicant()) + return false; + if (!m_supplicant->connectToSupplicant()) + return false; + + startWifiEventThread(); + return true; +} + +void QWifiController::terminateBackend() +{ + qCDebug(B2QT_WIFI) << "terminating wifi backend"; + emit backendStateChanged(QWifiController::Terminating); + + exitWifiEventThread(); + m_supplicant->stopSupplicant(); + m_supplicant->closeSupplicantConnection(); + + QProcess ifconfig; + ifconfig.start(QStringLiteral("ifconfig"), + QStringList() << QLatin1String(m_interface) << QStringLiteral("down")); + if (!ifconfig.waitForStarted()) { + emit raiseError(ifconfig.program() + QLatin1String(": ") + ifconfig.errorString()); + return; + } + + ifconfig.waitForFinished(); + QByteArray error = ifconfig.readAllStandardError(); + if (!error.isEmpty()) + emit raiseError(QLatin1String("failed to bring down wifi interface: " + error)); + + stopDhcp(); + emit backendStateChanged(QWifiController::NotRunning); +} + +void QWifiController::startWifiEventThread() +{ + m_exitEventThread = false; + m_eventThread->start(); +} + +void QWifiController::exitWifiEventThread() +{ + if (m_eventThread->isRunning()) { + m_exitEventThread = true; + m_manager->call(QStringLiteral("SCAN")); + if (!m_eventThread->wait(8000)) + qCWarning(B2QT_WIFI, "timed out waiting for wifi event thread to exit"); + } +} + +void QWifiController::killDhcpProcess(const QString &path) const +{ + QFile pidFile(path); + if (!pidFile.exists()) + return; + + if (!pidFile.open(QIODevice::ReadOnly)) { + qCWarning(B2QT_WIFI) << "could not open pid file: " << path; + return; + } + + bool ok; + int pid = pidFile.readAll().trimmed().toInt(&ok); + if (!ok) { + qCWarning(B2QT_WIFI) << "pid is not a number"; + return; + } + + kill(pid, 9); +} + +void QWifiController::acquireIPAddress() +{ + qCDebug(B2QT_WIFI, "acquireIPAddress"); + QString filePath = QLatin1String("/var/run/udhcpc." + m_interface + ".pid"); + killDhcpProcess(filePath); + QStringList args; + args << QStringLiteral("-R") << QStringLiteral("-n") << QStringLiteral("-p") + << filePath << QStringLiteral("-i") << QLatin1String(m_interface); + + QProcess udhcpc; + udhcpc.start(QStringLiteral("udhcpc"), args); + if (!udhcpc.waitForStarted()) { + emit raiseError(udhcpc.program() + QLatin1String(": ") + udhcpc.errorString()); + emit dhcpRequestFinished(QLatin1String("failed")); + return; + } + + udhcpc.waitForFinished(); + QByteArray error = udhcpc.readAllStandardError(); + QString status = QLatin1String("success"); + if (!error.isEmpty()) { + emit raiseError(QLatin1String("udhcpc failed: " + error)); + status = QLatin1String("failed"); + } else { + if (udhcpc.readAllStandardOutput().contains("No lease")) + status = QLatin1String("failed"); + } + + emit dhcpRequestFinished(status); +} + +void QWifiController::stopDhcp() const +{ + qCDebug(B2QT_WIFI, "stopDhcp"); + QString filePath = QLatin1String("/var/run/udhcpc." + m_interface + ".pid"); + killDhcpProcess(filePath); +} + +QT_END_NAMESPACE diff --git a/src/networksettings/wpasupplicant/qwificontroller_p.h b/src/networksettings/wpasupplicant/qwificontroller_p.h new file mode 100644 index 0000000..902bc6e --- /dev/null +++ b/src/networksettings/wpasupplicant/qwificontroller_p.h @@ -0,0 +1,116 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc +** All rights reserved. +** For any questions to Digia, please use the contact form at +** http://www.qt.io +** +** This file is part of Qt Enterprise Embedded. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** the contact form at http://www.qt.io +** +****************************************************************************/ +#ifndef QWIFICONTROLLER_H +#define QWIFICONTROLLER_H + +#include <QtCore/QEvent> +#include <QtCore/QVector> +#include <QtCore/QThread> +#include <QtCore/QMutex> +#include <QtCore/QWaitCondition> +#include <QtCore/QLoggingCategory> + +QT_BEGIN_NAMESPACE + +Q_DECLARE_LOGGING_CATEGORY(B2QT_WIFI) + +const QEvent::Type WIFI_SCAN_RESULTS = (QEvent::Type) (QEvent::User + 2001); +const QEvent::Type WIFI_CONNECTED = (QEvent::Type) (QEvent::User + 2002); +const QEvent::Type WIFI_HANDSHAKE_FAILED = (QEvent::Type) (QEvent::User + 2003); +const QEvent::Type WIFI_AUTHENTICATING = (QEvent::Type) (QEvent::User + 2004); +const QEvent::Type WIFI_DISCONNECTED = (QEvent::Type) (QEvent::User + 2005); + +class QWifiEventThread; +class QWifiSupplicant; +class QNetworkSettingsManagerPrivate; + +class QWifiEvent : public QEvent +{ +public: + QWifiEvent(QEvent::Type type, const QString &data = QString()) + : QEvent(type) + , m_data(data) + { + } + QString data() const { return m_data; } + +private: + QString m_data; +}; + +class QWifiController : public QThread +{ + Q_OBJECT + Q_ENUMS(BackendState) +public: + enum Method { + InitializeBackend, + TerminateBackend, + AcquireIPAddress, + StopDhcp, + ExitEventLoop + }; + + enum BackendState { + Initializing, + Running, + Terminating, + NotRunning + }; + + explicit QWifiController(QNetworkSettingsManagerPrivate *manager); + ~QWifiController(); + + void asyncCall(Method method); + QNetworkSettingsManagerPrivate *manager() const { return m_manager; } + bool isWifiThreadExitRequested() const { return m_exitEventThread; } + void startWifiEventThread(); + void acquireIPAddress(); + void stopDhcp() const; + bool resetSupplicantSocket(); + QWifiSupplicant *supplicant() const { return m_supplicant; } + +signals: + void backendStateChanged(BackendState backendState); + void dhcpRequestFinished(const QString &status); + void raiseError(const QString &error); + +protected: + void run(); + void initializeBackend(); + void terminateBackend(); + void exitWifiEventThread(); + void killDhcpProcess(const QString &path) const; + +private: + QNetworkSettingsManagerPrivate *m_manager; //not owned + bool m_exitEventThread; + QByteArray m_interface; + QVector<Method> m_methods; + QWifiEventThread *m_eventThread; + QMutex m_methodsMutex; + QWaitCondition methodCallRequested; + QWifiSupplicant *m_supplicant; +}; + +Q_DECLARE_METATYPE(QWifiController::BackendState) + +QT_END_NAMESPACE + +#endif // QWIFICONTROLLER_H diff --git a/src/networksettings/wpasupplicant/qwifidevice.cpp b/src/networksettings/wpasupplicant/qwifidevice.cpp new file mode 100644 index 0000000..a6812e7 --- /dev/null +++ b/src/networksettings/wpasupplicant/qwifidevice.cpp @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc +** All rights reserved. +** For any questions to Digia, please use the contact form at +** http://www.qt.io +** +** This file is part of Qt Enterprise Embedded. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** the contact form at http://www.qt.io +** +****************************************************************************/ +#include "qwifidevice.h" + +#include <QtCore/QString> +#include <QtCore/QByteArray> +#include <QtCore/QDir> + +QT_BEGIN_NAMESPACE + +/*! + \class QWifiDevice + \inmodule B2Qt.Wifi.Cpp + \ingroup wifi-cppclasses + \brief Represents a physical device. + + Use this class to query if a device is Wifi capable, before attempting + to use the functionality of QWifiManager. + + \code + QWifiManager *m_wifiManager = 0; + if (QWifiDevice::wifiSupported()) + m_wifiManager = QWifiManager::instance(); + + if (m_wifiManager) { + m_wifiManager->start(); + // and other wifi related code + } + \endcode + */ + +QWifiDevice::QWifiDevice() +{ +} + +QWifiDevice::~QWifiDevice() +{ +} + +/*! + Returns \c true if a device is Wifi capable - Wifi driver and firmware has been + successfully loaded by the system, otherwise returns \c false. +*/ +bool QWifiDevice::wifiSupported() +{ + QByteArray ifc = wifiInterfaceName(); + bool hasInterface = QDir().exists(QString::fromLatin1("/sys/class/net/" + ifc)); + if (!hasInterface) + qCWarning(B2QT_WIFI) << "could not find wifi interface in \"/sys/class/net/\", " + "looking for interface named: " << ifc; + return hasInterface; +} + +/*! + Returns Wifi interface name. + + Interface name is read from the \c B2QT_WIFI_INTERFACE + environment variable if it is set, otherwise, the default interface + name ("\e{wlan0}") is used. + + \sa setWifiInterfaceName() + */ +QByteArray QWifiDevice::wifiInterfaceName() +{ + return qEnvironmentVariableIsSet("B2QT_WIFI_INTERFACE") ? qgetenv("B2QT_WIFI_INTERFACE") : "wlan0"; +} + +/*! + A conveniece method to set the Wifi interface \a name. + + \sa wifiInterfaceName() + */ +void QWifiDevice::setWifiInterfaceName(const QByteArray &name) +{ + qputenv("B2QT_WIFI_INTERFACE", name); +} + +QT_END_NAMESPACE diff --git a/src/networksettings/wpasupplicant/qwifidevice.h b/src/networksettings/wpasupplicant/qwifidevice.h new file mode 100644 index 0000000..80b4891 --- /dev/null +++ b/src/networksettings/wpasupplicant/qwifidevice.h @@ -0,0 +1,44 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc +** All rights reserved. +** For any questions to Digia, please use the contact form at +** http://www.qt.io +** +** This file is part of Qt Enterprise Embedded. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** the contact form at http://www.qt.io +** +****************************************************************************/ +#ifndef QWIFIDEVICE_H +#define QWIFIDEVICE_H + +#include <QtCore/QObject> +#include <QtCore/QByteArray> +#include <QtCore/QLoggingCategory> + +QT_BEGIN_NAMESPACE + +Q_DECLARE_LOGGING_CATEGORY(B2QT_WIFI) + +class Q_DECL_EXPORT QWifiDevice : public QObject +{ + Q_OBJECT +public: + explicit QWifiDevice(); + virtual ~QWifiDevice(); + + Q_INVOKABLE static bool wifiSupported(); + static QByteArray wifiInterfaceName(); + static void setWifiInterfaceName(const QByteArray &name); +}; + +QT_END_NAMESPACE + +#endif // QWIFIDEVICE_H diff --git a/src/networksettings/wpasupplicant/qwifisupplicant.cpp b/src/networksettings/wpasupplicant/qwifisupplicant.cpp new file mode 100644 index 0000000..779475e --- /dev/null +++ b/src/networksettings/wpasupplicant/qwifisupplicant.cpp @@ -0,0 +1,443 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc +** All rights reserved. +** For any questions to Digia, please use the contact form at +** http://www.qt.io +** +** This file is part of Qt Enterprise Embedded. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** the contact form at http://www.qt.io +** +****************************************************************************/ +#include "qwifisupplicant_p.h" +#include "qwifidevice.h" + +#include <poll.h> +#include <unistd.h> +#include <sys/socket.h> +#include <errno.h> + +#include <QtCore/QFile> +#include <QtCore/QProcess> + +QT_BEGIN_NAMESPACE + +Q_LOGGING_CATEGORY(B2QT_WIFI_VERBOSE, "qt.b2qt.wifi.verbose") + +#define CONFIG_FILE "/etc/wpa_supplicant.qtwifi.conf" +#define CONTROL_INTERFACE_PATH "/var/run/wpa_supplicant/" + +QWifiSupplicant::QWifiSupplicant(QObject *parent) : + QObject(parent), + ctrl_conn(0), + monitor_conn(0), + interface(QWifiDevice::wifiInterfaceName()) +{ + createSupplicantConfig(); +} + +void QWifiSupplicant::createSupplicantConfig() +{ + QFile supplicantConfig(QLatin1String(CONFIG_FILE)); + if (supplicantConfig.exists()) + return; + + if (supplicantConfig.open(QIODevice::WriteOnly)) { + supplicantConfig.write("ctrl_interface=" CONTROL_INTERFACE_PATH "\n" + "ctrl_interface_group=0\n" + "update_config=1\n"); + } else { + emit raiseError(QLatin1String("failed to create wpa_supplicant configuration file.")); + } +} + +bool QWifiSupplicant::startSupplicant() +{ + QString pidFile = QLatin1String("/var/run/wpa_supplicant." + interface + ".pid"); + QString driver(QStringLiteral("nl80211,wext")); + + QStringList arg; + arg << QStringLiteral("--start") << QStringLiteral("--quiet") << QStringLiteral("--name"); + arg << QStringLiteral("wpa_supplicant") << QStringLiteral("--startas"); + arg << QStringLiteral("/usr/sbin/wpa_supplicant") << QStringLiteral("--pidfile") << pidFile; + arg << QStringLiteral("--") << QStringLiteral("-B") << QStringLiteral("-P") << pidFile; + arg << QStringLiteral("-i") << QLatin1String(interface) << QStringLiteral("-c"); + arg << QLatin1String(CONFIG_FILE) << QStringLiteral("-D") << driver; + + QProcess startStopDaemon; + startStopDaemon.setProcessChannelMode(QProcess::MergedChannels); + startStopDaemon.start(QStringLiteral("start-stop-daemon"), arg); + if (!startStopDaemon.waitForStarted()) { + emit raiseError(startStopDaemon.program() + QLatin1String(": ") + startStopDaemon.errorString()); + return false; + } + startStopDaemon.waitForFinished(); + // if the interface socket exists then wpa-supplicant was invoked successfully + if (!QFile(QLatin1String(CONTROL_INTERFACE_PATH + interface)).exists()) { + emit raiseError(QLatin1String("failed to invoke wpa_supplicant: " + + startStopDaemon.readAll())); + return false; + } + // reset sockets used for exiting from hung state + exit_sockets[0] = exit_sockets[1] = -1; + return true; +} + +bool QWifiSupplicant::stopSupplicant() +{ + QString pidFile = QLatin1String("/var/run/wpa_supplicant." + interface + ".pid"); + + if (QFile(pidFile).exists()) { + QStringList arg; + arg << QStringLiteral("--stop") << QStringLiteral("--quiet") << QStringLiteral("--name"); + arg << QStringLiteral("wpa_supplicant") << QStringLiteral("--pidfile") << pidFile; + + QProcess startStopDaemon; + startStopDaemon.start(QStringLiteral("start-stop-daemon"), arg); + if (!startStopDaemon.waitForStarted()) { + emit raiseError(startStopDaemon.program() + QLatin1String(": ") + startStopDaemon.errorString()); + return false; + } + startStopDaemon.waitForFinished(); + QByteArray error = startStopDaemon.readAllStandardError(); + if (!error.isEmpty()) { + emit raiseError(QLatin1String("failed to stop a wpa_supplicant process" + error)); + return false; + } + + QFile::remove(pidFile); + } + + QFile::remove(QLatin1String(CONTROL_INTERFACE_PATH + interface)); + + // workaround for QTEE-957 + QProcess killall; + killall.start(QStringLiteral("killall"), QStringList() << QStringLiteral("-9") << QStringLiteral("wpa_supplicant")); + killall.waitForFinished(); + + return true; +} + +/*! \internal + * + wpa_supplicant socket communication code (Apache License 2.0) with few modifications + from https://android.googlesource.com/platform/hardware/libhardware_legacy/ + + */ +bool QWifiSupplicant::connectToSupplicant() +{ + static char path[4096]; + snprintf(path, sizeof(path), "%s/%s", CONTROL_INTERFACE_PATH, interface.constData()); + bool connected = true; + + ctrl_conn = wpa_ctrl_open(path); + if (ctrl_conn == NULL) { + qCWarning(B2QT_WIFI, "Unable to open connection to wpa_supplicant on \"%s\": %s", + path, strerror(errno)); + connected = false; + } + monitor_conn = wpa_ctrl_open(path); + if (monitor_conn == NULL) { + wpa_ctrl_close(ctrl_conn); + ctrl_conn = NULL; + connected = false; + } + if (wpa_ctrl_attach(monitor_conn) != 0) { + wpa_ctrl_close(monitor_conn); + wpa_ctrl_close(ctrl_conn); + ctrl_conn = monitor_conn = NULL; + connected = false; + } + + if (socketpair(AF_UNIX, SOCK_STREAM, 0, exit_sockets) == -1) { + wpa_ctrl_close(monitor_conn); + wpa_ctrl_close(ctrl_conn); + ctrl_conn = monitor_conn = NULL; + connected = false; + } + + if (!connected) + emit raiseError(QLatin1String("failed to connect to wpa_supplicant")); + return connected; +} + +void QWifiSupplicant::closeSupplicantConnection() +{ + if (ctrl_conn != NULL) { + wpa_ctrl_close(ctrl_conn); + ctrl_conn = NULL; + } + + if (monitor_conn != NULL) { + wpa_ctrl_close(monitor_conn); + monitor_conn = NULL; + } + + if (exit_sockets[0] >= 0) { + close(exit_sockets[0]); + exit_sockets[0] = -1; + } + + if (exit_sockets[1] >= 0) { + close(exit_sockets[1]); + exit_sockets[1] = -1; + } +} + +int QWifiSupplicant::waitForEvent(char *buf, size_t buflen) +{ + size_t nread = buflen - 1; + int result; + char *match, *match2; + + if (monitor_conn == NULL) + return snprintf(buf, buflen, WPA_EVENT_TERMINATING " - connection closed"); + + result = receiveEvent(buf, &nread); + + // Terminate reception on exit socket + if (result == -2) + return snprintf(buf, buflen, WPA_EVENT_TERMINATING " - connection closed"); + + if (result < 0) { + qCWarning(B2QT_WIFI, "receiveEvent failed: %s", strerror(errno)); + return snprintf(buf, buflen, WPA_EVENT_TERMINATING " - recv error"); + } + + buf[nread] = '\0'; + // Check for EOF on the socket + if (result == 0 && nread == 0) { + // Fabricate an event to pass up + qCWarning(B2QT_WIFI, "Received EOF on supplicant socket"); + return snprintf(buf, buflen, WPA_EVENT_TERMINATING " - signal 0 received"); + } + + /* + * Events strings are in the format + * + * IFNAME=iface <N>CTRL-EVENT-XXX + * or + * <N>CTRL-EVENT-XXX + * + * where N is the message level in numerical form (0=VERBOSE, 1=DEBUG, + * etc.) and XXX is the event name. The level information is not useful + * to us, so strip it off. + */ + + if (strncmp(buf, "IFNAME=", (sizeof("IFNAME=") - 1)) == 0) { + match = strchr(buf, ' '); + if (match != NULL) { + if (match[1] == '<') { + match2 = strchr(match + 2, '>'); + if (match2 != NULL) { + nread -= (match2 - match); + memmove(match + 1, match2 + 1, nread - (match - buf) + 1); + } + } + } else { + return snprintf(buf, buflen, "%s", "CTRL-EVENT-IGNORE "); + } + } else if (buf[0] == '<') { + match = strchr(buf, '>'); + if (match != NULL) { + nread -= (match + 1 - buf); + memmove(buf, match + 1, nread + 1); + //qCWarning(B2QT_WIFI, "supplicant generated event without interface - %s", buf); + } + } else { + // let the event go as is! + qCWarning(B2QT_WIFI, "supplicant generated event without interface and without message level - %s", buf); + } + + return nread; +} + +bool QWifiSupplicant::sendCommand(const QString &command, QByteArray *reply) +{ + QByteArray cmd = command.toLocal8Bit(); + qCDebug(B2QT_WIFI) << "[command]: " << cmd; + + if (ctrl_conn == NULL) { + qCWarning(B2QT_WIFI, "Not connected to wpa_supplicant"); + return false; + } + + char data[8192]; + size_t len = sizeof(data) - 1; // -1: room to add a 0-terminator + int ret = wpa_ctrl_request(ctrl_conn, cmd, strlen(cmd), data, &len, NULL); + if (ret == -2) { + qCWarning(B2QT_WIFI) << "command timed out"; + // unblocks the monitor receive socket for termination + TEMP_FAILURE_RETRY(write(exit_sockets[0], "T", 1)); + return false; + } else if (ret < 0 || strncmp(data, "FAIL", 4) == 0) { + return false; + } + + if (len == sizeof(data) - 1) { + qCWarning(B2QT_WIFI) << "possible buffer overflow detected"; + return false; + } + + data[len] = 0; + qCDebug(B2QT_WIFI_VERBOSE) << "[response]: " << data; + *reply = QByteArray(data, len); + + return true; +} + +int QWifiSupplicant::receiveEvent(char *reply, size_t *reply_len) +{ + int res = 0; + int ctrlfd = wpa_ctrl_get_fd(monitor_conn); + struct pollfd rfds[2]; + + memset(rfds, 0, 2 * sizeof(struct pollfd)); + rfds[0].fd = ctrlfd; + rfds[0].events |= POLLIN; + rfds[1].fd = exit_sockets[1]; + rfds[1].events |= POLLIN; + res = TEMP_FAILURE_RETRY(poll(rfds, 2, -1)); + if (res < 0) { + qCWarning(B2QT_WIFI, "Error poll = %d", res); + return res; + } + if (rfds[0].revents & POLLIN) { + return wpa_ctrl_recv(monitor_conn, reply, reply_len); + } + + /* it is not rfds[0], then it must be rfts[1] (i.e. the exit socket) + * or we timed out. In either case, this call has failed .. + */ + return -2; +} + +static inline int hex2num(char c) +{ + if (c >= '0' && c <= '9') + return c - '0'; + if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + return -1; +} + +static inline int hex2byte(const char *hex) +{ + int a, b; + a = hex2num(*hex++); + if (a < 0) + return -1; + b = hex2num(*hex++); + if (b < 0) + return -1; + return (a << 4) | b; +} + +static inline int printf_decode(char *buf, int maxlen, const char *str) +{ + const char *pos = str; + int len = 0; + int val; + + while (*pos) { + if (len + 1 >= maxlen) + break; + switch (*pos) { + case '\\': + pos++; + switch (*pos) { + case '\\': + buf[len++] = '\\'; + pos++; + break; + case '"': + buf[len++] = '"'; + pos++; + break; + case 'n': + buf[len++] = '\n'; + pos++; + break; + case 'r': + buf[len++] = '\r'; + pos++; + break; + case 't': + buf[len++] = '\t'; + pos++; + break; + case 'e': + buf[len++] = '\033'; + pos++; + break; + case 'x': + pos++; + val = hex2byte(pos); + if (val < 0) { + val = hex2num(*pos); + if (val < 0) + break; + buf[len++] = val; + pos++; + } else { + buf[len++] = val; + pos += 2; + } + break; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + val = *pos++ - '0'; + if (*pos >= '0' && *pos <= '7') + val = val * 8 + (*pos++ - '0'); + if (*pos >= '0' && *pos <= '7') + val = val * 8 + (*pos++ - '0'); + buf[len++] = val; + break; + default: + break; + } + break; + default: + buf[len++] = *pos++; + break; + } + } + if (maxlen > len) + buf[len] = '\0'; + + return len; +} + +/*! \internal + * + Decode wpa_supplicant encoded string, see file hostapd/src/utils/common.c + in git://w1.fi/hostap.git repository. + + For Ascii encoded string, any octet < 32 or > 127 is encoded as a "\\x" + followed by the hex representation of the octet. Exception chars are ", + \\, \\e, \\n, \\r, \\t which are escaped by a backslash + + */ +QString QWifiSupplicant::decodeSsid(const QString &encoded) +{ + static char ssid[2048]; + printf_decode(ssid, sizeof(ssid), encoded.toLatin1().constData()); + return QString::fromUtf8(ssid); +} + +QT_END_NAMESPACE diff --git a/src/networksettings/wpasupplicant/qwifisupplicant_p.h b/src/networksettings/wpasupplicant/qwifisupplicant_p.h new file mode 100644 index 0000000..13855c0 --- /dev/null +++ b/src/networksettings/wpasupplicant/qwifisupplicant_p.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc +** All rights reserved. +** For any questions to Digia, please use the contact form at +** http://www.qt.io +** +** This file is part of Qt Enterprise Embedded. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** the contact form at http://www.qt.io +** +****************************************************************************/ +#ifndef QWIFISUPPLICANT_H +#define QWIFISUPPLICANT_H + +#include <QObject> +#include <QByteArray> +#include <QLoggingCategory> + +#include "wpa-supplicant/wpa_ctrl.h" + +QT_BEGIN_NAMESPACE + +Q_DECLARE_LOGGING_CATEGORY(B2QT_WIFI) +Q_DECLARE_LOGGING_CATEGORY(B2QT_WIFI_VERBOSE) + +class QWifiManagerPrivate; + +class QWifiSupplicant : public QObject +{ + Q_OBJECT +public: + explicit QWifiSupplicant(QObject *parent); + + void createSupplicantConfig(); + bool startSupplicant(); + bool stopSupplicant(); + bool connectToSupplicant(); + void closeSupplicantConnection(); + int waitForEvent(char *buf, size_t buflen); + bool sendCommand(const QString &command, QByteArray *reply); + static QString decodeSsid(const QString &encoded); + +signals: + void raiseError(const QString& error); + +protected: + int receiveEvent(char *reply, size_t *reply_len); + +private: + wpa_ctrl *ctrl_conn; + wpa_ctrl *monitor_conn; + int exit_sockets[2]; + QByteArray interface; +}; + +QT_END_NAMESPACE + +#endif // QWIFISUPPLICANT_H |