MessageWindow.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. private static readonly BitmapImage _warning = InABox.Wpf.Resources.warning.AsBitmapImage();
  172. public static BitmapImage WarningImage => _warning;
  173. private 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 NewWarn(string message, string title = "Warning", ImageSource? image = null)
  192. {
  193. return NewMessage(message, title, image);
  194. }
  195. public static void Warn(string message, string title = "Warning", ImageSource? image = null)
  196. {
  197. NewMessage(message, title, image).Display();
  198. }
  199. public static MessageWindow NewOKCancel(string message, string title, ImageSource? image = null)
  200. {
  201. return new MessageWindow()
  202. .Title(title)
  203. .Message(message)
  204. .Image(image)
  205. .AddOKButton()
  206. .AddCancelButton();
  207. }
  208. public static bool ShowOKCancel(string message, string title, ImageSource? image = null)
  209. {
  210. return NewOKCancel(message, title, image)
  211. .Display()
  212. .Result == MessageWindowResult.OK;
  213. }
  214. public static MessageWindow NewYesNo(string message, string title, ImageSource? image = null)
  215. {
  216. return new MessageWindow()
  217. .Title(title)
  218. .Message(message)
  219. .Image(image)
  220. .AddYesButton()
  221. .AddNoButton();
  222. }
  223. public static bool ShowYesNo(string message, string title, ImageSource? image = null)
  224. {
  225. return NewYesNo(message, title, image)
  226. .Display()
  227. .Result == MessageWindowResult.Yes;
  228. }
  229. public static MessageWindow NewYesNoCancel(string message, string title, ImageSource? image = null)
  230. {
  231. return new MessageWindow()
  232. .Title(title)
  233. .Message(message)
  234. .Image(image)
  235. .AddYesButton()
  236. .AddNoButton()
  237. .AddCancelButton();
  238. }
  239. public static MessageWindowResult ShowYesNoCancel(string message, string title, ImageSource? image = null)
  240. {
  241. return NewYesNoCancel(message, title, image)
  242. .Display().Result;
  243. }
  244. /// <summary>
  245. /// Display a message box for an exception, giving options to view the logs.
  246. /// </summary>
  247. /// <param name="message">The message to display. Set to <see langword="null"/> to default to the exception message.</param>
  248. /// <param name="exception"></param>
  249. /// <param name="title"></param>
  250. /// <param name="shouldLog">If <see langword="true"/>, also logs the exception.</param>
  251. public static MessageWindow NewError(string? message, Exception exception, string title = "Error", bool shouldLog = true, ImageSource? image = null)
  252. {
  253. if (shouldLog)
  254. {
  255. CoreUtils.LogException(ClientFactory.UserID, exception);
  256. }
  257. var window = new MessageWindow()
  258. .Message(message ?? exception.Message)
  259. .Title(title)
  260. .Details(CoreUtils.FormatException(exception))
  261. .Image(image ?? _warning)
  262. .AddButton(new MessageWindowButton("Email Logs", (window, button) =>
  263. {
  264. EmailLogs_Click(exception);
  265. }, MessageWindowButtonPosition.Left));
  266. var showDetailsButton = new MessageWindowButton("Show Details", (win, button) =>
  267. {
  268. win.ShowDetails = !win.ShowDetails;
  269. button.Content = win.ShowDetails
  270. ? "Hide Details"
  271. : "Show Details";
  272. }, MessageWindowButtonPosition.Left);
  273. return window.AddButton(showDetailsButton)
  274. .AddOKButton();
  275. }
  276. private static void EmailLogs_Click(Exception e)
  277. {
  278. var logFile = CoreUtils.GetVersion();
  279. const int nRead = 1024 * 1024;
  280. byte[] data;
  281. using (var stream = File.OpenRead(logFile))
  282. {
  283. if (stream.Length > nRead)
  284. {
  285. stream.Seek(-nRead, SeekOrigin.End);
  286. }
  287. data = new BinaryReader(stream).ReadBytes(Math.Min(nRead, (int)stream.Length));
  288. }
  289. var message = EmailUtils.CreateMessage(
  290. subject: "Error logs",
  291. to: "support@prsdigital.com.au",
  292. body: $"Error logs for PRS:\n\nException: {CoreUtils.FormatException(e)}");
  293. message.AddAttachment("Error Logs.txt", data);
  294. EmailUtils.OpenEmail(message);
  295. }
  296. /// <summary>
  297. /// Display a message box for a non-exception error, giving options to view the logs.
  298. /// </summary>
  299. /// <param name="message">The message to display. Set to <see langword="null"/> to default to the exception message.</param>
  300. /// <param name="details"></param>
  301. /// <param name="title"></param>
  302. /// <param name="shouldLog">If <see langword="true"/>, also logs the exception.</param>
  303. public static MessageWindow NewError(string message, string? details = null, string title = "Error", bool shouldLog = true, ImageSource? image = null)
  304. {
  305. if (shouldLog)
  306. {
  307. Logger.Send(LogType.Error, ClientFactory.UserID, details ?? message);
  308. }
  309. var window = new MessageWindow()
  310. .Message(message)
  311. .Title(title);
  312. if(details is not null)
  313. {
  314. window.Details(details);
  315. }
  316. window.Image(image ?? _warning)
  317. .AddButton(new MessageWindowButton(
  318. "Email Logs",
  319. (window, button) => EmailLogs_Click(new Exception(details ?? message)),
  320. MessageWindowButtonPosition.Left));
  321. if(details is not null)
  322. {
  323. var showDetailsButton = new MessageWindowButton("Show Details", (win, button) =>
  324. {
  325. win.ShowDetails = !win.ShowDetails;
  326. button.Content = win.ShowDetails
  327. ? "Hide Details"
  328. : "Show Details";
  329. }, MessageWindowButtonPosition.Left);
  330. window.AddButton(showDetailsButton);
  331. }
  332. return window.AddOKButton();
  333. }
  334. public static void ShowError(string? message, Exception exception, string title = "Error", bool shouldLog = true, ImageSource? image = null)
  335. {
  336. NewError(message, exception, title, shouldLog, image).Display();
  337. }
  338. public static void ShowError(string message, string details, string title = "Error", bool shouldLog = true, ImageSource? image = null)
  339. {
  340. NewError(message, details, title, shouldLog, image).Display();
  341. }
  342. private static void ShowLogs_Click(MessageWindow window, MessageWindowButton button)
  343. {
  344. var console = new MessageWindowConsole("Logs", Path.Combine(CoreUtils.GetPath(), string.Format("{0:yyyy-MM-dd}.log", DateTime.Today)));
  345. console.ShowDialog();
  346. }
  347. #endregion
  348. }
  349. public static class MessageWindowBuilder
  350. {
  351. public static MessageWindow Title(this MessageWindow window, string title)
  352. {
  353. window.Title = title;
  354. return window;
  355. }
  356. public static MessageWindow Message(this MessageWindow window, string message)
  357. {
  358. window.Message = message;
  359. return window;
  360. }
  361. public static MessageWindow Image(this MessageWindow window, ImageSource? image)
  362. {
  363. window.Image = image;
  364. return window;
  365. }
  366. public static MessageWindow Icon(this MessageWindow window, ImageSource image)
  367. {
  368. window.Icon = image;
  369. return window;
  370. }
  371. public static MessageWindow Details(this MessageWindow window, string details)
  372. {
  373. window.Details = details;
  374. return window;
  375. }
  376. public static MessageWindow Display(this MessageWindow window)
  377. {
  378. window.ShowDialog();
  379. return window;
  380. }
  381. }
  382. public class MessageWindowConsole : Console.Console
  383. {
  384. public string FileName { get; set; }
  385. public MessageWindowConsole(string description, string file) : base(description)
  386. {
  387. FileName = file;
  388. ConsoleControl.AllowLoadLogButton = false;
  389. }
  390. protected override void OnLoaded()
  391. {
  392. base.OnLoaded();
  393. if (File.Exists(FileName))
  394. {
  395. var lines = File.ReadLines(FileName);
  396. ConsoleControl.LoadLogEntries(lines);
  397. }
  398. }
  399. protected override string GetLogDirectory()
  400. {
  401. return CoreUtils.GetPath();
  402. }
  403. }