Files
RRJClient/mainwindow.cpp
2025-01-20 10:58:52 +03:00

422 lines
12 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "updatenotifywidget.h"
#include <QFontDatabase>
#include <QPaintEvent>
#include <QPainter>
#include <QTimer>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
painting();
initialize();
}
void MainWindow::initialize()
{
ui->loadingProgressBar->setValue(0);
ui->settingsWidget->hide();
ui->notificationLabel->hide();
ui->loadingProgressBar->hide();
ui->updateButton->hide();
ui->connectButton->hide();
ui->startButton->setEnabled(false);
ui->debugText->hide();
ui->displayGroupWidget->hide();
ui->autostartCheckBox->hide();
ui->updateButton->setEnabled(false);
ui->startButton->setEnabled(false);
createObjects();
bindConnection();
emit sigCalculateHash();
emit sigInitializeClient(recognizeSystem,externalExecuter,sendSystem,connectionThread);
recognizeSystem->initialize(updateController,dataParser,this);
screenChecker->check();
loadStaticData();
emit sigSetConnect(dataParser->getServerSettings(),connectionThread);
checkAppAvailable();
}
void MainWindow::createObjects()
{
connectionThread = new QThread;
client = new TCPClient;
client->moveToThread(connectionThread);
dataParser = new DataParser;
sendSystem = new SendSystem;
sendSystem->moveToThread(connectionThread);
updateController = new UpdateController(dataParser,sendSystem);
updateController->moveToThread(connectionThread);
recognizeSystem = new RecognizeSystem;
recognizeSystem->moveToThread(connectionThread);
screenChecker = new ScreenChecker(dataParser,ui->displayWidget);
externalExecuter = new ExternalExecuter;
hashComparer = new HashComparer(dataParser);
connectionThread->start();
connectionThread->setPriority(QThread::HighestPriority);
timer = new QTimer;
}
void MainWindow::bindConnection()
{
connect(timer,&QTimer::timeout,this,&MainWindow::slotDisableNotify);
connect(recognizeSystem,&RecognizeSystem::sigStartCompare,hashComparer,&HashComparer::CompareDeltas);
connect(recognizeSystem,&RecognizeSystem::sigUpdateBytesAvailable,this,&MainWindow::updateProgress,Qt::QueuedConnection);
connect(recognizeSystem,&RecognizeSystem::sigLoadComplete,this,&MainWindow::loadComplete);
connect(recognizeSystem,&RecognizeSystem::sigNeedUpdate,this,&MainWindow::setNeedUpdate);
connect(recognizeSystem,&RecognizeSystem::sigSendDebugLog,this,&MainWindow::debugLog);
connect(recognizeSystem,&RecognizeSystem::sigSocketDisabled,this,&MainWindow::lostConnection);
connect(recognizeSystem,&RecognizeSystem::sigSaveLoginData,this,&MainWindow::checkLoginResult);
connect(recognizeSystem,&RecognizeSystem::sigSocketWaitForReadyRead,client,&TCPClient::waitRead,Qt::AutoConnection);
connect(recognizeSystem,&RecognizeSystem::sigServerBlocked,this,&MainWindow::serverBlocked);
connect(hashComparer,&HashComparer::sigCallCheck,this,&MainWindow::checkUpdate);
connect(sendSystem,&SendSystem::sigGetXmlAnswer,dataParser,&DataParser::slotGetXmlAnswer);
connect(client,&TCPClient::sigSendDebugLog,this,&MainWindow::debugLog,Qt::AutoConnection);
connect(this,&MainWindow::sigInitializeClient,client,&TCPClient::initialize,Qt::AutoConnection);
connect(this,&MainWindow::sigSetConnect,client,&TCPClient::setConnect,Qt::AutoConnection);
connect(this,&MainWindow::sigSendCommand,client,&TCPClient::slotSendCommand,Qt::AutoConnection);
connect(client,&TCPClient::sigConnectionState,this,&MainWindow::slotConnectionState,Qt::AutoConnection);
connect(client,&TCPClient::sigServerDisconnect,this,&MainWindow::slotServerDisconnect);
connect(this,&MainWindow::sigGetConnected,client,&TCPClient::getIsConnected);
connect(this,&MainWindow::sigCalculateHash,updateController,&UpdateController::calculateCommonHash);
connect(this,&MainWindow::sigSendAutorization,sendSystem,&SendSystem::sendClientAutorization);
}
void MainWindow::updateProgress()
{
filesLoaded++;
float value = 100 / ((float)fileCountForUpdate / filesLoaded);
ui->loadingProgressBar->setValue(value);
}
void MainWindow::loadComplete()
{
externalExecuter->findApp();
ui->updateButton->setEnabled(false);
ui->startButton->setEnabled(true);
autoStart();
ui->inlineTextDebug->setText(tr("Обновление завершено..."));
ui->loadingProgressBar->setValue(100);
}
void MainWindow::setNeedUpdate(bool flag,quint64 size, quint64 fileCount)
{
fileCountForUpdate = 0;
filesLoaded = 0;
fileCountForUpdate = fileCount;
QString availableSizeText;
if (flag){
QString result = tr("Доступно обновление: ") + Tools::convertFileSize(size);
result += tr("Количество файлов: ") + QString::number(fileCount);
ui->inlineTextDebug->setText(result);
}
else
{
ui->inlineTextDebug->setText(tr("Установлена последняя версия"));
ui->loadingProgressBar->setMaximum(100);
ui->loadingProgressBar->setValue(100);
autoStart();
}
ui->updateButton->setEnabled(flag);
ui->startButton->setEnabled(!flag);
}
void MainWindow::lostConnection()
{
ui->loadingProgressBar->setValue(0);
slotConnectionState(false);
}
void MainWindow::serverBlocked()
{
ui->notificationLabel->show();
QPalette palette = ui->notificationLabel->palette();
QColor orangeColor(255,165,0);
palette.setColor(ui->notificationLabel->foregroundRole(),orangeColor);
ui->notificationLabel->setText(tr("Сервер заблокирован"));
ui->notificationLabel->setPalette(palette);
timer->start(3000);
}
void MainWindow::checkLoginResult(ServerAuthorization *serverAuth)
{
if (serverAuth->Result)
{
if (serverAuth->AccessType != "instructor") //временно для отладки загрузки на сервер
{
checkUpdate();
}
ui->updateButton->show();
ui->displayGroupWidget->show();
ui->autostartCheckBox->show();
dataParser->createAuthData(serverAuth);
ui->loginWidget->hide();
}
else {
ui->notificationLabel->setText(tr("Неверный логин/пароль"));
timer->setInterval(3000);
timer->start();
QPalette palette = ui->notificationLabel->palette();
palette.setColor(ui->notificationLabel->foregroundRole(), Qt::red);
ui->notificationLabel->setPalette(palette);
ui->notificationLabel->show();
}
}
void MainWindow::checkAppAvailable()
{
bool isAvailable = externalExecuter->findApp();
ui->startButton->setEnabled(isAvailable);
}
void MainWindow::checkLanguage(QString language)
{
if (language == "RUS")
{
translator.load("QtLanguage_ru_RU",".");
}
else if(language == "ENG")
{
translator.load("QtLanguage_eng_EN",".");
}
qApp->installTranslator(&translator);
ui->retranslateUi(this);
}
void MainWindow::autoStart()
{
if(ui->autostartCheckBox->isChecked()){
on_startButton_clicked();
}
}
void MainWindow::loadStaticData()
{
ServerSettings *currentSettings = dataParser->getServerSettings();
ui->serverInputField->setText(currentSettings->Address);
ui->portInputField->setText(currentSettings->Port);
ui->languageComboBox->setCurrentText(currentSettings->Language);
ui->autostartCheckBox->setChecked(currentSettings->isAutoStart);
checkLanguage(currentSettings->Language);
}
void MainWindow::slotConnectionState(bool flag)
{
ui->notificationLabel->show();
QPalette palette = ui->notificationLabel->palette();
if(flag)
{
palette.setColor(ui->notificationLabel->foregroundRole(),Qt::green);
ui->notificationLabel->setText(tr("Соединение установлено"));
ui->connectButton->hide();
}
else
{
palette.setColor(ui->notificationLabel->foregroundRole(),Qt::red);
ui->notificationLabel->setText(tr("Соединение отсутсвует"));
ui->connectButton->show();
}
ui->notificationLabel->setPalette(palette);
timer->start(3000);
}
void MainWindow::slotServerDisconnect()
{
ui->loadingProgressBar->hide();
ui->updateButton->hide();
ui->displayGroupWidget->hide();
ui->autostartCheckBox->hide();
ui->loginWidget->show();
ui->inlineTextDebug->setText("");
ui->updateButton->setEnabled(false);
slotConnectionState(false);
}
void MainWindow::slotDisableNotify()
{
ui->notificationLabel->hide();
QPalette palette = ui->notificationLabel->palette();
palette.setColor(ui->notificationLabel->foregroundRole(), Qt::black);
ui->notificationLabel->setPalette(palette);
timer->stop();
}
void MainWindow::debugLog(QString message)
{
ui->debugText->append(message);
}
void MainWindow::callUpdateList()
{
updateController->calculateStreamingHash();
hashComparer->setWidget(updateWidget);
QByteArray answer = dataParser->xmlAnswer_notify("GETSERVERDATALIST");
sendSystem->sendXMLAnswer(answer);
updateWidget->initialize(this,updateController);
}
void MainWindow::bindNotifyWidget(UpdateNotifyWidget *widget)
{
updateWidget = widget;
connect(sendSystem,&SendSystem::sigSend,updateWidget,&UpdateNotifyWidget::updateCount);
}
void MainWindow::on_loginButton_clicked()
{
if (!client->getIsConnected())
{
QPalette palette = ui->notificationLabel->palette();
palette.setColor(ui->notificationLabel->foregroundRole(),Qt::red);
ui->notificationLabel->setText(tr("Соединение отсутсвует"));
ui->notificationLabel->setPalette(palette);
ui->notificationLabel->show();
return;
}
QString username = ui->loginInputField->text();
QString password = ui->passwordInputField->text();
ClientAutorization *autorization = new ClientAutorization;
autorization->Login = username;
autorization->Password = password;
dataParser->createAuthMessage(autorization);
emit sigSendAutorization();
}
void MainWindow::on_updateButton_clicked()
{
emit sigSendCommand("update");
ui->updateButton->hide();
ui->loadingProgressBar->setValue(0);
}
void MainWindow::on_startButton_clicked()
{
externalExecuter->callApp();
sendSystem->sendDisable();
}
void MainWindow::on_saveServerButton_clicked()
{
ui->settingsWidget->hide();
ui->loginWidget->show();
if(client->getIsConnected()) return;
QString server = ui->serverInputField->text();
QString port = ui->portInputField->text();
dataParser->createServerSettings(server,port);
emit sigSetConnect(dataParser->getServerSettings(),connectionThread);
}
void MainWindow::on_settingsButton_clicked()
{
ui->settingsWidget->show();
ui->loginWidget->hide();
}
void MainWindow::on_connectButton_clicked()
{
emit sigSetConnect(dataParser->getServerSettings(),connectionThread);
}
void MainWindow::on_languageComboBox_activated(const QString &arg1)
{
qDebug() << arg1;
dataParser->saveClientSettrings(arg1,ui->autostartCheckBox->isChecked());
checkLanguage(arg1);
ui->retranslateUi(this);
}
void MainWindow::checkUpdate()
{
ui->loadingProgressBar->setValue(0);
ui->loadingProgressBar->show();
emit sigSendCommand("check");
ui->inlineTextDebug->setText(tr("Проверка обновлений..."));
}
void MainWindow::painting()
{
// QPixmap background(":/resource/SSJ-100.jpg");
// QColor color(77,77,77,255);
// background.scaled(this->size(),Qt::IgnoreAspectRatio);
// QPalette palette;
// palette.setBrush(QPalette::Window,background);
// palette.dark();
// this->setPalette(palette);
QFontDatabase::addApplicationFont(":/Fonts/Kanit Cyrillic.ttf");
}
MainWindow::~MainWindow()
{
connectionThread->quit();
connectionThread->wait();
sendSystem->sendDisable();
delete connectionThread;
delete ui;
}