add calls

create interface dbus interface
This commit is contained in:
arcan1s 2015-03-13 08:22:02 +03:00
parent 26afd90df9
commit ef2694d7a7
11 changed files with 311 additions and 118 deletions

View File

@ -8,7 +8,7 @@ ProjectRootRelative=./
[CMake][CMake Build Directory 0]
Build Directory Path=file:///home/arcanis/Documents/github/netctl-gui/build
Build Type=Release
Build Type=Debug
CMake Binary=file:///usr/bin/cmake
Environment Profile=
Extra Arguments=

View File

@ -24,160 +24,93 @@
#include "dbusoperation.h"
bool enableProfileSlot(const QString profile, Netctl *netctlCommand,
InterfaceAnswer enableProfileSlot(const QString profile, NetctlInterface *interface,
const bool useHelper, const bool debug)
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "Profile" << profile;
bool current;
if (useHelper) {
// enable
if (!useHelper) return interface->enableProfile(profile);
try {
QList<QVariant> args;
args.append(profile);
sendRequestToCtrlWithArgs(QString("Enable"), args, debug);
// check
QList<QVariant> responce = sendRequestToLibWithArgs(QString("isProfileEnabled"), args, debug);
if (responce.isEmpty())
current = netctlCommand->isProfileEnabled(profile);
else
current = responce[0].toBool();
} else {
// enable
netctlCommand->enableProfile(profile);
// check
current = netctlCommand->isProfileEnabled(profile);
int responce = sendRequestToInterfaceWithArgs(QString("Enable"), args, debug)[0].toInt();
return static_cast<InterfaceAnswer>(responce);
} catch (...) {
if (debug) qDebug() << PDEBUG << ":" << "An exception recevied";
return InterfaceAnswer::Error;
}
return current;
}
bool restartProfileSlot(const QString profile, Netctl *netctlCommand,
InterfaceAnswer restartProfileSlot(const QString profile, NetctlInterface *interface,
const bool useHelper, const bool debug)
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "Profile" << profile;
bool current;
if (useHelper) {
// restart
if (!useHelper) return interface->restartProfile(profile);
try {
QList<QVariant> args;
args.append(profile);
sendRequestToCtrlWithArgs(QString("Restart"), args, debug);
// check
QList<QVariant> responce = sendRequestToLibWithArgs(QString("isProfileActive"), args, debug);
if (responce.isEmpty())
current = netctlCommand->isProfileActive(profile);
else
current = responce[0].toBool();
} else {
// restart
netctlCommand->restartProfile(profile);
// check
current = netctlCommand->isProfileActive(profile);
int responce = sendRequestToInterfaceWithArgs(QString("Restart"), args, debug)[0].toInt();
return static_cast<InterfaceAnswer>(responce);
} catch (...) {
if (debug) qDebug() << PDEBUG << ":" << "An exception recevied";
return InterfaceAnswer::Error;
}
return current;
}
bool startProfileSlot(const QString profile, Netctl *netctlCommand,
InterfaceAnswer startProfileSlot(const QString profile, NetctlInterface *interface,
const bool useHelper, const bool debug)
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "Profile" << profile;
bool current;
if (useHelper) {
// get current
if (!useHelper) return interface->startProfile(profile);
try {
QList<QVariant> args;
args.append(profile);
QList<QVariant> responce = sendRequestToLib(QString("ActiveProfile"), debug);
QStringList currentProfile;
if (!responce.isEmpty()) currentProfile = responce[0].toString().split(QChar('|'));
// start or switch
if ((currentProfile.isEmpty()) || (currentProfile.contains(profile)))
sendRequestToCtrlWithArgs(QString("Start"), args, debug);
else
sendRequestToCtrlWithArgs(QString("SwitchTo"), args, debug);
// check
responce = sendRequestToLibWithArgs(QString("isProfileActive"), args, debug);
if (responce.isEmpty())
current = netctlCommand->isProfileActive(profile);
else
current = responce[0].toBool();
} else {
// get current
QStringList currentProfile = netctlCommand->getActiveProfile();
// start or switch
if ((currentProfile.isEmpty()) || (currentProfile.contains(profile)))
netctlCommand->startProfile(profile);
else
netctlCommand->switchToProfile(profile);
// check
current = netctlCommand->isProfileActive(profile);
int responce = sendRequestToInterfaceWithArgs(QString("Start"), args, debug)[0].toInt();
return static_cast<InterfaceAnswer>(responce);
} catch (...) {
if (debug) qDebug() << PDEBUG << ":" << "An exception recevied";
return InterfaceAnswer::Error;
}
return current;
}
bool MainWindow::stopAllProfilesSlot()
InterfaceAnswer stopAllProfilesSlot(NetctlInterface *interface, const bool useHelper,
const bool debug)
{
if (debug) qDebug() << PDEBUG;
if (useHelper)
sendRequestToCtrl(QString("StolAll"), debug);
else
netctlCommand->stopAllProfiles();
return true;
if (!useHelper) return interface->stopAllProfiles();
try {
int responce = sendRequestToInterface(QString("StopAll"), debug)[0].toInt();
return static_cast<InterfaceAnswer>(responce);
} catch (...) {
if (debug) qDebug() << PDEBUG << ":" << "An exception recevied";
return InterfaceAnswer::Error;
}
}
bool MainWindow::switchToProfileSlot(const QString profile)
InterfaceAnswer switchToProfileSlot(const QString profile, NetctlInterface *interface,
const bool useHelper, const bool debug)
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "Profile" << profile;
bool netctlAutoStatus = false;
if (useHelper) {
QList<QVariant> responce = sendRequestToLib(QString("isNetctlAutoActive"), debug);
if (!responce.isEmpty()) netctlAutoStatus = responce[0].toBool();
} else
netctlAutoStatus = netctlCommand->isNetctlAutoRunning();
bool current;
if (netctlAutoStatus) {
if (useHelper) {
if (!useHelper) return interface->switchToProfile(profile);
try {
QList<QVariant> args;
args.append(profile);
sendRequestToCtrlWithArgs(QString("autoStart"), args, debug);
QList<QVariant> responce = sendRequestToLibWithArgs(QString("autoIsProfileActive"), args, debug);
if (responce.isEmpty())
current = netctlCommand->autoIsProfileActive(profile);
else
current = responce[0].toBool();
} else {
netctlCommand->autoStartProfile(profile);
current = netctlCommand->autoIsProfileActive(profile);
}
} else {
if (useHelper) {
QList<QVariant> args;
args.append(profile);
sendRequestToCtrlWithArgs(QString("SwitchTo"), args, debug);
QList<QVariant> responce = sendRequestToLibWithArgs(QString("isProfileActive"), args, debug);
if (responce.isEmpty())
current = netctlCommand->isProfileActive(profile);
else
current = responce[0].toBool();
} else {
netctlCommand->switchToProfile(profile);
current = netctlCommand->isProfileActive(profile);
int responce = sendRequestToInterfaceWithArgs(QString("SwitchTo"), args, debug)[0].toInt();
return static_cast<InterfaceAnswer>(responce);
} catch (...) {
if (debug) qDebug() << PDEBUG << ":" << "An exception recevied";
return InterfaceAnswer::Error;
}
}
return current;
}

View File

@ -24,8 +24,16 @@
#include <netctlgui/netctlgui.h>
bool enableProfileSlot(const QString profile, Netctl *netctlCommand,
InterfaceAnswer enableProfileSlot(const QString profile, NetctlInterface *interface,
const bool useHelper, const bool debug = false);
InterfaceAnswer restartProfileSlot(const QString profile, NetctlInterface *interface,
const bool useHelper, const bool debug = false);
InterfaceAnswer startProfileSlot(const QString profile, NetctlInterface *interface,
const bool useHelper, const bool debug = false);
InterfaceAnswer stopAllProfilesSlot(NetctlInterface *interface, const bool useHelper,
const bool debug);
InterfaceAnswer switchToProfileSlot(const QString profile, NetctlInterface *interface,
const bool useHelper, const bool debug);
#endif /* CALLS_H */

View File

@ -114,6 +114,26 @@ QList<QVariant> sendRequestToCtrlWithArgs(const QString cmd, const QList<QVarian
}
QList< QVariant > sendRequestToInterface(const QString cmd, const bool debug)
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "cmd" << cmd;
return sendRequestToInterfaceWithArgs(cmd, QList<QVariant>(), debug);
}
QList<QVariant> sendRequestToInterfaceWithArgs(const QString cmd, const QList<QVariant> args,
const bool debug)
{
if (debug) qDebug() << PDEBUG;
if (debug) qDebug() << PDEBUG << ":" << "cmd" << cmd;
if (debug) qDebug() << PDEBUG << ":" << "args" << args;
return sendRequestToHelper(DBUS_INTERFACE_PATH, cmd, args, debug);
}
QList<QVariant> sendRequestToLib(const QString cmd, const bool debug)
{
if (debug) qDebug() << PDEBUG;

View File

@ -30,6 +30,9 @@ QList<QVariant> sendRequestToHelper(const QString path, const QString cmd,
QList<QVariant> sendRequestToCtrl(const QString cmd, const bool debug = false);
QList<QVariant> sendRequestToCtrlWithArgs(const QString cmd, const QList<QVariant> args,
const bool debug = false);
QList<QVariant> sendRequestToInterface(const QString cmd, const bool debug = false);
QList<QVariant> sendRequestToInterfaceWithArgs(const QString cmd, const QList<QVariant> args,
const bool debug = false);
QList<QVariant> sendRequestToLib(const QString cmd, const bool debug = false);
QList<QVariant> sendRequestToLibWithArgs(const QString cmd, const QList<QVariant> args,
const bool debug = false);

View File

@ -0,0 +1,122 @@
/***************************************************************************
* 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 "interfaceadaptor.h"
#include <QTextCodec>
#include <unistd.h>
#include "netctlhelper.h"
#include "version.h"
InterfaceAdaptor::InterfaceAdaptor(QObject *parent, const bool debugCmd, const QMap<QString, QString> configuration)
: QDBusAbstractAdaptor(parent),
debug(debugCmd)
{
netctlInterface = new NetctlInterface(debug, configuration);
}
InterfaceAdaptor::~InterfaceAdaptor()
{
if (netctlInterface != nullptr) delete netctlInterface;
}
int InterfaceAdaptor::autoEnable(const QString profile)
{
return netctlInterface->autoEnableProfile(profile);
}
int InterfaceAdaptor::Create(const QString profile, const QStringList settingsList)
{
QMap<QString, QString> settings;
for (int i=0; i<settingsList.count(); i++) {
if (!settingsList[i].contains(QString("=="))) continue;
QString key = settingsList[i].split(QString("=="))[0];
QString value = settingsList[i].split(QString("=="))[1];
settings[key] = value;
}
return netctlInterface->createProfile(profile, settings);
}
int InterfaceAdaptor::Enable(const QString profile)
{
return netctlInterface->enableProfile(profile);
}
int InterfaceAdaptor::Essid(const QString essid, QStringList settingsList)
{
QMap<QString, QString> settings;
for (int i=0; i<settingsList.count(); i++) {
if (!settingsList[i].contains(QString("=="))) continue;
QString key = settingsList[i].split(QString("=="))[0];
QString value = settingsList[i].split(QString("=="))[1];
settings[key] = value;
}
return netctlInterface->connectToEssid(essid, settings);
}
int InterfaceAdaptor::KnownEssid(const QString essid)
{
return netctlInterface->connectToKnownEssid(essid);
}
int InterfaceAdaptor::Restart(const QString profile)
{
return netctlInterface->restartProfile(profile);
}
int InterfaceAdaptor::Start(const QString profile)
{
return netctlInterface->startProfile(profile);
}
int InterfaceAdaptor::StopAll()
{
return netctlInterface->stopAllProfiles();
}
int InterfaceAdaptor::SwitchTo(const QString profile)
{
return netctlInterface->switchToProfile(profile);
}
int InterfaceAdaptor::UnknownEssid(const QString essid, QStringList settingsList)
{
QMap<QString, QString> settings;
for (int i=0; i<settingsList.count(); i++) {
if (!settingsList[i].contains(QString("=="))) continue;
QString key = settingsList[i].split(QString("=="))[0];
QString value = settingsList[i].split(QString("=="))[1];
settings[key] = value;
}
return netctlInterface->connectToUnknownEssid(essid, settings);
}

View File

@ -0,0 +1,56 @@
/***************************************************************************
* 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 INTERFACEADAPTOR_H
#define INTERFACEADAPTOR_H
#include <QDBusAbstractAdaptor>
#include <netctlgui/netctlgui.h>
class InterfaceAdaptor : public QDBusAbstractAdaptor
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.netctlgui.helper")
public:
explicit InterfaceAdaptor(QObject *parent = 0,
const bool debugCmd = false,
const QMap<QString, QString> configuration = QMap<QString, QString>());
~InterfaceAdaptor();
public slots:
// interface
int autoEnable(const QString profile);
int Create(const QString profile, const QStringList settingsList);
int Enable(const QString profile);
int Essid(const QString essid, QStringList settingsList);
int KnownEssid(const QString essid);
int Restart(const QString profile);
int Start(const QString profile);
int StopAll();
int SwitchTo(const QString profile);
int UnknownEssid(const QString essid, QStringList settingsList);
private:
bool debug;
NetctlInterface *netctlInterface = nullptr;
};
#endif /* INTERFACEADAPTOR_H */

View File

@ -28,6 +28,7 @@
#include <pdebug/pdebug.h>
#include "controladaptor.h"
#include "interfaceadaptor.h"
#include "netctladaptor.h"
#include "version.h"
@ -88,6 +89,13 @@ void NetctlHelper::createInterface()
if (debug) qDebug() << PDEBUG << ":" << bus.lastError().message();
return quitHelper();
}
if (!bus.registerObject(DBUS_INTERFACE_PATH,
new InterfaceAdaptor(this, debug, configuration),
QDBusConnection::ExportAllContents)) {
if (debug) qDebug() << PDEBUG << ":" << "Could not register interface object";
if (debug) qDebug() << PDEBUG << ":" << bus.lastError().message();
return quitHelper();
}
// session bus
if (!session) return;
QDBusConnection sessionBus = QDBusConnection::sessionBus();
@ -107,6 +115,13 @@ void NetctlHelper::createInterface()
if (debug) qDebug() << PDEBUG << ":" << "Could not register session control object";
if (debug) qDebug() << PDEBUG << ":" << sessionBus.lastError().message();
}
if (!sessionBus.registerObject(DBUS_INTERFACE_PATH,
new InterfaceAdaptor(this, debug, configuration),
QDBusConnection::ExportAllContents)) {
if (debug) qDebug() << PDEBUG << ":" << "Could not register session interface object";
if (debug) qDebug() << PDEBUG << ":" << bus.lastError().message();
return quitHelper();
}
}
@ -116,11 +131,13 @@ void NetctlHelper::deleteInterface()
QDBusConnection::systemBus().unregisterObject(DBUS_LIB_PATH);
QDBusConnection::systemBus().unregisterObject(DBUS_CTRL_PATH);
QDBusConnection::systemBus().unregisterObject(DBUS_INTERFACE_PATH);
QDBusConnection::systemBus().unregisterService(DBUS_HELPER_SERVICE);
// session bus
if (!session) return;
QDBusConnection::sessionBus().unregisterObject(DBUS_LIB_PATH);
QDBusConnection::sessionBus().unregisterObject(DBUS_CTRL_PATH);
QDBusConnection::sessionBus().unregisterObject(DBUS_INTERFACE_PATH);
QDBusConnection::sessionBus().unregisterService(DBUS_HELPER_SERVICE);
}
@ -176,6 +193,14 @@ QMap<QString, QString> NetctlHelper::getSettings(const QString file)
config[QString("PREFERED_IFACE")] = settings.value(QString("PREFERED_IFACE"), QString("")).toString();
settings.endGroup();
settings.beginGroup(QString("Toolbars"));
config[QString("MAIN_TOOLBAR")] = settings.value(QString("MAIN_TOOLBAR"), Qt::TopToolBarArea).toString();
config[QString("NETCTL_TOOLBAR")] = settings.value(QString("NETCTL_TOOLBAR"), Qt::TopToolBarArea).toString();
config[QString("NETCTLAUTO_TOOLBAR")] = settings.value(QString("NETCTLAUTO_TOOLBAR"), Qt::TopToolBarArea).toString();
config[QString("PROFILE_TOOLBAR")] = settings.value(QString("PROFILE_TOOLBAR"), Qt::TopToolBarArea).toString();
config[QString("WIFI_TOOLBAR")] = settings.value(QString("WIFI_TOOLBAR"), Qt::TopToolBarArea).toString();
settings.endGroup();
for (int i=0; i<config.keys().count(); i++)
if (debug) qDebug() << PDEBUG << ":" << QString("%1=%2").arg(config.keys()[i]).arg(config[config.keys()[i]]);

View File

@ -145,6 +145,13 @@ public:
* @return InterfaceAnswer::Error if an error occurs
*/
InterfaceAnswer startProfile(const QString profile);
/**
* @brief method which stops all profiles
* @remark netctl only
* @return InterfaceAnswer::True if there are no errors
* @return InterfaceAnswer::Error if an error occurs
*/
InterfaceAnswer stopAllProfiles();
/**
* @brief method which switchs to selected profile and returns its status
* @remark both netctl and netctl-auto

View File

@ -225,6 +225,24 @@ InterfaceAnswer NetctlInterface::startProfile(const QString profile)
}
/**
* @fn stopAllProfiles
*/
InterfaceAnswer NetctlInterface::stopAllProfiles()
{
if (debug) qDebug() << PDEBUG;
if (netctlCommand == nullptr) {
if (debug) qDebug() << PDEBUG << ":" << "Could not find library";
return InterfaceAnswer::Error;
}
if (netctlCommand->stopAllProfiles())
return InterfaceAnswer::True;
else
return InterfaceAnswer::Error;
}
/**
* @fn switchToProfile
*/

View File

@ -49,6 +49,7 @@
#define DBUS_HELPER_SERVICE "org.netctlgui.helper"
#define DBUS_HELPER_INTERFACE "org.netctlgui.helper"
#define DBUS_CTRL_PATH "/ctrl"
#define DBUS_INTERFACE_PATH "/interface"
#define DBUS_LIB_PATH "/netctl"
#endif /* VERSION_H */