2017-09-23 15:51:35 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2017-09-23 15:51:35 +00:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library 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
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2018-04-25 10:54:00 +00:00
|
|
|
use toml::Value;
|
|
|
|
use toml::to_string as toml_to_string;
|
|
|
|
use toml::from_str as toml_from_str;
|
|
|
|
use toml_query::insert::TomlValueInsertExt;
|
|
|
|
use vobject::vcard::Vcard;
|
2018-10-30 17:40:51 +00:00
|
|
|
use failure::Error;
|
|
|
|
use failure::Fallible as Result;
|
2017-09-23 18:18:20 +00:00
|
|
|
|
2018-04-25 10:54:00 +00:00
|
|
|
use libimagstore::storeid::IntoStoreId;
|
|
|
|
use libimagstore::storeid::StoreId;
|
2017-09-23 15:51:35 +00:00
|
|
|
use libimagstore::store::Store;
|
|
|
|
use libimagstore::store::FileLockEntry;
|
2017-10-02 18:44:08 +00:00
|
|
|
use libimagstore::storeid::StoreIdIterator;
|
2018-01-08 22:39:23 +00:00
|
|
|
use libimagentryutil::isa::Is;
|
2017-09-23 15:51:35 +00:00
|
|
|
|
2018-01-08 22:39:23 +00:00
|
|
|
use contact::IsContact;
|
2018-04-25 10:54:00 +00:00
|
|
|
use deser::DeserVcard;
|
|
|
|
use module_path::ModuleEntryPath;
|
2017-09-23 18:18:20 +00:00
|
|
|
use util;
|
2017-09-23 15:51:35 +00:00
|
|
|
|
2018-04-25 09:28:40 +00:00
|
|
|
pub trait ContactStore<'a> {
|
2017-09-23 15:51:35 +00:00
|
|
|
|
|
|
|
// creating
|
|
|
|
|
2018-04-25 10:54:00 +00:00
|
|
|
fn create_from_path(&'a self, p: &PathBuf) -> Result<FileLockEntry<'a>>;
|
|
|
|
fn retrieve_from_path(&'a self, p: &PathBuf) -> Result<FileLockEntry<'a>>;
|
2017-09-23 15:51:35 +00:00
|
|
|
|
2018-04-25 10:54:00 +00:00
|
|
|
fn create_from_buf(&'a self, buf: &str) -> Result<FileLockEntry<'a>>;
|
|
|
|
fn retrieve_from_buf(&'a self, buf: &str) -> Result<FileLockEntry<'a>>;
|
2017-09-23 18:18:20 +00:00
|
|
|
|
2017-09-23 15:51:35 +00:00
|
|
|
// getting
|
|
|
|
|
2017-10-02 18:44:08 +00:00
|
|
|
fn all_contacts(&'a self) -> Result<StoreIdIterator>;
|
2017-09-23 15:51:35 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 18:44:08 +00:00
|
|
|
/// The extension for the Store to work with contacts
|
2017-09-23 15:51:35 +00:00
|
|
|
impl<'a> ContactStore<'a> for Store {
|
|
|
|
|
|
|
|
fn create_from_path(&'a self, p: &PathBuf) -> Result<FileLockEntry<'a>> {
|
2018-04-25 09:28:40 +00:00
|
|
|
util::read_to_string(p).and_then(|buf| self.create_from_buf(&buf))
|
2017-09-23 18:18:20 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 10:54:00 +00:00
|
|
|
fn retrieve_from_path(&'a self, p: &PathBuf) -> Result<FileLockEntry<'a>> {
|
|
|
|
util::read_to_string(p).and_then(|buf| self.retrieve_from_buf(&buf))
|
|
|
|
}
|
|
|
|
|
2017-09-23 18:18:20 +00:00
|
|
|
/// Create contact ref from buffer
|
2018-04-25 10:54:00 +00:00
|
|
|
fn create_from_buf(&'a self, buf: &str) -> Result<FileLockEntry<'a>> {
|
|
|
|
let (sid, value) = prepare_fetching_from_store(buf)?;
|
|
|
|
postprocess_fetched_entry(self.create(sid)?, value)
|
|
|
|
}
|
2017-09-23 18:18:20 +00:00
|
|
|
|
2018-04-25 10:54:00 +00:00
|
|
|
fn retrieve_from_buf(&'a self, buf: &str) -> Result<FileLockEntry<'a>> {
|
|
|
|
let (sid, value) = prepare_fetching_from_store(buf)?;
|
|
|
|
postprocess_fetched_entry(self.retrieve(sid)?, value)
|
2017-09-23 15:51:35 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 18:44:08 +00:00
|
|
|
fn all_contacts(&'a self) -> Result<StoreIdIterator> {
|
2018-03-12 11:52:24 +00:00
|
|
|
let iter = self
|
|
|
|
.entries()?
|
|
|
|
.without_store()
|
2018-04-30 15:28:54 +00:00
|
|
|
.filter(|id| match *id {
|
|
|
|
Ok(ref id) => id.is_in_collection(&["contact"]),
|
|
|
|
Err(_) => true,
|
|
|
|
});
|
2018-03-12 11:52:24 +00:00
|
|
|
|
|
|
|
Ok(StoreIdIterator::new(Box::new(iter)))
|
2017-09-23 15:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-10-02 18:44:08 +00:00
|
|
|
|
2018-04-25 10:54:00 +00:00
|
|
|
/// Prepare the fetching from the store.
|
|
|
|
///
|
|
|
|
/// That means calculating the StoreId and the Value from the vcard data
|
|
|
|
fn prepare_fetching_from_store(buf: &str) -> Result<(StoreId, Value)> {
|
2018-10-30 17:40:51 +00:00
|
|
|
let vcard = Vcard::build(&buf).map_err(Error::from)?;
|
2018-04-25 10:54:00 +00:00
|
|
|
debug!("Parsed: {:?}", vcard);
|
|
|
|
|
2018-10-30 17:40:51 +00:00
|
|
|
let uid = vcard.uid()
|
|
|
|
.ok_or_else(|| Error::from(format_err!("UID Missing: {}", buf.to_string())))?;
|
2018-04-25 10:54:00 +00:00
|
|
|
|
|
|
|
let value = { // dirty ugly hack
|
|
|
|
let serialized = DeserVcard::from(vcard);
|
|
|
|
let serialized = toml_to_string(&serialized)?;
|
|
|
|
toml_from_str::<Value>(&serialized)?
|
|
|
|
};
|
|
|
|
|
|
|
|
let sid = ModuleEntryPath::new(uid.raw()).into_storeid()?;
|
|
|
|
|
|
|
|
Ok((sid, value))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Postprocess the entry just fetched from the store
|
|
|
|
fn postprocess_fetched_entry<'a>(mut entry: FileLockEntry<'a>, value: Value) -> Result<FileLockEntry<'a>> {
|
|
|
|
entry.set_isflag::<IsContact>()?;
|
|
|
|
entry.get_header_mut().insert("contact.data", value)?;
|
|
|
|
Ok(entry)
|
|
|
|
}
|
|
|
|
|