Fix single instance socket path for compatibility with flatpak

This commit is contained in:
Ilya Fedin 2020-01-22 07:12:07 +04:00 committed by John Preston
parent 3a748e20c2
commit ffe037f9f1
7 changed files with 45 additions and 19 deletions

View File

@ -98,12 +98,7 @@ int Sandbox::start() {
const auto d = QFile::encodeName(QDir(cWorkingDir()).absolutePath());
char h[33] = { 0 };
hashMd5Hex(d.constData(), d.size(), h);
#ifndef OS_MAC_STORE
_localServerName = psServerPrefix() + h + '-' + cGUIDStr();
#else // OS_MAC_STORE
h[4] = 0; // use only first 4 chars
_localServerName = psServerPrefix() + h;
#endif // OS_MAC_STORE
_localServerName = Platform::SingleInstanceLocalServerName(h);
connect(
&_localSocket,

View File

@ -95,6 +95,13 @@ void SetApplicationIcon(const QIcon &icon) {
QApplication::setWindowIcon(icon);
}
bool InSandbox() {
static const auto Sandbox = QFileInfo::exists(
QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation)
+ qsl("/flatpak-info"));
return Sandbox;
}
QString CurrentExecutablePath(int argc, char *argv[]) {
constexpr auto kMaxPath = 1024;
char result[kMaxPath] = { 0 };
@ -112,6 +119,23 @@ QString CurrentExecutablePath(int argc, char *argv[]) {
return argc ? QFile::decodeName(argv[0]) : QString();
}
QString SingleInstanceLocalServerName(const QString &hash) {
const auto runtimeDir = QStandardPaths::writableLocation(
QStandardPaths::RuntimeLocation);
if (InSandbox()) {
return runtimeDir
+ qsl("/app/")
+ QString::fromUtf8(qgetenv("FLATPAK_ID"))
+ '/' + hash;
} else if (QFileInfo::exists(runtimeDir)) {
return runtimeDir + '/' + hash + '-' + cGUIDStr();
} else { // non-systemd distros
return QStandardPaths::writableLocation(QStandardPaths::TempLocation)
+ '/' + hash + '-' + cGUIDStr();
}
}
} // namespace Platform
namespace {

View File

@ -20,8 +20,12 @@ namespace Platform {
inline void SetWatchingMediaKeys(bool watching) {
}
bool InSandbox();
QString CurrentExecutablePath(int argc, char *argv[]);
QString SingleInstanceLocalServerName(const QString &hash);
inline std::optional<crl::time> LastUserInputTime() {
return std::nullopt;
}
@ -31,9 +35,6 @@ inline void IgnoreApplicationActivationRightNow() {
} // namespace Platform
inline QString psServerPrefix() {
return qsl("/tmp/");
}
inline void psCheckLocalSocket(const QString &serverName) {
QFile address(serverName);
if (address.exists()) {

View File

@ -18,6 +18,8 @@ namespace Platform {
QString CurrentExecutablePath(int argc, char *argv[]);
QString SingleInstanceLocalServerName(const QString &hash);
void RemoveQuarantine(const QString &path);
namespace ThirdParty {
@ -31,13 +33,6 @@ inline void finish() {
} // namespace ThirdParty
} // namespace Platform
inline QString psServerPrefix() {
#ifndef OS_MAC_STORE
return qsl("/tmp/");
#else // OS_MAC_STORE
return objc_documentsPath();
#endif // OS_MAC_STORE
}
inline void psCheckLocalSocket(const QString &serverName) {
QFile address(serverName);
if (address.exists()) {

View File

@ -120,6 +120,14 @@ QString CurrentExecutablePath(int argc, char *argv[]) {
return NS2QString([[NSBundle mainBundle] bundlePath]);
}
QString SingleInstanceLocalServerName(const QString &hash) {
#ifndef OS_MAC_STORE
return qsl("/tmp/") + hash + '-' + cGUIDStr();
#else // OS_MAC_STORE
return objc_documentsPath() + hash.left(4);
#endif // OS_MAC_STORE
}
void RemoveQuarantine(const QString &path) {
const auto kQuarantineAttribute = "com.apple.quarantine";

View File

@ -325,6 +325,10 @@ QString CurrentExecutablePath(int argc, char *argv[]) {
return QString();
}
QString SingleInstanceLocalServerName(const QString &hash) {
return qsl("Global\\") + hash + '-' + cGUIDStr();
}
std::optional<crl::time> LastUserInputTime() {
auto lii = LASTINPUTINFO{ 0 };
lii.cbSize = sizeof(LASTINPUTINFO);

View File

@ -21,6 +21,8 @@ inline void SetWatchingMediaKeys(bool watching) {
QString CurrentExecutablePath(int argc, char *argv[]);
QString SingleInstanceLocalServerName(const QString &hash);
inline void IgnoreApplicationActivationRightNow() {
}
@ -34,9 +36,6 @@ inline void finish() {
} // namespace ThirdParty
} // namespace Platform
inline QString psServerPrefix() {
return qsl("Global\\");
}
inline void psCheckLocalSocket(const QString &) {
}