-
Notifications
You must be signed in to change notification settings - Fork 2
/
systemcontrollersystemd.cpp
173 lines (153 loc) · 7.41 KB
/
systemcontrollersystemd.cpp
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2020, nymea GmbH
* Contact: contact@nymea.io
*
* This file is part of nymea.
* This project including source code and documentation is protected by
* copyright law, and remains the property of nymea GmbH. All rights, including
* reproduction, publication, editing and translation, are reserved. The use of
* this project is subject to the terms of a license agreement to be concluded
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
* under https://nymea.io/license
*
* GNU Lesser General Public License Usage
* Alternatively, this project may be redistributed and/or modified under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; version 3. This project 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this project. If not, see <https://www.gnu.org/licenses/>.
*
* For any further details and any questions please contact us under
* contact@nymea.io or see our FAQ/Licensing Information on
* https://nymea.io/license/faq
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <QDBusInterface>
#include <QDBusPendingReply>
#include <QTimeZone>
#include <QTimer>
#include <QProcess>
#include "systemcontrollersystemd.h"
#include "loggingcategories.h"
SystemControllerSystemd::SystemControllerSystemd(QObject *parent):
PlatformSystemController(parent)
{
QDBusInterface logind("org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", QDBusConnection::systemBus());
QDBusPendingReply<QString> canPowerOff = logind.callWithArgumentList(QDBus::Block, "CanPowerOff", {});
canPowerOff.waitForFinished();
if (canPowerOff.isError()) {
qCWarning(dcPlatform) << "DBus call to logind failed:" << canPowerOff.error().name() << canPowerOff.error().message();
} else if (canPowerOff.value() == "yes") {
m_canControlPower = true;
}
m_canControlTime = QDBusConnection::systemBus().connect("org.freedesktop.timedate1", "/org/freedesktop/timedate1", "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(timePropertiesChanged(const QString &, const QVariantMap &, const QStringList &)));
}
bool SystemControllerSystemd::powerManagementAvailable() const
{
return m_canControlPower;
}
bool SystemControllerSystemd::restart()
{
QDBusInterface systemd("org.freedesktop.systemd1", "/org/freedesktop/systemd1/unit/nymead_2eservice", "org.freedesktop.systemd1.Unit", QDBusConnection::systemBus());
QDBusPendingReply<> restart = systemd.callWithArgumentList(QDBus::Block, "Restart", {"replace"});
restart.waitForFinished();
if (restart.isError()) {
const auto error = restart.error();
qCWarning(dcPlatform()) << "Error restarting nymea:" << error.message();
}
qCDebug(dcPlatform()) << "Restarting nymea...";
return true;
}
bool SystemControllerSystemd::reboot()
{
QDBusInterface logind("org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", QDBusConnection::systemBus());
QDBusPendingReply<> powerOff = logind.callWithArgumentList(QDBus::Block, "Reboot", {true, });
powerOff.waitForFinished();
if (powerOff.isError()) {
const auto error = powerOff.error();
qCWarning(dcPlatform) << "Error calling reboot on logind:" << error.message();
return false;
}
qCDebug(dcPlatform) << "Rebooting...";
return true;
}
bool SystemControllerSystemd::shutdown()
{
QDBusInterface logind("org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", QDBusConnection::systemBus());
QDBusPendingReply<> powerOff = logind.callWithArgumentList(QDBus::Block, "PowerOff", {true, });
powerOff.waitForFinished();
if (powerOff.isError()) {
const auto error = powerOff.error();
qCWarning(dcPlatform) << "Error calling poweroff on logind:" << error.message();
return false;
}
qCDebug(dcPlatform()) << "Shutting down...";
return true;
}
bool SystemControllerSystemd::timeManagementAvailable() const
{
return m_canControlTime;
}
bool SystemControllerSystemd::setTime(const QDateTime &dateTime)
{
QDBusInterface timedated("org.freedesktop.timedate1", "/org/freedesktop/timedate1", "org.freedesktop.timedate1", QDBusConnection::systemBus());
QDBusPendingReply<> setTimeZone = timedated.callWithArgumentList(QDBus::Block, "SetTime", {dateTime.toMSecsSinceEpoch() * 1000, false, false});
if (setTimeZone.isError()) {
qCWarning(dcPlatform()) << "Error setting time zone:" << setTimeZone.error().name() << setTimeZone.error().message();
return false;
}
qCDebug(dcPlatform()) << "System time changed:" << dateTime;
emit timeConfigurationChanged();
return true;
}
bool SystemControllerSystemd::setTimeZone(const QTimeZone &timeZone)
{
QDBusInterface timedated("org.freedesktop.timedate1", "/org/freedesktop/timedate1", "org.freedesktop.timedate1", QDBusConnection::systemBus());
QDBusPendingReply<> setTimeZone = timedated.callWithArgumentList(QDBus::Block, "SetTimezone", {QString(timeZone.id()), false});
if (setTimeZone.isError()) {
qCWarning(dcPlatform()) << "Error setting time zone:" << setTimeZone.error().name() << setTimeZone.error().message();
return false;
}
qCDebug(dcPlatform()) << "Time zone set to" << timeZone;
emit timeConfigurationChanged();
return true;
}
bool SystemControllerSystemd::automaticTimeAvailable() const
{
QDBusInterface timedated("org.freedesktop.timedate1", "/org/freedesktop/timedate1", "org.freedesktop.timedate1", QDBusConnection::systemBus());
return timedated.property("CanNTP").toBool();
}
bool SystemControllerSystemd::automaticTime() const
{
QDBusInterface timedated("org.freedesktop.timedate1", "/org/freedesktop/timedate1", "org.freedesktop.timedate1", QDBusConnection::systemBus());
return timedated.property("NTP").toBool();
}
bool SystemControllerSystemd::setAutomaticTime(bool automaticTime)
{
QDBusInterface timedated("org.freedesktop.timedate1", "/org/freedesktop/timedate1", "org.freedesktop.timedate1", QDBusConnection::systemBus());
QDBusPendingReply<> setNtp = timedated.callWithArgumentList(QDBus::Block, "SetNTP", {automaticTime, false});
if (setNtp.isError()) {
qCWarning(dcPlatform()) << "Error disabling NTP:" << setNtp.error().name() << setNtp.error().message();
return false;
}
qCDebug(dcPlatform()) << "Automatic time updates" << (automaticTime ? "enabled" : "disabled");
if (automaticTime) {
// If enabling NTP, wait 2 secs before emitting changed again, NTP should have processed by then...
// If not, it probably failed anyways
QTimer::singleShot(2000, this, [this](){
emit timeConfigurationChanged();
});
}
emit timeConfigurationChanged();
return true;
}
void SystemControllerSystemd::timePropertiesChanged(const QString &interface, const QVariantMap &changedProperties, const QStringList &invalidatedProperties)
{
qCDebug(dcPlatform()) << "Time configuration changed" << interface << changedProperties << invalidatedProperties;
emit timeConfigurationChanged();
}