[refactor] Some code refactors

This commit is contained in:
Leroy.H.Y 2020-01-01 18:56:06 +08:00
parent f85e697595
commit 16b9841034
No known key found for this signature in database
GPG Key ID: 6AC1673B587DC37D
9 changed files with 85 additions and 60 deletions

View File

@ -1 +1 @@
2525 2550

View File

@ -12,15 +12,17 @@
const int QV2RAY_CONFIG_VERSION = 6; const int QV2RAY_CONFIG_VERSION = 6;
// Linux users and DEs should handle the darkMode UI themselves. // Linux users and DEs should handle the darkMode UI themselves.
#ifndef Q_OS_LINUX #ifndef QV2RAY_USE_BUILTIN_DARKTHEME
#define QV2RAY_USE_BUILTIN_DARKTHEME # ifndef Q_OS_LINUX
# define QV2RAY_USE_BUILTIN_DARKTHEME
# endif
#endif #endif
// Base folder suffix. // Base folder suffix.
#ifdef QT_DEBUG #ifdef QT_DEBUG
#define QV2RAY_CONFIG_DIR_SUFFIX "_debug/" # define QV2RAY_CONFIG_DIR_SUFFIX "_debug/"
#else #else
#define QV2RAY_CONFIG_DIR_SUFFIX "/" # define QV2RAY_CONFIG_DIR_SUFFIX "/"
#endif #endif
// Get Configured Config Dir Path // Get Configured Config Dir Path
@ -37,15 +39,22 @@ const int QV2RAY_CONFIG_VERSION = 6;
#define QV2RAY_GENERATED_DIR (QV2RAY_CONFIG_DIR + "generated/") #define QV2RAY_GENERATED_DIR (QV2RAY_CONFIG_DIR + "generated/")
#define QV2RAY_GENERATED_FILE_PATH (QV2RAY_GENERATED_DIR + "config.gen.json") #define QV2RAY_GENERATED_FILE_PATH (QV2RAY_GENERATED_DIR + "config.gen.json")
#ifndef QV2RAY_DEFAULT_VCORE_PATH #if ! defined (QV2RAY_DEFAULT_VCORE_PATH) && ! defined (QV2RAY_DEFAULT_VASSETS_PATH)
#define QV2RAY_DEFAULT_VASSETS_PATH (QV2RAY_CONFIG_DIR + "vcore/") # define QV2RAY_DEFAULT_VASSETS_PATH (QV2RAY_CONFIG_DIR + "vcore/")
#ifdef Q_OS_WIN # ifdef Q_OS_WIN
#define QV2RAY_DEFAULT_VCORE_PATH (QV2RAY_CONFIG_DIR + "vcore/v2ray.exe") # define QV2RAY_DEFAULT_VCORE_PATH (QV2RAY_CONFIG_DIR + "vcore/v2ray.exe")
# else
# define QV2RAY_DEFAULT_VCORE_PATH (QV2RAY_CONFIG_DIR + "vcore/v2ray")
# endif
#elif defined (QV2RAY_DEFAULT_VCORE_PATH) && defined (QV2RAY_DEFAULT_VASSETS_PATH)
// ---- Using user-specified VCore and VAssets path
#else #else
#define QV2RAY_DEFAULT_VCORE_PATH (QV2RAY_CONFIG_DIR + "vcore/v2ray") # error Both QV2RAY_DEFAULT_VCORE_PATH and QV2RAY_DEFAULT_VASSETS_PATH need to present when specifying the paths.
#endif
#endif #endif
#undef _HAS_MANUAL_VCORE
#undef _HAS_MANUAL_VASSETS
#define QV2RAY_VCORE_LOG_DIRNAME "logs/" #define QV2RAY_VCORE_LOG_DIRNAME "logs/"
#define QV2RAY_VCORE_ACCESS_LOG_FILENAME "access.log" #define QV2RAY_VCORE_ACCESS_LOG_FILENAME "access.log"
#define QV2RAY_VCORE_ERROR_LOG_FILENAME "error.log" #define QV2RAY_VCORE_ERROR_LOG_FILENAME "error.log"
@ -60,13 +69,13 @@ const int QV2RAY_CONFIG_VERSION = 6;
#define BLACK(obj) \ #define BLACK(obj) \
obj->setPalette(this->palette()); obj->setPalette(this->palette());
#define QV2RAY_UI_RESOURCES_ROOT QString::fromStdString(QV2RAY_IS_DARKTHEME ? ":/icons/ui_dark/" : ":/icons/ui_light/") #define QV2RAY_UI_RESOURCES_ROOT (QV2RAY_IS_DARKTHEME ? QStringLiteral(":/icons/ui_dark/") : QStringLiteral(":/icons/ui_light/"))
#define QICON_R(file) QIcon(QV2RAY_UI_RESOURCES_ROOT + file) #define QICON_R(file) QIcon(QV2RAY_UI_RESOURCES_ROOT + file)
#define NEWLINE "\r\n" #define NEWLINE "\r\n"
#ifndef MAX #ifndef MAX
#define MAX(a, b) (((a) > (b)) ? (a) : (b)) # define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif #endif
using namespace std; using namespace std;

View File

@ -25,7 +25,7 @@ namespace Qv2ray
case 2: { case 2: {
// We copied those files. // We copied those files.
auto vCoreFilePath = root["v2CorePath"].toString(); auto vCoreFilePath = root["v2CorePath"].toString();
auto vCoreDestPath = QV2RAY_DEFAULT_VCORE_PATH; auto vCoreDestPath = QString(QV2RAY_DEFAULT_VCORE_PATH);
// We also need v2ctl // We also need v2ctl
auto v2CtlFilePath = QFileInfo(vCoreFilePath).dir().path() + "/v2ctl"; auto v2CtlFilePath = QFileInfo(vCoreFilePath).dir().path() + "/v2ctl";
auto v2CtlDestPath = QFileInfo(vCoreDestPath).dir().path() + "/v2ctl"; auto v2CtlDestPath = QFileInfo(vCoreDestPath).dir().path() + "/v2ctl";

View File

@ -146,8 +146,8 @@ bool initialiseQv2ray()
} }
Qv2rayConfig conf; Qv2rayConfig conf;
conf.v2AssetsPath = QV2RAY_DEFAULT_VASSETS_PATH; conf.v2AssetsPath = QString(QV2RAY_DEFAULT_VASSETS_PATH);
conf.v2CorePath = QV2RAY_DEFAULT_VCORE_PATH; conf.v2CorePath = QString(QV2RAY_DEFAULT_VCORE_PATH);
conf.logLevel = 3; conf.logLevel = 3;
// //
// Save initial config. // Save initial config.

View File

@ -332,7 +332,6 @@ void MainWindow::OnConfigListChanged(bool need_restart)
connections.clear(); connections.clear();
connectionListWidget->clear(); connectionListWidget->clear();
auto _regularConnections = GetRegularConnections(currentConfig.configs); auto _regularConnections = GetRegularConnections(currentConfig.configs);
//
auto _subsConnections = GetSubscriptionConnections(currentConfig.subscriptions.keys()); auto _subsConnections = GetSubscriptionConnections(currentConfig.subscriptions.keys());
for (auto i = 0; i < _regularConnections.count(); i++) { for (auto i = 0; i < _regularConnections.count(); i++) {
@ -562,7 +561,7 @@ void MainWindow::ShowAndSetConnection(QString guiConnectionName, bool SetConnect
} }
// Get Connection info // Get Connection info
auto host_port = MWGetConnectionPortNumber(currentSelectedName); auto host_port = MWGetConnectionInfo(currentSelectedName);
_hostLabel->setText(get<0>(host_port)); _hostLabel->setText(get<0>(host_port));
_portLabel->setText(QString::number(get<1>(host_port))); _portLabel->setText(QString::number(get<1>(host_port)));
_OutBoundTypeLabel->setText(get<2>(host_port)); _OutBoundTypeLabel->setText(get<2>(host_port));

View File

@ -45,15 +45,11 @@ class MainWindow : public QMainWindow, Ui::MainWindow
void UpdateVCoreLog(const QString &log); void UpdateVCoreLog(const QString &log);
void OnConfigListChanged(bool need_restart); void OnConfigListChanged(bool need_restart);
private slots: private slots:
void setMasterLogHBar();
void on_action_RCM_ShareQR_triggered(); void on_action_RCM_ShareQR_triggered();
void on_startButton_clicked(); void on_startButton_clicked();
void on_stopButton_clicked(); void on_stopButton_clicked();
void on_reconnectButton_clicked(); void on_reconnectButton_clicked();
void VersionUpdate(QByteArray &data);
void on_activatedTray(QSystemTrayIcon::ActivationReason reason); void on_activatedTray(QSystemTrayIcon::ActivationReason reason);
void ToggleVisibility();
void quit();
void on_actionExit_triggered(); void on_actionExit_triggered();
void on_preferencesBtn_clicked(); void on_preferencesBtn_clicked();
void on_connectionListWidget_doubleClicked(const QModelIndex &index); void on_connectionListWidget_doubleClicked(const QModelIndex &index);
@ -69,10 +65,14 @@ class MainWindow : public QMainWindow, Ui::MainWindow
void on_shareBtn_clicked(); void on_shareBtn_clicked();
void on_duplicateBtn_clicked(); void on_duplicateBtn_clicked();
void on_subsButton_clicked(); void on_subsButton_clicked();
//
void ToggleVisibility();
void setMasterLogHBar();
void VersionUpdate(QByteArray &data);
void quit();
public: public:
static MainWindow *mwInstance; static MainWindow *mwInstance;
CONFIGROOT CurrentFullConfig;
QString CurrentConnectionName = ""; QString CurrentConnectionName = "";
ConnectionInstance *vinstance; ConnectionInstance *vinstance;
@ -93,6 +93,7 @@ class MainWindow : public QMainWindow, Ui::MainWindow
void SetEditWidgetEnable(bool enabled); void SetEditWidgetEnable(bool enabled);
void ShowAndSetConnection(QString currentText, bool SetConnection, bool Apply); void ShowAndSetConnection(QString currentText, bool SetConnection, bool Apply);
Qv2rayConfig currentConfig; Qv2rayConfig currentConfig;
CONFIGROOT currentFullConfig;
// //
// Charts // Charts
QChartView *speedChartView; QChartView *speedChartView;
@ -148,7 +149,7 @@ class MainWindow : public QMainWindow, Ui::MainWindow
bool MWtryStartConnection(); bool MWtryStartConnection();
void MWStopConnection(); void MWStopConnection();
void MWTryPingConnection(const QString &alias); void MWTryPingConnection(const QString &alias);
tuple<QString, int, QString> MWGetConnectionPortNumber(const QString &alias); tuple<QString, int, QString> MWGetConnectionInfo(const QString &alias);
void MWSetSystemProxy(); void MWSetSystemProxy();
void MWClearSystemProxy(bool); void MWClearSystemProxy(bool);
}; };

View File

@ -106,8 +106,8 @@ void MainWindow::MWSetSystemProxy()
bool MainWindow::MWtryStartConnection() bool MainWindow::MWtryStartConnection()
{ {
auto connectionRoot = connections[CurrentConnectionName].config; auto connectionRoot = connections[CurrentConnectionName].config;
CurrentFullConfig = GenerateRuntimeConfig(connectionRoot); currentFullConfig = GenerateRuntimeConfig(connectionRoot);
bool startFlag = this->vinstance->StartConnection(CurrentFullConfig, currentConfig.connectionConfig.statsPort); bool startFlag = this->vinstance->StartConnection(currentFullConfig, currentConfig.connectionConfig.statsPort);
if (startFlag) { if (startFlag) {
bool usePAC = currentConfig.inboundConfig.pacConfig.enablePAC; bool usePAC = currentConfig.inboundConfig.pacConfig.enablePAC;
@ -177,7 +177,7 @@ void MainWindow::MWStopConnection()
void MainWindow::MWTryPingConnection(const QString &alias) void MainWindow::MWTryPingConnection(const QString &alias)
{ {
try { try {
auto info = MWGetConnectionPortNumber(alias); auto info = MWGetConnectionInfo(alias);
QString host = get<0>(info); QString host = get<0>(info);
int port = get<1>(info); int port = get<1>(info);
tcpingModel->StartPing(alias, host, port); tcpingModel->StartPing(alias, host, port);
@ -186,12 +186,11 @@ void MainWindow::MWTryPingConnection(const QString &alias)
} }
} }
tuple<QString, int, QString> MainWindow::MWGetConnectionPortNumber(const QString &alias) tuple<QString, int, QString> MainWindow::MWGetConnectionInfo(const QString &alias)
{ {
if (!connections.contains(alias)) if (!connections.contains(alias))
return make_tuple(tr("N/A"), 0, tr("N/A")); return make_tuple(tr("N/A"), 0, tr("N/A"));
auto root = connections[alias].config; return GetConnectionInfo(connections[alias].config);
return GetConnectionInfo(root);
} }

View File

@ -377,8 +377,8 @@ void PreferencesWindow::on_tProxyCheckBox_stateChanged(int arg1)
// Only trying to remove file when they are not in the default dir. // Only trying to remove file when they are not in the default dir.
// (In other words...) Keep using the current files. <Because we don't know where else we can copy the file from...> // (In other words...) Keep using the current files. <Because we don't know where else we can copy the file from...>
if (QFile(QV2RAY_DEFAULT_VCORE_PATH).exists()) { if (QFile(QV2RAY_DEFAULT_VCORE_PATH).exists()) {
LOG(MODULE_FILE, QV2RAY_DEFAULT_VCORE_PATH.toStdString() + ": File already exists.") LOG(MODULE_FILE, QString(QV2RAY_DEFAULT_VCORE_PATH).toStdString() + ": File already exists.")
LOG(MODULE_FILE, QV2RAY_DEFAULT_VCORE_PATH.toStdString() + ": Deleting file.") LOG(MODULE_FILE, QString(QV2RAY_DEFAULT_VCORE_PATH).toStdString() + ": Deleting file.")
QFile(QV2RAY_DEFAULT_VCORE_PATH).remove(); QFile(QV2RAY_DEFAULT_VCORE_PATH).remove();
} }

View File

@ -42,7 +42,7 @@
<enum>QTabWidget::Rounded</enum> <enum>QTabWidget::Rounded</enum>
</property> </property>
<property name="currentIndex"> <property name="currentIndex">
<number>0</number> <number>1</number>
</property> </property>
<widget class="QWidget" name="tab"> <widget class="QWidget" name="tab">
<attribute name="title"> <attribute name="title">
@ -525,7 +525,7 @@
</item> </item>
<item> <item>
<layout class="QGridLayout" name="gridLayout_7"> <layout class="QGridLayout" name="gridLayout_7">
<item row="0" column="0" rowspan="2"> <item row="0" column="0">
<widget class="QGroupBox" name="socksGroupBox"> <widget class="QGroupBox" name="socksGroupBox">
<property name="title"> <property name="title">
<string>SOCKS Settings</string> <string>SOCKS Settings</string>
@ -559,62 +559,79 @@
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_8">
<item>
<widget class="QCheckBox" name="socksUDPCB"> <widget class="QCheckBox" name="socksUDPCB">
<property name="text"> <property name="text">
<string>Enabled</string> <string>Enabled</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="0"> <item>
<spacer name="horizontalSpacer_5">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label_25"> <widget class="QLabel" name="label_25">
<property name="text"> <property name="text">
<string>Local IP (For UDP)</string> <string>IP:</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1"> <item>
<widget class="QLineEdit" name="socksUDPIP"> <widget class="QLineEdit" name="socksUDPIP">
<property name="placeholderText"> <property name="placeholderText">
<string notr="true">127.0.0.1</string> <string notr="true">127.0.0.1</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="0"> </layout>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_10"> <widget class="QLabel" name="label_10">
<property name="text"> <property name="text">
<string>Authentication</string> <string>Authentication</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="1"> <item row="2" column="1">
<widget class="QCheckBox" name="socksAuthCB"> <widget class="QCheckBox" name="socksAuthCB">
<property name="text"> <property name="text">
<string>Enabled</string> <string>Enabled</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="4" column="0"> <item row="3" column="0">
<widget class="QLabel" name="label_11"> <widget class="QLabel" name="label_11">
<property name="text"> <property name="text">
<string>Username</string> <string>Username</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="4" column="1"> <item row="3" column="1">
<widget class="QLineEdit" name="socksAuthUsernameTxt"> <widget class="QLineEdit" name="socksAuthUsernameTxt">
<property name="placeholderText"> <property name="placeholderText">
<string notr="true">user</string> <string notr="true">user</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="5" column="0"> <item row="4" column="0">
<widget class="QLabel" name="label_12"> <widget class="QLabel" name="label_12">
<property name="text"> <property name="text">
<string>Password</string> <string>Password</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="5" column="1"> <item row="4" column="1">
<widget class="QLineEdit" name="socksAuthPasswordTxt"> <widget class="QLineEdit" name="socksAuthPasswordTxt">
<property name="placeholderText"> <property name="placeholderText">
<string notr="true">pass</string> <string notr="true">pass</string>
@ -624,7 +641,7 @@
</layout> </layout>
</widget> </widget>
</item> </item>
<item row="0" column="1"> <item row="1" column="0">
<widget class="QGroupBox" name="httpGroupBox"> <widget class="QGroupBox" name="httpGroupBox">
<property name="title"> <property name="title">
<string>HTTP Settings</string> <string>HTTP Settings</string>
@ -695,7 +712,7 @@
</layout> </layout>
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="0" column="1" rowspan="2">
<widget class="QGroupBox" name="pacGroupBox"> <widget class="QGroupBox" name="pacGroupBox">
<property name="title"> <property name="title">
<string>PAC Settings</string> <string>PAC Settings</string>