mirror of
https://github.com/FAUSheppy/speech-server-client-qt
synced 2025-12-06 17:01:35 +01:00
refactor networking for use in multiple windows
This commit is contained in:
49
about.cpp
Normal file
49
about.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "about.h"
|
||||
#include "ui_about.h"
|
||||
|
||||
#include <QGridLayout>
|
||||
#include <QGroupBox>
|
||||
#include <QLabel>
|
||||
|
||||
About::About(QWidget *parent) : QMainWindow(parent) {
|
||||
|
||||
QGridLayout *mainLayout = new QGridLayout();
|
||||
|
||||
/* config options layout */
|
||||
QGridLayout *layoutLegal = new QGridLayout();
|
||||
QGridLayout *layoutSoftwareinfo = new QGridLayout();
|
||||
|
||||
/* create legal box */
|
||||
QGroupBox *legalGroup = new QGroupBox();
|
||||
|
||||
|
||||
/* create software infor group */
|
||||
QGroupBox *softwareInfoGroup = new QGroupBox();
|
||||
QString *version = getCurrentVersion();
|
||||
QLabel *richText = new QLabel();
|
||||
richText->setText(*version);
|
||||
softwareInfoGroup->setTitle("Software Information");
|
||||
|
||||
layoutSoftwareinfo->addWidget(richText);
|
||||
softwareInfoGroup->setLayout(layoutSoftwareinfo);
|
||||
|
||||
/* add groups to main layout */
|
||||
mainLayout->addWidget(softwareInfoGroup);
|
||||
|
||||
/* setup main window */
|
||||
QWidget *mainWidget = new QWidget(this);
|
||||
mainWidget->setLayout(mainLayout);
|
||||
this->setCentralWidget(mainWidget);
|
||||
}
|
||||
|
||||
About::~About()
|
||||
{
|
||||
}
|
||||
|
||||
QString* About::getCurrentVersion(){
|
||||
return new QString("<p>LOLOLOL</p>");
|
||||
}
|
||||
|
||||
QString* About::getServerVersion(){
|
||||
return new QString("<p>LOLOLOL</p>");
|
||||
}
|
||||
19
about.h
Normal file
19
about.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef ABOUT_H
|
||||
#define ABOUT_H
|
||||
|
||||
#include <QMainWindow>
|
||||
|
||||
class About : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit About(QWidget *parent = nullptr);
|
||||
~About();
|
||||
|
||||
private:
|
||||
QString* getCurrentVersion();
|
||||
QString *getServerVersion();
|
||||
};
|
||||
|
||||
#endif // ABOUT_H
|
||||
132
mainwindow.cpp
132
mainwindow.cpp
@@ -1,4 +1,5 @@
|
||||
#include "mainwindow.h"
|
||||
#include "about.h"
|
||||
#include "mainwindow.h"
|
||||
#include "notificationwidget.h"
|
||||
#include "settings.h"
|
||||
#include "ui_mainwindow.h"
|
||||
@@ -20,6 +21,7 @@
|
||||
#include <QStandardPaths>
|
||||
#include <QString>
|
||||
#include <settingkeys.h>
|
||||
#include <serverconnection.h>
|
||||
|
||||
#define FILENAME_COL 0
|
||||
#define TRACKING_ID_COL 1
|
||||
@@ -47,8 +49,7 @@ MainWindow::MainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::MainWind
|
||||
|
||||
handleInitialSettings();
|
||||
|
||||
setAuthHeader(mySettings->value(SETTING_USER).toString(), mySettings->value(SETTING_PASS).toString());
|
||||
networkManager = new QNetworkAccessManager(this);
|
||||
serverConnection = new ServerConnection(this, mySettings);
|
||||
|
||||
/* setup buttons */
|
||||
button = ui->centralWidget->findChild<QPushButton*>("pushButton");
|
||||
@@ -75,11 +76,12 @@ MainWindow::MainWindow(QWidget *parent):QMainWindow(parent), ui(new Ui::MainWind
|
||||
|
||||
/* create status update timer */
|
||||
QTimer *timer = new QTimer(this);
|
||||
connect(timer, SIGNAL(timeout()), this, SLOT(queryStatusAll()));
|
||||
connect(timer, SIGNAL(timeout()), this, SLOT(queryStatusAllWrapper()));
|
||||
timer->start(1000);
|
||||
|
||||
/* add handler for menu configuration */
|
||||
ui->menuKonfiguration->addAction("Standards", this, SLOT(openConfigurationWindow()) );
|
||||
ui->menuKonfiguration->addAction("Über diese Software", this, SLOT(openAboutWindow()) );
|
||||
|
||||
/* set window options */
|
||||
this->setWindowTitle(WINDOW_TITLE);
|
||||
@@ -93,9 +95,14 @@ void MainWindow::openConfigurationWindow(){
|
||||
settingsWindow->show();
|
||||
}
|
||||
|
||||
void MainWindow::openAboutWindow(){
|
||||
About *aboutWindow = new About();
|
||||
aboutWindow->setAttribute(Qt::WA_DeleteOnClose);
|
||||
aboutWindow->show();
|
||||
}
|
||||
|
||||
void MainWindow::appyConfigChanges(){
|
||||
setAuthHeader(mySettings->value(SETTING_USER).toString(), mySettings->value(SETTING_PASS).toString());
|
||||
networkManager = new QNetworkAccessManager(this);
|
||||
serverConnection = new ServerConnection(this, mySettings);
|
||||
}
|
||||
|
||||
void MainWindow::handleInitialSettings(){
|
||||
@@ -142,7 +149,7 @@ void MainWindow::importFile(){
|
||||
if(filename.isNull()){
|
||||
return;
|
||||
}else{
|
||||
submitFile(filename);
|
||||
this->submitFileSlot(filename);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,74 +162,7 @@ void MainWindow::showNotification(QString str){
|
||||
auto *timer = new QTimer();
|
||||
connect(timer, SIGNAL(timeout()), popUp, SLOT(fadeOut()));
|
||||
timer->start(5000);
|
||||
}
|
||||
|
||||
void MainWindow::setAuthHeader(const QString username, const QString password){
|
||||
|
||||
/* prepare auth */
|
||||
QString concatenated = username + ":" + password;
|
||||
QByteArray data = concatenated.toLocal8Bit().toBase64();
|
||||
QString authHeaderStr = "Basic " + data;
|
||||
authHeaderData = authHeaderStr.toLocal8Bit();
|
||||
|
||||
}
|
||||
|
||||
QString MainWindow::buildURLFromLocation(QVariant location){
|
||||
return buildURLFromLocation(location.toString());
|
||||
}
|
||||
|
||||
QString MainWindow::buildURLFromLocation(QString location){
|
||||
QString proto = mySettings->value(SETTING_PROTO).toString();
|
||||
QString host = mySettings->value(SETTING_HOST).toString();
|
||||
QString port = mySettings->value(SETTING_PORT).toString();
|
||||
if(!proto.endsWith("://")){
|
||||
proto += "://";
|
||||
}
|
||||
if(!port.startsWith(":")){
|
||||
port = ":" + port;
|
||||
}
|
||||
QString url = proto + host + port + location;
|
||||
return url;
|
||||
}
|
||||
|
||||
void MainWindow::submitFile(QString filename){
|
||||
|
||||
/* prepare request */
|
||||
QUrl serviceUrl = QUrl(buildURLFromLocation(mySettings->value(SETTING_LOC_SUBMIT)));
|
||||
QNetworkRequest request(serviceUrl);
|
||||
request.setRawHeader("Authorization", authHeaderData);
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||
|
||||
/* read audio as base64 */
|
||||
QFile sourceFile(filename);
|
||||
sourceFile.open(QIODevice::ReadOnly);
|
||||
QByteArray base64Encoded = sourceFile.readAll().toBase64();
|
||||
QString dataString = QString::fromUtf8(base64Encoded);
|
||||
sourceFile.close();
|
||||
|
||||
/* prepare json */
|
||||
QJsonObject json = QJsonObject();
|
||||
QJsonValue dataFieldContent = QJsonValue(dataString);
|
||||
QFileInfo info(filename);
|
||||
QString basename(info.fileName());
|
||||
json["filename"] = filename;
|
||||
json["data"] = dataFieldContent;
|
||||
|
||||
/* make request */
|
||||
connect(networkManager, SIGNAL(finished(QNetworkReply*)), this,
|
||||
SLOT(requestFinished(QNetworkReply*)), Qt::UniqueConnection);
|
||||
networkManager->post(request, QJsonDocument(json).toJson());
|
||||
|
||||
qDebug("Request submitted");
|
||||
}
|
||||
|
||||
void MainWindow::queryTransscript(QString trackingId){
|
||||
QString url = buildURLFromLocation(mySettings->value(SETTING_LOC_TRANSCRIPT));
|
||||
QString paramUrl = QString("%1?id=%2").arg(url, trackingId);
|
||||
QUrl transcriptUrl = QUrl(paramUrl);
|
||||
QNetworkRequest request(transcriptUrl);
|
||||
request.setRawHeader("Authorization", authHeaderData);
|
||||
networkManager->get(request);
|
||||
}
|
||||
|
||||
void MainWindow::openContainingDir(){
|
||||
@@ -276,7 +216,7 @@ void MainWindow::updateList(QNetworkReply* reply){
|
||||
auto status = tw->model()->data(tw->model()->index(i, TRANSCRIPT_STATUS_COL));
|
||||
auto tStatus = status.toString().toInt();
|
||||
if(tStatus == 0){
|
||||
queryTransscript(trackingId);
|
||||
serverConnection->queryTransscript(trackingId);
|
||||
}
|
||||
}else{
|
||||
pg->setValue(percentage);
|
||||
@@ -387,11 +327,36 @@ void MainWindow::addTrackingToList(QNetworkReply* reply){
|
||||
qDebug("Reply added to be tracked by list");
|
||||
}
|
||||
|
||||
void MainWindow::submitFileSlot(QString filename){
|
||||
|
||||
/* read audio as base64 */
|
||||
QFile sourceFile(filename);
|
||||
sourceFile.open(QIODevice::ReadOnly);
|
||||
QByteArray base64Encoded = sourceFile.readAll().toBase64();
|
||||
QString dataString = QString::fromUtf8(base64Encoded);
|
||||
sourceFile.close();
|
||||
|
||||
/* prepare json */
|
||||
QJsonObject json = QJsonObject();
|
||||
QJsonValue dataFieldContent = QJsonValue(dataString);
|
||||
QFileInfo info(filename);
|
||||
QString basename(info.fileName());
|
||||
json["filename"] = filename;
|
||||
json["data"] = dataFieldContent;
|
||||
|
||||
/* make request */
|
||||
connect(serverConnection->getNetworkManager(), SIGNAL(finished(QNetworkReply*)), this,
|
||||
SLOT(requestFinished(QNetworkReply*)), Qt::UniqueConnection);
|
||||
serverConnection->submitFile(QJsonDocument(json));
|
||||
|
||||
qDebug("Request submission requested");
|
||||
}
|
||||
|
||||
void MainWindow::requestFinished(QNetworkReply *reply){
|
||||
|
||||
QString submitUrl = buildURLFromLocation(mySettings->value(SETTING_LOC_SUBMIT));
|
||||
QString statusRequestUrl = buildURLFromLocation(mySettings->value(SETTING_LOC_STATE));
|
||||
QString requestTranscriptUrl = buildURLFromLocation(mySettings->value(SETTING_LOC_TRANSCRIPT));
|
||||
QString submitUrl = serverConnection->buildURLFromLocation(mySettings->value(SETTING_LOC_SUBMIT));
|
||||
QString statusRequestUrl = serverConnection->buildURLFromLocation(mySettings->value(SETTING_LOC_STATE));
|
||||
QString requestTranscriptUrl = serverConnection->buildURLFromLocation(mySettings->value(SETTING_LOC_TRANSCRIPT));
|
||||
|
||||
if(QString::compare(reply->url().toString(), submitUrl) == 0){
|
||||
addTrackingToList(reply);
|
||||
@@ -406,16 +371,11 @@ void MainWindow::requestFinished(QNetworkReply *reply){
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::queryStatusAll(){
|
||||
|
||||
QString statusRequestUrl = buildURLFromLocation(mySettings->value(SETTING_LOC_STATE));
|
||||
QUrl trackingUrl = QUrl(statusRequestUrl);
|
||||
QNetworkRequest request(trackingUrl);
|
||||
request.setRawHeader("Authorization", authHeaderData);
|
||||
networkManager->get(request);
|
||||
//qDebug("Status query sent");
|
||||
void MainWindow::queryStatusAllWrapper(){
|
||||
serverConnection->queryStatusAll();
|
||||
}
|
||||
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
{
|
||||
delete ui;
|
||||
|
||||
14
mainwindow.h
14
mainwindow.h
@@ -6,6 +6,8 @@
|
||||
#include <QPushButton>
|
||||
#include <QSettings>
|
||||
#include <QTableWidget>
|
||||
#include <serverconnection.h>
|
||||
|
||||
|
||||
namespace Ui {
|
||||
class MainWindow;
|
||||
@@ -22,28 +24,24 @@ public:
|
||||
private slots:
|
||||
void importFile();
|
||||
void requestFinished(QNetworkReply*);
|
||||
void queryStatusAll();
|
||||
void queryStatusAllWrapper();
|
||||
void openContainingDir();
|
||||
void openConfigurationWindow();
|
||||
void appyConfigChanges();
|
||||
void openAboutWindow();
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
QTableWidget *tw;
|
||||
QPushButton *button;
|
||||
QAction *standardsMenu;
|
||||
QByteArray authHeaderData;
|
||||
QNetworkAccessManager *networkManager;
|
||||
QSettings *mySettings;
|
||||
void submitFile(QString filename);
|
||||
void setAuthHeader(const QString username, const QString password);
|
||||
ServerConnection *serverConnection;
|
||||
void updateList(QNetworkReply *reply);
|
||||
void addTrackingToList(QNetworkReply *reply);
|
||||
void showNotification(QString str);
|
||||
void queryTransscript(QString trackingId);
|
||||
void saveTranscript(QNetworkReply *reply);
|
||||
void handleInitialSettings();
|
||||
QString buildURLFromLocation(QString location);
|
||||
QString buildURLFromLocation(QVariant location);
|
||||
void submitFileSlot(QString filename);
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
||||
|
||||
81
serverconnection.cpp
Normal file
81
serverconnection.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
#include "serverconnection.h"
|
||||
#include "settingkeys.h"
|
||||
#include <QFileInfo>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QSettings>
|
||||
|
||||
ServerConnection::ServerConnection(QObject *parent, QSettings *settings)
|
||||
{
|
||||
|
||||
setAuthHeader(settings->value(SETTING_USER).toString(), settings->value(SETTING_PASS).toString());
|
||||
networkManager = new QNetworkAccessManager(parent);
|
||||
mySettings = settings;
|
||||
this->parent = parent;
|
||||
}
|
||||
|
||||
QString ServerConnection::buildURLFromLocation(QVariant location){
|
||||
return buildURLFromLocation(location.toString());
|
||||
}
|
||||
|
||||
QString ServerConnection::buildURLFromLocation(QString location){
|
||||
QString proto = mySettings->value(SETTING_PROTO).toString();
|
||||
QString host = mySettings->value(SETTING_HOST).toString();
|
||||
QString port = mySettings->value(SETTING_PORT).toString();
|
||||
if(!proto.endsWith("://")){
|
||||
proto += "://";
|
||||
}
|
||||
if(!port.startsWith(":")){
|
||||
port = ":" + port;
|
||||
}
|
||||
QString url = proto + host + port + location;
|
||||
return url;
|
||||
}
|
||||
|
||||
void ServerConnection::setAuthHeader(const QString username, const QString password){
|
||||
|
||||
/* prepare auth */
|
||||
QString concatenated = username + ":" + password;
|
||||
QByteArray data = concatenated.toLocal8Bit().toBase64();
|
||||
QString authHeaderStr = "Basic " + data;
|
||||
authHeaderData = authHeaderStr.toLocal8Bit();
|
||||
|
||||
}
|
||||
|
||||
void ServerConnection::queryStatusAll(){
|
||||
|
||||
QString statusRequestUrl = buildURLFromLocation(mySettings->value(SETTING_LOC_STATE));
|
||||
QUrl trackingUrl = QUrl(statusRequestUrl);
|
||||
QNetworkRequest request(trackingUrl);
|
||||
request.setRawHeader("Authorization", authHeaderData);
|
||||
networkManager->get(request);
|
||||
//qDebug("Status query sent");
|
||||
}
|
||||
|
||||
void ServerConnection::queryTransscript(QString trackingId){
|
||||
QString url = buildURLFromLocation(mySettings->value(SETTING_LOC_TRANSCRIPT));
|
||||
QString paramUrl = QString("%1?id=%2").arg(url, trackingId);
|
||||
QUrl transcriptUrl = QUrl(paramUrl);
|
||||
QNetworkRequest request(transcriptUrl);
|
||||
request.setRawHeader("Authorization", authHeaderData);
|
||||
networkManager->get(request);
|
||||
}
|
||||
|
||||
void ServerConnection::submitFile(QJsonDocument jsonDocument){
|
||||
|
||||
/* prepare request */
|
||||
QUrl serviceUrl = QUrl(buildURLFromLocation(mySettings->value(SETTING_LOC_SUBMIT)));
|
||||
QNetworkRequest request(serviceUrl);
|
||||
request.setRawHeader("Authorization", authHeaderData);
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||
|
||||
/* make request */
|
||||
networkManager->post(request, jsonDocument.toJson());
|
||||
|
||||
qDebug("Request submitted");
|
||||
}
|
||||
|
||||
QNetworkAccessManager *ServerConnection::getNetworkManager(){
|
||||
return networkManager;
|
||||
}
|
||||
30
serverconnection.h
Normal file
30
serverconnection.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef SERVERCONNECTION_H
|
||||
#define SERVERCONNECTION_H
|
||||
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QSettings>
|
||||
|
||||
|
||||
|
||||
class ServerConnection
|
||||
{
|
||||
public:
|
||||
ServerConnection(QObject *parent, QSettings *settings);
|
||||
void queryTransscript(QString trackingId);
|
||||
void submitFile(QJsonDocument json);
|
||||
QString buildURLFromLocation(QVariant location);
|
||||
QString buildURLFromLocation(QString location);
|
||||
QNetworkAccessManager *getNetworkManager();
|
||||
public slots:
|
||||
void queryStatusAll();
|
||||
private slots:
|
||||
void queryStatusAllWrapper();
|
||||
private:
|
||||
QNetworkAccessManager *networkManager;
|
||||
QByteArray authHeaderData;
|
||||
QSettings *mySettings;
|
||||
void setAuthHeader(const QString username, const QString password);
|
||||
QObject *parent;
|
||||
};
|
||||
|
||||
#endif // SERVERCONNECTION_H
|
||||
@@ -28,13 +28,17 @@ SOURCES += \
|
||||
main.cpp \
|
||||
mainwindow.cpp \
|
||||
notificationwidget.cpp \
|
||||
settings.cpp
|
||||
settings.cpp \
|
||||
about.cpp \
|
||||
serverconnection.cpp
|
||||
|
||||
HEADERS += \
|
||||
mainwindow.h \
|
||||
notificationwidget.h \
|
||||
settings.h \
|
||||
settingkeys.h
|
||||
settingkeys.h \
|
||||
about.h \
|
||||
serverconnection.h
|
||||
|
||||
FORMS += \
|
||||
mainwindow.ui \
|
||||
|
||||
Reference in New Issue
Block a user