-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaymentdialog.cpp
127 lines (107 loc) · 2.68 KB
/
paymentdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "paymentdialog.h"
#include "ui_paymentdialog.h"
#include "config.h"
#include "debug.h"
PaymentDialog::PaymentDialog(const JConfig &conf, QWidget *parent) :
QDialog(parent),
ui(new Ui::PaymentDialog)
{
ui->setupUi(this);
m_currency = conf["global"]["currency"].toString(DEF_CURRENCY);
m_helperFile = conf["global"]["helper"].toPath(DEF_HELPER);
m_helperParams << "acceptor"
<< conf["acceptor"]["device"].toString()
<< conf["acceptor"]["baudrate"].toString(PD_ACCEPTOR_DEF_BAUDRATE)
<< conf["acceptor"]["timeout"].toString(PD_ACCEPTOR_DEF_TIMEOUT);
m_helper = new QProcess();
m_helperState = HelperCreated;
connect(m_helper, SIGNAL(readyReadStandardOutput()), SLOT(helperRead()));
connect(m_helper, SIGNAL(started()), SLOT(helperStarted()));
connect(m_helper, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(helperFinished()));
}
PaymentDialog::~PaymentDialog()
{
delete m_helper;
delete ui;
}
void PaymentDialog::open()
{
ui->closePb->setEnabled(false);
ui->payPb->setEnabled(false);
ui->paidLb->setText("0 " + m_currency);
m_paid = 0;
m_helper->start(m_helperFile, m_helperParams);
m_helperState = HelperStarting;
if (!m_helper->waitForStarted(PD_HELPER_START_TIMEOUT))
{
close();
return;
}
}
void PaymentDialog::paid(int amount)
{
if (m_paid == 0)
{
ui->closePb->setEnabled(false);
ui->payPb->setEnabled(true);
}
m_paid = amount;
ui->paidLb->setText(QString::number(m_paid) + " " + m_currency);
}
void PaymentDialog::close()
{
HelperState state = m_helperState;
if (state == HelperTerminating)
return;
m_helperState = HelperTerminating;
if (state & (HelperStarting | HelperRunning))
{
ui->closePb->setEnabled(false);
ui->payPb->setEnabled(false);
m_helper->terminate();
m_helper->waitForFinished(PD_HELPER_STOP_TIMEOUT);
}
}
void PaymentDialog::helperRead()
{
QString input, event;
Json data;
int code;
while (m_helper->canReadLine())
{
input = m_helper->readLine();
data.parse(input);
#ifdef DEBUG
dbg << "\t[ " << input << " ]: " << data.dump() << "(" + QString::number(data.error()) + ")";
#endif
if (data.error() != Json::ErrorNone)
continue;
if ((code = data["code"].toInt()) == 0)
{
if ((event = data["event"].toString()) == "received")
paid(data["amount"].toInt());
else if (event == "started")
showFullScreen();
else if (event == "finished")
{
emit credit(m_paid);
hide();
m_helperState = HelperTerminating;
}
}
else
close();
}
}
void PaymentDialog::helperStarted()
{
if (m_helperState == HelperRunning)
return;
m_helperState = HelperRunning;
ui->closePb->setEnabled(true);
}
void PaymentDialog::helperFinished()
{
close();
m_helperState = HelperFinished;
}