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
//! Branding capabilities for ommui.

use std::fs::File;
use std::io::Read;
use std::path::PathBuf;

fn default_company_name() -> String {
    "OMMUI".to_string()
}

fn default_product_name() -> String {
    "OMMUI".to_string()
}

fn default_company_id() -> String {
    "ommui".to_string()
}

fn default_product_id() -> String {
    "ommui".to_string()
}

/// A set of branding properties.
#[derive(Serialize, Deserialize, Debug)]
pub struct Branding {
    /// The name of the company as a human-readable string.
    #[serde(default = "default_company_name")]
    pub company_name: String,

    /// The name of the ommui product as a human-readable string.
    #[serde(default = "default_product_name")]
    pub product_name: String,

    /// An id string of the company.
    /// Should be lower-case alphanumeric only.
    /// This gets used for determining file paths that contain the
    /// company name.
    #[serde(default = "default_company_id")]
    pub company_id: String,

    /// An id string of the product.
    /// Should be lower-case alphanumeric only.
    /// This gets used for determining file paths that contain the
    /// product name.
    #[serde(default = "default_product_id")]
    pub product_id: String,
}

impl Default for Branding {
    fn default() -> Self {
        Branding {
            company_name: default_company_name(),
            product_name: default_product_name(),
            company_id: default_company_id(),
            product_id: default_product_id(),
        }
    }
}

impl Branding {
    /// Load a branding from the default locations.
    pub fn load() -> Self {
        let mut locations = Vec::new();
        if let Some(dir) = dirs::config_dir() {
            locations.push(dir.join("ommui/branding.toml"));
        }
        locations.push(PathBuf::from("/etc/ommui/branding.toml"));
        locations.push(PathBuf::from("/usr/share/ommui/branding.toml"));

        for location in locations {
            if let Ok(mut file) = File::open(&location) {
                info!("Attempting to load branding from {:?}", location);
                let mut input = String::new();
                match file.read_to_string(&mut input) {
                    Ok(_) => {
                        let branding = toml::from_str(&input);
                        if let Ok(branding) = branding {
                            info!("Using branding from {:?}", location);
                            return branding;
                        }
                    }
                    Err(e) => {
                        warn!(
                            "Couldn't read from {:?}: {:?}",
                            location, e
                        );
                    }
                }
            }
        }

        info!("Using default branding");
        Branding::default()
    }
}