Skip to content

Commit

Permalink
Refs #95, weblogin now enables server selection.
Browse files Browse the repository at this point in the history
  • Loading branch information
doumdi committed Sep 23, 2024
1 parent 3d46479 commit c6280b0
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 20 deletions.
15 changes: 8 additions & 7 deletions client/src/ClientApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
61 changes: 55 additions & 6 deletions client/src/dialogs/WebLoginDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <QVBoxLayout>


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);

Expand All @@ -18,24 +18,51 @@ WebLoginDialog::WebLoginDialog(QWidget *parent)

QWebChannel *channel = new QWebChannel(m_webPage);


WebLoginSharedObject *myObject = new WebLoginSharedObject();
channel->registerObject("qtObject", myObject);
m_webPage->setWebChannel(channel);

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()
{
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)
{
Expand All @@ -45,3 +72,25 @@ void WebLoginDialog::onCertificateError(const QWebEngineCertificateError &certif
auto mutableError = const_cast<QWebEngineCertificateError&>(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();
}
16 changes: 9 additions & 7 deletions client/src/dialogs/WebLoginDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <QDialog>
#include "TeraSettings.h"
#include "managers/ConfigManagerClient.h"
#include <QWebEngineView>
#include <QWebEnginePage>
#include <QWebEngineSettings>
Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions client/src/dialogs/WebLoginDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cmbServers"/>
</item>
<item>
<widget class="QWidget" name="centralWidget" native="true">
<property name="sizePolicy">
Expand Down
8 changes: 8 additions & 0 deletions client/src/managers/ConfigManagerClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions client/src/managers/ConfigManagerClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ConfigManagerClient : public ConfigManager

QStringList getServerNames();
QUrl getServerUrl(const QString &server_name);
QUrl getServerLoginUrl(const QString &server_name);

bool getLogToFile();

Expand Down

0 comments on commit c6280b0

Please sign in to comment.