diff --git a/libnymea-app-core/configuration/nymeaconfiguration.cpp b/libnymea-app-core/configuration/nymeaconfiguration.cpp index 5886b724..8fc68999 100644 --- a/libnymea-app-core/configuration/nymeaconfiguration.cpp +++ b/libnymea-app-core/configuration/nymeaconfiguration.cpp @@ -251,6 +251,8 @@ void NymeaConfiguration::getConfigurationsResponse(const QVariantMap ¶ms) void NymeaConfiguration::getAvailableLanguagesResponse(const QVariantMap ¶ms) { qDebug() << "available languages" << params; + m_availableLanguages = params.value("params").toMap().value("languages").toStringList(); + emit availableLanguagesChanged(); } void NymeaConfiguration::getTimezonesResponse(const QVariantMap ¶ms) diff --git a/libnymea-app-core/jsonrpc/jsonrpcclient.cpp b/libnymea-app-core/jsonrpc/jsonrpcclient.cpp index 059c455b..c77de29e 100644 --- a/libnymea-app-core/jsonrpc/jsonrpcclient.cpp +++ b/libnymea-app-core/jsonrpc/jsonrpcclient.cpp @@ -30,6 +30,7 @@ #include #include #include +#include JsonRpcClient::JsonRpcClient(NymeaConnection *connection, QObject *parent) : JsonHandler(parent), @@ -321,7 +322,11 @@ void JsonRpcClient::onInterfaceConnectedChanged(bool connected) request.insert("id", 0); qDebug() << "Connected. Starting JSONRPC Handshake"; request.insert("method", "JSONRPC.Hello"); - sendRequest(request); + QVariantMap params; + params.insert("locale", QLocale().name()); + request.insert("params", params); +// sendRequest(request); + sendCommand("JSONRPC.Hello", params, this, "helloReply"); } } @@ -348,9 +353,57 @@ void JsonRpcClient::dataReceived(const QByteArray &data) QVariantMap dataMap = jsonDoc.toVariant().toMap(); - // Check if this is the initial handshake - if (dataMap.value("id").toInt() == 0 && dataMap.contains("params") && !dataMap.contains("notification")) { - dataMap = dataMap.value("params").toMap(); + // check if this is a notification + if (dataMap.contains("notification")) { + QStringList notification = dataMap.value("notification").toString().split("."); + QString nameSpace = notification.first(); + JsonHandler *handler = m_notificationHandlers.value(nameSpace).first; + + if (!handler) { +// qWarning() << "JsonRpc: handler not implemented:" << nameSpace; + return; + } + +// qDebug() << "Incoming notification:" << jsonDoc.toJson(); + QMetaObject::invokeMethod(handler, m_notificationHandlers.value(nameSpace).second.toLatin1().data(), Q_ARG(QVariantMap, dataMap)); + return; + } + + // check if this is a reply to a request + int commandId = dataMap.value("id").toInt(); + JsonRpcReply *reply = m_replies.take(commandId); + if (reply) { + reply->deleteLater(); +// qDebug() << QString("JsonRpc: got response for %1.%2: %3").arg(reply->nameSpace(), reply->method(), QString::fromUtf8(jsonDoc.toJson(QJsonDocument::Indented))) << reply->callback() << reply->callback(); + + if (dataMap.value("status").toString() == "unauthorized") { + qWarning() << "Something's off with the token"; + m_authenticationRequired = true; + m_token.clear(); + QSettings settings; + settings.beginGroup("jsonTokens"); + settings.setValue(m_serverUuid, m_token); + settings.endGroup(); + emit authenticationRequiredChanged(); + } + + if (!reply->caller().isNull() && !reply->callback().isEmpty()) { + QMetaObject::invokeMethod(reply->caller(), reply->callback().toLatin1().data(), Q_ARG(QVariantMap, dataMap)); + } + + emit responseReceived(reply->commandId(), dataMap.value("params").toMap()); + return; + } +} + +void JsonRpcClient::helloReply(const QVariantMap ¶ms) +{ + if (params.value("status").toString() == "error") { + qWarning() << "Hello call failed. Trying again without locale"; + m_id = 0; + sendCommand("JSONRPC.Hello", QVariantMap(), this, "helloReply"); + } else { + QVariantMap dataMap = params.value("params").toMap(); m_initialSetupRequired = dataMap.value("initialSetupRequired").toBool(); m_authenticationRequired = dataMap.value("authenticationRequired").toBool(); m_pushButtonAuthAvailable = dataMap.value("pushButtonAuthAvailable").toBool(); @@ -402,48 +455,7 @@ void JsonRpcClient::dataReceived(const QByteArray &data) setNotificationsEnabled(true); getCloudConnectionStatus(); - } - // check if this is a notification - if (dataMap.contains("notification")) { - QStringList notification = dataMap.value("notification").toString().split("."); - QString nameSpace = notification.first(); - JsonHandler *handler = m_notificationHandlers.value(nameSpace).first; - - if (!handler) { -// qWarning() << "JsonRpc: handler not implemented:" << nameSpace; - return; - } - -// qDebug() << "Incoming notification:" << jsonDoc.toJson(); - QMetaObject::invokeMethod(handler, m_notificationHandlers.value(nameSpace).second.toLatin1().data(), Q_ARG(QVariantMap, dataMap)); - return; - } - - // check if this is a reply to a request - int commandId = dataMap.value("id").toInt(); - JsonRpcReply *reply = m_replies.take(commandId); - if (reply) { - reply->deleteLater(); -// qDebug() << QString("JsonRpc: got response for %1.%2: %3").arg(reply->nameSpace(), reply->method(), QString::fromUtf8(jsonDoc.toJson(QJsonDocument::Indented))) << reply->callback() << reply->callback(); - - if (dataMap.value("status").toString() == "unauthorized") { - qWarning() << "Something's off with the token"; - m_authenticationRequired = true; - m_token.clear(); - QSettings settings; - settings.beginGroup("jsonTokens"); - settings.setValue(m_serverUuid, m_token); - settings.endGroup(); - emit authenticationRequiredChanged(); - } - - if (!reply->caller().isNull() && !reply->callback().isEmpty()) { - QMetaObject::invokeMethod(reply->caller(), reply->callback().toLatin1().data(), Q_ARG(QVariantMap, dataMap)); - } - - emit responseReceived(reply->commandId(), dataMap.value("params").toMap()); - return; } } diff --git a/libnymea-app-core/jsonrpc/jsonrpcclient.h b/libnymea-app-core/jsonrpc/jsonrpcclient.h index 0a5a5ac5..0a7048a9 100644 --- a/libnymea-app-core/jsonrpc/jsonrpcclient.h +++ b/libnymea-app-core/jsonrpc/jsonrpcclient.h @@ -102,6 +102,8 @@ private slots: void onInterfaceConnectedChanged(bool connected); void dataReceived(const QByteArray &data); + void helloReply(const QVariantMap ¶ms); + private: int m_id; // < namespace, > diff --git a/nymea-app/ui/SettingsPage.qml b/nymea-app/ui/SettingsPage.qml index df0dbc7f..7374af9f 100644 --- a/nymea-app/ui/SettingsPage.qml +++ b/nymea-app/ui/SettingsPage.qml @@ -76,13 +76,33 @@ Page { Layout.leftMargin: app.margins Layout.rightMargin: app.margins spacing: app.margins + visible: !engine.jsonRpcClient.ensureServerVersion("1.14") + Label { Layout.fillWidth: true text: qsTr("Language") } ComboBox { + id: languageBox + Layout.fillWidth: true model: engine.nymeaConfiguration.availableLanguages currentIndex: model.indexOf(engine.nymeaConfiguration.language) + contentItem: Label { + leftPadding: app.margins / 2 + text: Qt.locale(languageBox.displayText).nativeLanguageName + " (" + Qt.locale(languageBox.displayText).nativeCountryName + ")" + elide: Text.ElideRight + verticalAlignment: Text.AlignVCenter + } + + delegate: ItemDelegate { + width: languageBox.width + contentItem: Label { + text: Qt.locale(modelData).nativeLanguageName + " (" + Qt.locale(modelData).nativeCountryName + ")" + elide: Text.ElideRight + verticalAlignment: Text.AlignVCenter + } + highlighted: languageBox.highlightedIndex === index + } onActivated: { engine.nymeaConfiguration.language = currentText; }