merge the formfield patch from ooo-build
[ooovba.git] / scripting / source / provider / ActiveMSPList.cxx
blob641ca09a59d493a825309c262cec494fe6c27599
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ActiveMSPList.cxx,v $
10 * $Revision: 1.16.16.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_scripting.hxx"
33 #include <cppuhelper/implementationentry.hxx>
34 #include <cppuhelper/factory.hxx>
35 #include <cppuhelper/implbase1.hxx>
36 #include <cppuhelper/exc_hlp.hxx>
37 #include <util/scriptingconstants.hxx>
38 #include <util/util.hxx>
39 #include <util/MiscUtils.hxx>
41 #include <com/sun/star/beans/XPropertySet.hpp>
42 #include <com/sun/star/util/XMacroExpander.hpp>
43 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
45 #include <com/sun/star/script/browse/BrowseNodeTypes.hpp>
47 #include "MasterScriptProvider.hxx"
48 #include "ActiveMSPList.hxx"
50 #include <tools/diagnose_ex.h>
52 using namespace com::sun::star;
53 using namespace com::sun::star::uno;
54 using namespace com::sun::star::script;
55 using namespace ::scripting_util;
56 using namespace ::sf_misc;
58 namespace func_provider
61 ActiveMSPList::ActiveMSPList( const Reference< XComponentContext > & xContext ) : m_xContext( xContext )
63 userDirString = ::rtl::OUString::createFromAscii("user");
64 shareDirString = ::rtl::OUString::createFromAscii("share");
67 ActiveMSPList::~ActiveMSPList()
71 Reference< provider::XScriptProvider >
72 ActiveMSPList::createNewMSP( const uno::Any& context )
74 ::rtl::OUString serviceName = ::rtl::OUString::createFromAscii("com.sun.star.script.provider.MasterScriptProvider");
75 Sequence< Any > args( &context, 1 );
77 Reference< provider::XScriptProvider > msp(
78 m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
79 serviceName, args, m_xContext ), UNO_QUERY );
80 return msp;
83 Reference< provider::XScriptProvider >
84 ActiveMSPList::getMSPFromAnyContext( const Any& aContext )
85 SAL_THROW(( lang::IllegalArgumentException, RuntimeException ))
87 Reference< provider::XScriptProvider > msp;
88 ::rtl::OUString sContext;
89 if ( aContext >>= sContext )
91 msp = getMSPFromStringContext( sContext );
92 return msp;
95 Reference< frame::XModel > xModel( aContext, UNO_QUERY );
97 Reference< document::XScriptInvocationContext > xScriptContext( aContext, UNO_QUERY );
98 if ( xScriptContext.is() )
102 // the component supports executing scripts embedded in a - possibly foreign document.
103 // Check whether this other document its the component itself.
104 if ( !xModel.is() || ( xModel != xScriptContext->getScriptContainer() ) )
106 msp = getMSPFromInvocationContext( xScriptContext );
107 return msp;
110 catch( const lang::IllegalArgumentException& )
112 xModel.set( Reference< frame::XModel >() );
116 if ( xModel.is() )
118 sContext = MiscUtils::xModelToTdocUrl( xModel, m_xContext );
119 msp = getMSPFromStringContext( sContext );
120 return msp;
123 createNonDocMSPs();
124 return m_hMsps[ shareDirString ];
127 Reference< provider::XScriptProvider >
128 ActiveMSPList::getMSPFromInvocationContext( const Reference< document::XScriptInvocationContext >& xContext )
129 SAL_THROW(( lang::IllegalArgumentException, RuntimeException ))
131 Reference< provider::XScriptProvider > msp;
133 Reference< document::XEmbeddedScripts > xScripts;
134 if ( xContext.is() )
135 xScripts.set( xContext->getScriptContainer() );
136 if ( !xScripts.is() )
138 ::rtl::OUStringBuffer buf;
139 buf.appendAscii( "Failed to create MasterScriptProvider for ScriptInvocationContext: " );
140 buf.appendAscii( "Component supporting XEmbeddScripts interface not found." );
141 throw lang::IllegalArgumentException( buf.makeStringAndClear(), NULL, 1 );
144 ::osl::MutexGuard guard( m_mutex );
146 Reference< XInterface > xNormalized( xContext, UNO_QUERY );
147 ScriptComponent_map::const_iterator pos = m_mScriptComponents.find( xNormalized );
148 if ( pos == m_mScriptComponents.end() )
150 // TODO
151 msp = createNewMSP( uno::makeAny( xContext ) );
152 addActiveMSP( xNormalized, msp );
154 else
156 msp = pos->second;
159 return msp;
162 Reference< provider::XScriptProvider >
163 ActiveMSPList::getMSPFromStringContext( const ::rtl::OUString& context )
164 SAL_THROW(( lang::IllegalArgumentException, RuntimeException ))
166 Reference< provider::XScriptProvider > msp;
169 if ( context.indexOf( OUSTR( "vnd.sun.star.tdoc" ) ) == 0 )
171 Reference< frame::XModel > xModel( MiscUtils::tDocUrlToModel( context ) );
173 Reference< document::XEmbeddedScripts > xScripts( xModel, UNO_QUERY );
174 Reference< document::XScriptInvocationContext > xScriptsContext( xModel, UNO_QUERY );
175 if ( !xScripts.is() && !xScriptsContext.is() )
177 ::rtl::OUStringBuffer buf;
178 buf.appendAscii( "Failed to create MasterScriptProvider for '" );
179 buf.append ( context );
180 buf.appendAscii( "': Either XEmbeddScripts or XScriptInvocationContext need to be supported by the document." );
181 throw lang::IllegalArgumentException( buf.makeStringAndClear(), NULL, 1 );
184 ::osl::MutexGuard guard( m_mutex );
185 Reference< XInterface > xNormalized( xModel, UNO_QUERY );
186 ScriptComponent_map::const_iterator pos = m_mScriptComponents.find( xNormalized );
187 if ( pos == m_mScriptComponents.end() )
189 msp = createNewMSP( context );
190 addActiveMSP( xNormalized, msp );
192 else
194 msp = pos->second;
197 else
199 ::osl::MutexGuard guard( m_mutex );
200 Msp_hash::iterator h_itEnd = m_hMsps.end();
201 Msp_hash::const_iterator itr = m_hMsps.find( context );
202 if ( itr == h_itEnd )
204 msp = createNewMSP( context );
205 m_hMsps[ context ] = msp;
207 else
209 msp = m_hMsps[ context ];
213 catch( const lang::IllegalArgumentException& )
215 // allowed to leave
217 catch( const RuntimeException& )
219 // allowed to leave
221 catch( const Exception& )
223 ::rtl::OUStringBuffer aMessage;
224 aMessage.appendAscii( "Failed to create MasterScriptProvider for context '" );
225 aMessage.append ( context );
226 aMessage.appendAscii( "'." );
227 throw lang::WrappedTargetRuntimeException(
228 aMessage.makeStringAndClear(), *this, ::cppu::getCaughtException() );
230 return msp;
233 void
234 ActiveMSPList::addActiveMSP( const Reference< uno::XInterface >& xComponent,
235 const Reference< provider::XScriptProvider >& msp )
237 ::osl::MutexGuard guard( m_mutex );
238 Reference< XInterface > xNormalized( xComponent, UNO_QUERY );
239 ScriptComponent_map::const_iterator pos = m_mScriptComponents.find( xNormalized );
240 if ( pos == m_mScriptComponents.end() )
242 m_mScriptComponents[ xNormalized ] = msp;
244 // add self as listener for component disposal
245 // should probably throw from this method!!, reexamine
248 Reference< lang::XComponent > xBroadcaster =
249 Reference< lang::XComponent >( xComponent, UNO_QUERY_THROW );
250 xBroadcaster->addEventListener( this );
252 catch ( const Exception& )
254 DBG_UNHANDLED_EXCEPTION();
259 //*************************************************************************
260 void SAL_CALL
261 ActiveMSPList::disposing( const ::com::sun::star::lang::EventObject& Source )
262 throw ( ::com::sun::star::uno::RuntimeException )
267 Reference< XInterface > xNormalized( Source.Source, UNO_QUERY );
268 if ( xNormalized.is() )
270 ::osl::MutexGuard guard( m_mutex );
271 ScriptComponent_map::iterator pos = m_mScriptComponents.find( xNormalized );
272 if ( pos != m_mScriptComponents.end() )
273 m_mScriptComponents.erase( pos );
276 catch ( const Exception& )
278 // if we get an exception here, there is not much we can do about
279 // it can't throw as it will screw up the model that is calling dispose
280 DBG_UNHANDLED_EXCEPTION();
285 void
286 ActiveMSPList::createNonDocMSPs()
288 static bool created = false;
289 if ( created )
291 return;
293 else
295 ::osl::MutexGuard guard( m_mutex );
296 if ( created )
298 return;
300 // do creation of user and share MSPs here
301 ::rtl::OUString serviceName = ::rtl::OUString::createFromAscii("com.sun.star.script.provider.MasterScriptProvider");
302 Sequence< Any > args(1);
304 args[ 0 ] <<= userDirString;
305 Reference< provider::XScriptProvider > userMsp( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( serviceName, args, m_xContext ), UNO_QUERY );
306 // should check if provider reference is valid
307 m_hMsps[ userDirString ] = userMsp;
309 args[ 0 ] <<= shareDirString;
310 Reference< provider::XScriptProvider > shareMsp( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( serviceName, args, m_xContext ), UNO_QUERY );
311 // should check if provider reference is valid
312 m_hMsps[ shareDirString ] = shareMsp;
313 created = true;
319 } // namespace func_provider