@@ -18,11 +18,21 @@ MainWindow::MainWindow(QWidget *parent)
1818 this ->validator = new QRegularExpressionValidator (QRegularExpression (" [A-Za-z0-9_]+" ), this );
1919 this ->ui ->reg_login ->setValidator (this ->validator );
2020 this ->ui ->reg_pas ->setValidator (this ->validator );
21- QFile file (" :/connection.txt" );
21+
22+ if (!fileExists (QDir::currentPath () + " /connection.txt" ))
23+ {
24+ QFile file (" connection.txt" );
25+ file.open (QIODevice::WriteOnly | QIODevice::Text);
26+ file.write (" localhost\n " );
27+ file.write (" 20002" );
28+ file.close ();
29+ }
30+ QFile file (" connection.txt" );
2231 file.open (QIODevice::ReadOnly | QIODevice::Text);
2332 this ->host = file.readLine ();
24- this ->host = this ->host .sliced ( 0 , this -> host . size () - 1 );
33+ this ->host = this ->host .simplified ( );
2534 this ->port = file.readLine ().toInt ();
35+ file.close ();
2636
2737 QDir dir1 (QDir::currentPath ());
2838 dir1.mkdir (" images" );
@@ -40,6 +50,7 @@ MainWindow::MainWindow(QWidget *parent)
4050 this ->ui ->screen_stacked ->setCurrentIndex ((int )screen::login);
4151 this ->forward = type_forward::login;
4252
53+ this ->is_auth = false ;
4354 this ->socket .reset (new QTcpSocket ());
4455 socket->connectToHost (this ->host , this ->port );
4556 connect (socket.get (), SIGNAL (connected ()), SLOT (slotConnected ()));
@@ -48,6 +59,7 @@ MainWindow::MainWindow(QWidget *parent)
4859 }
4960 else
5061 {
62+ this ->is_auth = true ;
5163 this ->db = QSqlDatabase::addDatabase (" QSQLITE" );
5264
5365
@@ -66,6 +78,12 @@ MainWindow::MainWindow(QWidget *parent)
6678
6779MainWindow::~MainWindow ()
6880{
81+ QFile file (" connection.txt" );
82+ if (!file.open (QIODevice::WriteOnly | QIODevice::Text))
83+ qDebug () << " not open to write!" ;
84+ file.write (QString (this ->host + " \n " ).toUtf8 ());
85+ file.write (QString::number (this ->port ).toUtf8 ());
86+ file.close ();
6987 delete ui;
7088}
7189
@@ -228,6 +246,9 @@ void MainWindow::recv_data_handler(const QString &data)
228246 this ->create_db ();
229247 this ->fill_db_login (doc.FirstChildElement (" body" ));
230248 this ->fill_personal_area ();
249+ this ->is_auth = true ;
250+
251+ this ->socket .reset ();
231252 }
232253 else
233254 {
@@ -248,6 +269,8 @@ void MainWindow::recv_data_handler(const QString &data)
248269
249270 this ->base64_file = " " ;
250271 this ->file_type = " " ;
272+ this ->is_auth = true ;
273+ this ->socket .reset ();
251274 }
252275 else
253276 {
@@ -258,13 +281,6 @@ void MainWindow::recv_data_handler(const QString &data)
258281
259282 case (type_forward::load_result):
260283 {
261- if (!data.size ())
262- {
263- this ->popup ->set_title (" Успешно" );
264- this ->popup ->set_description (" Данные успешно скачаны с сервера! Пройдено тестов: 0" );
265- this ->popup ->exec ();
266- break ;
267- }
268284 auto type = doc.FirstChildElement (" body" )->FirstChildElement (" answer" );
269285 if (type->GetText () == QString (" correct" ))
270286 {
@@ -274,6 +290,7 @@ void MainWindow::recv_data_handler(const QString &data)
274290 this ->popup ->set_title (" Успешно" );
275291 this ->popup ->set_description (" Данные успешно скачаны с сервера!" );
276292 this ->popup ->exec ();
293+ this ->socket .reset ();
277294 }
278295 else
279296 {
@@ -292,6 +309,7 @@ void MainWindow::recv_data_handler(const QString &data)
292309 this ->popup ->set_title (" Успешно" );
293310 this ->popup ->set_description (" Данные успешно загружены на сервер!" );
294311 this ->popup ->exec ();
312+ this ->socket .reset ();
295313 }
296314 else
297315 {
@@ -310,6 +328,7 @@ void MainWindow::recv_data_handler(const QString &data)
310328 this ->popup ->set_title (" Успешно" );
311329 this ->popup ->set_description (" Список паттернов обновлен!" );
312330 this ->popup ->exec ();
331+ this ->socket .reset ();
313332 break ;
314333 }
315334 }
@@ -956,6 +975,7 @@ void MainWindow::on_reg_img_clicked()
956975
957976void MainWindow::on_quit_clicked ()
958977{
978+ this ->is_auth = false ;
959979 this ->quit ();
960980}
961981
@@ -1017,3 +1037,43 @@ void MainWindow::on_to_pattern_list_clicked()
10171037{
10181038 this ->ui ->screen_stacked ->setCurrentIndex ((int )screen::patterns);
10191039}
1040+
1041+ void MainWindow::on_to_settings_clicked ()
1042+ {
1043+ this ->ui ->screen_stacked ->setCurrentIndex ((int )screen::settings);
1044+
1045+ this ->ui ->adress ->setText (this ->host );
1046+ this ->ui ->port ->setValue (this ->port );
1047+ }
1048+
1049+ void MainWindow::on_to_back_clicked ()
1050+ {
1051+ this ->ui ->screen_stacked ->setCurrentIndex ((int )(this ->is_auth ?screen::personal_area : screen::login));
1052+ }
1053+
1054+ void MainWindow::on_settings_save_clicked ()
1055+ {
1056+ this ->host = this ->ui ->adress ->text ();
1057+ this ->port = this ->ui ->port ->value ();
1058+
1059+ if (this ->socket )
1060+ {
1061+ this ->ui ->login_2 ->setEnabled (false );
1062+ this ->ui ->register_2 ->setEnabled (false );
1063+ this ->socket .reset (new QTcpSocket ());
1064+ socket->connectToHost (this ->host , this ->port );
1065+ connect (socket.get (), SIGNAL (connected ()), SLOT (slotConnected ()));
1066+ connect (socket.get (), SIGNAL (readyRead ()), SLOT (slotReadyRead ()));
1067+ connect (socket.get (), SIGNAL (errorOccurred (QAbstractSocket::SocketError)), this , SLOT (slotError (QAbstractSocket::SocketError)));
1068+ }
1069+ }
1070+
1071+ void MainWindow::on_to_settings_register_clicked ()
1072+ {
1073+ this ->on_to_settings_clicked ();
1074+ }
1075+
1076+ void MainWindow::on_to_settings_login_clicked ()
1077+ {
1078+ this ->on_to_settings_clicked ();
1079+ }
0 commit comments