meli/ui/src/conf.rs

388 lines
10 KiB
Rust
Raw Normal View History

2018-08-19 13:12:48 +03:00
/*
* meli - configuration module.
*
* Copyright 2017 Manos Pitsidianakis
*
* This file is part of meli.
*
* meli 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.
*
* meli 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 meli. If not, see <http://www.gnu.org/licenses/>.
*/
2019-03-14 12:19:25 +02:00
extern crate bincode;
2018-08-19 13:12:48 +03:00
extern crate config;
extern crate serde;
extern crate xdg;
2019-02-15 09:06:42 +02:00
pub mod mailer;
2019-03-02 21:40:57 +02:00
pub mod notifications;
2019-03-14 12:19:25 +02:00
pub mod pager;
pub mod shortcuts;
2018-08-19 13:12:48 +03:00
2019-02-10 18:26:49 +02:00
pub mod accounts;
pub use self::accounts::Account;
use self::config::{Config, File, FileFormat};
pub use self::mailer::*;
2019-03-14 12:19:25 +02:00
pub use self::shortcuts::*;
2019-02-10 18:26:49 +02:00
use self::default_vals::*;
2019-03-14 12:19:25 +02:00
use self::notifications::NotificationsSettings;
2019-06-18 21:13:58 +03:00
use crate::pager::PagerSettings;
2018-08-19 13:12:48 +03:00
use melib::conf::AccountSettings;
use melib::error::*;
2018-08-19 13:12:48 +03:00
use self::serde::{de, Deserialize, Deserializer};
2018-08-19 13:12:48 +03:00
use std::collections::HashMap;
use std::env;
use std::fs::OpenOptions;
use std::io::{self, BufRead, Write};
use std::path::PathBuf;
#[macro_export]
macro_rules! split_command {
($cmd:expr) => {{
$cmd.split_whitespace().collect::<Vec<&str>>()
}};
}
#[derive(Debug, Clone, PartialEq)]
pub enum ToggleFlag {
Unset,
InternalVal(bool),
False,
True,
}
impl Default for ToggleFlag {
fn default() -> Self {
ToggleFlag::Unset
}
}
impl ToggleFlag {
pub fn is_unset(&self) -> bool {
ToggleFlag::Unset == *self
}
pub fn is_internal(&self) -> bool {
if let ToggleFlag::InternalVal(_) = *self {
true
} else {
false
}
}
pub fn is_false(&self) -> bool {
ToggleFlag::False == *self || ToggleFlag::InternalVal(false) == *self
}
pub fn is_true(&self) -> bool {
ToggleFlag::True == *self || ToggleFlag::InternalVal(true) == *self
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct FolderConf {
rename: Option<String>,
#[serde(default = "true_val")]
autoload: bool,
#[serde(deserialize_with = "toggleflag_de", default)]
ignore: ToggleFlag,
}
impl Default for FolderConf {
fn default() -> Self {
FolderConf {
rename: None,
autoload: true,
ignore: ToggleFlag::Unset,
}
}
}
impl FolderConf {
pub fn rename(&self) -> Option<&str> {
2019-05-13 22:05:00 +03:00
self.rename.as_ref().map(String::as_str)
}
}
2018-08-19 13:12:48 +03:00
2018-08-19 13:30:43 +03:00
#[derive(Debug, Clone, Default, Deserialize)]
2018-08-19 13:12:48 +03:00
pub struct FileAccount {
root_folder: String,
format: String,
sent_folder: String,
draft_folder: String,
identity: String,
#[serde(default = "none")]
display_name: Option<String>,
#[serde(deserialize_with = "index_from_str")]
index: IndexStyle,
/// A command to pipe html output before displaying it in a pager
/// Default: None
#[serde(default = "none", deserialize_with = "non_empty_string")]
html_filter: Option<String>,
folders: Option<HashMap<String, FolderConf>>,
2018-08-19 13:12:48 +03:00
}
impl From<FileAccount> for AccountConf {
fn from(x: FileAccount) -> Self {
let format = x.format.to_lowercase();
let sent_folder = x.sent_folder.clone();
let root_folder = x.root_folder.clone();
let identity = x.identity.clone();
let display_name = x.display_name.clone();
let acc = AccountSettings {
name: String::new(),
root_folder,
format,
sent_folder,
identity,
display_name,
};
2019-05-13 22:05:00 +03:00
let folder_confs = x.folders.clone().unwrap_or_else(Default::default);
AccountConf {
account: acc,
conf: x,
folder_confs,
}
}
}
2018-08-19 13:12:48 +03:00
impl FileAccount {
pub fn folders(&self) -> Option<&HashMap<String, FolderConf>> {
self.folders.as_ref()
}
2018-08-19 13:12:48 +03:00
pub fn folder(&self) -> &str {
&self.root_folder
}
pub fn index(&self) -> IndexStyle {
self.index
2018-08-19 14:08:20 +03:00
}
2019-04-10 18:57:09 +03:00
pub fn sent_folder(&self) -> &str {
self.sent_folder.as_str()
}
pub fn html_filter(&self) -> Option<&str> {
2019-05-13 22:05:00 +03:00
self.html_filter.as_ref().map(String::as_str)
}
2018-08-19 13:12:48 +03:00
}
2018-08-19 13:30:43 +03:00
#[derive(Debug, Clone, Default, Deserialize)]
2018-08-19 13:12:48 +03:00
struct FileSettings {
accounts: HashMap<String, FileAccount>,
#[serde(default)]
2018-08-19 13:12:48 +03:00
pager: PagerSettings,
#[serde(default)]
2019-03-02 21:40:57 +02:00
notifications: NotificationsSettings,
#[serde(default)]
shortcuts: Shortcuts,
mailer: MailerSettings,
2018-08-19 13:12:48 +03:00
}
2018-08-19 13:30:43 +03:00
#[derive(Debug, Clone, Default)]
2018-08-19 14:08:20 +03:00
pub struct AccountConf {
2018-08-19 13:30:43 +03:00
account: AccountSettings,
conf: FileAccount,
folder_confs: HashMap<String, FolderConf>,
2018-08-19 13:30:43 +03:00
}
2018-08-19 14:08:20 +03:00
impl AccountConf {
2018-08-19 13:30:43 +03:00
pub fn account(&self) -> &AccountSettings {
&self.account
}
pub fn conf(&self) -> &FileAccount {
&self.conf
}
2018-08-19 14:08:20 +03:00
pub fn conf_mut(&mut self) -> &mut FileAccount {
&mut self.conf
}
2018-08-19 13:30:43 +03:00
}
2018-08-19 13:12:48 +03:00
#[derive(Debug, Clone, Default)]
pub struct Settings {
2018-08-19 14:08:20 +03:00
pub accounts: HashMap<String, AccountConf>,
2018-08-19 13:12:48 +03:00
pub pager: PagerSettings,
2019-03-02 21:40:57 +02:00
pub notifications: NotificationsSettings,
pub shortcuts: Shortcuts,
pub mailer: MailerSettings,
2018-08-19 13:12:48 +03:00
}
impl FileSettings {
pub fn new() -> Result<FileSettings> {
let config_path = match env::var("MELI_CONFIG") {
Ok(path) => PathBuf::from(path),
Err(_) => {
let xdg_dirs = xdg::BaseDirectories::with_prefix("meli").unwrap();
xdg_dirs
.place_config_file("config")
.expect("cannot create configuration directory")
}
};
if !config_path.exists() {
println!(
"No configuration found. Would you like to generate one in {}? [Y/n]",
config_path.display()
);
let mut buffer = String::new();
let stdin = io::stdin();
let mut handle = stdin.lock();
loop {
buffer.clear();
handle
.read_line(&mut buffer)
.expect("Could not read from stdin.");
match buffer.trim() {
"Y" | "y" | "yes" | "YES" | "Yes" => {
let mut file = OpenOptions::new()
.write(true)
.create_new(true)
.open(config_path.as_path())
.expect("Could not create config file.");
2019-06-18 21:13:58 +03:00
file.write_all(include_bytes!("../../sample-config"))
.expect("Could not write to config file.");
println!("Written config to {}", config_path.display());
std::process::exit(1);
}
"n" | "N" | "no" | "No" | "NO" => {
std::process::exit(1);
}
_ => {
println!(
"No configuration found. Would you like to generate one in {}? [Y/n]",
config_path.display()
);
}
}
}
}
2018-08-19 13:12:48 +03:00
let mut s = Config::new();
if s.merge(File::new(config_path.to_str().unwrap(), FileFormat::Toml))
.is_err()
{
println!("Config file contains errors.");
std::process::exit(1);
}
2018-08-19 13:12:48 +03:00
/* No point in returning without a config file. */
2019-05-13 22:05:00 +03:00
match s.try_into() {
Ok(v) => Ok(v),
Err(e) => Err(MeliError::new(e.to_string())),
}
2018-08-19 13:12:48 +03:00
}
}
impl Settings {
pub fn new() -> Settings {
let fs = FileSettings::new().unwrap_or_else(|e| {
println!("Configuration error: {}", e);
std::process::exit(1);
});
2018-08-19 14:08:20 +03:00
let mut s: HashMap<String, AccountConf> = HashMap::new();
2018-08-19 13:12:48 +03:00
for (id, x) in fs.accounts {
let mut ac = AccountConf::from(x);
ac.account.set_name(id.clone());
s.insert(id, ac);
2018-08-19 13:12:48 +03:00
}
Settings {
accounts: s,
pager: fs.pager,
2019-03-02 21:40:57 +02:00
notifications: fs.notifications,
shortcuts: fs.shortcuts,
mailer: fs.mailer,
2018-08-19 13:12:48 +03:00
}
}
}
#[derive(Copy, Debug, Clone, Deserialize)]
pub enum IndexStyle {
Plain,
Threaded,
Compact,
}
impl Default for IndexStyle {
fn default() -> Self {
IndexStyle::Compact
}
}
fn index_from_str<'de, D>(deserializer: D) -> std::result::Result<IndexStyle, D::Error>
2019-03-14 12:19:25 +02:00
where
D: Deserializer<'de>,
{
let s = <String>::deserialize(deserializer)?;
match s.as_str() {
"Plain" | "plain" => Ok(IndexStyle::Plain),
"Threaded" | "threaded" => Ok(IndexStyle::Threaded),
"Compact" | "compact" => Ok(IndexStyle::Compact),
_ => Err(de::Error::custom("invalid `index` value")),
}
}
fn non_empty_string<'de, D>(deserializer: D) -> std::result::Result<Option<String>, D::Error>
where
D: Deserializer<'de>,
{
let s = <String>::deserialize(deserializer)?;
if s.is_empty() {
Ok(None)
} else {
Ok(Some(s))
}
}
fn toggleflag_de<'de, D>(deserializer: D) -> std::result::Result<ToggleFlag, D::Error>
where
D: Deserializer<'de>,
{
let s = <bool>::deserialize(deserializer);
Ok(match s {
Err(_) => ToggleFlag::Unset,
Ok(true) => ToggleFlag::True,
Ok(false) => ToggleFlag::False,
})
}
/*
* Deserialize default functions
*/
mod default_vals {
2019-06-18 21:13:58 +03:00
pub(in crate::conf) fn false_val() -> bool {
true
}
2019-06-18 21:13:58 +03:00
pub(in crate::conf) fn true_val() -> bool {
true
}
2019-06-18 21:13:58 +03:00
pub(in crate::conf) fn zero_val() -> usize {
0
}
2019-06-18 21:13:58 +03:00
pub(in crate::conf) fn eighty_percent() -> usize {
80
}
2019-06-18 21:13:58 +03:00
pub(in crate::conf) fn none() -> Option<String> {
None
}
}