some improvements in helper and gui APIs and replacement and restoring

sessions
This commit is contained in:
arcan1s 2014-08-12 14:01:00 +04:00
parent fddd4703cc
commit b65f761520
11 changed files with 128 additions and 16 deletions

View File

@ -58,7 +58,7 @@ th.sub {
<!-- helper actions --> <!-- helper actions -->
<tr> <tr>
<td>bool Active()</td> <td>bool Active()</td>
<td><code>true</code> if helper is active</td> <td><code>true</code> if the helper is active</td>
<td>no</td> <td>no</td>
</tr> </tr>
<tr> <tr>
@ -91,9 +91,14 @@ th.sub {
<td>current helper settings <code>KEY==VALUE</code></td> <td>current helper settings <code>KEY==VALUE</code></td>
<td>no</td> <td>no</td>
</tr> </tr>
<tr>
<td>QStringList UIDs()</td>
<td>returns the helper process UIDs <code>[UID, EUID]</code></td>
<td>no</td>
</tr>
<tr> <tr>
<td>bool Update()</td> <td>bool Update()</td>
<td>calls update helper configuration. Returns <code>true</code></td> <td>calls update the helper configuration. Returns <code>true</code></td>
<td>no</td> <td>no</td>
</tr> </tr>
<!-- netctl actions --> <!-- netctl actions -->
@ -257,6 +262,11 @@ th.sub {
<th colspan="3" class="sub"><a href="#netctlgui" class="anchor" name="netctlgui"></a><code>/netctlgui</code> path</th> <th colspan="3" class="sub"><a href="#netctlgui" class="anchor" name="netctlgui"></a><code>/netctlgui</code> path</th>
</tr> </tr>
<!-- gui actions --> <!-- gui actions -->
<tr>
<td>bool Active()</td>
<td><code>true</code> if the application is active</td>
<td>no</td>
</tr>
<tr> <tr>
<td>void ApiDocs()</td> <td>void ApiDocs()</td>
<td>opens this API documentation</td> <td>opens this API documentation</td>
@ -269,7 +279,7 @@ th.sub {
</tr> </tr>
<tr> <tr>
<td>QString Information()</td> <td>QString Information()</td>
<td>the same as <code>org.netctlgui.helper /netctl Information</code></td> <td>returns general information in format <code>[ActiveProfile, ActiveProfileStatus]</code></td>
<td>no</td> <td>no</td>
</tr> </tr>
<tr> <tr>
@ -317,11 +327,15 @@ th.sub {
<td><code>true</code> and shows settings window</td> <td><code>true</code> and shows settings window</td>
<td>no</td> <td>no</td>
</tr> </tr>
<tr>
<td>QStringList UIDs()</td>
<td>returns the application process UIDs <code>[UID, EUID]</code></td>
<td>no</td>
</tr>
<tr> <tr>
<td>QStringList VerboseInformation()</td> <td>QStringList VerboseInformation()</td>
<td>returns information in format <code>[isNetctlAutoActive, ProfileList, ActiveProfile, isProfileEnabled/autoIsProfileEnabled]</code></td> <td>returns information in format <code>[isNetctlAutoActive, ProfileList, ActiveProfile, isProfileEnabled/autoIsProfileEnabled]</code></td>
<td>no</td> <td>no</td>
<td>no</td>
</tr> </tr>
</table> </table>

View File

@ -34,11 +34,11 @@
using namespace std; using namespace std;
bool restoreExistSession() bool existingSessionOperation(const QString operation)
{ {
QDBusConnection bus = QDBusConnection::sessionBus(); QDBusConnection bus = QDBusConnection::sessionBus();
QDBusMessage request = QDBusMessage::createMethodCall(DBUS_SERVICE, DBUS_OBJECT_PATH, QDBusMessage request = QDBusMessage::createMethodCall(DBUS_SERVICE, DBUS_OBJECT_PATH,
DBUS_INTERFACE, QString("Restore")); DBUS_INTERFACE, operation);
QDBusMessage response = bus.call(request); QDBusMessage response = bus.call(request);
QList<QVariant> arguments = response.arguments(); QList<QVariant> 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<QVariant> arguments = response.arguments();
return arguments[0].toStringList()[type].toUInt();
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QMap<QString, QVariant> args = getArgs(); QMap<QString, QVariant> args = getArgs();
@ -164,8 +176,20 @@ int main(int argc, char *argv[])
} }
// check if exists // check if exists
if (restoreExistSession()) if (existingSessionOperation(QString("Active"))) {
return 0; 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); MainWindow w(0, args, &qtTranslator, &translator);
return a.exec(); return a.exec();
} }

View File

@ -18,6 +18,7 @@
#include "netctlguiadaptor.h" #include "netctlguiadaptor.h"
#include <QTextCodec> #include <QTextCodec>
#include <unistd.h>
#include "mainwindow.h" #include "mainwindow.h"
@ -34,6 +35,12 @@ NetctlGuiAdaptor::~NetctlGuiAdaptor()
} }
bool NetctlGuiAdaptor::Active()
{
return true;
}
void NetctlGuiAdaptor::ApiDocs() void NetctlGuiAdaptor::ApiDocs()
{ {
return mainWindow->showApi(); 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() QStringList NetctlGuiAdaptor::VerboseInformation()
{ {
return mainWindow->printTrayInformation(); return mainWindow->printTrayInformation();

View File

@ -34,6 +34,7 @@ public:
~NetctlGuiAdaptor(); ~NetctlGuiAdaptor();
public slots: public slots:
bool Active();
void ApiDocs(); void ApiDocs();
bool Close(); bool Close();
QStringList Information(); QStringList Information();
@ -46,6 +47,7 @@ public slots:
bool ShowMain(); bool ShowMain();
bool ShowNetctlAuto(); bool ShowNetctlAuto();
bool ShowSettings(); bool ShowSettings();
QStringList UIDs();
QStringList VerboseInformation(); QStringList VerboseInformation();
private: private:

View File

@ -18,11 +18,13 @@
# variables # variables
_netctlgui_helper_arglist=( _netctlgui_helper_arglist=(
'--nodaemon'
'-c' '-c'
'--config' '--config'
'-d' '-d'
'--debug' '--debug'
'--nodaemon'
'--replace'
'--restore'
'-v' '-v'
'--version' '--version'
'-i' '-i'

View File

@ -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 without any additional permissions. To have access to DBus interface user should have group
.B network .B network
.SH OPTIONS .SH OPTIONS
.IP "--nodaemon"
do not run as daemon
.IP "-c, --config FILE" .IP "-c, --config FILE"
read configuration from file read configuration from file
.I FILE .I FILE
.IP "-d, --debug" .IP "-d, --debug"
print debug information 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" .IP "-v, --version"
show version and exit show version and exit
.IP "-i, --info" .IP "-i, --info"

View File

@ -18,6 +18,7 @@
#include "controladaptor.h" #include "controladaptor.h"
#include <QTextCodec> #include <QTextCodec>
#include <unistd.h>
#include "netctlhelper.h" #include "netctlhelper.h"
#include "version.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() bool ControlAdaptor::Update()
{ {
helper->updateConfiguration(); helper->updateConfiguration();

View File

@ -44,6 +44,7 @@ public slots:
QString Pony(); QString Pony();
QString SecurityDocs(); QString SecurityDocs();
QStringList Settings(); QStringList Settings();
QStringList UIDs();
bool Update(); bool Update();
// netctlCommand // netctlCommand
bool autoDisableAll(); bool autoDisableAll();

View File

@ -34,13 +34,14 @@
using namespace std; using namespace std;
bool checkExistSession() bool existingSessionOperation(const QString operation)
{ {
QDBusConnection bus = QDBusConnection::systemBus(); QDBusConnection bus = QDBusConnection::systemBus();
QDBusMessage request = QDBusMessage::createMethodCall(DBUS_HELPER_SERVICE, DBUS_CTRL_PATH, QDBusMessage request = QDBusMessage::createMethodCall(DBUS_HELPER_SERVICE, DBUS_CTRL_PATH,
DBUS_HELPER_INTERFACE, QString("Active")); DBUS_HELPER_INTERFACE, operation);
QDBusMessage response = bus.call(request); QDBusMessage response = bus.call(request);
QList<QVariant> arguments = response.arguments(); QList<QVariant> arguments = response.arguments();
return !arguments.isEmpty(); return !arguments.isEmpty();
} }
@ -60,6 +61,12 @@ int main(int argc, char *argv[])
} else if (QString(argv[i]) == QString("--nodaemon")) { } else if (QString(argv[i]) == QString("--nodaemon")) {
// daemonized // daemonized
args[QString("nodaemon")] = true; 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"))) { } else if ((QString(argv[i]) == QString("-h")) || (QString(argv[i]) == QString("--help"))) {
// help message // help message
args[QString("help")] = true; args[QString("help")] = true;
@ -119,8 +126,31 @@ int main(int argc, char *argv[])
} }
// check if exists // check if exists
if (checkExistSession()) if (existingSessionOperation(QString("Active"))) {
return 0; 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); NetctlHelper w(0, args);
return a.exec(); return a.exec();
} }

View File

@ -37,6 +37,7 @@ QMap<QString, QVariant> getArgs()
args[QString("config")] = QString(QDir::homePath() + QString("/.config/netctl-gui.conf")); args[QString("config")] = QString(QDir::homePath() + QString("/.config/netctl-gui.conf"));
args[QString("debug")] = false; args[QString("debug")] = false;
args[QString("nodaemon")] = false; args[QString("nodaemon")] = false;
args[QString("state")] = (int) 0;
args[QString("help")] = false; args[QString("help")] = false;
args[QString("info")] = false; args[QString("info")] = false;
args[QString("version")] = false; args[QString("version")] = false;
@ -59,6 +60,10 @@ QString helpMessage()
.arg(QCoreApplication::translate("NetctlHelper", "print debug information")); .arg(QCoreApplication::translate("NetctlHelper", "print debug information"));
helpMessage += QString(" --nodaemon - %1\n") helpMessage += QString(" --nodaemon - %1\n")
.arg(QCoreApplication::translate("NetctlHelper", "do not start as daemon")); .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(" %1\n").arg(QCoreApplication::translate("NetctlHelper", "Show messages:"));
helpMessage += QString(" -v, --version - %1\n") helpMessage += QString(" -v, --version - %1\n")
.arg(QCoreApplication::translate("NetctlHelper", "show version and exit")); .arg(QCoreApplication::translate("NetctlHelper", "show version and exit"));

View File

@ -19,9 +19,11 @@
# variables # variables
_netctlgui_helper_arglist=( _netctlgui_helper_arglist=(
{'--nodaemon','--nodaemon'}'[do not start as daemon]'
{'(--config)-c','(-c)--config'}'[read configuration from this file]:select file:->files' {'(--config)-c','(-c)--config'}'[read configuration from this file]:select file:->files'
{'(--debug)-d','(-d)--debug'}'[print debug information]' {'(--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]' {'(--version)-v','(-v)--version'}'[show version and exit]'
{'(--info)-i','(-i)--info'}'[show build information and exit]' {'(--info)-i','(-i)--info'}'[show build information and exit]'
{'(--help)-h','(-h)--help'}'[show help and exit]' {'(--help)-h','(-h)--help'}'[show help and exit]'