You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

303 lines
11 KiB

#include "networkselector.h"
#include "./ui_networkselector.h"
#include <QDebug>
NetworkSelector::NetworkSelector(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::NetworkSelector)
{
ui->setupUi(this);
this->setWindowTitle(tr("Connection Editor"));
regenConnTree();
connect(ui->connTree, &QTreeView::clicked, this, &NetworkSelector::onTreeSingleClicked);
connect(ui->connTree, &QTreeView::doubleClicked, this, &NetworkSelector::onTreeDoubleClicked);
connect(ui->createConnButton, &QPushButton::clicked, this, &NetworkSelector::onCreateClicked);
connect(ui->deleteConnButton, &QPushButton::clicked, this, &NetworkSelector::onDeleteClicked);
connect(ui->modifyConnButton, &QPushButton::clicked, this, &NetworkSelector::onModifyClicked);
}
NetworkSelector::~NetworkSelector()
{
delete ui;
delete connTreeModel;
}
void NetworkSelector::onTreeSingleClicked(QModelIndex index)
{
QVariant connUuid = index.data(Qt::UserRole+1);
if (!connUuid.isValid()) {
ui->deleteConnButton->setEnabled(false);
ui->modifyConnButton->setEnabled(false);
return;
}
ui->deleteConnButton->setEnabled(true);
ui->modifyConnButton->setEnabled(true);
}
void NetworkSelector::onTreeDoubleClicked(QModelIndex index)
{
modifyConnection(index);
}
void NetworkSelector::onCreateClicked()
{
dialogWindow = new NetworkCreator();
dialogWindow->exec();
delete dialogWindow;
regenConnTree();
}
void NetworkSelector::onDeleteClicked()
{
QModelIndexList selectedNetworks = ui->connTree->selectionModel()->selectedIndexes();
if (selectedNetworks.length() == 0) {
return;
}
if (!selectedNetworks[0].data(Qt::UserRole+1).isValid()) {
return;
}
dialogWindow = new NetworkDeleter(selectedNetworks[0].data().toString(), selectedNetworks[0].data(Qt::UserRole+1).toString());
dialogWindow->exec();
delete dialogWindow;
regenConnTree();
}
void NetworkSelector::onModifyClicked()
{
QModelIndexList selectedNetworks = ui->connTree->selectionModel()->selectedIndexes();
if (selectedNetworks.length() == 0) {
return;
}
if (!selectedNetworks[0].data(Qt::UserRole+1).isValid()) {
return;
}
modifyConnection(selectedNetworks[0]);
}
QStandardItem *NetworkSelector::connGroupItem(QString itemName)
{
for (int i = 0;i < connTreeList.length();i++) {
if (connTreeList[i]->text() == itemName) {
return connTreeList[i];
}
}
return NULL;
}
void NetworkSelector::regenConnTree()
{
if (connTreeModel != NULL) {
delete connTreeModel;
}
connTreeList = QList<QStandardItem *>();
connTreeModel = new QStandardItemModel(0, 2);
connTreeModel->setHeaderData(0, Qt::Orientation::Horizontal, "Connection");
connTreeModel->setHeaderData(1, Qt::Orientation::Horizontal, "Last used");
connList = NetworkManager::listConnections();
for (int i = 0;i < connList.length();i++) {
if (connList[i]->name() == "lo") { // don't show the loopback device
continue;
}
QList<QStandardItem *> infoRow;
infoRow.append(new QStandardItem(connList[i]->name()));
infoRow.append(new QStandardItem(connList[i]->settings()->timestamp().toString("yyyy-MM-dd hh:mm")));
// When the user double-clicks a cell in the tree, the double-click handler gets an object that can fetch a data value from the clicked cell.
// We need to identify the exact connection that was double-clicked on in order for this to work, so we bind the connection's UUID to both visible
// cells in the row representing a connection.
infoRow[0]->setData(connList[i]->uuid());
infoRow[1]->setData(connList[i]->uuid());
NetworkManager::ConnectionSettings::ConnectionType connType = connList[i]->settings()->connectionType();
QStandardItem *targetItem;
switch(connType) {
case NetworkManager::ConnectionSettings::Wired:
targetItem = connGroupItem("Ethernet");
if (targetItem == NULL) {
targetItem = new QStandardItem("Ethernet");
connTreeList.append(targetItem);
}
break;
case NetworkManager::ConnectionSettings::Wireless:
targetItem = connGroupItem("WiFi");
if (targetItem == NULL) {
targetItem = new QStandardItem("WiFi");
connTreeList.append(targetItem);
}
break;
case NetworkManager::ConnectionSettings::Adsl:
case NetworkManager::ConnectionSettings::Pppoe:
targetItem = connGroupItem("DSL");
if (targetItem == NULL) {
targetItem = new QStandardItem("DSL");
connTreeList.append(targetItem);
}
break;
case NetworkManager::ConnectionSettings::Bluetooth:
targetItem = connGroupItem("Bluetooth");
if (targetItem == NULL) {
targetItem = new QStandardItem("Bluetooth");
connTreeList.append(targetItem);
}
break;
case NetworkManager::ConnectionSettings::Bond:
case NetworkManager::ConnectionSettings::Bridge:
case NetworkManager::ConnectionSettings::Vlan:
targetItem = connGroupItem("Virtual");
if (targetItem == NULL) {
targetItem = new QStandardItem("Virtual");
connTreeList.append(targetItem);
}
break;
case NetworkManager::ConnectionSettings::Cdma:
case NetworkManager::ConnectionSettings::Gsm:
targetItem = connGroupItem("Mobile");
if (targetItem == NULL) {
targetItem = new QStandardItem("Mobile");
connTreeList.append(targetItem);
}
break;
case NetworkManager::ConnectionSettings::Infiniband:
targetItem = connGroupItem("Infiniband");
if (targetItem == NULL) {
targetItem = new QStandardItem("Infiniband");
connTreeList.append(targetItem);
}
break;
case NetworkManager::ConnectionSettings::OLPCMesh:
targetItem = connGroupItem("OLPC-Mesh");
if (targetItem == NULL) {
targetItem = new QStandardItem("OLPC-Mesh");
connTreeList.append(targetItem);
}
break;
case NetworkManager::ConnectionSettings::Vpn:
case NetworkManager::ConnectionSettings::WireGuard:
targetItem = connGroupItem("VPN");
if (targetItem == NULL) {
targetItem = new QStandardItem("VPN");
connTreeList.append(targetItem);
}
break;
case NetworkManager::ConnectionSettings::Tun:
targetItem = connGroupItem("Tun");
if (targetItem == NULL) {
targetItem = new QStandardItem("Tun");
connTreeList.append(targetItem);
}
break;
case NetworkManager::ConnectionSettings::IpTunnel:
targetItem = connGroupItem("IpTunnel");
if (targetItem == NULL) {
targetItem = new QStandardItem("IpTunnel");
connTreeList.append(targetItem);
}
break;
case NetworkManager::ConnectionSettings::Wimax:
targetItem = connGroupItem("WiMAX");
if (targetItem == NULL) {
targetItem = new QStandardItem("WiMAX");
connTreeList.append(targetItem);
}
break;
case NetworkManager::ConnectionSettings::Team:
targetItem = connGroupItem("Team");
if (targetItem == NULL) {
targetItem = new QStandardItem("Team");
connTreeList.append(targetItem);
}
break;
case NetworkManager::ConnectionSettings::Generic:
targetItem = connGroupItem("Generic");
if (targetItem == NULL) {
targetItem = new QStandardItem("Generic");
connTreeList.append(targetItem);
}
break;
case NetworkManager::ConnectionSettings::Unknown:
targetItem = connGroupItem("Unknown");
if (targetItem == NULL) {
targetItem = new QStandardItem("Unknown");
connTreeList.append(targetItem);
}
break;
}
targetItem->appendRow(infoRow);
}
for (int i = 0;i < connTreeList.length();i++) {
QList<QStandardItem *> currentItemWrap;
currentItemWrap.append(connTreeList[i]);
connTreeModel->appendRow(currentItemWrap);
}
connTreeModel->sort(0);
ui->modifyConnButton->setEnabled(false);
ui->deleteConnButton->setEnabled(false);
ui->connTree->setModel(connTreeModel);
ui->connTree->setColumnWidth(0, 500);
}
void NetworkSelector::modifyConnection(QModelIndex index)
{
QVariant connUuid = index.data(Qt::UserRole+1);
if (!connUuid.isValid()) {
return;
}
for (int i = 0;i < connList.length();i++) {
if (connUuid.toString() == connList[i]->uuid()) {
NetworkManager::ConnectionSettings::ConnectionType connType = connList[i]->settings()->connectionType();
switch(connType) {
case NetworkManager::ConnectionSettings::Wired:
dialogWindow = new EthernetSettings(index.data(Qt::UserRole+1).toString());
dialogWindow->exec();
delete dialogWindow;
regenConnTree();
break;
case NetworkManager::ConnectionSettings::Wireless:
case NetworkManager::ConnectionSettings::Adsl:
case NetworkManager::ConnectionSettings::Pppoe:
case NetworkManager::ConnectionSettings::Bluetooth:
case NetworkManager::ConnectionSettings::Bond:
case NetworkManager::ConnectionSettings::Bridge:
case NetworkManager::ConnectionSettings::Vlan:
case NetworkManager::ConnectionSettings::Cdma:
case NetworkManager::ConnectionSettings::Gsm:
case NetworkManager::ConnectionSettings::Infiniband:
case NetworkManager::ConnectionSettings::OLPCMesh:
case NetworkManager::ConnectionSettings::Vpn:
case NetworkManager::ConnectionSettings::Tun:
case NetworkManager::ConnectionSettings::IpTunnel:
case NetworkManager::ConnectionSettings::WireGuard:
case NetworkManager::ConnectionSettings::Wimax:
case NetworkManager::ConnectionSettings::Team:
case NetworkManager::ConnectionSettings::Generic:
case NetworkManager::ConnectionSettings::Unknown:
;
}
}
}
}