Skip to content

Commit

Permalink
feat(background): Autostart method
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuamegnauth54 committed Sep 11, 2024
1 parent b62c1bc commit b1026bb
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions src/background.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// SPDX-License-Identifier: GPL-3.0-only

use std::sync::{Arc, Condvar, Mutex};
use std::{
fs, io,
sync::{Arc, Condvar, Mutex},
};

// use ashpd::enumflags2::{bitflags, BitFlag, BitFlags};
use cosmic::{iced::window, widget};
Expand Down Expand Up @@ -160,16 +163,55 @@ impl Background {

/// Enable or disable autostart for an application
///
/// Deprecated but seemingly still in use
/// Deprecated in terms of the portal but seemingly still in use
/// Spec: https://specifications.freedesktop.org/autostart-spec/latest/
pub async fn enable_autostart(
&self,
app_id: String,
enable: bool,
commandline: Vec<String>,
flags: u32,
) -> fdo::Result<bool> {
log::warn!("Autostart not implemented");
Ok(enable)
log::info!(
"{} autostart for {app_id}",
if enable { "Enabling" } else { "Disabling" }
);

let Some((autostart, launch_entry)) = dirs::config_dir().map(|config| {
let autostart = config.join("autostart");
(autostart, autostart.join(format!("{app_id}.desktop")))
}) else {
return Err(fdo::Error::FileNotFound("XDG_CONFIG_HOME".into()));
};

if !enable {
log::debug!("Removing autostart entry {}", launch_entry.display());
match fs::remove_file(launch_entry) {
Ok(()) => Ok(false),
Err(e) if e.kind() == io::ErrorKind::NotFound => {
log::debug!("Autostart entry for {app_id} doesn't exist");
Ok(false)
}
Err(e) => {
log::error!(
"Error removing autostart entry for {app_id}\n\tPath: {}\n\tError: {e}",
launch_entry.display()
);
Err(fdo::Error::IOError(format!(
"{e}: ({})",
launch_entry.display()
)))
}
}
} else {
match fs::create_dir(autostart) {
Ok(()) => log::debug!("Created autostart directory at {}", autostart.display()),
Err(e) if e.kind() == io::ErrorKind::AlreadyExists => (),
Err(e) => {}
}

Ok(true)
}
}

/// Emitted when running applications change their state
Expand Down

0 comments on commit b1026bb

Please sign in to comment.