From d62d5908d14a622cb402842c94c36abfb1098f97 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 27 Nov 2014 21:20:48 +0300 Subject: [PATCH] improved os x updater --- Telegram/SourceFiles/_other/updater_osx.m | 17 ++++++++++----- Telegram/SourceFiles/pspecific_linux.cpp | 14 ++++++++++-- Telegram/SourceFiles/pspecific_linux.h | 1 + Telegram/SourceFiles/pspecific_mac.cpp | 26 ++++++++++++++++------- Telegram/SourceFiles/pspecific_mac.h | 1 + Telegram/SourceFiles/pspecific_wnd.cpp | 18 ++++++++++++++-- Telegram/SourceFiles/pspecific_wnd.h | 1 + Telegram/SourceFiles/settings.cpp | 3 ++- Telegram/SourceFiles/settings.h | 1 + Telegram/Telegram.plist | 2 +- 10 files changed, 65 insertions(+), 19 deletions(-) diff --git a/Telegram/SourceFiles/_other/updater_osx.m b/Telegram/SourceFiles/_other/updater_osx.m index b531fb76d..293bf4e35 100644 --- a/Telegram/SourceFiles/_other/updater_osx.m +++ b/Telegram/SourceFiles/_other/updater_osx.m @@ -64,11 +64,17 @@ int main(int argc, const char * argv[]) { if (!path) { return -1; } - NSRange range = [path rangeOfString:appName options:NSBackwardsSearch]; + NSRange range = [path rangeOfString:@".app/" options:NSBackwardsSearch]; if (range.location == NSNotFound) { return -1; } - appDir = [path substringToIndex:range.location > 0 ? range.location : 0]; + path = [path substringToIndex:range.location > 0 ? range.location : 0]; + + range = [path rangeOfString:@"/" options:NSBackwardsSearch]; + NSString *appRealName = (range.location == NSNotFound) ? path : [path substringFromIndex:range.location + 1]; + appRealName = [[NSArray arrayWithObjects:appRealName, @".app", nil] componentsJoinedByString:@""]; + appDir = (range.location == NSNotFound) ? @"" : [path substringToIndex:range.location + 1]; + NSString *appDirFull = [appDir stringByAppendingString:appRealName]; openLog(); pid_t procId = 0; @@ -144,11 +150,12 @@ int main(int argc, const char * argv[]) { break; } NSString *pathPart = [srcPath substringFromIndex:r.length]; - if ([pathPart rangeOfString:appName].location != 0) { + r = [pathPart rangeOfString:appName]; + if (r.location != 0) { writeLog([@"Skipping not app file " stringByAppendingString:srcPath]); continue; } - NSString *dstPath = [appDir stringByAppendingString:pathPart]; + NSString *dstPath = [appDirFull stringByAppendingString:[pathPart substringFromIndex:r.length]]; NSError *error; NSNumber *isDirectory = nil; writeLog([[NSArray arrayWithObjects: @"Copying file ", srcPath, @" to ", dstPath, nil] componentsJoinedByString:@""]); @@ -180,7 +187,7 @@ int main(int argc, const char * argv[]) { delFolder(); } - NSString *appPath = [[NSArray arrayWithObjects:appDir, appName, nil] componentsJoinedByString:@""]; + NSString *appPath = [[NSArray arrayWithObjects:appDir, appRealName, nil] componentsJoinedByString:@""]; NSMutableArray *args = [[NSMutableArray alloc] initWithObjects:@"-noupdate", nil]; if (toSettings) [args addObject:@"-tosettings"]; if (_debug) [args addObject:@"-debug"]; diff --git a/Telegram/SourceFiles/pspecific_linux.cpp b/Telegram/SourceFiles/pspecific_linux.cpp index a8b7a1e57..6342bcae9 100644 --- a/Telegram/SourceFiles/pspecific_linux.cpp +++ b/Telegram/SourceFiles/pspecific_linux.cpp @@ -800,13 +800,23 @@ QString psCurrentExeDirectory(int argc, char *argv[]) { if (!first.isEmpty()) { QFileInfo info(first); if (info.exists()) { - QDir result(info.absolutePath()); - return result.absolutePath() + '/'; + return QDir(info.absolutePath()).absolutePath() + '/'; } } return QString(); } +QString psCurrentExeName(int argc, char *argv[]) { + QString first = argc ? QString::fromLocal8Bit(argv[0]) : QString(); + if (!first.isEmpty()) { + QFileInfo info(first); + if (info.exists()) { + return info.fileName(); + } + } + return QString(); +} + void psDoCleanup() { try { psAutoStart(false, true); diff --git a/Telegram/SourceFiles/pspecific_linux.h b/Telegram/SourceFiles/pspecific_linux.h index af64d15b2..e1411a025 100644 --- a/Telegram/SourceFiles/pspecific_linux.h +++ b/Telegram/SourceFiles/pspecific_linux.h @@ -167,6 +167,7 @@ QString psCurrentLanguage(); QString psAppDataPath(); QString psDownloadPath(); QString psCurrentExeDirectory(int argc, char *argv[]); +QString psCurrentExeName(int argc, char *argv[]); void psAutoStart(bool start, bool silent = false); void psSendToMenu(bool send, bool silent = false); diff --git a/Telegram/SourceFiles/pspecific_mac.cpp b/Telegram/SourceFiles/pspecific_mac.cpp index 547bad9d7..bc390b097 100644 --- a/Telegram/SourceFiles/pspecific_mac.cpp +++ b/Telegram/SourceFiles/pspecific_mac.cpp @@ -984,13 +984,23 @@ QString psCurrentExeDirectory(int argc, char *argv[]) { if (!first.isEmpty()) { QFileInfo info(first); if (info.exists()) { - QDir result(info.absolutePath() + qsl("/../../..")); - return result.absolutePath() + '/'; + return QDir(info.absolutePath() + qsl("/../../..")).absolutePath() + '/'; } } return QString(); } +QString psCurrentExeName(int argc, char *argv[]) { + QString first = argc ? QString::fromLocal8Bit(argv[0]) : QString(); + if (!first.isEmpty()) { + QFileInfo info(first); + if (info.exists()) { + return QDir(QDir(info.absolutePath() + qsl("/../..")).absolutePath()).dirName(); + } + } + return QString(); +} + void psDoCleanup() { try { psAutoStart(false, true); @@ -1042,11 +1052,11 @@ bool psCheckReadyUpdate() { } #ifdef Q_OS_WIN - QString curUpdater = (cExeDir() + "Updater.exe"); - QFileInfo updater(cWorkingDir() + "tupdates/ready/Updater.exe"); + QString curUpdater = (cExeDir() + qsl("Updater.exe")); + QFileInfo updater(cWorkingDir() + qsl("tupdates/ready/Updater.exe")); #elif defined Q_OS_MAC - QString curUpdater = (cExeDir() + "Telegram.app/Contents/Frameworks/Updater"); - QFileInfo updater(cWorkingDir() + "tupdates/ready/Telegram.app/Contents/Frameworks/Updater"); + QString curUpdater = (cExeDir() + cExeName() + qsl("/Contents/Frameworks/Updater")); + QFileInfo updater(cWorkingDir() + qsl("tupdates/ready/Telegram.app/Contents/Frameworks/Updater")); #endif if (!updater.exists()) { QFileInfo current(curUpdater); @@ -1069,8 +1079,8 @@ bool psCheckReadyUpdate() { return false; } #elif defined Q_OS_MAC - QFileInfo to(curUpdater); - QDir().mkpath(to.absolutePath()); + QDir().mkpath(QFileInfo(curUpdater).absolutePath()); + DEBUG_LOG(("Update Info: moving %1 to %2..").arg(updater.absoluteFilePath()).arg(curUpdater)); if (!objc_moveFile(updater.absoluteFilePath(), curUpdater)) { PsUpdateDownloader::clearAll(); return false; diff --git a/Telegram/SourceFiles/pspecific_mac.h b/Telegram/SourceFiles/pspecific_mac.h index 8bfaa4f2c..c0845fc0e 100644 --- a/Telegram/SourceFiles/pspecific_mac.h +++ b/Telegram/SourceFiles/pspecific_mac.h @@ -200,6 +200,7 @@ QString psCurrentLanguage(); QString psAppDataPath(); QString psDownloadPath(); QString psCurrentExeDirectory(int argc, char *argv[]); +QString psCurrentExeName(int argc, char *argv[]); void psAutoStart(bool start, bool silent = false); void psSendToMenu(bool send, bool silent = false); diff --git a/Telegram/SourceFiles/pspecific_wnd.cpp b/Telegram/SourceFiles/pspecific_wnd.cpp index 6912b252e..11ba71827 100644 --- a/Telegram/SourceFiles/pspecific_wnd.cpp +++ b/Telegram/SourceFiles/pspecific_wnd.cpp @@ -1966,6 +1966,20 @@ QString psCurrentExeDirectory(int argc, char *argv[]) { return QString(); } +QString psCurrentExeName(int argc, char *argv[]) { + LPWSTR *args; + int argsCount; + args = CommandLineToArgvW(GetCommandLine(), &argsCount); + if (args) { + QFileInfo info(QDir::fromNativeSeparators(QString::fromWCharArray(args[0]))); + if (info.isFile()) { + return info.fileName(); + } + LocalFree(args); + } + return QString(); +} + void psDoCleanup() { try { psAutoStart(false, true); @@ -2077,8 +2091,8 @@ bool psCheckReadyUpdate() { } } - QString curUpdater = (cExeDir() + "Updater.exe"); - QFileInfo updater(cWorkingDir() + "tupdates/ready/Updater.exe"); + QString curUpdater = (cExeDir() + qsl("Updater.exe")); + QFileInfo updater(cWorkingDir() + qsl("tupdates/ready/Updater.exe")); if (!updater.exists()) { QFileInfo current(curUpdater); if (!current.exists()) { diff --git a/Telegram/SourceFiles/pspecific_wnd.h b/Telegram/SourceFiles/pspecific_wnd.h index 5a8e97c1a..39f094bc1 100644 --- a/Telegram/SourceFiles/pspecific_wnd.h +++ b/Telegram/SourceFiles/pspecific_wnd.h @@ -178,6 +178,7 @@ QString psAppDataPath(); QString psAppDataPathOld(); QString psDownloadPath(); QString psCurrentExeDirectory(int argc, char *argv[]); +QString psCurrentExeName(int argc, char *argv[]); void psAutoStart(bool start, bool silent = false); void psSendToMenu(bool send, bool silent = false); diff --git a/Telegram/SourceFiles/settings.cpp b/Telegram/SourceFiles/settings.cpp index a3959e33c..387c7f5c1 100644 --- a/Telegram/SourceFiles/settings.cpp +++ b/Telegram/SourceFiles/settings.cpp @@ -23,7 +23,7 @@ bool gTestMode = false; bool gDebug = false; bool gManyInstance = false; QString gKeyFile; -QString gWorkingDir, gExeDir; +QString gWorkingDir, gExeDir, gExeName; QStringList gSendPaths; @@ -106,6 +106,7 @@ void settingsParseArgs(int argc, char *argv[]) { } memset_rand(&gInstance, sizeof(gInstance)); gExeDir = psCurrentExeDirectory(argc, argv); + gExeName = psCurrentExeName(argc, argv); for (int32 i = 0; i < argc; ++i) { if (string("-release") == argv[i]) { gTestMode = false; diff --git a/Telegram/SourceFiles/settings.h b/Telegram/SourceFiles/settings.h index 9cd54f0ad..2e29bc90a 100644 --- a/Telegram/SourceFiles/settings.h +++ b/Telegram/SourceFiles/settings.h @@ -62,6 +62,7 @@ inline void cForceWorkingDir(const QString &newDir) { QDir dir; if (!gWorkingDir.isEmpty()) dir.mkpath(gWorkingDir); } +DeclareReadSetting(QString, ExeName); DeclareReadSetting(QString, ExeDir); DeclareSetting(QString, DialogLastPath); DeclareSetting(QString, DialogHelperPath); diff --git a/Telegram/Telegram.plist b/Telegram/Telegram.plist index c5acc4123..9158401f3 100644 --- a/Telegram/Telegram.plist +++ b/Telegram/Telegram.plist @@ -7,7 +7,7 @@ CFBundleGetInfoString Telegram Desktop messaging app CFBundleIdentifier - com.tdesktop.${PRODUCT_NAME:rfc1034identifier} + com.tdesktop.$(PRODUCT_NAME:rfc1034identifier) CFBundlePackageType APPL CFBundleShortVersionString