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)
{
auto path = _path;
if (!path.endsWith("/"))
{
path.append("/");
}
// Does not exist.
if (!QDir(path).exists())
return false;
{
// A temp file used to test file permissions in that folder.
QFile testFile(path + ".qv2ray_file_write_test_file" + QSTRN(QTime::currentTime().msecsSinceStartOfDay()));
bool opened = testFile.open(QFile::OpenModeFlag::ReadWrite);
QFile testFile(path + ".qv2ray_test_file" + QSTRN(QTime::currentTime().msecsSinceStartOfDay()));
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, "---> Cannot create a new file or open a file for writing.")
return false;
}
else
{
testFile.write("Qv2ray test file, feel free to remove.");
testFile.flush();
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.
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.
QFile configFile(path + "Qv2ray.conf");
@ -211,63 +213,33 @@ namespace Qv2ray
if (!configFile.exists())
return false;
bool opened2 = configFile.open(QIODevice::ReadWrite);
try
if (!configFile.open(QIODevice::ReadWrite))
{
if (opened2)
{
// 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));
LOG(MODULE_SETTINGS, "File: " + configFile.fileName() + " cannot be opened!")
return false;
}
const auto err = VerifyJsonString(StringFromFile(configFile));
if (!err.isEmpty())
{
LOG(MODULE_INIT, "Json parse returns: " + err)
return false;
}
else
{
// If the file format is valid.
auto conf = JsonFromString(StringFromFile(configFile));
LOG(MODULE_SETTINGS,
"Path: " + path + " contains a config file, in version " + conf["config_version"].toVariant().toString())
const auto conf = JsonFromString(StringFromFile(configFile));
LOG(MODULE_SETTINGS, "Found a config file, v=" + conf["config_version"].toString() + " path=" + path)
configFile.close();
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[])
{
QCoreApplication consoleApp(argc, argv);
QString errorMessage;
const auto &args = consoleApp.arguments();
{
QCoreApplication coreApp(argc, argv);
const auto &args = coreApp.arguments();
Qv2rayProcessArgument.path = args.first();
Qv2rayProcessArgument.version = QV2RAY_VERSION_STRING;
Qv2rayProcessArgument.data = args.join(" ");
@ -287,6 +259,7 @@ namespace Qv2ray
break;
}
}
}
// noScaleFactors = disable HiDPI
if (StartupOption.noScaleFactor)
{

View File

@ -47,7 +47,10 @@ int main(int argc, char *argv[])
#endif
//
// 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);
if (app.SetupQv2ray())
{
@ -210,8 +213,8 @@ int main(int argc, char *argv[])
{
// Initialise Connection Handler
PluginHost = new QvPluginHost();
ConnectionManager = new QvConfigHandler(qvApp);
RouteManager = new RouteHandler(qvApp);
ConnectionManager = new QvConfigHandler();
RouteManager = new RouteHandler();
#ifdef Q_OS_LINUX
qvApp->setFallbackSessionManagementEnabled(false);
@ -234,6 +237,8 @@ int main(int argc, char *argv[])
signal(SIGUSR2, [](int) { ConnectionManager->StopConnection(); });
#endif
auto rcode = app.exec();
delete ConnectionManager;
delete RouteManager;
delete PluginHost;
LOG(MODULE_INIT, "Quitting normally")
return rcode;