added ofd scene, working http server
This commit is contained in:
138
ofdscene.cpp
138
ofdscene.cpp
@@ -1,5 +1,17 @@
|
||||
#include "ofdscene.h"
|
||||
#include "ui_ofdscene.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
#include <QFileDialog>
|
||||
#include <QMessageBox>
|
||||
#include <adjustpicturedialog.h>
|
||||
#include <httplib.h>
|
||||
#include <outputdialog.h>
|
||||
#include <solvecaptchadialog.h>
|
||||
|
||||
#include <net/net.h>
|
||||
|
||||
#include <exceptions/ofdrequestexception.h>
|
||||
|
||||
OFDScene::OFDScene(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
@@ -10,3 +22,129 @@ OFDScene::OFDScene(QWidget *parent)
|
||||
OFDScene::~OFDScene() {
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void OFDScene::startHttpServer() {
|
||||
std::string localIp = "";
|
||||
try {
|
||||
localIp = get_local_ip_address();
|
||||
} catch(std::exception e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
httplib::Server svr;
|
||||
//TODO: generate random port from 1024 to 65535 and check if its used.
|
||||
svr.Get("/", [&](const httplib::Request &, httplib::Response &res){
|
||||
res.set_redirect("http://"+ localIp +":8080/", 301);
|
||||
});
|
||||
|
||||
svr.listen("0.0.0.0", 8080);
|
||||
}
|
||||
|
||||
void OFDScene::on_choose_image_button_clicked() {
|
||||
QString filename = QFileDialog::getOpenFileName();
|
||||
|
||||
if (filename == "") {
|
||||
QMessageBox infoDialog;
|
||||
infoDialog.setText(tr("Please, select a picture where QR code that contains info about check is present"));
|
||||
infoDialog.setIcon(QMessageBox::Critical);
|
||||
infoDialog.setWindowTitle(tr("Picture was not selected"));
|
||||
infoDialog.exec();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
ui->info_label->setText(tr("Selected image: ") + filename);
|
||||
|
||||
AdjustPictureDialog dialog = AdjustPictureDialog(this, filename.toStdString());
|
||||
connect(&dialog, &AdjustPictureDialog::decodedData, this, &OFDScene::onDataDecode);
|
||||
dialog.exec();
|
||||
}
|
||||
|
||||
void OFDScene::onDataDecode(std::string data) {
|
||||
std::vector<std::string> 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]);
|
||||
//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");
|
||||
ui->purchase_datetime_edit->setDateTime(datetime);
|
||||
|
||||
int type = std::stoi(split(dataSplit[5], "=")[1]);
|
||||
ui->operation_type_combo_box->setCurrentIndex(type - 1);
|
||||
|
||||
std::string total = split(dataSplit[1], "=")[1];
|
||||
|
||||
ui->total_spin_box->setValue(std::stod(total));
|
||||
}
|
||||
|
||||
|
||||
void OFDScene::on_parse_button_clicked() {
|
||||
Net net;
|
||||
net.get_captcha_from_ofdru();
|
||||
|
||||
std::string solved_captcha = "";
|
||||
bool success = true;
|
||||
bool is_captcha_solved = true;
|
||||
Check check;
|
||||
|
||||
do {
|
||||
SolveCaptchaDialog dialog = SolveCaptchaDialog(this, &solved_captcha);
|
||||
dialog.exec();
|
||||
is_captcha_solved = true;
|
||||
|
||||
try {
|
||||
std::string check_content = net.fetch_check_data_from_ofdru(
|
||||
ui->fn_line_edit->text().toStdString(),
|
||||
ui->fd_line_edit->text().toStdString(),
|
||||
ui->fi_line_edit->text().toStdString(),
|
||||
ui->purchase_datetime_edit->dateTime().toString(Qt::ISODate).toStdString(),
|
||||
ui->operation_type_combo_box->currentIndex() + 1,
|
||||
// In the request to ofd.ru, total is in a format with 2 last digits represent decimal part of a number.
|
||||
ui->total_spin_box->text().toDouble() * 100,
|
||||
solved_captcha);
|
||||
|
||||
check = parseOfdRuAnswer(check_content);
|
||||
} catch(OfdRequestException e) {
|
||||
success = false;
|
||||
if (!strcmp(e.what(), "Incorrect captcha")) {
|
||||
is_captcha_solved = false;
|
||||
QMessageBox infoDialog;
|
||||
infoDialog.setText(tr("Captcha was not solved correctly!"));
|
||||
infoDialog.setIcon(QMessageBox::Critical);
|
||||
infoDialog.setWindowTitle(tr("Captcha is incorrect"));
|
||||
infoDialog.exec();
|
||||
break;
|
||||
} else if (!strcmp(e.what(), "Internal server error")) {
|
||||
QMessageBox infoDialog;
|
||||
infoDialog.setText(tr("Internal server error. Please, try again later."));
|
||||
infoDialog.setIcon(QMessageBox::Critical);
|
||||
infoDialog.setWindowTitle(tr("Internal server error"));
|
||||
infoDialog.exec();
|
||||
return;
|
||||
} else if (!strcmp(e.what(), "Does not exist")) {
|
||||
QMessageBox infoDialog;
|
||||
infoDialog.setText(tr("Check not found. Please, ensure correctness of entered data."));
|
||||
infoDialog.setIcon(QMessageBox::Critical);
|
||||
infoDialog.setWindowTitle(tr("Check was not found"));
|
||||
infoDialog.exec();
|
||||
return;
|
||||
}
|
||||
}
|
||||
} while (!is_captcha_solved);
|
||||
|
||||
if (success) {
|
||||
OutputDialog d = OutputDialog(this, check);
|
||||
d.exec();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void OFDScene::on_binary_eye_button_clicked() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user