DocumentConfirm.xaml.cs 5.3 KB

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