Skip to content

Commit 78be554

Browse files
feat(background): Autostart method
1 parent dd9d729 commit 78be554

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

src/background.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// SPDX-License-Identifier: GPL-3.0-only
22

3-
use std::sync::{Arc, Condvar, Mutex};
3+
use std::{
4+
fs, io,
5+
sync::{Arc, Condvar, Mutex},
6+
};
47

58
// use ashpd::enumflags2::{bitflags, BitFlag, BitFlags};
69
use cosmic::{iced::window, widget};
@@ -160,16 +163,49 @@ impl Background {
160163

161164
/// Enable or disable autostart for an application
162165
///
163-
/// Deprecated but seemingly still in use
166+
/// Deprecated in terms of the portal but seemingly still in use
167+
/// Spec: https://specifications.freedesktop.org/autostart-spec/latest/
164168
pub async fn enable_autostart(
165169
&self,
166170
app_id: String,
167171
enable: bool,
168172
commandline: Vec<String>,
169173
flags: u32,
170174
) -> fdo::Result<bool> {
171-
log::warn!("Autostart not implemented");
172-
Ok(enable)
175+
log::info!(
176+
"{} autostart for {app_id}",
177+
if enable { "Enabling" } else { "Disabling" }
178+
);
179+
180+
let Some((autostart, launch_entry)) = dirs::config_dir().map(|config| {
181+
let autostart = config.join("autostart");
182+
(autostart, autostart.join(format!("{app_id}.desktop")))
183+
}) else {
184+
return fdo::Error::FileNotFound("XDG_CONFIG_HOME");
185+
};
186+
187+
if !enable {
188+
log::debug!("Removing autostart entry {}", launch_entry.display());
189+
match fs::remove_file(launch_entry) {
190+
Ok(_) => Ok(false),
191+
Err(e) if e.kind() == io::ErrorKind::NotFound => {
192+
log::debug!("Autostart entry for {app_id} doesn't exist");
193+
Ok(false)
194+
}
195+
Err(e) => {
196+
log::error!(
197+
"Error removing autostart entry for {app_id}\n\tPath: {}\n\tError: {e}",
198+
launch_entry.display()
199+
);
200+
Err(fdo::Error::IOError(format!(
201+
"{e}: ({})",
202+
launch_entry.display()
203+
)))
204+
}
205+
}
206+
} else {
207+
Ok(enable)
208+
}
173209
}
174210

175211
/// Emitted when running applications change their state

0 commit comments

Comments
 (0)