Skip to content

Commit

Permalink
Refs #99. Added language and token update in Survey Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
SBriere committed Nov 29, 2024
1 parent 0493a4a commit af2df1d
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 44 deletions.
4 changes: 4 additions & 0 deletions client/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ set(headers
data/TransferringFile.h
data/UploadingFile.h
data/IconMenuDelegate.h

# Librairies
libs/AudioVideoUtils.h
libs/WebPageRequestInterceptor.h
libs/highlighter/languagedata.h
libs/highlighter/qsourcehighliter.h
libs/highlighter/qsourcehighliterthemes.h

# Main Windows
main/MainWindow.h
# Managers
Expand Down Expand Up @@ -175,6 +178,7 @@ set(srcs

# Librairies
libs/AudioVideoUtils.cpp
libs/WebPageRequestInterceptor.cpp
libs/highlighter/languagedata.cpp
libs/highlighter/qsourcehighliter.cpp
libs/highlighter/qsourcehighliterthemes.cpp
Expand Down
31 changes: 1 addition & 30 deletions client/src/dialogs/WebLoginDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <QScreen>
#include <QTextDocumentFragment>

#include "Utils.h"
#include "TeraSettings.h"

WebLoginDialog::WebLoginDialog(ConfigManagerClient *config, QWidget *parent)
Expand All @@ -28,7 +27,7 @@ WebLoginDialog::WebLoginDialog(ConfigManagerClient *config, QWidget *parent)
QVBoxLayout *layout = new QVBoxLayout(ui->centralWidget);
layout->addWidget(m_webView);

m_requestInterceptor = new WebLoginRequestInterceptor(this);
m_requestInterceptor = new WebPageRequestInterceptor(true, this);

m_webPage = new QWebEnginePage(m_webView);
m_webPage->setUrlRequestInterceptor(m_requestInterceptor);
Expand Down Expand Up @@ -233,34 +232,6 @@ QString WebLoginDialog::currentServerName()
return QString();
}

///////////////////////////////////////////////////////////////////////////////////////////////////////
WebLoginRequestInterceptor::WebLoginRequestInterceptor(QObject *p) : QWebEngineUrlRequestInterceptor(p)
{
// Cache OS information
m_osName = Utils::getOsName();
m_osVersion = Utils::getOsVersion();
}

WebLoginRequestInterceptor::~WebLoginRequestInterceptor()
{

}

void WebLoginRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
{
// Inject client name and version
info.setHttpHeader("X-Client-Name", QByteArray(OPENTERAPLUS_CLIENT_NAME));
info.setHttpHeader("X-Client-Version", QByteArray(OPENTERAPLUS_VERSION));
info.setHttpHeader("X-OS-Name", m_osName.toUtf8());
info.setHttpHeader("X-OS-Version", m_osVersion.toUtf8());

// Inject required language
QString localeString = QLocale().bcp47Name();
//qDebug() << "localeString : " << localeString;
info.setHttpHeader(QByteArray("Accept-Language"), localeString.toUtf8());
}


void WebLoginDialog::on_btnCancel_clicked()
{
showLargeView(false);
Expand Down
17 changes: 3 additions & 14 deletions client/src/dialogs/WebLoginDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <QDialog>

#include "managers/ConfigManagerClient.h"
#include "libs/WebPageRequestInterceptor.h"

#include <QWebEngineView>
#include <QWebEnginePage>
#include <QWebEngineSettings>
Expand Down Expand Up @@ -63,19 +65,6 @@ namespace Ui {
class WebLoginDialog;
}


class WebLoginRequestInterceptor : public QWebEngineUrlRequestInterceptor {

// QWebEngineUrlRequestInterceptor interface
public:
WebLoginRequestInterceptor(QObject *p = nullptr);
~WebLoginRequestInterceptor();
void interceptRequest(QWebEngineUrlRequestInfo &info) override;
private:
QString m_osName;
QString m_osVersion;
};

class WebLoginDialog : public QDialog
{
Q_OBJECT
Expand Down Expand Up @@ -119,7 +108,7 @@ private slots:
Ui::WebLoginDialog *ui;
QWebEngineView *m_webView = nullptr;
QWebEnginePage *m_webPage = nullptr;
WebLoginRequestInterceptor *m_requestInterceptor = nullptr;
WebPageRequestInterceptor *m_requestInterceptor = nullptr;
ConfigManagerClient *m_config = nullptr;

bool m_showServers = false;
Expand Down
34 changes: 34 additions & 0 deletions client/src/libs/WebPageRequestInterceptor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "WebPageRequestInterceptor.h"
#include "Utils.h"

WebPageRequestInterceptor::WebPageRequestInterceptor(bool inject_client, QObject *p) :
QWebEngineUrlRequestInterceptor(p)
,m_injectClient(inject_client)
{
// Cache OS information
if (m_injectClient){
m_osName = Utils::getOsName();
m_osVersion = Utils::getOsVersion();
}
}

WebPageRequestInterceptor::~WebPageRequestInterceptor()
{

}

void WebPageRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
{
// Inject client name and version
if (m_injectClient){
info.setHttpHeader("X-Client-Name", QByteArray(OPENTERAPLUS_CLIENT_NAME));
info.setHttpHeader("X-Client-Version", QByteArray(OPENTERAPLUS_VERSION));
info.setHttpHeader("X-OS-Name", m_osName.toUtf8());
info.setHttpHeader("X-OS-Version", m_osVersion.toUtf8());
}

// Inject required language
QString localeString = QLocale().bcp47Name();
//qDebug() << "localeString : " << localeString;
info.setHttpHeader(QByteArray("Accept-Language"), localeString.toUtf8());
}
20 changes: 20 additions & 0 deletions client/src/libs/WebPageRequestInterceptor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef WEBPAGEREQUESTINTERCEPTOR_H
#define WEBPAGEREQUESTINTERCEPTOR_H

#include <QObject>
#include <QWebEngineUrlRequestInterceptor>

class WebPageRequestInterceptor : public QWebEngineUrlRequestInterceptor
{
Q_OBJECT
public:
WebPageRequestInterceptor(bool inject_client = false, QObject *p = nullptr);
~WebPageRequestInterceptor();
void interceptRequest(QWebEngineUrlRequestInfo &info) override;
private:
QString m_osName;
QString m_osVersion;
bool m_injectClient = false;
};

#endif // WEBPAGEREQUESTINTERCEPTOR_H
1 change: 1 addition & 0 deletions client/src/managers/ComManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ComManager::ComManager(QUrl serverUrl, bool connectWebsocket, QObject *parent) :

// Initialize token refresher timer @ each 15 minutes
m_tokenRefreshTimer.setInterval(1000*60*15);
//m_tokenRefreshTimer.setInterval(1000*60);
m_tokenRefreshTimer.setSingleShot(false);
connect(&m_tokenRefreshTimer, &QTimer::timeout, this, &ComManager::refreshUserToken);

Expand Down
2 changes: 2 additions & 0 deletions client/src/services/BaseServiceComManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ void BaseServiceComManager::handleUserTokenUpdated()
{
// Update token
setCredentials(m_comManager->getCurrentToken());

emit userTokenUpdated();
}

void BaseServiceComManager::processServicesReply(QList<TeraData> services, QUrlQuery reply_query)
Expand Down
1 change: 1 addition & 0 deletions client/src/services/BaseServiceComManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ private slots:
signals:
void dataReceived(QList<QJsonObject> items, QString endpoint, QUrlQuery reply_query);
void readyChanged(bool ready);
void userTokenUpdated();

};

Expand Down
11 changes: 11 additions & 0 deletions client/src/services/SurveyService/SurveyEditorDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ SurveyEditorDialog::SurveyEditorDialog(SurveyComManager *surveyComManager, const
profile->setHttpCacheType(QWebEngineProfile::DiskHttpCache);
qDebug() << "Cache Path = " << profile->cachePath();*/

m_requestInterceptor = new WebPageRequestInterceptor(false, this);
m_webPage = new QWebEnginePage(m_webView);
m_webPage->setUrlRequestInterceptor(m_requestInterceptor);

connect(m_webPage, &QWebEnginePage::certificateError, this, &SurveyEditorDialog::onCertificateError);
connect(m_webPage, &QWebEnginePage::loadingChanged, this, &SurveyEditorDialog::onPageLoadingChanged);
connect(m_webPage, &QWebEnginePage::loadProgress, this, &SurveyEditorDialog::onPageLoadingProcess);
m_webView->setPage(m_webPage);

connect(m_surveyComManager, &SurveyComManager::userTokenUpdated, this, &SurveyEditorDialog::onUserTokenUpdated);

}

SurveyEditorDialog::~SurveyEditorDialog()
Expand Down Expand Up @@ -94,3 +99,9 @@ void SurveyEditorDialog::onPageLoadingProcess(int progress)
ui->progressLoading->setValue(progress);
}

void SurveyEditorDialog::onUserTokenUpdated()
{
qDebug() << "Changing user token in Survey Editor";
m_webPage->runJavaScript("set_user_token(\"" + m_surveyComManager->getCurrentToken() + "\");");
}

4 changes: 4 additions & 0 deletions client/src/services/SurveyService/SurveyEditorDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <QWebEngineLoadingInfo>

#include "SurveyComManager.h"
#include "libs/WebPageRequestInterceptor.h"

namespace Ui {
class SurveyEditorDialog;
Expand All @@ -31,12 +32,15 @@ private slots:
void onPageLoadingChanged(const QWebEngineLoadingInfo &loadingInfo);
void onPageLoadingProcess(int progress);

void onUserTokenUpdated();


private:
Ui::SurveyEditorDialog *ui;
SurveyComManager *m_surveyComManager;
QWebEngineView *m_webView = nullptr;
QWebEnginePage *m_webPage = nullptr;
WebPageRequestInterceptor *m_requestInterceptor = nullptr;
QString m_testTypeUuid;

QMovie* m_loadingIcon;
Expand Down

0 comments on commit af2df1d

Please sign in to comment.