EmailForm.xaml.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media;
  8. using System.Windows.Threading;
  9. using InABox.Clients;
  10. using InABox.Core;
  11. using InABox.Mail;
  12. using InABox.Wpf;
  13. using InABox.WPF;
  14. using Microsoft.Win32;
  15. namespace PRSDesktop
  16. {
  17. /// <summary>
  18. /// Interaction logic for EmailForm.xaml
  19. /// </summary>
  20. public partial class EmailForm : ThemableWindow
  21. {
  22. private readonly ICoreMailMessage _message;
  23. private readonly DataModel _model;
  24. private readonly List<Tuple<string, byte[]>> attachments = new();
  25. public EmailForm(ICoreMailMessage message, DataModel model = null)
  26. {
  27. _message = message;
  28. _model = model;
  29. InitializeComponent();
  30. EmailImage.Source = PRSDesktop.Resources.email.AsBitmapImage(32, 32);
  31. Subject.Text = _message.Subject;
  32. Body.Text = _message.Body;
  33. To.Text = _message.To != null ? string.Join("; ", message.To) : "";
  34. Body.SetColor(Colors.LightYellow);
  35. if (_message.Attachments != null)
  36. attachments.AddRange(_message.Attachments);
  37. Attachments.ItemsSource = attachments;
  38. ApplyTemplate.Visibility = model != null ? Visibility.Visible : Visibility.Collapsed;
  39. var timer = new DispatcherTimer();
  40. timer.Interval = TimeSpan.FromMilliseconds(100);
  41. timer.Tick += (o, e) =>
  42. {
  43. var bEnabled = !string.IsNullOrWhiteSpace(To.Text) && !string.IsNullOrWhiteSpace(Subject.Text) &&
  44. !string.IsNullOrWhiteSpace(Body.Text);
  45. if (bEnabled != Send.IsEnabled)
  46. Send.IsEnabled = bEnabled;
  47. };
  48. timer.IsEnabled = true;
  49. }
  50. public double Zoom
  51. {
  52. get => Body.ZoomFactor;
  53. set => Body.ZoomFactor = value;
  54. }
  55. private void SendClick(object sender, RoutedEventArgs e)
  56. {
  57. _message.To = To.Text.Split(new[] { "; " }, StringSplitOptions.RemoveEmptyEntries);
  58. //_message.CC = CC.Text.Split(new String[] { "; " }, StringSplitOptions.RemoveEmptyEntries);
  59. //_message.BCC = BCC.Text.Split(new String[] { "; " }, StringSplitOptions.RemoveEmptyEntries);
  60. _message.Subject = Subject.Text;
  61. _message.Body = Body.Text;
  62. _message.Attachments = attachments.ToArray();
  63. DialogResult = true;
  64. Close();
  65. }
  66. private void AddAttachment_Click(object sender, RoutedEventArgs e)
  67. {
  68. var dlg = new OpenFileDialog();
  69. dlg.Title = "Select Attachment";
  70. dlg.Filter = "All Files (*.*)|*.*";
  71. if (dlg.ShowDialog() == true)
  72. attachments.Add(new Tuple<string, byte[]>(Path.GetFileName(dlg.FileName), File.ReadAllBytes(dlg.FileName)));
  73. Attachments.ItemsSource = null;
  74. Attachments.ItemsSource = attachments;
  75. }
  76. private void DeleteAttachment_Click(object sender, RoutedEventArgs e)
  77. {
  78. var attachment = Attachments.SelectedItem as Tuple<string, byte[]>;
  79. if (attachment != null)
  80. {
  81. attachments.Remove(attachment);
  82. Attachments.ItemsSource = null;
  83. Attachments.ItemsSource = attachments;
  84. }
  85. }
  86. private void ApplyTemplate_Click(object sender, RoutedEventArgs e)
  87. {
  88. var templates = new Client<DataModelTemplate>().Query(new Filter<DataModelTemplate>(x => x.Model).IsEqualTo(_model.Name));
  89. if (templates.Rows.Any())
  90. {
  91. var context = new ContextMenu();
  92. foreach (var row in templates.Rows)
  93. {
  94. var menu = new MenuItem
  95. {
  96. Header = row.Get<DataModelTemplate, string>(x => x.Name),
  97. Tag = row
  98. };
  99. menu.Click += (o, args) =>
  100. {
  101. var cur = ((MenuItem)o).Tag as CoreRow;
  102. var data = cur.Get<DataModelTemplate, string>(x => x.Template);
  103. try
  104. {
  105. Body.Text = DataModelUtils.ParseTemplate(_model, data);
  106. }
  107. catch (Exception err)
  108. {
  109. MessageBox.Show("Unable to Parse Template!\n\n" + err.Message);
  110. }
  111. };
  112. context.Items.Add(menu);
  113. }
  114. context.Items.Add(new Separator());
  115. var item = new MenuItem
  116. {
  117. Header = "Manage Templates",
  118. Tag = null
  119. };
  120. item.Click += (o, args) =>
  121. {
  122. var list = new DataModelTemplateForm(_model);
  123. list.ShowDialog();
  124. };
  125. context.Items.Add(item);
  126. context.PlacementTarget = ApplyTemplate;
  127. context.IsOpen = true;
  128. }
  129. else
  130. {
  131. var list = new DataModelTemplateForm(_model);
  132. list.ShowDialog();
  133. }
  134. }
  135. }
  136. }