1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_forms.hxx"
26 #include "controlfeatureinterception.hxx"
27 #include "urltransformer.hxx"
28 
29 /** === begin UNO includes === **/
30 /** === end UNO includes === **/
31 #include <tools/debug.hxx>
32 
33 //........................................................................
34 namespace frm
35 {
36 //........................................................................
37 
38     using namespace ::com::sun::star::uno;
39     using namespace ::com::sun::star::frame;
40     using namespace ::com::sun::star::util;
41     using namespace ::com::sun::star::lang;
42 
43 	//====================================================================
44 	//= ControlFeatureInterception
45 	//====================================================================
46 	//--------------------------------------------------------------------
ControlFeatureInterception(const Reference<XMultiServiceFactory> & _rxORB)47     ControlFeatureInterception::ControlFeatureInterception( const Reference< XMultiServiceFactory >& _rxORB )
48         :m_pUrlTransformer( new UrlTransformer( _rxORB ) )
49     {
50     }
51 
52     //--------------------------------------------------------------------
registerDispatchProviderInterceptor(const Reference<XDispatchProviderInterceptor> & _rxInterceptor)53     void SAL_CALL ControlFeatureInterception::registerDispatchProviderInterceptor( const Reference< XDispatchProviderInterceptor >& _rxInterceptor ) throw (RuntimeException )
54     {
55         if ( !_rxInterceptor.is() )
56         {
57             DBG_ERROR( "ControlFeatureInterception::registerDispatchProviderInterceptor: invalid interceptor!" );
58             return;
59         }
60 
61 		if ( m_xFirstDispatchInterceptor.is() )
62 		{
63 			// there is already an interceptor; the new one will become its master
64 			Reference< XDispatchProvider > xFirstProvider( m_xFirstDispatchInterceptor, UNO_QUERY );
65 			_rxInterceptor->setSlaveDispatchProvider( xFirstProvider );
66 			m_xFirstDispatchInterceptor->setMasterDispatchProvider( xFirstProvider );
67 		}
68 
69 		// we are the master of the chain's first interceptor
70 		m_xFirstDispatchInterceptor = _rxInterceptor;
71         m_xFirstDispatchInterceptor->setMasterDispatchProvider( NULL );
72             // it's the first of the interceptor chain
73     }
74 
75     //--------------------------------------------------------------------
releaseDispatchProviderInterceptor(const Reference<XDispatchProviderInterceptor> & _rxInterceptor)76     void SAL_CALL ControlFeatureInterception::releaseDispatchProviderInterceptor( const Reference< XDispatchProviderInterceptor >& _rxInterceptor ) throw (RuntimeException )
77     {
78 	    if ( !_rxInterceptor.is() )
79         {
80             DBG_ERROR( "ControlFeatureInterception::releaseDispatchProviderInterceptor: invalid interceptor!" );
81             return;
82         }
83 
84 	    Reference< XDispatchProviderInterceptor >  xChainWalk( m_xFirstDispatchInterceptor );
85 
86 	    if ( m_xFirstDispatchInterceptor == _rxInterceptor )
87 	    {	// our chain will have a new first element
88 		    Reference< XDispatchProviderInterceptor >  xSlave( m_xFirstDispatchInterceptor->getSlaveDispatchProvider(), UNO_QUERY );
89 		    m_xFirstDispatchInterceptor = xSlave;
90 	    }
91 	    // do this before removing the interceptor from the chain as we won't know it's slave afterwards)
92 
93 	    while ( xChainWalk.is() )
94 	    {
95 		    // walk along the chain of interceptors and look for the interceptor that has to be removed
96 		    Reference< XDispatchProviderInterceptor >  xSlave( xChainWalk->getSlaveDispatchProvider(), UNO_QUERY );
97 
98 		    if ( xChainWalk == _rxInterceptor )
99 		    {
100 			    // old master may be an interceptor too
101 			    Reference< XDispatchProviderInterceptor >  xMaster( xChainWalk->getMasterDispatchProvider(), UNO_QUERY );
102 
103 			    // unchain the interceptor that has to be removed
104 			    xChainWalk->setSlaveDispatchProvider( NULL );
105 			    xChainWalk->setMasterDispatchProvider( NULL );
106 
107 			    // reconnect the chain
108 			    if ( xMaster.is() )
109 			    {
110 				    xMaster->setSlaveDispatchProvider( Reference< XDispatchProvider >::query( xSlave ) );
111 			    }
112 
113                 // if somebody has registered the same interceptor twice, then we will remove
114                 // it once per call ...
115                 break;
116 		    }
117 
118 		    xChainWalk = xSlave;
119 	    }
120     }
121 
122     //--------------------------------------------------------------------
dispose()123     void ControlFeatureInterception::dispose()
124     {
125         // release all interceptors
126 	    Reference< XDispatchProviderInterceptor > xInterceptor( m_xFirstDispatchInterceptor );
127 	    m_xFirstDispatchInterceptor.clear();
128 	    while ( xInterceptor.is() )
129 	    {
130 		    // tell the interceptor it has a new (means no) predecessor
131 		    xInterceptor->setMasterDispatchProvider( NULL );
132 
133 		    // ask for it's successor
134 		    Reference< XDispatchProvider > xSlave = xInterceptor->getSlaveDispatchProvider();
135 		    // and give it the new (means no) successoert
136 		    xInterceptor->setSlaveDispatchProvider( NULL );
137 
138 		    // start over with the next chain element
139 		    xInterceptor = xInterceptor.query( xSlave );
140 	    }
141     }
142     //--------------------------------------------------------------------
queryDispatch(const URL & _rURL,const::rtl::OUString & _rTargetFrameName,::sal_Int32 _nSearchFlags)143     Reference< XDispatch > ControlFeatureInterception::queryDispatch( const URL& _rURL, const ::rtl::OUString& _rTargetFrameName, ::sal_Int32 _nSearchFlags ) SAL_THROW((RuntimeException))
144     {
145         Reference< XDispatch > xDispatcher;
146         if ( m_xFirstDispatchInterceptor.is() )
147             xDispatcher = m_xFirstDispatchInterceptor->queryDispatch( _rURL, _rTargetFrameName, _nSearchFlags );
148         return xDispatcher;
149     }
150 
151     //--------------------------------------------------------------------
queryDispatch(const URL & _rURL)152     Reference< XDispatch > ControlFeatureInterception::queryDispatch( const URL& _rURL ) SAL_THROW((RuntimeException))
153     {
154         return queryDispatch( _rURL, ::rtl::OUString(), 0 );
155     }
156 
157     //--------------------------------------------------------------------
queryDispatch(const sal_Char * _pAsciiURL)158     Reference< XDispatch > ControlFeatureInterception::queryDispatch( const sal_Char* _pAsciiURL ) SAL_THROW((RuntimeException))
159     {
160         return queryDispatch( m_pUrlTransformer->getStrictURLFromAscii( _pAsciiURL ) );
161     }
162 
163 //........................................................................
164 } // namespace frm
165 //........................................................................
166 
167