Check autostart enabling success on Linux

This commit is contained in:
Ilya Fedin 2023-01-02 12:50:33 +04:00 committed by John Preston
parent 2afa2cd9ab
commit c56977cbc1

View file

@ -119,11 +119,13 @@ namespace {
constexpr auto kDesktopFile = ":/misc/org.telegram.desktop.desktop"_cs;
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
void PortalAutostart(bool start, bool silent) {
bool PortalAutostart(bool start, bool silent) {
if (cExeName().isEmpty()) {
return;
return false;
}
auto error = false;
try {
const auto connection = Gio::DBus::Connection::get_sync(
Gio::DBus::BusType::SESSION);
@ -182,14 +184,18 @@ void PortalAutostart(bool start, bool silent) {
const auto response = base::Platform::GlibVariantCast<
uint>(parameters.get_child(0));
if (response && !silent) {
LOG(("Portal Autostart Error: Request denied"));
if (response) {
if (!silent) {
LOG(("Portal Autostart Error: Request denied"));
}
error = true;
}
} catch (const std::exception &e) {
if (!silent) {
LOG(("Portal Autostart Error: %1").arg(
QString::fromStdString(e.what())));
}
error = true;
}
loop->quit();
@ -227,7 +233,10 @@ void PortalAutostart(bool start, bool silent) {
LOG(("Portal Autostart Error: %1").arg(
QString::fromStdString(e.what())));
}
error = true;
}
return !error;
}
void LaunchGApplication() {
@ -620,25 +629,31 @@ bool AutostartSupported() {
void AutostartToggle(bool enabled, Fn<void(bool)> done) {
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
const auto guard = gsl::finally([&] {
if (done) {
done(enabled);
}
});
const auto success = [&] {
const auto silent = !done;
if (KSandbox::isFlatpak()) {
return PortalAutostart(enabled, silent);
}
const auto silent = !done;
if (KSandbox::isFlatpak()) {
PortalAutostart(enabled, silent);
} else {
const auto autostart = QStandardPaths::writableLocation(
QStandardPaths::GenericConfigLocation)
+ u"/autostart/"_q;
if (enabled) {
GenerateDesktopFile(autostart, { u"-autostart"_q }, true, silent);
} else {
QFile::remove(autostart + QGuiApplication::desktopFileName());
if (!enabled) {
return QFile::remove(
autostart + QGuiApplication::desktopFileName());
}
return GenerateDesktopFile(
autostart,
{ u"-autostart"_q },
true,
silent);
}();
if (done) {
done(enabled && success);
}
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
}