1
0
Fork 0

macOS: detect and warn if running translocated

Translocation forces us to rebuild the nav-cache each launch, and might
mess up some other details, so let’s warn the user they should copy
the app to Applications or similar.
This commit is contained in:
James Turner 2021-01-10 12:51:41 +00:00
parent 39f3a83a7e
commit 3fd54b2477
3 changed files with 54 additions and 0 deletions

View file

@ -46,4 +46,12 @@ void transformToForegroundApp();
*/
void cocoaRegisterTerminateHandler();
/**
* @brief helper to detect if we're running translocated or not.
* Google 'Gatekeep app translation' for more info about this; basically it
* happens when the user runs us directly from the DMG, and this makes
* for very nasty file paths.
*/
bool cocoaIsRunningTranslocated();
#endif // of FG_GUI_COCOA_HELPERS_H

View file

@ -24,6 +24,7 @@
// standard
#include <string>
#include <dlfcn.h>
// OS
@ -264,3 +265,30 @@ void cocoaRegisterTerminateHandler()
}
bool cocoaIsRunningTranslocated()
{
// #define NSAppKitVersionNumber10_11 1404
if (floor(NSAppKitVersionNumber) <= 1404) {
return false;
}
void *handle = dlopen("/System/Library/Frameworks/Security.framework/Security", RTLD_LAZY);
if (handle == NULL) {
return false;
}
bool isTranslocated = false;
using SecIsTranslocatedFunc = Boolean (*)(CFURLRef path, bool *isTranslocated, CFErrorRef * __nullable error);
SecIsTranslocatedFunc f = reinterpret_cast<SecIsTranslocatedFunc>(dlsym(handle, "SecTranslocateIsTranslocatedURL"));
if (f != NULL) {
auto url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
f(url, &isTranslocated, NULL);
CFRelease(url);
}
dlclose(handle);
return isTranslocated;
}

View file

@ -76,6 +76,10 @@
#include "PathListModel.hxx"
#include "UnitsModel.hxx"
#if defined(SG_MAC)
#include <GUI/CocoaHelpers.h>
#endif
using namespace flightgear;
using namespace simgear::pkg;
using std::string;
@ -366,6 +370,20 @@ void initApp(int& argc, char** argv, bool doInitQSettings)
// http://doc.qt.io/qt-5/qcoreapplication.html#details
::setlocale(LC_NUMERIC, "C");
::setlocale(LC_COLLATE, "C");
#if defined(SG_MAC)
if (cocoaIsRunningTranslocated()) {
addSentryBreadcrumb("did show translocation warning", "info");
const char* titleKey = QT_TRANSLATE_NOOP("macTranslationWarning", "Application running from download location");
const char* key = QT_TRANSLATE_NOOP("macTranslationWarning", "FlightGear is running from the download image. For better performance and to avoid potential problems, "
"please copy FlightGear to some other location, such as your desktop or Applications folder.");
QString msg = qApp->translate("macTranslationWarning", key);
QString title = qApp->translate("macTranslationWarning", titleKey);
QMessageBox::warning(nullptr, title, msg);
}
#endif
}
if (doInitQSettings) {