Skip to content

Commit

Permalink
feat: auto detect console page code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mythologyli committed Dec 1, 2023
1 parent 5ae9222 commit d8617c0
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ find_package(Qt6 COMPONENTS
Widgets
Network
Svg
Core5Compat
REQUIRED
)

Expand Down Expand Up @@ -44,6 +45,7 @@ target_link_libraries(ZJUConnectForWindows
Qt::Widgets
Qt::Network
Qt::Svg
Qt::Core5Compat
SingleApplication::SingleApplication
QrCodeGenerator
wininet
Expand Down
8 changes: 4 additions & 4 deletions l2tpmode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand All @@ -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") &&
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 6 additions & 5 deletions networkdetector/networkdetector.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "networkdetector.h"
#include "../utils/utils.h"

NetworkDetector::NetworkDetector()
{
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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"))
Expand All @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
49 changes: 48 additions & 1 deletion utils/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <QMessageBox>
#include <QApplication>
#include <QSettings>
#include <QNetworkInterface>
#include <QTextCodec>

#include "windows.h"
#include "wininet.h"
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions utils/utils.h
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 1 addition & 1 deletion webloginmode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions zjuconnectcontroller/zjuconnectcontroller.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include "zjuconnectcontroller.h"
#include "../utils/utils.h"

ZjuConnectController::ZjuConnectController()
{
zjuConnectProcess = new QProcess(this);

connect(zjuConnectProcess, &QProcess::readyReadStandardOutput, this, [&]()
{
QString output = QString::fromLocal8Bit(zjuConnectProcess->readAllStandardOutput());
QString output = Utils::ConsoleOutputToQString(zjuConnectProcess->readAllStandardOutput());

emit outputRead(output);

Expand All @@ -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);

Expand Down

0 comments on commit d8617c0

Please sign in to comment.