netctl-gui/sources/gui/src/netctlautowindow.cpp
arcan1s 362a11f0bc + new Verbose* DBus calls
+ new force request to netctl (DBus)
+ PointType enum
+ notification on hidding,
+ new columns in WiFi menu
* improve user expierence
* fix language definition
* fix tests
* update 3rdparty components
* small refactoring
2015-03-09 08:29:26 +03:00

508 lines
20 KiB
C++

/***************************************************************************
* This file is part of netctl-gui *
* *
* netctl-gui 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-gui 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-gui. If not, see http://www.gnu.org/licenses/ *
***************************************************************************/
#include "netctlautowindow.h"
#include "ui_netctlautowindow.h"
#include <QDebug>
#include <netctlgui/netctlgui.h>
#include <pdebug/pdebug.h>
#include "dbusoperation.h"
// #include "version.h"
NetctlAutoWindow::NetctlAutoWindow(QWidget *parent, const bool debugCmd, const QMap<QString, QString> settings)
: QMainWindow(parent),
ui(new Ui::NetctlAutoWindow),
debug(debugCmd)
{
useHelper = (settings[QString("USE_HELPER")] == QString("true"));
ui->setupUi(this);
ui->tableWidget->setColumnHidden(2, true);
ui->tableWidget->setColumnHidden(3, true);
netctlCommand = new Netctl(debug, settings);
createToolBars();
createActions();
ui->statusBar->showMessage(QApplication::translate("NetctlAutoWindow", "Ready"));
}
NetctlAutoWindow::~NetctlAutoWindow()
{
if (debug) qDebug() << PDEBUG;
delete netctlCommand;
if (actionMenu != nullptr) {
actionMenu->menu()->clear();
delete actionMenu;
}
if (actionToolBar != nullptr) {
actionToolBar->clear();
delete actionToolBar;
}
delete ui;
}
QString NetctlAutoWindow::checkStatus(const bool statusBool, const bool nullFalse)
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "Status" << statusBool;
if (debug) qDebug() << PDEBUG << ":" << "Return null false" << nullFalse;
if (statusBool) return QApplication::translate("NetctlAutoWindow", "yes");
if (!nullFalse) return QApplication::translate("NetctlAutoWindow", "no");
return QString("");
}
void NetctlAutoWindow::createActions()
{
if (debug) qDebug() << PDEBUG;
// menu actions
connect(ui->actionClose, SIGNAL(triggered(bool)), this, SLOT(close()));
connect(ui->actionDisableAll, SIGNAL(triggered(bool)), this, SLOT(netctlAutoDisableAllProfiles()));
connect(ui->actionEnable, SIGNAL(triggered(bool)), this, SLOT(netctlAutoEnableProfile()));
connect(ui->actionEnableAll, SIGNAL(triggered(bool)), this, SLOT(netctlAutoEnableAllProfiles()));
connect(ui->actionSwitch, SIGNAL(triggered(bool)), this, SLOT(netctlAutoStartProfile()));
// service
connect(ui->actionEnableService, SIGNAL(triggered(bool)), this, SLOT(netctlAutoEnableService()));
connect(ui->actionRestartService, SIGNAL(triggered(bool)), this, SLOT(netctlAutoRestartService()));
connect(ui->actionStartService, SIGNAL(triggered(bool)), this, SLOT(netctlAutoStartService()));
// table actions
connect(ui->tableWidget, SIGNAL(itemActivated(QTableWidgetItem *)), this, SLOT(netctlAutoStartProfile()));
connect(ui->tableWidget, SIGNAL(currentItemChanged(QTableWidgetItem *, QTableWidgetItem *)), this, SLOT(netctlAutoRefreshButtons(QTableWidgetItem *, QTableWidgetItem *)));
connect(ui->tableWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(netctlAutoContextualMenu(QPoint)));
}
void NetctlAutoWindow::createToolBars()
{
if (debug) qDebug() << PDEBUG;
actionToolBar = new QToolBar(this);
actionToolBar->setToolButtonStyle(Qt::ToolButtonFollowStyle);
toolBarActions[QString("refresh")] = actionToolBar->addAction(QIcon::fromTheme(QString("view-refresh")),
QApplication::translate("NetctlAutoWindow", "Refresh"),
this, SLOT(netctlAutoUpdateTable()));
actionMenu = new QToolButton(this);
actionMenu->setPopupMode(QToolButton::DelayedPopup);
actionMenu->setToolButtonStyle(Qt::ToolButtonFollowStyle);
QMenu *menu = new QMenu(actionMenu);
toolBarActions[QString("enable")] = menu->addAction(QApplication::translate("NetctlAutoWindow", "Enable"),
this, SLOT(netctlAutoEnableProfile()));
toolBarActions[QString("switch")] = menu->addAction(QIcon::fromTheme(QString("system-run")),
QApplication::translate("NetctlAutoWindow", "Switch"),
this, SLOT(netctlAutoStartProfile()));
actionMenu->setDefaultAction(toolBarActions[QString("switch")]);
actionMenu->setMenu(menu);
actionToolBar->addWidget(actionMenu);
ui->centralLayout->insertWidget(0, actionToolBar);
}
void NetctlAutoWindow::showWindow()
{
if (debug) qDebug() << PDEBUG;
netctlAutoUpdateTable();
show();
}
void NetctlAutoWindow::netctlAutoContextualMenu(const QPoint &pos)
{
if (debug) qDebug() << PDEBUG;
if (ui->tableWidget->currentItem() == nullptr) return;
// create menu
QMenu menu(this);
QAction *startProfile = menu.addAction(QApplication::translate("NetctlAutoWindow", "Switch to profile"));
startProfile->setIcon(QIcon::fromTheme("system-run"));
QAction *enableProfile = menu.addAction(QApplication::translate("NetctlAutoWindow", "Enable profile"));
menu.addSeparator();
QAction *enableAllProfiles = menu.addAction(QApplication::translate("NetctlAutoWindow", "Enable all profiles"));
enableAllProfiles->setIcon(QIcon::fromTheme("list-add"));
QAction *disableAllProfiles = menu.addAction(QApplication::translate("NetctlAutoWindow", "Disable all profiles"));
disableAllProfiles->setIcon(QIcon::fromTheme("edit-delete"));
// set text
startProfile->setVisible(ui->tableWidget->item(ui->tableWidget->currentItem()->row(), 2)->text().isEmpty());
if (!ui->tableWidget->item(ui->tableWidget->currentItem()->row(), 3)->text().isEmpty()) {
enableProfile->setText(QApplication::translate("NetctlAutoWindow", "Enable"));
enableProfile->setIcon(QIcon::fromTheme("list-add"));
} else {
enableProfile->setText(QApplication::translate("NetctlAutoWindow", "Disable"));
enableProfile->setIcon(QIcon::fromTheme("edit-delete"));
}
// actions
QAction *action = menu.exec(ui->tableWidget->viewport()->mapToGlobal(pos));
if (action == startProfile) {
if (debug) qDebug() << PDEBUG << ":" << "Switch to profile";
netctlAutoStartProfile();
} else if (action == enableProfile) {
if (debug) qDebug() << PDEBUG << ":" << "Enable profile";
netctlAutoEnableProfile();
} else if (action == enableAllProfiles) {
if (debug) qDebug() << PDEBUG << ":" << "Enable all profiles";
netctlAutoEnableAllProfiles();
} else if (action == disableAllProfiles) {
if (debug) qDebug() << PDEBUG << ":" << "Disable all profiles";
netctlAutoDisableAllProfiles();
}
}
void NetctlAutoWindow::netctlAutoUpdateTable()
{
if (debug) qDebug() << PDEBUG;
ui->tableWidget->setDisabled(true);
// actions
bool enabled = false;
bool running = false;
if (useHelper) {
QList<QVariant> responce = sendRequestToLib(QString("isNetctlAutoActive"), debug);
if (responce.isEmpty()) {
if (debug) qDebug() << PDEBUG << ":" << "Could not interact with helper, disable it";
useHelper = false;
return netctlAutoUpdateTable();
}
enabled = responce[0].toBool();
responce = sendRequestToLib(QString("isNetctlAutoActive"), debug);
if (responce.isEmpty()) {
if (debug) qDebug() << PDEBUG << ":" << "Could not interact with helper, disable it";
useHelper = false;
return netctlAutoUpdateTable();
}
running = responce[0].toBool();
} else {
enabled = netctlCommand->isNetctlAutoEnabled();
running = netctlCommand->isNetctlAutoRunning();
}
if (enabled)
ui->actionEnableService->setText(QApplication::translate("NetctlAutoWindow", "Disable service"));
else
ui->actionEnableService->setText(QApplication::translate("NetctlAutoWindow", "Enable service"));
ui->actionEnableService->setVisible(true);
if (running) {
ui->label_info->setText(QApplication::translate("NetctlAutoWindow", "netctl-auto is running"));
ui->actionStartService->setText(QApplication::translate("NetctlAutoWindow", "Stop service"));
ui->actionDisableAll->setVisible(true);
ui->actionEnableAll->setVisible(true);
ui->actionRestartService->setVisible(true);
} else {
ui->label_info->setText(QApplication::translate("NetctlAutoWindow", "netctl-auto is not running"));
ui->actionStartService->setText(QApplication::translate("NetctlAutoWindow", "Start service"));
ui->actionDisableAll->setVisible(false);
ui->actionEnableAll->setVisible(false);
ui->actionRestartService->setVisible(false);
netctlAutoRefreshButtons(nullptr, nullptr);
return;
}
QList<netctlProfileInfo> profiles;
if (useHelper)
profiles = parseOutputNetctl(sendRequestToLib(QString("VerboseProfileList"), debug));
else
profiles = netctlCommand->getProfileListFromNetctlAuto();
ui->tableWidget->setSortingEnabled(false);
ui->tableWidget->selectRow(-1);
ui->tableWidget->sortByColumn(0, Qt::AscendingOrder);
ui->tableWidget->clear();
ui->tableWidget->setRowCount(profiles.count());
// create header
QStringList headerList;
headerList.append(QApplication::translate("NetctlAutoWindow", "Name"));
headerList.append(QApplication::translate("NetctlAutoWindow", "Description"));
headerList.append(QApplication::translate("NetctlAutoWindow", "Active"));
headerList.append(QApplication::translate("NetctlAutoWindow", "Disabled"));
ui->tableWidget->setHorizontalHeaderLabels(headerList);
// create items
for (int i=0; i<profiles.count(); i++) {
// font
QFont font;
font.setBold(profiles[i].active);
font.setItalic(profiles[i].enabled);
// tooltip
QString toolTip = QString("");
toolTip += QString("%1: %2\n").arg(QApplication::translate("NetctlAutoWindow", "Profile")).arg(profiles[i].name);
toolTip += QString("%1: %2\n").arg(QApplication::translate("NetctlAutoWindow", "Active")).arg(checkStatus(profiles[i].active));
toolTip += QString("%1: %2").arg(QApplication::translate("NetctlAutoWindow", "Disabled")).arg(checkStatus(!profiles[i].enabled));
// name
ui->tableWidget->setItem(i, 0, new QTableWidgetItem(profiles[i].name));
ui->tableWidget->item(i, 0)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
ui->tableWidget->item(i, 0)->setToolTip(toolTip);
ui->tableWidget->item(i, 0)->setFont(font);
// description
ui->tableWidget->setItem(i, 1, new QTableWidgetItem(profiles[i].description));
ui->tableWidget->item(i, 1)->setTextAlignment(Qt::AlignJustify | Qt::AlignVCenter);
ui->tableWidget->item(i, 1)->setToolTip(toolTip);
// active
ui->tableWidget->setItem(i, 2, new QTableWidgetItem(checkStatus(profiles[i].active, true)));
ui->tableWidget->item(i, 2)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
// enabled
ui->tableWidget->setItem(i, 3, new QTableWidgetItem(checkStatus(!profiles[i].enabled, true)));
ui->tableWidget->item(i, 3)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
}
ui->tableWidget->setSortingEnabled(true);
ui->tableWidget->resizeRowsToContents();
ui->tableWidget->resizeColumnsToContents();
ui->tableWidget->resizeRowsToContents();
#if QT_VERSION >= 0x050000
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
#else
ui->tableWidget->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
#endif
ui->tableWidget->setCurrentCell(-1, -1);
ui->tableWidget->setEnabled(true);
ui->statusBar->showMessage(QApplication::translate("NetctlAutoWindow", "Updated"));
netctlAutoRefreshButtons(nullptr, nullptr);
update();
}
void NetctlAutoWindow::netctlAutoDisableAllProfiles()
{
if (debug) qDebug() << PDEBUG;
ui->tableWidget->setDisabled(true);
bool status = false;
if (useHelper) {
QList<QVariant> responce = sendRequestToCtrl(QString("autoDisableAll"), debug);
if (responce.isEmpty()) {
if (debug) qDebug() << PDEBUG << ":" << "Could not interact with helper, disable it";
useHelper = false;
return netctlAutoDisableAllProfiles();
}
status = responce[0].toBool();
} else
status = netctlCommand->autoDisableAllProfiles();
if (status)
ui->statusBar->showMessage(QApplication::translate("NetctlAutoWindow", "Done"));
else
ui->statusBar->showMessage(QApplication::translate("NetctlAutoWindow", "Error"));
netctlAutoUpdateTable();
}
void NetctlAutoWindow::netctlAutoEnableProfile()
{
if (debug) qDebug() << PDEBUG;
if (ui->tableWidget->currentItem() == nullptr) return;
ui->tableWidget->setDisabled(true);
QString profile = ui->tableWidget->item(ui->tableWidget->currentItem()->row(), 0)->text();
bool status = false;
if (useHelper) {
QList<QVariant> args;
args.append(profile);
QList<QVariant> responce = sendRequestToCtrlWithArgs(QString("autoEnable"), args, debug);
if (responce.isEmpty()) {
if (debug) qDebug() << PDEBUG << ":" << "Could not interact with helper, disable it";
useHelper = false;
return netctlAutoEnableProfile();
}
status = responce[0].toBool();
} else
status = netctlCommand->autoEnableProfile(profile);
if (status)
ui->statusBar->showMessage(QApplication::translate("NetctlAutoWindow", "Done"));
else
ui->statusBar->showMessage(QApplication::translate("NetctlAutoWindow", "Error"));
netctlAutoUpdateTable();
}
void NetctlAutoWindow::netctlAutoEnableAllProfiles()
{
if (debug) qDebug() << PDEBUG;
ui->tableWidget->setDisabled(true);
bool status = false;
if (useHelper) {
QList<QVariant> responce = sendRequestToCtrl(QString("autoEnableAll"), debug);
if (responce.isEmpty()) {
if (debug) qDebug() << PDEBUG << ":" << "Could not interact with helper, disable it";
useHelper = false;
return netctlAutoEnableAllProfiles();
}
status = responce[0].toBool();
} else
status = netctlCommand->autoEnableAllProfiles();
if (status)
ui->statusBar->showMessage(QApplication::translate("NetctlAutoWindow", "Done"));
else
ui->statusBar->showMessage(QApplication::translate("NetctlAutoWindow", "Error"));
netctlAutoUpdateTable();
}
void NetctlAutoWindow::netctlAutoStartProfile()
{
if (debug) qDebug() << PDEBUG;
if (ui->tableWidget->currentItem() == nullptr) return;
ui->tableWidget->setDisabled(true);
QString profile = ui->tableWidget->item(ui->tableWidget->currentItem()->row(), 0)->text();
bool status = false;
if (useHelper) {
QList<QVariant> args;
args.append(profile);
QList<QVariant> responce = sendRequestToCtrlWithArgs(QString("autoStart"), args, debug);
if (responce.isEmpty()) {
if (debug) qDebug() << PDEBUG << ":" << "Could not interact with helper, disable it";
useHelper = false;
return netctlAutoStartProfile();
}
status = responce[0].toBool();
} else
status = netctlCommand->autoStartProfile(profile);
if (status)
ui->statusBar->showMessage(QApplication::translate("NetctlAutoWindow", "Done"));
else
ui->statusBar->showMessage(QApplication::translate("NetctlAutoWindow", "Error"));
netctlAutoUpdateTable();
}
void NetctlAutoWindow::netctlAutoEnableService()
{
if (debug) qDebug() << PDEBUG;
bool status = false;
if (useHelper) {
QList<QVariant> responce = sendRequestToCtrl(QString("autoServiceEnable"), debug);
if (responce.isEmpty()) {
if (debug) qDebug() << PDEBUG << ":" << "Could not interact with helper, disable it";
useHelper = false;
return netctlAutoEnableService();
}
status = responce[0].toBool();
} else
status = netctlCommand->autoEnableService();
if (status)
ui->statusBar->showMessage(QApplication::translate("NetctlAutoWindow", "Done"));
else
ui->statusBar->showMessage(QApplication::translate("NetctlAutoWindow", "Error"));
netctlAutoUpdateTable();
}
void NetctlAutoWindow::netctlAutoRestartService()
{
if (debug) qDebug() << PDEBUG;
bool status = false;
if (useHelper) {
QList<QVariant> responce = sendRequestToCtrl(QString("autoServiceRestart"), debug);
if (responce.isEmpty()) {
if (debug) qDebug() << PDEBUG << ":" << "Could not interact with helper, disable it";
useHelper = false;
return netctlAutoRestartService();
}
status = responce[0].toBool();
} else
status = netctlCommand->autoRestartService();
if (status)
ui->statusBar->showMessage(QApplication::translate("NetctlAutoWindow", "Done"));
else
ui->statusBar->showMessage(QApplication::translate("NetctlAutoWindow", "Error"));
netctlAutoUpdateTable();
}
void NetctlAutoWindow::netctlAutoStartService()
{
if (debug) qDebug() << PDEBUG;
bool status = false;
if (useHelper) {
QList<QVariant> responce = sendRequestToCtrl(QString("autoServiceStart"), debug);
if (responce.isEmpty()) {
if (debug) qDebug() << PDEBUG << ":" << "Could not interact with helper, disable it";
useHelper = false;
return netctlAutoStartService();
}
status = responce[0].toBool();
} else
status = netctlCommand->autoStartService();
if (status)
ui->statusBar->showMessage(QApplication::translate("NetctlAutoWindow", "Done"));
else
ui->statusBar->showMessage(QApplication::translate("NetctlAutoWindow", "Error"));
netctlAutoUpdateTable();
}
void NetctlAutoWindow::netctlAutoRefreshButtons(QTableWidgetItem *current, QTableWidgetItem *previous)
{
Q_UNUSED(previous);
if (debug) qDebug() << PDEBUG;
if (current == nullptr) {
// buttons
toolBarActions[QString("enable")]->setDisabled(true);
toolBarActions[QString("switch")]->setDisabled(true);
// menu
ui->actionEnable->setVisible(false);
ui->actionSwitch->setVisible(false);
return;
}
toolBarActions[QString("switch")]->setEnabled(ui->tableWidget->item(current->row(), 2)->text().isEmpty());
ui->actionSwitch->setVisible(ui->tableWidget->item(current->row(), 2)->text().isEmpty());
toolBarActions[QString("enable")]->setEnabled(true);
ui->actionEnable->setVisible(true);
if (!ui->tableWidget->item(current->row(), 3)->text().isEmpty()) {
// buttons
toolBarActions[QString("enable")]->setText(QApplication::translate("NetctlAutoWindow", "Enable"));
toolBarActions[QString("enable")]->setIcon(QIcon::fromTheme("list-add"));
// menu
ui->actionEnable->setText(QApplication::translate("NetctlAutoWindow", "Enable profile"));
ui->actionEnable->setIcon(QIcon::fromTheme("list-add"));
} else {
// buttons
toolBarActions[QString("enable")]->setText(QApplication::translate("NetctlAutoWindow", "Disable"));
toolBarActions[QString("enable")]->setIcon(QIcon::fromTheme("edit-delete"));
// menu
ui->actionEnable->setText(QApplication::translate("NetctlAutoWindow", "Disable profile"));
ui->actionEnable->setIcon(QIcon::fromTheme("edit-delete"));
}
}