MessageWindow.xaml.cs 15 KB

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