refactor: main.cpp refactor, fixed a crash

This commit is contained in:
Qv2ray-dev 2020-06-13 22:33:57 +08:00
parent fce84ece23
commit 22e853f04c
3 changed files with 69 additions and 91 deletions

View File

@ -1 +1 @@
5579 5580

View File

@ -168,32 +168,30 @@ namespace Qv2ray
bool Qv2rayApplication::CheckSettingsPathAvailability(const QString &_path, bool checkExistingConfig) bool Qv2rayApplication::CheckSettingsPathAvailability(const QString &_path, bool checkExistingConfig)
{ {
auto path = _path; auto path = _path;
if (!path.endsWith("/")) if (!path.endsWith("/"))
{
path.append("/"); path.append("/");
}
// Does not exist. // Does not exist.
if (!QDir(path).exists()) if (!QDir(path).exists())
return false; return false;
{
// A temp file used to test file permissions in that folder. // A temp file used to test file permissions in that folder.
QFile testFile(path + ".qv2ray_file_write_test_file" + QSTRN(QTime::currentTime().msecsSinceStartOfDay())); QFile testFile(path + ".qv2ray_test_file" + QSTRN(QTime::currentTime().msecsSinceStartOfDay()));
bool opened = testFile.open(QFile::OpenModeFlag::ReadWrite);
if (!opened) if (!testFile.open(QFile::OpenModeFlag::ReadWrite))
{ {
LOG(MODULE_SETTINGS, "Directory at: " + path + " cannot be used as a valid config file path.") LOG(MODULE_SETTINGS, "Directory at: " + path + " cannot be used as a valid config file path.")
LOG(MODULE_SETTINGS, "---> Cannot create a new file or open a file for writing.") LOG(MODULE_SETTINGS, "---> Cannot create a new file or open a file for writing.")
return false; return false;
} }
else
{
testFile.write("Qv2ray test file, feel free to remove."); testFile.write("Qv2ray test file, feel free to remove.");
testFile.flush(); testFile.flush();
testFile.close(); testFile.close();
bool removed = testFile.remove();
if (!removed) if (!testFile.remove())
{ {
// This is rare, as we can create a file but failed to remove it. // This is rare, as we can create a file but failed to remove it.
LOG(MODULE_SETTINGS, "Directory at: " + path + " cannot be used as a valid config file path.") LOG(MODULE_SETTINGS, "Directory at: " + path + " cannot be used as a valid config file path.")
@ -202,8 +200,12 @@ namespace Qv2ray
} }
} }
if (checkExistingConfig) if (!checkExistingConfig)
{ {
// Just pass the test
return true;
}
// Check if an existing config is found. // Check if an existing config is found.
QFile configFile(path + "Qv2ray.conf"); QFile configFile(path + "Qv2ray.conf");
@ -211,63 +213,33 @@ namespace Qv2ray
if (!configFile.exists()) if (!configFile.exists())
return false; return false;
bool opened2 = configFile.open(QIODevice::ReadWrite); if (!configFile.open(QIODevice::ReadWrite))
try
{ {
if (opened2) LOG(MODULE_SETTINGS, "File: " + configFile.fileName() + " cannot be opened!")
{ return false;
// Verify if the config can be loaded. }
// Failed to parse if we changed the file structure...
// Original:
// -- auto conf =
// StructFromJsonString<Qv2rayConfig>(configFile.readAll());
//
// Verify JSON file format. (only) because this file version may
// not be upgraded and may contain unsupported structure.
auto err = VerifyJsonString(StringFromFile(configFile));
const auto err = VerifyJsonString(StringFromFile(configFile));
if (!err.isEmpty()) if (!err.isEmpty())
{ {
LOG(MODULE_INIT, "Json parse returns: " + err) LOG(MODULE_INIT, "Json parse returns: " + err)
return false; return false;
} }
else
{
// If the file format is valid. // If the file format is valid.
auto conf = JsonFromString(StringFromFile(configFile)); const auto conf = JsonFromString(StringFromFile(configFile));
LOG(MODULE_SETTINGS, LOG(MODULE_SETTINGS, "Found a config file, v=" + conf["config_version"].toString() + " path=" + path)
"Path: " + path + " contains a config file, in version " + conf["config_version"].toVariant().toString())
configFile.close(); configFile.close();
return true; return true;
} }
}
else
{
LOG(MODULE_SETTINGS, "File: " + configFile.fileName() + " cannot be opened!")
return false;
}
}
catch (...)
{
LOG(MODULE_SETTINGS, "Exception raised when checking config: " + configFile.fileName())
// LOG(INIT, e->what())
QvMessageBoxWarn(nullptr, tr("Warning"), tr("Qv2ray cannot load the config file from here:") + NEWLINE + configFile.fileName());
return false;
}
}
else
{
return true;
}
}
bool Qv2rayApplication::PreInitilize(int argc, char *argv[]) bool Qv2rayApplication::PreInitilize(int argc, char *argv[])
{ {
QCoreApplication consoleApp(argc, argv);
QString errorMessage; QString errorMessage;
const auto &args = consoleApp.arguments(); {
QCoreApplication coreApp(argc, argv);
const auto &args = coreApp.arguments();
Qv2rayProcessArgument.path = args.first(); Qv2rayProcessArgument.path = args.first();
Qv2rayProcessArgument.version = QV2RAY_VERSION_STRING; Qv2rayProcessArgument.version = QV2RAY_VERSION_STRING;
Qv2rayProcessArgument.data = args.join(" "); Qv2rayProcessArgument.data = args.join(" ");
@ -287,6 +259,7 @@ namespace Qv2ray
break; break;
} }
} }
}
// noScaleFactors = disable HiDPI // noScaleFactors = disable HiDPI
if (StartupOption.noScaleFactor) if (StartupOption.noScaleFactor)
{ {

View File

@ -47,7 +47,10 @@ int main(int argc, char *argv[])
#endif #endif
// //
// parse the command line before starting as a Qt application // parse the command line before starting as a Qt application
Qv2rayApplication::PreInitilize(argc, argv); if (!Qv2rayApplication::PreInitilize(argc, argv))
{
return -1;
}
Qv2rayApplication app(argc, argv); Qv2rayApplication app(argc, argv);
if (app.SetupQv2ray()) if (app.SetupQv2ray())
{ {
@ -210,8 +213,8 @@ int main(int argc, char *argv[])
{ {
// Initialise Connection Handler // Initialise Connection Handler
PluginHost = new QvPluginHost(); PluginHost = new QvPluginHost();
ConnectionManager = new QvConfigHandler(qvApp); ConnectionManager = new QvConfigHandler();
RouteManager = new RouteHandler(qvApp); RouteManager = new RouteHandler();
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX
qvApp->setFallbackSessionManagementEnabled(false); qvApp->setFallbackSessionManagementEnabled(false);
@ -234,6 +237,8 @@ int main(int argc, char *argv[])
signal(SIGUSR2, [](int) { ConnectionManager->StopConnection(); }); signal(SIGUSR2, [](int) { ConnectionManager->StopConnection(); });
#endif #endif
auto rcode = app.exec(); auto rcode = app.exec();
delete ConnectionManager;
delete RouteManager;
delete PluginHost; delete PluginHost;
LOG(MODULE_INIT, "Quitting normally") LOG(MODULE_INIT, "Quitting normally")
return rcode; return rcode;