meli/ui/src/conf.rs

562 lines
16 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 serde;
extern crate toml;
2018-08-19 13:12:48 +03:00
extern crate xdg;
2019-02-15 09:06:42 +02:00
pub mod composing;
2019-03-02 21:40:57 +02:00
pub mod notifications;
2019-03-14 12:19:25 +02:00
pub mod pager;
2019-09-26 12:10:36 +03:00
pub mod pgp;
pub mod shortcuts;
2019-10-06 10:58:47 +03:00
pub mod terminal;
2018-08-19 13:12:48 +03:00
2019-02-10 18:26:49 +02:00
pub mod accounts;
pub use self::accounts::Account;
pub use self::composing::*;
2019-09-26 12:10:36 +03:00
pub use self::pgp::*;
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-10-06 10:58:47 +03:00
use self::terminal::TerminalSettings;
2019-06-18 21:13:58 +03:00
use crate::pager::PagerSettings;
use melib::backends::SpecialUseMailbox;
use melib::conf::{toggleflag_de, AccountSettings, FolderConf, ToggleFlag};
use melib::error::*;
2018-08-19 13:12:48 +03:00
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
2018-08-19 13:12:48 +03:00
use std::collections::HashMap;
use std::env;
use std::fs::{File, OpenOptions};
use std::io::{self, BufRead, Read, Write};
use std::path::PathBuf;
#[macro_export]
macro_rules! split_command {
($cmd:expr) => {{
$cmd.split_whitespace().collect::<Vec<&str>>()
}};
}
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct MailUIConf {
pub pager: Option<PagerSettings>,
pub notifications: Option<NotificationsSettings>,
pub shortcuts: Option<Shortcuts>,
pub composing: Option<ComposingSettings>,
pub identity: Option<String>,
pub index_style: Option<IndexStyle>,
}
#[serde(default)]
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct FileFolderConf {
#[serde(flatten)]
pub folder_conf: FolderConf,
#[serde(flatten)]
pub conf_override: MailUIConf,
}
impl FileFolderConf {
pub fn conf_override(&self) -> &MailUIConf {
&self.conf_override
}
pub fn folder_conf(&self) -> &FolderConf {
&self.folder_conf
}
}
2018-08-19 13:12:48 +03:00
use crate::conf::deserializers::extra_settings;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
2018-08-19 13:12:48 +03:00
pub struct FileAccount {
root_folder: String,
format: String,
identity: String,
#[serde(flatten)]
#[serde(deserialize_with = "extra_settings")]
pub extra: HashMap<String, String>, /* use custom deserializer to convert any given value (eg bool, number, etc) to string */
#[serde(default = "none")]
display_name: Option<String>,
index_style: IndexStyle,
2019-07-18 20:16:51 +03:00
#[serde(default = "false_val")]
read_only: bool,
2019-08-23 21:32:32 +03:00
subscribed_folders: Vec<String>,
#[serde(default)]
folders: HashMap<String, FileFolderConf>,
#[serde(default)]
cache_type: CacheType,
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 root_folder = x.root_folder.clone();
let identity = x.identity.clone();
let display_name = x.display_name.clone();
let folders = x
.folders
.iter()
.map(|(k, v)| (k.clone(), v.folder_conf.clone()))
.collect();
2019-08-23 21:32:32 +03:00
let mut acc = AccountSettings {
name: String::new(),
root_folder,
format,
identity,
2019-07-18 20:16:51 +03:00
read_only: x.read_only,
display_name,
2019-08-23 21:32:32 +03:00
subscribed_folders: x.subscribed_folders.clone(),
folders,
extra: x.extra.clone(),
};
2019-08-23 21:32:32 +03:00
let root_path = PathBuf::from(acc.root_folder.as_str());
let root_tmp = root_path
.components()
.last()
.unwrap()
.as_os_str()
.to_str()
.unwrap()
.to_string();
if !acc.subscribed_folders.contains(&root_tmp) {
acc.subscribed_folders.push(root_tmp);
}
let mut folder_confs = x.folders.clone();
2019-08-23 21:32:32 +03:00
for s in &x.subscribed_folders {
if !folder_confs.contains_key(s) {
use text_processing::GlobMatch;
if s.is_glob() {
continue;
}
2019-08-23 21:32:32 +03:00
folder_confs.insert(
s.to_string(),
FileFolderConf {
folder_conf: FolderConf {
subscribe: ToggleFlag::True,
..FolderConf::default()
},
..FileFolderConf::default()
2019-08-23 21:32:32 +03:00
},
);
} else {
if !folder_confs[s].folder_conf().subscribe.is_unset() {
continue;
2019-08-23 21:32:32 +03:00
}
folder_confs.get_mut(s).unwrap().folder_conf.subscribe = ToggleFlag::True;
2019-08-23 21:32:32 +03:00
}
if folder_confs[s].folder_conf().usage.is_none() {
let name = s
.split(if s.contains('/') { '/' } else { '.' })
.last()
.unwrap_or("");
folder_confs.get_mut(s).unwrap().folder_conf.usage = usage(name);
}
if folder_confs[s].folder_conf().ignore.is_unset() {
use SpecialUseMailbox::*;
if [Junk, Sent, Trash]
.contains(&folder_confs[s].folder_conf().usage.as_ref().unwrap())
{
folder_confs.get_mut(s).unwrap().folder_conf.ignore =
ToggleFlag::InternalVal(true);
}
}
2019-08-23 21:32:32 +03:00
}
AccountConf {
account: acc,
conf: x,
folder_confs,
}
}
}
2018-08-19 13:12:48 +03:00
impl FileAccount {
pub fn folders(&self) -> &HashMap<String, FileFolderConf> {
&self.folders
}
2018-08-19 13:12:48 +03:00
pub fn folder(&self) -> &str {
&self.root_folder
}
pub fn index_style(&self) -> IndexStyle {
self.index_style
2019-04-10 18:57:09 +03:00
}
pub fn cache_type(&self) -> &CacheType {
&self.cache_type
}
2018-08-19 13:12:48 +03:00
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FileSettings {
2018-08-19 13:12:48 +03:00
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,
composing: ComposingSettings,
2019-09-26 12:10:36 +03:00
#[serde(default)]
pgp: PGPSettings,
2019-10-06 10:58:47 +03:00
#[serde(default)]
terminal: TerminalSettings,
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 {
pub(crate) account: AccountSettings,
pub(crate) conf: FileAccount,
pub(crate) folder_confs: HashMap<String, FileFolderConf>,
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 composing: ComposingSettings,
2019-09-26 12:10:36 +03:00
pub pgp: PGPSettings,
2019-10-06 10:58:47 +03:00
pub terminal: TerminalSettings,
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());
return Err(MeliError::new(
"Edit the sample configuration and relaunch meli.",
));
}
"n" | "N" | "no" | "No" | "NO" => {
return Err(MeliError::new("No configuration file found."));
}
_ => {
println!(
"No configuration found. Would you like to generate one in {}? [Y/n]",
config_path.display()
);
}
}
}
}
let mut file = File::open(config_path.to_str().unwrap())?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let s = toml::from_str(&contents);
if let Err(e) = s {
return Err(MeliError::new(format!(
"Config file contains errors: {}",
e.to_string()
)));
}
2018-08-19 13:12:48 +03:00
Ok(s.unwrap())
2018-08-19 13:12:48 +03:00
}
pub fn validate(path: &str) -> Result<()> {
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let s: std::result::Result<FileSettings, toml::de::Error> = toml::from_str(&contents);
if let Err(e) = s {
return Err(MeliError::new(format!(
"Config file contains errors: {}",
e.to_string()
)));
}
Ok(())
}
2018-08-19 13:12:48 +03:00
}
impl Settings {
pub fn new() -> Result<Settings> {
let fs = FileSettings::new()?;
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
}
Ok(Settings {
2018-08-19 13:12:48 +03:00
accounts: s,
pager: fs.pager,
2019-03-02 21:40:57 +02:00
notifications: fs.notifications,
shortcuts: fs.shortcuts,
composing: fs.composing,
2019-09-26 12:10:36 +03:00
pgp: fs.pgp,
2019-10-06 10:58:47 +03:00
terminal: fs.terminal,
})
2018-08-19 13:12:48 +03:00
}
}
#[derive(Copy, Debug, Clone, Hash, PartialEq)]
pub enum IndexStyle {
Plain,
Threaded,
Compact,
Conversations,
}
impl Default for IndexStyle {
fn default() -> Self {
IndexStyle::Compact
}
}
/*
* Deserialize default functions
*/
mod default_vals {
2019-06-18 21:13:58 +03:00
pub(in crate::conf) fn false_val() -> bool {
2019-07-18 20:16:51 +03:00
false
}
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-08-23 21:32:32 +03:00
pub(in crate::conf) fn none<T>() -> Option<T> {
None
}
pub(in crate::conf) fn internal_value_false() -> super::ToggleFlag {
super::ToggleFlag::InternalVal(false)
}
}
mod deserializers {
use serde::{Deserialize, Deserializer};
pub(in crate::conf) 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))
}
}
use toml::Value;
fn any_of<'de, D>(deserializer: D) -> std::result::Result<String, D::Error>
where
D: Deserializer<'de>,
{
let v: Value = Deserialize::deserialize(deserializer)?;
let mut ret = v.to_string();
if ret.starts_with('"') && ret.ends_with('"') {
ret.drain(0..1).count();
ret.drain(ret.len() - 1..).count();
}
Ok(ret)
}
use std::collections::HashMap;
pub(in crate::conf) fn extra_settings<'de, D>(
deserializer: D,
) -> std::result::Result<HashMap<String, String>, D::Error>
where
D: Deserializer<'de>,
{
/* Why is this needed? If the user gives a configuration value such as key = true, the
* parsing will fail since it expects string values. We want to accept key = true as well
* as key = "true". */
#[derive(Deserialize)]
struct Wrapper(#[serde(deserialize_with = "any_of")] String);
let v = <HashMap<String, Wrapper>>::deserialize(deserializer)?;
Ok(v.into_iter().map(|(k, Wrapper(v))| (k, v)).collect())
}
}
impl<'de> Deserialize<'de> for IndexStyle {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
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),
"Conversations" | "conversations" => Ok(IndexStyle::Conversations),
_ => Err(de::Error::custom("invalid `index_style` value")),
}
}
}
impl Serialize for IndexStyle {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
IndexStyle::Plain => serializer.serialize_str("plain"),
IndexStyle::Threaded => serializer.serialize_str("threaded"),
IndexStyle::Compact => serializer.serialize_str("compact"),
IndexStyle::Conversations => serializer.serialize_str("conversations"),
}
}
}
2019-10-30 21:09:48 +02:00
#[derive(Debug, Clone, PartialEq)]
pub enum CacheType {
None,
2019-10-30 21:09:48 +02:00
#[cfg(feature = "sqlite3")]
Sqlite3,
}
impl Default for CacheType {
2019-10-30 21:09:48 +02:00
fn default() -> Self {
#[cfg(feature = "sqlite3")]
{
CacheType::Sqlite3
2019-10-30 21:09:48 +02:00
}
#[cfg(not(feature = "sqlite3"))]
{
CacheType::None
}
}
}
impl<'de> Deserialize<'de> for CacheType {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = <String>::deserialize(deserializer)?;
match s.as_str() {
#[cfg(feature = "sqlite3")]
"sqlite3" => Ok(CacheType::Sqlite3),
"nothing" | "none" | "" => Ok(CacheType::None),
_ => Err(de::Error::custom("invalid `index_cache` value")),
}
}
}
impl Serialize for CacheType {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
#[cfg(feature = "sqlite3")]
CacheType::Sqlite3 => serializer.serialize_str("sqlite3"),
CacheType::None => serializer.serialize_str("none"),
2019-10-30 21:09:48 +02:00
}
}
}
pub fn usage(name: &str) -> Option<SpecialUseMailbox> {
if name.eq_ignore_ascii_case("inbox") {
Some(SpecialUseMailbox::Inbox)
} else if name.eq_ignore_ascii_case("archive") {
Some(SpecialUseMailbox::Archive)
} else if name.eq_ignore_ascii_case("drafts") {
Some(SpecialUseMailbox::Drafts)
} else if name.eq_ignore_ascii_case("junk") {
Some(SpecialUseMailbox::Junk)
} else if name.eq_ignore_ascii_case("spam") {
Some(SpecialUseMailbox::Junk)
} else if name.eq_ignore_ascii_case("sent") {
Some(SpecialUseMailbox::Sent)
} else if name.eq_ignore_ascii_case("trash") {
Some(SpecialUseMailbox::Trash)
} else {
Some(SpecialUseMailbox::Normal)
}
}