qt/LedOK/globaldefine.cpp

166 lines
6.5 KiB
C++
Raw Normal View History

2022-01-04 18:11:48 +08:00
#include <QRegExp>
#include <globaldefine.h>
#include <QDir>
#include <QDateTime>
2022-08-25 18:37:24 +08:00
#include <QCoreApplication>
#include <QMessageBox>
#include <QMetaEnum>
2022-09-13 23:16:36 +08:00
2022-08-25 18:37:24 +08:00
bool gTextAntialiasing = false;
bool gShowLoraScreen = false;
2022-01-04 18:11:48 +08:00
2022-08-25 18:37:24 +08:00
QNetworkRequest netReq(const QString &url, int timeout, const QVariant &contentType) {
QNetworkRequest request{url};
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
request.setTransferTimeout(timeout);
if(contentType.isValid()) request.setHeader(QNetworkRequest::ContentTypeHeader, contentType);
return request;
}
QString parseReplyJson(QNetworkReply *reply, QJsonDocument *outJson) {
reply->deleteLater();
auto error = reply->error();
if(error != QNetworkReply::NoError) {
auto errStr = reply->errorString();
if(error!=QNetworkReply::InternalServerError || ! errStr.endsWith("replied: Unknown")) {
if(error==QNetworkReply::OperationCanceledError) {
error = QNetworkReply::TimeoutError;
errStr = QCoreApplication::translate("Def","Connection Timeout");
}
2022-09-13 23:16:36 +08:00
return QString(QMetaEnum::fromType<QNetworkReply::NetworkError>().valueToKey(error))+" ("+QString::number(error)+") "+errStr+"\n"+QCoreApplication::translate("Def","Device replied")+":\n"+reply->readAll();
2022-08-25 18:37:24 +08:00
}
}
auto status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
2022-09-13 23:16:36 +08:00
if(status != 200) return QString::number(status)+" "+reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString()+"\n"+QCoreApplication::translate("Def","Device replied")+":\n"+reply->readAll();
2022-08-25 18:37:24 +08:00
if(outJson) {
auto data = reply->readAll();
QJsonParseError jsonErr;
*outJson = QJsonDocument::fromJson(data, &jsonErr);
2022-09-13 23:16:36 +08:00
if(jsonErr.error != QJsonParseError::NoError) return "Json error: "+jsonErr.errorString()+"\n"+QCoreApplication::translate("Def","Device replied")+":\n"+data;
2022-08-25 18:37:24 +08:00
}
return "";
}
QString checkReplyForJson(QNetworkReply *reply, QJsonDocument *doc) {
reply->deleteLater();
auto error = reply->error();
auto data = reply->readAll();
if(error != QNetworkReply::NoError) {
auto errStr = reply->errorString();
if(error!=QNetworkReply::InternalServerError || ! errStr.endsWith("replied: Unknown")) {
if(error==QNetworkReply::OperationCanceledError) {
error = QNetworkReply::TimeoutError;
errStr = QCoreApplication::translate("Def","Connection Timeout");
}
2022-09-13 23:16:36 +08:00
return QString(QMetaEnum::fromType<QNetworkReply::NetworkError>().valueToKey(error))+" ("+QString::number(error)+") "+errStr+"\n"+QCoreApplication::translate("Def","Device replied")+":\n"+data;
2022-08-25 18:37:24 +08:00
}
}
auto status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
2022-09-13 23:16:36 +08:00
if(status != 200) return QString::number(status)+" - "+reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString()+"\n"+QCoreApplication::translate("Def","Device replied")+":\n"+data;
2022-08-25 18:37:24 +08:00
QJsonParseError jsonErr;
QJsonDocument json = QJsonDocument::fromJson(data, &jsonErr);
2022-09-13 23:16:36 +08:00
if(jsonErr.error != QJsonParseError::NoError) return "Json error: "+jsonErr.errorString()+"\n"+QCoreApplication::translate("Def","Device replied")+":\n"+data;
if(! json["success"].toBool()) return QCoreApplication::translate("Def","Fail")+". "+QCoreApplication::translate("Def","Device replied")+":\n"+data;
2022-08-25 18:37:24 +08:00
if(doc) doc->swap(json);
return "";
}
2022-09-13 23:16:36 +08:00
2022-01-20 10:08:17 +08:00
bool isTextValid(const QString &strIP){
if(! strIP.isEmpty()) {
2022-01-04 18:11:48 +08:00
QRegExp rx2("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");
2022-01-20 10:08:17 +08:00
if (!rx2.exactMatch(strIP)) return false;
2022-01-04 18:11:48 +08:00
}
return true;
}
2022-08-25 18:37:24 +08:00
QString getRandomString(int len) {
static const char table[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
static const int tableSize = sizeof(table) - 1;
QString str;
str.resize(len);
for(int i=0; i<len; i++) str[i] = table[rand() % tableSize];
return str;
2022-01-04 18:11:48 +08:00
}
bool isFileExist(QString fullFileName)
{
QFileInfo fileInfo(fullFileName);
if(fileInfo.isFile())
{
return true;
}
return false;
}
quint64 dirFileSize(const QString &path)
{
QDir dir(path);
quint64 size = 0;
//dir.entryInfoList(QDir::Files)返回文件信息
foreach(QFileInfo fileInfo, dir.entryInfoList(QDir::Files))
{
//计算文件大小
size += fileInfo.size();
}
//dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot)返回所有子目录,并进行过滤
foreach(QString subDir, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot))
{
//若存在子目录则递归调用dirFileSize()函数
size += dirFileSize(path + QDir::separator() + subDir);
}
return size;
}
2022-08-25 18:37:24 +08:00
void HttpPostByTypeJsonObject(HpptClient *pHpptClient, QString strUrl, QJsonObject json) {
pHpptClient->httpPost(strUrl, QJsonDocument(json).toJson(QJsonDocument::Compact));
2022-01-04 18:11:48 +08:00
}
bool copyDir(const QString &source, const QString &destination, bool override)
{
QDir directory(source);
if (!directory.exists())
{
return false;
}
QString srcPath = QDir::toNativeSeparators(source);
if (!srcPath.endsWith(QDir::separator()))
srcPath += QDir::separator();
QString dstPath = QDir::toNativeSeparators(destination);
if (!dstPath.endsWith(QDir::separator()))
dstPath += QDir::separator();
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;
}
2022-09-28 18:21:57 +08:00
unsigned char GetCheckCodeIn8(unsigned char * pBuffer,unsigned int uiSize) {
unsigned int i=0;
unsigned char ucCheckCode=0;
2022-01-04 18:11:48 +08:00
for (i=0;i<uiSize;i++) ucCheckCode+=pBuffer[i];
return (~ucCheckCode)&0x0ff;
}