Skip to content

Commit

Permalink
Remove files from old entries
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanabx committed Apr 15, 2024
1 parent 0ce25b0 commit 19b9480
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 22 deletions.
31 changes: 20 additions & 11 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ use std::error::Error;
use std::fmt::Display;
use std::path::{Path, PathBuf};

use async_std::sync::Arc;
use async_std::sync::Mutex;
use std::fs::{self, create_dir_all};
use zbus::interface;
use zbus::message::Header;
use zbus::names::OwnedUniqueName;
use zbus::object_server::SignalContext;

use crate::desktop_entry::validate_desktop_entry;
use crate::types::{DesktopEntry, IconEntry};
use crate::types::{DesktopEntry, EntryCatalog, IconEntry};

pub struct Daemon {
pub data_dir: PathBuf,
pub entries: HashMap<OwnedUniqueName, DesktopEntry>,
pub icons: HashMap<OwnedUniqueName, IconEntry>,
pub change_handlers: HashSet<OwnedUniqueName>,
pub catalog: Arc<Mutex<EntryCatalog>>,
}

#[interface(name = "net.ryanabx.DesktopEntry")]
Expand Down Expand Up @@ -53,8 +53,10 @@ impl Daemon {
path: desktop_file_path.clone(),
};
let _ = Daemon::entry_changed(&ctxt, &new_entry.appid).await;
self.entries
.insert(OwnedUniqueName::from(sender.clone()), new_entry);
self.catalog
.lock()
.await
.add_desktop_entry(OwnedUniqueName::from(sender.clone()), new_entry);
Ok(())
}
Err(e) => {
Expand Down Expand Up @@ -138,8 +140,10 @@ impl Daemon {
icon_path: self.data_dir.join(Path::new(f_path)),
};
let _ = Daemon::icon_changed(&ctxt, &new_entry.icon_name).await;
self.icons
.insert(OwnedUniqueName::from(sender.clone()), new_entry);
self.catalog
.lock()
.await
.add_icon(OwnedUniqueName::from(sender.clone()), new_entry);
Ok(())
}
Err(e) => {
Expand Down Expand Up @@ -175,8 +179,10 @@ impl Daemon {
icon_path: self.data_dir.join(Path::new(f_path)),
};
let _ = Daemon::icon_changed(&ctxt, &new_entry.icon_name).await;
self.icons
.insert(OwnedUniqueName::from(sender.clone()), new_entry);
self.catalog
.lock()
.await
.add_icon(OwnedUniqueName::from(sender.clone()), new_entry);
Ok(())
}
Err(e) => {
Expand Down Expand Up @@ -223,7 +229,10 @@ impl Daemon {
) -> zbus::fdo::Result<()> {
match hdr.sender() {
Some(x) => {
self.change_handlers
self.catalog
.lock()
.await
.change_handlers
.insert(OwnedUniqueName::from(x.clone()));
Ok(())
}
Expand Down
10 changes: 5 additions & 5 deletions src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::{
path::{Path, PathBuf},
};

use crate::daemon::Daemon;
use async_std::sync::{Arc, Mutex};

use crate::{daemon::Daemon, types::EntryCatalog};

pub fn get_data_dir(clean: bool) -> PathBuf {
let home = env::var("HOME").expect("can't find home environment variable!");
Expand All @@ -23,12 +25,10 @@ pub fn get_data_dir(clean: bool) -> PathBuf {
app_dir.to_owned()
}

pub fn set_up_environment() -> Daemon {
pub fn set_up_environment(catalog: Arc<Mutex<EntryCatalog>>) -> Daemon {
Daemon {
data_dir: get_data_dir(true).into(),
entries: HashMap::new(),
icons: HashMap::new(),
change_handlers: HashSet::new(),
catalog,
}
}

Expand Down
27 changes: 24 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use async_std::sync::Arc;

use async_std::stream::StreamExt;
use async_std::sync::Mutex;
use clap::Parser;
use daemon::Daemon;
use zbus::fdo::{DBusProxy, NameOwnerChangedArgs};
use zbus::names::OwnedUniqueName;
use zbus::{Connection, Result as ZbusResult};

use crate::files::{clean_environment, set_up_environment};
use crate::types::EntryCatalog;

mod daemon;
mod desktop_entry;
Expand All @@ -28,9 +34,17 @@ async fn main() -> ZbusResult<()> {
clean_environment();
return Ok(());
}
let _ = async_std::task::spawn(async { watch_name_owner_changed().await });
let catalog = Arc::new(Mutex::new(EntryCatalog::new()));
let c = catalog.clone();
let _ = async_std::task::spawn(async { watch_name_owner_changed(c).await });
let c = catalog.clone();
provide_desktop_entry_api(c).await?;
Ok(())
}

async fn provide_desktop_entry_api(catalog: Arc<Mutex<EntryCatalog>>) -> zbus::Result<()> {
let daemon = set_up_environment(catalog);
// start daemon
let daemon = set_up_environment();
let connection = Connection::session().await?;
// setup the server
connection
Expand All @@ -48,7 +62,7 @@ async fn main() -> ZbusResult<()> {
}
}

async fn watch_name_owner_changed() -> zbus::Result<()> {
async fn watch_name_owner_changed(catalog: Arc<Mutex<EntryCatalog>>) -> zbus::Result<()> {
log::info!("Watching if name owner changes!");
let connection = Connection::system().await?;
// `Systemd1ManagerProxy` is generated from `Systemd1Manager` trait
Expand All @@ -66,6 +80,13 @@ async fn watch_name_owner_changed() -> zbus::Result<()> {
args.old_owner(),
args.new_owner()
);

log::info!("{:?}", catalog.lock().await);
if args.new_owner().is_none() && args.old_owner().is_some() {
catalog.lock().await.remove_owner(OwnedUniqueName::from(
args.old_owner().as_ref().unwrap().clone(),
));
}
}

panic!("Stream ended unexpectedly");
Expand Down
68 changes: 65 additions & 3 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,80 @@
use std::path::PathBuf;
use std::{
collections::{HashMap, HashSet},
fs::remove_file,
path::PathBuf,
};

use zbus::names::{OwnedUniqueName, UniqueName};

#[derive(Clone, Debug)]
pub struct EntryCatalog {
pub owned_resources: HashMap<OwnedUniqueName, (Vec<DesktopEntry>, Vec<IconEntry>)>,
pub change_handlers: HashSet<OwnedUniqueName>,
}

impl EntryCatalog {
pub fn new() -> Self {
Self {
owned_resources: HashMap::new(),
change_handlers: HashSet::new(),
}
}

pub fn add_desktop_entry(&mut self, name: OwnedUniqueName, entry: DesktopEntry) {
if !self.owned_resources.contains_key(&name) {
self.owned_resources
.insert(name.clone(), (Vec::new(), Vec::new()));
}
self.owned_resources.get_mut(&name).unwrap().0.push(entry);
}

pub fn add_icon(&mut self, name: OwnedUniqueName, entry: IconEntry) {
if !self.owned_resources.contains_key(&name) {
self.owned_resources
.insert(name.clone(), (Vec::new(), Vec::new()));
}
self.owned_resources.get_mut(&name).unwrap().1.push(entry);
}

pub fn remove_owner(&mut self, name: OwnedUniqueName) {
if !self.owned_resources.contains_key(&name) {
return;
}
let (entries, icons) = self.owned_resources.get(&name).unwrap();
for entry in entries {
let _ = entry.clone().delete_self();
}
for icon in icons {
let _ = icon.clone().delete_self();
}

self.owned_resources.remove(&name);
log::info!("Removed owner with name {:?}", name);
}
}

#[derive(Clone, Debug)]
pub struct DesktopEntry {
pub appid: String,
pub path: PathBuf,
}

impl DesktopEntry {}
impl DesktopEntry {
fn delete_self(self) -> Result<(), std::io::Error> {
remove_file(&self.path)?;
Ok(())
}
}

#[derive(Clone, Debug)]
pub struct IconEntry {
pub icon_name: String,
pub icon_path: PathBuf,
}

impl IconEntry {}
impl IconEntry {
fn delete_self(self) -> Result<(), std::io::Error> {
remove_file(&self.icon_path)?;
Ok(())
}
}

0 comments on commit 19b9480

Please sign in to comment.