File: servicecontrol.cpp

package info (click to toggle)
lomiri-cloudsync-app 0.8.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,560 kB
  • sloc: cpp: 1,053; python: 67; javascript: 25; makefile: 21; sh: 9
file content (187 lines) | stat: -rw-r--r-- 5,086 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "servicecontrol.h"

#include <QFile>
#include <QDir>
#include <QDebug>
#include <QCoreApplication>
#include <QProcess>

ServiceControl::ServiceControl(QObject *parent) : QObject(parent)
{

}

QString ServiceControl::serviceName() const
{
    return m_serviceName;
}

void ServiceControl::setServiceName(const QString &serviceName)
{
    if (m_serviceName != serviceName) {
        m_serviceName = serviceName;
        emit serviceNameChanged();
    }
}

bool ServiceControl::serviceFileInstalled() const
{
    if (m_serviceName.isEmpty()) {
        qDebug() << "Service name not set.";
        return false;
    }
    QFile f(QDir::homePath() + "/.config/systemd/user/" + m_serviceName + ".service");
    
    return (f.exists());
}

bool ServiceControl::installServiceFile()
{
    if (m_serviceName.isEmpty()) {
        qDebug() << "Service name not set. Cannot generate service file.";
        return false;
    }

    QDir dir(QDir::homePath() + "/.config/systemd/user/");
    if (!dir.exists()) {
        dir.mkpath(".");
    }
    
    QFile f(QDir::homePath() + "/.config/systemd/user/" + m_serviceName + ".service");
    
    if (f.exists()) {
        qDebug() << "Service file already exist...";
    }

    if (!f.open(QFile::WriteOnly | QFile::Truncate)) {
        qDebug() << "Cannot create service file";
        return false;
    }

    f.write("[Unit]\n");
    f.write("Description=Lomiri Cloudsync App Owncloud/Nextcloud client\n");
    f.write("After=network.target\n");
    f.write("\n");
    f.write("[Service]\n");
    f.write("Type=simple\n");
    f.write("WorkingDirectory=%h/.config/ubsync\n");
#if defined(CLICK_LIB_PATH) && defined(CLICK_BIN_PATH)
    f.write("Environment=\"LD_LIBRARY_PATH=" CLICK_LIB_PATH "\"\n");
    f.write("ExecStart=" CLICK_BIN_PATH "" + m_serviceName.toUtf8() + "\n");
#else
    f.write("ExecStart=/usr/bin/" + m_serviceName.toUtf8() + "\n");
#endif
    f.write("\n");
    f.write("[Install]\n");
    f.write("WantedBy=default.target\n");

    f.close();
    
    int ret = QProcess::execute("systemctl", {"--user", "daemon-reload"});
    if (ret != 0) {
        return false;
    }
    ret = QProcess::execute("systemctl", {"--user", "enable", m_serviceName.toUtf8() + ".service"});
    if (ret != 0) {
        return false;
    }
    
    return true;
}

bool ServiceControl::removeServiceFile()
{
    if (m_serviceName.isEmpty()) {
        qDebug() << "Service name not set.";
        return false;
    }
    QFile f(QDir::homePath() + "/.config/systemd/user/" + m_serviceName + ".service");
    
    return (f.remove());
}

bool ServiceControl::serviceRunning() const
{

    QProcess p;
    p.start("systemctl", {"--user", "status", m_serviceName.toUtf8() + ".service"});
    p.waitForFinished();
    QByteArray output = p.readAllStandardOutput();
    //qDebug() << "Reading service state:" << output;
    return output.contains("running");
}

bool ServiceControl::setServiceRunning(bool running)
{
    qDebug() << "ServiceControl::setServiceRunning:" << running;
    if (running && !serviceRunning()) {
        return startService();
    } else if (!running && serviceRunning()) {
        return stopService();
    }
    return true; // Requested state is already the current state.
}

bool ServiceControl::isServiceEnabled() const
{
    QProcess p;
    p.start("systemctl", {"--user", "status", m_serviceName.toUtf8() + ".service"});
    p.waitForFinished();
    QByteArray output = p.readAllStandardOutput();
    return (!output.contains("disabled;"));
}

bool ServiceControl::enableService()
{
    qDebug() << "should enable service";

    int ret = QProcess::execute("systemctl", {"--user", "enable", m_serviceName.toUtf8() + ".service"});
    emit serviceEnableChanged();
    return (ret == 0);
}

bool ServiceControl::setServiceEnable(bool enable)
{
    qDebug() << "ServiceControl::setServiceEnable:" << enable;
    if (enable && !isServiceEnabled()) {
        return enableService();
    } else if (!enable && isServiceEnabled()) {
        return disableService();
    }
    return true; // Requested state is already the current state.
}

bool ServiceControl::disableService()
{
    qDebug() << "should disable service";

    int ret = QProcess::execute("systemctl", {"--user", "disable", m_serviceName.toUtf8() + ".service"});
    emit serviceEnableChanged();
    return (ret == 0);
}


bool ServiceControl::startService()
{
    qDebug() << "should start service";

    int ret = QProcess::execute("systemctl", {"--user", "start", m_serviceName.toUtf8() + ".service"});
    emit serviceRunningChanged();
    return (ret == 0);
}

bool ServiceControl::stopService()
{
    qDebug() << "should stop service";
    int ret = QProcess::execute("systemctl", {"--user", "stop", m_serviceName.toUtf8() + ".service"});
    emit serviceRunningChanged();
    return (ret == 0);
}

bool ServiceControl::restartService()
{
    qDebug() << "should restart service";
    int ret = QProcess::execute("systemctl", {"--user", "restart", m_serviceName.toUtf8() + ".service"});
    emit serviceRunningChanged();
    return ret == 0;
}