DocumentConfirm.xaml.cs 5.3 KB

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