diff --git a/README.md b/README.md index cc21fbe..6fddadd 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Additional information TODO (wish list) ---------------- -* plasmoid helper integration +* refactoring (I think that classes with more than 1k lines is not good :/) * remove suid from helper (polkit integration or run through sudo) * security notes / project architecture * autotests diff --git a/sources/gui/src/mainactions.cpp b/sources/gui/src/mainactions.cpp index f229e68..ec2e4a8 100644 --- a/sources/gui/src/mainactions.cpp +++ b/sources/gui/src/mainactions.cpp @@ -435,6 +435,20 @@ void MainWindow::reportABug() } +void MainWindow::checkHelperStatus() +{ + if (debug) qDebug() << "[MainWindow]" << "[checkHelperStatus]"; + + if (useHelper) useHelper = isHelperActive(); + if (useHelper) + sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_CTRL_PATH, + DBUS_HELPER_INTERFACE, QString("Update"), + QList(), true, debug); + if (isHelperServiceActive()) + configuration[QString("CLOSE_HELPER")] = QString("false"); +} + + // main tab slots void MainWindow::mainTabContextualMenu(const QPoint &pos) { diff --git a/sources/gui/src/mainwindow.cpp b/sources/gui/src/mainwindow.cpp index 406400f..bc15d7b 100644 --- a/sources/gui/src/mainwindow.cpp +++ b/sources/gui/src/mainwindow.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include "aboutwindow.h" @@ -495,6 +496,9 @@ void MainWindow::updateConfiguration(const QMap args) useHelper = false; configuration[QString("USE_HELPER")] = QString("false"); } + // some helper fixs + // because interface will be created with a delay + QTimer::singleShot(1000, this, SLOT(checkHelperStatus())); // update translation qApp->removeTranslator(translator); @@ -506,14 +510,6 @@ void MainWindow::updateConfiguration(const QMap args) delete settingsWin; createObjects(); - // some helper fixs - if (useHelper) useHelper = isHelperActive(); - if (useHelper) - sendDBusRequest(DBUS_HELPER_SERVICE, DBUS_CTRL_PATH, - DBUS_HELPER_INTERFACE, QString("Update"), - QList(), true, debug); - if (isHelperServiceActive()) - configuration[QString("CLOSE_HELPER")] = QString("false"); // update ui setTab(args[QString("tab")].toInt() - 1); createActions(); diff --git a/sources/gui/src/mainwindow.h b/sources/gui/src/mainwindow.h index 8d3c591..c28ac5a 100644 --- a/sources/gui/src/mainwindow.h +++ b/sources/gui/src/mainwindow.h @@ -96,6 +96,7 @@ public slots: void setHiddenName(const QString name); private slots: + void checkHelperStatus(); void reportABug(); // menu update slots void setMenuActionsShown(const bool state = true); diff --git a/sources/plasmoid/netctl.cpp b/sources/plasmoid/netctl.cpp index 7c86822..cb7b5b7 100644 --- a/sources/plasmoid/netctl.cpp +++ b/sources/plasmoid/netctl.cpp @@ -23,10 +23,13 @@ #include #include +#include +#include #include #include #include #include +#include #include "netctl.h" #include "ui_about.h" @@ -290,8 +293,6 @@ void Netctl::enableProfileSlot() { if (debug) qDebug() << "[PLASMOID]" << "[enableProfileSlot]"; - QProcess command; - QString commandLine = QString(""); QString enableStatus = QString(""); if (info[QString("status")].contains(QString("enabled"))) { enableStatus = QString(" disable "); @@ -301,11 +302,40 @@ void Netctl::enableProfileSlot() enableStatus = QString(" enable "); sendNotification(QString("Info"), i18n("Set profile %1 enabled", info[QString("name")])); } - if (useSudo) - commandLine = paths[QString("sudo")] + QString(" "); - commandLine += paths[QString("netctl")] + enableStatus + info[QString("name")]; + if (useHelper) { + QList args; + args.append(info[QString("name")]); + sendDBusRequest(QString("Enable"), args); + } + else { + QProcess command; + QString commandLine = QString(""); + if (useSudo) + commandLine = paths[QString("sudo")] + QString(" "); + commandLine += paths[QString("netctl")] + enableStatus + info[QString("name")]; + command.startDetached(commandLine); + } +} - command.startDetached(commandLine); + +void Netctl::restartProfileSlot() +{ + if (debug) qDebug() << "[PLASMOID]" << "[restartProfileSlot]"; + + sendNotification(QString("Info"), i18n("Restart profile %1", info[QString("name")])); + if (useHelper) { + QList args; + args.append(info[QString("name")]); + sendDBusRequest(QString("Restart"), args); + } + else { + QProcess command; + QString commandLine = QString(""); + if (useSudo) + commandLine = paths[QString("sudo")] + QString(" "); + commandLine += paths[QString("netctl")] + QString(" restart ") + info[QString("name")]; + command.startDetached(commandLine); + } } @@ -314,17 +344,26 @@ void Netctl::startProfileSlot(QAction *profile) if (debug) qDebug() << "[PLASMOID]" << "[startProfileSlot]"; if (debug) qDebug() << "[PLASMOID]" << "[startProfileSlot]" << ":" << "Profile" << profile->text().remove(QChar('&')); - QProcess command; - QString commandLine = QString(""); sendNotification(QString("Info"), i18n("Start profile %1", profile->text().remove(QChar('&')))); - if (useSudo) - commandLine = paths[QString("sudo")] + QString(" "); - if (status) - commandLine += paths[QString("netctl")] + QString(" switch-to ") + profile->text().remove(QChar('&')); - else - commandLine += paths[QString("netctl")] + QString(" start ") + profile->text().remove(QChar('&')); - - command.startDetached(commandLine); + if (useHelper) { + QList args; + args.append(profile->text().remove(QChar('&'))); + if (status) + sendDBusRequest(QString("SwitchTo"), args); + else + sendDBusRequest(QString("Start"), args); + } + else { + QProcess command; + QString commandLine = QString(""); + if (useSudo) + commandLine = paths[QString("sudo")] + QString(" "); + if (status) + commandLine += paths[QString("netctl")] + QString(" switch-to ") + profile->text().remove(QChar('&')); + else + commandLine += paths[QString("netctl")] + QString(" start ") + profile->text().remove(QChar('&')); + command.startDetached(commandLine); + } } @@ -332,14 +371,20 @@ void Netctl::stopProfileSlot() { if (debug) qDebug() << "[PLASMOID]" << "[stopProfileSlot]"; - QProcess command; - QString commandLine = QString(""); sendNotification(QString("Info"), i18n("Stop profile %1", info[QString("name")])); - if (useSudo) - commandLine = paths[QString("sudo")] + QString(" "); - commandLine += paths[QString("netctl")] + QString(" stop ") + info[QString("name")]; - - command.startDetached(commandLine); + if (useHelper) { + QList args; + args.append(info[QString("name")]); + sendDBusRequest(QString("Start"), args); + } + else { + QProcess command; + QString commandLine = QString(""); + if (useSudo) + commandLine = paths[QString("sudo")] + QString(" "); + commandLine += paths[QString("netctl")] + QString(" stop ") + info[QString("name")]; + command.startDetached(commandLine); + } } @@ -348,29 +393,37 @@ void Netctl::switchToProfileSlot(QAction *profile) if (debug) qDebug() << "[PLASMOID]" << "[switchToProfileSlot]"; if (debug) qDebug() << "[PLASMOID]" << "[switchToProfileSlot]" << ":" << "Profile" << profile->text().remove(QChar('&')); - QProcess command; - QString commandLine; - commandLine = QString(""); - sendNotification(QString("Info"), i18n("Switch to profile %1", profile->text().remove(QChar('&')))); - commandLine = paths[QString("netctlAuto")] + QString(" switch-to ") + - profile->text().remove(QChar('&')); + sendNotification(QString("Info"), i18n("Switch to profile %1", profile->text().remove(QChar('&')))); + if (useHelper) { + QList args; + args.append(profile->text().remove(QChar('&'))); + sendDBusRequest(QString("autoStart"), args); + } + else { + QProcess command; + QString commandLine = paths[QString("netctlAuto")] + QString(" switch-to ") + + profile->text().remove(QChar('&')); + command.startDetached(commandLine); + } +} + + +void Netctl::startHelper() +{ + if (debug) qDebug() << "[PLASMOID]" << "[startHelper]"; + + QProcess command; + QString commandLine = paths[QString("helper")]; command.startDetached(commandLine); } -void Netctl::restartProfileSlot() +void Netctl::checkHelperStatus() { - if (debug) qDebug() << "[PLASMOID]" << "[restartProfileSlot]"; + if (debug) qDebug() << "[PLASMOID]" << "[checkHelperStatus]"; - QProcess command; - QString commandLine = QString(""); - sendNotification(QString("Info"), i18n("Restart profile %1", info[QString("name")])); - if (useSudo) - commandLine = paths[QString("sudo")] + QString(" "); - commandLine += paths[QString("netctl")] + QString(" restart ") + info[QString("name")]; - - command.startDetached(commandLine); + if (useHelper) useHelper = !sendDBusRequest(QString("Active"), QList()).isEmpty(); } @@ -608,6 +661,26 @@ void Netctl::disconnectFromEngine() } +QList Netctl::sendDBusRequest(const QString cmd, const QList args) +{ + if (debug) qDebug() << "[PLASMOID]" << "[sendDBusRequest]"; + if (debug) qDebug() << "[PLASMOID]" << "[sendDBusRequest]" << ":" << "cmd" << cmd; + if (debug) qDebug() << "[PLASMOID]" << "[sendDBusRequest]" << ":" << "args" << args; + + QDBusConnection bus = QDBusConnection::systemBus(); + QDBusMessage request = QDBusMessage::createMethodCall(DBUS_HELPER_SERVICE, DBUS_CTRL_PATH, + DBUS_HELPER_INTERFACE, cmd); + if (!args.isEmpty()) + request.setArguments(args); + QDBusMessage response = bus.call(request); + QList arguments = response.arguments(); + if (arguments.size() == 0) + if (debug) qDebug() << "[PLASMOID]" << "[sendDBusRequest]" << ":" << "Error message" << response.errorMessage(); + + return arguments; +} + + // configuration interface void Netctl::selectAbstractSomething() { @@ -625,6 +698,8 @@ void Netctl::selectAbstractSomething() } else if (sender() == uiWidConfig.pushButton_gui) lineEdit = uiWidConfig.lineEdit_gui; + else if (sender() == uiWidConfig.pushButton_helper) + lineEdit = uiWidConfig.lineEdit_helper; else if (sender() == uiWidConfig.pushButton_netctl) lineEdit = uiWidConfig.lineEdit_netctl; else if (sender() == uiWidConfig.pushButton_netctlAuto) @@ -662,6 +737,12 @@ void Netctl::createConfigurationInterface(KConfigDialog *parent) uiWidConfig.spinBox_autoUpdate->setValue(autoUpdateInterval); uiWidConfig.lineEdit_gui->setText(paths[QString("gui")]); + if (useHelper) + uiWidConfig.checkBox_helper->setCheckState(Qt::Checked); + else + uiWidConfig.checkBox_helper->setCheckState(Qt::Unchecked); + uiWidConfig.lineEdit_helper->setText(paths[QString("helper")]); + setHelper(); uiWidConfig.lineEdit_netctl->setText(paths[QString("netctl")]); uiWidConfig.lineEdit_netctlAuto->setText(paths[QString("netctlAuto")]); if (useSudo) @@ -742,6 +823,7 @@ void Netctl::createConfigurationInterface(KConfigDialog *parent) parent->addPage(deWidget, i18n("DataEngine"), Applet::icon()); parent->addPage(aboutWidget, i18n("About"), QString("help-about")); + connect(uiWidConfig.checkBox_helper, SIGNAL(stateChanged(int)), this, SLOT(setHelper())); connect(uiWidConfig.checkBox_showBigInterface, SIGNAL(stateChanged(int)), this, SLOT(setBigInterface())); connect(uiWidConfig.checkBox_sudo, SIGNAL(stateChanged(int)), this, SLOT(setSudo())); @@ -750,6 +832,7 @@ void Netctl::createConfigurationInterface(KConfigDialog *parent) connect(uiDEConfig.checkBox_extIp6, SIGNAL(stateChanged(int)), this, SLOT(setDataEngineExternalIp6())); connect(uiWidConfig.pushButton_gui, SIGNAL(clicked()), this, SLOT(selectAbstractSomething())); + connect(uiWidConfig.pushButton_helper, SIGNAL(clicked()), this, SLOT(selectAbstractSomething())); connect(uiWidConfig.pushButton_netctl, SIGNAL(clicked()), this, SLOT(selectAbstractSomething())); connect(uiWidConfig.pushButton_netctlAuto, SIGNAL(clicked()), this, SLOT(selectAbstractSomething())); connect(uiWidConfig.pushButton_sudo, SIGNAL(clicked()), this, SLOT(selectAbstractSomething())); @@ -775,6 +858,11 @@ void Netctl::configAccepted() cg.writeEntry("autoUpdateInterval", uiWidConfig.spinBox_autoUpdate->value()); cg.writeEntry("guiPath", uiWidConfig.lineEdit_gui->text()); + if (uiWidConfig.checkBox_helper->checkState() == 0) + cg.writeEntry("useHelper", false); + else + cg.writeEntry("useHelper", true); + cg.writeEntry("helperPath", uiWidConfig.lineEdit_helper->text()); cg.writeEntry("netctlPath", uiWidConfig.lineEdit_netctl->text()); cg.writeEntry("netctlAutoPath", uiWidConfig.lineEdit_netctlAuto->text()); if (uiWidConfig.checkBox_sudo->checkState() == 0) @@ -829,6 +917,7 @@ void Netctl::configChanged() autoUpdateInterval = cg.readEntry("autoUpdateInterval", 1000); paths[QString("gui")] = cg.readEntry("guiPath", "/usr/bin/netctl-gui"); + paths[QString("helper")] = cg.readEntry("helperPath", "/usr/bin/netctlgui-helper"); paths[QString("netctl")] = cg.readEntry("netctlPath", "/usr/bin/netctl"); paths[QString("netctlAuto")] = cg.readEntry("netctlAutoPath", "/usr/bin/netctl-auto"); paths[QString("sudo")] = cg.readEntry("sudoPath", "/usr/bin/kdesu"); @@ -836,6 +925,7 @@ void Netctl::configChanged() useSudo = cg.readEntry("useSudo", true); useWifi = cg.readEntry("useWifi", false); bigInterface = cg.readEntry("showBigInterface", true); + useHelper = cg.readEntry("useHelper", true); textPattern = cg.readEntry("textPattern", "$current $status
IPv4: $intip4
IPv6: $intip6"); QString textAlign = cg.readEntry("textAlign", "center"); @@ -860,6 +950,8 @@ void Netctl::configChanged() .arg(fontColor); formatLine[1] = QString("

"); + if (useHelper) startHelper(); + QTimer::singleShot(1000, this, SLOT(checkHelperStatus())); connectToEngine(); } @@ -905,6 +997,19 @@ void Netctl::setDataEngineExternalIp6() } +void Netctl::setHelper() +{ + if (uiWidConfig.checkBox_helper->checkState() == 0) { + uiWidConfig.lineEdit_helper->setDisabled(true); + uiWidConfig.pushButton_helper->setDisabled(true); + } + else if (uiWidConfig.checkBox_helper->checkState() == 2) { + uiWidConfig.lineEdit_helper->setEnabled(true); + uiWidConfig.pushButton_helper->setEnabled(true); + } +} + + void Netctl::setSudo() { if (debug) qDebug() << "[PLASMOID]" << "[setSudo]"; diff --git a/sources/plasmoid/netctl.h b/sources/plasmoid/netctl.h index ddf08a5..e2fd9ee 100644 --- a/sources/plasmoid/netctl.h +++ b/sources/plasmoid/netctl.h @@ -73,6 +73,7 @@ public slots: void setBigInterface(); void setDataEngineExternalIp(); void setDataEngineExternalIp6(); + void setHelper(); void setSudo(); void setWifi(); @@ -84,10 +85,13 @@ private slots: void selectAbstractSomething(); // context menu void enableProfileSlot(); + void restartProfileSlot(); void startProfileSlot(QAction *profile); void stopProfileSlot(); void switchToProfileSlot(QAction *profile); - void restartProfileSlot(); + // helper + void checkHelperStatus(); + void startHelper(); protected: void createConfigurationInterface(KConfigDialog *parent); @@ -113,6 +117,7 @@ private: Plasma::DataEngine *netctlEngine; void connectToEngine(); void disconnectFromEngine(); + QList sendDBusRequest(const QString cmd, const QList args = QList()); // configuration interface Ui::AppearanceWindow uiAppConfig; Ui::DataEngineWindow uiDEConfig; @@ -125,7 +130,7 @@ private: QString textPattern; QStringList formatLine; QMap paths; - bool useSudo, useWifi; + bool useHelper, useSudo, useWifi; }; K_EXPORT_PLASMA_APPLET(netctl, Netctl) diff --git a/sources/plasmoid/widget.ui b/sources/plasmoid/widget.ui index ea12e4d..f452c05 100644 --- a/sources/plasmoid/widget.ui +++ b/sources/plasmoid/widget.ui @@ -7,7 +7,7 @@ 0 0 480 - 335 + 342 @@ -111,6 +111,42 @@ + + + + + + + 150 + 23 + + + + Use helper + + + true + + + + + + + + + + + 100 + 23 + + + + Browse + + + + +