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
//! DE-specific terminal detection code  
//!
//! TODO: Merge similar code

use crate::{DetectionError, Terminal};
use configparser::ini::Ini;
use std::{env::var, process::Command};

trait StdoutStringify {
    fn stdout_stringify(self)
        -> Result<Result<String, std::string::FromUtf8Error>, DetectionError>;
}

impl StdoutStringify for std::io::Result<std::process::Child> {
    fn stdout_stringify(
        self,
    ) -> Result<Result<String, std::string::FromUtf8Error>, DetectionError> {
        let child = self?;
        let output = child.wait_with_output()?;
        Ok(String::from_utf8(output.stdout))
    }
}

fn get_home() -> String {
    var("HOME").unwrap_or_default()
}

/// Cinnamon detector
///
/// Same as GNOME except the id is "org.cinnamon" instead of "org.gnome"
pub fn detect_terminal_desktop_cinnamon() -> Result<Terminal, DetectionError> {
    let mut maybe_term = Command::new("gsettings")
        .arg("get")
        .arg("org.cinnamon.desktop.default-applications.terminal")
        .arg("exec")
        .stdout(std::process::Stdio::piped())
        .spawn()
        .stdout_stringify()?
        .unwrap_or_default()
        .trim()
        .to_string();
    if maybe_term.starts_with('\'') && maybe_term.ends_with('\'') && maybe_term.len() > 1 {
        maybe_term = maybe_term[1..maybe_term.len() - 1].to_string();
    }
    if maybe_term.is_empty() {
        Err(DetectionError::DEFindError)
    } else {
        Ok(maybe_term.into())
    }
}

/// Deepin detector
///
/// Same as GNOME except the id is "org.deepin" instead of "org.gnome"
pub fn detect_terminal_desktop_deepin() -> Result<Terminal, DetectionError> {
    let mut maybe_term = Command::new("gsettings")
        .arg("get")
        .arg("org.deepin.desktop.default-applications.terminal")
        .arg("exec")
        .stdout(std::process::Stdio::piped())
        .spawn()
        .stdout_stringify()?
        .unwrap_or_default()
        .trim()
        .to_string();
    if maybe_term.starts_with('\'') && maybe_term.ends_with('\'') && maybe_term.len() > 1 {
        maybe_term = maybe_term[1..maybe_term.len() - 1].to_string();
    }
    if maybe_term.is_empty() {
        Err(DetectionError::DEFindError)
    } else {
        Ok(maybe_term.into())
    }
}

/// GNOME and Budgie detector
///
/// GNOME stored its default terminal app in GConf  
///
/// This also works for Budgie, since it's a GNOME fork
///
/// This method is deprecated in GNOME and may not work in the future  
pub fn detect_terminal_desktop_gnome() -> Result<Terminal, DetectionError> {
    let mut maybe_term = Command::new("gsettings")
        .arg("get")
        .arg("org.gnome.desktop.default-applications.terminal")
        .arg("exec")
        .stdout(std::process::Stdio::piped())
        .spawn()
        .stdout_stringify()?
        .unwrap_or_default()
        .trim()
        .to_string();
    if maybe_term.starts_with('\'') && maybe_term.ends_with('\'') && maybe_term.len() > 1 {
        maybe_term = maybe_term[1..maybe_term.len() - 1].to_string();
    }
    if maybe_term.is_empty() {
        Err(DetectionError::DEFindError)
    } else {
        Ok(maybe_term.into())
    }
}

/// KDE Plasma detector
///
/// KDE Plasma 5 stores its default terminal config in ~/.config/kdeglobals  
///
/// If konsole is set as the default, then it's empty
pub fn detect_terminal_desktop_kde() -> Result<Terminal, DetectionError> {
    let maybe_term = Command::new("kreadconfig5")
        .arg("--group")
        .arg("General")
        .arg("--key")
        .arg("TerminalApplication")
        .stdout(std::process::Stdio::piped())
        .spawn()
        .stdout_stringify()?
        .unwrap_or_default()
        .trim()
        .to_string();
    Ok(if maybe_term.is_empty() {
        "konsole".into()
    } else {
        maybe_term.into()
    })
}

/// LXDE detector
///
/// LXDE stores its config in ~/.config/lxsession/**NAME**/desktop.conf
///
/// where **NAME** is the desktop session name ex. "Lubuntu" or "LXDE"
pub fn detect_terminal_desktop_lxde() -> Result<Terminal, DetectionError> {
    let filename = format!(
        "{}/.config/lxsession/{}/desktop.conf",
        get_home(),
        var("XDG_SESSION_DESKTOP").unwrap_or("LXDE".to_string()),
    );
    let mut config = Ini::new_cs();
    if let Err(e) = config.load(filename) {
        return Err(DetectionError::IniParseError(e));
    };
    let Some(term) = config.get("Session", "terminal_manager/command") else {
        return Err(DetectionError::DEFindError)
    };
    Ok(term.into())
}

/// LXQt detector
///
/// It's kind of a mess. <https://github.com/lxqt/lxqt/issues/433>  
///
/// LXQt stores the terminal in the env variable LXQT_TERMINAL_EMULATOR
/// OR in ~/.config/lxqt/session.conf
pub fn detect_terminal_desktop_lxqt() -> Result<Terminal, DetectionError> {
    if let Ok(term) = var("LXQT_TERMINAL_EMULATOR") {
        return Ok(term.into());
    }
    let filename = format!("{}/.config/lxqt/session.conf", get_home());
    let mut config = Ini::new_cs();
    if let Err(e) = config.load(filename) {
        return Err(DetectionError::IniParseError(e));
    };
    let Some(term) = config.get("Environment", "TERM") else {
        return Err(DetectionError::DEFindError)
    };
    Ok(term.into())
}

/// XFCE detector
///
/// In XFCE the default terminal is stored in ~/.config/xfce4/helpers.rc
pub fn detect_terminal_desktop_xfce() -> Result<Terminal, DetectionError> {
    let filename = format!("{}/.config/xfce4/helpers.rc", get_home());
    let mut config = Ini::new_cs();
    if let Err(e) = config.load(filename) {
        return Err(DetectionError::IniParseError(e));
    };
    Ok(config
        .get("default", "TerminalEmulator")
        .unwrap_or("xfce4-terminal".to_string())
        .into())
}