100 lines
4.4 KiB
C++
100 lines
4.4 KiB
C++
#include "RegistrationWindow.h"
|
||
#include <QVBoxLayout>
|
||
#include <QLabel>
|
||
#include <QLineEdit>
|
||
#include <QPushButton>
|
||
#include <QApplication>
|
||
#include <QDebug>
|
||
#include <QMessageBox>
|
||
#include <QThread>
|
||
#include <QSettings>
|
||
|
||
#include "DesktopSelectionWindow.h"
|
||
#include "LoaderDialog.h"
|
||
#include "dao/SettingsDAO.h"
|
||
#include "net/NetworkService.h"
|
||
|
||
RegistrationWindow::RegistrationWindow(QWidget *parent): QWidget(parent) {
|
||
setWindowTitle("Вход");
|
||
resize(300, 130);
|
||
|
||
auto *layout = new QVBoxLayout(this);
|
||
|
||
layout->addWidget(new QLabel("Введите ваш идентификатор:"));
|
||
|
||
QLineEdit *apiKeyEdit = new QLineEdit();
|
||
apiKeyEdit->setPlaceholderText("Идентификатор");
|
||
layout->addWidget(apiKeyEdit);
|
||
|
||
QHBoxLayout *buttonLayout = new QHBoxLayout();
|
||
|
||
QPushButton *loginButton = new QPushButton("Войти");
|
||
QPushButton *closeButton = new QPushButton("Закрыть");
|
||
|
||
buttonLayout->addWidget(loginButton);
|
||
buttonLayout->addWidget(closeButton);
|
||
|
||
layout->addLayout(buttonLayout);
|
||
|
||
connect(closeButton, &QPushButton::clicked, qApp, &QApplication::quit);
|
||
connect(loginButton, &QPushButton::clicked, this, [=]() {
|
||
const QString apiKey = apiKeyEdit->text().trimmed();
|
||
if (apiKey.isEmpty()) {
|
||
QMessageBox::warning(this, "Ошибка", "Введите ваш идентификатор");
|
||
return;
|
||
}
|
||
|
||
SettingsDAO::set("api_key", apiKey);
|
||
|
||
auto *loader = new LoaderDialog(this);
|
||
loader->show();
|
||
|
||
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, [=](int result) {
|
||
QMetaObject::invokeMethod(this, [=]() {
|
||
loader->close();
|
||
loader->deleteLater();
|
||
|
||
if (result == 1) {
|
||
emit onRegisteredSuccess();
|
||
} else if (result == 0) {
|
||
SettingsDAO::remove("api_key");
|
||
QMessageBox::critical(this, "Ошибка входа", "Неверный идентификатор. Проверьте и попробуйте снова.");
|
||
} else {
|
||
QMessageBox::critical(this, "Ошибка подключения",
|
||
"Нет подключения к интернету или сервер не отвечает.\n\nПожалуйста, попробуйте позже.");
|
||
}
|
||
}, Qt::QueuedConnection);
|
||
});
|
||
|
||
// На сервере есть десктопы, но локального выбора нет — показываем окно выбора.
|
||
// loginAndSetup продолжится в continueWithDesktop() после выбора пользователя.
|
||
connect(networkService, &NetworkService::desktopSelectionRequired, this,
|
||
[this, networkService, loader](const QJsonArray &desktops) {
|
||
loader->close();
|
||
auto *dialog = new DesktopSelectionWindow(desktops, this);
|
||
dialog->setAttribute(Qt::WA_DeleteOnClose);
|
||
// Контекст this (UI-поток): loader->show() обязан выполняться
|
||
// в главном потоке, иначе создание NSWindow вне main thread → краш.
|
||
connect(dialog, &DesktopSelectionWindow::desktopChosen, this,
|
||
[networkService, loader](const QString &id) {
|
||
loader->show();
|
||
QMetaObject::invokeMethod(networkService, "continueWithDesktop",
|
||
Qt::QueuedConnection, Q_ARG(QString, id));
|
||
});
|
||
connect(dialog, &DesktopSelectionWindow::cancelled, qApp, &QApplication::quit);
|
||
dialog->show();
|
||
}, Qt::QueuedConnection);
|
||
|
||
thread->start();
|
||
});
|
||
}
|