From 2b2127e3b5050117ed1de5380b27597c0f268865 Mon Sep 17 00:00:00 2001 From: leca Date: Sat, 15 Mar 2025 00:48:46 +0300 Subject: [PATCH] restructure, change onDataDecode signal and succesfully parsing http request --- adjustpicturedialog.cpp | 8 ++++++- adjustpicturedialog.h | 2 +- ofdscene.cpp | 50 +++++++++++++++++++++++++++-------------- ofdscene.h | 3 ++- translations/en_US.ts | 22 +++++++++--------- translations/ru_RU.ts | 22 +++++++++--------- 6 files changed, 65 insertions(+), 42 deletions(-) diff --git a/adjustpicturedialog.cpp b/adjustpicturedialog.cpp index 7709665..d7d52f8 100644 --- a/adjustpicturedialog.cpp +++ b/adjustpicturedialog.cpp @@ -41,7 +41,13 @@ void AdjustPictureDialog::accept() { infoDialog.setWindowTitle(tr("No QR code")); infoDialog.exec(); } else { - emit decodedData(result); + std::map paramsMap; + std::vector dataSplit = split(result, "&"); + for (std::string &pair : dataSplit) { + std::vector values = split(pair, "="); + paramsMap.insert(std::pair(values[0], values[1])); + } + emit decodedData(paramsMap); QDialog::accept(); } diff --git a/adjustpicturedialog.h b/adjustpicturedialog.h index b3feef9..b9d74a5 100644 --- a/adjustpicturedialog.h +++ b/adjustpicturedialog.h @@ -22,7 +22,7 @@ public: void computeContrastLookupTable(); std::vector contrastLUT[100]; signals: - void decodedData(std::string data); + void decodedData(std::map data); private slots: diff --git a/ofdscene.cpp b/ofdscene.cpp index 88b647a..238174e 100644 --- a/ofdscene.cpp +++ b/ofdscene.cpp @@ -13,6 +13,8 @@ #include +#include + OFDScene::OFDScene(QWidget *parent) : QWidget(parent) , ui(new Ui::OFDScene) { @@ -46,8 +48,20 @@ void OFDScene::startHttpServer() { httplib::Server svr; - svr.Get("/", [&](const httplib::Request &, httplib::Response &res){ - res.set_redirect("http://"+ localIp +":"+ std::to_string(port) +"/", 301); + svr.Get("/", [&](const httplib::Request &req, httplib::Response &res){ + if (req.params.size() < 6) { + res.set_redirect("binaryeye://scan/?ret=http://"+ localIp +":"+ std::to_string(port) +"/", 301); + std::cerr << "Too few params: " << req.params.size() << std::endl; + return; + } + std::map paramsMap; + for (auto ¶m : req.params) { + paramsMap.insert(std::pair(param.first, param.second)); + } + + emit onDataDecode(paramsMap); + + res.set_redirect("binaryeye://scan/?ret=http://"+ localIp +":"+ std::to_string(port) +"/", 301); }); number_of_retries ++; @@ -79,22 +93,26 @@ void OFDScene::on_choose_image_button_clicked() { dialog.exec(); } -void OFDScene::onDataDecode(std::string data) { - std::vector dataSplit = split(data, "&"); +void OFDScene::onDataDecode(std::map data) { + // std::vector dataSplit = split(data, "&"); - ui->fn_line_edit->setText(QString::fromStdString(split(dataSplit[2], "=")[1])); - ui->fd_line_edit->setText(QString::fromStdString(split(dataSplit[3], "=")[1])); - ui->fi_line_edit->setText(QString::fromStdString(split(dataSplit[4], "=")[1])); - QString extractedDateTime = QString::fromStdString(split(dataSplit[0], "=")[1]); + ui->fn_line_edit->setText(QString::fromStdString(data["fn"])); + ui->fd_line_edit->setText(QString::fromStdString(data["i"])); + ui->fi_line_edit->setText(QString::fromStdString(data["fp"])); + + QString extractedDateTime = QString::fromStdString(data["t"]); //TODO: some QRs contain datetime in format yyyyMMddThhmmss. Perhaps there is more different formats, should write function to detect them. QDateTime datetime = QDateTime::fromString(extractedDateTime, "yyyyMMddThhmm"); + if (datetime == QDateTime::fromString(extractedDateTime, "20000101T1200")) { + datetime = QDateTime::fromString(extractedDateTime, "yyyyMMddThhmmss"); + } ui->purchase_datetime_edit->setDateTime(datetime); - int type = std::stoi(split(dataSplit[5], "=")[1]); + int type = std::stoi(data["n"]); ui->operation_type_combo_box->setCurrentIndex(type - 1); - std::string total = split(dataSplit[1], "=")[1]; + std::string total = data["s"]; ui->total_spin_box->setValue(std::stod(total)); } @@ -158,7 +176,6 @@ void OFDScene::on_parse_button_clicked() { OutputDialog d = OutputDialog(this, check); d.exec(); } - } @@ -167,12 +184,11 @@ void OFDScene::on_binary_eye_button_clicked() { } void OFDScene::notifyHttpServerFailure() { - QMessageBox *infoDialog = new QMessageBox(); - infoDialog->setText(tr("Could not start http server. 10 times in a row random port was occupied. Either you should run for a lottery ticket, or the problem is in the program. If the lottery ticket wasn't lucky, please, contact the developer.")); - infoDialog->setIcon(QMessageBox::Warning); - infoDialog->setWindowTitle(tr("Could not start http server.")); - infoDialog->exec(); - delete infoDialog; + QMessageBox infoDialog = QMessageBox(); + infoDialog.setText(tr("Could not start http server. 10 times in a row random port was occupied. Either you should run for a lottery ticket, or the problem is in the program. If the lottery ticket wasn't lucky, please, contact the developer.")); + infoDialog.setIcon(QMessageBox::Warning); + infoDialog.setWindowTitle(tr("Could not start http server.")); + infoDialog.exec(); } unsigned int OFDScene::getPort() { diff --git a/ofdscene.h b/ofdscene.h index 13bb918..0b8e019 100644 --- a/ofdscene.h +++ b/ofdscene.h @@ -20,7 +20,7 @@ public: unsigned int getPort(); private slots: void on_choose_image_button_clicked(); - void onDataDecode(std::string data); + void onDataDecode(std::map); void on_parse_button_clicked(); @@ -28,6 +28,7 @@ private slots: void notifyHttpServerFailure(); signals: + void httpErrorOccured(); private: diff --git a/translations/en_US.ts b/translations/en_US.ts index 317dd12..6a34cdd 100644 --- a/translations/en_US.ts +++ b/translations/en_US.ts @@ -367,57 +367,57 @@ Parse - + Could not start http server. 10 times in a row random port was occupied. Either you should run for a lottery ticket, or the problem is in the program. If the lottery ticket wasn't lucky, please, contact the developer. - + Could not start http server. - + Please, select a picture where QR code that contains info about check is present Please, select a picture where QR code that contains info about check is present - + Picture was not selected Picture was not selected - + Selected image: Selected image: - + Captcha was not solved correctly! Captcha was not solved correctly! - + Captcha is incorrect Captcha is incorrect - + Internal server error. Please, try again later. Internal server error. Please, try again later. - + Internal server error Internal server error - + Check not found. Please, ensure correctness of entered data. Check not found. Please, ensure correctness of entered data. - + Check was not found Check was not found diff --git a/translations/ru_RU.ts b/translations/ru_RU.ts index 7b1fd6d..06dd4a5 100644 --- a/translations/ru_RU.ts +++ b/translations/ru_RU.ts @@ -367,57 +367,57 @@ Парсить - + Could not start http server. 10 times in a row random port was occupied. Either you should run for a lottery ticket, or the problem is in the program. If the lottery ticket wasn't lucky, please, contact the developer. - + Could not start http server. - + Please, select a picture where QR code that contains info about check is present Пожалуйста, выберете изображение, содержащее QR код с информацией о чеке - + Picture was not selected Изображение не было выбрано - + Selected image: Выбранное изображение: - + Captcha was not solved correctly! Капча была решена неверно! - + Captcha is incorrect Капча введена неверно - + Internal server error. Please, try again later. Внутренняя ошибка сервера. Пожалуйста, попробуйте снова позже. - + Internal server error Внутренняя ошибка сервера - + Check not found. Please, ensure correctness of entered data. Чек не найден. Пожалуйста, убедитесь в правильности введённых данных. - + Check was not found Чек не найден