Add methods to detect appimage, static binary and forced gtk dialog

This commit is contained in:
Ilya Fedin 2020-04-25 09:45:46 +04:00 committed by John Preston
parent 422831fa79
commit 7f890122e6
2 changed files with 108 additions and 38 deletions

View File

@ -194,14 +194,7 @@ bool GenerateDesktopFile(
QFile target(targetFile); QFile target(targetFile);
if (target.open(QIODevice::WriteOnly)) { if (target.open(QIODevice::WriteOnly)) {
#ifdef DESKTOP_APP_USE_PACKAGED if (IsStaticBinary() || InAppImage()) {
fileText = fileText.replace(
QRegularExpression(
qsl("^Exec=(.*) -- %u$"),
QRegularExpression::MultilineOption),
qsl("Exec=\\1")
+ (args.isEmpty() ? QString() : ' ' + args));
#else // DESKTOP_APP_USE_PACKAGED
fileText = fileText.replace( fileText = fileText.replace(
QRegularExpression( QRegularExpression(
qsl("^TryExec=.*$"), qsl("^TryExec=.*$"),
@ -217,7 +210,15 @@ bool GenerateDesktopFile(
+ EscapeShell(QFile::encodeName(cExeDir() + cExeName())) + EscapeShell(QFile::encodeName(cExeDir() + cExeName()))
.replace('\\', qsl("\\\\")) .replace('\\', qsl("\\\\"))
+ (args.isEmpty() ? QString() : ' ' + args)); + (args.isEmpty() ? QString() : ' ' + args));
#endif // !DESKTOP_APP_USE_PACKAGED } else {
fileText = fileText.replace(
QRegularExpression(
qsl("^Exec=(.*) -- %u$"),
QRegularExpression::MultilineOption),
qsl("Exec=\\1")
+ (args.isEmpty() ? QString() : ' ' + args));
}
target.write(fileText.toUtf8()); target.write(fileText.toUtf8());
target.close(); target.close();
@ -249,6 +250,27 @@ bool InSnap() {
return Snap; return Snap;
} }
bool InAppImage() {
static const auto AppImage = qEnvironmentVariableIsSet("APPIMAGE");
return AppImage;
}
bool IsStaticBinary() {
#ifdef DESKTOP_APP_USE_PACKAGED
return false;
#else // DESKTOP_APP_USE_PACKAGED
return true;
#endif // !DESKTOP_APP_USE_PACKAGED
}
bool IsGtkFileDialogForced() {
#ifdef TDESKTOP_FORCE_GTK_FILE_DIALOG
return true;
#else // TDESKTOP_FORCE_GTK_FILE_DIALOG
return false;
#endif // !TDESKTOP_FORCE_GTK_FILE_DIALOG
}
bool IsXDGDesktopPortalPresent() { bool IsXDGDesktopPortalPresent() {
#ifdef TDESKTOP_DISABLE_DBUS_INTEGRATION #ifdef TDESKTOP_DISABLE_DBUS_INTEGRATION
static const auto XDGDesktopPortalPresent = false; static const auto XDGDesktopPortalPresent = false;
@ -266,7 +288,11 @@ bool UseXDGDesktopPortal() {
const auto envVar = qEnvironmentVariableIsSet("TDESKTOP_USE_PORTAL"); const auto envVar = qEnvironmentVariableIsSet("TDESKTOP_USE_PORTAL");
const auto portalPresent = IsXDGDesktopPortalPresent(); const auto portalPresent = IsXDGDesktopPortalPresent();
return (DesktopEnvironment::IsKDE() || envVar) && portalPresent; return (
DesktopEnvironment::IsKDE()
|| InSnap()
|| envVar
) && portalPresent;
}(); }();
return UsePortal; return UsePortal;
@ -288,7 +314,7 @@ QString ProcessNameByPID(const QString &pid) {
return QString(); return QString();
} }
QString CurrentExecutablePath(int argc, char *argv[]) { QString RealExecutablePath(int argc, char *argv[]) {
const auto processName = ProcessNameByPID(qsl("self")); const auto processName = ProcessNameByPID(qsl("self"));
// Fallback to the first command line argument. // Fallback to the first command line argument.
@ -299,6 +325,25 @@ QString CurrentExecutablePath(int argc, char *argv[]) {
: QString(); : QString();
} }
QString CurrentExecutablePath(int argc, char *argv[]) {
if (InAppImage()) {
const auto appimagePath = QString::fromUtf8(qgetenv("APPIMAGE"));
const auto appimagePathList = appimagePath.split('/');
if (qEnvironmentVariableIsSet("ARGV0")
&& appimagePathList.size() >= 5
&& appimagePathList[1] == qstr("run")
&& appimagePathList[2] == qstr("user")
&& appimagePathList[4] == qstr("appimagelauncherfs")) {
return QString::fromUtf8(qgetenv("ARGV0"));
}
return appimagePath;
}
return RealExecutablePath(argc, argv);
}
QString AppRuntimeDirectory() { QString AppRuntimeDirectory() {
static const auto RuntimeDirectory = [&] { static const auto RuntimeDirectory = [&] {
auto runtimeDir = QStandardPaths::writableLocation( auto runtimeDir = QStandardPaths::writableLocation(
@ -356,6 +401,20 @@ QString GetLauncherBasename() {
.arg(cExeName()); .arg(cExeName());
} }
if (InAppImage()) {
const auto appimagePath = qsl("file://%1%2")
.arg(cExeDir())
.arg(cExeName())
.toUtf8();
char md5Hash[33] = { 0 };
hashMd5Hex(appimagePath.constData(), appimagePath.size(), md5Hash);
return qsl("appimagekit_%1-%2")
.arg(md5Hash)
.arg(AppName.utf16().replace(' ', '_'));
}
const auto possibleBasenames = std::vector<QString>{ const auto possibleBasenames = std::vector<QString>{
qsl(MACRO_TO_STRING(TDESKTOP_LAUNCHER_BASENAME)), qsl(MACRO_TO_STRING(TDESKTOP_LAUNCHER_BASENAME)),
qsl("Telegram") qsl("Telegram")
@ -564,6 +623,10 @@ namespace Platform {
void start() { void start() {
LOG(("Launcher filename: %1").arg(GetLauncherFilename())); LOG(("Launcher filename: %1").arg(GetLauncherFilename()));
if (InAppImage()) {
qputenv("LIBGL_ALWAYS_INDIRECT", "1");
}
#ifdef TDESKTOP_USE_FONTCONFIG_FALLBACK #ifdef TDESKTOP_USE_FONTCONFIG_FALLBACK
FallbackFontConfig(); FallbackFontConfig();
#endif // TDESKTOP_USE_FONTCONFIG_FALLBACK #endif // TDESKTOP_USE_FONTCONFIG_FALLBACK
@ -571,7 +634,10 @@ void start() {
qputenv("PULSE_PROP_application.name", AppName.utf8()); qputenv("PULSE_PROP_application.name", AppName.utf8());
qputenv("PULSE_PROP_application.icon_name", GetIconName().toLatin1()); qputenv("PULSE_PROP_application.icon_name", GetIconName().toLatin1());
#ifdef TDESKTOP_FORCE_GTK_FILE_DIALOG if(IsStaticBinary()
|| InAppImage()
|| InSnap()
|| IsGtkFileDialogForced()) {
LOG(("Checking for XDG Desktop Portal...")); LOG(("Checking for XDG Desktop Portal..."));
// this can give us a chance to use a proper file dialog for current session // this can give us a chance to use a proper file dialog for current session
if (IsXDGDesktopPortalPresent()) { if (IsXDGDesktopPortalPresent()) {
@ -585,7 +651,7 @@ void start() {
} else { } else {
LOG(("XDG Desktop Portal is not present :(")); LOG(("XDG Desktop Portal is not present :("));
} }
#endif // TDESKTOP_FORCE_GTK_FILE_DIALOG }
} }
void finish() { void finish() {

View File

@ -22,11 +22,15 @@ inline void SetWatchingMediaKeys(bool watching) {
bool InSandbox(); bool InSandbox();
bool InSnap(); bool InSnap();
bool InAppImage();
bool IsStaticBinary();
bool IsGtkFileDialogForced();
bool IsXDGDesktopPortalPresent(); bool IsXDGDesktopPortalPresent();
bool UseXDGDesktopPortal(); bool UseXDGDesktopPortal();
QString ProcessNameByPID(const QString &pid); QString ProcessNameByPID(const QString &pid);
QString RealExecutablePath(int argc, char *argv[]);
QString CurrentExecutablePath(int argc, char *argv[]); QString CurrentExecutablePath(int argc, char *argv[]);
QString AppRuntimeDirectory(); QString AppRuntimeDirectory();