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.

80 lines
2.9 KiB

  1. /*
  2. * meli - notifications conf module
  3. *
  4. * Copyright 2018 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::default_vals::{internal_value_false, none, true_val};
  22. use super::DotAddressable;
  23. use melib::{MeliError, Result, ToggleFlag};
  24. /// Settings for the notifications function.
  25. #[derive(Debug, Serialize, Deserialize, Clone)]
  26. #[serde(deny_unknown_fields)]
  27. pub struct NotificationsSettings {
  28. /// Enable notifications.
  29. /// Default: True
  30. #[serde(default = "true_val")]
  31. pub enable: bool,
  32. /// A command to pipe notifications through
  33. /// Default: None
  34. #[serde(default = "none")]
  35. pub script: Option<String>,
  36. /// A file location which has its size changed when new mail arrives (max 128 bytes). Can be
  37. /// used to trigger new mail notifications eg with `xbiff(1)`
  38. /// Default: None
  39. #[serde(default = "none", alias = "xbiff-file-path")]
  40. pub xbiff_file_path: Option<String>,
  41. #[serde(default = "internal_value_false", alias = "play-sound")]
  42. pub play_sound: ToggleFlag,
  43. #[serde(default = "none", alias = "sound-file")]
  44. pub sound_file: Option<String>,
  45. }
  46. impl Default for NotificationsSettings {
  47. fn default() -> Self {
  48. Self {
  49. enable: true,
  50. script: None,
  51. xbiff_file_path: None,
  52. play_sound: ToggleFlag::InternalVal(false),
  53. sound_file: None,
  54. }
  55. }
  56. }
  57. impl DotAddressable for NotificationsSettings {
  58. fn lookup(&self, parent_field: &str, path: &[&str]) -> Result<String> {
  59. match path.first() {
  60. Some(field) => {
  61. let tail = &path[1..];
  62. match *field {
  63. "enable" => self.enable.lookup(field, tail),
  64. "script" => self.script.lookup(field, tail),
  65. "xbiff_file_path" => self.xbiff_file_path.lookup(field, tail),
  66. "play_sound" => self.play_sound.lookup(field, tail),
  67. "sound_file" => self.sound_file.lookup(field, tail),
  68. other => Err(MeliError::new(format!(
  69. "{} has no field named {}",
  70. parent_field, other
  71. ))),
  72. }
  73. }
  74. None => Ok(toml::to_string(self).map_err(|err| err.to_string())?),
  75. }
  76. }
  77. }