mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Rewrite GenerateDesktopFile using Glib::KeyFile and KShell
This commit is contained in:
parent
899ab9a16a
commit
a94dd22caa
1 changed files with 86 additions and 56 deletions
|
@ -401,11 +401,10 @@ void LaunchGApplication() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
|
||||||
|
|
||||||
bool GenerateDesktopFile(
|
bool GenerateDesktopFile(
|
||||||
const QString &targetPath,
|
const QString &targetPath,
|
||||||
const QString &args,
|
const QStringList &args = {},
|
||||||
bool silent = false) {
|
bool silent = false) {
|
||||||
if (targetPath.isEmpty() || cExeName().isEmpty()) {
|
if (targetPath.isEmpty() || cExeName().isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -418,7 +417,6 @@ bool GenerateDesktopFile(
|
||||||
const auto targetFile = targetPath + QGuiApplication::desktopFileName();
|
const auto targetFile = targetPath + QGuiApplication::desktopFileName();
|
||||||
|
|
||||||
QString fileText;
|
QString fileText;
|
||||||
|
|
||||||
QFile source(sourceFile);
|
QFile source(sourceFile);
|
||||||
if (source.open(QIODevice::ReadOnly)) {
|
if (source.open(QIODevice::ReadOnly)) {
|
||||||
QTextStream s(&source);
|
QTextStream s(&source);
|
||||||
|
@ -431,65 +429,95 @@ bool GenerateDesktopFile(
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QFile target(targetFile);
|
try {
|
||||||
if (target.open(QIODevice::WriteOnly)) {
|
const auto target = Glib::KeyFile::create();
|
||||||
fileText = fileText.replace(
|
target->load_from_data(
|
||||||
QRegularExpression(
|
fileText.toStdString(),
|
||||||
qsl("^TryExec=.*$"),
|
Glib::KeyFile::Flags::KEEP_COMMENTS
|
||||||
QRegularExpression::MultilineOption),
|
| Glib::KeyFile::Flags::KEEP_TRANSLATIONS);
|
||||||
qsl("TryExec=%1").arg(
|
|
||||||
QString(cExeDir() + cExeName()).replace('\\', "\\\\")));
|
|
||||||
|
|
||||||
fileText = fileText.replace(
|
for (const auto &group : target->get_groups()) {
|
||||||
QRegularExpression(
|
if (target->has_key(group, "TryExec")) {
|
||||||
qsl("^Exec=telegram-desktop(.*)$"),
|
target->set_string(
|
||||||
QRegularExpression::MultilineOption),
|
group,
|
||||||
qsl("Exec=%1\\1").arg(
|
"TryExec",
|
||||||
KShell::joinArgs(QStringList{
|
KShell::joinArgs({ cExeDir() + cExeName() }).replace(
|
||||||
cExeDir() + cExeName(),
|
'\\',
|
||||||
} + (Core::Sandbox::Instance().customWorkingDir()
|
qstr("\\\\")).toStdString());
|
||||||
? QStringList{ "-workdir", cWorkingDir() }
|
}
|
||||||
: QStringList{})).replace('\\', "\\\\")));
|
|
||||||
|
|
||||||
fileText = fileText.replace(
|
if (target->has_key(group, "Exec")) {
|
||||||
QRegularExpression(
|
if (group == "Desktop Entry" && !args.isEmpty()) {
|
||||||
qsl("^Exec=(.*) -- %u$"),
|
QStringList exec;
|
||||||
QRegularExpression::MultilineOption),
|
exec.append(cExeDir() + cExeName());
|
||||||
qsl("Exec=\\1%1").arg(
|
if (Core::Sandbox::Instance().customWorkingDir()) {
|
||||||
args.isEmpty() ? QString() : ' ' + args));
|
exec.append(qsl("-workdir"));
|
||||||
|
exec.append(cWorkingDir());
|
||||||
|
}
|
||||||
|
exec.append(args);
|
||||||
|
target->set_string(
|
||||||
|
group,
|
||||||
|
"Exec",
|
||||||
|
KShell::joinArgs(exec).replace(
|
||||||
|
'\\',
|
||||||
|
qstr("\\\\")).toStdString());
|
||||||
|
} else {
|
||||||
|
auto exec = KShell::splitArgs(
|
||||||
|
QString::fromStdString(
|
||||||
|
target->get_string(group, "Exec")
|
||||||
|
).replace(
|
||||||
|
qstr("\\\\"),
|
||||||
|
qstr("\\")));
|
||||||
|
|
||||||
target.write(fileText.toUtf8());
|
if (!exec.isEmpty()) {
|
||||||
target.close();
|
exec[0] = cExeDir() + cExeName();
|
||||||
|
if (Core::Sandbox::Instance().customWorkingDir()) {
|
||||||
|
exec.insert(1, qsl("-workdir"));
|
||||||
|
exec.insert(2, cWorkingDir());
|
||||||
|
}
|
||||||
|
target->set_string(
|
||||||
|
group,
|
||||||
|
"Exec",
|
||||||
|
KShell::joinArgs(exec).replace(
|
||||||
|
'\\',
|
||||||
|
qstr("\\\\")).toStdString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!Core::UpdaterDisabled()) {
|
target->save_to_file(targetFile.toStdString());
|
||||||
DEBUG_LOG(("App Info: removing old .desktop files"));
|
|
||||||
QFile::remove(qsl("%1telegram.desktop").arg(targetPath));
|
|
||||||
QFile::remove(qsl("%1telegramdesktop.desktop").arg(targetPath));
|
|
||||||
|
|
||||||
const auto appimagePath = qsl("file://%1%2").arg(
|
|
||||||
cExeDir(),
|
|
||||||
cExeName()).toUtf8();
|
|
||||||
|
|
||||||
char md5Hash[33] = { 0 };
|
|
||||||
hashMd5Hex(
|
|
||||||
appimagePath.constData(),
|
|
||||||
appimagePath.size(),
|
|
||||||
md5Hash);
|
|
||||||
|
|
||||||
QFile::remove(qsl("%1appimagekit_%2-%3.desktop").arg(
|
|
||||||
targetPath,
|
|
||||||
md5Hash,
|
|
||||||
AppName.utf16().replace(' ', '_')));
|
|
||||||
}
|
}
|
||||||
|
} catch (const std::exception &e) {
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
if (!silent) {
|
if (!silent) {
|
||||||
LOG(("App Error: Could not open '%1' for write").arg(targetFile));
|
LOG(("App Error: %1").arg(QString::fromStdString(e.what())));
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!Core::UpdaterDisabled()) {
|
||||||
|
DEBUG_LOG(("App Info: removing old .desktop files"));
|
||||||
|
QFile::remove(qsl("%1telegram.desktop").arg(targetPath));
|
||||||
|
QFile::remove(qsl("%1telegramdesktop.desktop").arg(targetPath));
|
||||||
|
|
||||||
|
const auto appimagePath = qsl("file://%1%2").arg(
|
||||||
|
cExeDir(),
|
||||||
|
cExeName()).toUtf8();
|
||||||
|
|
||||||
|
char md5Hash[33] = { 0 };
|
||||||
|
hashMd5Hex(
|
||||||
|
appimagePath.constData(),
|
||||||
|
appimagePath.size(),
|
||||||
|
md5Hash);
|
||||||
|
|
||||||
|
QFile::remove(qsl("%1appimagekit_%2-%3.desktop").arg(
|
||||||
|
targetPath,
|
||||||
|
md5Hash,
|
||||||
|
AppName.utf16().replace(' ', '_')));
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
@ -551,6 +579,7 @@ bool AutostartSupported() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void AutostartToggle(bool enabled, Fn<void(bool)> done) {
|
void AutostartToggle(bool enabled, Fn<void(bool)> done) {
|
||||||
|
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
const auto guard = gsl::finally([&] {
|
const auto guard = gsl::finally([&] {
|
||||||
if (done) {
|
if (done) {
|
||||||
done(enabled);
|
done(enabled);
|
||||||
|
@ -559,20 +588,19 @@ void AutostartToggle(bool enabled, Fn<void(bool)> done) {
|
||||||
|
|
||||||
const auto silent = !done;
|
const auto silent = !done;
|
||||||
if (KSandbox::isFlatpak()) {
|
if (KSandbox::isFlatpak()) {
|
||||||
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
|
||||||
PortalAutostart(enabled, silent);
|
PortalAutostart(enabled, silent);
|
||||||
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
|
||||||
} else {
|
} else {
|
||||||
const auto autostart = QStandardPaths::writableLocation(
|
const auto autostart = QStandardPaths::writableLocation(
|
||||||
QStandardPaths::GenericConfigLocation)
|
QStandardPaths::GenericConfigLocation)
|
||||||
+ qsl("/autostart/");
|
+ qsl("/autostart/");
|
||||||
|
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
GenerateDesktopFile(autostart, qsl("-autostart"), silent);
|
GenerateDesktopFile(autostart, { qsl("-autostart") }, silent);
|
||||||
} else {
|
} else {
|
||||||
QFile::remove(autostart + QGuiApplication::desktopFileName());
|
QFile::remove(autostart + QGuiApplication::desktopFileName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AutostartSkip() {
|
bool AutostartSkip() {
|
||||||
|
@ -716,6 +744,7 @@ void finish() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void InstallLauncher(bool force) {
|
void InstallLauncher(bool force) {
|
||||||
|
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
static const auto DisabledByEnv = !qEnvironmentVariableIsEmpty(
|
static const auto DisabledByEnv = !qEnvironmentVariableIsEmpty(
|
||||||
"DESKTOPINTEGRATION");
|
"DESKTOPINTEGRATION");
|
||||||
|
|
||||||
|
@ -728,7 +757,7 @@ void InstallLauncher(bool force) {
|
||||||
const auto applicationsPath = QStandardPaths::writableLocation(
|
const auto applicationsPath = QStandardPaths::writableLocation(
|
||||||
QStandardPaths::ApplicationsLocation) + '/';
|
QStandardPaths::ApplicationsLocation) + '/';
|
||||||
|
|
||||||
GenerateDesktopFile(applicationsPath, qsl("-- %u"));
|
GenerateDesktopFile(applicationsPath);
|
||||||
|
|
||||||
const auto icons = QStandardPaths::writableLocation(
|
const auto icons = QStandardPaths::writableLocation(
|
||||||
QStandardPaths::GenericDataLocation) + qsl("/icons/");
|
QStandardPaths::GenericDataLocation) + qsl("/icons/");
|
||||||
|
@ -752,6 +781,7 @@ void InstallLauncher(bool force) {
|
||||||
QProcess::execute("update-desktop-database", {
|
QProcess::execute("update-desktop-database", {
|
||||||
applicationsPath
|
applicationsPath
|
||||||
});
|
});
|
||||||
|
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
}
|
}
|
||||||
|
|
||||||
PermissionStatus GetPermissionStatus(PermissionType type) {
|
PermissionStatus GetPermissionStatus(PermissionType type) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue