1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_forms.hxx"
30 #include "controlfeatureinterception.hxx"
31 #include "urltransformer.hxx"
32 
33 /** === begin UNO includes === **/
34 /** === end UNO includes === **/
35 #include <tools/debug.hxx>
36 
37 //........................................................................
38 namespace frm
39 {
40 //........................................................................
41 
42     using namespace ::com::sun::star::uno;
43     using namespace ::com::sun::star::frame;
44     using namespace ::com::sun::star::util;
45     using namespace ::com::sun::star::lang;
46 
47 	//====================================================================
48 	//= ControlFeatureInterception
49 	//====================================================================
50 	//--------------------------------------------------------------------
51     ControlFeatureInterception::ControlFeatureInterception( const Reference< XMultiServiceFactory >& _rxORB )
52         :m_pUrlTransformer( new UrlTransformer( _rxORB ) )
53     {
54     }
55 
56     //--------------------------------------------------------------------
57     void SAL_CALL ControlFeatureInterception::registerDispatchProviderInterceptor( const Reference< XDispatchProviderInterceptor >& _rxInterceptor ) throw (RuntimeException )
58     {
59         if ( !_rxInterceptor.is() )
60         {
61             DBG_ERROR( "ControlFeatureInterception::registerDispatchProviderInterceptor: invalid interceptor!" );
62             return;
63         }
64 
65 		if ( m_xFirstDispatchInterceptor.is() )
66 		{
67 			// there is already an interceptor; the new one will become its master
68 			Reference< XDispatchProvider > xFirstProvider( m_xFirstDispatchInterceptor, UNO_QUERY );
69 			_rxInterceptor->setSlaveDispatchProvider( xFirstProvider );
70 			m_xFirstDispatchInterceptor->setMasterDispatchProvider( xFirstProvider );
71 		}
72 
73 		// we are the master of the chain's first interceptor
74 		m_xFirstDispatchInterceptor = _rxInterceptor;
75         m_xFirstDispatchInterceptor->setMasterDispatchProvider( NULL );
76             // it's the first of the interceptor chain
77     }
78 
79     //--------------------------------------------------------------------
80     void SAL_CALL ControlFeatureInterception::releaseDispatchProviderInterceptor( const Reference< XDispatchProviderInterceptor >& _rxInterceptor ) throw (RuntimeException )
81     {
82 	    if ( !_rxInterceptor.is() )
83         {
84             DBG_ERROR( "ControlFeatureInterception::releaseDispatchProviderInterceptor: invalid interceptor!" );
85             return;
86         }
87 
88 	    Reference< XDispatchProviderInterceptor >  xChainWalk( m_xFirstDispatchInterceptor );
89 
90 	    if ( m_xFirstDispatchInterceptor == _rxInterceptor )
91 	    {	// our chain will have a new first element
92 		    Reference< XDispatchProviderInterceptor >  xSlave( m_xFirstDispatchInterceptor->getSlaveDispatchProvider(), UNO_QUERY );
93 		    m_xFirstDispatchInterceptor = xSlave;
94 	    }
95 	    // do this before removing the interceptor from the chain as we won't know it's slave afterwards)
96 
97 	    while ( xChainWalk.is() )
98 	    {
99 		    // walk along the chain of interceptors and look for the interceptor that has to be removed
100 		    Reference< XDispatchProviderInterceptor >  xSlave( xChainWalk->getSlaveDispatchProvider(), UNO_QUERY );
101 
102 		    if ( xChainWalk == _rxInterceptor )
103 		    {
104 			    // old master may be an interceptor too
105 			    Reference< XDispatchProviderInterceptor >  xMaster( xChainWalk->getMasterDispatchProvider(), UNO_QUERY );
106 
107 			    // unchain the interceptor that has to be removed
108 			    xChainWalk->setSlaveDispatchProvider( NULL );
109 			    xChainWalk->setMasterDispatchProvider( NULL );
110 
111 			    // reconnect the chain
112 			    if ( xMaster.is() )
113 			    {
114 				    xMaster->setSlaveDispatchProvider( Reference< XDispatchProvider >::query( xSlave ) );
115 			    }
116 
117                 // if somebody has registered the same interceptor twice, then we will remove
118                 // it once per call ...
119                 break;
120 		    }
121 
122 		    xChainWalk = xSlave;
123 	    }
124     }
125 
126     //--------------------------------------------------------------------
127     void ControlFeatureInterception::dispose()
128     {
129         // release all interceptors
130 	    Reference< XDispatchProviderInterceptor > xInterceptor( m_xFirstDispatchInterceptor );
131 	    m_xFirstDispatchInterceptor.clear();
132 	    while ( xInterceptor.is() )
133 	    {
134 		    // tell the interceptor it has a new (means no) predecessor
135 		    xInterceptor->setMasterDispatchProvider( NULL );
136 
137 		    // ask for it's successor
138 		    Reference< XDispatchProvider > xSlave = xInterceptor->getSlaveDispatchProvider();
139 		    // and give it the new (means no) successoert
140 		    xInterceptor->setSlaveDispatchProvider( NULL );
141 
142 		    // start over with the next chain element
143 		    xInterceptor = xInterceptor.query( xSlave );
144 	    }
145     }
146     //--------------------------------------------------------------------
147     Reference< XDispatch > ControlFeatureInterception::queryDispatch( const URL& _rURL, const ::rtl::OUString& _rTargetFrameName, ::sal_Int32 _nSearchFlags ) SAL_THROW((RuntimeException))
148     {
149         Reference< XDispatch > xDispatcher;
150         if ( m_xFirstDispatchInterceptor.is() )
151             xDispatcher = m_xFirstDispatchInterceptor->queryDispatch( _rURL, _rTargetFrameName, _nSearchFlags );
152         return xDispatcher;
153     }
154 
155     //--------------------------------------------------------------------
156     Reference< XDispatch > ControlFeatureInterception::queryDispatch( const URL& _rURL ) SAL_THROW((RuntimeException))
157     {
158         return queryDispatch( _rURL, ::rtl::OUString(), 0 );
159     }
160 
161     //--------------------------------------------------------------------
162     Reference< XDispatch > ControlFeatureInterception::queryDispatch( const sal_Char* _pAsciiURL ) SAL_THROW((RuntimeException))
163     {
164         return queryDispatch( m_pUrlTransformer->getStrictURLFromAscii( _pAsciiURL ) );
165     }
166 
167 //........................................................................
168 } // namespace frm
169 //........................................................................
170 
171