diff --git a/CMakeLists.txt b/CMakeLists.txt index 64525bb..16195e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ find_package(Qt6 COMPONENTS Widgets Network Svg + Core5Compat REQUIRED ) @@ -44,6 +45,7 @@ target_link_libraries(ZJUConnectForWindows Qt::Widgets Qt::Network Qt::Svg + Qt::Core5Compat SingleApplication::SingleApplication QrCodeGenerator wininet diff --git a/l2tpmode.cpp b/l2tpmode.cpp index 28b8f57..cbcbc2c 100644 --- a/l2tpmode.cpp +++ b/l2tpmode.cpp @@ -48,7 +48,7 @@ void MainWindow::setModeToL2tp() disconnect(processForL2tp, &QProcess::finished, nullptr, nullptr); connect(processForL2tp, &QProcess::finished, this, [&]() { - QString output = QString::fromLocal8Bit(processForL2tp->readAllStandardOutput()).trimmed(); + QString output = Utils::ConsoleOutputToQString(processForL2tp->readAllStandardOutput()).trimmed(); if (output.isEmpty()) { addLog("警告:在系统中未发现 VPN 配置!请使用高级 - 创建 L2TP VPN 创建 VPN"); @@ -122,7 +122,7 @@ void MainWindow::setModeToL2tp() disconnect(processForL2tp, &QProcess::finished, nullptr, nullptr); connect(processForL2tp, &QProcess::finished, this, [&]() { - QString output = QString::fromLocal8Bit(processForL2tp->readAllStandardOutput()).trimmed(); + QString output = Utils::ConsoleOutputToQString(processForL2tp->readAllStandardOutput()).trimmed(); if (output.contains("命令已完成") || output.contains("Command completed successfully")) { isL2tpLinked = true; @@ -138,7 +138,7 @@ void MainWindow::setModeToL2tp() disconnect(processForL2tpCheck, &QProcess::finished, nullptr, nullptr); connect(processForL2tpCheck, &QProcess::finished, this, [&]() { - QString output = QString::fromLocal8Bit( + QString output = Utils::ConsoleOutputToQString( processForL2tpCheck->readAllStandardOutput() ).trimmed(); if (output.contains("(0%") && !output.contains("unreachable") && @@ -229,7 +229,7 @@ void MainWindow::setModeToL2tp() disconnect(processForL2tp, &QProcess::finished, nullptr, nullptr); connect(processForL2tp, &QProcess::finished, this, [&]() { - QString output = QString::fromLocal8Bit(processForL2tp->readAllStandardOutput()); + QString output = Utils::ConsoleOutputToQString(processForL2tp->readAllStandardOutput()); if (output.contains("命令已完成") || output.contains("Command completed successfully")) { isL2tpLinked = false; diff --git a/mainwindow.cpp b/mainwindow.cpp index 68c758c..259d4d7 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -130,7 +130,7 @@ MainWindow::MainWindow(QWidget *parent) : disconnect(process, &QProcess::finished, nullptr, nullptr); connect(process, &QProcess::finished, this, [&]() { - QString output = QString::fromLocal8Bit(process->readAllStandardError()); + QString output = Utils::ConsoleOutputToQString(process->readAllStandardError()); if (!output.contains("Add-VpnConnection")) { HINSTANCE hInstance = ShellExecute( diff --git a/networkdetector/networkdetector.cpp b/networkdetector/networkdetector.cpp index a385f43..2737237 100644 --- a/networkdetector/networkdetector.cpp +++ b/networkdetector/networkdetector.cpp @@ -1,4 +1,5 @@ #include "networkdetector.h" +#include "../utils/utils.h" NetworkDetector::NetworkDetector() { @@ -23,7 +24,7 @@ void NetworkDetector::start() disconnect(process, &QProcess::finished, this, nullptr); connect(process, &QProcess::finished, this, [&]() { - QString output = QString::fromLocal8Bit(process->readAllStandardOutput()); + QString output = Utils::ConsoleOutputToQString(process->readAllStandardOutput()); if (!output.contains("timed out")) { result.isDefaultDnsAvailable = true; @@ -44,7 +45,7 @@ void NetworkDetector::start() disconnect(process, &QProcess::finished, this, nullptr); connect(process, &QProcess::finished, this, [&]() { - QString output = QString::fromLocal8Bit(process->readAllStandardOutput()); + QString output = Utils::ConsoleOutputToQString(process->readAllStandardOutput()); if (!output.contains("timed out")) { result.isZjuNet = true; @@ -68,7 +69,7 @@ void NetworkDetector::checkZjuWlan() disconnect(process, &QProcess::finished, this, nullptr); connect(process, &QProcess::finished, this, [&]() { - QString output = QString::fromLocal8Bit(process->readAllStandardOutput()); + QString output = Utils::ConsoleOutputToQString(process->readAllStandardOutput()); if (output.contains("ZJUWLAN")) { if (output.contains("ZJUWLAN-Secure")) @@ -95,7 +96,7 @@ void NetworkDetector::checkZjuLan() disconnect(process, &QProcess::finished, this, nullptr); connect(process, &QProcess::finished, this, [&]() { - QString output = QString::fromLocal8Bit(process->readAllStandardOutput()); + QString output = Utils::ConsoleOutputToQString(process->readAllStandardOutput()); QString line; QTextStream stream(&output); @@ -151,7 +152,7 @@ void NetworkDetector::checkInternet() disconnect(process, &QProcess::finished, this, nullptr); connect(process, &QProcess::finished, this, [&]() { - QString output = QString::fromLocal8Bit(process->readAllStandardOutput()); + QString output = Utils::ConsoleOutputToQString(process->readAllStandardOutput()); if (output.contains("(0%") && !output.contains("unreachable") && !output.contains("无法")) { result.isInternetAvailable = true; diff --git a/utils/utils.cpp b/utils/utils.cpp index 5b43c76..5e8d214 100644 --- a/utils/utils.cpp +++ b/utils/utils.cpp @@ -1,7 +1,7 @@ #include #include -#include #include +#include #include "windows.h" #include "wininet.h" @@ -10,6 +10,53 @@ #include "utils.h" +QString Utils::ConsoleOutputToQString(const QByteArray &byteArray) +{ + static QString codeName = ""; + if (codeName == "UTF-8") + { + return QTextCodec::codecForName("UTF-8")->toUnicode(byteArray);; + } + else if (codeName == "GBK") + { + return QTextCodec::codecForName("GBK")->toUnicode(byteArray);; + } + else if (codeName == "locale") + { + return QString::fromLocal8Bit(byteArray); + } + else + { + QTextCodec *utf8Codec = QTextCodec::codecForName("UTF-8"); + QString utf8Str = utf8Codec->toUnicode(byteArray); + QByteArray utf8ByteArrayBack = utf8Codec->fromUnicode(utf8Str); + + QTextCodec *gbkCodec = QTextCodec::codecForName("GBK"); + QString gbkStr = gbkCodec->toUnicode(byteArray); + QByteArray gbkByteArrayBack = gbkCodec->fromUnicode(gbkStr); + + if (utf8ByteArrayBack == byteArray || gbkByteArrayBack != byteArray) + { + codeName = "UTF-8"; + return utf8Str; + } + else if (gbkByteArrayBack == byteArray || utf8ByteArrayBack != byteArray) + { + codeName = "GBK"; + return QTextCodec::codecForName("GBK")->toUnicode(byteArray);; + } + else if (gbkByteArrayBack == byteArray && utf8ByteArrayBack == byteArray) + { + return utf8Str; + } + else + { + codeName = "locale"; + return QString::fromLocal8Bit(byteArray); + } + } +} + void Utils::setWidgetFixedWhenHidden(QWidget *widget) { QSizePolicy originPolicy = widget->sizePolicy(); diff --git a/utils/utils.h b/utils/utils.h index a0dceea..f0a0851 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -1,10 +1,10 @@ #ifndef UTILS_H #define UTILS_H -#include "../networkdetector/networkdetector.h" - namespace Utils { + QString ConsoleOutputToQString(const QByteArray &byteArray); + void setWidgetFixedWhenHidden(QWidget *widget); void showAboutMessageBox(QWidget *parent = nullptr); diff --git a/webloginmode.cpp b/webloginmode.cpp index ccf083f..7d12d22 100644 --- a/webloginmode.cpp +++ b/webloginmode.cpp @@ -69,7 +69,7 @@ void MainWindow::setModeToWebLogin() disconnect(processForWebLogin, &QProcess::finished, nullptr, nullptr); connect(processForWebLogin, &QProcess::finished, this, [&]() { - QString output = QString::fromLocal8Bit(processForWebLogin->readAllStandardOutput()).trimmed(); + QString output = Utils::ConsoleOutputToQString(processForWebLogin->readAllStandardOutput()).trimmed(); if (output.contains("[Login Successful]") || output.contains("[Already Online]")) { isWebLogged = true; diff --git a/zjuconnectcontroller/zjuconnectcontroller.cpp b/zjuconnectcontroller/zjuconnectcontroller.cpp index 3323b56..3330474 100644 --- a/zjuconnectcontroller/zjuconnectcontroller.cpp +++ b/zjuconnectcontroller/zjuconnectcontroller.cpp @@ -1,4 +1,5 @@ #include "zjuconnectcontroller.h" +#include "../utils/utils.h" ZjuConnectController::ZjuConnectController() { @@ -6,7 +7,7 @@ ZjuConnectController::ZjuConnectController() connect(zjuConnectProcess, &QProcess::readyReadStandardOutput, this, [&]() { - QString output = QString::fromLocal8Bit(zjuConnectProcess->readAllStandardOutput()); + QString output = Utils::ConsoleOutputToQString(zjuConnectProcess->readAllStandardOutput()); emit outputRead(output); @@ -22,7 +23,7 @@ ZjuConnectController::ZjuConnectController() connect(zjuConnectProcess, &QProcess::readyReadStandardError, this, [&]() { - QString output = QString::fromLocal8Bit(zjuConnectProcess->readAllStandardError()); + QString output = Utils::ConsoleOutputToQString(zjuConnectProcess->readAllStandardError()); emit outputRead(output);