From b65f7615206892c25d900ea5da92dfbdea99b1d3 Mon Sep 17 00:00:00 2001 From: arcan1s Date: Tue, 12 Aug 2014 14:01:00 +0400 Subject: [PATCH] some improvements in helper and gui APIs and replacement and restoring sessions --- sources/gui/docs/netctl-gui-dbus-api.html | 22 ++++++++++--- sources/gui/src/main.cpp | 32 ++++++++++++++++--- sources/gui/src/netctlguiadaptor.cpp | 17 ++++++++++ sources/gui/src/netctlguiadaptor.h | 2 ++ sources/helper/bash-completions | 4 ++- sources/helper/netctlgui-helper.1 | 8 +++-- sources/helper/src/controladaptor.cpp | 11 +++++++ sources/helper/src/controladaptor.h | 1 + sources/helper/src/main.cpp | 38 ++++++++++++++++++++--- sources/helper/src/messages.cpp | 5 +++ sources/helper/zsh-completions | 4 ++- 11 files changed, 128 insertions(+), 16 deletions(-) diff --git a/sources/gui/docs/netctl-gui-dbus-api.html b/sources/gui/docs/netctl-gui-dbus-api.html index ed25164..4543303 100644 --- a/sources/gui/docs/netctl-gui-dbus-api.html +++ b/sources/gui/docs/netctl-gui-dbus-api.html @@ -58,7 +58,7 @@ th.sub { bool Active() - true if helper is active + true if the helper is active no @@ -91,9 +91,14 @@ th.sub { current helper settings KEY==VALUE no + + QStringList UIDs() + returns the helper process UIDs [UID, EUID] + no + bool Update() - calls update helper configuration. Returns true + calls update the helper configuration. Returns true no @@ -257,6 +262,11 @@ th.sub { /netctlgui path + + bool Active() + true if the application is active + no + void ApiDocs() opens this API documentation @@ -269,7 +279,7 @@ th.sub { QString Information() - the same as org.netctlgui.helper /netctl Information + returns general information in format [ActiveProfile, ActiveProfileStatus] no @@ -317,11 +327,15 @@ th.sub { true and shows settings window no + + QStringList UIDs() + returns the application process UIDs [UID, EUID] + no + QStringList VerboseInformation() returns information in format [isNetctlAutoActive, ProfileList, ActiveProfile, isProfileEnabled/autoIsProfileEnabled] no - no diff --git a/sources/gui/src/main.cpp b/sources/gui/src/main.cpp index 6e5e8ac..b2513af 100644 --- a/sources/gui/src/main.cpp +++ b/sources/gui/src/main.cpp @@ -34,11 +34,11 @@ using namespace std; -bool restoreExistSession() +bool existingSessionOperation(const QString operation) { QDBusConnection bus = QDBusConnection::sessionBus(); QDBusMessage request = QDBusMessage::createMethodCall(DBUS_SERVICE, DBUS_OBJECT_PATH, - DBUS_INTERFACE, QString("Restore")); + DBUS_INTERFACE, operation); QDBusMessage response = bus.call(request); QList arguments = response.arguments(); @@ -46,6 +46,18 @@ bool restoreExistSession() } +unsigned int getUidFromSession(const int type = 0) +{ + QDBusConnection bus = QDBusConnection::sessionBus(); + QDBusMessage request = QDBusMessage::createMethodCall(DBUS_SERVICE, DBUS_OBJECT_PATH, + DBUS_INTERFACE, QString("UIDs")); + QDBusMessage response = bus.call(request); + QList arguments = response.arguments(); + + return arguments[0].toStringList()[type].toUInt(); +} + + int main(int argc, char *argv[]) { QMap args = getArgs(); @@ -164,8 +176,20 @@ int main(int argc, char *argv[]) } // check if exists - if (restoreExistSession()) - return 0; + if (existingSessionOperation(QString("Active"))) { + if ((getuid() == getUidFromSession(0)) && (geteuid() == getUidFromSession(1))) { + // restore session + cout << QCoreApplication::translate("MainWindow", "Restore existing session.") + .toUtf8().data() << endl; + existingSessionOperation(QString("Restore")); + return 0; + } else if ((getuid() == getUidFromSession(0)) && (geteuid() != getUidFromSession(1))) { + cout << QCoreApplication::translate("MainWindow", "Close existing session.") + .toUtf8().data() << endl; + existingSessionOperation(QString("Restore")); + return 0; + } + } MainWindow w(0, args, &qtTranslator, &translator); return a.exec(); } diff --git a/sources/gui/src/netctlguiadaptor.cpp b/sources/gui/src/netctlguiadaptor.cpp index 1a5a992..7ad88c9 100644 --- a/sources/gui/src/netctlguiadaptor.cpp +++ b/sources/gui/src/netctlguiadaptor.cpp @@ -18,6 +18,7 @@ #include "netctlguiadaptor.h" #include +#include #include "mainwindow.h" @@ -34,6 +35,12 @@ NetctlGuiAdaptor::~NetctlGuiAdaptor() } +bool NetctlGuiAdaptor::Active() +{ + return true; +} + + void NetctlGuiAdaptor::ApiDocs() { return mainWindow->showApi(); @@ -119,6 +126,16 @@ bool NetctlGuiAdaptor::ShowSettings() } +QStringList NetctlGuiAdaptor::UIDs() +{ + QStringList uids; + uids.append(QString::number(getuid())); + uids.append(QString::number(geteuid())); + + return uids; +} + + QStringList NetctlGuiAdaptor::VerboseInformation() { return mainWindow->printTrayInformation(); diff --git a/sources/gui/src/netctlguiadaptor.h b/sources/gui/src/netctlguiadaptor.h index 20b5fe6..c2074a7 100644 --- a/sources/gui/src/netctlguiadaptor.h +++ b/sources/gui/src/netctlguiadaptor.h @@ -34,6 +34,7 @@ public: ~NetctlGuiAdaptor(); public slots: + bool Active(); void ApiDocs(); bool Close(); QStringList Information(); @@ -46,6 +47,7 @@ public slots: bool ShowMain(); bool ShowNetctlAuto(); bool ShowSettings(); + QStringList UIDs(); QStringList VerboseInformation(); private: diff --git a/sources/helper/bash-completions b/sources/helper/bash-completions index 94c0170..0a2ffb8 100644 --- a/sources/helper/bash-completions +++ b/sources/helper/bash-completions @@ -18,11 +18,13 @@ # variables _netctlgui_helper_arglist=( - '--nodaemon' '-c' '--config' '-d' '--debug' + '--nodaemon' + '--replace' + '--restore' '-v' '--version' '-i' diff --git a/sources/helper/netctlgui-helper.1 b/sources/helper/netctlgui-helper.1 index 4ede1d8..2b9efc1 100644 --- a/sources/helper/netctlgui-helper.1 +++ b/sources/helper/netctlgui-helper.1 @@ -11,13 +11,17 @@ is a Qt based helper daemon which provides a DBus interface for interaction with without any additional permissions. To have access to DBus interface user should have group .B network .SH OPTIONS -.IP "--nodaemon" -do not run as daemon .IP "-c, --config FILE" read configuration from file .I FILE .IP "-d, --debug" print debug information +.IP "--nodaemon" +do not run as daemon +.IP "--replace" +force replace the existing session +.IP "--restore" +force restore the existing session .IP "-v, --version" show version and exit .IP "-i, --info" diff --git a/sources/helper/src/controladaptor.cpp b/sources/helper/src/controladaptor.cpp index 874a311..61cfae0 100644 --- a/sources/helper/src/controladaptor.cpp +++ b/sources/helper/src/controladaptor.cpp @@ -18,6 +18,7 @@ #include "controladaptor.h" #include +#include #include "netctlhelper.h" #include "version.h" @@ -93,6 +94,16 @@ QStringList ControlAdaptor::Settings() } +QStringList ControlAdaptor::UIDs() +{ + QStringList uids; + uids.append(QString::number(getuid())); + uids.append(QString::number(geteuid())); + + return uids; +} + + bool ControlAdaptor::Update() { helper->updateConfiguration(); diff --git a/sources/helper/src/controladaptor.h b/sources/helper/src/controladaptor.h index 8f7eede..281c540 100644 --- a/sources/helper/src/controladaptor.h +++ b/sources/helper/src/controladaptor.h @@ -44,6 +44,7 @@ public slots: QString Pony(); QString SecurityDocs(); QStringList Settings(); + QStringList UIDs(); bool Update(); // netctlCommand bool autoDisableAll(); diff --git a/sources/helper/src/main.cpp b/sources/helper/src/main.cpp index a7dfee1..1c125ef 100644 --- a/sources/helper/src/main.cpp +++ b/sources/helper/src/main.cpp @@ -34,13 +34,14 @@ using namespace std; -bool checkExistSession() +bool existingSessionOperation(const QString operation) { QDBusConnection bus = QDBusConnection::systemBus(); QDBusMessage request = QDBusMessage::createMethodCall(DBUS_HELPER_SERVICE, DBUS_CTRL_PATH, - DBUS_HELPER_INTERFACE, QString("Active")); + DBUS_HELPER_INTERFACE, operation); QDBusMessage response = bus.call(request); QList arguments = response.arguments(); + return !arguments.isEmpty(); } @@ -60,6 +61,12 @@ int main(int argc, char *argv[]) } else if (QString(argv[i]) == QString("--nodaemon")) { // daemonized args[QString("nodaemon")] = true; + } else if (QString(argv[i]) == QString("--replace")) { + // replace + args[QString("state")] = (int) 1; + } else if (QString(argv[i]) == QString("--restore")) { + // restore + args[QString("state")] = (int) 2; } else if ((QString(argv[i]) == QString("-h")) || (QString(argv[i]) == QString("--help"))) { // help message args[QString("help")] = true; @@ -119,8 +126,31 @@ int main(int argc, char *argv[]) } // check if exists - if (checkExistSession()) - return 0; + if (existingSessionOperation(QString("Active"))) { + if (args[QString("state")].toInt() == 1) { + // replace session + cout << QCoreApplication::translate("NetctlHelper", "Replace existing session.") + .toUtf8().data() << endl; + existingSessionOperation(QString("Close")); + } else if (args[QString("state")].toInt() == 2) { + // restore session + cout << QCoreApplication::translate("NetctlHelper", "Restore existing session.") + .toUtf8().data() << endl; + return 0; + } + else if (geteuid() == 0) { + // replace if running as root + cout << QCoreApplication::translate("NetctlHelper", "Replace existing session.") + .toUtf8().data() << endl; + existingSessionOperation(QString("Close")); + } + else { + // restore if running as non-root + cout << QCoreApplication::translate("NetctlHelper", "Restore existing session.") + .toUtf8().data() << endl; + return 0; + } + } NetctlHelper w(0, args); return a.exec(); } diff --git a/sources/helper/src/messages.cpp b/sources/helper/src/messages.cpp index aa5427a..714f984 100644 --- a/sources/helper/src/messages.cpp +++ b/sources/helper/src/messages.cpp @@ -37,6 +37,7 @@ QMap getArgs() args[QString("config")] = QString(QDir::homePath() + QString("/.config/netctl-gui.conf")); args[QString("debug")] = false; args[QString("nodaemon")] = false; + args[QString("state")] = (int) 0; args[QString("help")] = false; args[QString("info")] = false; args[QString("version")] = false; @@ -59,6 +60,10 @@ QString helpMessage() .arg(QCoreApplication::translate("NetctlHelper", "print debug information")); helpMessage += QString(" --nodaemon - %1\n") .arg(QCoreApplication::translate("NetctlHelper", "do not start as daemon")); + helpMessage += QString(" --replace - %1\n") + .arg(QCoreApplication::translate("NetctlHelper", "force replace the existing session")); + helpMessage += QString(" --restore - %1\n") + .arg(QCoreApplication::translate("NetctlHelper", "force restore the existing session")); helpMessage += QString(" %1\n").arg(QCoreApplication::translate("NetctlHelper", "Show messages:")); helpMessage += QString(" -v, --version - %1\n") .arg(QCoreApplication::translate("NetctlHelper", "show version and exit")); diff --git a/sources/helper/zsh-completions b/sources/helper/zsh-completions index f39c696..5eea1ea 100644 --- a/sources/helper/zsh-completions +++ b/sources/helper/zsh-completions @@ -19,9 +19,11 @@ # variables _netctlgui_helper_arglist=( - {'--nodaemon','--nodaemon'}'[do not start as daemon]' {'(--config)-c','(-c)--config'}'[read configuration from this file]:select file:->files' {'(--debug)-d','(-d)--debug'}'[print debug information]' + {'--nodaemon','--nodaemon'}'[do not start as daemon]' + {'--replace','--replace'}'[force replace the existing session]' + {'--restore','--restore'}'[force restore the existing session]' {'(--version)-v','(-v)--version'}'[show version and exit]' {'(--info)-i','(-i)--info'}'[show build information and exit]' {'(--help)-h','(-h)--help'}'[show help and exit]'