2019-02-15 07:06:42 +00:00
|
|
|
/*
|
|
|
|
* meli - addressbook module
|
|
|
|
*
|
|
|
|
* Copyright 2019 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-10-16 11:55:49 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "vcard")]
|
|
|
|
pub mod vcard;
|
|
|
|
|
2020-01-06 14:10:36 +00:00
|
|
|
use crate::datetime::{self, UnixTimestamp};
|
2020-05-10 18:14:49 +00:00
|
|
|
use std::collections::HashMap;
|
2019-03-14 10:19:25 +00:00
|
|
|
use uuid::Uuid;
|
2019-02-15 07:06:42 +00:00
|
|
|
|
2019-02-18 21:14:06 +00:00
|
|
|
use std::ops::Deref;
|
2019-02-15 07:06:42 +00:00
|
|
|
|
2019-10-20 08:06:26 +00:00
|
|
|
#[derive(Hash, Debug, PartialEq, Eq, Clone, Copy, Deserialize, Serialize)]
|
|
|
|
#[serde(from = "String")]
|
|
|
|
#[serde(into = "String")]
|
|
|
|
pub enum CardId {
|
|
|
|
Uuid(Uuid),
|
|
|
|
Hash(u64),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<String> for CardId {
|
|
|
|
fn into(self) -> String {
|
|
|
|
match self {
|
|
|
|
CardId::Uuid(u) => u.to_string(),
|
|
|
|
CardId::Hash(u) => u.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<String> for CardId {
|
|
|
|
fn from(s: String) -> CardId {
|
|
|
|
if let Ok(u) = uuid::Uuid::parse_str(s.as_str()) {
|
|
|
|
CardId::Uuid(u)
|
|
|
|
} else {
|
|
|
|
use std::str::FromStr;
|
|
|
|
CardId::Hash(u64::from_str(&s).unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-21 13:44:26 +00:00
|
|
|
|
2019-02-15 07:06:42 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
|
|
|
|
pub struct AddressBook {
|
|
|
|
display_name: String,
|
2020-01-06 14:10:36 +00:00
|
|
|
created: UnixTimestamp,
|
|
|
|
last_edited: UnixTimestamp,
|
2020-05-10 18:14:49 +00:00
|
|
|
pub cards: HashMap<CardId, Card>,
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
|
|
|
|
pub struct Card {
|
2019-02-21 13:44:26 +00:00
|
|
|
id: CardId,
|
2019-02-15 07:06:42 +00:00
|
|
|
title: String,
|
2019-06-06 09:21:47 +00:00
|
|
|
name: String,
|
2019-02-15 07:06:42 +00:00
|
|
|
additionalname: String,
|
|
|
|
name_prefix: String,
|
|
|
|
name_suffix: String,
|
|
|
|
//address
|
2020-01-06 14:10:36 +00:00
|
|
|
birthday: Option<UnixTimestamp>,
|
2019-02-15 07:06:42 +00:00
|
|
|
email: String,
|
|
|
|
url: String,
|
|
|
|
key: String,
|
|
|
|
|
2019-02-26 08:55:22 +00:00
|
|
|
color: u8,
|
2020-01-06 14:10:36 +00:00
|
|
|
last_edited: UnixTimestamp,
|
2020-05-10 18:14:49 +00:00
|
|
|
extra_properties: HashMap<String, String>,
|
2019-10-20 08:14:29 +00:00
|
|
|
|
|
|
|
/// If true, we can't make any changes because we do not manage this resource.
|
|
|
|
external_resource: bool,
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AddressBook {
|
|
|
|
pub fn new(display_name: String) -> AddressBook {
|
|
|
|
AddressBook {
|
|
|
|
display_name,
|
2020-01-06 14:10:36 +00:00
|
|
|
created: datetime::now(),
|
|
|
|
last_edited: datetime::now(),
|
2020-05-10 18:14:49 +00:00
|
|
|
cards: HashMap::default(),
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-26 23:39:06 +00:00
|
|
|
|
|
|
|
pub fn with_account(s: &crate::conf::AccountSettings) -> AddressBook {
|
2020-09-09 11:24:30 +00:00
|
|
|
#[cfg(not(feature = "vcard"))]
|
|
|
|
{
|
|
|
|
AddressBook::new(s.name.clone())
|
|
|
|
}
|
2019-11-26 23:39:06 +00:00
|
|
|
#[cfg(feature = "vcard")]
|
|
|
|
{
|
2020-09-09 11:24:30 +00:00
|
|
|
let mut ret = AddressBook::new(s.name.clone());
|
2019-11-26 23:39:06 +00:00
|
|
|
if let Some(vcard_path) = s.vcard_folder() {
|
2021-10-24 11:17:49 +00:00
|
|
|
match vcard::load_cards(std::path::Path::new(vcard_path)) {
|
|
|
|
Ok(cards) => {
|
|
|
|
for c in cards {
|
|
|
|
ret.add_card(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
crate::log(
|
|
|
|
format!("Could not load vcards from {:?}: {}", vcard_path, err),
|
|
|
|
crate::WARN,
|
|
|
|
);
|
2019-11-26 23:39:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-09 11:24:30 +00:00
|
|
|
ret
|
2019-11-26 23:39:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 07:06:42 +00:00
|
|
|
pub fn add_card(&mut self, card: Card) {
|
2019-02-21 13:44:26 +00:00
|
|
|
self.cards.insert(card.id, card);
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
2019-02-21 13:44:26 +00:00
|
|
|
pub fn remove_card(&mut self, card_id: CardId) {
|
|
|
|
self.cards.remove(&card_id);
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
2019-02-21 13:44:26 +00:00
|
|
|
pub fn card_exists(&self, card_id: CardId) -> bool {
|
|
|
|
self.cards.contains_key(&card_id)
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
2019-03-09 08:24:28 +00:00
|
|
|
pub fn search(&self, term: &str) -> Vec<String> {
|
2019-03-14 10:19:25 +00:00
|
|
|
self.cards
|
|
|
|
.values()
|
|
|
|
.filter(|c| c.email.contains(term))
|
2019-11-26 23:39:06 +00:00
|
|
|
.map(|c| format!("{} <{}>", &c.name, &c.email))
|
2019-03-14 10:19:25 +00:00
|
|
|
.collect()
|
2019-03-09 08:24:28 +00:00
|
|
|
}
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 21:14:06 +00:00
|
|
|
impl Deref for AddressBook {
|
2020-05-10 18:14:49 +00:00
|
|
|
type Target = HashMap<CardId, Card>;
|
2019-02-18 21:14:06 +00:00
|
|
|
|
2020-05-10 18:14:49 +00:00
|
|
|
fn deref(&self) -> &HashMap<CardId, Card> {
|
2019-02-18 21:14:06 +00:00
|
|
|
&self.cards
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 07:06:42 +00:00
|
|
|
impl Card {
|
|
|
|
pub fn new() -> Card {
|
|
|
|
Card {
|
2019-10-20 08:06:26 +00:00
|
|
|
id: CardId::Uuid(Uuid::new_v4()),
|
2019-02-15 07:06:42 +00:00
|
|
|
title: String::new(),
|
2019-06-06 09:21:47 +00:00
|
|
|
name: String::new(),
|
2019-02-15 07:06:42 +00:00
|
|
|
additionalname: String::new(),
|
|
|
|
name_prefix: String::new(),
|
|
|
|
name_suffix: String::new(),
|
|
|
|
//address
|
|
|
|
birthday: None,
|
|
|
|
email: String::new(),
|
|
|
|
url: String::new(),
|
|
|
|
key: String::new(),
|
|
|
|
|
2020-01-06 14:10:36 +00:00
|
|
|
last_edited: datetime::now(),
|
2019-10-20 08:14:29 +00:00
|
|
|
external_resource: false,
|
2020-05-10 18:14:49 +00:00
|
|
|
extra_properties: HashMap::default(),
|
2019-02-26 08:55:22 +00:00
|
|
|
color: 0,
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 13:44:26 +00:00
|
|
|
pub fn id(&self) -> &CardId {
|
|
|
|
&self.id
|
2019-02-18 21:14:06 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 07:06:42 +00:00
|
|
|
pub fn title(&self) -> &str {
|
|
|
|
self.title.as_str()
|
|
|
|
}
|
2019-06-06 09:21:47 +00:00
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
self.name.as_str()
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
|
|
|
pub fn additionalname(&self) -> &str {
|
|
|
|
self.additionalname.as_str()
|
|
|
|
}
|
|
|
|
pub fn name_prefix(&self) -> &str {
|
|
|
|
self.name_prefix.as_str()
|
|
|
|
}
|
|
|
|
pub fn name_suffix(&self) -> &str {
|
|
|
|
self.name_suffix.as_str()
|
|
|
|
}
|
|
|
|
pub fn email(&self) -> &str {
|
|
|
|
self.email.as_str()
|
|
|
|
}
|
|
|
|
pub fn url(&self) -> &str {
|
|
|
|
self.url.as_str()
|
|
|
|
}
|
|
|
|
pub fn key(&self) -> &str {
|
|
|
|
self.key.as_str()
|
|
|
|
}
|
2019-02-18 21:14:06 +00:00
|
|
|
pub fn last_edited(&self) -> String {
|
2021-01-05 18:44:57 +00:00
|
|
|
datetime::timestamp_to_string(self.last_edited, None, false)
|
2019-02-18 21:14:06 +00:00
|
|
|
}
|
2019-02-15 07:06:42 +00:00
|
|
|
|
2019-10-20 08:06:26 +00:00
|
|
|
pub fn set_id(&mut self, new_val: CardId) {
|
|
|
|
self.id = new_val;
|
2019-02-25 10:00:17 +00:00
|
|
|
}
|
2019-03-31 17:08:37 +00:00
|
|
|
pub fn set_title(&mut self, new: String) {
|
|
|
|
self.title = new;
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
2019-06-06 09:21:47 +00:00
|
|
|
pub fn set_name(&mut self, new: String) {
|
|
|
|
self.name = new;
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
2019-03-31 17:08:37 +00:00
|
|
|
pub fn set_additionalname(&mut self, new: String) {
|
|
|
|
self.additionalname = new;
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
2019-03-31 17:08:37 +00:00
|
|
|
pub fn set_name_prefix(&mut self, new: String) {
|
|
|
|
self.name_prefix = new;
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
2019-03-31 17:08:37 +00:00
|
|
|
pub fn set_name_suffix(&mut self, new: String) {
|
|
|
|
self.name_suffix = new;
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
2019-03-31 17:08:37 +00:00
|
|
|
pub fn set_email(&mut self, new: String) {
|
|
|
|
self.email = new;
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
2019-03-31 17:08:37 +00:00
|
|
|
pub fn set_url(&mut self, new: String) {
|
|
|
|
self.url = new;
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
2019-03-31 17:08:37 +00:00
|
|
|
pub fn set_key(&mut self, new: String) {
|
|
|
|
self.key = new;
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_extra_property(&mut self, key: &str, value: String) {
|
|
|
|
self.extra_properties.insert(key.to_string(), value);
|
|
|
|
}
|
2019-10-20 08:14:29 +00:00
|
|
|
|
2019-02-15 07:06:42 +00:00
|
|
|
pub fn extra_property(&self, key: &str) -> Option<&str> {
|
2019-06-18 18:13:58 +00:00
|
|
|
self.extra_properties.get(key).map(String::as_str)
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
2019-10-20 08:14:29 +00:00
|
|
|
|
2020-05-10 18:14:49 +00:00
|
|
|
pub fn extra_properties(&self) -> &HashMap<String, String> {
|
2019-10-20 08:14:29 +00:00
|
|
|
&self.extra_properties
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_external_resource(&mut self, new_val: bool) {
|
|
|
|
self.external_resource = new_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn external_resource(&self) -> bool {
|
|
|
|
self.external_resource
|
|
|
|
}
|
2019-02-15 07:06:42 +00:00
|
|
|
}
|
2019-02-25 10:00:17 +00:00
|
|
|
|
2020-05-10 18:14:49 +00:00
|
|
|
impl From<HashMap<String, String>> for Card {
|
|
|
|
fn from(mut map: HashMap<String, String>) -> Card {
|
2019-02-25 10:00:17 +00:00
|
|
|
let mut card = Card::new();
|
2019-10-20 08:14:29 +00:00
|
|
|
if let Some(val) = map.remove("TITLE") {
|
2019-02-25 10:00:17 +00:00
|
|
|
card.title = val;
|
|
|
|
}
|
2019-10-20 08:14:29 +00:00
|
|
|
if let Some(val) = map.remove("NAME") {
|
2019-06-06 09:21:47 +00:00
|
|
|
card.name = val;
|
2019-02-25 10:00:17 +00:00
|
|
|
}
|
2019-10-20 08:14:29 +00:00
|
|
|
if let Some(val) = map.remove("ADDITIONAL NAME") {
|
2019-02-25 10:00:17 +00:00
|
|
|
card.additionalname = val;
|
|
|
|
}
|
2019-10-20 08:14:29 +00:00
|
|
|
if let Some(val) = map.remove("NAME PREFIX") {
|
2019-02-25 10:00:17 +00:00
|
|
|
card.name_prefix = val;
|
|
|
|
}
|
2019-10-20 08:14:29 +00:00
|
|
|
if let Some(val) = map.remove("NAME SUFFIX") {
|
2019-02-25 10:00:17 +00:00
|
|
|
card.name_suffix = val;
|
|
|
|
}
|
|
|
|
|
2019-10-20 08:14:29 +00:00
|
|
|
if let Some(val) = map.remove("E-MAIL") {
|
2019-02-25 10:00:17 +00:00
|
|
|
card.email = val;
|
|
|
|
}
|
2019-10-20 08:14:29 +00:00
|
|
|
if let Some(val) = map.remove("URL") {
|
2019-02-25 10:00:17 +00:00
|
|
|
card.url = val;
|
|
|
|
}
|
2019-10-20 08:14:29 +00:00
|
|
|
if let Some(val) = map.remove("KEY") {
|
2019-02-25 10:00:17 +00:00
|
|
|
card.key = val;
|
|
|
|
}
|
|
|
|
card.extra_properties = map;
|
|
|
|
card
|
|
|
|
}
|
|
|
|
}
|
2019-03-03 20:11:15 +00:00
|
|
|
|
|
|
|
impl Default for Card {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|