Add support for restarting nymea in the settings

This commit is contained in:
Michael Zanetti 2020-05-01 21:20:38 +02:00
parent 26cda609c2
commit 24dddd6c4c
3 changed files with 90 additions and 10 deletions

View File

@ -76,14 +76,19 @@ bool SystemController::updateManagementAvailable() const
return m_updateManagementAvailable;
}
void SystemController::reboot()
int SystemController::restart()
{
m_jsonRpcClient->sendCommand("System.Reboot");
return m_jsonRpcClient->sendCommand("System.Restart", this, "restartResponse");
}
void SystemController::shutdown()
int SystemController::reboot()
{
m_jsonRpcClient->sendCommand("System.Shutdown");
return m_jsonRpcClient->sendCommand("System.Reboot", this, "rebootResponse");
}
int SystemController::shutdown()
{
return m_jsonRpcClient->sendCommand("System.Shutdown", this, "shutdownResponse");
}
bool SystemController::updateManagementBusy() const
@ -293,6 +298,27 @@ void SystemController::setTimeResponse(const QVariantMap &params)
qDebug() << "set time response" << params;
}
void SystemController::restartResponse(const QVariantMap &params)
{
int id = params.value("id").toInt();
bool success = params.value("params").toMap().value("success").toBool();
emit restartReply(id, success);
}
void SystemController::rebootResponse(const QVariantMap &params)
{
int id = params.value("id").toInt();
bool success = params.value("params").toMap().value("success").toBool();
emit rebootReply(id, success);
}
void SystemController::shutdownResponse(const QVariantMap &params)
{
int id = params.value("id").toInt();
bool success = params.value("params").toMap().value("success").toBool();
emit shutdownReply(id, success);
}
void SystemController::notificationReceived(const QVariantMap &data)
{
QString notification = data.value("notification").toString();

View File

@ -64,8 +64,9 @@ public:
QString nameSpace() const override;
bool powerManagementAvailable() const;
Q_INVOKABLE void reboot();
Q_INVOKABLE void shutdown();
Q_INVOKABLE int restart();
Q_INVOKABLE int reboot();
Q_INVOKABLE int shutdown();
bool updateManagementAvailable() const;
bool updateManagementBusy() const;
@ -99,6 +100,10 @@ signals:
void automaticTimeAvailableChanged();
void automaticTimeChanged();
void restartReply(int id, bool success);
void rebootReply(int id, bool success);
void shutdownReply(int id, bool success);
private slots:
void getCapabilitiesResponse(const QVariantMap &data);
void getUpdateStatusResponse(const QVariantMap &data);
@ -108,6 +113,9 @@ private slots:
void enableRepositoryResponse(const QVariantMap &params);
void getServerTimeResponse(const QVariantMap &params);
void setTimeResponse(const QVariantMap &params);
void restartResponse(const QVariantMap &params);
void rebootResponse(const QVariantMap &params);
void shutdownResponse(const QVariantMap &params);
void notificationReceived(const QVariantMap &data);

View File

@ -38,7 +38,30 @@ import "../components"
SettingsPageBase {
id: root
title: qsTr("General settings")
busy: d.pendingCommand !== -1
QtObject {
id: d
property int pendingCommand: -1
}
Connections {
target: engine.systemController
onRestartReply: handleReply(id, success)
onRebootReply: handleReply(id, success)
onShutdownReply: handleReply(id, success)
function handleReply(id, success) {
if (id === d.pendingCommand) {
d.pendingCommand = -1
if (!success) {
var component = Qt.createComponent(Qt.resolvedUrl("../components/ErrorDialog.qml"))
var popup = component.createObject(root);
popup.open()
}
}
}
}
SettingsPageSectionHeader {
text: qsTr("General")
@ -200,7 +223,30 @@ SettingsPageBase {
Layout.fillWidth: true
Layout.leftMargin: app.margins
Layout.rightMargin: app.margins
text: qsTr("Reboot %1:core").arg(app.systemName)
text: qsTr("Restart %1:core").arg(app.systemName)
visible: engine.systemController.powerManagementAvailable && engine.jsonRpcClient.ensureServerVersion("5.1")
onClicked: {
var dialog = Qt.createComponent(Qt.resolvedUrl("../components/MeaDialog.qml"));
var text = qsTr("Are you sure you want to restart %1:core now?").arg(app.systemName)
var popup = dialog.createObject(app,
{
headerIcon: "../images/dialog-warning-symbolic.svg",
title: qsTr("Restart %1:core").arg(app.systemName),
text: text,
standardButtons: Dialog.Ok | Dialog.Cancel
});
popup.open();
popup.accepted.connect(function() {
d.pendingCommand = engine.systemController.restart()
})
}
}
Button {
Layout.fillWidth: true
Layout.leftMargin: app.margins
Layout.rightMargin: app.margins
text: qsTr("Reboot %1:core system").arg(app.systemName)
visible: engine.systemController.powerManagementAvailable
onClicked: {
var dialog = Qt.createComponent(Qt.resolvedUrl("../components/MeaDialog.qml"));
@ -214,7 +260,7 @@ SettingsPageBase {
});
popup.open();
popup.accepted.connect(function() {
engine.systemController.reboot()
d.pendingCommand = engine.systemController.reboot()
})
}
}
@ -222,7 +268,7 @@ SettingsPageBase {
Layout.fillWidth: true
Layout.leftMargin: app.margins
Layout.rightMargin: app.margins
text: qsTr("Shutdown %1:core").arg(app.systemName)
text: qsTr("Shutdown %1:core system").arg(app.systemName)
visible: engine.systemController.powerManagementAvailable
onClicked: {
var dialog = Qt.createComponent(Qt.resolvedUrl("../components/MeaDialog.qml"));
@ -236,7 +282,7 @@ SettingsPageBase {
});
popup.open();
popup.accepted.connect(function() {
engine.systemController.shutdown()
d.pendingCommand = engine.systemController.shutdown()
})
}
}