🐝 I really like where this mua is(was?) headed, but it seems as though there has not been much activity recently.
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.

212 lines
6.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  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: Uuid,
  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(("First Name".into(), self.card.firstname().to_string()));
  93. self.form
  94. .push(("Last Name".into(), self.card.lastname().to_string()));
  95. self.form.push((
  96. "Additional Name".into(),
  97. self.card.additionalname().to_string(),
  98. ));
  99. self.form
  100. .push(("Name Prefix".into(), self.card.name_prefix().to_string()));
  101. self.form
  102. .push(("Name Suffix".into(), self.card.name_suffix().to_string()));
  103. self.form
  104. .push(("E-mail".into(), self.card.email().to_string()));
  105. self.form.push(("url".into(), self.card.url().to_string()));
  106. self.form.push(("key".into(), self.card.key().to_string()));
  107. }
  108. }
  109. impl Component for ContactManager {
  110. fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
  111. if !self.initialized {
  112. self.initialize();
  113. self.initialized = true;
  114. }
  115. clear_area(grid, area);
  116. let (width, _height) = self.content.size();
  117. copy_area(grid, &self.content, area, ((0, 0), (width - 1, 0)));
  118. let upper_left = upper_left!(area);
  119. let bottom_right = bottom_right!(area);
  120. self.form.draw(
  121. grid,
  122. (set_y(upper_left, get_y(upper_left) + 1), bottom_right),
  123. context,
  124. );
  125. context.dirty_areas.push_back(area);
  126. }
  127. fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
  128. if self.form.process_event(event, context) {
  129. match self.form.buttons_result() {
  130. None => {}
  131. Some(true) => {
  132. let mut fields = std::mem::replace(&mut self.form, FormWidget::default())
  133. .collect()
  134. .unwrap();
  135. let fields: FnvHashMap<String, String> = fields
  136. .into_iter()
  137. .map(|(s, v)| {
  138. (
  139. s,
  140. match v {
  141. Field::Text(v, _, _) | Field::TextArea(v, _) => v,
  142. Field::Choice(mut v, c) => v.remove(c),
  143. },
  144. )
  145. })
  146. .collect();
  147. let mut new_card = Card::from(fields);
  148. new_card.set_id(*self.card.id());
  149. context.accounts[self.account_pos]
  150. .address_book
  151. .add_card(new_card);
  152. context.replies.push_back(UIEvent {
  153. id: 0,
  154. event_type: UIEventType::StatusEvent(StatusEvent::DisplayMessage(
  155. "Saved.".into(),
  156. )),
  157. });
  158. context.replies.push_back(UIEvent {
  159. id: 0,
  160. event_type: UIEventType::EntityKill(self.id),
  161. });
  162. }
  163. Some(false) => {
  164. context.replies.push_back(UIEvent {
  165. id: 0,
  166. event_type: UIEventType::EntityKill(self.id),
  167. });
  168. }
  169. }
  170. return true;
  171. }
  172. /*
  173. match event.event_type {
  174. UIEventType::Input(Key::Char('\n')) => {
  175. context.replies.push_back(UIEvent {
  176. id: 0,
  177. event_type: UIEventType::EntityKill(self.id),
  178. });
  179. return true;
  180. },
  181. _ => {},
  182. }
  183. */
  184. false
  185. }
  186. fn is_dirty(&self) -> bool {
  187. self.dirty | self.form.is_dirty()
  188. }
  189. fn set_dirty(&mut self) {
  190. self.dirty = true;
  191. self.initialized = false;
  192. self.form.set_dirty();
  193. }
  194. fn set_id(&mut self, uuid: Uuid) {
  195. self.id = uuid;
  196. }
  197. }