private bool NeedForbidSaving(IXApplication app, IXDocument doc, OpenFileOptions_e opts) { if (opts.HasFlag(OpenFileOptions_e.ForbidUpgrade)) { try { if (TextUtils.MatchesAnyFilter(doc.Path, m_AppProvider.Descriptor.PartFileFilter.Extensions) || TextUtils.MatchesAnyFilter(doc.Path, m_AppProvider.Descriptor.AssemblyFileFilter.Extensions) || TextUtils.MatchesAnyFilter(doc.Path, m_AppProvider.Descriptor.DrawingFileFilter.Extensions)) { if (app.Version.Compare(doc.Version) == VersionEquality_e.Newer) { return(true); } } } catch (Exception ex) { throw new UserException("Failed to extract version of the file. This can indicate that the file is corrupted", ex); } } return(false); }
private IXDocument EnsureDocument(IXApplication app, IXDocument templateDoc, OpenFileOptions_e opts, CancellationToken cancellationToken, out bool forbidSaving) { IXDocument doc; if (templateDoc.IsCommitted && templateDoc.IsAlive) { doc = templateDoc; } else { doc = app.Documents.FirstOrDefault(d => string.Equals(d.Path, templateDoc.Path)); if (doc == null) { doc = app.Documents.PreCreate <IXDocument>(); doc.Path = templateDoc.Path; } } if (!doc.IsCommitted) { var state = DocumentState_e.Default; if (opts.HasFlag(OpenFileOptions_e.Silent)) { state |= DocumentState_e.Silent; } if (opts.HasFlag(OpenFileOptions_e.ReadOnly)) { state |= DocumentState_e.ReadOnly; } if (opts.HasFlag(OpenFileOptions_e.Rapid)) { state |= DocumentState_e.Rapid; } if (opts.HasFlag(OpenFileOptions_e.Invisible)) { state |= DocumentState_e.Hidden; } forbidSaving = NeedForbidSaving(app, doc, opts); if (forbidSaving) { if (!state.HasFlag(DocumentState_e.ReadOnly)) { m_JournalWriter.WriteLine($"Setting the readonly flag to {doc.Path} to prevent upgrade of the file"); state |= DocumentState_e.ReadOnly; } } doc.State = state; m_JournalWriter.WriteLine($"Opening '{doc.Path}'"); doc.Commit(cancellationToken); } else { forbidSaving = NeedForbidSaving(app, doc, opts); if (forbidSaving && !doc.State.HasFlag(DocumentState_e.ReadOnly)) { TryCloseDocument(doc); throw new UserException("Document is opened with write access, but saving is forbidden"); } } return(doc); }