ATBTariffCalculator/CalculatorCInterface/local_git_repository.cpp

124 lines
3.7 KiB
C++
Raw Normal View History

2024-03-21 16:57:02 +01:00
#include "local_git_repository.h"
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QJsonDocument>
#include <QByteArray>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QFile>
2024-03-27 08:40:16 +01:00
QString LocalGitRepository::m_repoRootDirectory = "H:\\";
void LocalGitRepository::SetReposRootDirectory(QString s) {
m_repoRootDirectory = s;
}
QString LocalGitRepository::GetReposRootDirectory() {
return m_repoRootDirectory;
}
2024-03-21 16:57:02 +01:00
LocalGitRepository::LocalGitRepository(QString const &localRepository)
: m_localRepository(localRepository)
, m_fileMenu("{}")
, m_fileMenuSize(-1) {
}
2024-03-25 16:13:26 +01:00
int32_t LocalGitRepository::GetFileMenuSizeInternal() const {
2024-03-21 16:57:02 +01:00
return m_fileMenuSize;
}
2024-03-25 16:13:26 +01:00
QByteArray LocalGitRepository::GetFileMenuInternal() {
2024-03-22 13:46:54 +01:00
if (m_fileMenuSize == -1) {
2024-03-27 08:40:16 +01:00
QFile f(QDir::cleanPath(m_repoRootDirectory + QDir::separator()
+ m_localRepository + QDir::separator()
+ "etc/psa_webinterface/menu_config.json"));
2024-03-21 16:57:02 +01:00
if (f.exists()) {
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
m_fileMenu = QTextStream(&f).readAll().toUtf8();
if (m_fileMenu.isValidUtf8()) {
QString const &s = QString::fromUtf8(m_fileMenu);
m_fileMenuSize = s.toLocal8Bit().size();
}
}
}
}
return m_fileMenu;
}
2024-03-25 16:13:26 +01:00
QByteArray LocalGitRepository::GetFileNameInternal(QString const &fId) {
2024-03-21 16:57:02 +01:00
QRegularExpressionMatch match;
static const QRegularExpression re("(master|[0-9]+)");
if (fId.lastIndexOf(re, -1, &match) != -1) {
int idx = fId.indexOf("/");
if (idx != -1) {
QString path = fId.mid(idx);
idx = path.indexOf(":");
if (idx != -1) {
path = path.mid(0, idx);
QString s = match.captured(match.lastCapturedIndex());
if (s != "master") {
if (fId.contains("psa_tariff")) {
QString fn(QDir::cleanPath(
2024-03-27 08:40:16 +01:00
m_repoRootDirectory + QDir::separator() +
2024-03-21 16:57:02 +01:00
QString(m_localRepository) + QDir::separator()
+ path + QDir::separator()
+ QString("tariff%1.json").arg(s.toUInt(), 2, 10, QChar('0'))));
return fn.toUtf8();
}
}
}
}
}
return QByteArray();
}
2024-03-25 16:13:26 +01:00
QByteArray LocalGitRepository::GetFileInternal(QString const &fId) {
QByteArray a = GetFileNameInternal(fId);
2024-03-21 16:57:02 +01:00
if (a.isValidUtf8()) {
QFile fn(a);
if (fn.exists()) {
if (fn.open(QIODevice::ReadOnly)) {
return fn.readAll();
}
}
}
return QByteArray("{}");
}
int32_t LocalGitRepository::GetFileSize(QString const &fId) {
2024-03-25 16:13:26 +01:00
QByteArray a = GetFileNameInternal(fId);
2024-03-21 16:57:02 +01:00
if (a.isValidUtf8()) {
QFile fn(a);
if (fn.exists()) {
return fn.size();
}
}
return -1;
}
2024-03-25 16:13:26 +01:00
bool LocalGitRepository::SetFileInternal(QString const &fId, QByteArray const &json) {
QByteArray a = GetFileNameInternal(fId);
2024-03-21 16:57:02 +01:00
if (a.isValidUtf8()) {
QFile fn(a);
if (fn.exists()) {
if (fn.open(QIODevice::WriteOnly)) {
2024-03-22 13:46:54 +01:00
qint64 bytesWritten = 0;
2024-03-25 16:13:26 +01:00
qint64 bytesToWrite = json.size();
2024-03-21 16:57:02 +01:00
while (bytesToWrite > 0 &&
(bytesWritten = fn.write(json.constData(), bytesToWrite)) != -1) {
bytesToWrite -= bytesWritten;
}
2024-03-25 16:13:26 +01:00
fn.flush();
2024-03-21 16:57:02 +01:00
return (bytesToWrite == 0);
}
}
}
return false;
}