1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use super::Desktop;
use std::error::Error;
use std::io::BufRead;
use std::path::PathBuf;
use std::process::Command;

/// A desktop environment
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum DesktopEnvt {
    GNOME,
    Cinnamon,
    MATE,
    XFCE,
    Deepin,
    KDE,
    BSPWM,
    I3,
}

impl Desktop for DesktopEnvt {
    fn new() -> Result<Self, Box<dyn Error>> {
        let desktop = std::env::var("XDG_CURRENT_DESKTOP")?;
        if is_gnome_compliant(&desktop) {
            Ok(DesktopEnvt::GNOME)
        } else {
            Ok(match &desktop[..] {
                "X-Cinnamon" => DesktopEnvt::Cinnamon,
                "MATE" => DesktopEnvt::MATE,
                "XFCE" => DesktopEnvt::XFCE,
                "Deepin" => DesktopEnvt::Deepin,
                "KDE" => DesktopEnvt::KDE,
                "bspwm" => DesktopEnvt::BSPWM,
                "i3" => DesktopEnvt::I3,
                _ => panic!("Unsupported Desktop Environment"),
            })
        }
    }

    fn set_wallpaper(&self, path: &str) -> Result<(), Box<dyn Error>> {
        let path = enquote::enquote('"', &format!("{}", path));

        match self {
            DesktopEnvt::GNOME => {
                Command::new("gsettings")
                    .args(&["set", "org.gnome.desktop.background", "picture-uri", &path])
                    .output()?;
            }

            DesktopEnvt::Cinnamon => {
                Command::new("dconf")
                    .args(&[
                        "write",
                        "/org/cinnamon/desktop/background/picture-uri",
                        &path,
                    ])
                    .output()?;
            }

            DesktopEnvt::MATE => {
                let mate_path = path.replace("file://", "");

                Command::new("dconf")
                    .args(&[
                        "write",
                        "/org/mate/desktop/background/picture-filename",
                        &mate_path,
                    ])
                    .output()?;
            }

            DesktopEnvt::XFCE => {
                let path_unquoted = enquote::unquote(&path).unwrap();
                let xfce_path = path_unquoted
                    .strip_prefix("file://")
                    .unwrap();
                
                // Get the raw output of xfconf-query for the wallpaper
                let values_raw = Command::new("xfconf-query")
                    .args(&[
                        "-c",
                        "xfce4-desktop",
                        "-p",
                        "/backdrop/screen0",
                        "-lv",
                    ])
                    .output()
                    .unwrap()
                    .stdout;

                // Filter out unwanted values (everything except */last-image)
                let values_str = match std::str::from_utf8(&values_raw) {
                    Ok(v) => v.to_string(),
                    Err(_) => "/backdrop/screen0/monitor0/workspace0/last-image".to_string(),
                };

                // Collect the keys for the filtered values
                let values_vec: Vec<&str> = values_str
                    .split_whitespace()
                    .step_by(2)
                    .filter(|v| v.contains("last-image"))
                    .collect();

                // Set all the keys to the new wallpaper
                for v in values_vec {
                    Command::new("xfconf-query")
                        .args(&[
                            "-c",
                            "xfce4-desktop",
                            "-p",
                            v,
                            "-s",
                            &xfce_path,
                        ])
                        .output()?;
                }
            }

            DesktopEnvt::Deepin => {
                Command::new("dconf")
                    .args(&[
                        "write",
                        "/com/deepin/wrap/gnome/desktop/background/picture-uri",
                        &path,
                    ])
                    .output()?;
            }

            DesktopEnvt::KDE => {
                // KDE needs plasma shell scripting to change the wallpaper
                let kde_set_arg = format!(
                    r#"
                    const monitors = desktops()
                    for (var i = 0; i < monitors.length; i++) {{
                        monitors[i].wallpaperPlugin = "org.kde.image"
                        monitors[i].currentConfigGroup = ["Wallpaper"]
                        monitors[i].writeConfig("Image", {})
                    }}"#,
                    &path
                );

                Command::new("qdbus")
                    .args(&[
                        "org.kde.plasmashell",
                        "/PlasmaShell",
                        "org.kde.PlasmaShell.evaluateScript",
                        &kde_set_arg,
                    ])
                    .output()?;
            }

            DesktopEnvt::BSPWM | DesktopEnvt::I3 => {
                Command::new("feh")
                    .args(&["--bg-fill", &path.replace("\"", "")])
                    .output()?;
            }
        }

        Ok(())
    }

    fn get_wallpaper(&self) -> Result<PathBuf, Box<dyn Error>> {
        let output = match self {
            DesktopEnvt::GNOME => Command::new("gsettings")
                .args(&["get", "org.gnome.desktop.background", "picture-uri"])
                .output()?,

            DesktopEnvt::Cinnamon => Command::new("dconf")
                .arg("read")
                .arg("/org/cinnamon/desktop/background/picture-uri")
                .output()?,

            DesktopEnvt::MATE => Command::new("dconf")
                .args(&["read", "/org/mate/desktop/background/picture-filename"])
                .output()?,

            DesktopEnvt::XFCE => Command::new("xfconf-query")
                .args(&[
                    "-c",
                    "xfce4-desktop",
                    "-p",
                    "/backdrop/screen0/monitor0/workspace0/last-image",
                ])
                .output()?,

            DesktopEnvt::Deepin => Command::new("dconf")
                .args(&[
                    "read",
                    "/com/deepin/wrap/gnome/desktop/background/picture-uri",
                ])
                .output()?,
            DesktopEnvt::KDE => return Ok(kde_get_wallpaper()?),
            DesktopEnvt::BSPWM | DesktopEnvt::I3 => Command::new("sed")
                .args(&[
                    "-n",
                    "'s/feh.*\\('.*'\\)/\\1/gp'",
                    &format!("/home/{}/.fehbg", std::env::var("USER")?.trim()),
                ])
                .output()?,
        };

        let output = enquote::unquote(String::from_utf8(output.stdout)?.trim().into())?;
        Ok(PathBuf::from(output))
    }
}

/// Check if desktop is Gnome compliant
fn is_gnome_compliant(desktop: &str) -> bool {
    desktop.contains("GNOME") || desktop == "Unity" || desktop == "Pantheon"
}

/// Returns the absolute wallpaper path on KDE, if possible.
///
/// It reads the first line starting with "Image="
/// in the file "~/.config/plasma-org.kde.plasma.desktop-appletsrc"
fn kde_get_wallpaper() -> Result<PathBuf, Box<dyn Error>> {
    let mut path = dirs_next::config_dir().ok_or("Could not determine config directory")?;
    path.push("plasma-org.kde.plasma.desktop-appletsrc");

    // Opening the file into a buffer reader
    let file = std::fs::File::open(path)?;

    let reader = std::io::BufReader::new(file);
    for line in reader.lines() {
        let line = line?;
        if line.starts_with("Image=") {
            let mut line = line[6..].trim();
            if line.starts_with("file://") {
                line = &line[7..];
            }
            return Ok(PathBuf::from(line));
        }
    }

    Err("KDE Image not found".into())
}