2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-09-05 00:46:08 +00:00

Move relaunch / update logic to Core::Launcher.

Also pass "-workdir" argument through relaunch / update.

Fixes #4149.
This commit is contained in:
John Preston
2017-12-12 11:52:53 +04:00
parent 97c15865a5
commit 251176df47
21 changed files with 505 additions and 415 deletions

View File

@@ -29,6 +29,8 @@ namespace Core {
namespace {
QStringList ReadArguments(int argc, char *argv[]) {
Expects(argc >= 0);
auto result = QStringList();
result.reserve(argc);
for (auto i = 0; i != argc; ++i) {
@@ -47,7 +49,7 @@ Launcher::Launcher(int argc, char *argv[])
: _argc(argc)
, _argv(argv)
, _arguments(ReadArguments(argc, argv)) {
InitFromCommandLine(argc, argv);
prepareSettings();
}
void Launcher::init() {
@@ -84,12 +86,14 @@ int Launcher::exec() {
#ifndef TDESKTOP_DISABLE_AUTOUPDATE
if (cRestartingUpdate()) {
DEBUG_LOG(("Application Info: executing updater to install update..."));
psExecUpdater();
if (!launchUpdater(UpdaterLaunch::PerformUpdate)) {
psDeleteDir(cWorkingDir() + qsl("tupdates/temp"));
}
} else
#endif // !TDESKTOP_DISABLE_AUTOUPDATE
if (cRestarting()) {
DEBUG_LOG(("Application Info: executing Telegram, because of restart..."));
psExecTelegram();
launchUpdater(UpdaterLaunch::JustRelaunch);
}
CrashReports::Finish();
@@ -103,4 +107,134 @@ QString Launcher::argumentsString() const {
return _arguments.join(' ');
}
void Launcher::prepareSettings() {
#ifdef Q_OS_MAC
#ifndef OS_MAC_OLD
if (QSysInfo::macVersion() >= QSysInfo::MV_10_11) {
gIsElCapitan = true;
}
#else // OS_MAC_OLD
if (QSysInfo::macVersion() < QSysInfo::MV_10_7) {
gIsSnowLeopard = true;
}
#endif // OS_MAC_OLD
#endif // Q_OS_MAC
switch (cPlatform()) {
case dbipWindows:
gUpdateURL = QUrl(qsl("http://tdesktop.com/win/tupdates/current"));
#ifndef OS_WIN_STORE
gPlatformString = qsl("Windows");
#else // OS_WIN_STORE
gPlatformString = qsl("WinStore");
#endif // OS_WIN_STORE
break;
case dbipMac:
gUpdateURL = QUrl(qsl("http://tdesktop.com/mac/tupdates/current"));
#ifndef OS_MAC_STORE
gPlatformString = qsl("MacOS");
#else // OS_MAC_STORE
gPlatformString = qsl("MacAppStore");
#endif // OS_MAC_STORE
break;
case dbipMacOld:
gUpdateURL = QUrl(qsl("http://tdesktop.com/mac32/tupdates/current"));
gPlatformString = qsl("MacOSold");
break;
case dbipLinux64:
gUpdateURL = QUrl(qsl("http://tdesktop.com/linux/tupdates/current"));
gPlatformString = qsl("Linux64bit");
break;
case dbipLinux32:
gUpdateURL = QUrl(qsl("http://tdesktop.com/linux32/tupdates/current"));
gPlatformString = qsl("Linux32bit");
break;
}
auto path = Platform::CurrentExecutablePath(_argc, _argv);
LOG(("Executable path before check: %1").arg(path));
if (!path.isEmpty()) {
auto info = QFileInfo(path);
if (info.isSymLink()) {
info = info.symLinkTarget();
}
if (info.exists()) {
gExeDir = info.absoluteDir().absolutePath() + '/';
gExeName = info.fileName();
}
}
if (cExeName().isEmpty()) {
LOG(("WARNING: Could not compute executable path, some features will be disabled."));
}
processArguments();
}
void Launcher::processArguments() {
enum class KeyFormat {
NoValues,
OneValue,
AllLeftValues,
};
auto parseMap = std::map<QByteArray, KeyFormat> {
{ "-testmode" , KeyFormat::NoValues },
{ "-debug" , KeyFormat::NoValues },
{ "-many" , KeyFormat::NoValues },
{ "-key" , KeyFormat::OneValue },
{ "-autostart" , KeyFormat::NoValues },
{ "-fixprevious", KeyFormat::NoValues },
{ "-cleanup" , KeyFormat::NoValues },
{ "-noupdate" , KeyFormat::NoValues },
{ "-tosettings" , KeyFormat::NoValues },
{ "-startintray", KeyFormat::NoValues },
{ "-sendpath" , KeyFormat::AllLeftValues },
{ "-workdir" , KeyFormat::OneValue },
{ "--" , KeyFormat::OneValue },
};
auto parseResult = QMap<QByteArray, QStringList>();
auto parsingKey = QByteArray();
auto parsingFormat = KeyFormat::NoValues;
for (const auto &argument : _arguments) {
switch (parsingFormat) {
case KeyFormat::OneValue: {
parseResult[parsingKey] = QStringList(argument.mid(0, 8192));
parsingFormat = KeyFormat::NoValues;
} break;
case KeyFormat::AllLeftValues: {
parseResult[parsingKey].push_back(argument.mid(0, 8192));
} break;
case KeyFormat::NoValues: {
parsingKey = argument.toLatin1();
auto it = parseMap.find(parsingKey);
if (it != parseMap.end()) {
parsingFormat = it->second;
parseResult[parsingKey] = QStringList();
}
} break;
}
}
gTestMode = parseResult.contains("-testmode");
gDebug = parseResult.contains("-debug");
gManyInstance = parseResult.contains("-many");
gKeyFile = parseResult.value("-key", QStringList()).join(QString());
gLaunchMode = parseResult.contains("-autostart") ? LaunchModeAutoStart
: parseResult.contains("-fixprevious") ? LaunchModeFixPrevious
: parseResult.contains("-cleanup") ? LaunchModeCleanup
: LaunchModeNormal;
gNoStartUpdate = parseResult.contains("-noupdate");
gStartToSettings = parseResult.contains("-tosettings");
gStartInTray = parseResult.contains("-startintray");
gSendPaths = parseResult.value("-sendpath", QStringList());
gWorkingDir = parseResult.value("-workdir", QStringList()).join(QString());
if (!gWorkingDir.isEmpty()) {
if (QDir().exists(gWorkingDir)) {
_customWorkingDir = true;
} else {
gWorkingDir = QString();
}
}
gStartUrl = parseResult.value("--", QStringList()).join(QString());
}
} // namespace Core