82 lines
3.3 KiB
C++
82 lines
3.3 KiB
C++
#include "LoaderWindow.h"
|
||
#include <QVBoxLayout>
|
||
#include <QLabel>
|
||
#include <QThread>
|
||
#include <QApplication>
|
||
|
||
#include "DesktopSelectionWindow.h"
|
||
#include "net/NetworkService.h"
|
||
|
||
LoaderWindow::LoaderWindow(QWidget *parent) : QWidget(parent) {
|
||
setWindowTitle("Проверка приложения");
|
||
setFixedSize(300, 100);
|
||
|
||
QVBoxLayout *layout = new QVBoxLayout(this);
|
||
|
||
QLabel *label = new QLabel("Пожалуйста, подождите...");
|
||
layout->addWidget(label);
|
||
|
||
progressBar = new QProgressBar();
|
||
progressBar->setRange(0, 100);
|
||
layout->addWidget(progressBar);
|
||
|
||
timer = new QTimer(this);
|
||
connect(timer, &QTimer::timeout, this, &LoaderWindow::updateProgress);
|
||
timer->start(25);
|
||
|
||
auto *thread = new QThread;
|
||
auto *networkService = new NetworkService;
|
||
networkService->moveToThread(thread);
|
||
|
||
connect(thread, &QThread::started, networkService, &NetworkService::loginAndSetup);
|
||
connect(networkService, &NetworkService::finished, thread, &QThread::quit);
|
||
connect(networkService, &NetworkService::finished, networkService, &QObject::deleteLater);
|
||
connect(thread, &QThread::finished, thread, &QThread::deleteLater);
|
||
|
||
connect(networkService, &NetworkService::finishedWithResult, this, [this](const int result) {
|
||
m_checkResult = result;
|
||
if (m_checkResult == -1) {
|
||
if (timer) {
|
||
timer->stop();
|
||
}
|
||
emit loadingFinished(m_checkResult);
|
||
} else if (progressBar->value() >= 100) {
|
||
emit loadingFinished(m_checkResult);
|
||
}
|
||
}, Qt::QueuedConnection);
|
||
|
||
// На сервере есть десктопы, но локального выбора нет — показываем окно выбора.
|
||
// loginAndSetup продолжится в continueWithDesktop() после выбора пользователя.
|
||
connect(networkService, &NetworkService::desktopSelectionRequired, this,
|
||
[this, networkService](const QJsonArray &desktops) {
|
||
auto *dialog = new DesktopSelectionWindow(desktops);
|
||
dialog->setAttribute(Qt::WA_DeleteOnClose);
|
||
// Контекст this (UI-поток): слот не должен исполняться в воркер-потоке
|
||
// networkService — любые UI-операции вне main thread на macOS падают.
|
||
connect(dialog, &DesktopSelectionWindow::desktopChosen, this,
|
||
[networkService](const QString &id) {
|
||
QMetaObject::invokeMethod(networkService, "continueWithDesktop",
|
||
Qt::QueuedConnection, Q_ARG(QString, id));
|
||
});
|
||
connect(dialog, &DesktopSelectionWindow::cancelled, qApp, &QApplication::quit);
|
||
dialog->show();
|
||
}, Qt::QueuedConnection);
|
||
|
||
thread->start();
|
||
}
|
||
|
||
void LoaderWindow::updateProgress() {
|
||
const int value = progressBar->value();
|
||
if (value < 99) {
|
||
progressBar->setValue(value + 1);
|
||
} else if (m_checkResult != 1000) {
|
||
// Результат получен — завершаем
|
||
progressBar->setValue(100);
|
||
if (timer) {
|
||
timer->stop();
|
||
}
|
||
emit loadingFinished(m_checkResult);
|
||
}
|
||
// Иначе ждём на 99 пока сеть не ответит
|
||
}
|