diff --git a/client/src/ClientApp.cpp b/client/src/ClientApp.cpp index 0428944..8ba40de 100755 --- a/client/src/ClientApp.cpp +++ b/client/src/ClientApp.cpp @@ -124,21 +124,22 @@ void ClientApp::showLogin() if (m_loginDiag == nullptr){ #ifndef OPENTERA_WEBASSEMBLY - m_loginDiag = new WebLoginDialog(); + m_loginDiag = new WebLoginDialog(&m_config); connect(m_loginDiag, &WebLoginDialog::loginSuccess, this, &ClientApp::onLoginSuccess, Qt::QueuedConnection); #else m_loginDiag = new LoginDialog(); connect(m_loginDiag, &LoginDialog::loginRequest, this, &ClientApp::loginRequested); connect(m_loginDiag, &LoginDialog::quitRequest, this, &ClientApp::loginQuitRequested); - - // Set server names - m_loginDiag->setServerNames(m_config.getServerNames()); - - // Show servers list... or not! - m_loginDiag->showServers(m_config.showServers()); #endif } + // Set server names + m_loginDiag->setServerNames(m_config.getServerNames()); + + // Show servers list... or not! + m_loginDiag->showServers(m_config.showServers()); + + // Delete main window, if present if (m_mainWindow){ m_mainWindow->deleteLater(); diff --git a/client/src/dialogs/WebLoginDialog.cpp b/client/src/dialogs/WebLoginDialog.cpp index 53767f6..6dbe493 100644 --- a/client/src/dialogs/WebLoginDialog.cpp +++ b/client/src/dialogs/WebLoginDialog.cpp @@ -3,8 +3,8 @@ #include -WebLoginDialog::WebLoginDialog(QWidget *parent) - : QDialog(parent), ui(new Ui::WebLoginDialog) +WebLoginDialog::WebLoginDialog(ConfigManagerClient *config, QWidget *parent) + : QDialog(parent), ui(new Ui::WebLoginDialog), m_config(config) { ui->setupUi(this); @@ -18,7 +18,6 @@ WebLoginDialog::WebLoginDialog(QWidget *parent) QWebChannel *channel = new QWebChannel(m_webPage); - WebLoginSharedObject *myObject = new WebLoginSharedObject(); channel->registerObject("qtObject", myObject); m_webPage->setWebChannel(channel); @@ -26,9 +25,7 @@ WebLoginDialog::WebLoginDialog(QWidget *parent) connect(m_webPage, &QWebEnginePage::certificateError, this, &WebLoginDialog::onCertificateError); connect(myObject, &WebLoginSharedObject::loginSuccess, this, &WebLoginDialog::loginSuccess); - m_webPage->setUrl(QUrl("https://127.0.0.1:40075/login")); - - m_webView->setPage(m_webPage); + m_webView->setPage(m_webPage); } WebLoginDialog::~WebLoginDialog() @@ -36,6 +33,36 @@ WebLoginDialog::~WebLoginDialog() delete ui; } +void WebLoginDialog::setServerNames(QStringList servers) +{ + ui->cmbServers->clear(); + foreach(QString server, servers){ + ui->cmbServers->addItem(server); + } + + // Connecting signal after will avoid calling onServerSelected when adding initial servers + connect(ui->cmbServers, &QComboBox::currentIndexChanged, this, &WebLoginDialog::onServerSelected); + + // Get last server used + QVariant current_server = TeraSettings::getGlobalSetting("last_used_server"); + + // Select server from the list if we have a setting for that + if (current_server.isValid()){ + QString server_name = current_server.toString(); + if (servers.contains(server_name)) + ui->cmbServers->setCurrentText(server_name); + } +} + + +void WebLoginDialog::showServers(bool show) +{ + if (ui->cmbServers->count() == 1) + show = false; // Never show server list if only one item. + + ui->cmbServers->setVisible(show); +} + void WebLoginDialog::onCertificateError(const QWebEngineCertificateError &certificateError) { @@ -45,3 +72,25 @@ void WebLoginDialog::onCertificateError(const QWebEngineCertificateError &certif auto mutableError = const_cast(certificateError); mutableError.acceptCertificate(); } + +void WebLoginDialog::onServerSelected(int index) +{ + QString currentServer = ui->cmbServers->itemText(index); + // Update last server + TeraSettings::setGlobalSetting("last_used_server", currentServer); + + //Get URL + if (m_config && m_webPage) + { + QUrl loginUrl = m_config->getServerLoginUrl(currentServer); + m_webPage->setUrl(loginUrl); + } +} + +QString WebLoginDialog::currentServerName() +{ + if (ui->cmbServers->currentIndex() >=0 && ui->cmbServers->isVisible()){ + return ui->cmbServers->currentText(); + } + return QString(); +} diff --git a/client/src/dialogs/WebLoginDialog.h b/client/src/dialogs/WebLoginDialog.h index 2aeafe1..5afe2d6 100644 --- a/client/src/dialogs/WebLoginDialog.h +++ b/client/src/dialogs/WebLoginDialog.h @@ -3,6 +3,7 @@ #include #include "TeraSettings.h" +#include "managers/ConfigManagerClient.h" #include #include #include @@ -38,26 +39,27 @@ class WebLoginDialog : public QDialog Q_OBJECT public: - explicit WebLoginDialog(QWidget *parent = nullptr); + explicit WebLoginDialog(ConfigManagerClient *config, QWidget *parent = nullptr); ~WebLoginDialog(); - QString currentServerName() {return QString();} void setStatusMessage(const QString &message, bool error=false) {qDebug() << "Unhandled message: " << message << "error: " << error;} + void setServerNames(QStringList servers); + QString currentServerName(); + void showServers(bool show); +signals: -private: + void loginSuccess(const QString &token, const QString &websocket_url, const QString &user_uuid); private slots: void onCertificateError(const QWebEngineCertificateError &certificateError); - -signals: - - void loginSuccess(const QString &token, const QString &websocket_url, const QString &user_uuid); + void onServerSelected(int index); private: Ui::WebLoginDialog *ui; QWebEngineView *m_webView; QWebEnginePage *m_webPage; + ConfigManagerClient *m_config; }; #endif // WEBLOGINDIALOG_H diff --git a/client/src/dialogs/WebLoginDialog.ui b/client/src/dialogs/WebLoginDialog.ui index 826e797..e7288e1 100644 --- a/client/src/dialogs/WebLoginDialog.ui +++ b/client/src/dialogs/WebLoginDialog.ui @@ -42,6 +42,9 @@ + + + diff --git a/client/src/managers/ConfigManagerClient.cpp b/client/src/managers/ConfigManagerClient.cpp index c9e00dc..ce2235b 100644 --- a/client/src/managers/ConfigManagerClient.cpp +++ b/client/src/managers/ConfigManagerClient.cpp @@ -33,10 +33,18 @@ QUrl ConfigManagerClient::getServerUrl(const QString &server_name) QVariantMap server_info = servers[server_name].toMap(); server_url.setHost(server_info["url"].toString()); server_url.setPort(server_info["port"].toInt()); + server_url.setScheme("https"); } return server_url; } +QUrl ConfigManagerClient::getServerLoginUrl(const QString &server_name) +{ + QUrl login_url = getServerUrl(server_name); + login_url.setPath("/login"); + return login_url; +} + bool ConfigManagerClient::getLogToFile() { bool rval = false; diff --git a/client/src/managers/ConfigManagerClient.h b/client/src/managers/ConfigManagerClient.h index 3b7b854..266306a 100644 --- a/client/src/managers/ConfigManagerClient.h +++ b/client/src/managers/ConfigManagerClient.h @@ -13,6 +13,7 @@ class ConfigManagerClient : public ConfigManager QStringList getServerNames(); QUrl getServerUrl(const QString &server_name); + QUrl getServerLoginUrl(const QString &server_name); bool getLogToFile();