DocumentEditorControl.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media;
  8. using InABox.Core;
  9. using Microsoft.Win32;
  10. namespace InABox.DynamicGrid
  11. {
  12. public enum DocumentAction
  13. {
  14. Replace,
  15. UseExisting,
  16. MakeCopy
  17. }
  18. public class DocumentEditorControl : DynamicEditorControl<Guid>
  19. {
  20. public delegate Document? FindDocumentEvent(string FileName);
  21. public delegate Document? GetDocumentEvent(Guid id);
  22. public delegate void OnUpdateOtherEditorHandler(string columnname, object value);
  23. public delegate void SaveDocumentEvent(Document document);
  24. private Document _document = new();
  25. private TextBox Editor;
  26. public DocumentEditorControl()
  27. {
  28. OtherColumns = new Dictionary<string, string>();
  29. }
  30. public Dictionary<string, string> OtherColumns { get; }
  31. public string Filter { get; set; }
  32. public event GetDocumentEvent? OnGetDocument;
  33. public event FindDocumentEvent? OnFindDocument;
  34. public event SaveDocumentEvent? OnSaveDocument;
  35. public event OnUpdateOtherEditorHandler? OnUpdateOtherEditor;
  36. protected override FrameworkElement CreateEditor()
  37. {
  38. var Grid = new Grid
  39. {
  40. VerticalAlignment = VerticalAlignment.Stretch,
  41. HorizontalAlignment = HorizontalAlignment.Stretch
  42. };
  43. //Grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(25, GridUnitType.Pixel) });
  44. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  45. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(50, GridUnitType.Pixel) });
  46. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(50, GridUnitType.Pixel) });
  47. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(50, GridUnitType.Pixel) });
  48. Editor = new TextBox
  49. {
  50. VerticalAlignment = VerticalAlignment.Stretch,
  51. VerticalContentAlignment = VerticalAlignment.Center,
  52. HorizontalAlignment = HorizontalAlignment.Stretch,
  53. Margin = new Thickness(0, 0, 0, 0),
  54. IsEnabled = false
  55. };
  56. //Editor.LostFocus += (o, e) => CheckChanged();
  57. Editor.SetValue(Grid.ColumnProperty, 0);
  58. Editor.SetValue(Grid.RowProperty, 0);
  59. Grid.Children.Add(Editor);
  60. var Select = new Button
  61. {
  62. VerticalAlignment = VerticalAlignment.Stretch,
  63. VerticalContentAlignment = VerticalAlignment.Center,
  64. HorizontalAlignment = HorizontalAlignment.Stretch,
  65. Margin = new Thickness(5, 1, 0, 1),
  66. Content = "Select",
  67. Focusable = false
  68. };
  69. Select.SetValue(Grid.ColumnProperty, 1);
  70. Select.SetValue(Grid.RowProperty, 0);
  71. Select.Click += Select_Click;
  72. Grid.Children.Add(Select);
  73. var Clear = new Button
  74. {
  75. VerticalAlignment = VerticalAlignment.Stretch,
  76. VerticalContentAlignment = VerticalAlignment.Center,
  77. HorizontalAlignment = HorizontalAlignment.Stretch,
  78. Margin = new Thickness(5, 1, 0, 1),
  79. Content = "Clear",
  80. Focusable = false
  81. };
  82. Clear.SetValue(Grid.ColumnProperty, 2);
  83. Clear.SetValue(Grid.RowProperty, 0);
  84. Clear.Click += Clear_Click;
  85. Grid.Children.Add(Clear);
  86. var View = new Button
  87. {
  88. VerticalAlignment = VerticalAlignment.Stretch,
  89. VerticalContentAlignment = VerticalAlignment.Center,
  90. HorizontalAlignment = HorizontalAlignment.Stretch,
  91. Margin = new Thickness(5, 1, 0, 1),
  92. Content = "View",
  93. Focusable = false
  94. };
  95. View.SetValue(Grid.ColumnProperty, 3);
  96. View.SetValue(Grid.RowProperty, 0);
  97. View.Click += View_Click;
  98. Grid.Children.Add(View);
  99. return Grid;
  100. }
  101. private void Select_Click(object sender, RoutedEventArgs e)
  102. {
  103. var dlg = new OpenFileDialog();
  104. dlg.Filter = Filter;
  105. if (dlg.ShowDialog() == true)
  106. {
  107. var filename = Path.GetFileName(dlg.FileName).ToLower();
  108. var timestamp = new FileInfo(dlg.FileName).LastWriteTime;
  109. var data = File.ReadAllBytes(dlg.FileName);
  110. var crc = CoreUtils.CalculateCRC(data);
  111. //var existing = OnFindDocument?.Invoke(filename);
  112. //if (existing != null)
  113. //{
  114. // if ((existing.TimeStamp == DateTime.MinValue || existing.TimeStamp.ToString("yyyy-MM-ddThh:mm.ss.fff")
  115. // .Equals(timestamp.ToString("yyyy-MM-ddThh:mm.ss.fff"))) && existing.CRC.Equals(crc))
  116. // {
  117. // if (existing.TimeStamp == DateTime.MinValue)
  118. // {
  119. // existing.TimeStamp = timestamp;
  120. // OnSaveDocument?.Invoke(existing);
  121. // }
  122. // _document = existing;
  123. // }
  124. // else
  125. // {
  126. // var confirm = new DocumentConfirm
  127. // {
  128. // FileName = filename,
  129. // LocalSize = data.Length,
  130. // RemoteSize = existing.Data.Length,
  131. // LocalTimeStamp = timestamp,
  132. // RemoteTimeStamp = existing.TimeStamp
  133. // };
  134. // if (confirm.ShowDialog() == true)
  135. // {
  136. // if (confirm.Result == DocumentAction.Replace)
  137. // {
  138. // existing.Data = data;
  139. // existing.TimeStamp = timestamp;
  140. // existing.CRC = crc;
  141. // OnSaveDocument?.Invoke(existing);
  142. // _document = existing;
  143. // }
  144. // else if (confirm.Result == DocumentAction.UseExisting)
  145. // {
  146. // _document = existing;
  147. // }
  148. // else if (confirm.Result == DocumentAction.MakeCopy)
  149. // {
  150. // var basefilename = Path.GetFileNameWithoutExtension(filename);
  151. // var ext = Path.GetExtension(filename);
  152. // var i = 0;
  153. // while (existing is not null)
  154. // {
  155. // i++;
  156. // filename = Path.ChangeExtension(string.Format("{0} ({1})", basefilename, i), ext);
  157. // existing = OnFindDocument?.Invoke(filename);
  158. // }
  159. // var document = new Document
  160. // {
  161. // FileName = filename,
  162. // Data = data,
  163. // TimeStamp = timestamp,
  164. // CRC = crc
  165. // };
  166. // OnSaveDocument?.Invoke(document);
  167. // _document = document;
  168. // }
  169. // }
  170. // }
  171. //}
  172. //else
  173. //{
  174. // _document = new Document
  175. // {
  176. // FileName = filename,
  177. // Data = data,
  178. // TimeStamp = timestamp,
  179. // CRC = crc
  180. // };
  181. // OnSaveDocument?.Invoke(_document);
  182. //}
  183. var newDocument = DocumentConfirm.CheckDocument(new Document
  184. {
  185. FileName = filename,
  186. TimeStamp = timestamp,
  187. Data = data,
  188. CRC = crc
  189. }, OnFindDocument, out var shouldSave);
  190. if(newDocument != null)
  191. {
  192. _document = newDocument;
  193. if (shouldSave)
  194. {
  195. OnSaveDocument?.Invoke(newDocument);
  196. }
  197. }
  198. Editor.Text = _document.FileName;
  199. if (OtherColumns.ContainsKey("FileName"))
  200. OtherValues[OtherColumns["FileName"]] = _document.FileName;
  201. CheckChanged();
  202. }
  203. }
  204. private void View_Click(object sender, RoutedEventArgs e)
  205. {
  206. if (_document.ID == Guid.Empty)
  207. {
  208. MessageBox.Show("Please select a document first!");
  209. return;
  210. }
  211. var ext = Path.GetExtension(_document.FileName);
  212. var filename = Path.ChangeExtension(Path.GetTempFileName(), ext);
  213. File.WriteAllBytes(filename, _document.Data);
  214. var gsProcessInfo = new ProcessStartInfo();
  215. gsProcessInfo.Verb = "open";
  216. gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
  217. gsProcessInfo.FileName = filename;
  218. gsProcessInfo.UseShellExecute = true;
  219. Process.Start(gsProcessInfo);
  220. }
  221. private void Clear_Click(object sender, RoutedEventArgs e)
  222. {
  223. _document = new Document();
  224. Editor.Text = "";
  225. CheckChanged();
  226. }
  227. public override int DesiredHeight()
  228. {
  229. return 25;
  230. }
  231. public override int DesiredWidth()
  232. {
  233. return int.MaxValue;
  234. }
  235. protected override Guid RetrieveValue()
  236. {
  237. return _document.ID;
  238. }
  239. protected override void UpdateValue(Guid value)
  240. {
  241. if (value != Guid.Empty)
  242. _document = OnGetDocument?.Invoke(value) ?? new Document();
  243. Editor.Text = _document.FileName;
  244. }
  245. public override void SetFocus()
  246. {
  247. Editor.Focus();
  248. }
  249. public override void SetColor(Color color)
  250. {
  251. Editor.Background = new SolidColorBrush(color);
  252. }
  253. }
  254. }