mirror of
https://github.com/arcan1s/netctl-gui.git
synced 2025-12-14 14:53:42 +00:00
add helper proto
This commit is contained in:
152
sources/helper/src/netctlhelper.cpp
Normal file
152
sources/helper/src/netctlhelper.cpp
Normal file
@ -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 <QCoreApplication>
|
||||
#include <QDBusConnection>
|
||||
#include <QDebug>
|
||||
|
||||
#include <netctlgui/netctlgui.h>
|
||||
|
||||
#include "controladaptor.h"
|
||||
#include "netctladaptor.h"
|
||||
#include "netctlhelper.h"
|
||||
#include "version.h"
|
||||
|
||||
|
||||
NetctlHelper::NetctlHelper(QObject *parent, QMap<QString, QVariant> args)
|
||||
: QObject(parent),
|
||||
configPath(args[QString("config")].toString()),
|
||||
debug(args[QString("debug")].toBool())
|
||||
{
|
||||
updateConfiguration();
|
||||
}
|
||||
|
||||
|
||||
NetctlHelper::~NetctlHelper()
|
||||
{
|
||||
if (debug) qDebug() << "[NetctlHelper]" << "[~NetctlHelper]";
|
||||
|
||||
deleteInterface();
|
||||
}
|
||||
|
||||
|
||||
QMap<QString, QString> NetctlHelper::getDefault()
|
||||
{
|
||||
if (debug) qDebug() << "[NetctlHelper]" << "[getDefault]";
|
||||
|
||||
QMap<QString, QString> 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.keys().count(); i++)
|
||||
if (debug) qDebug() << "[NetctlHelper]" << "[getDefault]" << ":" <<
|
||||
settings.keys()[i] + QString("=") + settings[settings.keys()[i]];
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
|
||||
QMap<QString, QString> NetctlHelper::getSettings()
|
||||
{
|
||||
if (debug) qDebug() << "[NetctlHelper]" << "[getSettings]";
|
||||
|
||||
QMap<QString, QString> 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<settings.keys().count(); i++)
|
||||
if (debug) qDebug() << "[NetctlHelper]" << "[getSettings]" << ":" <<
|
||||
settings.keys()[i] + QString("=") + settings[settings.keys()[i]];
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
|
||||
void NetctlHelper::quitHelper()
|
||||
{
|
||||
if (debug) qDebug() << "[NetctlHelper]" << "[quitHelper]";
|
||||
|
||||
QCoreApplication::quit();
|
||||
}
|
||||
|
||||
|
||||
void NetctlHelper::createInterface()
|
||||
{
|
||||
if (debug) qDebug() << "[NetctlHelper]" << "[createInterface]";
|
||||
|
||||
QDBusConnection bus = QDBusConnection::sessionBus();
|
||||
if (!bus.registerService(QString(DBUS_HELPER_SERVICE)))
|
||||
if (debug) qDebug() << "[NetctlHelper]" << "[createInterface]" << ":" << "Could not register service";
|
||||
if (!bus.registerObject(QString(DBUS_LIB_PATH),
|
||||
new NetctlAdaptor(this, configuration),
|
||||
QDBusConnection::ExportAllContents))
|
||||
if (debug) qDebug() << "[NetctlHelper]" << "[createInterface]" << ":" << "Could not register library object";
|
||||
if (!bus.registerObject(QString(DBUS_CONTROL_PATH),
|
||||
new ControlAdaptor(this, configuration),
|
||||
QDBusConnection::ExportAllContents))
|
||||
if (debug) qDebug() << "[NetctlHelper]" << "[createInterface]" << ":" << "Could not register control object";
|
||||
}
|
||||
|
||||
|
||||
void NetctlHelper::deleteInterface()
|
||||
{
|
||||
if (debug) qDebug() << "[NetctlHelper]" << "[deleteInterface]";
|
||||
|
||||
QDBusConnection::sessionBus().unregisterObject(QString(DBUS_LIB_PATH));
|
||||
QDBusConnection::sessionBus().unregisterObject(QString(DBUS_CONTROL_PATH));
|
||||
QDBusConnection::sessionBus().unregisterService(QString(DBUS_HELPER_SERVICE));
|
||||
}
|
||||
|
||||
|
||||
void NetctlHelper::updateConfiguration()
|
||||
{
|
||||
if (debug) qDebug() << "[NetctlHelper]" << "[updateConfiguration]";
|
||||
|
||||
deleteInterface();
|
||||
|
||||
configuration = getSettings();
|
||||
|
||||
createInterface();
|
||||
}
|
||||
Reference in New Issue
Block a user