/// ------------------------------------------------------------------------------------ /// <summary> /// Subscribes to the Position changed event for all windows in current link set. /// </summary> /// <remarks>NOTE: subscribing to all windows in the link set fires the position changed /// event multiple times. However, if we don't do it and the first window happens not to /// have the same range as the other resources, then we don't get event.</remarks> /// ------------------------------------------------------------------------------------ private void SetupPositionEvents() { m_cookies.Clear(); try { if (m_libronixApp == null || m_PositionChangedBridge == null) { return; } LbxWindows windows = m_libronixApp.Application.Windows; foreach (LbxWindow window in windows) { if (window.Type == "resource") { LbxResourceWindowInfo windowInfo = (LbxResourceWindowInfo)window.Info; if (windowInfo.ActiveDataType == "bible") { int cookie = m_PositionChangedBridge.Connect(windowInfo); LbxResourceView view = (LbxResourceView)window.View; LbxResource resource = (LbxResource)view.Resource; LbxResourcePositions positions = (LbxResourcePositions)resource.get_Positions(null); m_cookies.Add(new LinkInfo(windowInfo, cookie, positions)); } } } } catch (COMException e) { Debug.Fail("Got exception in LibronixPositionHandler.SetupPositionEvents: " + e.Message); m_libronixApp = null; } }
/// <summary> /// If Libronix is running, save its workspace in the specified file. /// <param name="path"></param> /// </summary> public static void SaveWorkspace(string path) { try { // If Libronix isn't running, we'll get an exception here object libApp = Marshal.GetActiveObject("LibronixDLS.LbxApplication"); if (libApp == null) { return; } LbxApplication libronixApp = libApp as LbxApplication; object document = libronixApp.MSXML.CreateDocument(0); //MSXML2.DOMDocument40 doc = new MSXML2.DOMDocument40(); //doc. libronixApp.SaveWorkspace(document, ""); MSXML2.DOMDocument doc = document as MSXML2.DOMDocument; doc.save(path); //Type docType = document.GetType(); //MethodInfo info = docType.GetMethod("save"); //if (info != null) // info.Invoke(document, new object[] {path}); } catch (COMException) { return; } }
static void DoTheRestore(string path) { LbxApplication libronixApp = null; try { // If Libronix isn't running, we'll get an exception here object libApp = Marshal.GetActiveObject("LibronixDLS.LbxApplication"); return; // It IS running; don't disturb it. } catch (COMException e) { if ((uint)e.ErrorCode == 0x800401E3) // MK_E_UNAVAILABLE { // try to start libronixApp = new LbxApplicationClass(); } } if (libronixApp == null) // can't start, or not installed. { return; } try { // Try to load workspace. if (!File.Exists(path)) { libronixApp.Visible = true; //let them see it, anyway. return; } object document = libronixApp.MSXML.CreateDocument(0); MSXML2.DOMDocument doc = document as MSXML2.DOMDocument; doc.load(path); //Type docType = document.GetType(); //MethodInfo info = docType.GetMethod("Save"); //if (info == null) //{ // ReportLoadProblem(); // return; //} //info.Invoke(document, new object[] { path }); libronixApp.LoadWorkspace(document, "", DlsSaveChanges.dlsPromptToSaveChanges); libronixApp.Visible = true; //only after we reload the workspace, to save flashing. } catch (Exception) { libronixApp.Visible = true; //let them see it, anyway. ReportLoadProblem(); } }
/// ------------------------------------------------------------------------------------ /// <summary> /// Closes the connection to Libronix /// </summary> /// ------------------------------------------------------------------------------------ private void Close() { lock (this) { if (m_PositionChangedBridge != null) { if (m_cookies != null) { RemovePositionEvents(); } m_PositionChangedBridge.PositionChanged -= OnPositionChangedInLibronix; } m_PositionChangedBridge = null; m_cookies.Clear(); if (m_pollTimer != null) { m_pollTimer.Stop(); m_pollTimer.Dispose(); m_pollTimer = null; } if (m_ApplicationEventsBridge != null) { try { m_ApplicationEventsBridge.Disconnect(m_libronixApp, m_ApplicationEventsCookie); } catch { } m_ApplicationEventsBridge.EventFired -= OnApplicationEventsBridgeEventFired; } m_ApplicationEventsBridge = null; m_libronixApp = null; } }
/// ------------------------------------------------------------------------------------ /// <summary> /// Initalizes the Libronix COM objects. /// </summary> /// ------------------------------------------------------------------------------------ private void Initalize() { try { // If Libronix isn't running, we'll get an exception here object libApp = Marshal.GetActiveObject("LibronixDLS.LbxApplication"); m_libronixApp = libApp as LbxApplication; } catch (COMException e) { if ((uint)e.ErrorCode == 0x800401E3) // MK_E_UNAVAILABLE { if (m_fStart) { try { // try to start m_libronixApp = new LbxApplicationClass(); m_libronixApp.Visible = true; } catch (Exception e1) { Debug.Fail("Got exception in Initialize trying to start Libronix: " + e1.Message); m_libronixApp = null; s_fNotInstalled = true; } } else { m_libronixApp = null; StartTimer(); } } else { s_fNotInstalled = true; } } catch (Exception e) { Debug.Fail("Got exception in Initialize trying to get running Libronix object: " + e.Message); } if (m_libronixApp == null) { return; } // The commented-out lines are necessary to receive sync messages from Libronix, but currently // they cause a crash (in release builds only! beware!). // We may be able to create this object in a separate AppDomain. This has not yet been tried. // Various and confusing error messages come up and exceptions get thrown within the method. // (See TE-6457.) If we don't otherwise get a fix, it may be worth anothe try when we upgrade // to the next version of ICU, because the problem is something to do with not being able // to find an ICU method...possibly some of Libronix's marshalling code is messed up by our // version of ICU?? m_PositionChangedBridge = null; StartPollTimer(); //m_PositionChangedBridge = new LbxResourcePositionChangedBridgeClass(); //m_PositionChangedBridge.PositionChanged += // new DLbxResourcePositionChanged_PositionChangedEventHandler(OnPositionChangedInLibronix); m_ApplicationEventsBridge = new LbxApplicationEventsBridgeClass(); m_ApplicationEventsBridge.EventFired += new DLbxApplicationEvents_EventFiredEventHandler(OnApplicationEventsBridgeEventFired); m_ApplicationEventsCookie = m_ApplicationEventsBridge.Connect(m_libronixApp); SetupPositionEvents(); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Subscribes to the Position changed event for all windows in current link set. /// </summary> /// <remarks>NOTE: subscribing to all windows in the link set fires the position changed /// event multiple times. However, if we don't do it and the first window happens not to /// have the same range as the other resources, then we don't get event.</remarks> /// ------------------------------------------------------------------------------------ private void SetupPositionEvents() { m_cookies.Clear(); try { if (m_libronixApp == null || m_PositionChangedBridge == null) return; LbxWindows windows = m_libronixApp.Application.Windows; foreach (LbxWindow window in windows) { if (window.Type == "resource") { LbxResourceWindowInfo windowInfo = (LbxResourceWindowInfo)window.Info; if (windowInfo.ActiveDataType == "bible") { int cookie = m_PositionChangedBridge.Connect(windowInfo); LbxResourceView view = (LbxResourceView)window.View; LbxResource resource = (LbxResource)view.Resource; LbxResourcePositions positions = (LbxResourcePositions)resource.get_Positions(null); m_cookies.Add(new LinkInfo(windowInfo, cookie, positions)); } } } } catch (COMException e) { Debug.Fail("Got exception in LibronixPositionHandler.SetupPositionEvents: " + e.Message); m_libronixApp = null; } }
/// ------------------------------------------------------------------------------------ /// <summary> /// Closes the connection to Libronix /// </summary> /// ------------------------------------------------------------------------------------ private void Close() { lock (this) { if (m_PositionChangedBridge != null) { if (m_cookies != null) RemovePositionEvents(); m_PositionChangedBridge.PositionChanged -= OnPositionChangedInLibronix; } m_PositionChangedBridge = null; m_cookies.Clear(); if (m_pollTimer != null) { m_pollTimer.Stop(); m_pollTimer.Dispose(); m_pollTimer = null; } if (m_ApplicationEventsBridge != null) { try { m_ApplicationEventsBridge.Disconnect(m_libronixApp, m_ApplicationEventsCookie); } catch { } m_ApplicationEventsBridge.EventFired -= OnApplicationEventsBridgeEventFired; } m_ApplicationEventsBridge = null; m_libronixApp = null; } }
/// ------------------------------------------------------------------------------------ /// <summary> /// Initalizes the Libronix COM objects. /// </summary> /// ------------------------------------------------------------------------------------ private void Initalize() { try { // If Libronix isn't running, we'll get an exception here object libApp = Marshal.GetActiveObject("LibronixDLS.LbxApplication"); m_libronixApp = libApp as LbxApplication; } catch (COMException e) { if ((uint)e.ErrorCode == 0x800401E3) // MK_E_UNAVAILABLE { if (m_fStart) { try { // try to start m_libronixApp = new LbxApplicationClass(); m_libronixApp.Visible = true; } catch (Exception e1) { Debug.Fail("Got exception in Initialize trying to start Libronix: " + e1.Message); m_libronixApp = null; s_fNotInstalled = true; } } else { m_libronixApp = null; StartTimer(); } } else s_fNotInstalled = true; } catch (Exception e) { Debug.Fail("Got exception in Initialize trying to get running Libronix object: " + e.Message); } if (m_libronixApp == null) return; // The commented-out lines are necessary to receive sync messages from Libronix, but currently // they cause a crash (in release builds only! beware!). // We may be able to create this object in a separate AppDomain. This has not yet been tried. // Various and confusing error messages come up and exceptions get thrown within the method. // (See TE-6457.) If we don't otherwise get a fix, it may be worth anothe try when we upgrade // to the next version of ICU, because the problem is something to do with not being able // to find an ICU method...possibly some of Libronix's marshalling code is messed up by our // version of ICU?? m_PositionChangedBridge = null; StartPollTimer(); //m_PositionChangedBridge = new LbxResourcePositionChangedBridgeClass(); //m_PositionChangedBridge.PositionChanged += // new DLbxResourcePositionChanged_PositionChangedEventHandler(OnPositionChangedInLibronix); m_ApplicationEventsBridge = new LbxApplicationEventsBridgeClass(); m_ApplicationEventsBridge.EventFired += new DLbxApplicationEvents_EventFiredEventHandler(OnApplicationEventsBridgeEventFired); m_ApplicationEventsCookie = m_ApplicationEventsBridge.Connect(m_libronixApp); SetupPositionEvents(); }