DocumentConfirm.xaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using InABox.Core;
  2. using InABox.Wpf;
  3. using NPOI.HPSF;
  4. using System;
  5. using System.IO;
  6. using System.Reactive;
  7. using System.Windows;
  8. namespace InABox.DynamicGrid
  9. {
  10. /// <summary>
  11. /// Interaction logic for DocumentConfirm.xaml
  12. /// </summary>
  13. public partial class DocumentConfirm : ThemableWindow
  14. {
  15. public DocumentConfirm()
  16. {
  17. InitializeComponent();
  18. }
  19. public string FileName { get; set; }
  20. public DateTime LocalTimeStamp { get; set; }
  21. public DateTime RemoteTimeStamp { get; set; }
  22. public int LocalSize { get; set; }
  23. public int RemoteSize { get; set; }
  24. public DocumentAction Result { get; set; }
  25. private void Window_Loaded(object sender, RoutedEventArgs e)
  26. {
  27. Header.Content = string.Format("A file named [{0}] already exists on the server!", FileName);
  28. NewTimeStamp.Content = string.Format("{0:dd MMM yy HH:mm:ss}", LocalTimeStamp);
  29. OldTimeStamp.Content = string.Format("{0:dd MMM yy HH:mm:ss}", RemoteTimeStamp);
  30. OldSize.Content = string.Format("{0:n0}", RemoteSize);
  31. NewSize.Content = string.Format("{0:n0}", LocalSize);
  32. Result = DocumentAction.MakeCopy;
  33. }
  34. private void ReplaceButton_Click(object sender, RoutedEventArgs e)
  35. {
  36. Result = DocumentAction.Replace;
  37. DialogResult = true;
  38. Close();
  39. }
  40. private void ExistingButton_Click(object sender, RoutedEventArgs e)
  41. {
  42. Result = DocumentAction.UseExisting;
  43. DialogResult = true;
  44. Close();
  45. }
  46. private void MakeCopyButton_Click(object sender, RoutedEventArgs e)
  47. {
  48. Result = DocumentAction.MakeCopy;
  49. DialogResult = true;
  50. Close();
  51. }
  52. private void CancelButton_Click(object sender, RoutedEventArgs e)
  53. {
  54. DialogResult = false;
  55. Close();
  56. }
  57. public static Document? CheckDocument(Document newDocument, DocumentEditorControl.FindDocumentEvent? findDocument, out bool shouldSave)
  58. {
  59. var existing = findDocument?.Invoke(newDocument.FileName);
  60. if (existing != null)
  61. {
  62. if ((existing.TimeStamp == DateTime.MinValue || existing.TimeStamp.ToString("yyyy-MM-ddThh:mm.ss.fff")
  63. .Equals(newDocument.TimeStamp.ToString("yyyy-MM-ddThh:mm.ss.fff"))) && existing.CRC.Equals(newDocument.CRC))
  64. {
  65. if (existing.TimeStamp == DateTime.MinValue)
  66. {
  67. existing.TimeStamp = newDocument.TimeStamp;
  68. shouldSave = true;
  69. }
  70. else
  71. {
  72. shouldSave = false;
  73. }
  74. return existing;
  75. }
  76. else
  77. {
  78. var confirm = new DocumentConfirm
  79. {
  80. FileName = newDocument.FileName,
  81. LocalSize = newDocument.Data.Length,
  82. RemoteSize = existing.Data.Length,
  83. LocalTimeStamp = newDocument.TimeStamp,
  84. RemoteTimeStamp = existing.TimeStamp
  85. };
  86. if (confirm.ShowDialog() == true)
  87. {
  88. if (confirm.Result == DocumentAction.Replace)
  89. {
  90. existing.Data = newDocument.Data;
  91. existing.TimeStamp = newDocument.TimeStamp;
  92. existing.CRC = newDocument.CRC;
  93. shouldSave = true;
  94. return existing;
  95. }
  96. else if (confirm.Result == DocumentAction.UseExisting)
  97. {
  98. shouldSave = false;
  99. return existing;
  100. }
  101. else if (confirm.Result == DocumentAction.MakeCopy)
  102. {
  103. var basefilename = Path.GetFileNameWithoutExtension(newDocument.FileName);
  104. var ext = Path.GetExtension(newDocument.FileName);
  105. var i = 0;
  106. while (existing is not null)
  107. {
  108. i++;
  109. newDocument.FileName = Path.ChangeExtension(string.Format("{0} ({1})", basefilename, i), ext);
  110. existing = findDocument?.Invoke(newDocument.FileName);
  111. }
  112. shouldSave = true;
  113. return newDocument;
  114. }
  115. }
  116. }
  117. }
  118. else
  119. {
  120. shouldSave = true;
  121. return newDocument;
  122. }
  123. shouldSave = false;
  124. return null;
  125. }
  126. }
  127. }