SpreadsheetEditorControl.xaml.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media.Imaging;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. using InABox.DynamicGrid;
  11. using InABox.WPF;
  12. using Syncfusion.UI.Xaml.Spreadsheet.Helpers;
  13. namespace InABox.DynamicGrid
  14. {
  15. public partial class SpreadsheetEditorControl : UserControl, IDocumentEditor
  16. {
  17. private IEntityDocument _document;
  18. private byte[] documentdata;
  19. public SpreadsheetEditorControl()
  20. {
  21. InitializeComponent();
  22. }
  23. public IEntityDocument Document
  24. {
  25. get => _document;
  26. set
  27. {
  28. Unload();
  29. _document = value;
  30. Load();
  31. }
  32. }
  33. private bool DownloadNeeded(Guid id)
  34. {
  35. var cachefile = Path.Combine(CoreUtils.GetPath(), string.Format("{0}.ffd", id.ToString()));
  36. if (!File.Exists(cachefile))
  37. return true;
  38. var indexfile = Path.Combine(CoreUtils.GetPath(), "pdfindex.json");
  39. if (!File.Exists(indexfile))
  40. return true;
  41. var json = File.ReadAllText(indexfile);
  42. var index = Serialization.Deserialize<Dictionary<Guid, string>>(json);
  43. if (!index.ContainsKey(id))
  44. return true;
  45. var entry = new Client<Document>().Query(
  46. new Filter<Document>(x => x.ID).IsEqualTo(id),
  47. Columns.None<Document>().Add(x => x.CRC)
  48. ).Rows.FirstOrDefault();
  49. if (entry == null)
  50. return true;
  51. if (!entry.Get<Document, string>(x => x.CRC).Equals(index[id]))
  52. {
  53. MessageBox.Show("This document has been revised, and will now refresh.\n\nPlease check the drawing for any applicable changes.");
  54. return true;
  55. }
  56. return false;
  57. }
  58. private void Load()
  59. {
  60. if (_document == null)
  61. return;
  62. var cachefile = Path.Combine(CoreUtils.GetPath(), string.Format("{0}.ffd", _document.DocumentLink.ID.ToString()));
  63. using (new WaitCursor())
  64. {
  65. if (DownloadNeeded(_document.DocumentLink.ID))
  66. {
  67. var dbdoc = new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(_document.DocumentLink.ID)).FirstOrDefault();
  68. if (dbdoc == null)
  69. {
  70. MessageBox.Show("Unable to Load File!");
  71. return;
  72. }
  73. var indexfile = Path.Combine(CoreUtils.GetPath(), "pdfindex.json");
  74. var index = new Dictionary<Guid, string>();
  75. if (File.Exists(indexfile))
  76. {
  77. var json = File.ReadAllText(indexfile);
  78. index = Serialization.Deserialize<Dictionary<Guid, string>>(json);
  79. }
  80. index[_document.DocumentLink.ID] = dbdoc.CRC;
  81. File.WriteAllText(indexfile, Serialization.Serialize(index));
  82. documentdata = dbdoc.Data;
  83. File.WriteAllBytes(cachefile, documentdata);
  84. }
  85. else
  86. {
  87. documentdata = File.ReadAllBytes(cachefile);
  88. }
  89. using (var ms = new MemoryStream(documentdata))
  90. {
  91. Editor.Open(ms);
  92. }
  93. }
  94. }
  95. private void Unload()
  96. {
  97. if (_document != null)
  98. {
  99. Editor.Commands.FileClose.Execute(Editor);
  100. _document = null;
  101. }
  102. }
  103. private void Editor_OnWorkbookLoaded(object sender, WorkbookLoadedEventArgs args)
  104. {
  105. Editor.Protect(true, true,"");
  106. foreach (var sheet in Editor.Workbook.Worksheets)
  107. Editor.ProtectSheet(sheet,"");
  108. }
  109. }
  110. }