You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.4 KiB
C++

#include "eetresult.h"
#include "eettemplate.h"
#include <QFile>
#include <QTextStream>
EetTemplate::EetTemplate(QObject *parent) : QObject(parent)
{
m_result = nullptr;
}
EetTemplate::EetTemplate(const QString &tmp, QObject *parent) : QObject(parent)
{
QFile tmpFile(tmp);
tmpFile.open(QFile::ReadOnly | QFile::Text);
QTextStream stream(&tmpFile);
m_template = stream.readAll();
m_result = nullptr;
}
QString EetTemplate::fillTemplate(EetRequest *request)
{
m_template = fillTemplateInternal(request, nullptr);
QString pkp = getPkpString(request);
QByteArray signedPkp = m_signer->signData(pkp.toUtf8());
QByteArray sha1Bkp = m_signer->sha1HashData(signedPkp);
QString base64Sign(signedPkp.toBase64());
QString base16bkp(sha1Bkp.toHex());
QString splitedBkp;
for (int i = 0; i < base16bkp.length(); i++)
{
if (i > 0 && i % 8 == 0)
{
splitedBkp += "-";
}
splitedBkp += base16bkp[i];
}
if (m_result != nullptr)
{
m_result->setPkp(base64Sign);
m_result->setBkp(splitedBkp.toUpper());
}
QMap<QString, QString> varMap;
varMap["pkp"] = base64Sign;
varMap["bkp"] = splitedBkp.toUpper();
return fillTemplate(varMap);
}
QString EetTemplate::fillTemplate(QMap<QString, QString> map)
{
return fillTemplateInternal(nullptr, &map);
}
QString EetTemplate::fillTemplateInternal(EetRequest *request, QMap<QString, QString> *map)
{
QString ret;
QString phStart;
if (request != nullptr)
{
phStart = "@{";
}
else
{
phStart = "${";
}
for (int i = 0; i < m_template.size(); i++)
{
QChar c = m_template[i];
if (!m_template.mid(i).startsWith(phStart))
{
if ((i > 0 && c == ' ' && m_template[i - 1] == ' '))
{
continue;
}
else
{
ret += c;
continue;
}
}
int p = m_template.indexOf("}", i + 2);
if (p < 0)
{
return ret;
}
QString var = m_template.mid(i + 2, p - i - 2);
QString value;
if (request != nullptr)
{
value = request->property(var.toStdString().c_str()).toString();
}
else
{
value = map->value(var, NULL_VAL);
}
if (value != NULL_VAL)
{
QString attrib;
if (request != NULL)
{
attrib = QString("%1=\"%2\"").arg(var, value);
}
else
{
attrib = value;
}
ret += attrib;
}
i = p;
}
return ret.replace(QRegExp("\" >"), "\">");
}
QString EetTemplate::getPkpString(EetRequest *request)
{
QString ret = QString("%1|%2|%3|%4|%5|%6").arg(request->property("dic_popl").toString(),
request->property("id_provoz").toString(),
request->property("id_pokl").toString(),
request->property("porad_cis").toString(),
request->property("dat_trzby").toString(),
request->property("celk_trzba").toString());
return ret;
}
void EetTemplate::setResult(EetResult *result)
{
m_result = result;
}