2018-06-04 10:19:50 +02:00
|
|
|
// Copyright 2015-2018 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/>.
|
|
|
|
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path::Path;
|
2016-11-05 10:38:00 +01:00
|
|
|
use std::time::Duration;
|
2018-07-23 13:57:50 +02:00
|
|
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
2016-09-26 19:21:25 +02:00
|
|
|
use serde_json::de::from_reader;
|
|
|
|
use serde_json::ser::to_string;
|
2017-10-17 06:41:05 +02:00
|
|
|
use journaldb::Algorithm;
|
2018-07-23 13:57:50 +02:00
|
|
|
use ethcore::client::{Mode as ClientMode};
|
2016-09-26 19:21:25 +02:00
|
|
|
|
2018-07-23 13:57:50 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Seconds(Duration);
|
|
|
|
|
|
|
|
impl Seconds {
|
|
|
|
pub fn value(&self) -> u64 {
|
|
|
|
self.0.as_secs()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u64> for Seconds {
|
|
|
|
fn from(s: u64) -> Seconds {
|
|
|
|
Seconds(Duration::from_secs(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Duration> for Seconds {
|
|
|
|
fn from(d: Duration) -> Seconds {
|
|
|
|
Seconds(d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<Duration> for Seconds {
|
|
|
|
fn into(self) -> Duration {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for Seconds {
|
|
|
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
|
|
|
serializer.serialize_u64(self.value())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for Seconds {
|
|
|
|
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
|
|
|
let secs = u64::deserialize(deserializer)?;
|
|
|
|
Ok(Seconds::from(secs))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "lowercase", tag = "mode")]
|
|
|
|
pub enum Mode {
|
|
|
|
Active,
|
|
|
|
Passive {
|
|
|
|
#[serde(rename = "mode.timeout")]
|
|
|
|
timeout: Seconds,
|
|
|
|
#[serde(rename = "mode.alarm")]
|
|
|
|
alarm: Seconds,
|
|
|
|
},
|
|
|
|
Dark {
|
|
|
|
#[serde(rename = "mode.timeout")]
|
|
|
|
timeout: Seconds,
|
|
|
|
},
|
|
|
|
Offline,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<ClientMode> for Mode {
|
|
|
|
fn into(self) -> ClientMode {
|
|
|
|
match self {
|
|
|
|
Mode::Active => ClientMode::Active,
|
|
|
|
Mode::Passive { timeout, alarm } => ClientMode::Passive(timeout.into(), alarm.into()),
|
|
|
|
Mode::Dark { timeout } => ClientMode::Dark(timeout.into()),
|
|
|
|
Mode::Offline => ClientMode::Off,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ClientMode> for Mode {
|
|
|
|
fn from(mode: ClientMode) -> Mode {
|
|
|
|
match mode {
|
|
|
|
ClientMode::Active => Mode::Active,
|
|
|
|
ClientMode::Passive(timeout, alarm) => Mode::Passive { timeout: timeout.into(), alarm: alarm.into() },
|
|
|
|
ClientMode::Dark(timeout) => Mode::Dark { timeout: timeout.into() },
|
|
|
|
ClientMode::Off => Mode::Offline,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
2016-09-26 19:21:25 +02:00
|
|
|
pub struct UserDefaults {
|
|
|
|
pub is_first_launch: bool,
|
2018-07-23 13:57:50 +02:00
|
|
|
#[serde(with = "algorithm_serde")]
|
2016-09-26 19:21:25 +02:00
|
|
|
pub pruning: Algorithm,
|
|
|
|
pub tracing: bool,
|
2016-10-03 11:13:10 +02:00
|
|
|
pub fat_db: bool,
|
2018-07-23 13:57:50 +02:00
|
|
|
#[serde(flatten)]
|
|
|
|
mode: Mode,
|
2016-09-26 19:21:25 +02:00
|
|
|
}
|
|
|
|
|
2018-07-23 13:57:50 +02:00
|
|
|
impl UserDefaults {
|
|
|
|
pub fn mode(&self) -> ClientMode {
|
|
|
|
self.mode.clone().into()
|
2016-09-26 19:21:25 +02:00
|
|
|
}
|
|
|
|
|
2018-07-23 13:57:50 +02:00
|
|
|
pub fn set_mode(&mut self, mode: ClientMode) {
|
|
|
|
self.mode = mode.into();
|
2016-09-26 19:21:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 13:57:50 +02:00
|
|
|
mod algorithm_serde {
|
|
|
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
|
|
|
use serde::de::Error;
|
|
|
|
use journaldb::Algorithm;
|
2016-09-26 19:21:25 +02:00
|
|
|
|
2018-07-23 13:57:50 +02:00
|
|
|
pub fn serialize<S>(algorithm: &Algorithm, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where S: Serializer {
|
|
|
|
algorithm.as_str().serialize(serializer)
|
2017-02-13 16:38:47 +01:00
|
|
|
}
|
|
|
|
|
2018-07-23 13:57:50 +02:00
|
|
|
pub fn deserialize<'de, D>(deserializer: D) -> Result<Algorithm, D::Error>
|
|
|
|
where D: Deserializer<'de> {
|
|
|
|
let pruning = String::deserialize(deserializer)?;
|
|
|
|
pruning.parse().map_err(|_| Error::custom("invalid pruning method"))
|
2016-09-26 19:21:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for UserDefaults {
|
|
|
|
fn default() -> Self {
|
|
|
|
UserDefaults {
|
|
|
|
is_first_launch: true,
|
2018-07-02 19:00:06 +02:00
|
|
|
pruning: Algorithm::OverlayRecent,
|
2016-09-26 19:21:25 +02:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|