meli/melib/src/conf/mod.rs

174 lines
5.0 KiB
Rust
Raw Normal View History

2017-09-01 15:24:32 +03:00
/*
* meli - configuration module.
*
* Copyright 2017 Manos Pitsidianakis
*
2017-09-01 15:24:32 +03:00
* 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/>.
*/
extern crate config;
extern crate xdg;
2018-07-11 18:58:57 +03:00
extern crate serde;
pub mod pager;
use pager::PagerSettings;
2018-07-11 18:58:57 +03:00
2017-09-01 15:24:32 +03:00
use std::collections::HashMap;
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher;
2017-09-01 15:24:32 +03:00
use std::fs;
use std::path::{Path, PathBuf};
2017-09-01 15:24:32 +03:00
2018-07-11 23:12:25 +03:00
#[derive(Debug, Default, Clone)]
pub struct Folder {
hash: u64,
2018-07-11 23:12:25 +03:00
name: String,
path: String,
children: Vec<usize>,
}
impl Folder {
fn new(path: String, file_name: String, children: Vec<usize>) -> Self {
let mut h = DefaultHasher::new();
h.write(&path.as_bytes());
2018-07-11 23:12:25 +03:00
Folder {
hash: h.finish(),
2018-07-11 23:12:25 +03:00
name: file_name,
path: path,
children: children,
}
}
pub fn path(&self) -> &str {
2018-07-11 23:12:25 +03:00
&self.path
}
pub fn name(&self) -> &str {
2018-07-11 23:12:25 +03:00
&self.name
}
pub fn children(&self) -> &Vec<usize> {
2018-07-11 23:12:25 +03:00
&self.children
}
}
2017-09-01 15:24:32 +03:00
#[derive(Debug, Deserialize)]
struct FileAccount {
folders: String,
format: String,
sent_folder: String,
threaded: bool,
2017-09-01 15:24:32 +03:00
}
2018-07-14 18:03:43 +03:00
#[derive(Debug, Deserialize)]
2017-09-01 15:24:32 +03:00
struct FileSettings {
accounts: HashMap<String, FileAccount>,
pager: PagerSettings,
2017-09-01 15:24:32 +03:00
}
#[derive(Debug, Clone)]
pub struct AccountSettings {
2018-07-11 23:12:25 +03:00
name: String,
pub folders: Vec<Folder>,
format: String,
2017-09-01 15:24:32 +03:00
pub sent_folder: String,
pub threaded: bool,
2017-09-01 15:24:32 +03:00
}
impl AccountSettings {
pub fn format(&self) -> &str {
&self.format
}
pub fn name(&self) -> &str {
2018-07-11 23:12:25 +03:00
&self.name
}
}
#[derive(Debug, Clone, Default)]
2017-09-01 15:24:32 +03:00
pub struct Settings {
pub accounts: HashMap<String, AccountSettings>,
pub pager: PagerSettings,
2017-09-01 15:24:32 +03:00
}
use self::config::{Config, File, FileFormat};
impl FileSettings {
pub fn new() -> FileSettings {
let xdg_dirs = xdg::BaseDirectories::with_prefix("meli").unwrap();
let config_path = xdg_dirs
.place_config_file("config")
.expect("cannot create configuration directory");
2017-09-01 15:24:32 +03:00
//let setts = Config::default().merge(File::new(config_path.to_str().unwrap_or_default(), config::FileFormat::Toml)).unwrap();
let mut s = Config::new();
let s = s.merge(File::new(config_path.to_str().unwrap(), FileFormat::Toml));
2018-07-14 18:03:43 +03:00
// TODO: Return result
s.unwrap().deserialize().unwrap()
2017-09-01 15:24:32 +03:00
}
}
impl Settings {
pub fn new() -> Settings {
let fs = FileSettings::new();
let mut s: HashMap<String, AccountSettings> = HashMap::new();
2017-09-01 15:24:32 +03:00
for (id, x) in fs.accounts {
let mut folders = Vec::new();
2018-07-11 23:12:25 +03:00
fn recurse_folders<P: AsRef<Path>>(folders: &mut Vec<Folder>, p: P) -> Vec<usize> {
let mut children = Vec::new();
for mut f in fs::read_dir(p).unwrap() {
for f in f.iter_mut() {
{
let path = f.path();
if path.ends_with("cur") || path.ends_with("new") ||
path.ends_with("tmp")
{
2017-09-01 15:24:32 +03:00
continue;
}
if path.is_dir() {
2018-07-11 23:12:25 +03:00
let path_children = recurse_folders(folders, &path);
folders.push(Folder::new(path.to_str().unwrap().to_string(), path.file_name().unwrap().to_str().unwrap().to_string(), path_children));
children.push(folders.len()-1);
}
2017-09-01 15:24:32 +03:00
}
}
}
2018-07-11 23:12:25 +03:00
children
2017-09-01 15:24:32 +03:00
};
let path = PathBuf::from(&x.folders);
2018-07-11 23:12:25 +03:00
let path_children = recurse_folders(&mut folders, &path);
2017-09-01 15:24:32 +03:00
if path.is_dir() {
2018-07-11 23:12:25 +03:00
folders.push(Folder::new(path.to_str().unwrap().to_string(), path.file_name().unwrap().to_str().unwrap().to_string(), path_children));
2017-09-01 15:24:32 +03:00
}
//folders.sort_by(|a, b| b.name.cmp(&a.name));
s.insert(
id.clone(),
AccountSettings {
2018-07-11 23:12:25 +03:00
name: id.clone(),
folders: folders,
format: x.format.to_lowercase(),
sent_folder: x.sent_folder.clone(),
threaded: x.threaded,
},
);
2017-09-01 15:24:32 +03:00
}
Settings { accounts: s, pager: fs.pager }
2017-09-01 15:24:32 +03:00
}
}