RichTextEditorControl.xaml.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 InABox.Clients;
  8. using InABox.Core;
  9. using InABox.DynamicGrid;
  10. using InABox.WPF;
  11. using Syncfusion.UI.Xaml.Spreadsheet.Helpers;
  12. using Syncfusion.Windows.Controls.RichTextBoxAdv;
  13. namespace InABox.DynamicGrid
  14. {
  15. public partial class RTFEditorControl : UserControl, IDocumentEditor
  16. {
  17. private IEntityDocument? _document;
  18. public RTFEditorControl()
  19. {
  20. InitializeComponent();
  21. }
  22. public IEntityDocument? Document
  23. {
  24. get => _document;
  25. set
  26. {
  27. Unload();
  28. _document = value;
  29. Load();
  30. }
  31. }
  32. private bool DownloadNeeded(Guid id)
  33. {
  34. var cachefile = Path.Combine(CoreUtils.GetPath(), string.Format("{0}.ffd", id.ToString()));
  35. if (!File.Exists(cachefile))
  36. return true;
  37. var indexfile = Path.Combine(CoreUtils.GetPath(), "pdfindex.json");
  38. if (!File.Exists(indexfile))
  39. return true;
  40. var json = File.ReadAllText(indexfile);
  41. var index = Serialization.Deserialize<Dictionary<Guid, string>>(json);
  42. if (!index.ContainsKey(id))
  43. return true;
  44. var entry = new Client<Document>().Query(
  45. new Filter<Document>(x => x.ID).IsEqualTo(id),
  46. Columns.None<Document>().Add(x => x.CRC)
  47. ).Rows.FirstOrDefault();
  48. if (entry == null)
  49. return true;
  50. if (!entry.Get<Document, string>(x => x.CRC).Equals(index[id]))
  51. {
  52. MessageBox.Show("This document has been revised, and will now refresh.\n\nPlease check the drawing for any applicable changes.");
  53. return true;
  54. }
  55. return false;
  56. }
  57. private void Load()
  58. {
  59. if (_document == null)
  60. return;
  61. var cachefile = Path.Combine(CoreUtils.GetPath(), string.Format("{0}.ffd", _document.DocumentLink.ID.ToString()));
  62. using (new WaitCursor())
  63. {
  64. byte[] documentdata;
  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. var format = Path.GetExtension(_document.DocumentLink.FileName).ToLower() switch
  92. {
  93. "docx" => FormatType.Docx,
  94. "doc" => FormatType.Doc,
  95. "html" => FormatType.Html,
  96. "htm" => FormatType.Html,
  97. "rtf" => FormatType.Rtf,
  98. _ => FormatType.Txt
  99. };
  100. Editor.Load(ms, format);
  101. }
  102. }
  103. }
  104. private void Unload()
  105. {
  106. if (_document != null)
  107. {
  108. Editor.Document = null;
  109. _document = null;
  110. }
  111. }
  112. private void Editor_OnWorkbookLoaded(object sender, WorkbookLoadedEventArgs args)
  113. {
  114. Editor.IsReadOnly = true;
  115. }
  116. }
  117. }