Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WifiSettingsDialog::WifiSettingsDialog(QWidget *parent) :
- QDialog(parent),
- ui(new Ui::WifiSettingsDialog), _qpd(NULL), _connecting(false)
- {
- setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
- ui->setupUi(this);
- ui->list->setItemDelegate(new TwoIconsDelegate(this));
- ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
- connect(ui->passwordEdit, SIGNAL(textChanged(QString)), this, SLOT(checkSettings()));
- FiW1Wpa_supplicant1Interface *wpa = WpaFactory::createWpaSupplicantProxy(this);
- QList<QDBusObjectPath> ifaces = wpa->interfaces();
- if (ifaces.count() == 0)
- {
- QMessageBox::critical(parent, tr("No wifi interfaces"), tr("No wifi interfaces available"), QMessageBox::Close);
- close();
- }
- foreach (QDBusObjectPath ifpath, ifaces)
- {
- FiW1Wpa_supplicant1InterfaceInterface interface = WpaFactory::createInterfaceProxy(ifpath, this);
- QString ifname = interface->ifname();
- qDebug() << "Using wifi interface" << ifname;
- _currentBSS = interface->currentBSS();
- connect(interface, SIGNAL(BSSAdded(QDBusObjectPath,QVariantMap)), this, SLOT(onBSSAdded(QDBusObjectPath)));
- connect(interface, SIGNAL(PropertiesChanged(QVariantMap)), this, SLOT(onPropertiesChanged(QVariantMap)));
- /* Call onBSSAdded() for BSSes already scanned prior to opening the dialog */
- QList<QDBusObjectPath> bSSs = interface->bSSs();
- foreach (QDBusObjectPath bsspath, bSSs)
- {
- onBSSAdded(bsspath, ifname);
- }
- if (ui->list->count() && ui->list->currentRow() == -1)
- ui->list->setCurrentRow(0);
- /* Copy existing login information if possible
- Password is only retrievable if network has just been added */
- QDBusObjectPath cnp = interface->currentNetwork();
- if (!cnp.path().isEmpty())
- {
- FiW1Wpa_supplicant1NetworkInterface *cni = WpaFactory::createNetworkProxy(cnp);
- QVariantMap prop = cni->properties();
- if (prop.contains("identity") && prop.contains("password"))
- {
- ui->userEdit->setText(removeQuotes( prop.value("identity").toString() ));
- ui->passwordEdit->setText(removeQuotes( prop.value("password").toString() ));
- }
- else if (prop.contains("psk"))
- {
- ui->passwordEdit->setText(removeQuotes( prop.value("psk").toString() ));
- }
- cni->deleteLater();
- }
- QVariantMap args;
- args.insert("Type", "passive");
- interface->Scan(args);
- }
- }
- void WifiSettingsDialog::onBSSAdded(const QDBusObjectPath &path, QString &ifname)
- {
- FiW1Wpa_supplicant1BSSInterface *bss = WpaFactory::createBSSProxy(path);
- QString ssid = bss->sSID();
- int signal = bss->signal();
- QVariantMap properties;
- /* or maybe you can get the interface from 'bss' eg. bss->ifname();
- Then no need to pass the interface to this method at all */
- properties.insert("ifname", ifname);
- properties.insert("SSID", bss->sSID());
- properties.insert("WPA", bss->wPA());
- properties.insert("WPS", bss->wPS());
- properties.insert("RSN", bss->rSN());
- properties.insert("Frequency", bss->frequency());
- qDebug() << "New BSS detected:" << path.path() << ssid << "freq" << bss->frequency() << "WPS support:" << bss->wPS();
- if (ui->list->findItems(ssid, Qt::MatchExactly).count() )
- {
- bss->deleteLater();
- return;
- }
- QString icon;
- if (signal > -50)
- icon = ":/icons/network-wireless-connected-100.png";
- else if (signal > -60)
- icon = ":/icons/network-wireless-connected-75.png";
- else if (signal > -70)
- icon = ":/icons/network-wireless-connected-50.png";
- else if (signal > -80)
- icon = ":/icons/network-wireless-connected-25.png";
- else
- icon = ":/icons/network-wireless-connected-00.png";
- /* or maybe you can get the interface from 'bss' eg. bss->ifname();
- Then no need to pass the interface to this method at all */
- QString label = QString("%1 (%2)").arg(ssid, interface)
- QListWidgetItem *item = new QListWidgetItem(QIcon(icon), label);
- item->setData(Qt::UserRole, properties);
- item->setData(Qt::UserRole+1, signal);
- if (path == _currentBSS)
- item->setData(SecondIconRole, QIcon(":/icons/tick.png"));
- /* Insert the discovered BSS before entries with lower strength */
- for (int i=0; i<ui->list->count(); i++)
- {
- if (ui->list->item(i)->data(Qt::UserRole+1).toInt() < signal)
- {
- ui->list->insertItem(i, item);
- if (path == _currentBSS)
- ui->list->setCurrentItem(item);
- return;
- }
- }
- /* No existing entries with lower signal, append to end of list. */
- ui->list->addItem(item);
- if (path == _currentBSS)
- ui->list->setCurrentItem(item);
- bss->deleteLater();
- }
- void WifiSettingsDialog::accept()
- {
- QVariantMap properties = ui->list->currentItem()->data(Qt::UserRole).toMap();
- QString username, password;
- QString ifname = properties.value("ifname");
- QString ssid = properties.value("SSID");
- bool useWPS = ui->WpsRadio->isEnabled() && ui->WpsRadio->isChecked();
- if (ui->userEdit->isEnabled())
- username = ui->userEdit->text();
- if (ui->passwordEdit->isEnabled())
- password = ui->passwordEdit->text();
- if (connectToWifi(ifname, ssid, username, password, useWPS))
- QDialog::accept();
- }
- bool WifiSettingsDialog::connectToWifi(const QString &ifname, const QString &ssid, const QString &username, const QString &password, bool useWPS)
- {
- /* Clear the icon showing the current connection (if any) */
- for (int i=0; i< ui->list->count(); i++)
- {
- QListWidgetItem *wi = ui->list->item(i);
- if ( wi->data(SecondIconRole).isValid() )
- {
- wi->setData(SecondIconRole, QVariant() );
- }
- }
- _currentBSS = QDBusObjectPath();
- _qpd = new QProgressDialog("", tr("Cancel"), 0, 0, this, Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
- _qpd->show();
- ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
- /* Disconnect all wifi interfaces */
- FiW1Wpa_supplicant1Interface *wpa = WpaFactory::createWpaSupplicantProxy(this);
- QList<QDBusObjectPath> ifaces = wpa->interfaces();
- foreach (QDBusObjectPath d_ifpath, ifaces)
- {
- FiW1Wpa_supplicant1InterfaceInterface d_interface = WpaFactory::createInterfaceProxy(d_ifpath, this);
- QString d_ifname = d_interface->ifname();
- if (!QNetworkInterface::interfaceFromName(d_ifname).addressEntries().isEmpty())
- {
- _qpd->setLabelText(tr("Releasing old DHCP lease"));
- QApplication::processEvents();
- QStringList args;
- args << "--release" << d_ifname;
- QProcess::execute("/sbin/dhcpcd", args);
- /* Give the dhcp client some time to say goodbye to the DHCP server */
- msleep(100);
- /* Make sure IP is unset before proceeding */
- args.clear();
- args << d_ifname << "0.0.0.0";
- QProcess::execute("/sbin/ifconfig", args);
- }
- /* Maybe add ifname to the _qpd labels */
- _qpd->setLabelText(tr("Disconnecting"));
- QApplication::processEvents();
- d_interface->Disconnect().waitForFinished();
- d_interface->RemoveAllNetworks();
- }
- /* Connect */
- QDBusObjectPath ifpath = wpa->GetInterface(ifname).value();
- FiW1Wpa_supplicant1InterfaceInterface interface = WpaFactory::createInterfaceProxy(ifpath, this);
- _qpd->setLabelText(tr("Connecting"));
- QApplication::processEvents();
- if (useWPS)
- {
- /* WPS authentication */
- FiW1Wpa_supplicant1InterfaceWPSInterface *wps = WpaFactory::createWPSInterfaceProxy(ifpath);
- QVariantMap config;
- config.insert("Role", "enrollee");
- config.insert("Type", "pbc");
- /* TODO: should we restrict WPS to the BSSID of the access point selected? */
- wps->Start(config).waitForFinished();
- wps->deleteLater();
- }
- else
- {
- QVariantMap config;
- config.insert("ssid", ssid);
- if (!username.isEmpty())
- {
- config.insert("key_mgmt", "WPA-EAP");
- config.insert("identity", username);
- config.insert("password", password);
- }
- else if (!password.isEmpty())
- {
- config.insert("psk", password);
- }
- else
- {
- config.insert("key_mgmt", "NONE");
- config.insert("auth_alg", "OPEN");
- }
- qDebug() << "Connecting to new wifi network, connection parameters:" << config;
- /* Set _connectiong to true. If we receive a "state => disconnected"
- property change signal from this point, assume authentication failed */
- _connecting = true;
- /* Add network to configuration, and select the network */
- interface->SelectNetwork( interface->AddNetwork(config).value() );
- }
- /* Check if we have a DHCP lease every 100 msec, while displaying the progress dialog */
- while (QNetworkInterface::interfaceFromName(ifname).addressEntries().isEmpty() && !_qpd->wasCanceled())
- {
- msleep(100);
- }
- _connecting = false;
- _qpd->hide();
- _qpd->deleteLater();
- _qpd = NULL;
- if (QNetworkInterface::interfaceFromName(ifname).addressEntries().isEmpty())
- {
- interface->Disconnect();
- /* Re-enable OK button */
- checkSettings();
- return false;
- }
- else
- {
- /* There does not seem to be a DBus command to save the configuration.
- So ask wpa_cli to do it for us. */
- QStringList args;
- args << "-i"+ifname << "save_config";
- qDebug() << "Saving wifi configuration";
- QProcess::execute("/usr/sbin/wpa_cli", args);
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement