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
use std::rc::{Rc, Weak};
use std::cell::RefCell;
use crate::shell::Shell;
#[cfg(unix)]
use gio;
#[cfg(unix)]
use gio::SettingsExt;
#[derive(PartialEq)]
pub enum FontSource {
Rpc,
#[cfg(unix)]
Gnome,
Default,
}
struct State {
font_source: FontSource,
#[cfg(unix)]
gnome_interface_settings: gio::Settings,
}
impl State {
#[cfg(unix)]
pub fn new() -> State {
State {
font_source: FontSource::Default,
gnome_interface_settings: gio::Settings::new("org.gnome.desktop.interface"),
}
}
#[cfg(target_os = "windows")]
pub fn new() -> State {
State { font_source: FontSource::Default }
}
#[cfg(unix)]
fn update_font(&mut self, shell: &mut Shell) {
if self.font_source == FontSource::Rpc {
return;
}
if let Some(ref font_name) =
self.gnome_interface_settings.get_string(
"monospace-font-name",
)
{
shell.set_font_desc(font_name);
self.font_source = FontSource::Gnome;
}
}
}
pub struct Settings {
shell: Option<Weak<RefCell<Shell>>>,
state: Rc<RefCell<State>>,
}
impl Settings {
pub fn new() -> Settings {
Settings {
shell: None,
state: Rc::new(RefCell::new(State::new())),
}
}
pub fn set_shell(&mut self, shell: Weak<RefCell<Shell>>) {
self.shell = Some(shell);
}
#[cfg(unix)]
pub fn init(&mut self) {
let shell = Weak::upgrade(self.shell.as_ref().unwrap()).unwrap();
let state = self.state.clone();
self.state.borrow_mut().update_font(
&mut *shell.borrow_mut(),
);
self.state
.borrow()
.gnome_interface_settings
.connect_changed(move |_, _| {
monospace_font_changed(&mut *shell.borrow_mut(), &mut *state.borrow_mut())
});
}
#[cfg(target_os = "windows")]
pub fn init(&mut self) {}
pub fn set_font_source(&mut self, src: FontSource) {
self.state.borrow_mut().font_source = src;
}
}
#[cfg(unix)]
fn monospace_font_changed(mut shell: &mut Shell, state: &mut State) {
if state.font_source != FontSource::Rpc {
state.update_font(&mut shell);
}
}
use std::path::Path;
use std::fs::File;
use std::io::prelude::*;
use toml;
use serde;
use crate::dirs;
pub trait SettingsLoader: Sized + serde::Serialize + Default {
const SETTINGS_FILE: &'static str;
fn from_str(s: &str) -> Result<Self, String>;
fn load() -> Self {
match load_err() {
Ok(settings) => settings,
Err(e) => {
error!("{}", e);
Default::default()
}
}
}
fn is_file_exists() -> bool {
if let Ok(mut toml_path) = dirs::get_app_config_dir() {
toml_path.push(Self::SETTINGS_FILE);
toml_path.is_file()
} else {
false
}
}
fn save(&self) {
match save_err(self) {
Ok(()) => (),
Err(e) => error!("{}", e),
}
}
}
fn load_from_file<T: SettingsLoader>(path: &Path) -> Result<T, String> {
if path.exists() {
let mut file = File::open(path).map_err(|e| format!("{}", e))?;
let mut contents = String::new();
file.read_to_string(&mut contents).map_err(
|e| format!("{}", e),
)?;
T::from_str(&contents)
} else {
Ok(Default::default())
}
}
fn load_err<T: SettingsLoader>() -> Result<T, String> {
let mut toml_path = dirs::get_app_config_dir_create()?;
toml_path.push(T::SETTINGS_FILE);
load_from_file(&toml_path)
}
fn save_err<T: SettingsLoader>(sl: &T) -> Result<(), String> {
let mut toml_path = dirs::get_app_config_dir_create()?;
toml_path.push(T::SETTINGS_FILE);
let mut file = File::create(toml_path).map_err(|e| format!("{}", e))?;
let contents = toml::to_vec::<T>(sl).map_err(|e| format!("{}", e))?;
file.write_all(&contents).map_err(|e| format!("{}", e))?;
Ok(())
}