From d00540e8649cc6ea5c5862d3a0f1603b5b9d6eb4 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Fri, 16 Dec 2022 15:50:09 +0100 Subject: [PATCH] Improve managing of manual connections --- .../connection/discovery/nymeadiscovery.cpp | 2 + .../connection/discovery/upnpdiscovery.cpp | 3 +- .../discovery/zeroconfdiscovery.cpp | 3 +- libnymea-app/connection/nymeaconnection.cpp | 2 +- libnymea-app/connection/nymeahost.cpp | 23 +++- libnymea-app/connection/nymeahost.h | 5 +- libnymea-app/connection/nymeahosts.cpp | 4 +- libnymea-app/jsonrpc/jsonrpcclient.cpp | 31 ++++-- libnymea-app/jsonrpc/jsonrpcclient.h | 2 +- nymea-app/resources.qrc | 1 + nymea-app/ui/MainMenu.qml | 28 ++++- nymea-app/ui/RootItem.qml | 17 ++- .../ui/components/ConnectionInfoDialog.qml | 73 ++++++++++-- nymea-app/ui/connection/ConnectionWizard.qml | 101 ++--------------- .../ui/connection/ManualConnectionEntry.qml | 105 ++++++++++++++++++ 15 files changed, 266 insertions(+), 134 deletions(-) create mode 100644 nymea-app/ui/connection/ManualConnectionEntry.qml diff --git a/libnymea-app/connection/discovery/nymeadiscovery.cpp b/libnymea-app/connection/discovery/nymeadiscovery.cpp index 0c31070d..d578a788 100644 --- a/libnymea-app/connection/discovery/nymeadiscovery.cpp +++ b/libnymea-app/connection/discovery/nymeadiscovery.cpp @@ -143,6 +143,7 @@ void NymeaDiscovery::cacheHost(NymeaHost *host) settings.setValue("bearerType", connection->bearerType()); settings.setValue("secure", connection->secure()); settings.setValue("displayName", connection->displayName()); + settings.setValue("manual", connection->manual()); settings.endGroup(); } settings.endGroup(); @@ -221,6 +222,7 @@ void NymeaDiscovery::loadFromDisk() bool secure = settings.value("secure").toBool(); QString displayName = settings.value("displayName").toString(); connection = new Connection(url, bearerType, secure, displayName, host); + connection->setManual(settings.value("manual").toBool()); host->connections()->addConnection(connection); qCDebug(dcDiscovery()) << "|- Connection:" << group << connection->url() << connection->bearerType() << "secure:" << connection->secure(); } diff --git a/libnymea-app/connection/discovery/upnpdiscovery.cpp b/libnymea-app/connection/discovery/upnpdiscovery.cpp index 936653a8..b4f1fa74 100644 --- a/libnymea-app/connection/discovery/upnpdiscovery.cpp +++ b/libnymea-app/connection/discovery/upnpdiscovery.cpp @@ -292,9 +292,8 @@ void UpnpDiscovery::networkReplyFinished(QNetworkReply *reply) Connection *connection = device->connections()->find(url); if (!connection) { bool sslEnabled = url.scheme() == "nymeas" || url.scheme() == "wss"; - QString displayName = QString("%1:%2").arg(url.host()).arg(url.port()); Connection::BearerType bearerType = QHostAddress(url.host()).isLoopback() ? Connection::BearerTypeLoopback : Connection::BearerTypeLan; - connection = new Connection(url, bearerType, sslEnabled, displayName); + connection = new Connection(url, bearerType, sslEnabled, "UPnP"); connection->setOnline(true); qCInfo(dcUPnP()) << "Adding new connection to host:" << device->name() << url << bearerType; device->connections()->addConnection(connection); diff --git a/libnymea-app/connection/discovery/zeroconfdiscovery.cpp b/libnymea-app/connection/discovery/zeroconfdiscovery.cpp index 9f0ca5c7..21aaba19 100644 --- a/libnymea-app/connection/discovery/zeroconfdiscovery.cpp +++ b/libnymea-app/connection/discovery/zeroconfdiscovery.cpp @@ -157,9 +157,8 @@ void ZeroconfDiscovery::serviceEntryAdded(const QZeroConfService &entry) Connection *connection = host->connections()->find(url); if (!connection) { qCInfo(dcZeroConf()) << "Adding new connection to host:" << host->name() << url.toString(); - QString displayName = QString("%1:%2").arg(url.host()).arg(url.port()); Connection::BearerType bearerType = QHostAddress(url.host()).isLoopback() ? Connection::BearerTypeLoopback : Connection::BearerTypeLan; - connection = new Connection(url, bearerType, sslEnabled, displayName); + connection = new Connection(url, bearerType, sslEnabled, "mDNS"); connection->setOnline(true); host->connections()->addConnection(connection); } else { diff --git a/libnymea-app/connection/nymeaconnection.cpp b/libnymea-app/connection/nymeaconnection.cpp index fe7d327f..baf71268 100644 --- a/libnymea-app/connection/nymeaconnection.cpp +++ b/libnymea-app/connection/nymeaconnection.cpp @@ -128,7 +128,7 @@ void NymeaConnection::setCurrentHost(NymeaHost *host) return; } - if (m_preferredConnection) { + if (host && m_preferredConnection) { if (host->connections()->find(m_preferredConnection->url())) { qCInfo(dcNymeaConnection()) << "Setting preferred connection to" << m_preferredConnection->url(); // Unset the preferred connection when it is removed diff --git a/libnymea-app/connection/nymeahost.cpp b/libnymea-app/connection/nymeahost.cpp index 50cbc95e..d80eb89a 100644 --- a/libnymea-app/connection/nymeahost.cpp +++ b/libnymea-app/connection/nymeahost.cpp @@ -232,9 +232,10 @@ Connection *Connections::bestMatch(Connection::BearerTypes bearerTypes) const return best; } -void Connections::addConnection(const QUrl &url, Connection::BearerType bearerType, bool secure, const QString &displayName) +void Connections::addConnection(const QUrl &url, Connection::BearerType bearerType, bool secure, const QString &displayName, bool manual) { Connection *connection = new Connection(url, bearerType, secure, displayName); + connection->setManual(manual); addConnection(connection); } @@ -249,12 +250,12 @@ QHash Connections::roleNames() const return roles; } -Connection::Connection(const QUrl &url, Connection::BearerType bearerType, bool secure, const QString &displayName, QObject *parent): +Connection::Connection(const QUrl &url, Connection::BearerType bearerType, bool secure, const QString ¬e, QObject *parent): QObject(parent), m_url(url), m_bearerType(bearerType), m_secure(secure), - m_displayName(displayName) + m_displayName(note) { qRegisterMetaType("Connection.BearerType"); } @@ -296,7 +297,7 @@ QString Connection::displayName() const bool Connection::online() const { - return m_online; + return m_manual || m_online; } void Connection::setOnline(bool online) @@ -309,6 +310,20 @@ void Connection::setOnline(bool online) } } +bool Connection::manual() const +{ + return m_manual; +} + +void Connection::setManual(bool manual) +{ + if (m_manual != manual) { + m_manual = manual; + emit onlineChanged(); + emit priorityChanged(); + } +} + int Connection::priority() const { int prio = 0; diff --git a/libnymea-app/connection/nymeahost.h b/libnymea-app/connection/nymeahost.h index c07bc772..e8a54d52 100644 --- a/libnymea-app/connection/nymeahost.h +++ b/libnymea-app/connection/nymeahost.h @@ -76,6 +76,8 @@ public: QString displayName() const; bool online() const; void setOnline(bool online); + bool manual() const; + void setManual(bool manual); int priority() const; signals: @@ -88,6 +90,7 @@ private: bool m_secure = false; QString m_displayName; bool m_online = false; + bool m_manual = false; QDateTime m_lastSeen; }; @@ -115,7 +118,7 @@ public: Q_INVOKABLE Connection* bestMatch(Connection::BearerTypes bearerTypes = Connection::BearerTypeAll) const; void addConnection(Connection *connection); - Q_INVOKABLE void addConnection(const QUrl &url, Connection::BearerType bearerType, bool secure, const QString &displayName); + Q_INVOKABLE void addConnection(const QUrl &url, Connection::BearerType bearerType, bool secure, const QString &displayName, bool manual = false); Q_INVOKABLE void removeConnection(Connection *connection); Q_INVOKABLE void removeConnection(int index); diff --git a/libnymea-app/connection/nymeahosts.cpp b/libnymea-app/connection/nymeahosts.cpp index 69a25a47..8789572c 100644 --- a/libnymea-app/connection/nymeahosts.cpp +++ b/libnymea-app/connection/nymeahosts.cpp @@ -124,8 +124,8 @@ NymeaHost *NymeaHosts::createHost(const QString &name, const QUrl &url, Connecti { NymeaHost *host = new NymeaHost(this); host->setName(name); - Connection *connection = new Connection(url, bearerType, false, url.toString(), host); - connection->setOnline(true); + Connection *connection = new Connection(url, bearerType, false, name, host); + connection->setManual(true); host->connections()->addConnection(connection); addHost(host); return host; diff --git a/libnymea-app/jsonrpc/jsonrpcclient.cpp b/libnymea-app/jsonrpc/jsonrpcclient.cpp index d9f093c2..722a87dc 100644 --- a/libnymea-app/jsonrpc/jsonrpcclient.cpp +++ b/libnymea-app/jsonrpc/jsonrpcclient.cpp @@ -197,7 +197,7 @@ void JsonRpcClient::notificationReceived(const QVariantMap &data) m_token = data.value("params").toMap().value("token").toByteArray(); QSettings settings; settings.beginGroup("jsonTokens"); - settings.setValue(m_serverUuid, m_token); + settings.setValue(m_connection->currentHost()->uuid().toString(), m_token); settings.endGroup(); m_initialSetupRequired = false; @@ -305,7 +305,7 @@ QString JsonRpcClient::jsonRpcVersion() const QString JsonRpcClient::serverUuid() const { - return m_serverUuid; + return m_connection && m_connection->currentHost() ? m_connection->currentHost()->uuid().toString() : ""; } QString JsonRpcClient::serverName() const @@ -392,7 +392,7 @@ void JsonRpcClient::processAuthenticate(int /*commandId*/, const QVariantMap &da emit permissionsChanged(); QSettings settings; settings.beginGroup("jsonTokens"); - settings.setValue(m_serverUuid, m_token); + settings.setValue(m_connection->currentHost()->uuid().toString(), m_token); settings.endGroup(); emit authenticationRequiredChanged(); @@ -598,7 +598,7 @@ void JsonRpcClient::dataReceived(const QByteArray &data) m_token.clear(); QSettings settings; settings.beginGroup("jsonTokens"); - settings.setValue(m_serverUuid, m_token); + settings.setValue(serverUuid(), m_token); settings.endGroup(); emit authenticationRequiredChanged(); m_authenticated = false; @@ -649,8 +649,9 @@ void JsonRpcClient::helloReply(int /*commandId*/, const QVariantMap ¶ms) m_pushButtonAuthAvailable = params.value("pushButtonAuthAvailable").toBool(); emit pushButtonAuthAvailableChanged(); - m_serverUuid = params.value("uuid").toString(); m_serverVersion = params.value("version").toString(); + QUuid serverUuid = params.value("uuid").toUuid(); + QString name = params.value("name").toString(); m_experiences.clear(); foreach (const QVariant &experience, params.value("experiences").toList()) { m_experiences.insert(experience.toMap().value("name").toString(), experience.toMap().value("version").toString()); @@ -666,10 +667,16 @@ void JsonRpcClient::helloReply(int /*commandId*/, const QVariantMap ¶ms) qCInfo(dcJsonRpc()) << "Handshake reply:" << "Protocol version:" << protoVersionString << "InitRequired:" << m_initialSetupRequired << "AuthRequired:" << m_authenticationRequired << "PushButtonAvailable:" << m_pushButtonAuthAvailable; if (m_connection->currentHost()->uuid().isNull()) { - qCDebug(dcJsonRpc()) << "Updating Server UUID in connection:" << m_connection->currentHost()->uuid().toString() << "->" << m_serverUuid; - m_connection->currentHost()->setUuid(m_serverUuid); + qCDebug(dcJsonRpc()) << "Updating Server UUID in connection:" << m_connection->currentHost()->uuid().toString() << "->" << serverUuid; + m_connection->currentHost()->setUuid(serverUuid); + } else if (m_connection->currentHost()->uuid() != serverUuid) { + qCWarning(dcJsonRpc()) << "Unexpected server UUID" << serverUuid.toString() << "expected:" << m_connection->currentHost()->uuid(); + emit invalidServerUuid(serverUuid); + return; } + m_connection->currentHost()->setName(name); + QVersionNumber minimumRequiredVersion = QVersionNumber(5, 0); QVersionNumber maximumMajorVersion = QVersionNumber(7); if (m_jsonRpcVersion < minimumRequiredVersion) { @@ -688,11 +695,11 @@ void JsonRpcClient::helloReply(int /*commandId*/, const QVariantMap ¶ms) if (m_connection->isEncrypted()) { QByteArray oldPem; QSslCertificate certificate = m_connection->sslCertificate(); - if (!loadPem(m_serverUuid, oldPem)) { + if (!loadPem(serverUuid, oldPem)) { qCInfo(dcJsonRpc()) << "No SSL certificate for this host stored. Accepting and pinning new certificate."; // No certificate yet! Inform ui about it. emit newSslCertificate(); - storePem(m_serverUuid, m_connection->sslCertificate().toPem()); + storePem(serverUuid, m_connection->sslCertificate().toPem()); } else { // We have a certificate pinned already. Check if it's the same if (certificate.toPem() != oldPem) { @@ -707,7 +714,7 @@ void JsonRpcClient::helloReply(int /*commandId*/, const QVariantMap ¶ms) // Reject the connection until the UI explicitly accepts this... m_connection->disconnectFromHost(); - emit verifyConnectionCertificate(m_serverUuid, issuerInfo, certificate.toPem()); + emit verifyConnectionCertificate(serverUuid.toString(), issuerInfo, certificate.toPem()); return; } qCInfo(dcJsonRpc()) << "This connections certificate is trusted."; @@ -728,7 +735,7 @@ void JsonRpcClient::helloReply(int /*commandId*/, const QVariantMap ¶ms) m_token.clear(); QSettings settings; settings.beginGroup("jsonTokens"); - settings.setValue(m_serverUuid, m_token); + settings.setValue(serverUuid.toString(), m_token); settings.endGroup(); emit authenticationRequiredChanged(); m_authenticated = false; @@ -755,7 +762,7 @@ void JsonRpcClient::helloReply(int /*commandId*/, const QVariantMap ¶ms) // Reload the token, now that we're certain about the server uuid. QSettings settings; settings.beginGroup("jsonTokens"); - m_token = settings.value(m_serverUuid).toByteArray(); + m_token = settings.value(serverUuid.toString()).toByteArray(); settings.endGroup(); emit authenticationRequiredChanged(); diff --git a/libnymea-app/jsonrpc/jsonrpcclient.h b/libnymea-app/jsonrpc/jsonrpcclient.h index 74ffbfe7..8c3eee85 100644 --- a/libnymea-app/jsonrpc/jsonrpcclient.h +++ b/libnymea-app/jsonrpc/jsonrpcclient.h @@ -126,6 +126,7 @@ signals: void tokenChanged(); void invalidMinimumVersion(const QString &actualVersion, const QString &minVersion); void invalidMaximumVersion(const QString &actualVersion, const QString &maxVersion); + void invalidServerUuid(const QUuid &uuid); void authenticationFailed(); void pushButtonAuthFailed(); void createUserSucceeded(); @@ -158,7 +159,6 @@ private: bool m_pushButtonAuthAvailable = false; bool m_authenticated = false; int m_pendingPushButtonTransaction = -1; - QString m_serverUuid; QVersionNumber m_jsonRpcVersion; QString m_serverVersion; QString m_serverQtVersion; diff --git a/nymea-app/resources.qrc b/nymea-app/resources.qrc index 3e267958..befdceeb 100644 --- a/nymea-app/resources.qrc +++ b/nymea-app/resources.qrc @@ -289,5 +289,6 @@ ui/mainviews/energy/ConsumersHistoryPage.qml ui/mainviews/energy/ConsumerStatsPage.qml ui/mainviews/energy/ConsumersPieChartPage.qml + ui/connection/ManualConnectionEntry.qml diff --git a/nymea-app/ui/MainMenu.qml b/nymea-app/ui/MainMenu.qml index 1ad8b97b..23b497fc 100644 --- a/nymea-app/ui/MainMenu.qml +++ b/nymea-app/ui/MainMenu.qml @@ -88,6 +88,7 @@ Drawer { Layout.alignment: Qt.AlignVCenter visible: index === configuredHostsModel.currentIndex && !topSectionLayout.configureConnections } + ProgressButton { imageSource: "/ui/images/close.svg" visible: topSectionLayout.configureConnections && (autoConnectHost.length === 0 || index > 0) @@ -104,8 +105,24 @@ Drawer { } } onClicked: { - configuredHostsModel.currentIndex = index - root.close() + if (topSectionLayout.configureConnections) { + var nymeaHost = nymeaDiscovery.nymeaHosts.find(hostDelegate.configuredHost.uuid); + if (nymeaHost) { + var connectionInfoDialog = Qt.createComponent("/ui/components/ConnectionInfoDialog.qml") + var popup = connectionInfoDialog.createObject(app,{nymeaEngine: configuredHost.engine, nymeaHost: nymeaHost}) + popup.open() + popup.connectionSelected.connect(function(connection) { + print("...") + configuredHost.engine.jsonRpcClient.disconnectFromHost(); + configuredHost.engine.jsonRpcClient.connectToHost(nymeaHost, connection) + configuredHostsModel.currentIndex = index + root.close() + }) + } + } else { + configuredHostsModel.currentIndex = index + root.close() + } } } } @@ -233,5 +250,12 @@ Drawer { } } } + +// Component { +// id: hostConnectionInfoComponent +// MeaDialog { + +// } +// } } diff --git a/nymea-app/ui/RootItem.qml b/nymea-app/ui/RootItem.qml index 69dad3af..1436403b 100644 --- a/nymea-app/ui/RootItem.qml +++ b/nymea-app/ui/RootItem.qml @@ -150,7 +150,7 @@ Item { engine.jsonRpcClient.connectToHost(cachedHost) return; } - print("Warning: There is a last connected host but UUID is unknown to discovery...") + console.warn("There is a last connected host but UUID is unknown to discovery...") } else if (autoConnectHost.length > 0 && index === 0) { var host = nymeaDiscovery.nymeaHosts.createLanHost(Configuration.systemName, autoConnectHost); engine.jsonRpcClient.connectToHost(host) @@ -295,6 +295,13 @@ Item { if (engine.jsonRpcClient.connected) { nymeaDiscovery.cacheHost(engine.jsonRpcClient.currentHost) configuredHost.uuid = engine.jsonRpcClient.serverUuid + + for (var i = 0; i < configuredHostsModel.count; i++) { + if (i != index && configuredHostsModel.get(i).uuid == engine.jsonRpcClient.serverUuid) { + configuredHostsModel.removeHost(i); + break; + } + } } init(); } @@ -320,6 +327,12 @@ Item { popup.maxVersion = maxVersion; popup.open() } + onInvalidServerUuid: { + var connection = engine.jsonRpcClient.currentConnection; + engine.jsonRpcClient.disconnectFromHost(); + engine.jsonRpcClient.currentHost.connections.removeConnection(connection); + nymeaDiscovery.cacheHost(engine.jsonRpcClient.currentHost); + } } Connections { @@ -365,7 +378,7 @@ Item { url += ":" + tunnelProxyConfig.port url += "?uuid=" + engine.jsonRpcClient.currentHost.uuid console.info("Adding tunnel proxy connection:", url) - engine.jsonRpcClient.currentHost.connections.addConnection(url, Connection.BearerTypeCloud, tunnelProxyConfig.sslEnabled, "Remote proxy connection"); + engine.jsonRpcClient.currentHost.connections.addConnection(url, Connection.BearerTypeCloud, tunnelProxyConfig.sslEnabled, "Remote proxy connection", true); } nymeaDiscovery.cacheHost(engine.jsonRpcClient.currentHost) } diff --git a/nymea-app/ui/components/ConnectionInfoDialog.qml b/nymea-app/ui/components/ConnectionInfoDialog.qml index 6e7cb810..8b10686f 100644 --- a/nymea-app/ui/components/ConnectionInfoDialog.qml +++ b/nymea-app/ui/components/ConnectionInfoDialog.qml @@ -2,18 +2,22 @@ import QtQuick 2.9 import Nymea 1.0 import QtQuick.Controls 2.2 import QtQuick.Layouts 1.2 +import "qrc:/ui/connection" Dialog { - id: dialog + id: root x: (parent.width - width) / 2 y: (parent.height - height) / 2 modal: true title: qsTr("System information") - standardButtons: Dialog.Ok + standardButtons: Dialog.NoButton + property Engine nymeaEngine: null property var nymeaHost: null + signal connectionSelected(Connection connection) + header: Item { implicitHeight: headerRow.height + Style.margins * 2 implicitWidth: parent.width @@ -32,7 +36,7 @@ Dialog { id: titleLabel Layout.fillWidth: true wrapMode: Text.WrapAtWordBoundaryOrAnywhere - text: dialog.title + text: root.title color: Style.accentColor font.pixelSize: app.largeFont } @@ -48,7 +52,7 @@ Dialog { text: "Name:" } Label { - text: dialog.nymeaHost.name + text: root.nymeaHost.name Layout.fillWidth: true elide: Text.ElideRight } @@ -56,7 +60,7 @@ Dialog { text: "UUID:" } Label { - text: dialog.nymeaHost.uuid + text: root.nymeaHost.uuid Layout.fillWidth: true elide: Text.ElideRight } @@ -64,7 +68,7 @@ Dialog { text: "Version:" } Label { - text: dialog.nymeaHost.version + text: root.nymeaHost.version Layout.fillWidth: true elide: Text.ElideRight } @@ -84,19 +88,19 @@ Dialog { id: contentColumn width: parent.width Repeater { - model: dialog.nymeaHost.connections + model: root.nymeaHost.connections delegate: NymeaSwipeDelegate { Layout.fillWidth: true wrapTexts: false progressive: false - text: model.name - subText: model.url + text: model.url + subText: model.name prominentSubText: false iconName: { switch (model.bearerType) { case Connection.BearerTypeLan: case Connection.BearerTypeWan: - if (engine.jsonRpcClient.availableBearerTypes & NymeaConnection.BearerTypeEthernet != NymeaConnection.BearerTypeNone) { + if (nymeaEngine.jsonRpcClient.availableBearerTypes & NymeaConnection.BearerTypeEthernet != NymeaConnection.BearerTypeNone) { return "../images/connections/network-wired.svg" } return "../images/connections/network-wifi.svg"; @@ -113,14 +117,59 @@ Dialog { tertiaryIconName: model.secure ? "../images/connections/network-secure.svg" : "" secondaryIconName: !model.online ? "../images/connections/cloud-error.svg" : "" secondaryIconColor: "red" + canDelete: root.nymeaEngine.jsonRpcClient.currentConnection !== nymeaHost.connections.get(index) + onDeleteClicked: { + root.nymeaHost.connections.removeConnection(root.nymeaHost.connections.get(index)) + nymeaDiscovery.cacheHost(nymeaHost) + } onClicked: { - dialog.close() - engine.jsonRpcClient.connectToHost(dialog.nymeaHost, dialog.nymeaHost.connections.get(index)) + print("selecting", root.nymeaHost.connections.get(index)) + root.connectionSelected(root.nymeaHost.connections.get(index)) + root.close() } } } } } + + RowLayout { + Layout.columnSpan: 2 + Button { + text: qsTr("Add") + onClicked: { + var popup = addManualConnectionComponent.createObject(root.parent) + popup.open(); + popup.accepted.connect(function() { + root.nymeaHost.connections.addConnection(popup.rpcUrl, Connection.BearerTypeWan, popup.sslEnabled, "Manual connection", true) + }) + } + } + + Item { + Layout.fillWidth: true + } + + Button { + text: qsTr("Close") + onClicked: { + root.close() + } + } + } + + } + + Component { + id: addManualConnectionComponent + MeaDialog { + id: addManualConnectionDialog + standardButtons: Dialog.Ok | Dialog.Cancel + property alias rpcUrl: manualEntry.rpcUrl + property alias sslEnabled: manualEntry.sslEnabled + ManualConnectionEntry { + id: manualEntry + } + } } } diff --git a/nymea-app/ui/connection/ConnectionWizard.qml b/nymea-app/ui/connection/ConnectionWizard.qml index b8df682c..c5c30b9a 100644 --- a/nymea-app/ui/connection/ConnectionWizard.qml +++ b/nymea-app/ui/connection/ConnectionWizard.qml @@ -271,8 +271,12 @@ WizardPageBase { var nymeaHost = hostsProxy.get(index); var connectionInfoDialog = Qt.createComponent("/ui/components/ConnectionInfoDialog.qml") print("**", connectionInfoDialog.errorString()) - var popup = connectionInfoDialog.createObject(app,{nymeaHost: nymeaHost}) + var popup = connectionInfoDialog.createObject(app,{nymeaEngine: engine, nymeaHost: nymeaHost}) popup.open() + popup.connectionSelected.connect(function(connection) { + engine.jsonRpcClient.disconnectFromHost(); + engine.jsonRpcClient.connectToHost(nymeaHost, connection) + }) } } ] @@ -290,107 +294,18 @@ WizardPageBase { onBack: pageStack.pop() onNext: { - var rpcUrl - var hostAddress - var port - - // Set default to placeholder - if (addressTextInput.text === "") { - hostAddress = addressTextInput.placeholderText - } else { - hostAddress = addressTextInput.text - } - - if (portTextInput.text === "") { - port = portTextInput.placeholderText - } else { - port = portTextInput.text - } - - if (connectionTypeComboBox.currentIndex == 0) { - if (secureCheckBox.checked) { - rpcUrl = "nymeas://" + hostAddress + ":" + port - } else { - rpcUrl = "nymea://" + hostAddress + ":" + port - } - } else if (connectionTypeComboBox.currentIndex == 1) { - if (secureCheckBox.checked) { - rpcUrl = "wss://" + hostAddress + ":" + port - } else { - rpcUrl = "ws://" + hostAddress + ":" + port - } - } else if (connectionTypeComboBox.currentIndex == 2) { - if (secureCheckBox.checked) { - rpcUrl = "tunnels://" + hostAddress + ":" + port + "?uuid=" + serverUuidTextInput.text - } else { - rpcUrl = "tunnel://" + hostAddress + ":" + port + "?uuid=" + serverUuidTextInput.text - } - } - + var rpcUrl = manualEntry.rpcUrl; print("Try to connect ", rpcUrl) var host = nymeaDiscovery.nymeaHosts.createWanHost("Manual connection", rpcUrl); engine.jsonRpcClient.connectToHost(host) } - content: ColumnLayout { + content: ManualConnectionEntry { + id: manualEntry Layout.fillWidth: true Layout.margins: Style.margins Layout.maximumWidth: 500 Layout.alignment: Qt.AlignHCenter - - GridLayout { - columns: 2 - - Label { - text: qsTr("Protocol") - } - - ComboBox { - id: connectionTypeComboBox - Layout.fillWidth: true - model: [ qsTr("TCP"), qsTr("Websocket"), qsTr("Remote proxy") ] - } - - Label { - text: connectionTypeComboBox.currentIndex < 2 ? qsTr("Address:") : qsTr("Proxy address:") - } - TextField { - id: addressTextInput - objectName: "addressTextInput" - Layout.fillWidth: true - placeholderText: connectionTypeComboBox.currentIndex < 2 ? "127.0.0.1" : "tunnelproxy.nymea.io" - } - - Label { - text: qsTr("%1 UUID:").arg(Configuration.systemName) - visible: connectionTypeComboBox.currentIndex == 2 - } - TextField { - id: serverUuidTextInput - Layout.fillWidth: true - visible: connectionTypeComboBox.currentIndex == 2 - } - Label { text: qsTr("Port:") } - TextField { - id: portTextInput - Layout.fillWidth: true - placeholderText: connectionTypeComboBox.currentIndex === 0 - ? "2222" - : connectionTypeComboBox.currentIndex == 1 - ? "4444" - : "2213" - validator: IntValidator{bottom: 1; top: 65535;} - } - - Label { - Layout.fillWidth: true - text: qsTr("SSL:") - } - CheckBox { - id: secureCheckBox - checked: true - } - } } } } diff --git a/nymea-app/ui/connection/ManualConnectionEntry.qml b/nymea-app/ui/connection/ManualConnectionEntry.qml new file mode 100644 index 00000000..43f2f05b --- /dev/null +++ b/nymea-app/ui/connection/ManualConnectionEntry.qml @@ -0,0 +1,105 @@ +import QtQuick 2.3 +import QtQuick.Controls 2.2 +import QtQuick.Layouts 1.2 +import "../components" +import Nymea 1.0 + +ColumnLayout { + + property string rpcUrl: { + var rpcUrl + var hostAddress + var port + + // Set default to placeholder + if (addressTextInput.text === "") { + hostAddress = addressTextInput.placeholderText + } else { + hostAddress = addressTextInput.text + } + + if (portTextInput.text === "") { + port = portTextInput.placeholderText + } else { + port = portTextInput.text + } + + if (connectionTypeComboBox.currentIndex == 0) { + if (secureCheckBox.checked) { + rpcUrl = "nymeas://" + hostAddress + ":" + port + } else { + rpcUrl = "nymea://" + hostAddress + ":" + port + } + } else if (connectionTypeComboBox.currentIndex == 1) { + if (secureCheckBox.checked) { + rpcUrl = "wss://" + hostAddress + ":" + port + } else { + rpcUrl = "ws://" + hostAddress + ":" + port + } + } else if (connectionTypeComboBox.currentIndex == 2) { + if (secureCheckBox.checked) { + rpcUrl = "tunnels://" + hostAddress + ":" + port + "?uuid=" + serverUuidTextInput.text + } else { + rpcUrl = "tunnel://" + hostAddress + ":" + port + "?uuid=" + serverUuidTextInput.text + } + } + + return rpcUrl; + } + + property bool sslEnabled: secureCheckBox.checked + + GridLayout { + columns: 2 + + Label { + text: qsTr("Protocol") + } + + ComboBox { + id: connectionTypeComboBox + Layout.fillWidth: true + model: [ qsTr("TCP"), qsTr("Websocket"), qsTr("Remote proxy") ] + } + + Label { + text: connectionTypeComboBox.currentIndex < 2 ? qsTr("Address:") : qsTr("Proxy address:") + } + TextField { + id: addressTextInput + objectName: "addressTextInput" + Layout.fillWidth: true + placeholderText: connectionTypeComboBox.currentIndex < 2 ? "127.0.0.1" : "tunnelproxy.nymea.io" + } + + Label { + text: qsTr("%1 UUID:").arg(Configuration.systemName) + visible: connectionTypeComboBox.currentIndex == 2 + } + TextField { + id: serverUuidTextInput + Layout.fillWidth: true + visible: connectionTypeComboBox.currentIndex == 2 + } + Label { text: qsTr("Port:") } + TextField { + id: portTextInput + Layout.fillWidth: true + placeholderText: connectionTypeComboBox.currentIndex === 0 + ? "2222" + : connectionTypeComboBox.currentIndex == 1 + ? "4444" + : "2213" + validator: IntValidator{bottom: 1; top: 65535;} + } + + Label { + Layout.fillWidth: true + text: qsTr("SSL:") + } + CheckBox { + id: secureCheckBox + checked: true + } + } +}