174 lines
4.9 KiB
C++
Executable File
174 lines
4.9 KiB
C++
Executable File
|
|
|
|
|
|
|
|
#include "lts_licence.h"
|
|
#include <QDomDocument>
|
|
#include <NGLLogger/NGLLoggerBootstrap.h>
|
|
#include <QObject>
|
|
|
|
namespace LtsLicence
|
|
{
|
|
static bool extractLicence(const QDomElement &licenceElement, QVector<Licence> &licenceVec);
|
|
static NGL::INGLLogger &getLogger()
|
|
{
|
|
return NGL::NGLLoggerBootstrap::getNGLRootLogger().getNGLLogger("LtsLicence");
|
|
}
|
|
|
|
QVector<Licence> readLicences(const QByteArray &unencryptedXmlData)
|
|
{
|
|
QVector<Licence> licenceVec;
|
|
|
|
QDomDocument xmlDoc;
|
|
QString errorMsg;
|
|
int errorLine = 0;
|
|
int errorColumn = 0;
|
|
|
|
if (!xmlDoc.setContent(unencryptedXmlData, &errorMsg, &errorLine, &errorColumn))
|
|
{
|
|
ERROR_LOG(getLogger()) << "Error reading xml data, " << "error in line " << errorLine
|
|
<< ", column " << errorColumn
|
|
<< ", message: " << errorMsg;
|
|
return licenceVec;
|
|
}
|
|
|
|
INFO_LOG(getLogger()) << "Try to extract licences";
|
|
|
|
QDomElement licenceElement = xmlDoc.documentElement().firstChildElement(QLatin1String("licence"));
|
|
|
|
while (!licenceElement.isNull())
|
|
{
|
|
if (!extractLicence(licenceElement, licenceVec))
|
|
{
|
|
licenceVec.clear();
|
|
return licenceVec;
|
|
}
|
|
|
|
licenceElement = licenceElement.nextSiblingElement(QLatin1String("licence"));
|
|
}
|
|
return licenceVec;
|
|
}
|
|
|
|
|
|
QByteArray writeUnencryptedLicences(const QVector<Licence> &licences)
|
|
{
|
|
QDomDocument xmlDoc;
|
|
|
|
QDomProcessingInstruction instr = xmlDoc.createProcessingInstruction(QLatin1String("xml"),
|
|
QLatin1String("version='1.0' encoding='UTF-8' standalone='yes'"));
|
|
xmlDoc.appendChild(instr);
|
|
|
|
QDomElement root = xmlDoc.createElement(QLatin1String("licences"));
|
|
xmlDoc.appendChild(root);
|
|
|
|
for (int i = 0; i < licences.size(); ++i)
|
|
{
|
|
QDomElement lic = xmlDoc.createElement(QLatin1String("licence"));
|
|
root.appendChild(lic);
|
|
|
|
|
|
QDomElement element = xmlDoc.createElement(QLatin1String("name"));
|
|
QDomText text = xmlDoc.createTextNode(licences[i].name);
|
|
element.appendChild(text);
|
|
lic.appendChild(element);
|
|
|
|
|
|
element = xmlDoc.createElement(QLatin1String("ignoreMaxCount"));
|
|
text = xmlDoc.createTextNode(QString::number(licences[i].ignoreMaxCount));
|
|
element.appendChild(text);
|
|
lic.appendChild(element);
|
|
|
|
|
|
element = xmlDoc.createElement(QLatin1String("ignoreExpiration"));
|
|
text = xmlDoc.createTextNode(QString::number(licences[i].ignoreExpiration));
|
|
element.appendChild(text);
|
|
lic.appendChild(element);
|
|
|
|
|
|
element = xmlDoc.createElement(QLatin1String("maxCount"));
|
|
text = xmlDoc.createTextNode(QString::number(licences[i].maxCount));
|
|
element.appendChild(text);
|
|
lic.appendChild(element);
|
|
|
|
|
|
const QLatin1String format("dd.MM.yyyy hh:mm:ss");
|
|
const QString qs = licences[i].expiration.toString(format);
|
|
|
|
|
|
element = xmlDoc.createElement(QLatin1String("expiration"));
|
|
text = xmlDoc.createTextNode(qs);
|
|
element.appendChild(text);
|
|
lic.appendChild(element);
|
|
}
|
|
INFO_LOG(getLogger()) << "Generated XML Data...\n" << xmlDoc.toByteArray();
|
|
return xmlDoc.toByteArray();
|
|
}
|
|
|
|
|
|
bool extractLicence(const QDomElement &licenceElement, QVector<Licence> &licenceVec)
|
|
{
|
|
Licence licence;
|
|
|
|
|
|
QDomElement tag = licenceElement.firstChildElement(QLatin1String("name"));
|
|
licence.name = tag.text();
|
|
|
|
|
|
tag = licenceElement.firstChildElement(QLatin1String("ignoreMaxCount"));
|
|
licence.ignoreMaxCount = tag.text().toInt();
|
|
|
|
|
|
tag = licenceElement.firstChildElement(QLatin1String("ignoreExpiration"));
|
|
licence.ignoreExpiration = tag.text().toInt();
|
|
|
|
|
|
tag = licenceElement.firstChildElement(QLatin1String("maxCount"));
|
|
licence.maxCount = tag.text().toInt();
|
|
|
|
|
|
tag = licenceElement.firstChildElement(QLatin1String("expiration"));
|
|
|
|
const QString qs(tag.text());
|
|
|
|
|
|
const QLatin1String format("dd.MM.yyyy hh:mm:ss");
|
|
const QDateTime qdt = QDateTime::fromString(qs, format);
|
|
|
|
if (!qdt.isValid())
|
|
{
|
|
ERROR_LOG(getLogger()) << "Invalid expiration format! " << qs
|
|
<< " used format: " << format
|
|
;
|
|
return false;
|
|
}
|
|
|
|
licence.expiration = qdt;
|
|
INFO_LOG(getLogger()) << "Read licence for:'" << licence.name << "'"
|
|
<< " IgnoreMaxCount: " << licence.ignoreMaxCount
|
|
<< " IgnoreExpiration: " << licence.ignoreExpiration
|
|
<< " MaxCount: " << licence.maxCount
|
|
<< " Expiration: " << licence.expiration.date().toString()
|
|
<< " " << licence.expiration.time().toString()
|
|
<< " RefCount: " << licence.licenceRefCount
|
|
;
|
|
|
|
licenceVec.append(licence);
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
QString Licence::toString() const
|
|
{
|
|
return QObject::tr("Name: %1, Count: %2, ExpireDate: %3, Expires: %4, UseMaxCount: %5")
|
|
.arg(name)
|
|
.arg(maxCount)
|
|
.arg(expiration.toString())
|
|
.arg(ignoreExpiration)
|
|
.arg(ignoreMaxCount);
|
|
}
|
|
|
|
|
|
|
|
}
|