1use crate::HOME;
2use anyhow::{Context, Result};
3use std::{
4 fs::{self, File},
5 path::Path, io::{Read, Write},
6};
7
8pub fn refreshicons() -> Result<()> {
10 let desktoppath = &format!("{}/.local/share/applications", &*HOME);
11 let iconpath = &format!("{}/.local/share/icons/nixrefresh.png", &*HOME);
12 fs::create_dir_all(desktoppath)?;
13 fs::create_dir_all(&format!("{}/.local/share/icons", &*HOME))?;
14
15 for filename in (fs::read_dir(desktoppath)?).flatten() {
17 if filename.file_type()?.is_file() && fs::read_to_string(filename.path())?.lines().next() == Some("# Nix Desktop Entry") {
18 fs::remove_file(filename.path())?;
19 }
20 }
21
22 for filename in
23 (fs::read_dir(&format!("{}/.nix-profile/share/applications", &*HOME))?).flatten()
24 {
25 let filepath = filename.path().to_str().context("file path")?.to_string();
26 let localpath = format!(
27 "{}/{}",
28 desktoppath,
29 filename.file_name().to_str().context("file name")?
30 );
31 if Path::new(&localpath).exists() {
32 fs::remove_file(&localpath)?;
33 }
34 fs::copy(&filepath, &localpath)?;
35 let mut file = File::open(&localpath)?;
37 let mut contents = String::new();
38 file.read_to_string(&mut contents)?;
39 contents = format!("# Nix Desktop Entry\n{}", contents);
40 fs::remove_file(&localpath)?;
41 let mut file = File::create(&localpath)?;
42 file.write_all(contents.as_bytes())?;
43 let mut perms = fs::metadata(&localpath)?.permissions();
44 perms.set_readonly(true);
45 fs::set_permissions(&localpath, perms)?;
46 }
47
48 if Path::new(iconpath).exists() {
49 fs::remove_file(iconpath)?;
50 }
51 File::create(iconpath)?;
52 if Path::new(iconpath).exists() {
53 fs::remove_file(iconpath)?;
54 }
55
56 Ok(())
57}