2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-09-26 19:21:25 +02:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-02-13 16:38:47 +01:00
|
|
|
use std::fmt;
|
2016-09-26 19:21:25 +02:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::collections::BTreeMap;
|
2016-11-05 10:38:00 +01:00
|
|
|
use std::time::Duration;
|
2017-02-13 16:38:47 +01:00
|
|
|
use serde::{Serialize, Serializer, Deserialize, Deserializer};
|
2017-07-04 14:31:16 +02:00
|
|
|
use serde::de::{Error, Visitor, MapAccess};
|
|
|
|
use serde::de::value::MapAccessDeserializer;
|
2016-09-26 19:21:25 +02:00
|
|
|
use serde_json::Value;
|
|
|
|
use serde_json::de::from_reader;
|
|
|
|
use serde_json::ser::to_string;
|
|
|
|
use util::journaldb::Algorithm;
|
2016-11-05 10:38:00 +01:00
|
|
|
use ethcore::client::Mode;
|
2016-09-26 19:21:25 +02:00
|
|
|
|
|
|
|
pub struct UserDefaults {
|
|
|
|
pub is_first_launch: bool,
|
|
|
|
pub pruning: Algorithm,
|
|
|
|
pub tracing: bool,
|
2016-10-03 11:13:10 +02:00
|
|
|
pub fat_db: bool,
|
2016-11-05 10:38:00 +01:00
|
|
|
pub mode: Mode,
|
2016-09-26 19:21:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for UserDefaults {
|
2017-02-13 16:38:47 +01:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
2016-09-26 19:21:25 +02:00
|
|
|
where S: Serializer {
|
|
|
|
let mut map: BTreeMap<String, Value> = BTreeMap::new();
|
|
|
|
map.insert("pruning".into(), Value::String(self.pruning.as_str().into()));
|
|
|
|
map.insert("tracing".into(), Value::Bool(self.tracing));
|
2016-10-03 11:13:10 +02:00
|
|
|
map.insert("fat_db".into(), Value::Bool(self.fat_db));
|
2016-11-05 10:38:00 +01:00
|
|
|
let mode_str = match self.mode {
|
|
|
|
Mode::Off => "offline",
|
|
|
|
Mode::Dark(timeout) => {
|
2017-02-13 16:38:47 +01:00
|
|
|
map.insert("mode.timeout".into(), Value::Number(timeout.as_secs().into()));
|
2016-11-05 10:38:00 +01:00
|
|
|
"dark"
|
|
|
|
},
|
|
|
|
Mode::Passive(timeout, alarm) => {
|
2017-02-13 16:38:47 +01:00
|
|
|
map.insert("mode.timeout".into(), Value::Number(timeout.as_secs().into()));
|
|
|
|
map.insert("mode.alarm".into(), Value::Number(alarm.as_secs().into()));
|
2016-11-05 10:38:00 +01:00
|
|
|
"passive"
|
|
|
|
},
|
|
|
|
Mode::Active => "active",
|
|
|
|
};
|
|
|
|
map.insert("mode".into(), Value::String(mode_str.into()));
|
|
|
|
|
2016-09-26 19:21:25 +02:00
|
|
|
map.serialize(serializer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct UserDefaultsVisitor;
|
|
|
|
|
2017-07-04 14:31:16 +02:00
|
|
|
impl<'a> Deserialize<'a> for UserDefaults {
|
2017-02-13 16:38:47 +01:00
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
2017-07-04 14:31:16 +02:00
|
|
|
where D: Deserializer<'a> {
|
|
|
|
deserializer.deserialize_any(UserDefaultsVisitor)
|
2016-09-26 19:21:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-04 14:31:16 +02:00
|
|
|
impl<'a> Visitor<'a> for UserDefaultsVisitor {
|
2016-09-26 19:21:25 +02:00
|
|
|
type Value = UserDefaults;
|
|
|
|
|
2017-02-13 16:38:47 +01:00
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(formatter, "a valid UserDefaults object")
|
|
|
|
}
|
|
|
|
|
2017-07-04 14:31:16 +02:00
|
|
|
fn visit_map<V>(self, visitor: V) -> Result<Self::Value, V::Error> where V: MapAccess<'a> {
|
|
|
|
let mut map: BTreeMap<String, Value> = Deserialize::deserialize(MapAccessDeserializer::new(visitor))?;
|
2016-12-27 12:53:56 +01:00
|
|
|
let pruning: Value = map.remove("pruning").ok_or_else(|| Error::custom("missing pruning"))?;
|
|
|
|
let pruning = pruning.as_str().ok_or_else(|| Error::custom("invalid pruning value"))?;
|
|
|
|
let pruning = pruning.parse().map_err(|_| Error::custom("invalid pruning method"))?;
|
|
|
|
let tracing: Value = map.remove("tracing").ok_or_else(|| Error::custom("missing tracing"))?;
|
|
|
|
let tracing = tracing.as_bool().ok_or_else(|| Error::custom("invalid tracing value"))?;
|
2016-12-22 07:51:25 +01:00
|
|
|
let fat_db: Value = map.remove("fat_db").unwrap_or_else(|| Value::Bool(false));
|
2016-12-27 12:53:56 +01:00
|
|
|
let fat_db = fat_db.as_bool().ok_or_else(|| Error::custom("invalid fat_db value"))?;
|
2016-09-26 19:21:25 +02:00
|
|
|
|
2016-12-22 07:51:25 +01:00
|
|
|
let mode: Value = map.remove("mode").unwrap_or_else(|| Value::String("active".to_owned()));
|
2016-12-27 12:53:56 +01:00
|
|
|
let mode = match mode.as_str().ok_or_else(|| Error::custom("invalid mode value"))? {
|
2016-11-05 10:38:00 +01:00
|
|
|
"offline" => Mode::Off,
|
|
|
|
"dark" => {
|
2016-12-27 12:53:56 +01:00
|
|
|
let timeout = map.remove("mode.timeout").and_then(|v| v.as_u64()).ok_or_else(|| Error::custom("invalid/missing mode.timeout value"))?;
|
2016-11-05 10:38:00 +01:00
|
|
|
Mode::Dark(Duration::from_secs(timeout))
|
|
|
|
},
|
|
|
|
"passive" => {
|
2016-12-27 12:53:56 +01:00
|
|
|
let timeout = map.remove("mode.timeout").and_then(|v| v.as_u64()).ok_or_else(|| Error::custom("invalid/missing mode.timeout value"))?;
|
|
|
|
let alarm = map.remove("mode.alarm").and_then(|v| v.as_u64()).ok_or_else(|| Error::custom("invalid/missing mode.alarm value"))?;
|
2016-11-05 10:38:00 +01:00
|
|
|
Mode::Passive(Duration::from_secs(timeout), Duration::from_secs(alarm))
|
|
|
|
},
|
|
|
|
"active" => Mode::Active,
|
|
|
|
_ => { return Err(Error::custom("invalid mode value")); },
|
|
|
|
};
|
|
|
|
|
2016-09-26 19:21:25 +02:00
|
|
|
let user_defaults = UserDefaults {
|
|
|
|
is_first_launch: false,
|
|
|
|
pruning: pruning,
|
|
|
|
tracing: tracing,
|
2016-10-03 11:13:10 +02:00
|
|
|
fat_db: fat_db,
|
2016-11-05 10:38:00 +01:00
|
|
|
mode: mode,
|
2016-09-26 19:21:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(user_defaults)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for UserDefaults {
|
|
|
|
fn default() -> Self {
|
|
|
|
UserDefaults {
|
|
|
|
is_first_launch: true,
|
|
|
|
pruning: Algorithm::default(),
|
|
|
|
tracing: false,
|
2016-10-03 11:13:10 +02:00
|
|
|
fat_db: false,
|
2016-11-05 10:38:00 +01:00
|
|
|
mode: Mode::Active,
|
2016-09-26 19:21:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UserDefaults {
|
|
|
|
pub fn load<P>(path: P) -> Result<Self, String> where P: AsRef<Path> {
|
|
|
|
match File::open(path) {
|
2016-12-11 13:00:49 +01:00
|
|
|
Ok(file) => match from_reader(file) {
|
|
|
|
Ok(defaults) => Ok(defaults),
|
|
|
|
Err(e) => {
|
|
|
|
warn!("Error loading user defaults file: {:?}", e);
|
|
|
|
Ok(UserDefaults::default())
|
|
|
|
},
|
|
|
|
},
|
2016-09-26 19:21:25 +02:00
|
|
|
_ => Ok(UserDefaults::default()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-05 10:38:00 +01:00
|
|
|
pub fn save<P>(&self, path: P) -> Result<(), String> where P: AsRef<Path> {
|
2016-12-27 12:53:56 +01:00
|
|
|
let mut file: File = File::create(path).map_err(|_| "Cannot create user defaults file".to_owned())?;
|
2016-09-26 19:21:25 +02:00
|
|
|
file.write_all(to_string(&self).unwrap().as_bytes()).map_err(|_| "Failed to save user defaults".to_owned())
|
|
|
|
}
|
|
|
|
}
|