2023-05-19 15:32:52 +02:00
|
|
|
#include "utils.h"
|
2023-08-11 11:03:21 +02:00
|
|
|
#include "message_handler.h"
|
2023-08-22 12:29:52 +02:00
|
|
|
#include "git/git_client.h"
|
2023-05-19 15:32:52 +02:00
|
|
|
|
2023-09-11 10:12:20 +02:00
|
|
|
|
|
|
|
#if defined (Q_OS_UNIX) || defined (Q_OS_LINUX)
|
2023-09-04 11:42:12 +02:00
|
|
|
#include "unistd.h"
|
2023-09-11 10:12:20 +02:00
|
|
|
#endif
|
2023-09-04 11:42:12 +02:00
|
|
|
|
2023-05-19 15:32:52 +02:00
|
|
|
#include <QFile>
|
|
|
|
#include <QTextStream>
|
2023-08-11 11:03:21 +02:00
|
|
|
#include <QDebug>
|
2023-09-09 14:33:13 +02:00
|
|
|
#include <QDir>
|
|
|
|
#include <QDirIterator>
|
2023-09-10 16:46:59 +02:00
|
|
|
#include <QRegularExpression>
|
|
|
|
|
|
|
|
#include <fstream>
|
2023-05-19 15:32:52 +02:00
|
|
|
|
2023-08-02 15:45:20 +02:00
|
|
|
int Utils::read1stLineOfFile(QString fileName) {
|
|
|
|
QFile f(fileName);
|
|
|
|
if (f.exists()) {
|
|
|
|
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
|
QTextStream in(&f);
|
|
|
|
in.setCodec("UTF-8");
|
|
|
|
while(!in.atEnd()) {
|
|
|
|
return in.readLine().toInt();
|
2023-05-19 15:32:52 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-02 15:45:20 +02:00
|
|
|
}
|
|
|
|
return -1;
|
2023-05-19 15:32:52 +02:00
|
|
|
}
|
|
|
|
|
2024-01-17 15:18:03 +01:00
|
|
|
QString Utils::getLocation(QString fileName) {
|
|
|
|
QString location("N/A");
|
|
|
|
QFile f(fileName);
|
|
|
|
if (f.exists()) {
|
|
|
|
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
|
QTextStream in(&f);
|
|
|
|
in.setCodec("UTF-8");
|
|
|
|
while(!in.atEnd()) {
|
|
|
|
QString const &line = in.readLine();
|
|
|
|
if (line.indexOf("Project", Qt::CaseInsensitive) != -1) {
|
|
|
|
int const c = line.indexOf(":");
|
|
|
|
if (c != -1) {
|
|
|
|
location = line.mid(c+1);
|
|
|
|
if (!location.isEmpty()) {
|
2024-04-29 15:02:37 +02:00
|
|
|
location = location.replace(QChar(','), QString(""));
|
2024-01-17 15:18:03 +01:00
|
|
|
return location.replace(QChar('"'), QString("")).trimmed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return location;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Utils::getTariffVersion(QString fileName) {
|
|
|
|
QString version("N/A");
|
|
|
|
QFile f(fileName);
|
|
|
|
if (f.exists()) {
|
|
|
|
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
|
QTextStream in(&f);
|
|
|
|
in.setCodec("UTF-8");
|
|
|
|
while(!in.atEnd()) {
|
|
|
|
QString const &line = in.readLine();
|
|
|
|
if (line.indexOf("Version", Qt::CaseInsensitive) != -1) {
|
|
|
|
int const c = line.indexOf(":");
|
|
|
|
if (c != -1) {
|
|
|
|
version = line.mid(c+1);
|
|
|
|
if (!version.isEmpty()) {
|
2024-04-29 15:02:37 +02:00
|
|
|
version = version.replace(QChar(','), QString(""));
|
2024-01-17 15:18:03 +01:00
|
|
|
return version.replace(QChar('"'), QString("")).trimmed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Utils::getTariffInfo(QString fileName) {
|
|
|
|
QString info("N/A");
|
|
|
|
QFile f(fileName);
|
|
|
|
if (f.exists()) {
|
|
|
|
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
|
QTextStream in(&f);
|
|
|
|
in.setCodec("UTF-8");
|
|
|
|
while(!in.atEnd()) {
|
|
|
|
QString const &line = in.readLine();
|
|
|
|
if (line.indexOf("Info", Qt::CaseInsensitive) != -1) {
|
|
|
|
int const c = line.indexOf(":");
|
|
|
|
if (c != -1) {
|
|
|
|
info = line.mid(c+1);
|
|
|
|
if (!info.isEmpty()) {
|
2024-04-29 15:02:37 +02:00
|
|
|
info = info.replace(QChar(','), QString(""));
|
2024-01-17 15:18:03 +01:00
|
|
|
return info.replace(QChar('"'), QString("")).trimmed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-02 15:45:20 +02:00
|
|
|
QString Utils::zoneName(quint8 i) {
|
|
|
|
static constexpr char const *zName[] = {
|
|
|
|
"",
|
|
|
|
"purple",
|
|
|
|
"blue",
|
|
|
|
"yellow",
|
|
|
|
"green",
|
|
|
|
"yellow (mars)",
|
|
|
|
"green (mars)"
|
|
|
|
};
|
|
|
|
if (i < (sizeof(zName)/sizeof(char const *))) {
|
|
|
|
return zName[i];
|
2023-05-19 15:32:52 +02:00
|
|
|
}
|
2023-08-02 15:45:20 +02:00
|
|
|
return "N/A";
|
2023-05-19 15:32:52 +02:00
|
|
|
}
|
2023-08-11 11:03:21 +02:00
|
|
|
|
2023-10-22 19:44:45 +02:00
|
|
|
void Utils::printCriticalErrorMsg(QString const &errorMsg, bool upper, bool lower) {
|
|
|
|
if (upper) qCritical() << QString(80, 'E');
|
|
|
|
|
2023-08-11 11:03:21 +02:00
|
|
|
qCritical() << errorMsg;
|
2023-10-22 19:44:45 +02:00
|
|
|
|
|
|
|
if (lower) qCritical() << QString(80, 'E');
|
2023-10-18 11:21:32 +02:00
|
|
|
}
|
|
|
|
|
2023-10-18 16:20:49 +02:00
|
|
|
void Utils::printCriticalErrorMsg(QStringList const &errorMsg) {
|
|
|
|
qCritical() << QString(80, 'E');
|
|
|
|
for (int i = 0; i < errorMsg.size(); ++i) {
|
|
|
|
qCritical() << errorMsg.at(i);
|
|
|
|
}
|
|
|
|
qCritical() << QString(80, 'E');
|
|
|
|
}
|
|
|
|
|
2023-11-03 13:46:23 +01:00
|
|
|
void Utils::printUpdateStatusMsg(QDebug debug, QStringList const &updateMsg) {
|
|
|
|
//if (updateMsg.size() > 1) {
|
|
|
|
// qCritical() << QString(80, 'U');
|
|
|
|
//}
|
|
|
|
|
|
|
|
Q_UNUSED(debug);
|
|
|
|
|
2023-10-22 19:44:45 +02:00
|
|
|
for (int i = 0; i < updateMsg.size(); ++i) {
|
2023-11-03 13:46:23 +01:00
|
|
|
qInfo() << updateMsg.at(i);
|
2023-10-22 19:44:45 +02:00
|
|
|
}
|
2023-11-03 13:46:23 +01:00
|
|
|
|
|
|
|
//if (updateMsg.size() > 1) {
|
|
|
|
// qCritical() << QString(80, 'U');
|
|
|
|
//}
|
2023-10-22 19:44:45 +02:00
|
|
|
}
|
|
|
|
|
2023-10-18 16:20:49 +02:00
|
|
|
void Utils::printUpdateStatusMsg(QStringList const &updateMsg) {
|
2023-11-03 13:46:23 +01:00
|
|
|
//if (updateMsg.size() > 1) {
|
|
|
|
// qCritical() << QString(80, 'U');
|
|
|
|
//}
|
|
|
|
|
2023-10-18 16:20:49 +02:00
|
|
|
for (int i = 0; i < updateMsg.size(); ++i) {
|
|
|
|
qCritical() << updateMsg.at(i);
|
|
|
|
}
|
2023-11-03 13:46:23 +01:00
|
|
|
|
|
|
|
//if (updateMsg.size() > 1) {
|
|
|
|
// qCritical() << QString(80, 'U');
|
|
|
|
//}
|
2023-08-11 11:03:21 +02:00
|
|
|
}
|
|
|
|
|
2023-10-22 19:44:45 +02:00
|
|
|
void Utils::printUpdateStatusMsg(QString const &updateMsg, bool upper, bool lower) {
|
|
|
|
if (upper) qCritical() << QString(80, 'U');
|
|
|
|
|
2023-10-19 13:34:07 +02:00
|
|
|
qCritical() << updateMsg;
|
2023-10-22 19:44:45 +02:00
|
|
|
|
|
|
|
if (lower) qCritical() << QString(80, 'U');
|
2023-10-19 13:34:07 +02:00
|
|
|
}
|
|
|
|
|
2023-11-03 13:46:23 +01:00
|
|
|
void Utils::printUpdateStatusMsg(QDebug debug, QString const &updateMsg,
|
2023-10-23 16:16:11 +02:00
|
|
|
bool upper, bool lower) {
|
|
|
|
if (upper) debug << QString(80, 'U');
|
|
|
|
|
2023-11-03 13:46:23 +01:00
|
|
|
qInfo() << updateMsg;
|
2023-10-23 16:16:11 +02:00
|
|
|
|
|
|
|
if (lower) debug << QString(80, 'U');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-22 19:44:45 +02:00
|
|
|
void Utils::printInfoMsg(QString const &infoMsg, bool upper, bool lower) {
|
|
|
|
if (upper) qCritical() << QString(80, 'I');
|
|
|
|
|
2023-08-11 11:03:21 +02:00
|
|
|
qCritical() << infoMsg;
|
2023-10-22 19:44:45 +02:00
|
|
|
|
|
|
|
if (lower) qCritical() << QString(80, 'I');
|
2023-10-18 16:20:49 +02:00
|
|
|
}
|
2023-08-11 11:03:21 +02:00
|
|
|
|
2023-10-18 16:20:49 +02:00
|
|
|
void Utils::printInfoMsg(QStringList const &infoMsg) {
|
2023-11-03 13:46:23 +01:00
|
|
|
//if (infoMsg.size() > 1) {
|
|
|
|
// qCritical() << QString(80, 'I');
|
|
|
|
//}
|
|
|
|
|
2023-10-18 16:20:49 +02:00
|
|
|
for (int i = 0; i < infoMsg.size(); ++i) {
|
|
|
|
qCritical() << infoMsg.at(i);
|
|
|
|
}
|
2023-11-03 13:46:23 +01:00
|
|
|
|
|
|
|
//if (infoMsg.size() > 1) {
|
|
|
|
// qCritical() << QString(80, 'I');
|
|
|
|
//}
|
2023-08-11 11:03:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Utils::printLineEditInfo(QStringList const &lines) {
|
|
|
|
if (getDebugLevel() == LOG_DEBUG) {
|
|
|
|
for (int i=0; i<lines.size(); ++i) {
|
|
|
|
qInfo() << lines.at(i);
|
|
|
|
} qInfo() << ""; qInfo() << "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Utils::getTariffLoadTime(QString fileName) {
|
|
|
|
QFileInfo fInfo(fileName);
|
|
|
|
if (fInfo.exists()) {
|
|
|
|
QDateTime lastModifiedTime = fInfo.lastModified();
|
|
|
|
if (lastModifiedTime.isValid()) {
|
|
|
|
return lastModifiedTime.toString(Qt::ISODateWithMs);
|
|
|
|
} else {
|
|
|
|
printCriticalErrorMsg(fileName + " HAS INVALID MODIFIED-TIME");
|
|
|
|
QDateTime birthTime = fInfo.birthTime();
|
|
|
|
if (birthTime.isValid()) {
|
|
|
|
return birthTime.toString(Qt::ISODateWithMs);
|
|
|
|
} else {
|
|
|
|
printCriticalErrorMsg(fileName + " HAS INVALID BIRTH-TIME");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printCriticalErrorMsg(fileName + " DOES NOT EXIST");
|
|
|
|
}
|
|
|
|
return "N/A";
|
|
|
|
}
|
|
|
|
|
2023-08-18 11:41:16 +02:00
|
|
|
QString Utils::rstrip(QString const &str) {
|
|
|
|
int n = str.size() - 1;
|
|
|
|
for (; n >= 0; --n) {
|
|
|
|
if (!str.at(n).isSpace()) {
|
|
|
|
return str.left(n + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2023-08-22 12:29:52 +02:00
|
|
|
|
|
|
|
bool Utils::sameFilesInDirs(QDir const &dir1, QDir const &dir2,
|
|
|
|
QStringList const &nameFilters) {
|
|
|
|
if (!dir1.exists()) {
|
|
|
|
printCriticalErrorMsg(dir1.dirName() + " DOES NOT EXIST");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!dir2.exists()) {
|
|
|
|
printCriticalErrorMsg(dir2.dirName() + " DOES NOT EXIST");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (dir1.absolutePath() == dir2.absolutePath()) {
|
|
|
|
printCriticalErrorMsg(dir1.dirName() + " AND "+ dir2.dirName() + " HAVE SAME PATH");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// files, sorted by name
|
|
|
|
QFileInfoList const &lst1 = dir1.entryInfoList(nameFilters, QDir::Files, QDir::Name);
|
|
|
|
QFileInfoList const &lst2 = dir2.entryInfoList(nameFilters, QDir::Files, QDir::Name);
|
|
|
|
|
|
|
|
QStringList fileNameLst1{};
|
|
|
|
QStringList fileNameLst2{};
|
|
|
|
QListIterator<QFileInfo> i1(lst1);
|
|
|
|
while (i1.hasNext()) {
|
|
|
|
fileNameLst1 << i1.next().fileName();
|
|
|
|
}
|
|
|
|
QListIterator<QFileInfo> i2(lst2);
|
|
|
|
while (i2.hasNext()) {
|
|
|
|
fileNameLst2 << i2.next().fileName();
|
|
|
|
}
|
|
|
|
|
2024-04-09 14:53:50 +02:00
|
|
|
QString dirPath1 = dir1.absolutePath();
|
|
|
|
QString dirPath2 = dir2.absolutePath();
|
|
|
|
|
2023-08-22 12:29:52 +02:00
|
|
|
if (fileNameLst1.isEmpty()) {
|
2024-04-09 14:53:50 +02:00
|
|
|
qCritical() << "DIR1" << dirPath1 << " DOES NOT CONTAIN EXPECTED FILES";
|
2023-08-22 12:29:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (fileNameLst2.isEmpty()) {
|
2024-04-09 14:53:50 +02:00
|
|
|
qCritical() << "DIR1" << dirPath2 << " DOES NOT CONTAIN EXPECTED FILES";
|
2023-08-22 12:29:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (fileNameLst1 != fileNameLst2) {
|
2024-04-09 14:53:50 +02:00
|
|
|
printCriticalErrorMsg(dirPath1 + " AND " + dirPath2
|
2023-08-22 12:29:52 +02:00
|
|
|
+ " DIFFER: [" + fileNameLst1.join(',') + "],["
|
|
|
|
+ fileNameLst2.join(',') + "]");
|
|
|
|
return false;
|
|
|
|
} else {
|
2024-04-09 14:53:50 +02:00
|
|
|
printInfoMsg(dirPath1 + " AND " + dirPath2
|
2023-08-22 12:29:52 +02:00
|
|
|
+ " ARE EQUAL: [" + fileNameLst1.join(',') + "]");
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList gitBlobLst1{};
|
|
|
|
QStringList gitBlobLst2{};
|
|
|
|
QListIterator<QFileInfo> i3(lst1);
|
|
|
|
while (i3.hasNext()) {
|
|
|
|
gitBlobLst1 << GitClient::gitBlob(i3.next().fileName());
|
|
|
|
}
|
|
|
|
QListIterator<QFileInfo> i4(lst2);
|
|
|
|
while (i4.hasNext()) {
|
|
|
|
gitBlobLst2 << GitClient::gitBlob(i4.next().fileName());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gitBlobLst1.isEmpty()) {
|
2024-04-09 14:53:50 +02:00
|
|
|
qCritical() << "DIR1" << dirPath1 << " DOES NOT CONTAIN EXPECTED FILES";
|
2023-08-22 12:29:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (gitBlobLst2.isEmpty()) {
|
2024-04-09 14:53:50 +02:00
|
|
|
qCritical() << "DIR1" << dirPath2 << " DOES NOT CONTAIN EXPECTED FILES";
|
2023-08-22 12:29:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gitBlobLst1 != gitBlobLst2) {
|
2024-04-09 14:53:50 +02:00
|
|
|
printCriticalErrorMsg(dirPath1 + " AND " + dirPath2
|
2023-08-22 12:29:52 +02:00
|
|
|
+ " DIFFER: [" + gitBlobLst1.join(',') + "],["
|
|
|
|
+ gitBlobLst2.join(',') + "]");
|
|
|
|
return false;
|
|
|
|
} else {
|
2024-04-09 14:53:50 +02:00
|
|
|
printInfoMsg(dirPath1 + " AND " + dirPath2
|
2023-08-22 12:29:52 +02:00
|
|
|
+ " CONTAIN SAME GIT-BLOBS FOR FILES: [" + fileNameLst1.join(',') + "]");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-09-04 11:42:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
QString Utils::getParentName() { // get name of parent process
|
|
|
|
QString ppid = QString("/proc/%1/status").arg(getppid());
|
2023-09-11 10:12:20 +02:00
|
|
|
std::ifstream f(ppid.toStdString().c_str());
|
|
|
|
if (f.is_open()) {
|
|
|
|
std::string next;
|
|
|
|
while (std::getline(f, next)) {
|
|
|
|
QString line = QString(next.c_str()).simplified();
|
|
|
|
if (line.startsWith("Name")) {
|
|
|
|
int const idx = line.indexOf(QChar(':'));
|
|
|
|
if (idx != -1) {
|
|
|
|
return line.mid(idx+1).trimmed();
|
2023-09-04 11:42:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2023-09-09 14:33:13 +02:00
|
|
|
|
|
|
|
bool Utils::isATBQTRunning() {
|
|
|
|
QDirIterator it("/proc",
|
|
|
|
QStringList() << "status",
|
|
|
|
QDir::Files,
|
|
|
|
QDirIterator::Subdirectories);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
QString const &nextStatusFile = it.next();
|
2023-09-10 16:46:59 +02:00
|
|
|
static const QRegularExpression re("^/proc/[0-9]{1,}/status");
|
|
|
|
QRegularExpressionMatch match = re.match(nextStatusFile);
|
|
|
|
if (match.hasMatch()) {
|
|
|
|
std::ifstream f(nextStatusFile.toStdString().c_str());
|
|
|
|
if (f.is_open()) {
|
|
|
|
std::string next;
|
|
|
|
while (std::getline(f, next)) {
|
|
|
|
QString line = QString(next.c_str()).simplified();
|
|
|
|
if (line.startsWith("Name")) {
|
|
|
|
int const idx = line.indexOf(QChar(':'));
|
|
|
|
if (idx != -1) {
|
|
|
|
QString const binary = line.mid(idx+1).trimmed();
|
|
|
|
if (binary == "ATBQT") {
|
2023-09-09 14:33:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|