From 4a2a27176fd888f3cd1b751d97b65beaaee140d7 Mon Sep 17 00:00:00 2001 From: arcan1s Date: Thu, 6 Feb 2014 11:06:36 +0400 Subject: [PATCH] small commit --- sources/gui/show_scripts | 4 + sources/gui/src/mainwindow.cpp | 17 ++++- sources/gui/src/mainwindow.h | 12 ++- sources/gui/src/mainwindow.ui | 6 ++ sources/gui/src/netctlinteract.h | 2 + sources/gui/src/sleepthread.h | 46 +++++++++++ sources/gui/src/wpasupinteract.cpp | 119 ++++++++++++++++++++++++++--- sources/gui/src/wpasupinteract.h | 15 +++- 8 files changed, 203 insertions(+), 18 deletions(-) create mode 100755 sources/gui/show_scripts create mode 100644 sources/gui/src/sleepthread.h diff --git a/sources/gui/show_scripts b/sources/gui/show_scripts new file mode 100755 index 0000000..33626ea --- /dev/null +++ b/sources/gui/show_scripts @@ -0,0 +1,4 @@ +#!/bin/bash + +kate /usr/lib/network/wpa /usr/lib/network/globals /usr/bin/wifi-menu &> /dev/null + diff --git a/sources/gui/src/mainwindow.cpp b/sources/gui/src/mainwindow.cpp index ee45217..a12646c 100644 --- a/sources/gui/src/mainwindow.cpp +++ b/sources/gui/src/mainwindow.cpp @@ -37,12 +37,18 @@ MainWindow::MainWindow(QWidget *parent) // temporary block netctlPath = QString("/usr/bin/netctl"); profileDir = QString("/etc/netctl"); - sudoPath = QString("/usr/bin/kdesu -c"); - wpaCliPath = QString("/usr/bin/wpa_cli"); + sudoPath = QString("/usr/bin/kdesu"); + wpaConfig.append(QString("/usr/bin/wpa_cli")); + wpaConfig.append(QString("/usr/bin/wpa_supplicant")); ifaceDir = QString("/sys/class/net/"); + preferedInterface = QString("wifi0"); + // additional settings + wpaConfig.append(QString("/run/wpa_supplicant_netctl-gui.pid")); + wpaConfig.append(QString("nl80211,wext")); + wpaConfig.append(QString("/run/wpa_supplicant_netctl-gui")); netctlCommand = new Netctl(this, netctlPath, profileDir, sudoPath); - wpaCliCommand = new WpaSup(this, wpaCliPath, ifaceDir); + wpaCommand = new WpaSup(this, wpaConfig, sudoPath, ifaceDir, preferedInterface); createActions(); updateMainTab(); @@ -51,6 +57,7 @@ MainWindow::MainWindow(QWidget *parent) MainWindow::~MainWindow() { delete netctlCommand; + delete wpaCommand; delete ui; } @@ -106,7 +113,9 @@ void MainWindow::updateMainTab() void MainWindow::updateWifiTab() { - + QList scanResults = wpaCommand->scanWifi(); + for (int i=0; i Refresh + + Ctrl+R + true @@ -181,6 +184,9 @@ Refresh + + Ctrl+R + true diff --git a/sources/gui/src/netctlinteract.h b/sources/gui/src/netctlinteract.h index 47b8ca0..a05afe3 100644 --- a/sources/gui/src/netctlinteract.h +++ b/sources/gui/src/netctlinteract.h @@ -37,6 +37,8 @@ public: QStringList getProfileStatuses(QStringList profileList); bool isProfileActive(QString profile); bool isProfileEnabled(QString profile); + +public slots: // functions bool enableProfile(QString profile); bool restartProfile(QString profile); diff --git a/sources/gui/src/sleepthread.h b/sources/gui/src/sleepthread.h new file mode 100644 index 0000000..9e8a3a5 --- /dev/null +++ b/sources/gui/src/sleepthread.h @@ -0,0 +1,46 @@ +/*************************************************************************** + * This file is part of netctl-plasmoid * + * * + * netctl-plasmoid is free software: you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * netctl-plasmoid is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with netctl-plasmoid. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + +#ifndef SLEEPTHREAD_H +#define SLEEPTHREAD_H + +#include + + +// class for sleeping function +class SleepThread : public QThread +{ + Q_OBJECT + // private run + void run () {} + +public : + static void usleep(long iSleepTime) + { + QThread::usleep(iSleepTime); + } + static void sleep(long iSleepTime) + { + QThread::sleep(iSleepTime); + } + static void msleep(long iSleepTime) + { + QThread::msleep(iSleepTime); + } +}; + +#endif /* SLEEPTHREAD_H */ diff --git a/sources/gui/src/wpasupinteract.cpp b/sources/gui/src/wpasupinteract.cpp index 8cfcfc9..9fd9200 100644 --- a/sources/gui/src/wpasupinteract.cpp +++ b/sources/gui/src/wpasupinteract.cpp @@ -17,16 +17,25 @@ #include "wpasupinteract.h" +#include + #include "mainwindow.h" +#include "sleepthread.h" #include -WpaSup::WpaSup(MainWindow *wid, QString wpaCliPath, QString ifaceDir) +WpaSup::WpaSup(MainWindow *wid, QStringList wpaConfig, QString sudoPath, QString ifaceDir, QString preferedInterface) : parent(wid), - wpaCliCommand(wpaCliPath), - ifaceDirectory(new QDir(ifaceDir)) + wpaConf(wpaConfig), + sudoCommand(sudoPath), + ifaceDirectory(new QDir(ifaceDir)), + mainInterface(preferedInterface) { - + if (QFile(wpaConf[2]).exists()) { + QProcess command; + command.start(sudoCommand + QString(" /usr/bin/rm -f ") + wpaConf[2]); + command.waitForFinished(-1); + } } @@ -40,13 +49,103 @@ WpaSup::~WpaSup() QStringList WpaSup::getInterfaceList() { QStringList interfaces; - QStringList allInterfaces; - allInterfaces = ifaceDirectory->entryList(QDir::Dirs | QDir::NoDotAndDotDot); - for (int i=0; ipath() + QDir::separator() + allInterfaces[i] + - QDir::separator() + QString("wireless")).exists()) - interfaces.append(allInterfaces[i]); + if (mainInterface.isEmpty()) { + QStringList allInterfaces; + allInterfaces = ifaceDirectory->entryList(QDir::Dirs | QDir::NoDotAndDotDot); + for (int i=0; ipath() + QDir::separator() + allInterfaces[i] + + QDir::separator() + QString("wireless")).exists()) + interfaces.append(allInterfaces[i]); + } + else + interfaces.append(mainInterface); return interfaces; } + + +// functions +bool WpaSup::wpaCliCall(QString commandLine) +{ + QString interface = getInterfaceList()[0]; + QProcess command; + command.start(sudoCommand + QString(" ") + wpaConf[0] + QString(" -i ") + interface + + QString(" -p ") + wpaConf[4] + QString(" ") + commandLine); + command.waitForFinished(-1); + SleepThread::sleep(1); + if (command.exitCode() == 0) + return true; + else + return false; +} + + +QString WpaSup::getWpaCliOutput(QString commandLine) +{ + QString interface = getInterfaceList()[0]; + QProcess command; + command.start(sudoCommand + QString(" ") + wpaConf[0] + QString(" -i ") + interface + + QString(" -p ") + wpaConf[4] + QString(" ") + commandLine); + command.waitForFinished(-1); + return command.readAllStandardOutput(); +} + + +bool WpaSup::startWpaSupplicant() +{ + if (!QFile(wpaConf[2]).exists()) { + QString interface = getInterfaceList()[0]; + QProcess command; + command.start(sudoCommand + QString(" ") + wpaConf[1] + QString(" -B -P ") + wpaConf[2] + + QString(" -i ") + interface + QString(" -D ") + wpaConf[3] + QString(" -C ") + wpaConf[4]); + command.waitForFinished(-1); + SleepThread::sleep(1); + if (command.exitCode() != 0) + return false; + } + return true; +} + + +bool WpaSup::stopWpaSupplicant() +{ + return wpaCliCall(QString("terminate")); +} + + +QList WpaSup::scanWifi() +{ + QList scanResults; + startWpaSupplicant(); + if (!wpaCliCall(QString("scan"))) + return scanResults; + SleepThread::sleep(3); + + QStringList rawOutput = getWpaCliOutput(QString("scan_results")).split(QString("\n")); + rawOutput.removeFirst(); + for (int i=0; i 4) + for (int j=i+1; j 4) + if (rawOutput[i].split(QString(" "), QString::SkipEmptyParts)[4] == + rawOutput[j].split(QString(" "), QString::SkipEmptyParts)[4]) + rawOutput.removeAt(j); + + for (int i=0; i 4) + wifiPoint.append(rawOutput[i].split(QString(" "), QString::SkipEmptyParts)[4]); + else + wifiPoint.append(QString("")); + wifiPoint.append(rawOutput[i].split(QString(" "), QString::SkipEmptyParts)[2]); + wifiPoint.append(rawOutput[i].split(QString(" "), QString::SkipEmptyParts)[1]); + wifiPoint.append(rawOutput[i].split(QString(" "), QString::SkipEmptyParts)[0]); + + scanResults.append(wifiPoint); + } + + stopWpaSupplicant(); + return scanResults; +} diff --git a/sources/gui/src/wpasupinteract.h b/sources/gui/src/wpasupinteract.h index b285285..59c0c76 100644 --- a/sources/gui/src/wpasupinteract.h +++ b/sources/gui/src/wpasupinteract.h @@ -29,15 +29,26 @@ class WpaSup : public QWidget Q_OBJECT public: - WpaSup(MainWindow *wid, QString wpaCliPath, QString ifaceDir); + WpaSup(MainWindow *wid, QStringList wpaConfig, QString sudoPath, QString ifaceDir, QString preferedInterface); ~WpaSup(); // general information QStringList getInterfaceList(); + // functions + bool wpaCliCall(QString commandLine); + QString getWpaCliOutput(QString commandLine); + +public slots: + // functions + bool startWpaSupplicant(); + bool stopWpaSupplicant(); + QList scanWifi(); private: MainWindow *parent; - QString wpaCliCommand; + QStringList wpaConf; + QString sudoCommand; QDir *ifaceDirectory; + QString mainInterface; };