using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Imaging; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; using Syncfusion.UI.Xaml.Spreadsheet.Helpers; namespace InABox.DynamicGrid { public partial class SpreadsheetEditorControl : UserControl, IDocumentEditor { private IEntityDocument _document; private byte[] documentdata; public SpreadsheetEditorControl() { InitializeComponent(); } public IEntityDocument Document { get => _document; set { Unload(); _document = value; Load(); } } private bool DownloadNeeded(Guid id) { var cachefile = Path.Combine(CoreUtils.GetPath(), string.Format("{0}.ffd", id.ToString())); if (!File.Exists(cachefile)) return true; var indexfile = Path.Combine(CoreUtils.GetPath(), "pdfindex.json"); if (!File.Exists(indexfile)) return true; var json = File.ReadAllText(indexfile); var index = Serialization.Deserialize>(json); if (!index.ContainsKey(id)) return true; var entry = new Client().Query( new Filter(x => x.ID).IsEqualTo(id), Columns.None().Add(x => x.CRC) ).Rows.FirstOrDefault(); if (entry == null) return true; if (!entry.Get(x => x.CRC).Equals(index[id])) { MessageBox.Show("This document has been revised, and will now refresh.\n\nPlease check the drawing for any applicable changes."); return true; } return false; } private void Load() { if (_document == null) return; var cachefile = Path.Combine(CoreUtils.GetPath(), string.Format("{0}.ffd", _document.DocumentLink.ID.ToString())); using (new WaitCursor()) { if (DownloadNeeded(_document.DocumentLink.ID)) { var dbdoc = new Client().Load(new Filter(x => x.ID).IsEqualTo(_document.DocumentLink.ID)).FirstOrDefault(); if (dbdoc == null) { MessageBox.Show("Unable to Load File!"); return; } var indexfile = Path.Combine(CoreUtils.GetPath(), "pdfindex.json"); var index = new Dictionary(); if (File.Exists(indexfile)) { var json = File.ReadAllText(indexfile); index = Serialization.Deserialize>(json); } index[_document.DocumentLink.ID] = dbdoc.CRC; File.WriteAllText(indexfile, Serialization.Serialize(index)); documentdata = dbdoc.Data; File.WriteAllBytes(cachefile, documentdata); } else { documentdata = File.ReadAllBytes(cachefile); } using (var ms = new MemoryStream(documentdata)) { Editor.Open(ms); } } } private void Unload() { if (_document != null) { Editor.Commands.FileClose.Execute(Editor); _document = null; } } private void Editor_OnWorkbookLoaded(object sender, WorkbookLoadedEventArgs args) { Editor.Protect(true, true,""); foreach (var sheet in Editor.Workbook.Worksheets) Editor.ProtectSheet(sheet,""); } } }