qt/LedOK/globaldefine.cpp

128 lines
4.9 KiB
C++
Raw Normal View History

2023-05-15 16:06:10 +08:00
#include "globaldefine.h"
#include "devicepanel.h"
#include "deviceitem.h"
2023-07-24 11:25:14 +08:00
#include "gutil/qnetwork.h"
2023-04-18 14:14:46 +08:00
#include <QDir>
#include <QDateTime>
#include <QCoreApplication>
#include <QMessageBox>
#include <QMetaEnum>
2023-05-15 16:06:10 +08:00
QList<LedCard> gSelCards;
2023-04-18 14:14:46 +08:00
bool gVideoCompress = true;
bool gVideoTranscoding = true;
bool gTextAntialiasing = false;
bool gWidthSplit = false;
2023-08-01 11:42:41 +08:00
bool gHideDetect = false;
bool gShowLora = false;
2023-04-18 14:14:46 +08:00
2023-05-15 16:06:10 +08:00
DeviceItem *findItem(QString id) {
int cnt = gDevicePanel->mDeviceTable->topLevelItemCount();
for(int i=0; i<cnt; ++i) {
auto item = (DeviceItem*) gDevicePanel->mDeviceTable->topLevelItem(i);
if(item==0) continue;
if(item->mCard.id==id) return item;
}
return 0;
}
2023-04-18 14:14:46 +08:00
QString checkReply(QNetworkReply *reply, QJsonDocument *outJson) {
2023-07-24 11:25:14 +08:00
auto err = errStr(reply);
2023-04-18 14:14:46 +08:00
if(! err.isEmpty()) {
auto data = reply->readAll();
if(! data.isEmpty()) err = err+"\n"+QCoreApplication::translate("Def","Device replied")+": "+data;
return err;
}
if(outJson) {
auto data = reply->readAll();
QJsonParseError jsonErr;
*outJson = QJsonDocument::fromJson(data, &jsonErr);
if(jsonErr.error != QJsonParseError::NoError) return "Json error: "+jsonErr.errorString()+"\n"+QCoreApplication::translate("Def","Device replied")+": "+data;
}
return "";
}
QString checkReplyForJson(QNetworkReply *reply, QJsonDocument *outJson, QByteArray *outData) {
2023-07-24 11:25:14 +08:00
auto err = errStr(reply);
2023-04-18 14:14:46 +08:00
auto data = reply->readAll();
if(outData) *outData = data;
if(! err.isEmpty()) {
if(! data.isEmpty()) err = err+"\n"+QCoreApplication::translate("Def","Device replied")+": "+data;
return err;
}
QJsonParseError jsonErr;
QJsonDocument json = QJsonDocument::fromJson(data, &jsonErr);
if(jsonErr.error != QJsonParseError::NoError) return "Json error: "+jsonErr.errorString()+"\n"+QCoreApplication::translate("Def","Device replied")+": "+data;
if(! json["success"].toBool()) return QCoreApplication::translate("Def","Fail")+". "+QCoreApplication::translate("Def","Device replied")+": "+data;
if(outJson) outJson->swap(json);
return "";
}
2023-07-24 11:25:14 +08:00
QString checkReplyForJson(QNetworkReply *reply, QString errField) {
auto err = errStr(reply);
auto data = reply->readAll();
if(! err.isEmpty()) {
if(! data.isEmpty()) err = err+"\n"+QCoreApplication::translate("Def","Device replied")+": "+data;
return err;
}
QJsonParseError jsonErr;
QJsonDocument json = QJsonDocument::fromJson(data, &jsonErr);
if(jsonErr.error != QJsonParseError::NoError) return "Json error: "+jsonErr.errorString()+"\n"+QCoreApplication::translate("Def","Device replied")+": "+data;
if(! json["success"].toBool()) {
auto errStr = json[errField].toString();
return QCoreApplication::translate("Def","Fail")+". "+QCoreApplication::translate("Def","Device replied")+": "+(errStr.isEmpty() ? data : errStr);
}
return "";
}
2023-04-18 14:14:46 +08:00
quint64 dirFileSize(const QString &path) {
QDir dir(path);
quint64 size = 0;
auto infos = dir.entryInfoList(QDir::Files);
foreach(QFileInfo fileInfo, infos) size += fileInfo.size();
auto subDirs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
foreach(QString subDir, subDirs) size += dirFileSize(path + QDir::separator() + subDir);
return size;
}
2023-05-15 16:06:10 +08:00
bool copyDir(const QString &source, const QString &destination, bool override) {
2023-04-18 14:14:46 +08:00
QDir directory(source);
2023-05-15 16:06:10 +08:00
if(!directory.exists()) return false;
2023-04-18 14:14:46 +08:00
QString srcPath = QDir::toNativeSeparators(source);
2023-05-15 16:06:10 +08:00
if(!srcPath.endsWith(QDir::separator())) srcPath += QDir::separator();
2023-04-18 14:14:46 +08:00
QString dstPath = QDir::toNativeSeparators(destination);
2023-05-15 16:06:10 +08:00
if (!dstPath.endsWith(QDir::separator())) dstPath += QDir::separator();
2023-04-18 14:14:46 +08:00
bool error = false;
QStringList fileNames = directory.entryList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
for (QStringList::size_type i=0; i != fileNames.size(); ++i)
{
QString fileName = fileNames.at(i);
QString srcFilePath = srcPath + fileName;
QString dstFilePath = dstPath + fileName;
QFileInfo fileInfo(srcFilePath);
if (fileInfo.isFile() || fileInfo.isSymLink())
{
if (override)
{
QFile::setPermissions(dstFilePath, QFile::WriteOwner);
}
QFile::copy(srcFilePath, dstFilePath);
}
else if (fileInfo.isDir())
{
QDir dstDir(dstFilePath);
dstDir.mkpath(dstFilePath);
if (!copyDir(srcFilePath, dstFilePath, override))
{
error = true;
}
}
}
return !error;
}
unsigned char GetCheckCodeIn8(unsigned char * pBuffer,unsigned int uiSize) {
unsigned int i=0;
unsigned char ucCheckCode=0;
for (i=0;i<uiSize;i++) ucCheckCode+=pBuffer[i];
return (~ucCheckCode)&0x0ff;
}