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