ui: remember previous connections (#21233)
* remember previous connections * minor * remove ssid_from_path * abstract into function * forgot one * const * add path_from_ssid function * more clear what these do * fix * more clear * structured binding test * remove ssid_from_path * fix name * disconnect if connected * let's just match the activate function in naming * return if empty * revert and clean up
This commit is contained in:
parent
fcf9f00a2a
commit
305786f0da
|
@ -104,7 +104,9 @@ void Networking::refresh() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Networking::connectToNetwork(const Network &n) {
|
void Networking::connectToNetwork(const Network &n) {
|
||||||
if (n.security_type == SecurityType::OPEN) {
|
if (wifi->isKnownNetwork(n.ssid)) {
|
||||||
|
wifi->activateWifiConnection(n.ssid);
|
||||||
|
} else if (n.security_type == SecurityType::OPEN) {
|
||||||
wifi->connect(n);
|
wifi->connect(n);
|
||||||
} else if (n.security_type == SecurityType::WPA) {
|
} else if (n.security_type == SecurityType::WPA) {
|
||||||
QString pass = InputDialog::getText("Enter password for \"" + n.ssid + "\"", 8);
|
QString pass = InputDialog::getText("Enter password for \"" + n.ssid + "\"", 8);
|
||||||
|
|
|
@ -213,7 +213,7 @@ void WifiManager::connect(const Network &n, const QString &password) {
|
||||||
void WifiManager::connect(const Network &n, const QString &username, const QString &password) {
|
void WifiManager::connect(const Network &n, const QString &username, const QString &password) {
|
||||||
connecting_to_network = n.ssid;
|
connecting_to_network = n.ssid;
|
||||||
// disconnect();
|
// disconnect();
|
||||||
clear_connections(n.ssid); //Clear all connections that may already exist to the network we are connecting
|
forgetNetwork(n.ssid); //Clear all connections that may already exist to the network we are connecting
|
||||||
connect(n.ssid, username, password, n.security_type);
|
connect(n.ssid, username, password, n.security_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,10 +240,10 @@ void WifiManager::connect(const QByteArray &ssid, const QString &username, const
|
||||||
nm_settings.setTimeout(dbus_timeout);
|
nm_settings.setTimeout(dbus_timeout);
|
||||||
|
|
||||||
nm_settings.call("AddConnection", QVariant::fromValue(connection));
|
nm_settings.call("AddConnection", QVariant::fromValue(connection));
|
||||||
activate_wifi_connection(QString(ssid));
|
activateWifiConnection(QString(ssid));
|
||||||
}
|
}
|
||||||
|
|
||||||
void WifiManager::deactivate_connections(const QString &ssid) {
|
void WifiManager::deactivateConnection(const QString &ssid) {
|
||||||
for (QDBusObjectPath active_connection_raw : get_active_connections()) {
|
for (QDBusObjectPath active_connection_raw : get_active_connections()) {
|
||||||
QString active_connection = active_connection_raw.path();
|
QString active_connection = active_connection_raw.path();
|
||||||
QDBusInterface nm(nm_service, active_connection, props_iface, bus);
|
QDBusInterface nm(nm_service, active_connection, props_iface, bus);
|
||||||
|
@ -255,7 +255,7 @@ void WifiManager::deactivate_connections(const QString &ssid) {
|
||||||
if (Ssid == ssid) {
|
if (Ssid == ssid) {
|
||||||
QDBusInterface nm2(nm_service, nm_path, nm_iface, bus);
|
QDBusInterface nm2(nm_service, nm_path, nm_iface, bus);
|
||||||
nm2.setTimeout(dbus_timeout);
|
nm2.setTimeout(dbus_timeout);
|
||||||
nm2.call("DeactivateConnection", QVariant::fromValue(active_connection_raw));// TODO change to disconnect
|
nm2.call("DeactivateConnection", QVariant::fromValue(active_connection_raw));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -279,27 +279,15 @@ QVector<QDBusObjectPath> WifiManager::get_active_connections() {
|
||||||
return conns;
|
return conns;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WifiManager::clear_connections(const QString &ssid) {
|
bool WifiManager::isKnownNetwork(const QString &ssid) {
|
||||||
for(QDBusObjectPath path : list_connections()) {
|
return !pathFromSsid(ssid).path().isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
void WifiManager::forgetNetwork(const QString &ssid) {
|
||||||
|
QDBusObjectPath path = pathFromSsid(ssid);
|
||||||
|
if (!path.path().isEmpty()) {
|
||||||
QDBusInterface nm2(nm_service, path.path(), nm_settings_conn_iface, bus);
|
QDBusInterface nm2(nm_service, path.path(), nm_settings_conn_iface, bus);
|
||||||
nm2.setTimeout(dbus_timeout);
|
nm2.call("Delete");
|
||||||
|
|
||||||
QDBusMessage response = nm2.call("GetSettings");
|
|
||||||
|
|
||||||
const QDBusArgument &dbusArg = response.arguments().at(0).value<QDBusArgument>();
|
|
||||||
|
|
||||||
QMap<QString, QMap<QString,QVariant>> map;
|
|
||||||
dbusArg >> map;
|
|
||||||
for (auto &inner : map) {
|
|
||||||
for (auto &val : inner) {
|
|
||||||
QString key = inner.key(val);
|
|
||||||
if (key == "ssid") {
|
|
||||||
if (val == ssid) {
|
|
||||||
nm2.call("Delete");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -388,12 +376,22 @@ void WifiManager::change(unsigned int new_state, unsigned int previous_state, un
|
||||||
void WifiManager::disconnect() {
|
void WifiManager::disconnect() {
|
||||||
QString active_ap = get_active_ap();
|
QString active_ap = get_active_ap();
|
||||||
if (active_ap != "" && active_ap != "/") {
|
if (active_ap != "" && active_ap != "/") {
|
||||||
deactivate_connections(get_property(active_ap, "Ssid"));
|
deactivateConnection(get_property(active_ap, "Ssid"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QVector<QDBusObjectPath> WifiManager::list_connections() {
|
QDBusObjectPath WifiManager::pathFromSsid(const QString &ssid) {
|
||||||
QVector<QDBusObjectPath> connections;
|
QDBusObjectPath path; // returns uninitialized path if network is not known
|
||||||
|
for (auto const& [conn_ssid, conn_path] : listConnections()) {
|
||||||
|
if (conn_ssid == ssid) {
|
||||||
|
path = conn_path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
QVector<QPair<QString, QDBusObjectPath>> WifiManager::listConnections() {
|
||||||
|
QVector<QPair<QString, QDBusObjectPath>> connections;
|
||||||
QDBusInterface nm(nm_service, nm_settings_path, nm_settings_iface, bus);
|
QDBusInterface nm(nm_service, nm_settings_path, nm_settings_iface, bus);
|
||||||
nm.setTimeout(dbus_timeout);
|
nm.setTimeout(dbus_timeout);
|
||||||
|
|
||||||
|
@ -404,68 +402,33 @@ QVector<QDBusObjectPath> WifiManager::list_connections() {
|
||||||
while (!args.atEnd()) {
|
while (!args.atEnd()) {
|
||||||
QDBusObjectPath path;
|
QDBusObjectPath path;
|
||||||
args >> path;
|
args >> path;
|
||||||
connections.push_back(path);
|
|
||||||
|
// Get ssid
|
||||||
|
QDBusInterface nm2(nm_service, path.path(), nm_settings_conn_iface, bus);
|
||||||
|
nm2.setTimeout(dbus_timeout);
|
||||||
|
|
||||||
|
QDBusMessage response = nm2.call("GetSettings");
|
||||||
|
const QDBusArgument &dbusArg = response.arguments().at(0).value<QDBusArgument>();
|
||||||
|
QMap<QString, QMap<QString, QVariant>> map;
|
||||||
|
dbusArg >> map;
|
||||||
|
|
||||||
|
const QString ssid = map.value("802-11-wireless").value("ssid").toString();
|
||||||
|
connections.push_back(qMakePair(ssid, path));
|
||||||
}
|
}
|
||||||
return connections;
|
return connections;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WifiManager::activate_wifi_connection(const QString &ssid) {
|
void WifiManager::activateWifiConnection(const QString &ssid) {
|
||||||
QString devicePath = get_adapter();
|
QDBusObjectPath path = pathFromSsid(ssid);
|
||||||
|
if (!path.path().isEmpty()) {
|
||||||
for(QDBusObjectPath path : list_connections()) {
|
QString devicePath = get_adapter();
|
||||||
QDBusInterface nm2(nm_service, path.path(), nm_settings_conn_iface, bus);
|
QDBusInterface nm3(nm_service, nm_path, nm_iface, bus);
|
||||||
nm2.setTimeout(dbus_timeout);
|
nm3.setTimeout(dbus_timeout);
|
||||||
|
nm3.call("ActivateConnection", QVariant::fromValue(path), QVariant::fromValue(QDBusObjectPath(devicePath)), QVariant::fromValue(QDBusObjectPath("/")));
|
||||||
QDBusMessage response = nm2.call("GetSettings");
|
|
||||||
const QDBusArgument &dbusArg = response.arguments().at(0).value<QDBusArgument>();
|
|
||||||
|
|
||||||
QMap<QString, QMap<QString,QVariant>> map;
|
|
||||||
dbusArg >> map;
|
|
||||||
for (auto &inner : map) {
|
|
||||||
for (auto &val : inner) {
|
|
||||||
QString key = inner.key(val);
|
|
||||||
if (key == "ssid") {
|
|
||||||
if (val == ssid) {
|
|
||||||
QDBusInterface nm3(nm_service, nm_path, nm_iface, bus);
|
|
||||||
nm3.setTimeout(dbus_timeout);
|
|
||||||
nm3.call("ActivateConnection", QVariant::fromValue(path), QVariant::fromValue(QDBusObjectPath(devicePath)), QVariant::fromValue(QDBusObjectPath("/")));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
//Functions for tethering
|
|
||||||
bool WifiManager::activate_tethering_connection() {
|
|
||||||
QString devicePath = get_adapter();
|
|
||||||
|
|
||||||
for(QDBusObjectPath path : list_connections()) {
|
// Functions for tethering
|
||||||
QDBusInterface nm2(nm_service, path.path(), nm_settings_conn_iface, bus);
|
|
||||||
nm2.setTimeout(dbus_timeout);
|
|
||||||
|
|
||||||
QDBusMessage response = nm2.call("GetSettings");
|
|
||||||
const QDBusArgument &dbusArg = response.arguments().at(0).value<QDBusArgument>();
|
|
||||||
|
|
||||||
QMap<QString, QMap<QString,QVariant>> map;
|
|
||||||
dbusArg >> map;
|
|
||||||
for (auto &inner : map) {
|
|
||||||
for (auto &val : inner) {
|
|
||||||
QString key = inner.key(val);
|
|
||||||
if (key == "ssid") {
|
|
||||||
if (val == tethering_ssid.toUtf8()) {
|
|
||||||
QDBusInterface nm3(nm_service, nm_path, nm_iface, bus);
|
|
||||||
nm3.setTimeout(dbus_timeout);
|
|
||||||
nm3.call("ActivateConnection", QVariant::fromValue(path), QVariant::fromValue(QDBusObjectPath(devicePath)), QVariant::fromValue(QDBusObjectPath("/")));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
void WifiManager::addTetheringConnection() {
|
void WifiManager::addTetheringConnection() {
|
||||||
Connection connection;
|
Connection connection;
|
||||||
connection["connection"]["id"] = "Hotspot";
|
connection["connection"]["id"] = "Hotspot";
|
||||||
|
@ -499,15 +462,14 @@ void WifiManager::addTetheringConnection() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void WifiManager::enableTethering() {
|
void WifiManager::enableTethering() {
|
||||||
if(activate_tethering_connection()) {
|
if (!isKnownNetwork(tethering_ssid.toUtf8())) {
|
||||||
return;
|
addTetheringConnection();
|
||||||
}
|
}
|
||||||
addTetheringConnection();
|
activateWifiConnection(tethering_ssid.toUtf8());
|
||||||
activate_tethering_connection();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WifiManager::disableTethering() {
|
void WifiManager::disableTethering() {
|
||||||
deactivate_connections(tethering_ssid.toUtf8());
|
deactivateConnection(tethering_ssid.toUtf8());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WifiManager::tetheringEnabled() {
|
bool WifiManager::tetheringEnabled() {
|
||||||
|
@ -517,6 +479,8 @@ bool WifiManager::tetheringEnabled() {
|
||||||
|
|
||||||
void WifiManager::changeTetheringPassword(const QString &newPassword) {
|
void WifiManager::changeTetheringPassword(const QString &newPassword) {
|
||||||
tetheringPassword = newPassword;
|
tetheringPassword = newPassword;
|
||||||
clear_connections(tethering_ssid.toUtf8());
|
if (isKnownNetwork(tethering_ssid.toUtf8())) {
|
||||||
|
forgetNetwork(tethering_ssid.toUtf8());
|
||||||
|
}
|
||||||
addTetheringConnection();
|
addTetheringConnection();
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,8 @@ public:
|
||||||
QString ipv4_address;
|
QString ipv4_address;
|
||||||
|
|
||||||
void refreshNetworks();
|
void refreshNetworks();
|
||||||
|
bool isKnownNetwork(const QString &ssid);
|
||||||
|
|
||||||
void connect(const Network &ssid);
|
void connect(const Network &ssid);
|
||||||
void connect(const Network &ssid, const QString &password);
|
void connect(const Network &ssid, const QString &password);
|
||||||
void connect(const Network &ssid, const QString &username, const QString &password);
|
void connect(const Network &ssid, const QString &username, const QString &password);
|
||||||
|
@ -45,9 +47,8 @@ public:
|
||||||
void disableTethering();
|
void disableTethering();
|
||||||
bool tetheringEnabled();
|
bool tetheringEnabled();
|
||||||
|
|
||||||
bool activate_tethering_connection();
|
|
||||||
void addTetheringConnection();
|
void addTetheringConnection();
|
||||||
bool activate_wifi_connection(const QString &ssid);
|
void activateWifiConnection(const QString &ssid);
|
||||||
void changeTetheringPassword(const QString &newPassword);
|
void changeTetheringPassword(const QString &newPassword);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -64,14 +65,15 @@ private:
|
||||||
QList<Network> get_networks();
|
QList<Network> get_networks();
|
||||||
void connect(const QByteArray &ssid, const QString &username, const QString &password, SecurityType security_type);
|
void connect(const QByteArray &ssid, const QString &username, const QString &password, SecurityType security_type);
|
||||||
QString get_active_ap();
|
QString get_active_ap();
|
||||||
void deactivate_connections(const QString &ssid);
|
void deactivateConnection(const QString &ssid);
|
||||||
void clear_connections(const QString &ssid);
|
void forgetNetwork(const QString &ssid);
|
||||||
QVector<QDBusObjectPath> get_active_connections();
|
QVector<QDBusObjectPath> get_active_connections();
|
||||||
uint get_wifi_device_state();
|
uint get_wifi_device_state();
|
||||||
QByteArray get_property(const QString &network_path, const QString &property);
|
QByteArray get_property(const QString &network_path, const QString &property);
|
||||||
unsigned int get_ap_strength(const QString &network_path);
|
unsigned int get_ap_strength(const QString &network_path);
|
||||||
SecurityType getSecurityType(const QString &ssid);
|
SecurityType getSecurityType(const QString &ssid);
|
||||||
QVector<QDBusObjectPath> list_connections();
|
QDBusObjectPath pathFromSsid(const QString &ssid);
|
||||||
|
QVector<QPair<QString, QDBusObjectPath>> listConnections();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void change(unsigned int new_state, unsigned int previous_state, unsigned int change_reason);
|
void change(unsigned int new_state, unsigned int previous_state, unsigned int change_reason);
|
||||||
|
|
Loading…
Reference in a new issue