ATBTariffCalculator/CalculatorCInterface/local_git_repository.cpp
2024-03-28 16:45:46 +01:00

213 lines
6.7 KiB
C++

#include "local_git_repository.h"
#include "git_library.h"
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QJsonDocument>
#include <QByteArray>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QFile>
QString LocalGitRepository::repoRootDirectory = "";
QMap<QString, LocalGitRepository *> LocalGitRepository::localGitRepos;
QMap<QString, LocalGitRepository *> &LocalGitRepository::GetLocalGitRepos() {
return localGitRepos;
}
void LocalGitRepository::SetReposRootDirectory(QString s) {
if (repoRootDirectory.isEmpty()) {
repoRootDirectory = s;
return;
}
qCritical() << __func__ << ":" << __LINE__
<< "ERROR git-repository-root-directory already set"
<< repoRootDirectory;
}
QString LocalGitRepository::GetReposRootDirectory() {
return repoRootDirectory;
}
LocalGitRepository::LocalGitRepository(QString const &localRepository)
: m_localRepository(localRepository)
, m_fileMenu("{}")
, m_fileMenuSize(-1)
, m_git_repository(nullptr) {
}
LocalGitRepository::~LocalGitRepository() {
}
void LocalGitRepository::SetGitRepository(git_repository *git_repo) {
m_git_repository = git_repo;
}
git_repository const *LocalGitRepository::GetGitRepository() const {
return m_git_repository;
}
git_repository *LocalGitRepository::GetGitRepository() {
return m_git_repository;
}
QString LocalGitRepository::localRepositoryName() const { // e.g. customer_999
return m_localRepository;
}
QString LocalGitRepository::localRepositoryPath() const {
if (!repoRootDirectory.isEmpty()) {
return QDir::cleanPath(repoRootDirectory + QDir::separator() + m_localRepository);
}
qCritical() << __func__ << ":" << __LINE__
<< "ERROR git-repository-root-directory not set";
return "";
}
QString LocalGitRepository::localRepositoryPath(QString const &localRepository) {
if (!repoRootDirectory.isEmpty()) {
return QDir::cleanPath(repoRootDirectory + QDir::separator() + localRepository);
}
qCritical() << __func__ << ":" << __LINE__
<< "ERROR git-repository-root-directory not set";
return "";
}
LocalGitRepository *LocalGitRepository::GetInstance(QString const& localRepository) {
LocalGitRepository *repo = nullptr;
if (GetLocalGitRepos().count(localRepository) > 0) {
repo = GetLocalGitRepos()[localRepository];
} else {
repo = new LocalGitRepository(localRepository);
qCritical() << "created local git-repository" << localRepository;
GetLocalGitRepos().insert(localRepository, repo);
}
if (repo == nullptr) {
qCritical() << __func__ << ":" << __LINE__
<< "ERROR: could not find local git-repository" << localRepository;
}
return repo;
}
bool LocalGitRepository::DestroyInstance(QString const &localRepository) {
if (GetLocalGitRepos().count(localRepository) > 0) {
LocalGitRepository *repo = GetLocalGitRepos().take(localRepository);
delete repo;
qCritical() << "deleted local git-repository" << localRepository;
return true;
}
qCritical() << __func__ << ":" << __LINE__
<< "ERROR: could not find local git-repository" << localRepository;
return false;
}
void LocalGitRepository::DestroyAllRepositories() {
for (auto it = GetLocalGitRepos().keyValueBegin(); it != GetLocalGitRepos().keyValueEnd(); ++it) {
QString const &key = it->first;
LocalGitRepository *repo = GetLocalGitRepos()[key];
delete repo;
qCritical() << "deleted local git-repository" << key;
}
GetLocalGitRepos().clear();
}
int32_t LocalGitRepository::GetFileMenuSizeInternal() const {
return m_fileMenuSize;
}
QByteArray LocalGitRepository::GetFileMenuInternal() {
if (m_fileMenuSize == -1) {
QFile f(QDir::cleanPath(repoRootDirectory + QDir::separator()
+ m_localRepository + QDir::separator()
+ "etc/psa_webinterface/menu_config.json"));
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;
}
QByteArray LocalGitRepository::GetFileNameInternal(QString const &fId) {
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(
repoRootDirectory + QDir::separator() +
QString(m_localRepository) + QDir::separator()
+ path + QDir::separator()
+ QString("tariff%1.json").arg(s.toUInt(), 2, 10, QChar('0'))));
return fn.toUtf8();
}
}
}
}
}
return QByteArray();
}
QByteArray LocalGitRepository::GetFileInternal(QString const &fId) {
QByteArray a = GetFileNameInternal(fId);
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) {
QByteArray a = GetFileNameInternal(fId);
if (a.isValidUtf8()) {
QFile fn(a);
if (fn.exists()) {
return fn.size();
}
}
return -1;
}
bool LocalGitRepository::SetFileInternal(QString const &fId, QByteArray const &json) {
QByteArray a = GetFileNameInternal(fId);
if (a.isValidUtf8()) {
QFile fn(a);
if (fn.exists()) {
if (fn.open(QIODevice::WriteOnly)) {
qint64 bytesWritten = 0;
qint64 bytesToWrite = json.size();
while (bytesToWrite > 0 &&
(bytesWritten = fn.write(json.constData(), bytesToWrite)) != -1) {
bytesToWrite -= bytesWritten;
}
fn.flush();
return (bytesToWrite == 0);
}
}
}
return false;
}