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.

207 lines
6.0KB

  1. /*
  2. * meli - contacts 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 super::*;
  22. use fnv::FnvHashMap;
  23. mod contact_list;
  24. pub use self::contact_list::*;
  25. #[derive(Debug)]
  26. enum ViewMode {
  27. //ReadOnly,
  28. Read,
  29. //Edit,
  30. //New,
  31. }
  32. #[derive(Debug)]
  33. pub struct ContactManager {
  34. id: ComponentId,
  35. pub card: Card,
  36. mode: ViewMode,
  37. form: FormWidget,
  38. account_pos: usize,
  39. content: CellBuffer,
  40. dirty: bool,
  41. initialized: bool,
  42. }
  43. impl Default for ContactManager {
  44. fn default() -> Self {
  45. ContactManager {
  46. id: Uuid::nil(),
  47. card: Card::new(),
  48. mode: ViewMode::Read,
  49. form: FormWidget::default(),
  50. account_pos: 0,
  51. content: CellBuffer::new(200, 100, Cell::with_char(' ')),
  52. dirty: true,
  53. initialized: false,
  54. }
  55. }
  56. }
  57. impl fmt::Display for ContactManager {
  58. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  59. write!(f, "contacts")
  60. }
  61. }
  62. impl ContactManager {
  63. fn initialize(&mut self) {
  64. let (width, _) = self.content.size();
  65. let (x, _) = write_string_to_grid(
  66. "Contact Name ",
  67. &mut self.content,
  68. Color::Byte(33),
  69. Color::Default,
  70. ((0, 0), (width, 0)),
  71. false,
  72. );
  73. let (x, _) = write_string_to_grid(
  74. "Last edited: ",
  75. &mut self.content,
  76. Color::Byte(250),
  77. Color::Default,
  78. ((x, 0), (width, 0)),
  79. false,
  80. );
  81. write_string_to_grid(
  82. &self.card.last_edited(),
  83. &mut self.content,
  84. Color::Byte(250),
  85. Color::Default,
  86. ((x, 0), (width, 0)),
  87. false,
  88. );
  89. self.form = FormWidget::new("Save".into());
  90. self.form.add_button(("Cancel".into(), false));
  91. self.form
  92. .push(("Name".into(), self.card.name().to_string()));
  93. self.form.push((
  94. "Additional Name".into(),
  95. self.card.additionalname().to_string(),
  96. ));
  97. self.form
  98. .push(("Name Prefix".into(), self.card.name_prefix().to_string()));
  99. self.form
  100. .push(("Name Suffix".into(), self.card.name_suffix().to_string()));
  101. self.form
  102. .push(("E-mail".into(), self.card.email().to_string()));
  103. self.form.push(("url".into(), self.card.url().to_string()));
  104. self.form.push(("key".into(), self.card.key().to_string()));
  105. }
  106. }
  107. impl Component for ContactManager {
  108. fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
  109. if !self.initialized {
  110. self.initialize();
  111. self.initialized = true;
  112. }
  113. clear_area(grid, area);
  114. let (width, _height) = self.content.size();
  115. copy_area(grid, &self.content, area, ((0, 0), (width - 1, 0)));
  116. let upper_left = upper_left!(area);
  117. let bottom_right = bottom_right!(area);
  118. self.form.draw(
  119. grid,
  120. (set_y(upper_left, get_y(upper_left) + 1), bottom_right),
  121. context,
  122. );
  123. context.dirty_areas.push_back(area);
  124. }
  125. fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
  126. if self.form.process_event(event, context) {
  127. match self.form.buttons_result() {
  128. None => {}
  129. Some(true) => {
  130. let fields = std::mem::replace(&mut self.form, FormWidget::default())
  131. .collect()
  132. .unwrap();
  133. let fields: FnvHashMap<String, String> = fields
  134. .into_iter()
  135. .map(|(s, v)| {
  136. (
  137. s,
  138. match v {
  139. Field::Text(v, _) => v.as_str().to_string(),
  140. Field::Choice(mut v, c) => v.remove(c),
  141. },
  142. )
  143. })
  144. .collect();
  145. let mut new_card = Card::from(fields);
  146. new_card.set_id(*self.card.id());
  147. context.accounts[self.account_pos]
  148. .address_book
  149. .add_card(new_card);
  150. context
  151. .replies
  152. .push_back(UIEvent::StatusEvent(StatusEvent::DisplayMessage(
  153. "Saved.".into(),
  154. )));
  155. context.replies.push_back(UIEvent::ComponentKill(self.id));
  156. }
  157. Some(false) => {
  158. context.replies.push_back(UIEvent::ComponentKill(self.id));
  159. }
  160. }
  161. return true;
  162. }
  163. /*
  164. match *event {
  165. UIEvent::Input(Key::Char('\n')) => {
  166. context.replies.push_back(UIEvent {
  167. id: 0,
  168. event_type: UIEvent::ComponentKill(self.id),
  169. });
  170. return true;
  171. },
  172. _ => {},
  173. }
  174. */
  175. false
  176. }
  177. fn is_dirty(&self) -> bool {
  178. self.dirty | self.form.is_dirty()
  179. }
  180. fn set_dirty(&mut self) {
  181. self.dirty = true;
  182. self.initialized = false;
  183. self.form.set_dirty();
  184. }
  185. fn id(&self) -> ComponentId {
  186. self.id
  187. }
  188. fn set_id(&mut self, id: ComponentId) {
  189. self.id = id;
  190. }
  191. }