added main functions

This commit is contained in:
arcan1s
2014-02-04 21:59:06 +04:00
parent e4c2402530
commit 7a6930ce2c
11 changed files with 1293 additions and 4 deletions

30
sources/gui/src/main.cpp Normal file
View File

@ -0,0 +1,30 @@
/***************************************************************************
* 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/ *
***************************************************************************/
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

View File

@ -0,0 +1,180 @@
/***************************************************************************
* 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/ *
***************************************************************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "netctlinteract.h"
#include <cstdio>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableWidget_main->setSortingEnabled(true);
ui->statusBar->showMessage(QApplication::translate("MainWindow", "Ready"));
// SettingsWindow *settingsWindow;
// settingsWindow = new SettingsWindow(this);
// delete settingsWindow;
// temporary block
netctlPath = QString("/usr/bin/netctl");
profileDir = QString("/etc/netctl");
sudoPath = QString("/usr/bin/kdesu -c");
netctlCommand = new Netctl(this, netctlPath, profileDir, sudoPath);
createActions();
updateMainTab();
}
MainWindow::~MainWindow()
{
delete netctlCommand;
delete ui;
}
// window signals
void MainWindow::createActions()
{
connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(updateTabs(int)));
connect(ui->actionQuit, SIGNAL(triggered(bool)), this, SLOT(close()));
// main page events
connect(ui->pushButton_mainRefresh, SIGNAL(clicked(bool)), this, SLOT(updateMainTab()));
connect(ui->pushButton_mainEnable, SIGNAL(clicked(bool)), this, SLOT(mainTabEnableProfile()));
connect(ui->pushButton_mainRestart, SIGNAL(clicked(bool)), this, SLOT(mainTabRestartProfile()));
connect(ui->pushButton_mainStart, SIGNAL(clicked(bool)), this, SLOT(mainTabStartProfile()));
connect(ui->tableWidget_main, SIGNAL(currentItemChanged(QTableWidgetItem *, QTableWidgetItem *)), this, SLOT(mainTabRefreshButtons(QTableWidgetItem *, QTableWidgetItem *)));
}
// window slots
void MainWindow::updateTabs(const int tab)
{
if (tab == 0)
updateMainTab();
}
void MainWindow::updateMainTab()
{
QStringList profiles = netctlCommand->getProfileList();
QStringList descriptions = netctlCommand->getProfileDescriptions(profiles);
QStringList statuses = netctlCommand->getProfileStatuses(profiles);
ui->tableWidget_main->setRowCount(profiles.count());
ui->tableWidget_main->sortByColumn(0, Qt::AscendingOrder);
for (int i=0; i<profiles.count(); i++) {
ui->tableWidget_main->setItem(i, 0, new QTableWidgetItem(profiles[i]));
ui->tableWidget_main->setItem(i, 1, new QTableWidgetItem(descriptions[i]));
ui->tableWidget_main->setItem(i, 2, new QTableWidgetItem(statuses[i]));
}
ui->tableWidget_main->resizeColumnsToContents();
ui->tableWidget_main->resizeRowsToContents();
ui->tableWidget_main->horizontalHeader()->setStretchLastSection(true);
ui->tableWidget_main->setCurrentCell(0, 0);
update();
}
// main tab slots
void MainWindow::mainTabEnableProfile()
{
ui->tableWidget_main->setDisabled(true);
QString profile = ui->tableWidget_main->item(ui->tableWidget_main->currentItem()->row(), 0)->text();
netctlCommand->enableProfile(profile);
if (ui->tableWidget_main->item(ui->tableWidget_main->currentItem()->row(), 2)->text().indexOf(QString("enabled")) > -1) {
if (netctlCommand->isProfileEnabled(profile))
ui->statusBar->showMessage(QApplication::translate("MainWindow", "Error"));
else
ui->statusBar->showMessage(QApplication::translate("MainWindow", "Done"));
}
else {
if (netctlCommand->isProfileEnabled(profile))
ui->statusBar->showMessage(QApplication::translate("MainWindow", "Done"));
else
ui->statusBar->showMessage(QApplication::translate("MainWindow", "Error"));
}
ui->tableWidget_main->setEnabled(true);
updateMainTab();
}
void MainWindow::mainTabRestartProfile()
{
ui->tableWidget_main->setDisabled(true);
QString profile = ui->tableWidget_main->item(ui->tableWidget_main->currentItem()->row(), 0)->text();
netctlCommand->restartProfile(profile);
if (netctlCommand->isProfileActive(profile))
ui->statusBar->showMessage(QApplication::translate("MainWindow", "Done"));
else
ui->statusBar->showMessage(QApplication::translate("MainWindow", "Error"));
ui->tableWidget_main->setEnabled(true);
updateMainTab();
}
void MainWindow::mainTabStartProfile()
{
ui->tableWidget_main->setDisabled(true);
QString profile = ui->tableWidget_main->item(ui->tableWidget_main->currentItem()->row(), 0)->text();
netctlCommand->startProfile(profile);
if (ui->tableWidget_main->item(ui->tableWidget_main->currentItem()->row(), 2)->text().indexOf(QString("inactive")) == -1) {
if (netctlCommand->isProfileActive(profile))
ui->statusBar->showMessage(QApplication::translate("MainWindow", "Error"));
else
ui->statusBar->showMessage(QApplication::translate("MainWindow", "Done"));
}
else {
if (netctlCommand->isProfileActive(profile))
ui->statusBar->showMessage(QApplication::translate("MainWindow", "Done"));
else
ui->statusBar->showMessage(QApplication::translate("MainWindow", "Error"));
}
ui->tableWidget_main->setEnabled(true);
updateMainTab();
}
void MainWindow::mainTabRefreshButtons(QTableWidgetItem *current, QTableWidgetItem *previous)
{
Q_UNUSED(previous);
QString profile = ui->tableWidget_main->item(current->row(), 0)->text();
bool isActive = netctlCommand->isProfileActive(profile);
bool isEnable = netctlCommand->isProfileEnabled(profile);
if (isActive) {
ui->pushButton_mainRestart->setEnabled(true);
ui->pushButton_mainStart->setText(QApplication::translate("MainWindow", "Stop"));
}
else {
ui->pushButton_mainRestart->setDisabled(true);
ui->pushButton_mainStart->setText(QApplication::translate("MainWindow", "Start"));
}
if (isEnable)
ui->pushButton_mainEnable->setText(QApplication::translate("MainWindow", "Disable"));
else
ui->pushButton_mainEnable->setText(QApplication::translate("MainWindow", "Enable"));
}

View File

@ -0,0 +1,60 @@
/***************************************************************************
* 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 MAINWINDOW_H
#define MAINWINDOW_H
#include <QApplication>
#include <QItemSelection>
#include <QMainWindow>
#include <QTableWidgetItem>
class Netctl;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void updateTabs(const int tab);
void updateMainTab();
// main tab slots
void mainTabEnableProfile();
void mainTabRestartProfile();
void mainTabStartProfile();
void mainTabRefreshButtons(QTableWidgetItem *current, QTableWidgetItem *previous);
private:
Netctl *netctlCommand;
Ui::MainWindow *ui;
void createActions();
// configuration
QString netctlPath;
QString profileDir;
QString sudoPath;
};
#endif /* MAINWINDOW_H */

View File

@ -0,0 +1,170 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>466</width>
<height>542</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QTabWidget" name="tabWidget">
<widget class="QWidget" name="tab_main">
<attribute name="title">
<string>Connect to profile</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTableWidget" name="tableWidget_main">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string>Name</string>
</property>
</column>
<column>
<property name="text">
<string>Description</string>
</property>
</column>
<column>
<property name="text">
<string>Status</string>
</property>
</column>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="layout_mainButtons">
<item>
<widget class="QPushButton" name="pushButton_mainRefresh">
<property name="text">
<string>Refresh</string>
</property>
<property name="autoDefault">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="spacer_mainButtons">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton_mainEnable">
<property name="text">
<string>Enable</string>
</property>
<property name="autoDefault">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_mainRestart">
<property name="text">
<string>Restart</string>
</property>
<property name="autoDefault">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_mainStart">
<property name="text">
<string>Start</string>
</property>
<property name="autoDefault">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_new">
<attribute name="title">
<string>Create a new profile</string>
</attribute>
</widget>
<widget class="QWidget" name="tab_wifi">
<attribute name="title">
<string>Connect to Wi-Fi</string>
</attribute>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>466</width>
<height>20</height>
</rect>
</property>
<widget class="QMenu" name="menuMenu">
<property name="title">
<string>Menu</string>
</property>
<addaction name="actionSettings"/>
<addaction name="actionQuit"/>
</widget>
<addaction name="menuMenu"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionSettings">
<property name="text">
<string>Settings</string>
</property>
<property name="shortcut">
<string>Ctrl+S</string>
</property>
</action>
<action name="actionQuit">
<property name="text">
<string>Quit</string>
</property>
<property name="shortcut">
<string>Ctrl+Q</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -0,0 +1,180 @@
/***************************************************************************
* 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/ *
***************************************************************************/
#include "netctlinteract.h"
#include <QFile>
#include <QProcess>
#include "mainwindow.h"
#include "cstdio"
Netctl::Netctl(MainWindow *wid, QString netctlPath, QString profileDir, QString sudoPath)
: parent(wid),
netctlCommand(netctlPath),
profileDirectory(new QDir(profileDir)),
sudoCommand(sudoPath)
{
}
Netctl::~Netctl()
{
delete profileDirectory;
}
// general information
QStringList Netctl::getProfileDescriptions(QStringList profileList)
{
QStringList descriptions;
for (int i=0; i<profileList.count(); i++) {
QFile profile(profileDirectory->absolutePath() + QDir::separator() + profileList[i]);
QString fileStr;
if (profile.open(QIODevice::ReadOnly))
while (true) {
fileStr = QString(profile.readLine());
if (profile.atEnd())
break;
else if (fileStr[0] != '#')
if (fileStr.split(QString("="), QString::SkipEmptyParts).count() == 2)
if (fileStr.split(QString("="), QString::SkipEmptyParts)[0] == QString("Description"))
descriptions.append(fileStr.split(QString("="), QString::SkipEmptyParts)[1].split(QString("\n"), QString::SkipEmptyParts)[0]);
}
else
descriptions.append(QString("<unknown>"));
}
for (int i=0; i<profileList.count(); i++) {
descriptions[i].remove(QChar('\''));
descriptions[i].remove(QChar('"'));
}
return descriptions;
}
QStringList Netctl::getProfileList()
{
return profileDirectory->entryList(QDir::Files);
}
QStringList Netctl::getProfileStatuses(QStringList profileList)
{
QStringList statuses;
for (int i=0; i<profileList.count(); i++) {
QString status = QString("");
if (isProfileActive(profileList[i]))
status = status + QString("active");
else
status = status + QString("inactive");
if (isProfileEnabled(profileList[i]))
status = status + QString(" (enabled)");
else
status = status + QString(" (static)");
statuses.append(status);
}
return statuses;
}
bool Netctl::isProfileActive(QString profile)
{
bool status = false;
QProcess command;
QString cmdOutput = QString("");
command.start(netctlCommand + QString(" status ") + profile);
command.waitForFinished(-1);
cmdOutput = command.readAllStandardOutput();
if (!cmdOutput.isEmpty())
if (cmdOutput.indexOf(QString("Active: active")) > -1)
status = true;
return status;
}
bool Netctl::isProfileEnabled(QString profile)
{
bool status = false;
QProcess command;
QString cmdOutput = QString("");
command.start(netctlCommand + QString(" status ") + profile);
command.waitForFinished(-1);
cmdOutput = command.readAllStandardOutput();
if (!cmdOutput.isEmpty()) {
QStringList profileStatus = cmdOutput.split(QString("\n"), QString::SkipEmptyParts);
for (int i=0; i<profileStatus.count(); i++)
if (profileStatus[i].split(QString(" "), QString::SkipEmptyParts)[0] == QString("Loaded:"))
if (profileStatus[i].indexOf(QString("enabled")) > -1)
status = true;
}
return status;
}
// functions
bool Netctl::enableProfile(QString profile)
{
QProcess command;
if (isProfileEnabled(profile))
command.start(sudoCommand + QString(" ") + netctlCommand + QString(" disable ") + profile);
else
command.start(sudoCommand + QString(" ") + netctlCommand + QString(" enable ") + profile);
command.waitForFinished(-1);
if (command.exitCode() == 0)
return true;
else
return false;
}
bool Netctl::restartProfile(QString profile)
{
QProcess command;
if (isProfileActive(profile))
command.start(sudoCommand + QString(" ") + netctlCommand + QString(" restart ") + profile);
command.waitForFinished(-1);
if (command.exitCode() == 0)
return true;
else
return false;
}
bool Netctl::startProfile(QString profile)
{
QProcess command;
if (isProfileActive(profile))
command.start(sudoCommand + QString(" ") + netctlCommand + QString(" stop ") + profile);
else
command.start(sudoCommand + QString(" ") + netctlCommand + QString(" start ") + profile);
command.waitForFinished(-1);
if (command.exitCode() == 0)
return true;
else
return false;
}

View File

@ -0,0 +1,52 @@
/***************************************************************************
* 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 NETCTLINTERACT_H
#define NETCTLINTERACT_H
#include <QDir>
#include <QWidget>
class MainWindow;
class Netctl : public QWidget
{
Q_OBJECT
public:
Netctl(MainWindow *wid, QString netctlPath, QString profileDir, QString sudoPath);
~Netctl();
// general information
QStringList getProfileDescriptions(QStringList profileList);
QStringList getProfileList();
QStringList getProfileStatuses(QStringList profileList);
bool isProfileActive(QString profile);
bool isProfileEnabled(QString profile);
// functions
bool enableProfile(QString profile);
bool restartProfile(QString profile);
bool startProfile(QString profile);
private:
MainWindow *parent;
QString netctlCommand;
QDir *profileDirectory;
QString sudoCommand;
};
#endif /* NETCTLINTERACT_H */