MessageWindow.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. using InABox.Clients;
  2. using InABox.Core;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.ComponentModel;
  7. using System.Diagnostics.CodeAnalysis;
  8. using System.Linq;
  9. using System.Runtime.CompilerServices;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.IO;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using InABox.WPF;
  16. namespace InABox.Wpf;
  17. public enum MessageWindowButtonPosition
  18. {
  19. Left,
  20. Right
  21. }
  22. public enum MessageWindowResult
  23. {
  24. None,
  25. OK,
  26. Cancel,
  27. Yes,
  28. No,
  29. Other
  30. }
  31. public class MessageWindowButton : INotifyPropertyChanged
  32. {
  33. public delegate void MessageWindowButtonDelegate(MessageWindow window, MessageWindowButton button);
  34. public MessageWindowButtonPosition Position { get; set; }
  35. private string _content;
  36. public string Content
  37. {
  38. get => _content;
  39. [MemberNotNull(nameof(_content))]
  40. set
  41. {
  42. _content = value;
  43. OnPropertyChanged();
  44. }
  45. }
  46. public MessageWindowButtonDelegate Action { get; set; }
  47. public MessageWindowButton(string content, MessageWindowButtonDelegate action, MessageWindowButtonPosition position)
  48. {
  49. Content = content;
  50. Action = action;
  51. Position = position;
  52. }
  53. public event PropertyChangedEventHandler? PropertyChanged;
  54. protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
  55. {
  56. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  57. }
  58. }
  59. /// <summary>
  60. /// Interaction logic for MessageWindow.xaml
  61. /// </summary>
  62. public partial class MessageWindow : Window, INotifyPropertyChanged
  63. {
  64. public ObservableCollection<MessageWindowButton> Buttons { get; private set; } = new();
  65. public IEnumerable<MessageWindowButton> LeftButtons => Buttons.Where(x => x.Position == MessageWindowButtonPosition.Left);
  66. public IEnumerable<MessageWindowButton> RightButtons => Buttons.Where(x => x.Position == MessageWindowButtonPosition.Right);
  67. private string _message = "";
  68. public string Message
  69. {
  70. get => _message;
  71. set
  72. {
  73. _message = value;
  74. OnPropertyChanged();
  75. }
  76. }
  77. public ImageSource? _image = null;
  78. public ImageSource? Image
  79. {
  80. get => _image;
  81. set
  82. {
  83. _image = value;
  84. OnPropertyChanged();
  85. }
  86. }
  87. private string _details = "";
  88. public string Details
  89. {
  90. get => _details;
  91. set
  92. {
  93. _details = value;
  94. OnPropertyChanged();
  95. }
  96. }
  97. public static readonly DependencyProperty ShowDetailsProperty = DependencyProperty.Register(nameof(ShowDetails), typeof(bool), typeof(MessageWindow));
  98. public bool ShowDetails
  99. {
  100. get => (bool)GetValue(ShowDetailsProperty);
  101. set => SetValue(ShowDetailsProperty, value);
  102. }
  103. public MessageWindowResult Result { get; set; } = MessageWindowResult.None;
  104. public object? OtherResult { get; set; }
  105. public MessageWindow()
  106. {
  107. InitializeComponent();
  108. Buttons.CollectionChanged += Buttons_CollectionChanged;
  109. }
  110. private void Button_Click(object sender, RoutedEventArgs e)
  111. {
  112. if (sender is not Button button || button.Tag is not MessageWindowButton winButton) return;
  113. winButton.Action(this, winButton);
  114. }
  115. private void Buttons_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  116. {
  117. OnPropertyChanged(nameof(LeftButtons));
  118. OnPropertyChanged(nameof(RightButtons));
  119. }
  120. public MessageWindow AddButton(MessageWindowButton button)
  121. {
  122. Buttons.Add(button);
  123. return this;
  124. }
  125. public MessageWindow AddOKButton(string content = "OK")
  126. {
  127. Buttons.Add(new MessageWindowButton(content, OKButton_Click, MessageWindowButtonPosition.Right));
  128. return this;
  129. }
  130. public MessageWindow AddCancelButton(string content = "Cancel")
  131. {
  132. Buttons.Add(new MessageWindowButton(content, CancelButton_Click, MessageWindowButtonPosition.Right));
  133. return this;
  134. }
  135. public MessageWindow AddYesButton(string content = "Yes")
  136. {
  137. Buttons.Add(new MessageWindowButton(content, YesButton_Click, MessageWindowButtonPosition.Right));
  138. return this;
  139. }
  140. public MessageWindow AddNoButton(string content = "No")
  141. {
  142. Buttons.Add(new MessageWindowButton(content, NoButton_Click, MessageWindowButtonPosition.Right));
  143. return this;
  144. }
  145. private void YesButton_Click(MessageWindow window, MessageWindowButton button)
  146. {
  147. Result = MessageWindowResult.Yes;
  148. Close();
  149. }
  150. private void NoButton_Click(MessageWindow window, MessageWindowButton button)
  151. {
  152. Result = MessageWindowResult.No;
  153. Close();
  154. }
  155. private void CancelButton_Click(MessageWindow window, MessageWindowButton button)
  156. {
  157. Result = MessageWindowResult.Cancel;
  158. Close();
  159. }
  160. private void OKButton_Click(MessageWindow window, MessageWindowButton button)
  161. {
  162. Result = MessageWindowResult.OK;
  163. Close();
  164. }
  165. public event PropertyChangedEventHandler? PropertyChanged;
  166. protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
  167. {
  168. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  169. }
  170. #region Static Constructors
  171. public static readonly BitmapImage _warning = InABox.Wpf.Resources.warning.AsBitmapImage();
  172. public static BitmapImage WarningImage => _warning;
  173. public static readonly BitmapImage _question = InABox.Wpf.Resources.help.AsBitmapImage();
  174. public static BitmapImage QuestionImage => _question;
  175. public static MessageWindow New()
  176. {
  177. return new MessageWindow();
  178. }
  179. public static MessageWindow NewMessage(string message, string title, ImageSource? image = null)
  180. {
  181. return new MessageWindow()
  182. .Title(title)
  183. .Message(message)
  184. .Image(image)
  185. .AddOKButton();
  186. }
  187. public static void ShowMessage(string message, string title, ImageSource? image = null)
  188. {
  189. NewMessage(message, title, image).Display();
  190. }
  191. public static MessageWindow NewOKCancel(string message, string title, ImageSource? image = null)
  192. {
  193. return new MessageWindow()
  194. .Title(title)
  195. .Message(message)
  196. .Image(image)
  197. .AddOKButton()
  198. .AddCancelButton();
  199. }
  200. public static bool ShowOKCancel(string message, string title, ImageSource? image = null)
  201. {
  202. return NewOKCancel(message, title, image)
  203. .Display()
  204. .Result == MessageWindowResult.OK;
  205. }
  206. public static MessageWindow NewYesNo(string message, string title, ImageSource? image = null)
  207. {
  208. return new MessageWindow()
  209. .Title(title)
  210. .Message(message)
  211. .Image(image)
  212. .AddYesButton()
  213. .AddNoButton();
  214. }
  215. public static bool ShowYesNo(string message, string title, ImageSource? image = null)
  216. {
  217. return NewYesNo(message, title, image)
  218. .Display()
  219. .Result == MessageWindowResult.Yes;
  220. }
  221. public static MessageWindow NewYesNoCancel(string message, string title, ImageSource? image = null)
  222. {
  223. return new MessageWindow()
  224. .Title(title)
  225. .Message(message)
  226. .Image(image)
  227. .AddYesButton()
  228. .AddNoButton()
  229. .AddCancelButton();
  230. }
  231. public static MessageWindowResult ShowYesNoCancel(string message, string title, ImageSource? image = null)
  232. {
  233. return NewYesNoCancel(message, title, image)
  234. .Display().Result;
  235. }
  236. /// <summary>
  237. /// Display a message box for an exception, giving options to view the logs.
  238. /// </summary>
  239. /// <param name="message">The message to display. Set to <see langword="null"/> to default to the exception message.</param>
  240. /// <param name="exception"></param>
  241. /// <param name="title"></param>
  242. /// <param name="shouldLog">If <see langword="true"/>, also logs the exception.</param>
  243. public static MessageWindow NewError(string? message, Exception exception, string title = "Error", bool shouldLog = true, ImageSource? image = null)
  244. {
  245. if (shouldLog)
  246. {
  247. CoreUtils.LogException(ClientFactory.UserID, exception);
  248. }
  249. var window = new MessageWindow()
  250. .Message(message ?? exception.Message)
  251. .Title(title)
  252. .Details(CoreUtils.FormatException(exception))
  253. .Image(image ?? _warning)
  254. .AddButton(new MessageWindowButton("Email Logs", (window, button) =>
  255. {
  256. EmailLogs_Click(exception);
  257. }, MessageWindowButtonPosition.Left));
  258. var showDetailsButton = new MessageWindowButton("Show Details", (win, button) =>
  259. {
  260. win.ShowDetails = !win.ShowDetails;
  261. button.Content = win.ShowDetails
  262. ? "Hide Details"
  263. : "Show Details";
  264. }, MessageWindowButtonPosition.Left);
  265. return window.AddButton(showDetailsButton)
  266. .AddOKButton();
  267. }
  268. private static void EmailLogs_Click(Exception e)
  269. {
  270. var logFile = Path.Combine(CoreUtils.GetPath(), string.Format("{0:yyyy-MM-dd}.log", DateTime.Today));
  271. const int nRead = 1024 * 1024;
  272. byte[] data;
  273. using (var stream = File.OpenRead(logFile))
  274. {
  275. if (stream.Length > nRead)
  276. {
  277. stream.Seek(-nRead, SeekOrigin.End);
  278. }
  279. data = new BinaryReader(stream).ReadBytes(Math.Min(nRead, (int)stream.Length));
  280. }
  281. var message = EmailUtils.CreateMessage(
  282. subject: "Error logs",
  283. to: "support@prsdigital.com.au",
  284. body: $"Error logs for PRS:\n\nException: {CoreUtils.FormatException(e)}");
  285. message.AddAttachment("Error Logs.txt", data);
  286. EmailUtils.OpenEmail(message);
  287. }
  288. /// <summary>
  289. /// Display a message box for a non-exception error, giving options to view the logs.
  290. /// </summary>
  291. /// <param name="message">The message to display. Set to <see langword="null"/> to default to the exception message.</param>
  292. /// <param name="details"></param>
  293. /// <param name="title"></param>
  294. /// <param name="shouldLog">If <see langword="true"/>, also logs the exception.</param>
  295. public static MessageWindow NewError(string message, string? details = null, string title = "Error", bool shouldLog = true, ImageSource? image = null)
  296. {
  297. if (shouldLog)
  298. {
  299. Logger.Send(LogType.Error, ClientFactory.UserID, details ?? message);
  300. }
  301. var window = new MessageWindow()
  302. .Message(message)
  303. .Title(title);
  304. if(details is not null)
  305. {
  306. window.Details(details);
  307. }
  308. window.Image(image ?? _warning)
  309. .AddButton(new MessageWindowButton(
  310. "Email Logs",
  311. (window, button) => EmailLogs_Click(new Exception(details ?? message)),
  312. MessageWindowButtonPosition.Left));
  313. if(details is not null)
  314. {
  315. var showDetailsButton = new MessageWindowButton("Show Details", (win, button) =>
  316. {
  317. win.ShowDetails = !win.ShowDetails;
  318. button.Content = win.ShowDetails
  319. ? "Hide Details"
  320. : "Show Details";
  321. }, MessageWindowButtonPosition.Left);
  322. window.AddButton(showDetailsButton);
  323. }
  324. return window.AddOKButton();
  325. }
  326. public static void ShowError(string? message, Exception exception, string title = "Error", bool shouldLog = true, ImageSource? image = null)
  327. {
  328. NewError(message, exception, title, shouldLog, image).Display();
  329. }
  330. public static void ShowError(string message, string details, string title = "Error", bool shouldLog = true, ImageSource? image = null)
  331. {
  332. NewError(message, details, title, shouldLog, image).Display();
  333. }
  334. private static void ShowLogs_Click(MessageWindow window, MessageWindowButton button)
  335. {
  336. var console = new MessageWindowConsole("Logs", Path.Combine(CoreUtils.GetPath(), string.Format("{0:yyyy-MM-dd}.log", DateTime.Today)));
  337. console.ShowDialog();
  338. }
  339. #endregion
  340. }
  341. public static class MessageWindowBuilder
  342. {
  343. public static MessageWindow Title(this MessageWindow window, string title)
  344. {
  345. window.Title = title;
  346. return window;
  347. }
  348. public static MessageWindow Message(this MessageWindow window, string message)
  349. {
  350. window.Message = message;
  351. return window;
  352. }
  353. public static MessageWindow Image(this MessageWindow window, ImageSource? image)
  354. {
  355. window.Image = image;
  356. return window;
  357. }
  358. public static MessageWindow Icon(this MessageWindow window, ImageSource image)
  359. {
  360. window.Icon = image;
  361. return window;
  362. }
  363. public static MessageWindow Details(this MessageWindow window, string details)
  364. {
  365. window.Details = details;
  366. return window;
  367. }
  368. public static MessageWindow Display(this MessageWindow window)
  369. {
  370. window.ShowDialog();
  371. return window;
  372. }
  373. }
  374. public class MessageWindowConsole : Console.Console
  375. {
  376. public string FileName { get; set; }
  377. public MessageWindowConsole(string description, string file) : base(description)
  378. {
  379. FileName = file;
  380. ConsoleControl.AllowLoadLogButton = false;
  381. }
  382. protected override void OnLoaded()
  383. {
  384. base.OnLoaded();
  385. if (File.Exists(FileName))
  386. {
  387. var lines = File.ReadLines(FileName);
  388. ConsoleControl.LoadLogEntries(lines);
  389. }
  390. }
  391. protected override string GetLogDirectory()
  392. {
  393. return CoreUtils.GetPath();
  394. }
  395. }