From 6a3e3b14b3bbb99ad0ee70cc41eaaf0d15c0dd6b Mon Sep 17 00:00:00 2001 From: arcan1s Date: Fri, 8 Aug 2014 19:33:36 +0400 Subject: [PATCH] add helper proto --- sources/3rdparty/task/taskadds.cpp | 21 ++- sources/3rdparty/task/taskadds.h | 6 +- sources/CMakeLists.txt | 8 +- sources/gui/src/CMakeLists.txt | 3 +- sources/gui/src/main.cpp | 6 - sources/gui/src/mainwindow.cpp | 74 +++++++-- sources/gui/src/mainwindow.h | 7 + sources/gui/src/netctlguiadaptor.h | 2 +- sources/gui/src/settingswindow.cpp | 106 ++++++++++-- sources/gui/src/settingswindow.h | 4 + sources/gui/src/settingswindow.ui | 138 +++++++++++++++- sources/helper/CMakeLists.txt | 12 ++ sources/helper/src/CMakeLists.txt | 32 ++++ .../{gui => helper}/src/controladaptor.cpp | 21 ++- sources/{gui => helper}/src/controladaptor.h | 10 +- sources/helper/src/main.cpp | 121 ++++++++++++++ sources/helper/src/messages.cpp | 116 +++++++++++++ sources/helper/src/messages.h | 34 ++++ sources/{gui => helper}/src/netctladaptor.cpp | 0 sources/{gui => helper}/src/netctladaptor.h | 2 +- sources/helper/src/netctlhelper.cpp | 152 ++++++++++++++++++ sources/helper/src/netctlhelper.h | 50 ++++++ sources/version.h.in | 10 +- 23 files changed, 875 insertions(+), 60 deletions(-) create mode 100644 sources/helper/CMakeLists.txt create mode 100644 sources/helper/src/CMakeLists.txt rename sources/{gui => helper}/src/controladaptor.cpp (89%) rename sources/{gui => helper}/src/controladaptor.h (91%) create mode 100644 sources/helper/src/main.cpp create mode 100644 sources/helper/src/messages.cpp create mode 100644 sources/helper/src/messages.h rename sources/{gui => helper}/src/netctladaptor.cpp (100%) rename sources/{gui => helper}/src/netctladaptor.h (97%) create mode 100644 sources/helper/src/netctlhelper.cpp create mode 100644 sources/helper/src/netctlhelper.h diff --git a/sources/3rdparty/task/taskadds.cpp b/sources/3rdparty/task/taskadds.cpp index dc6ef97..d99628c 100644 --- a/sources/3rdparty/task/taskadds.cpp +++ b/sources/3rdparty/task/taskadds.cpp @@ -19,15 +19,24 @@ #include "taskadds.h" -TaskResult runTask(const QString cmd) +TaskResult runTask(const QString cmd, const bool sudo) { return Task::await( [ & ]() { - SandboxProcess command; - command.start(cmd); - command.waitForFinished(-1); TaskResult r; - r.exitCode = command.exitCode(); - r.output = command.readAllStandardOutput(); + if (sudo) { + QProcess command; + command.start(cmd); + command.waitForFinished(-1); + r.exitCode = command.exitCode(); + r.output = command.readAllStandardOutput(); + } + else { + RootProcess command; + command.start(cmd); + command.waitForFinished(-1); + r.exitCode = command.exitCode(); + r.output = command.readAllStandardOutput(); + } return r; }); diff --git a/sources/3rdparty/task/taskadds.h b/sources/3rdparty/task/taskadds.h index 617d47c..6d54e75 100644 --- a/sources/3rdparty/task/taskadds.h +++ b/sources/3rdparty/task/taskadds.h @@ -25,9 +25,9 @@ #include "task.h" -class SandboxProcess : public QProcess +class RootProcess : public QProcess { - protected: +protected: void setupChildProcess() { ::setuid(0); @@ -40,7 +40,7 @@ struct TaskResult int exitCode; QByteArray output; }; -TaskResult runTask(const QString cmd); +TaskResult runTask(const QString cmd, const bool sudo = false); #endif /* TASKADDS_H */ diff --git a/sources/CMakeLists.txt b/sources/CMakeLists.txt index cf40f43..608ea32 100644 --- a/sources/CMakeLists.txt +++ b/sources/CMakeLists.txt @@ -24,10 +24,11 @@ message (STATUS "Build date: ${CURRENT_DATE}") option (USE_QT5 "Use Qt5 instead of Qt4" ON) # components option (BUILD_GUI "Build GUI" ON) +option (BUILD_HELPER "Build helper" ON) option (BUILD_LIBRARY "Build library" ON) option (BUILD_DATAENGINE "Build data engine" ON) option (BUILD_PLASMOID "Build plasmoid" ON) -if (BUILD_GUI) +if (BUILD_GUI OR BUILD_HELPER) set (BUILD_LIBRARY ON) endif () if (BUILD_PLASMOID) @@ -58,7 +59,10 @@ add_subdirectory (${PROJECT_RESOURCE_DIR}) # components if (BUILD_LIBRARY) add_subdirectory (${PROJECT_LIBRARY}) -endif() +endif () +if (BUILD_HELPER) + add_subdirectory (helper) +endif () if (BUILD_GUI) add_subdirectory (gui) endif () diff --git a/sources/gui/src/CMakeLists.txt b/sources/gui/src/CMakeLists.txt index cfc89cf..27d1e48 100644 --- a/sources/gui/src/CMakeLists.txt +++ b/sources/gui/src/CMakeLists.txt @@ -60,5 +60,4 @@ endif() add_executable (${SUBPROJECT} ${UI_HEADERS} ${HEADERS} ${SOURCES} ${MOC_SOURCES} ${QRC_SOURCES} ${TRANSLATIONS}) target_link_libraries (${SUBPROJECT} ${PROJECT_LIBRARY} ${QT_NEEDED_LIBS}) # install properties -install (TARGETS ${SUBPROJECT} DESTINATION bin - PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE SETUID) +install (TARGETS ${SUBPROJECT} DESTINATION bin) diff --git a/sources/gui/src/main.cpp b/sources/gui/src/main.cpp index 4e50494..bac2c3e 100644 --- a/sources/gui/src/main.cpp +++ b/sources/gui/src/main.cpp @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include @@ -55,17 +54,12 @@ int main(int argc, char *argv[]) daemon(0, 0); break; } -#if QT_VERSION >= 0x050000 - QApplication::setSetuidAllowed(true); - qDebug() << QApplication::isSetuidAllowed(); -#endif QApplication a(argc, argv); QApplication::setQuitOnLastWindowClosed(false); // check if exists if (restoreExistSession()) return 0; - // config path QMap args = getArgs(); // translation QString language = Language::defineLanguage(args[QString("config")].toString()); diff --git a/sources/gui/src/mainwindow.cpp b/sources/gui/src/mainwindow.cpp index cb7d40a..a8249ff 100644 --- a/sources/gui/src/mainwindow.cpp +++ b/sources/gui/src/mainwindow.cpp @@ -19,6 +19,7 @@ #include "ui_mainwindow.h" #include +#include #include #include #include @@ -31,7 +32,6 @@ #include "aboutwindow.h" #include "bridgewidget.h" -#include "controladaptor.h" #include "errorwindow.h" #include "ethernetwidget.h" #include "generalwidget.h" @@ -39,7 +39,6 @@ #include "language.h" #include "macvlanwidget.h" #include "mobilewidget.h" -#include "netctladaptor.h" #include "netctlautowindow.h" #include "netctlguiadaptor.h" #include "passwdwidget.h" @@ -52,9 +51,6 @@ #include "vlanwidget.h" #include "wirelesswidget.h" -#include -#include - MainWindow::MainWindow(QWidget *parent, const QMap args, @@ -157,6 +153,20 @@ QStringList MainWindow::getSettings() } +bool MainWindow::isHelperActive() +{ + if (debug) qDebug() << "[MainWindow]" << "[isHelperActive]"; + + QList responce = sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_CONTROL_PATH, + DBUS_HELPER_INTERFACE, QString("Active")); + + if (responce.size() == 1) + return true; + else + return false; +} + + void MainWindow::closeEvent(QCloseEvent *event) { if (debug) qDebug() << "[MainWindow]" << "[closeEvent]"; @@ -279,14 +289,6 @@ void MainWindow::createDBusSession() new NetctlGuiAdaptor(this), QDBusConnection::ExportAllContents)) if (debug) qDebug() << "[MainWindow]" << "[createDBusSession]" << ":" << "Could not register GUI object"; - if (!bus.registerObject(QString(DBUS_LIB_PATH), - new NetctlAdaptor(this, configuration), - QDBusConnection::ExportAllContents)) - if (debug) qDebug() << "[MainWindow]" << "[createDBusSession]" << ":" << "Could not register library object"; - if (!bus.registerObject(QString(DBUS_CONTROL_PATH), - new ControlAdaptor(this, configuration), - QDBusConnection::ExportAllContents)) - if (debug) qDebug() << "[MainWindow]" << "[createDBusSession]" << ":" << "Could not register control object"; } @@ -342,6 +344,7 @@ void MainWindow::deleteObjects() { if (debug) qDebug() << "[MainWindow]" << "[deleteObjects]"; + QDBusConnection::sessionBus().unregisterObject(QString(DBUS_OBJECT_PATH)); QDBusConnection::sessionBus().unregisterService(QString(DBUS_SERVICE)); if (netctlCommand != nullptr) delete netctlCommand; if (netctlProfile != nullptr) delete netctlProfile; @@ -378,6 +381,20 @@ void MainWindow::keyPressEvent(QKeyEvent *pressedKey) } +QList MainWindow::sendDBusRequest(const QString service, const QString path, + const QString interface, const QString cmd) +{ + if (debug) qDebug() << "[MainWindow]" << "[sendDBusRequest]"; + + QDBusConnection bus = QDBusConnection::sessionBus(); + QDBusMessage request = QDBusMessage::createMethodCall(service, path, interface, cmd); + QDBusMessage response = bus.call(request); + QList arguments = response.arguments(); + + return arguments; +} + + void MainWindow::setIconsToTabs() { if (debug) qDebug() << "[MainWindow]" << "[setIconsToTabs]"; @@ -488,6 +505,37 @@ void MainWindow::showSettingsWindow() } +void MainWindow::forceStartHelper() +{ + if (debug) qDebug() << "[MainWindow]" << "[forceStartHelper]"; + + QProcess process; + QString cmd = configuration[QString("HELPER_PATH")] + QString(" -c ") + configPath; + + process.startDetached(cmd); +} + + +void MainWindow::forceStopHelper() +{ + if (debug) qDebug() << "[MainWindow]" << "[forceStartHelper]"; + + sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_CONTROL_PATH, + DBUS_HELPER_INTERFACE, QString("Close")); +} + + +void MainWindow::startHelper() +{ + if (debug) qDebug() << "[MainWindow]" << "[startHelper]"; + + if (isHelperActive()) + return forceStopHelper(); + else + return forceStartHelper(); +} + + void MainWindow::setTab(int tab) { if (debug) qDebug() << "[MainWindow]" << "[setTab]"; diff --git a/sources/gui/src/mainwindow.h b/sources/gui/src/mainwindow.h index 7702950..a4c7f74 100644 --- a/sources/gui/src/mainwindow.h +++ b/sources/gui/src/mainwindow.h @@ -59,6 +59,7 @@ public: ~MainWindow(); QString getInformation(); QStringList getSettings(); + bool isHelperActive(); protected: void closeEvent(QCloseEvent *event); @@ -70,6 +71,10 @@ public slots: void showMainWindow(); void showNetctlAutoWindow(); void showSettingsWindow(); + // helper + void forceStartHelper(); + void forceStopHelper(); + void startHelper(); // main void setTab(int tab); void updateConfiguration(const QMap args = QMap()); @@ -141,6 +146,8 @@ private: void createObjects(); void deleteObjects(); void keyPressEvent(QKeyEvent *pressedKey); + QList sendDBusRequest(const QString service, const QString path, + const QString interface, const QString cmd); void setIconsToTabs(); QString configPath; bool debug; diff --git a/sources/gui/src/netctlguiadaptor.h b/sources/gui/src/netctlguiadaptor.h index f2f00c9..ae98426 100644 --- a/sources/gui/src/netctlguiadaptor.h +++ b/sources/gui/src/netctlguiadaptor.h @@ -27,7 +27,7 @@ class MainWindow; class NetctlGuiAdaptor : public QDBusAbstractAdaptor { Q_OBJECT - Q_CLASSINFO("D-Bus Interface", "org.freedesktop.netctlgui") + Q_CLASSINFO("D-Bus Interface", "org.netctlgui.netctlgui") public: explicit NetctlGuiAdaptor(MainWindow *parent = 0); diff --git a/sources/gui/src/settingswindow.cpp b/sources/gui/src/settingswindow.cpp index 255fe40..f6aab5e 100644 --- a/sources/gui/src/settingswindow.cpp +++ b/sources/gui/src/settingswindow.cpp @@ -53,21 +53,22 @@ void SettingsWindow::createActions() connect(ui->buttonBox->button(QDialogButtonBox::Cancel), SIGNAL(clicked(bool)), this, SLOT(close())); connect(ui->buttonBox->button(QDialogButtonBox::Reset), SIGNAL(clicked(bool)), this, SLOT(setDefault())); - connect(ui->buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked(bool)), this, SLOT(saveSettings())); - connect(ui->buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked(bool)), this, SLOT(close())); + connect(ui->buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked(bool)), this, SLOT(closeWindow())); connect(ui->checkBox_enableTray, SIGNAL(stateChanged(int)), this, SLOT(setTray())); connect(ui->treeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)), this, SLOT(changePage(QTreeWidgetItem *, QTreeWidgetItem *))); // buttons - connect(ui->pushButton_interfaceDir, SIGNAL(clicked(bool)), SLOT(selectIfaceDir())); - connect(ui->pushButton_netctlPath, SIGNAL(clicked(bool)), SLOT(selectNetctlPath())); - connect(ui->pushButton_netctlAutoPath, SIGNAL(clicked(bool)), SLOT(selectNetctlAutoPath())); - connect(ui->pushButton_profilePath, SIGNAL(clicked(bool)), SLOT(selectProfileDir())); - connect(ui->pushButton_rfkill, SIGNAL(clicked(bool)), SLOT(selectRfkillDir())); - connect(ui->pushButton_sudo, SIGNAL(clicked(bool)), SLOT(selectSudoPath())); - connect(ui->pushButton_systemctlPath, SIGNAL(clicked(bool)), SLOT(selectSystemctlPath())); - connect(ui->pushButton_wpaCliPath, SIGNAL(clicked(bool)), SLOT(selectWpaCliPath())); - connect(ui->pushButton_wpaSupPath, SIGNAL(clicked(bool)), SLOT(selectWpaSupPath())); + connect(ui->pushButton_helperPath, SIGNAL(clicked(bool)), this, SLOT(selectHelperPath())); + connect(ui->pushButton_interfaceDir, SIGNAL(clicked(bool)), this, SLOT(selectIfaceDir())); + connect(ui->pushButton_netctlPath, SIGNAL(clicked(bool)), this, SLOT(selectNetctlPath())); + connect(ui->pushButton_netctlAutoPath, SIGNAL(clicked(bool)), this, SLOT(selectNetctlAutoPath())); + connect(ui->pushButton_profilePath, SIGNAL(clicked(bool)), this, SLOT(selectProfileDir())); + connect(ui->pushButton_rfkill, SIGNAL(clicked(bool)), this, SLOT(selectRfkillDir())); + connect(ui->pushButton_status, SIGNAL(clicked(bool)), this, SLOT(startHelper())); + connect(ui->pushButton_sudo, SIGNAL(clicked(bool)), this, SLOT(selectSudoPath())); + connect(ui->pushButton_systemctlPath, SIGNAL(clicked(bool)), this, SLOT(selectSystemctlPath())); + connect(ui->pushButton_wpaCliPath, SIGNAL(clicked(bool)), this, SLOT(selectWpaCliPath())); + connect(ui->pushButton_wpaSupPath, SIGNAL(clicked(bool)), this, SLOT(selectWpaSupPath())); } @@ -103,6 +104,16 @@ void SettingsWindow::changePage(QTreeWidgetItem *current, QTreeWidgetItem *previ } +void SettingsWindow::closeWindow() +{ + if (debug) qDebug() << "[SettingsWindow]" << "[closeWindow]"; + + saveSettings(); + close(); + ((MainWindow *)parent())->updateConfiguration(); +} + + void SettingsWindow::saveSettings() { if (debug) qDebug() << "[SettingsWindow]" << "[saveSettings]"; @@ -115,8 +126,6 @@ void SettingsWindow::saveSettings() for (int i=0; ilineEdit_helperPath->setText(filename); +} + + void SettingsWindow::selectIfaceDir() { if (debug) qDebug() << "[SettingsWindow]" << "[selectIfaceDir]"; @@ -273,6 +296,7 @@ void SettingsWindow::showWindow() setSettings(getSettings()); setTray(); + updateHelper(); show(); } @@ -283,12 +307,21 @@ QMap SettingsWindow::readSettings() if (debug) qDebug() << "[SettingsWindow]" << "[readSettings]"; QMap settings; + if (ui->checkBox_helperClose->checkState() == 2) + settings[QString("CLOSE_HELPER")] = QString("true"); + else + settings[QString("CLOSE_HELPER")] = QString("false"); if (ui->checkBox_closeToTray->checkState() == 2) settings[QString("CLOSETOTRAY")] = QString("true"); else settings[QString("CLOSETOTRAY")] = QString("false"); settings[QString("CTRL_DIR")] = ui->lineEdit_wpaDir->text(); settings[QString("CTRL_GROUP")] = ui->lineEdit_wpaGroup->text(); + if (ui->checkBox_forceSudo->checkState() == 2) + settings[QString("FORCE_SUDO")] = QString("true"); + else + settings[QString("FORCE_SUDO")] = QString("false"); + settings[QString("HELPER_PATH")] = ui->lineEdit_helperPath->text(); settings[QString("IFACE_DIR")] = ui->lineEdit_interfacesDir->text(); settings[QString("LANGUAGE")] = ui->comboBox_language->currentText(); settings[QString("NETCTL_PATH")] = ui->lineEdit_netctlPath->text(); @@ -308,6 +341,10 @@ QMap SettingsWindow::readSettings() settings[QString("SYSTRAY")] = QString("true"); else settings[QString("SYSTRAY")] = QString("false"); + if (ui->checkBox_useHelper->checkState() == 2) + settings[QString("USE_HELPER")] = QString("true"); + else + settings[QString("USE_HELPER")] = QString("false"); settings[QString("WPACLI_PATH")] = ui->lineEdit_wpaCliPath->text(); settings[QString("WPASUP_PATH")] = ui->lineEdit_wpaSupPath->text(); settings[QString("WPA_DRIVERS")] = ui->lineEdit_wpaSupDrivers->text(); @@ -323,12 +360,21 @@ void SettingsWindow::setSettings(const QMap settings) { if (debug) qDebug() << "[SettingsWindow]" << "[setSettings]"; + if (settings[QString("CLOSE_HELPER")] == QString("true")) + ui->checkBox_helperClose->setCheckState(Qt::Checked); + else + ui->checkBox_helperClose->setCheckState(Qt::Unchecked); if (settings[QString("CLOSETOTRAY")] == QString("true")) ui->checkBox_closeToTray->setCheckState(Qt::Checked); else ui->checkBox_closeToTray->setCheckState(Qt::Unchecked); ui->lineEdit_wpaDir->setText(settings[QString("CTRL_DIR")]); ui->lineEdit_wpaGroup->setText(settings[QString("CTRL_GROUP")]); + if (settings[QString("FORCE_SUDO")] == QString("true")) + ui->checkBox_forceSudo->setCheckState(Qt::Checked); + else + ui->checkBox_forceSudo->setCheckState(Qt::Unchecked); + ui->lineEdit_helperPath->setText(settings[QString("HELPER_PATH")]); ui->lineEdit_interfacesDir->setText(settings[QString("IFACE_DIR")]); ui->comboBox_language->setCurrentIndex(0); for (int i=0; icomboBox_language->count(); i++) @@ -351,6 +397,10 @@ void SettingsWindow::setSettings(const QMap settings) ui->checkBox_enableTray->setCheckState(Qt::Checked); else ui->checkBox_enableTray->setCheckState(Qt::Unchecked); + if (settings[QString("USE_HELPER")] == QString("true")) + ui->checkBox_useHelper->setCheckState(Qt::Checked); + else + ui->checkBox_useHelper->setCheckState(Qt::Unchecked); ui->lineEdit_wpaCliPath->setText(settings[QString("WPACLI_PATH")]); ui->lineEdit_wpaSupPath->setText(settings[QString("WPASUP_PATH")]); ui->lineEdit_wpaSupDrivers->setText(settings[QString("WPA_DRIVERS")]); @@ -365,9 +415,12 @@ QMap SettingsWindow::getDefault() if (debug) qDebug() << "[SettingsWindow]" << "[getDefault]"; QMap settings; + settings[QString("CLOSE_HELPER")] = QString("false"); settings[QString("CLOSETOTRAY")] = QString("true"); settings[QString("CTRL_DIR")] = QString("/run/wpa_supplicant_netctl-gui"); settings[QString("CTRL_GROUP")] = QString("users"); + settings[QString("FORCE_SUDO")] = QString("false"); + settings[QString("HELPER_PATH")] = QString("/usr/bin/netctlgui-helper"); settings[QString("IFACE_DIR")] = QString("/sys/class/net/"); settings[QString("LANGUAGE")] = QString("en"); settings[QString("NETCTL_PATH")] = QString("/usr/bin/netctl"); @@ -381,6 +434,7 @@ QMap SettingsWindow::getDefault() settings[QString("SUDO_PATH")] = QString("/usr/bin/kdesu"); settings[QString("SYSTEMCTL_PATH")] = QString("/usr/bin/systemctl"); settings[QString("SYSTRAY")] = QString("true"); + settings[QString("USE_HELPER")] = QString("true"); settings[QString("WPACLI_PATH")] = QString("/usr/bin/wpa_cli"); settings[QString("WPASUP_PATH")] = QString("/usr/bin/wpa_supplicant"); settings[QString("WPA_DRIVERS")] = QString("nl80211,wext"); @@ -417,3 +471,29 @@ QMap SettingsWindow::getSettings() return settings; } + + +void SettingsWindow::startHelper() +{ + if (debug) qDebug() << "[SettingsWindow]" << "[startHelper]"; + + ((MainWindow *)parent())->startHelper(); + updateHelper(); +} + + +void SettingsWindow::updateHelper() +{ + if (debug) qDebug() << "[SettingsWindow]" << "[updateHelper]"; + + if (((MainWindow *)parent())->isHelperActive()) { + ui->label_status->setText(QApplication::translate("SettingsWindow", "Active")); + ui->pushButton_status->setText(QApplication::translate("SettingsWindow", "Stop")); + ui->pushButton_status->setIcon(QIcon::fromTheme("process-stop")); + } + else { + ui->label_status->setText(QApplication::translate("SettingsWindow", "Inactive")); + ui->pushButton_status->setText(QApplication::translate("SettingsWindow", "Start")); + ui->pushButton_status->setIcon(QIcon::fromTheme("system-run")); + } +} diff --git a/sources/gui/src/settingswindow.h b/sources/gui/src/settingswindow.h index 9018451..392ed1f 100644 --- a/sources/gui/src/settingswindow.h +++ b/sources/gui/src/settingswindow.h @@ -42,6 +42,7 @@ public: QMap getSettings(); public slots: + void closeWindow(); void setDefault(); void showWindow(); @@ -50,7 +51,9 @@ private slots: void changePage(QTreeWidgetItem *current, QTreeWidgetItem *previous); void saveSettings(); void setTray(); + void updateHelper(); // buttons + void selectHelperPath(); void selectIfaceDir(); void selectNetctlPath(); void selectNetctlAutoPath(); @@ -60,6 +63,7 @@ private slots: void selectSystemctlPath(); void selectWpaCliPath(); void selectWpaSupPath(); + void startHelper(); private: bool debug; diff --git a/sources/gui/src/settingswindow.ui b/sources/gui/src/settingswindow.ui index a9ab83e..5135af5 100644 --- a/sources/gui/src/settingswindow.ui +++ b/sources/gui/src/settingswindow.ui @@ -6,8 +6,8 @@ 0 0 - 660 - 321 + 658 + 319 @@ -67,6 +67,15 @@ + + + Helper + + + + :/icon.png:/icon.png + + netctl @@ -110,7 +119,7 @@ - 0 + 1 @@ -127,8 +136,8 @@ 0 0 - 444 - 260 + 442 + 258 @@ -190,6 +199,125 @@ + + + + + + true + + + + + 0 + 0 + 442 + 258 + + + + + + + + + Helper status + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + + + Start + + + + + + + + + + + + Use helper + + + + + + + Force use sudo in helper + + + + + + + Close helper after exit + + + + + + + + + Helper command + + + + + + + + + + Browse + + + + + + + + + Qt::Vertical + + + + 20 + 104 + + + + + + + + + + diff --git a/sources/helper/CMakeLists.txt b/sources/helper/CMakeLists.txt new file mode 100644 index 0000000..6945696 --- /dev/null +++ b/sources/helper/CMakeLists.txt @@ -0,0 +1,12 @@ +# set project name +set (SUBPROJECT netctlgui-helper) +message (STATUS "Subproject ${SUBPROJECT}") + +# set directories +set (SUBPROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src) + +# additional targets +set (TARGETS "") +set (HEADERS "") + +add_subdirectory (${SUBPROJECT_SOURCE_DIR}) diff --git a/sources/helper/src/CMakeLists.txt b/sources/helper/src/CMakeLists.txt new file mode 100644 index 0000000..13497b5 --- /dev/null +++ b/sources/helper/src/CMakeLists.txt @@ -0,0 +1,32 @@ +# set files +file (GLOB SOURCES *.cpp) +file (GLOB HEADERS *.h) + +# include_path +include_directories (${CMAKE_CURRENT_SOURCE_DIR}/../../${PROJECT_LIBRARY}/include/ + ${CMAKE_CURRENT_BINARY_DIR}/../ + ${CMAKE_SOURCE_DIR} + ${CMAKE_BINARY_DIR} + ${CMAKE_CURRENT_BINARY_DIR}) +link_directories (${PROJECT_LIBRARY}/src/lib) + +if (USE_QT5) + find_package(Qt5Core REQUIRED) + find_package(Qt5DBus REQUIRED) + add_definitions(${Qt5Core_DEFINITIONS}) + add_definitions(${Qt5DBus_DEFINITIONS}) + include_directories (${Qt5Core_INCLUDE_DIRS} ${Qt5DBus_INCLUDE_DIRS}) + set (QT_NEEDED_LIBS ${Qt5Core_LIBRARIES} ${Qt5DBus_LIBRARIES}) + qt5_wrap_cpp (MOC_SOURCES ${HEADERS}) +else () + find_package (Qt4 COMPONENTS QtCore QtDBus REQUIRED) + include (${QT_USE_FILE}) + set (QT_NEEDED_LIBS ${QT_QTCORE_LIBRARY} ${QT_QTDBUS_LIBRARY}) + qt4_wrap_cpp (MOC_SOURCES ${HEADERS}) +endif() + +add_executable (${SUBPROJECT} ${SOURCES} ${HEADERS} ${MOC_SOURCES}) +target_link_libraries (${SUBPROJECT} ${PROJECT_LIBRARY} ${QT_NEEDED_LIBS}) +# install properties +install (TARGETS ${SUBPROJECT} DESTINATION bin + PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE SETUID) diff --git a/sources/gui/src/controladaptor.cpp b/sources/helper/src/controladaptor.cpp similarity index 89% rename from sources/gui/src/controladaptor.cpp rename to sources/helper/src/controladaptor.cpp index 05c4cde..df0c453 100644 --- a/sources/gui/src/controladaptor.cpp +++ b/sources/helper/src/controladaptor.cpp @@ -16,11 +16,14 @@ ***************************************************************************/ +#include "netctlhelper.h" #include "controladaptor.h" -ControlAdaptor::ControlAdaptor(QObject *parent, const QMap configuration) - : QDBusAbstractAdaptor(parent) +ControlAdaptor::ControlAdaptor(NetctlHelper *parent, const QMap configuration) + : QDBusAbstractAdaptor(parent), + helper(parent) + { netctlCommand = new Netctl(false, configuration); netctlProfile = new NetctlProfile(false, configuration); @@ -34,6 +37,20 @@ ControlAdaptor::~ControlAdaptor() } +// helper +bool ControlAdaptor::Active() +{ + return true; +} + + +bool ControlAdaptor::Close() +{ + helper->quitHelper(); + return true; +} + + // netctlCommand bool ControlAdaptor::autoDisableAll() { diff --git a/sources/gui/src/controladaptor.h b/sources/helper/src/controladaptor.h similarity index 91% rename from sources/gui/src/controladaptor.h rename to sources/helper/src/controladaptor.h index 57d8bc2..c2e4dc2 100644 --- a/sources/gui/src/controladaptor.h +++ b/sources/helper/src/controladaptor.h @@ -23,17 +23,22 @@ #include +class NetctlHelper; + class ControlAdaptor : public QDBusAbstractAdaptor { Q_OBJECT - Q_CLASSINFO("D-Bus Interface", "org.freedesktop.netctlgui") + Q_CLASSINFO("D-Bus Interface", "org.netctlgui.helper") public: - explicit ControlAdaptor(QObject *parent = 0, + explicit ControlAdaptor(NetctlHelper *parent = 0, const QMap configuration = QMap()); ~ControlAdaptor(); public slots: + // helper + bool Active(); + bool Close(); // netctlCommand bool autoDisableAll(); bool autoEnable(const QString profile); @@ -50,6 +55,7 @@ public slots: bool Remove(const QString profile); private: + NetctlHelper *helper; Netctl *netctlCommand; NetctlProfile *netctlProfile; }; diff --git a/sources/helper/src/main.cpp b/sources/helper/src/main.cpp new file mode 100644 index 0000000..d7fe346 --- /dev/null +++ b/sources/helper/src/main.cpp @@ -0,0 +1,121 @@ +/*************************************************************************** + * 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 +#include +#include +#include +#include +#include + +#include "messages.h" +#include "netctlhelper.h" +#include "version.h" + + +using namespace std; + + +bool checkExistSession() +{ + QDBusConnection bus = QDBusConnection::sessionBus(); + QDBusMessage request = QDBusMessage::createMethodCall(QString(DBUS_HELPER_SERVICE), + QString(DBUS_CONTROL_PATH), + QString(DBUS_HELPER_INTERFACE), + QString("Active")); + QDBusMessage response = bus.call(request); + QList arguments = response.arguments(); + return ((arguments.size() == 1) && arguments[0].toBool()); +} + + +int main(int argc, char *argv[]) +{ + // detach from console + bool isDaemon = true; + for (int i=0; i= 0x050000 + QCoreApplication::setSetuidAllowed(true); +#endif + QCoreApplication a(argc, argv); + // check if exists + if (checkExistSession()) + return 0; + + QMap args = getArgs(); + // reading + for (int i=1; i +#include + +#include "messages.h" +#include "version.h" + + +QString errorMessage() +{ + QString errorMessage = QCoreApplication::translate("NetctlHelper", "Unknown flag\n"); + + return errorMessage; +} + + +QMap getArgs() +{ + QMap args; + args[QString("config")] = QString(QDir::homePath() + QString("/.config/netctl-gui.conf")); + args[QString("debug")] = false; + args[QString("nodaemon")] = false; + args[QString("help")] = false; + args[QString("info")] = false; + args[QString("version")] = false; + args[QString("error")] = false; + + return args; +} + + +QString helpMessage() +{ + QString helpMessage = QString(""); + helpMessage += QString("%1\n").arg(QCoreApplication::translate("NetctlHelper", "Usage:")); + helpMessage += QString("netctlgui-helper [ options ]\n"); + helpMessage += QString("%1\n").arg(QCoreApplication::translate("NetctlHelper", "Options:")); + // windows + helpMessage += QString(" -c, --config - %1\n") + .arg(QCoreApplication::translate("NetctlHelper", "read configuration from this file")); + helpMessage += QString(" -d, --debug - %1\n") + .arg(QCoreApplication::translate("NetctlHelper", "print debug information")); + helpMessage += QString(" --nodaemon - %1\n") + .arg(QCoreApplication::translate("NetctlHelper", "do not start as daemon")); + helpMessage += QString(" %1\n").arg(QCoreApplication::translate("NetctlHelper", "Show messages:")); + helpMessage += QString(" -v, --version - %1\n") + .arg(QCoreApplication::translate("NetctlHelper", "show version and exit")); + helpMessage += QString(" -i, --info - %1\n") + .arg(QCoreApplication::translate("NetctlHelper", "show build information and exit")); + helpMessage += QString(" -h, --help - %1\n") + .arg(QCoreApplication::translate("NetctlHelper", "show this help and exit")); + + return helpMessage; +} + + +QString infoMessage() +{ + QString infoMessage = QString(""); + // build information + infoMessage += QCoreApplication::translate("NetctlHelper", "Build date: %1"). + arg(QString(BUILD_DATE)); + infoMessage += QString("\n%1:\n").arg(QCoreApplication::translate("NetctlHelper", "cmake flags")); + infoMessage += QString("\t-DCMAKE_BUILD_TYPE=%1 \\\n").arg(QString(CMAKE_BUILD_TYPE)); + infoMessage += QString("\t-DCMAKE_INSTALL_PREFIX=%1 \\\n").arg(QString(CMAKE_INSTALL_PREFIX)); + infoMessage += QString("\t-DBUILD_DOCS=%1 \\\n").arg(QString(PROJECT_BUILD_DOCS)); + infoMessage += QString("\t-DBUILD_LIBRARY=%1 \\\n").arg(QString(PROJECT_BUILD_LIBRARY)); + infoMessage += QString("\t-DBUILD_GUI=%1 \\\n").arg(QString(PROJECT_BUILD_GUI)); + infoMessage += QString("\t-DUSE_QT5=%1 \\\n").arg(QString(PROJECT_USE_QT5)); + infoMessage += QString("\t-DBUILD_DATAENGINE=%1 \\\n").arg(QString(PROJECT_BUILD_DATAENGINE)); + infoMessage += QString("\t-DBUILD_PLASMOID=%1\n").arg(QString(PROJECT_BUILD_PLASMOID)); + // transport information + infoMessage += QString("%1:\n").arg(QCoreApplication::translate("NetctlHelper", "DBus configuration")); + infoMessage += QString("\tDBUS_SERVICE=%1\n").arg(QString(DBUS_SERVICE)); + infoMessage += QString("\tDBUS_INTERFACE=%1\n").arg(QString(DBUS_INTERFACE)); + infoMessage += QString("\tDBUS_CONTROL_PATH=%1\n").arg(QString(DBUS_CONTROL_PATH)); + infoMessage += QString("\tDBUS_LIB_PATH=%1\n").arg(QString(DBUS_LIB_PATH)); + infoMessage += QString("\tDBUS_OBJECT_PATH=%1\n").arg(QString(DBUS_OBJECT_PATH)); + + return infoMessage; +} + + +QString versionMessage() +{ + QString versionMessage = QString(""); + versionMessage += QString("%1\n").arg(QString(NAME)); + versionMessage += QString("%1 : %2\n") + .arg(QCoreApplication::translate("NetctlHelper", "Version")) + .arg(QString(VERSION)); + versionMessage += QString("%1 : %2\n") + .arg(QCoreApplication::translate("NetctlHelper", "Author")) + .arg(QString(AUTHOR)); + versionMessage += QString("%1 : %2\n") + .arg(QCoreApplication::translate("NetctlHelper", "License")) + .arg(QString(LICENSE)); + + return versionMessage; +} diff --git a/sources/helper/src/messages.h b/sources/helper/src/messages.h new file mode 100644 index 0000000..6535478 --- /dev/null +++ b/sources/helper/src/messages.h @@ -0,0 +1,34 @@ +/*************************************************************************** + * 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/ * + ***************************************************************************/ + +#ifndef MESSAGES_H +#define MESSAGES_H + +#include +#include +#include +#include + + +QString errorMessage(); +QMap getArgs(); +QString helpMessage(); +QString infoMessage(); +QString versionMessage(); + + +#endif /* MESSAGES_H */ diff --git a/sources/gui/src/netctladaptor.cpp b/sources/helper/src/netctladaptor.cpp similarity index 100% rename from sources/gui/src/netctladaptor.cpp rename to sources/helper/src/netctladaptor.cpp diff --git a/sources/gui/src/netctladaptor.h b/sources/helper/src/netctladaptor.h similarity index 97% rename from sources/gui/src/netctladaptor.h rename to sources/helper/src/netctladaptor.h index e33548e..ac6983b 100644 --- a/sources/gui/src/netctladaptor.h +++ b/sources/helper/src/netctladaptor.h @@ -27,7 +27,7 @@ class NetctlAdaptor : public QDBusAbstractAdaptor { Q_OBJECT - Q_CLASSINFO("D-Bus Interface", "org.freedesktop.netctlgui") + Q_CLASSINFO("D-Bus Interface", "org.netctlgui.helper") public: explicit NetctlAdaptor(QObject *parent = 0, diff --git a/sources/helper/src/netctlhelper.cpp b/sources/helper/src/netctlhelper.cpp new file mode 100644 index 0000000..d54e96d --- /dev/null +++ b/sources/helper/src/netctlhelper.cpp @@ -0,0 +1,152 @@ +/*************************************************************************** + * 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 +#include +#include + +#include + +#include "controladaptor.h" +#include "netctladaptor.h" +#include "netctlhelper.h" +#include "version.h" + + +NetctlHelper::NetctlHelper(QObject *parent, QMap args) + : QObject(parent), + configPath(args[QString("config")].toString()), + debug(args[QString("debug")].toBool()) +{ + updateConfiguration(); +} + + +NetctlHelper::~NetctlHelper() +{ + if (debug) qDebug() << "[NetctlHelper]" << "[~NetctlHelper]"; + + deleteInterface(); +} + + +QMap NetctlHelper::getDefault() +{ + if (debug) qDebug() << "[NetctlHelper]" << "[getDefault]"; + + QMap settings; + settings[QString("CLOSETOTRAY")] = QString("true"); + settings[QString("CTRL_DIR")] = QString("/run/wpa_supplicant_netctl-gui"); + settings[QString("CTRL_GROUP")] = QString("users"); + settings[QString("IFACE_DIR")] = QString("/sys/class/net/"); + settings[QString("LANGUAGE")] = QString("en"); + settings[QString("NETCTL_PATH")] = QString("/usr/bin/netctl"); + settings[QString("NETCTLAUTO_PATH")] = QString("/usr/bin/netctl-auto"); + settings[QString("NETCTLAUTO_SERVICE")] = QString("netctl-auto"); + settings[QString("PID_FILE")] = QString("/run/wpa_supplicant_netctl-gui.pid"); + settings[QString("PREFERED_IFACE")] = QString(""); + settings[QString("PROFILE_DIR")] = QString("/etc/netctl/"); + settings[QString("RFKILL_DIR")] = QString("/sys/class/rfkill/"); + settings[QString("STARTTOTRAY")] = QString("false"); + settings[QString("SUDO_PATH")] = QString("/usr/bin/kdesu"); + settings[QString("SYSTEMCTL_PATH")] = QString("/usr/bin/systemctl"); + settings[QString("SYSTRAY")] = QString("true"); + settings[QString("WPACLI_PATH")] = QString("/usr/bin/wpa_cli"); + settings[QString("WPASUP_PATH")] = QString("/usr/bin/wpa_supplicant"); + settings[QString("WPA_DRIVERS")] = QString("nl80211,wext"); + for (int i=0; i settings = getDefault(); + QFile configFile(configPath); + QString fileStr; + if (!configFile.open(QIODevice::ReadOnly)) + return settings; + while (true) { + fileStr = QString(configFile.readLine()).trimmed(); + if ((fileStr.isEmpty()) && (!configFile.atEnd())) continue; + if ((fileStr[0] == QChar('#')) && (!configFile.atEnd())) continue; + if ((fileStr[0] == QChar(';')) && (!configFile.atEnd())) continue; + if (fileStr.contains(QChar('='))) + settings[fileStr.split(QChar('='))[0]] = fileStr.split(QChar('='))[1]; + if (configFile.atEnd()) break; + } + configFile.close(); + for (int i=0; i +#include +#include + + +class NetctlHelper : public QObject +{ + Q_OBJECT + +public: + explicit NetctlHelper(QObject *parent = 0, + QMap args = QMap()); + ~NetctlHelper(); + +public slots: + QMap getDefault(); + QMap getSettings(); + void quitHelper(); + +private: + QString configPath; + QMap configuration; + bool debug; + void createInterface(); + void deleteInterface(); + void updateConfiguration(); +}; + + +#endif /* NETCTLHELPER_H */ diff --git a/sources/version.h.in b/sources/version.h.in index 9cf9096..ffce9c9 100644 --- a/sources/version.h.in +++ b/sources/version.h.in @@ -27,10 +27,12 @@ #define PROJECT_BUILD_PLASMOID "@BUILD_PLASMOID@" #define PROJECT_USE_QT5 "@USE_QT5@" -#define DBUS_CONTROL_PATH "/ctrl" -#define DBUS_INTERFACE "org.freedesktop.netctlgui" -#define DBUS_LIB_PATH "/netctl" +#define DBUS_SERVICE "org.netctlgui.netctlgui" +#define DBUS_INTERFACE "org.netctlgui.netctlgui" #define DBUS_OBJECT_PATH "/netctlgui" -#define DBUS_SERVICE "org.freedesktop.netctlgui" +#define DBUS_HELPER_SERVICE "org.netctlgui.helper" +#define DBUS_HELPER_INTERFACE "org.netctlgui.helper" +#define DBUS_CONTROL_PATH "/ctrl" +#define DBUS_LIB_PATH "/netctl" #endif /* VERSION_H */