Files
Stacer/stacer-core/Tools/service_tool.cpp
Petr Tsymbarovich 76ca5ed692 Do not inherit from QObject with no need
And clean up of includings
2017-08-30 00:04:57 +03:00

116 lines
2.3 KiB
C++

#include "service_tool.h"
#include <QDebug>
Service::Service(QString name, bool status, bool active) :
name(name),
status(status),
active(active)
{
}
ServiceTool::ServiceTool()
{
}
QList<Service> ServiceTool::getServicesWithSystemctl()
{
QList<Service> services = {};
try {
QStringList args = { "list-unit-files", "-t", "service", "-a", "--state=enabled,disabled" };
QStringList lines = CommandUtil::exec("systemctl", args)
.split(QChar('\n'))
.filter(QRegExp("[^@].service"));
QRegExp sep("\\s+");
for (const QString &line : lines)
{
// e.g apache2.service [enabled|disabled]
QStringList s = line.trimmed().split(sep);
QString name = s.first().trimmed().replace(".service", "");
bool status = ! s.last().trimmed().compare("enabled");
bool active = serviceIsActive(s.first().trimmed());
Service service(name, status, active);
services << service;
}
} catch(QString &ex) {
qCritical() << ex;
}
return services;
}
bool ServiceTool::serviceIsActive(QString serviceName)
{
QStringList args = { "is-active", serviceName };
QString result("");
try {
result = CommandUtil::exec("systemctl", args);
} catch(QString &ex) {
qCritical() << ex;
}
return ! result.trimmed().compare("active");
}
bool ServiceTool::serviceIsEnabled(QString serviceName)
{
QStringList args = { "is-enabled", serviceName };
QString result("");
try {
result = CommandUtil::exec("systemctl", args);
} catch(QString &ex) {
qCritical() << ex;
}
return ! result.trimmed().compare("enabled");
}
bool ServiceTool::changeServiceStatus(QString sname, bool status)
{
try {
QStringList args = { (status ? "enable" : "disable") , sname };
CommandUtil::sudoExec("systemctl", args);
return true;
} catch(QString &ex) {
qCritical() << ex;
}
return false;
}
bool ServiceTool::changeServiceActive(QString sname, bool status)
{
try {
QStringList args = { (status ? "start" : "stop") , sname };
CommandUtil::sudoExec("systemctl", args);
return true;
} catch(QString &ex) {
qCritical() << ex;
}
return false;
}