#include "AppConfig.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(WIN32) #include #include #include #include //C (Windows) access #else #include #include #include void Sleep(long mSeconds); //{ // usleep(mSeconds * 1000); //} #endif QString AppConfig::APPID = "{a1db97ad-b8ed-11e9-a297-0235d2b38928}"; int AppConfig::VERSION = 1; QString AppConfig::VERSION_NAME = "2.1.3"; MainWindow *AppConfig::gMainWindow = NULL; QRect AppConfig::gMainWindowRect; QRect AppConfig::gScreenRect; bool AppConfig::gVideoKeepAspectRatio = false; //按比例显示 bool AppConfig::gVideoHardDecoder = false; //硬解解码 QString AppConfig::gVideoFilePath; QString AppConfig::AppDataPath_Main; QString AppConfig::AppDataPath_Data; QString AppConfig::AppDataPath_Tmp; QString AppConfig::AppDataPath_TmpFile; QString AppConfig::AppFilePath_Log; QString AppConfig::AppFilePath_LogFile; QString AppConfig::AppFilePath_EtcFile; AppConfig::AppConfig() { } void AppConfig::MakeDir(QString dirName) { QDir dir; dir.mkpath(dirName); } void AppConfig::InitAllDataPath() { #if defined(WIN32) ///windows数据存储在C盘的数据目录下 QFileInfo fileInfo(QCoreApplication::applicationFilePath()); QString exeFileName = fileInfo.baseName(); //当前程序名字 QString dataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); if (dataPath.right(exeFileName.length()) == exeFileName) { dataPath = dataPath.left(dataPath.length() - exeFileName.length()); } if (!dataPath.endsWith("/")) { dataPath += "/"; } #else ///Linux则放在程序所在目录下的data目录下 QFileInfo fileInfo(QCoreApplication::applicationFilePath()); QString dataPath = fileInfo.absoluteDir().path(); if (!dataPath.endsWith("/")) { dataPath += "/"; } #endif qDebug()<<__FUNCTION__< 0) { bytesToWrite = size; } else { bytesToWrite = totalBytes; } if (bytesToWrite > totalBytes) { bytesToWrite = totalBytes; } while (1) { if(bytesToWrite > 0) { buf = localFile.read(qMin(bytesToWrite, loadSize)); ch.addData(buf); bytesWritten += buf.length(); bytesToWrite -= buf.length(); buf.resize(0); } else { break; } if (bytesToWrite <= 0) { break; } // if(bytesWritten == totalBytes) // { // break; // } } localFile.close(); QByteArray md5 = ch.result(); return AppConfig::bufferToString(md5).toLower(); } void AppConfig::loadConfigInfoFromFile() { QFile file(AppConfig::AppFilePath_EtcFile); bool ret = file.open(QIODevice::ReadOnly); if (!ret) return; QString text(file.readAll()); if (!text.isEmpty()) { QJsonParseError json_error; QJsonDocument jsonDoc(QJsonDocument::fromJson(text.toUtf8(), &json_error)); if(json_error.error == QJsonParseError::NoError) { QJsonObject object = jsonDoc.object(); QJsonValue VideoKeepAspectRatioValue = object.value("VideoKeepAspectRatio"); QJsonValue VideoFilePathValue = object.value("VideoFilePath"); AppConfig::gVideoKeepAspectRatio = VideoKeepAspectRatioValue.toBool(); AppConfig::gVideoFilePath = VideoFilePathValue.toString(); } } file.close(); } void AppConfig::saveConfigInfoToFile() { QFile file(AppConfig::AppFilePath_EtcFile); if (file.open(QIODevice::WriteOnly)) { QTextStream fileOut(&file); fileOut.setCodec("UTF-8"); //unicode UTF-8 ANSI QJsonObject dataObject; dataObject.insert("appid", AppConfig::APPID); dataObject.insert("VideoKeepAspectRatio", AppConfig::gVideoKeepAspectRatio); dataObject.insert("VideoFilePath", AppConfig::gVideoFilePath); QJsonDocument json; // QJsonObject object; // object.insert("config", dataObject); //最外层是大括号所以是object json.setObject(dataObject); QString jsonStr = json.toJson(QJsonDocument::Compact); fileOut<= (24*3600*3)) //删除3天前的日志文件 // if (t >= (60*20)) { QFile::remove(fileInfo.absoluteFilePath()); } } } AppFilePath_LogFile = AppFilePath_Log + QString("/log_%1.txt").arg(QDate::currentDate().toString("yyyy-MM-dd")); WriteLog("\r\n=======================================\r\n=======================================\r\n[App Start...]\r\n\r\n"); } #if 0 bool AppConfig::WriteLog(QString str) { bool IsFileOpened = gLogFile.isOpen(); if (!IsFileOpened) { IsFileOpened = gLogFile.open(QIODevice::ReadWrite); gLogFile.seek(gLogFile.size()); } if (IsFileOpened) { QString tmpStr = QString("[%1] %2 \r\n") .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")) .arg(str); QTextStream fileOut(&gLogFile); fileOut.setCodec("UTF-8"); //unicode UTF-8 ANSI fileOut< (pee*pee*pee)) //大于1G { sprintf(ch,"%dGB",(int)(size*1.0/pee/pee/pee+0.5)); } else if (size > (pee*pee)) //大于1M { sprintf(ch,"%dMB",size/pee/pee); } else if (size > pee) //大于1K { sprintf(ch,"%dKB",size/pee); } else //小于1KB { sprintf(ch,"%dB",size); } QString str = QString(ch); return str; } QImage AppConfig::ImagetoGray( QImage image) { int height = image.height(); int width = image.width(); QImage ret(width, height, QImage::Format_Indexed8); ret.setColorCount(256); for(int i = 0; i < 256; i++) { ret.setColor(i, qRgb(i, i, i)); } switch(image.format()) { case QImage::Format_Indexed8: for(int i = 0; i < height; i ++) { const uchar *pSrc = (uchar *)image.constScanLine(i); uchar *pDest = (uchar *)ret.scanLine(i); memcpy(pDest, pSrc, width); } break; case QImage::Format_RGB32: case QImage::Format_ARGB32: case QImage::Format_ARGB32_Premultiplied: for(int i = 0; i < height; i ++) { const QRgb *pSrc = (QRgb *)image.constScanLine(i); uchar *pDest = (uchar *)ret.scanLine(i); for( int j = 0; j < width; j ++) { pDest[j] = qGray(pSrc[j]); } } break; } return ret; } //拷贝文件夹: bool AppConfig::copyDirectoryFiles(const QString &fromDir, const QString &toDir, bool coverFileIfExist) { QDir sourceDir(fromDir); QDir targetDir(toDir); if(!targetDir.exists()){ /**< 如果目标目录不存在,则进行创建 */ if(!targetDir.mkdir(targetDir.absolutePath())) return false; } QFileInfoList fileInfoList = sourceDir.entryInfoList(); foreach(QFileInfo fileInfo, fileInfoList){ if(fileInfo.fileName() == "." || fileInfo.fileName() == "..") continue; if(fileInfo.isDir()){ /**< 当为目录时,递归的进行copy */ if(!copyDirectoryFiles(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()), coverFileIfExist)) return false; } else{ /**< 当允许覆盖操作时,将旧文件进行删除操作 */ if(coverFileIfExist && targetDir.exists(fileInfo.fileName())){ targetDir.remove(fileInfo.fileName()); } /// 进行文件copy if(!QFile::copy(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()))){ return false; } } } return true; } bool AppConfig::removeDirectory(QString dirName) { QDir dir(dirName); QString tmpdir = ""; if (!QFile(dirName).exists()) return false; if(!dir.exists()){ return false; } QFileInfoList fileInfoList = dir.entryInfoList(); foreach(QFileInfo fileInfo, fileInfoList){ if(fileInfo.fileName() == "." || fileInfo.fileName() == "..") continue; if(fileInfo.isDir()){ tmpdir = dirName + ("/") + fileInfo.fileName(); removeDirectory(tmpdir); dir.rmdir(fileInfo.fileName()); /**< 移除子目录 */ } else if(fileInfo.isFile()){ QFile tmpFile(fileInfo.fileName()); dir.remove(tmpFile.fileName()); /**< 删除临时文件 */ } else{ ; } } dir.cdUp(); /**< 返回上级目录,因为只有返回上级目录,才可以删除这个目录 */ if(dir.exists(dirName)){ if(!dir.rmdir(dirName)) return false; } return true; } #if defined(Q_OS_WIN32) #include #include bool AppConfig::restartSelf() { SHELLEXECUTEINFO sei; TCHAR szModule [MAX_PATH],szComspec[MAX_PATH],szParams [MAX_PATH]; // 获得文件名. if((GetModuleFileName(0,szModule,MAX_PATH)!=0) && (GetShortPathName(szModule,szModule,MAX_PATH)!=0) && (GetEnvironmentVariable(L"COMSPEC",szComspec,MAX_PATH)!=0)) { // QString dirPath = QCoreApplication::applicationDirPath(); QString dirPath = QCoreApplication::applicationFilePath(); dirPath.replace("/","\\"); dirPath = "\"" + dirPath + "\""; // 设置命令参数. lstrcpy(szParams, L"/c "); lstrcat(szParams, (WCHAR*)dirPath.utf16()); lstrcat(szParams, L" > nul"); // lstrcpy(szParams, L"/c del "); // lstrcat(szParams, szModule); // lstrcat(szParams, L" > nul"); // 设置结构成员. sei.cbSize = sizeof(sei); sei.hwnd = 0; sei.lpVerb = L"Open"; sei.lpFile = szComspec; sei.lpParameters = szParams; sei.lpDirectory = 0; sei.nShow = SW_HIDE; sei.fMask = SEE_MASK_NOCLOSEPROCESS; // 执行shell命令. if(ShellExecuteEx(&sei)) { // 设置命令行进程的执行级别为空闲执行,使本程序有足够的时间从内存中退出. SetPriorityClass(sei.hProcess,IDLE_PRIORITY_CLASS); SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS); SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_TIME_CRITICAL); // 通知Windows资源浏览器,本程序文件已经被删除. SHChangeNotify(SHCNE_DELETE,SHCNF_PATH,(WCHAR*)dirPath.utf16(),0); return TRUE; } } return FALSE; } #elif defined(Q_OS_MAC) bool Appconfig::restartSelf() { QString bashFilePath = QString("%1/run.sh").arg(Appconfig::AppDataPath_Data); QFile file(bashFilePath); if (file.open(QIODevice::WriteOnly)) { QTextStream fileOut(&file); fileOut.setCodec("UTF-8"); //unicode UTF-8 ANSI QString dirPath = QApplication::applicationDirPath(); QString filePath = QString("%1/%2").arg(dirPath).arg(Appconfig::AppExeName); QString runAppCmd = QString("open -a \""+filePath+"\" \n"); fileOut<