You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
5.6KB

  1. /*
  2. * meli - addressbook module
  3. *
  4. * Copyright 2019 Manos Pitsidianakis
  5. *
  6. * This file is part of meli.
  7. *
  8. * meli is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * meli is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with meli. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. use chrono::{DateTime, Local};
  22. use fnv::FnvHashMap;
  23. use uuid::Uuid;
  24. use std::ops::Deref;
  25. pub type CardId = Uuid;
  26. #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
  27. pub struct AddressBook {
  28. display_name: String,
  29. created: DateTime<Local>,
  30. last_edited: DateTime<Local>,
  31. cards: FnvHashMap<CardId, Card>,
  32. }
  33. #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
  34. pub struct Card {
  35. id: CardId,
  36. title: String,
  37. name: String,
  38. additionalname: String,
  39. name_prefix: String,
  40. name_suffix: String,
  41. //address
  42. birthday: Option<DateTime<Local>>,
  43. email: String,
  44. url: String,
  45. key: String,
  46. color: u8,
  47. last_edited: DateTime<Local>,
  48. extra_properties: FnvHashMap<String, String>,
  49. }
  50. impl AddressBook {
  51. pub fn new(display_name: String) -> AddressBook {
  52. AddressBook {
  53. display_name,
  54. created: Local::now(),
  55. last_edited: Local::now(),
  56. cards: FnvHashMap::default(),
  57. }
  58. }
  59. pub fn add_card(&mut self, card: Card) {
  60. self.cards.insert(card.id, card);
  61. }
  62. pub fn remove_card(&mut self, card_id: CardId) {
  63. self.cards.remove(&card_id);
  64. }
  65. pub fn card_exists(&self, card_id: CardId) -> bool {
  66. self.cards.contains_key(&card_id)
  67. }
  68. pub fn search(&self, term: &str) -> Vec<String> {
  69. self.cards
  70. .values()
  71. .filter(|c| c.email.contains(term))
  72. .map(|c| c.email.clone())
  73. .collect()
  74. }
  75. }
  76. impl Deref for AddressBook {
  77. type Target = FnvHashMap<CardId, Card>;
  78. fn deref(&self) -> &FnvHashMap<CardId, Card> {
  79. &self.cards
  80. }
  81. }
  82. impl Card {
  83. pub fn new() -> Card {
  84. Card {
  85. id: Uuid::new_v4(),
  86. title: String::new(),
  87. name: String::new(),
  88. additionalname: String::new(),
  89. name_prefix: String::new(),
  90. name_suffix: String::new(),
  91. //address
  92. birthday: None,
  93. email: String::new(),
  94. url: String::new(),
  95. key: String::new(),
  96. last_edited: Local::now(),
  97. extra_properties: FnvHashMap::default(),
  98. color: 0,
  99. }
  100. }
  101. pub fn id(&self) -> &CardId {
  102. &self.id
  103. }
  104. pub fn title(&self) -> &str {
  105. self.title.as_str()
  106. }
  107. pub fn name(&self) -> &str {
  108. self.name.as_str()
  109. }
  110. pub fn additionalname(&self) -> &str {
  111. self.additionalname.as_str()
  112. }
  113. pub fn name_prefix(&self) -> &str {
  114. self.name_prefix.as_str()
  115. }
  116. pub fn name_suffix(&self) -> &str {
  117. self.name_suffix.as_str()
  118. }
  119. pub fn email(&self) -> &str {
  120. self.email.as_str()
  121. }
  122. pub fn url(&self) -> &str {
  123. self.url.as_str()
  124. }
  125. pub fn key(&self) -> &str {
  126. self.key.as_str()
  127. }
  128. pub fn last_edited(&self) -> String {
  129. self.last_edited.to_rfc2822()
  130. }
  131. pub fn set_id(&mut self, new: Uuid) {
  132. self.id = new;
  133. }
  134. pub fn set_title(&mut self, new: String) {
  135. self.title = new;
  136. }
  137. pub fn set_name(&mut self, new: String) {
  138. self.name = new;
  139. }
  140. pub fn set_additionalname(&mut self, new: String) {
  141. self.additionalname = new;
  142. }
  143. pub fn set_name_prefix(&mut self, new: String) {
  144. self.name_prefix = new;
  145. }
  146. pub fn set_name_suffix(&mut self, new: String) {
  147. self.name_suffix = new;
  148. }
  149. pub fn set_email(&mut self, new: String) {
  150. self.email = new;
  151. }
  152. pub fn set_url(&mut self, new: String) {
  153. self.url = new;
  154. }
  155. pub fn set_key(&mut self, new: String) {
  156. self.key = new;
  157. }
  158. pub fn set_extra_property(&mut self, key: &str, value: String) {
  159. self.extra_properties.insert(key.to_string(), value);
  160. }
  161. pub fn extra_property(&self, key: &str) -> Option<&str> {
  162. self.extra_properties.get(key).map(String::as_str)
  163. }
  164. }
  165. impl From<FnvHashMap<String, String>> for Card {
  166. fn from(mut map: FnvHashMap<String, String>) -> Card {
  167. let mut card = Card::new();
  168. if let Some(val) = map.remove("Title") {
  169. card.title = val;
  170. }
  171. if let Some(val) = map.remove("Name") {
  172. card.name = val;
  173. }
  174. if let Some(val) = map.remove("Additional Name") {
  175. card.additionalname = val;
  176. }
  177. if let Some(val) = map.remove("Name Prefix") {
  178. card.name_prefix = val;
  179. }
  180. if let Some(val) = map.remove("Name Suffix") {
  181. card.name_suffix = val;
  182. }
  183. if let Some(val) = map.remove("E-mail") {
  184. card.email = val;
  185. }
  186. if let Some(val) = map.remove("url") {
  187. card.url = val;
  188. }
  189. if let Some(val) = map.remove("key") {
  190. card.key = val;
  191. }
  192. card.extra_properties = map;
  193. card
  194. }
  195. }
  196. impl Default for Card {
  197. fn default() -> Self {
  198. Self::new()
  199. }
  200. }