Index: Excel/ExcelLibraryTesting/ExcelLibraryTesting.csproj =================================================================== --- Excel/ExcelLibraryTesting/ExcelLibraryTesting.csproj (revision 3367) +++ Excel/ExcelLibraryTesting/ExcelLibraryTesting.csproj (working copy) @@ -59,6 +59,14 @@ primary False + + {0002E157-0000-0000-C000-000000000046} + 5 + 3 + 0 + primary + False + Index: Excel/ExcelLibrary/AddIn/ExcelAddInHelper.cs =================================================================== --- Excel/ExcelLibrary/AddIn/ExcelAddInHelper.cs (revision 3367) +++ Excel/ExcelLibrary/AddIn/ExcelAddInHelper.cs (working copy) @@ -202,7 +202,7 @@ { - DocumentExcelMS doc = new DocumentExcelMS(workbook); + DocumentOfficeMS doc = new DocumentExcelMS(workbook); if (!IsUserDoc(doc)) @@ -214,7 +214,7 @@ LogAddIn.Logger.Trace("workbook: [" + doc.FullName + "] opened"); - OnDocumentOpen(doc); + OnDocumentOpen(ref doc); } @@ -244,16 +244,12 @@ m_AutoCreatedWindows.Clear(); - using (DocumentExcelMS docExcel = new DocumentExcelMS(workbook)) + DocumentOfficeMS docExcel = new DocumentExcelMS(workbook); - { + LogAddIn.Logger.Trace("workbook closing: [" + docExcel.FullName + "]"); - LogAddIn.Logger.Trace("workbook closing: [" + docExcel.FullName + "]"); + base.OnDocumentBeforeClose(ref docExcel, ref cancel); - base.OnDocumentBeforeClose(docExcel, ref cancel); - - } - } catch (Exception e) @@ -280,11 +276,11 @@ { - DocumentExcelMS docExcel = new DocumentExcelMS(workbook); + DocumentOfficeMS docExcel = new DocumentExcelMS(workbook); LogAddIn.Logger.Trace("Before Save, workbook: [" + docExcel.FullName + "]"); - base.OnDocumentBeforeSave(docExcel, saveAsUI, ref cancel); + base.OnDocumentBeforeSave(ref docExcel, saveAsUI, ref cancel); } @@ -450,7 +446,7 @@ { - DocumentExcelMS docExcel = (DocumentExcelMS)m_app.GetActiveDocument(); + DocumentOfficeMS docExcel = (DocumentExcelMS)m_app.GetActiveDocument(); if (User32.WA_ACTIVE != cwp.wParam.ToInt32() || docExcel.GetWindow().Handle.ToString() != cwp.hwnd.ToString()) @@ -474,7 +470,7 @@ item.SecondValue = true; - base.SendEditEvent(docExcel, true, EventType.Open); + base.SendEditEvent(ref docExcel, true, EventType.Open); return; Index: Excel/ExcelLibrary/Wrappers/DocumentExcelMS.cs =================================================================== --- Excel/ExcelLibrary/Wrappers/DocumentExcelMS.cs (revision 3367) +++ Excel/ExcelLibrary/Wrappers/DocumentExcelMS.cs (working copy) @@ -390,6 +390,13 @@ return AgentUtils.Windowing.Win32Window.GetWindow(applicationWindow); } + public override void SaveAs(string path) + { + DocAsExcelWorkbook.SaveAs(path, + m_MissingValue, m_MissingValue, m_MissingValue, m_MissingValue, m_MissingValue, + Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, m_MissingValue, m_MissingValue, m_MissingValue, m_MissingValue, m_MissingValue); + } + /// /// extract iprofs from document /// Index: Excel/ExcelLibrary/Wrappers/ApplicationExcelMS.cs =================================================================== --- Excel/ExcelLibrary/Wrappers/ApplicationExcelMS.cs (revision 3367) +++ Excel/ExcelLibrary/Wrappers/ApplicationExcelMS.cs (working copy) @@ -121,6 +121,7 @@ } } + //roeeimpl - XXX - here and in word - open without adding to recent docuemnts public override Document OpenDocument(string path, bool readOnly, bool visible) { if (!visible) Index: Excel/ExcelLibrary/ExcelLibrary.csproj =================================================================== --- Excel/ExcelLibrary/ExcelLibrary.csproj (revision 3367) +++ Excel/ExcelLibrary/ExcelLibrary.csproj (working copy) @@ -38,6 +38,7 @@ + @@ -67,6 +68,14 @@ primary False + + {0002E157-0000-0000-C000-000000000046} + 5 + 3 + 0 + primary + False + @@ -85,6 +94,10 @@ {98828D1C-7708-4C17-98BA-1AC2E890BF5B} EventsHandling + + {0E57A903-3064-43F4-9FF0-59618E59B3C5} + SI.RMS.Task + {BCDFD4B0-7535-4C6B-B5F6-A01E00F79631} ClipboardHelper Index: Excel/ExcelLibrary/EH/EHOfficeDocEditExcel.cs =================================================================== --- Excel/ExcelLibrary/EH/EHOfficeDocEditExcel.cs (revision 0) +++ Excel/ExcelLibrary/EH/EHOfficeDocEditExcel.cs (revision 3534) @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Text; +using AgentUtils; +using ExcelLibrary.Wrappers; +using ApplicationControl; +using OfficeLibrary.Events; + +namespace ExcelLibrary.EH +{ + public class EHOfficeDocEditExcel : OfficeLibrary.EH.EHOfficeDocEdit, CommonUtils.IFactoryObject + { + #region Constructors + + public EHOfficeDocEditExcel() + : base() + { + } + + public EHOfficeDocEditExcel(SIEventOfficeDocEdit ev) + : base(ev) + { + } + + #endregion + + protected override void ApplyRMSAsSI(string sRMSPolic) + { + string path = Doc.Path; + string name = Doc.Name; + //Re-open as read-only, this will release the lock... + bool eventsOnDoc = m_Event.AddInApi.SetIsDocEventsDisabled(name, true); + try + { + Doc = (DocumentExcelMS)Doc.Application.OpenDocument(new DataItemFile(path), ApplicationControl.AbstractTypes.AccessMode.View); + try + { + using (SITmpFile tmpCopy = new SITmpFile()) + { + tmpCopy.CreateUniqueName(Doc.Name); + Doc.SaveAs(tmpCopy.FileName); + using (DocumentExcelSI siDoc = new DocumentExcelSI( + new DataItemFile(path), + ApplicationControl.AbstractTypes.AccessMode.Edit)) + { + siDoc.ApplyRMS(sRMSPolic, SI.RMS.Task.LicenseUtils.GetMasterLicenses()); + } + } + } + finally + { + //XXX roeeimpl - handle no "AllowMacros", where we don't get back the document we opened. + //Re-Open as read-write. + Doc = (DocumentExcelMS)Doc.Application.OpenDocument(new DataItemFile(path), ApplicationControl.AbstractTypes.AccessMode.View); + } + } + finally + { + m_Event.AddInApi.SetIsDocEventsDisabled(name, eventsOnDoc); + } + } + + #region IFactoryObject + + public virtual bool IsMine(object createParams) + { + return (createParams is SIEventOfficeDocEdit) && ((SIEventOfficeDocEdit)createParams).AppType == ApplicationControl.ApplicationType.MSEXCEL; + } + + protected virtual EventsHandling.EnforcementHandlers.EH Create(SIEventOfficeDocEdit eventInfo) + { + return new EHOfficeDocEditExcel(eventInfo); + } + + public virtual EventsHandling.EnforcementHandlers.EH Create(object createParams) + { + return Create((SIEventOfficeDocEdit)createParams); + } + + #endregion + } +} Index: OfficeCommon/OfficeLibrary/AddIn/OfficeAddIn.cs =================================================================== --- OfficeCommon/OfficeLibrary/AddIn/OfficeAddIn.cs (revision 3367) +++ OfficeCommon/OfficeLibrary/AddIn/OfficeAddIn.cs (working copy) @@ -63,8 +63,9 @@ /// /// variables /// - // XXX might be better to replace dictionary to save string (fullname) instead of COM objects - protected Dictionary m_DocumentsInProcess = new Dictionary(); + + //String is document name, value is ignored. + protected Dictionary m_DocsWithEventsDisabled = new Dictionary(StringComparer.InvariantCultureIgnoreCase); /// /// Manual classification form @@ -76,7 +77,11 @@ /// protected ApplicationOfficeMS m_app = null; - protected AddInExtApi m_AddInApi = null; + private AddInExtApi m_AddInApi = null; + protected AddInExtApi AddInApi + { + get { return m_AddInApi; } + } private readonly ActiveDocData m_ActiveDocData = new ActiveDocData(); @@ -112,6 +117,7 @@ { get; } + /// /// Is this a user operation or our automatation operation. /// @@ -141,7 +147,68 @@ } + protected bool GetIsDocEventsDisabled(string docName) + { + lock (m_DocsWithEventsDisabled) + { + bool ret = m_DocsWithEventsDisabled.ContainsKey(docName); + return ret; + } + } + + public bool GetIsDocEventsDisabled(DocumentOfficeMS doc) + { + return GetIsDocEventsDisabled(doc.FullName); + } + /// + /// + /// + /// + /// If events for document are disabled. + public void CheckIsDocEventsDisabled(string docName) + { + if (GetIsDocEventsDisabled(docName)) + throw new SIExceptionEventsDisabled("events for doc: [" + docName + "] are disabled"); + } + + /// + /// + /// + /// + /// If events for document are disabled. + protected void CheckIsDocEventsDisabled(DocumentOfficeMS doc) + { + CheckIsDocEventsDisabled(doc.FullName); + } + + /// + /// + /// + /// + /// + /// The old value of enabled + public bool SetIsDocEventsDisabled(string name, bool disabled) + { + bool ret = GetIsDocEventsDisabled(name); + + lock (m_DocsWithEventsDisabled) + { + if (disabled) + { + m_DocsWithEventsDisabled[name] = name; + return ret; + } + if (m_DocsWithEventsDisabled.ContainsKey(name)) + { + m_DocsWithEventsDisabled.Remove(name); + } + } + + return ret; + } + + /// /// Is this a user document or an automation operation? /// Basically we don't currently work on automation. /// The main reason is that we span dialogs, which is problemtic on automation. @@ -170,7 +237,10 @@ protected static void ReportException(DocumentOfficeMS doc, Exception e) { - NLog.LogLevel level = e is System.Runtime.Remoting.RemotingException ? NLog.LogLevel.Warn : NLog.LogLevel.Error; + NLog.LogLevel level = + e is SIExceptionEventsDisabled ? NLog.LogLevel.Debug : + e is System.Runtime.Remoting.RemotingException ? NLog.LogLevel.Warn : + NLog.LogLevel.Error; ReportException(doc, e, level); } @@ -199,7 +269,7 @@ #region DialogSaveChanges protected virtual DialogResult DialogSaveChanges(DocumentOfficeMS doc) { - return MessageBox.Show(doc.GetWindow(), "Do you want to save changes in " + doc.Name + "?", DialogsTitle(), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning); + return MessageBox.Show(doc.GetWindow(), "Do you want to save changes in " + doc.FullName + "?", DialogsTitle(), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning); } #endregion @@ -220,7 +290,7 @@ #region OnDocumentOpen - public void OnDocumentOpen(DocumentOfficeMS doc) + public void OnDocumentOpen(ref DocumentOfficeMS doc) { try { @@ -233,7 +303,7 @@ return; } - SendEditEvent(doc, true, EventType.Open); + SendEditEvent(ref doc, true, EventType.Open); if (doc.ReadOnly) { @@ -248,30 +318,15 @@ } #endregion - #region SaveDocumentWithoutEvents - protected void SaveDocumentWithoutEvents(DocumentOfficeMS doc) - { - m_DocumentsInProcess[doc.NativeDoc] = doc.NativeDoc; - try - { - doc.Save(); - } - finally - { - m_DocumentsInProcess.Remove(doc.NativeDoc); - } - } - #endregion - #region OnDocumentBeforeClose - protected void OnDocumentBeforeClose(DocumentOfficeMS doc, ref bool cancel) + protected void OnDocumentBeforeClose(ref DocumentOfficeMS doc, ref bool cancel) { cancel = false; if (!IsUserDoc(doc)) return; - SIEventOfficeDocEdit ev = SendEditEvent(doc, false, EventType.Close); + SIEventOfficeDocEdit ev = SendEditEvent(ref doc, false, EventType.Close); ActiveDocDataChanged(doc); if (!doc.Saved) @@ -302,10 +357,13 @@ { try { + //rooeimpl //Here we would rather just let the save event handle it, //But for some reason the save-as dialog in excel doesn't work on nested events. //So save the document (and mark we're saving it to prevent handling in save event). - SaveDocumentWithoutEvents(doc); //needed only to propt save-as UI if document weren't save, so we'll have location. + bool events = SetIsDocEventsDisabled(doc.FullName, false); + Utilities.Utils.SaveDocumentWithoutEvents(doc, AddInApi); + } catch (CommonUtils.Exceptions.SIException e) //happened when user cancle { @@ -341,7 +399,7 @@ return; } } - SaveDocumentWithoutEvents(doc); + Utilities.Utils.SaveDocumentWithoutEvents(doc, AddInApi); ActiveDocDataChanged(null); // remove classification image for closed document @@ -502,7 +560,7 @@ if (!classificationAdded) return; - SendEditEvent(doc, true, EventType.Paste); + SendEditEvent(ref doc, true, EventType.Paste); } catch (ApplicationControl.Exceptions.SIExceptionDocNotFound) { @@ -516,61 +574,58 @@ #region SendEditEvent - protected virtual SIEventOfficeDocEdit SendEditEvent(DocumentOfficeMS doc, bool showManulaClassificationDialog, EventType eventType) + /// + /// Send event to agent + /// + /// Doc on which we should send event + /// + /// + /// The event returned from HandleEvent + /// If events for doc are disabled + protected virtual SIEventOfficeDocEdit SendEditEvent(ref DocumentOfficeMS doc, bool showManulaClassificationDialog, EventType eventType) { - try + CheckIsDocEventsDisabled(doc); + Log.Logger.Trace("SendEditEvent started"); + doc.DisconectedFromNativeDoc = true; + SIEventOfficeDocEdit ev = new SIEventOfficeDocEdit(doc, eventType, AddInApi); + if (!IsUserDoc(doc)) + return ev; + + ev = (SIEventOfficeDocEdit)EventsHandling.EventHandlingUtils.HandleEventAsync(ev, null); + doc = ev.Doc; + if (ev.Doc.IsRMSProtected()) { - Log.Logger.Trace("SendEditEvent started"); - doc.DisconectedFromNativeDoc = true; - SIEventOfficeDocEdit ev = new SIEventOfficeDocEdit(doc); + (doc.Application as ApplicationOfficeMS).ShowRmsToolbar(); + } - if (!IsUserDoc(doc)) - return ev; + if (eventType != EventType.SaveBefore) // don't update options on beforeSave events - after save will handle the update + doc.ManualClassificationOptions = ev.ClassificationUpdate.ManualClassificationOptions; - ev = (SIEventOfficeDocEdit)EventsHandling.EventHandlingUtils.HandleEventAsync(ev, null); - if (ev.Doc.IsRMSProtected()) - { - (doc.Application as ApplicationOfficeMS).ShowRmsToolbar(); - } + ActiveDocDataChanged(doc); - if (eventType != EventType.SaveBefore) // don't update options on beforeSave events - after save will handle the update - doc.ManualClassificationOptions = ev.ClassificationUpdate.ManualClassificationOptions; - - ActiveDocDataChanged(doc); - - // show manual classification if document manual options changed - if (ev.ClassificationUpdate.ManualClassificationChanged || eventType == EventType.ManualClassification) + // show manual classification if document manual options changed + if (ev.ClassificationUpdate.ManualClassificationChanged || eventType == EventType.ManualClassification) + { + if (showManulaClassificationDialog) { - if (showManulaClassificationDialog) + switch (eventType) { - switch (eventType) - { - case EventType.SaveBefore: - InvokeAfterSaveEvent(doc); - break; - case EventType.Paste: - Log.Logger.Trace("Async invoke of ShowClassificationUIAfterSendEditEvent"); - Win32Window.GetWindow(doc.GetWindow()).AsyncInvoke.BeginInvoke(new ShowManualClassification(ShowClassificationUIAfterSendEditEvent), doc); - break; - default: - ShowClassificationUIAfterSendEditEvent(doc); - break; - } + case EventType.SaveBefore: + InvokeAfterSaveEvent(doc); + break; + case EventType.Paste: + Log.Logger.Trace("Async invoke of ShowClassificationUIAfterSendEditEvent"); + Win32Window.GetWindow(doc.GetWindow()).AsyncInvoke.BeginInvoke(new ShowManualClassification(ShowClassificationUIAfterSendEditEvent), doc); + break; + default: + ShowClassificationUIAfterSendEditEvent(doc); + break; } } + } - Log.Logger.Trace("SendEditEvent finished"); - return ev; - } - catch (System.Runtime.Remoting.RemotingException ex) - { - Log.Logger.InfoException("SendEditEvent failed: " + ex.Message, ex); - } - catch (Exception ex) - { - Log.Logger.ErrorException("SendEditEvent failed: " + ex.Message, ex); - } - return null; + Log.Logger.Trace("SendEditEvent finished"); + return ev; } #endregion @@ -632,7 +687,7 @@ FileInfo info = new FileInfo(doc.Path); if (!info.LastWriteTime.Equals((DateTime)docWin.Context[BEFORE_SAVE_LAST_WRITE_TIME])) // document has changed since before save - SendEditEvent(doc, true, EventType.SaveAfter); + SendEditEvent(ref doc, true, EventType.SaveAfter); } catch (SIException e) { @@ -647,16 +702,12 @@ #endregion #region OnDocumentBeforeSave - protected virtual void OnDocumentBeforeSave(DocumentOfficeMS doc, bool SaveAsUI, ref bool cancel) + protected virtual void OnDocumentBeforeSave(ref DocumentOfficeMS doc, bool SaveAsUI, ref bool cancel) { - object docInProcess = null; - if (m_DocumentsInProcess.TryGetValue(doc.NativeDoc, out docInProcess)) - return; - if (SaveAsUI) - SendEditEvent(doc, true, EventType.SaveAs); + SendEditEvent(ref doc, true, EventType.SaveAs); else - SendEditEvent(doc, true, EventType.SaveBefore); + SendEditEvent(ref doc, true, EventType.SaveBefore); } #endregion @@ -679,7 +730,7 @@ { m_AddInApi = new AddInExtApi(this); IEventsServer agent = EventsHandling.EventHandlingUtils.GetEventsHandler(); - agent.HandleEvent(new SIEventOfficeAppStartup(AppType, m_AddInApi)); + agent.HandleEvent(new SIEventOfficeAppStartup(AppType, AddInApi)); } catch (System.Runtime.Remoting.RemotingException ex) { @@ -710,7 +761,7 @@ try { IEventsServer agent = EventsHandling.EventHandlingUtils.GetEventsHandler(); - agent.HandleEvent(new SIEventOfficeAppShutDown(AppType, m_AddInApi)); + agent.HandleEvent(new SIEventOfficeAppShutDown(AppType, AddInApi)); } catch (System.Runtime.Remoting.RemotingException ex) { @@ -751,7 +802,7 @@ private void ShowClassificationUIAfterSendEditEvent(DocumentOfficeMS doc) { - SIEventOfficeDocEdit ev = new SIEventOfficeDocEdit(doc); + SIEventOfficeDocEdit ev = new SIEventOfficeDocEdit(doc, EventType.ManualClassification, AddInApi); Pair> retUI = ShowClassificationUI(doc); bool changed = (retUI != null) && (retUI.SecondValue != null) && (retUI.SecondValue.Count > 0); @@ -796,7 +847,7 @@ try { DocumentOfficeMS doc = (DocumentOfficeMS)m_app.GetActiveDocument(); - SendEditEvent(doc, true, EventType.ManualClassification); + SendEditEvent(ref doc, true, EventType.ManualClassification); } catch (SIExceptionDocNotFound) { Index: OfficeCommon/OfficeLibrary/AddIn/IAddInExtApi.cs =================================================================== --- OfficeCommon/OfficeLibrary/AddIn/IAddInExtApi.cs (revision 3367) +++ OfficeCommon/OfficeLibrary/AddIn/IAddInExtApi.cs (working copy) @@ -23,5 +23,7 @@ bool GetAddInInstanceId(IntPtr foregroundWindow, ref Guid addInInstanceId); bool GetActiveDocName(IntPtr foregroundWindow, ref string activeDocName); + + bool SetIsDocEventsDisabled(string name, bool disabled); } } Index: OfficeCommon/OfficeLibrary/AddIn/AddInExtApi.cs =================================================================== --- OfficeCommon/OfficeLibrary/AddIn/AddInExtApi.cs (revision 3367) +++ OfficeCommon/OfficeLibrary/AddIn/AddInExtApi.cs (working copy) @@ -169,6 +169,11 @@ return false; } + public bool SetIsDocEventsDisabled(string name, bool disabled) + { + return m_AddIn.SetIsDocEventsDisabled(name, disabled); + } + public override bool Equals(object obj) { if (!(obj is AddInExtApi)) Index: OfficeCommon/OfficeLibrary/OfficeLibrary.csproj =================================================================== --- OfficeCommon/OfficeLibrary/OfficeLibrary.csproj (revision 3367) +++ OfficeCommon/OfficeLibrary/OfficeLibrary.csproj (working copy) @@ -58,6 +58,7 @@ + @@ -72,6 +73,7 @@ + @@ -84,6 +86,7 @@ + Index: OfficeCommon/OfficeLibrary/Documents/DocumentOfficeMS.cs =================================================================== --- OfficeCommon/OfficeLibrary/Documents/DocumentOfficeMS.cs (revision 3367) +++ OfficeCommon/OfficeLibrary/Documents/DocumentOfficeMS.cs (working copy) @@ -261,6 +261,8 @@ return remAr; } + public abstract void SaveAs(string path); + #region IDocumentWithWindow Members public abstract IWin32Window GetWindow(); Index: OfficeCommon/OfficeLibrary/Events/SIEventOfficeDocEdit.cs =================================================================== --- OfficeCommon/OfficeLibrary/Events/SIEventOfficeDocEdit.cs (revision 3367) +++ OfficeCommon/OfficeLibrary/Events/SIEventOfficeDocEdit.cs (working copy) @@ -5,9 +5,36 @@ namespace OfficeLibrary.Events { + public enum EventType + { + Open, + Close, + Paste, + /// + /// Could be user triggered Save or autosave + /// + SaveBefore, + /// + /// triggered by user initiated saves + /// + SaveAfter, + /// + /// Save as event - we know for certain that user triggered the event + /// + SaveAs, + ManualClassification + } + [Serializable] public class SIEventOfficeDocEdit : EventsHandling.SIEventApplication, EventsHandling.IEventWithLocation { + private OfficeLibrary.AddIn.IAddInExtApi m_AddInApi; + public OfficeLibrary.AddIn.IAddInExtApi AddInApi + { + get { return m_AddInApi; } + set { m_AddInApi = value; } + } + private bool m_NeedUIRefresh = false; /// /// Returned param - has changes been made to the document which requires UI refresh @@ -32,19 +59,49 @@ set { m_FullName = value; } } - public SIEventOfficeDocEdit(DocumentOfficeMS doc) + bool m_RmsNoFullCycle = true; + /// + /// RMS protection using office only, full cycle won't be performed. + /// + public bool RmsNoFullCycle + { + get { return m_RmsNoFullCycle; } + set { m_RmsNoFullCycle = value; } + } + + private EventType m_EventType; + + public EventType EventType + { + get { return m_EventType; } + set { m_EventType = value; } + } + + public SIEventOfficeDocEdit(DocumentOfficeMS doc, EventType eventType, OfficeLibrary.AddIn.IAddInExtApi addInApi) : base(doc.Application.Type) { + m_AddInApi = addInApi; m_Doc = doc; FullName = doc.FullName; // copy manual classification options so new options will be marked ClassificationUpdate.ManualClassificationOptions = doc.ManualClassificationOptions; + EventType = eventType; + switch (EventType) + { + case EventType.Paste : + case EventType.Close : + this.RmsNoFullCycle = false; + break; + default : + this.RmsNoFullCycle = true; + break; + } } public override string ToString() { - return base.ToString() + " for document: [" + FullName + "]"; + return base.ToString() + "of type [" + EventType.ToString() + "] for document: [" + FullName + "]"; } public Uri Location Index: OfficeCommon/OfficeLibrary/Applications/ApplicationOfficeMS.cs =================================================================== --- OfficeCommon/OfficeLibrary/Applications/ApplicationOfficeMS.cs (revision 3367) +++ OfficeCommon/OfficeLibrary/Applications/ApplicationOfficeMS.cs (working copy) @@ -106,7 +106,9 @@ public Document OpenDocument(DataItem dataStorage, AccessMode mode) { - throw new SIExceptionUnimplemented("Currently only support for path documents exists."); + if (!(dataStorage is ApplicationControl.DataItemFile)) + throw new SIExceptionUnimplemented("Currently only support for path documents exists."); + return OpenDocument(((ApplicationControl.DataItemFile)dataStorage).Name, mode == AccessMode.Edit ? false : true, true); } public abstract Document OpenDocument(string path, bool readOnly, bool visible); Index: OfficeCommon/OfficeLibrary/Utilities/CustomDocumentProperties.cs =================================================================== --- OfficeCommon/OfficeLibrary/Utilities/CustomDocumentProperties.cs (revision 3367) +++ OfficeCommon/OfficeLibrary/Utilities/CustomDocumentProperties.cs (working copy) @@ -10,8 +10,7 @@ namespace OfficeLibrary { - [Serializable] - public class CustomDocumentProperties : IDisposable + public class CustomDocumentProperties : MarshalByRefObject, IDisposable { /// Index: OfficeCommon/OfficeLibrary/EH/EHOfficeDocEdit.cs =================================================================== --- OfficeCommon/OfficeLibrary/EH/EHOfficeDocEdit.cs (revision 3367) +++ OfficeCommon/OfficeLibrary/EH/EHOfficeDocEdit.cs (working copy) @@ -15,7 +15,7 @@ namespace OfficeLibrary.EH { - public class EHOfficeDocEdit : EHOfficeDoc, CommonUtils.IFactoryObject + public abstract class EHOfficeDocEdit : EHOfficeDoc { #region Constructors @@ -48,22 +48,47 @@ #region EH + //XXX roeeimpl - read only. + protected abstract void ApplyRMSAsSI(string sRMSPolic); + + private void ApplyRMSAsUser(string sRMSPolicy) + { + if (!Doc.HasRMSSupport()) + { + Log.Logger.Info("ApplyRMS with : [" + sRMSPolicy + "] aborted - RMS not supported on office version"); + } + try + { + base.ApplyRMS(sRMSPolicy); + m_Event.NeedUIRefresh = true; + } + catch (SIExceptionUnauthorized e) + { + Log.Logger.InfoException("ApplyRMS with : [" + sRMSPolicy + "] aborted - missing permissions to assign RMS as user: " + e.Message, e); + } + } + + private bool IsRMSAsUser() + { + if (m_Event.RmsNoFullCycle) + return true; + + if (string.IsNullOrEmpty(Doc.Path)) + return true; //XXX roeeimpl - save into tmp path, in add-in if saved in tmp path should handle save as save-as + + //XXX roeeimpl If rule says - allow author full control && no RMS or user is author - return true. + + return false; + } + public override void ApplyRMS(string sRMSPolicy) { - if (!Doc.HasRMSSupport()) - { - Log.Logger.Info("RMS not supported on office version"); - return; - } - try - { - base.ApplyRMS(sRMSPolicy); - m_Event.NeedUIRefresh = true; - } - catch (SIExceptionUnauthorized e) - { - Log.Logger.InfoException("RMS template: [" + sRMSPolicy + "] not applied to missing permissions: " + e.Message, e); - } + if (IsRMSAsUser()) + { + ApplyRMSAsUser(sRMSPolicy); + return; + } + ApplyRMSAsSI(sRMSPolicy); } public override string FileName @@ -83,24 +108,5 @@ } } #endregion - - #region IFactoryObject - - public virtual bool IsMine(object createParams) - { - return createParams is SIEventOfficeDocEdit; - } - - protected virtual EventsHandling.EnforcementHandlers.EH Create(SIEventOfficeDocEdit eventInfo) - { - return new EHOfficeDocEdit(eventInfo); - } - - public virtual EventsHandling.EnforcementHandlers.EH Create(object createParams) - { - return Create((SIEventOfficeDocEdit)createParams); - } - - #endregion } } Index: OfficeCommon/testing/OfficeLibraryTests/EHTests.cs =================================================================== --- OfficeCommon/testing/OfficeLibraryTests/EHTests.cs (revision 3367) +++ OfficeCommon/testing/OfficeLibraryTests/EHTests.cs (working copy) @@ -127,13 +127,15 @@ try { // run protection - SIEventOfficeDocEdit ev = new SIEventOfficeDocEdit(doc); + /* XXX + SIEventOfficeDocEdit ev = new SIEventOfficeDocEdit(doc, EventType.SaveBefore); using (EHOfficeDocEdit eh = new EHOfficeDocEdit(ev)) { eh.ApplyRMS(TemplatePath(rmsTemplate)); eh.Close(); doc.Save(); } + */ } finally { Index: Word/WordLibrary/AddIn/WordAddinHelper.cs =================================================================== --- Word/WordLibrary/AddIn/WordAddinHelper.cs (revision 3367) +++ Word/WordLibrary/AddIn/WordAddinHelper.cs (working copy) @@ -40,6 +40,42 @@ } } + protected override void ApplyRMSAsSI(string sRMSPolic, DocumentWordMS doc) + { + string fullName = Doc.FullName; + object docState = Doc.State; + //roeeimpl Save state (can parallel protect original doc) + + bool eventsOnDoc = m_Event.AddInApi.SetIsDocEventsDisabled(fullName, true); + try + { + //Save doc to temp file, present the temp file while we protect the orig. + using (SITmpFile visibleCopy = new SITmpFile()) + { + visibleCopy.CreateUniqueName(Doc.Name); + bool eventsOnCopy = m_Event.AddInApi.SetIsDocEventsDisabled(visibleCopy.FileName, true); + try + { + Doc.SaveAs(visibleCopy.FileName); + SIEventOfficeProtectFileRequest protectFileEvent = new SIEventOfficeProtectFileRequest(fullName, ApplicationType.SIWORD); + EventsHandling.EventHandlingUtils.GetEventsHandler().HandleEvent(protectFileEvent); + //open original doc and restore state.(in AddIn Thread) + DocumentWordMS newDoc = (DocumentWordMS)Doc.Application.OpenDocument(new DataItemFile(fullName), AccessMode.Edit); + newDoc.State = docState; + Doc.Close(); + } + finally + { + m_Event.AddInApi.SetIsDocEventsDisabled(visibleCopy.FileName, eventsOnCopy); + } + } + } + finally + { + m_Event.AddInApi.SetIsDocEventsDisabled(fullName, eventsOnDoc); + } + } + #region deligates Form ApplicationEvents4 public void OnDocumentWindowActivated(MSWord.Document Doc, MSWord.Window Wn) @@ -49,20 +85,20 @@ if (!IsSupportedWord()) return; - DocumentWordMS wDoc = new DocumentWordMS(Doc); + DocumentOfficeMS wDoc = new DocumentWordMS(Doc); LogAddIn.Logger.Trace("doc: [" + wDoc.FullName + "] activated"); if (!m_AppStartAsVisible) { m_AppStartAsVisible = true; - base.SendEditEvent(wDoc, true, EventType.Open); + base.SendEditEvent(ref wDoc, true, EventType.Open); } OnDocumentActivated(wDoc); } catch (Exception e) { - ReportException(new DocumentWordMS(Doc), e); + ReportException(null, e); } } @@ -85,11 +121,12 @@ if (!m_app.Visible) m_AppStartAsVisible = false; - OnDocumentOpen(new DocumentWordMS(doc)); + DocumentOfficeMS wDoc = new DocumentWordMS(doc); + OnDocumentOpen(ref wDoc); } catch (Exception e) { - ReportException(new DocumentWordMS(doc), e); + ReportException(null, e); } } @@ -101,11 +138,12 @@ return; LogAddIn.Logger.Trace("doc: [" + doc.FullName + "]"); - OnDocumentBeforeClose(new DocumentWordMS(doc), ref cancel); + DocumentOfficeMS wDoc = new DocumentWordMS(doc); + OnDocumentBeforeClose(ref wDoc, ref cancel); } catch (Exception e) { - ReportException(new DocumentWordMS(doc), e); + ReportException(null, e); } } @@ -117,11 +155,12 @@ return; LogAddIn.Logger.Trace("Before Save, doc: [" + doc.FullName + "]"); - base.OnDocumentBeforeSave(new DocumentWordMS(doc), SaveAsUI, ref cancel); + DocumentOfficeMS wDoc = new DocumentWordMS(doc); + base.OnDocumentBeforeSave(ref wDoc, SaveAsUI, ref cancel); } catch (Exception e) { - ReportException(new DocumentWordMS(doc), e); + ReportException(null, e); } } Index: Word/WordLibrary/WordLibrary.csproj =================================================================== --- Word/WordLibrary/WordLibrary.csproj (revision 3367) +++ Word/WordLibrary/WordLibrary.csproj (working copy) @@ -36,6 +36,7 @@ + @@ -71,6 +72,10 @@ {98828D1C-7708-4C17-98BA-1AC2E890BF5B} EventsHandling + + {0E57A903-3064-43F4-9FF0-59618E59B3C5} + SI.RMS.Task + {BCDFD4B0-7535-4C6B-B5F6-A01E00F79631} ClipboardHelper Index: Word/WordLibrary/Wrappers/DocumentWordMS.cs =================================================================== --- Word/WordLibrary/Wrappers/DocumentWordMS.cs (revision 3367) +++ Word/WordLibrary/Wrappers/DocumentWordMS.cs (working copy) @@ -263,6 +263,14 @@ } + public override void SaveAs(string path) + { + object oPath = path; + DocAsWordDoc.SaveAs(ref oPath, + ref m_MissingValue, ref m_MissingValue, ref m_MissingValue, ref m_MissingValue, ref m_MissingValue, ref m_MissingValue, ref m_MissingValue, ref m_MissingValue, ref m_MissingValue, ref m_MissingValue, + ref m_MissingValue, ref m_MissingValue, ref m_MissingValue, ref m_MissingValue, ref m_MissingValue); + } + public override object State { get Index: Word/WordLibrary/EH/EHOfficeDocEditWord.cs =================================================================== --- Word/WordLibrary/EH/EHOfficeDocEditWord.cs (revision 0) +++ Word/WordLibrary/EH/EHOfficeDocEditWord.cs (revision 3534) @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Text; +using OfficeLibrary.Events; +using AgentUtils; +using WordLibrary.Wrappers; +using ApplicationControl; +using ApplicationControl.AbstractTypes; + +namespace WordLibrary.EH +{ + public class EHOfficeDocEditWord : OfficeLibrary.EH.EHOfficeDocEdit , CommonUtils.IFactoryObject + { + #region Constructors + + public EHOfficeDocEditWord() : base() + { + } + + public EHOfficeDocEditWord(SIEventOfficeDocEdit ev) : base(ev) + { + } + + #endregion + + + + #region IFactoryObject + + public virtual bool IsMine(object createParams) + { + return (createParams is SIEventOfficeDocEdit) && ((SIEventOfficeDocEdit)createParams).AppType == ApplicationControl.ApplicationType.MSWORD; + } + + protected virtual EventsHandling.EnforcementHandlers.EH Create(SIEventOfficeDocEdit eventInfo) + { + return new EHOfficeDocEditWord(eventInfo); + } + + public virtual EventsHandling.EnforcementHandlers.EH Create(object createParams) + { + return Create((SIEventOfficeDocEdit)createParams); + } + + #endregion + } +}