1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_scripting.hxx"
30*cdf0e10cSrcweir #include "basprov.hxx"
31*cdf0e10cSrcweir #include "basscript.hxx"
32*cdf0e10cSrcweir #include "baslibnode.hxx"
33*cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/script/browse/BrowseNodeTypes.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/document/XEmbeddedScripts.hpp>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #ifndef _CPPUHELPER_IMPLEMENTATIONENTRY_HXX_
39*cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx>
40*cdf0e10cSrcweir #endif
41*cdf0e10cSrcweir #include <rtl/uri.hxx>
42*cdf0e10cSrcweir #include <osl/process.h>
43*cdf0e10cSrcweir #include <osl/file.hxx>
44*cdf0e10cSrcweir #include <vos/mutex.hxx>
45*cdf0e10cSrcweir #include <vcl/svapp.hxx>
46*cdf0e10cSrcweir #include <basic/sbx.hxx>
47*cdf0e10cSrcweir #include <basic/basmgr.hxx>
48*cdf0e10cSrcweir #include <basic/basicmanagerrepository.hxx>
49*cdf0e10cSrcweir #include <basic/sbstar.hxx>
50*cdf0e10cSrcweir #include <basic/sbmod.hxx>
51*cdf0e10cSrcweir #include <basic/sbmeth.hxx>
52*cdf0e10cSrcweir #include <sfx2/app.hxx>
53*cdf0e10cSrcweir #include <sfx2/objsh.hxx>
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir #include <com/sun/star/util/XMacroExpander.hpp>
56*cdf0e10cSrcweir #include <com/sun/star/script/XLibraryContainer2.hpp>
57*cdf0e10cSrcweir #include <com/sun/star/uri/XUriReference.hpp>
58*cdf0e10cSrcweir #include <com/sun/star/uri/XUriReferenceFactory.hpp>
59*cdf0e10cSrcweir #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir #include <util/util.hxx>
62*cdf0e10cSrcweir #include <util/MiscUtils.hxx>
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir using namespace ::com::sun::star;
66*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
67*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
68*cdf0e10cSrcweir using namespace ::com::sun::star::script;
69*cdf0e10cSrcweir using namespace ::com::sun::star::document;
70*cdf0e10cSrcweir using namespace ::sf_misc;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir //.........................................................................
73*cdf0e10cSrcweir namespace basprov
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir //.........................................................................
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir     // =============================================================================
78*cdf0e10cSrcweir     // component operations
79*cdf0e10cSrcweir     // =============================================================================
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir     static ::rtl::OUString getImplementationName_BasicProviderImpl()
82*cdf0e10cSrcweir     {
83*cdf0e10cSrcweir         static ::rtl::OUString* pImplName = 0;
84*cdf0e10cSrcweir 	    if ( !pImplName )
85*cdf0e10cSrcweir 	    {
86*cdf0e10cSrcweir             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
87*cdf0e10cSrcweir             if ( !pImplName )
88*cdf0e10cSrcweir 		    {
89*cdf0e10cSrcweir                 static ::rtl::OUString aImplName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.scripting.ScriptProviderForBasic" ) );
90*cdf0e10cSrcweir 			    pImplName = &aImplName;
91*cdf0e10cSrcweir 		    }
92*cdf0e10cSrcweir 	    }
93*cdf0e10cSrcweir 	    return *pImplName;
94*cdf0e10cSrcweir     }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     static Sequence< ::rtl::OUString > getSupportedServiceNames_BasicProviderImpl()
99*cdf0e10cSrcweir     {
100*cdf0e10cSrcweir         static Sequence< ::rtl::OUString >* pNames = 0;
101*cdf0e10cSrcweir 	    if ( !pNames )
102*cdf0e10cSrcweir 	    {
103*cdf0e10cSrcweir             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
104*cdf0e10cSrcweir 		    if ( !pNames )
105*cdf0e10cSrcweir 		    {
106*cdf0e10cSrcweir                 static Sequence< ::rtl::OUString > aNames(4);
107*cdf0e10cSrcweir                 aNames.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.script.provider.ScriptProviderForBasic" ) );
108*cdf0e10cSrcweir                 aNames.getArray()[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.script.provider.LanguageScriptProvider" ) );
109*cdf0e10cSrcweir                 aNames.getArray()[2] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.script.provider.ScriptProvider" ) );
110*cdf0e10cSrcweir                 aNames.getArray()[3] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.script.browse.BrowseNode" ) );
111*cdf0e10cSrcweir                 pNames = &aNames;
112*cdf0e10cSrcweir 		    }
113*cdf0e10cSrcweir 	    }
114*cdf0e10cSrcweir 	    return *pNames;
115*cdf0e10cSrcweir     }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir     // =============================================================================
118*cdf0e10cSrcweir     // BasicProviderImpl
119*cdf0e10cSrcweir     // =============================================================================
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     BasicProviderImpl::BasicProviderImpl( const Reference< XComponentContext >& xContext )
122*cdf0e10cSrcweir         :m_pAppBasicManager( 0 )
123*cdf0e10cSrcweir         ,m_pDocBasicManager( 0 )
124*cdf0e10cSrcweir         ,m_xLibContainerApp( 0 )
125*cdf0e10cSrcweir         ,m_xLibContainerDoc( 0 )
126*cdf0e10cSrcweir         ,m_xContext( xContext )
127*cdf0e10cSrcweir         ,m_bIsAppScriptCtx( true )
128*cdf0e10cSrcweir         ,m_bIsUserCtx(true)
129*cdf0e10cSrcweir     {
130*cdf0e10cSrcweir     }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir     BasicProviderImpl::~BasicProviderImpl()
135*cdf0e10cSrcweir     {
136*cdf0e10cSrcweir     }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir     bool BasicProviderImpl::isLibraryShared( const Reference< script::XLibraryContainer >& rxLibContainer, const ::rtl::OUString& rLibName )
141*cdf0e10cSrcweir     {
142*cdf0e10cSrcweir         bool bIsShared = false;
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir         Reference< script::XLibraryContainer2 > xLibContainer( rxLibContainer, UNO_QUERY );
145*cdf0e10cSrcweir         if ( xLibContainer.is() && xLibContainer->hasByName( rLibName ) && xLibContainer->isLibraryLink( rLibName ) )
146*cdf0e10cSrcweir         {
147*cdf0e10cSrcweir             ::rtl::OUString aFileURL;
148*cdf0e10cSrcweir             if ( m_xContext.is() )
149*cdf0e10cSrcweir             {
150*cdf0e10cSrcweir                 Reference< uri::XUriReferenceFactory > xUriFac;
151*cdf0e10cSrcweir                 Reference< lang::XMultiComponentFactory > xSMgr( m_xContext->getServiceManager() );
152*cdf0e10cSrcweir                 if ( xSMgr.is() )
153*cdf0e10cSrcweir                 {
154*cdf0e10cSrcweir                     xUriFac.set( xSMgr->createInstanceWithContext( ::rtl::OUString::createFromAscii(
155*cdf0e10cSrcweir                         "com.sun.star.uri.UriReferenceFactory" ), m_xContext ), UNO_QUERY );
156*cdf0e10cSrcweir                 }
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir                 if ( xUriFac.is() )
159*cdf0e10cSrcweir                 {
160*cdf0e10cSrcweir                     ::rtl::OUString aLinkURL( xLibContainer->getLibraryLinkURL( rLibName ) );
161*cdf0e10cSrcweir                     Reference<  uri::XUriReference > xUriRef( xUriFac->parse( aLinkURL ), UNO_QUERY );
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir                     if ( xUriRef.is() )
164*cdf0e10cSrcweir                     {
165*cdf0e10cSrcweir                         ::rtl::OUString aScheme = xUriRef->getScheme();
166*cdf0e10cSrcweir                         if ( aScheme.equalsIgnoreAsciiCaseAscii( "file" ) )
167*cdf0e10cSrcweir                         {
168*cdf0e10cSrcweir                             aFileURL = aLinkURL;
169*cdf0e10cSrcweir                         }
170*cdf0e10cSrcweir                         else if ( aScheme.equalsIgnoreAsciiCaseAscii( "vnd.sun.star.pkg" ) )
171*cdf0e10cSrcweir                         {
172*cdf0e10cSrcweir                             ::rtl::OUString aAuthority = xUriRef->getAuthority();
173*cdf0e10cSrcweir                             if ( aAuthority.matchIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM( "vnd.sun.star.expand:" ) ) )
174*cdf0e10cSrcweir                             {
175*cdf0e10cSrcweir                                 ::rtl::OUString aDecodedURL( aAuthority.copy( sizeof ( "vnd.sun.star.expand:" ) - 1 ) );
176*cdf0e10cSrcweir                                 aDecodedURL = ::rtl::Uri::decode( aDecodedURL, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
177*cdf0e10cSrcweir                                 Reference<util::XMacroExpander> xMacroExpander(
178*cdf0e10cSrcweir                                     m_xContext->getValueByName(
179*cdf0e10cSrcweir                                     ::rtl::OUString::createFromAscii( "/singletons/com.sun.star.util.theMacroExpander" ) ),
180*cdf0e10cSrcweir                                     UNO_QUERY );
181*cdf0e10cSrcweir                                 if ( xMacroExpander.is() )
182*cdf0e10cSrcweir                                     aFileURL = xMacroExpander->expandMacros( aDecodedURL );
183*cdf0e10cSrcweir                             }
184*cdf0e10cSrcweir                         }
185*cdf0e10cSrcweir                     }
186*cdf0e10cSrcweir                 }
187*cdf0e10cSrcweir             }
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir             if ( aFileURL.getLength() )
190*cdf0e10cSrcweir             {
191*cdf0e10cSrcweir                 osl::DirectoryItem aFileItem;
192*cdf0e10cSrcweir                 osl::FileStatus aFileStatus( FileStatusMask_FileURL );
193*cdf0e10cSrcweir                 OSL_VERIFY( osl::DirectoryItem::get( aFileURL, aFileItem ) == osl::FileBase::E_None );
194*cdf0e10cSrcweir                 OSL_VERIFY( aFileItem.getFileStatus( aFileStatus ) == osl::FileBase::E_None );
195*cdf0e10cSrcweir                 ::rtl::OUString aCanonicalFileURL( aFileStatus.getFileURL() );
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir                 ::rtl::OUString aSearchURL1( RTL_CONSTASCII_USTRINGPARAM( "share/basic" ) );
198*cdf0e10cSrcweir 				::rtl::OUString aSearchURL2( RTL_CONSTASCII_USTRINGPARAM( "share/uno_packages" ) );
199*cdf0e10cSrcweir 				if( aCanonicalFileURL.indexOf( aSearchURL1 ) != -1 || aCanonicalFileURL.indexOf( aSearchURL2 ) != -1 )
200*cdf0e10cSrcweir                     bIsShared = true;
201*cdf0e10cSrcweir             }
202*cdf0e10cSrcweir         }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir         return bIsShared;
205*cdf0e10cSrcweir     }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
208*cdf0e10cSrcweir     // XServiceInfo
209*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir     ::rtl::OUString BasicProviderImpl::getImplementationName(  ) throw (RuntimeException)
212*cdf0e10cSrcweir     {
213*cdf0e10cSrcweir         return getImplementationName_BasicProviderImpl();
214*cdf0e10cSrcweir     }
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir     sal_Bool BasicProviderImpl::supportsService( const ::rtl::OUString& rServiceName ) throw (RuntimeException)
219*cdf0e10cSrcweir     {
220*cdf0e10cSrcweir 	    Sequence< ::rtl::OUString > aNames( getSupportedServiceNames() );
221*cdf0e10cSrcweir 	    const ::rtl::OUString* pNames = aNames.getConstArray();
222*cdf0e10cSrcweir 	    const ::rtl::OUString* pEnd = pNames + aNames.getLength();
223*cdf0e10cSrcweir 	    for ( ; pNames != pEnd && !pNames->equals( rServiceName ); ++pNames )
224*cdf0e10cSrcweir 		    ;
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 	    return pNames != pEnd;
227*cdf0e10cSrcweir     }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir     Sequence< ::rtl::OUString > BasicProviderImpl::getSupportedServiceNames(  ) throw (RuntimeException)
232*cdf0e10cSrcweir     {
233*cdf0e10cSrcweir         return getSupportedServiceNames_BasicProviderImpl();
234*cdf0e10cSrcweir     }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
237*cdf0e10cSrcweir     // XInitialization
238*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir     void BasicProviderImpl::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException)
241*cdf0e10cSrcweir     {
242*cdf0e10cSrcweir         // TODO
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir         ::vos::OGuard aGuard( Application::GetSolarMutex() );
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir         if ( aArguments.getLength() != 1 )
247*cdf0e10cSrcweir         {
248*cdf0e10cSrcweir             throw IllegalArgumentException(
249*cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BasicProviderImpl::initialize: incorrect argument count." ) ),
250*cdf0e10cSrcweir                 *this,
251*cdf0e10cSrcweir                 1
252*cdf0e10cSrcweir             );
253*cdf0e10cSrcweir         }
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir         Reference< frame::XModel > xModel;
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir         m_xInvocationContext.set( aArguments[0], UNO_QUERY );;
258*cdf0e10cSrcweir         if ( m_xInvocationContext.is() )
259*cdf0e10cSrcweir         {
260*cdf0e10cSrcweir             xModel.set( m_xInvocationContext->getScriptContainer(), UNO_QUERY );
261*cdf0e10cSrcweir             if ( !xModel.is() )
262*cdf0e10cSrcweir             {
263*cdf0e10cSrcweir                 throw IllegalArgumentException(
264*cdf0e10cSrcweir                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BasicProviderImpl::initialize: unable to determine the document model from the script invocation context." ) ),
265*cdf0e10cSrcweir                     *this,
266*cdf0e10cSrcweir                     1
267*cdf0e10cSrcweir                 );
268*cdf0e10cSrcweir             }
269*cdf0e10cSrcweir         }
270*cdf0e10cSrcweir         else
271*cdf0e10cSrcweir         {
272*cdf0e10cSrcweir             if ( !( aArguments[0] >>= m_sScriptingContext ) )
273*cdf0e10cSrcweir             {
274*cdf0e10cSrcweir                 throw IllegalArgumentException(
275*cdf0e10cSrcweir                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BasicProviderImpl::initialize: incorrect argument type " )  ).concat(  aArguments[0].getValueTypeName() ),
276*cdf0e10cSrcweir                     *this,
277*cdf0e10cSrcweir                     1
278*cdf0e10cSrcweir                 );
279*cdf0e10cSrcweir             }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir             ::rtl::OUString sDoc = OUSTR("vnd.sun.star.tdoc");
282*cdf0e10cSrcweir             if ( m_sScriptingContext.indexOf( sDoc  ) == 0 )
283*cdf0e10cSrcweir             {
284*cdf0e10cSrcweir                 xModel = MiscUtils::tDocUrlToModel(  m_sScriptingContext );
285*cdf0e10cSrcweir                 // TODO: use ScriptingContantsPool for SCRIPTING_DOC_REF
286*cdf0e10cSrcweir             }
287*cdf0e10cSrcweir         }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir         if ( xModel.is() )
290*cdf0e10cSrcweir         {
291*cdf0e10cSrcweir             Reference< XEmbeddedScripts > xDocumentScripts( xModel, UNO_QUERY );
292*cdf0e10cSrcweir             if ( xDocumentScripts.is() )
293*cdf0e10cSrcweir             {
294*cdf0e10cSrcweir                 m_pDocBasicManager = ::basic::BasicManagerRepository::getDocumentBasicManager( xModel );
295*cdf0e10cSrcweir                 m_xLibContainerDoc.set( xDocumentScripts->getBasicLibraries(), UNO_QUERY );
296*cdf0e10cSrcweir                 OSL_ENSURE( m_pDocBasicManager && m_xLibContainerDoc.is(),
297*cdf0e10cSrcweir                     "BasicProviderImpl::initialize: invalid BasicManager, or invalid script container!" );
298*cdf0e10cSrcweir             }
299*cdf0e10cSrcweir             m_bIsAppScriptCtx = false;
300*cdf0e10cSrcweir         }
301*cdf0e10cSrcweir         else
302*cdf0e10cSrcweir         {
303*cdf0e10cSrcweir             // Provider has been created with application context for user
304*cdf0e10cSrcweir             // or share
305*cdf0e10cSrcweir             if ( !m_sScriptingContext.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "user" ) ) )
306*cdf0e10cSrcweir             {
307*cdf0e10cSrcweir                 m_bIsUserCtx = false;
308*cdf0e10cSrcweir             }
309*cdf0e10cSrcweir             else
310*cdf0e10cSrcweir             {
311*cdf0e10cSrcweir                 /*
312*cdf0e10cSrcweir                 throw RuntimeException(
313*cdf0e10cSrcweir                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BasicProviderImpl::initialize: no scripting context!" ) ),
314*cdf0e10cSrcweir                     Reference< XInterface >() );
315*cdf0e10cSrcweir                 */
316*cdf0e10cSrcweir             }
317*cdf0e10cSrcweir         }
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir         // TODO
320*cdf0e10cSrcweir         if ( !m_pAppBasicManager )
321*cdf0e10cSrcweir             m_pAppBasicManager = SFX_APP()->GetBasicManager();
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir         if ( !m_xLibContainerApp.is() )
324*cdf0e10cSrcweir             m_xLibContainerApp = Reference< script::XLibraryContainer >( SFX_APP()->GetBasicContainer(), UNO_QUERY );
325*cdf0e10cSrcweir     }
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir     // XScriptProvider
330*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir     Reference < provider::XScript > BasicProviderImpl::getScript( const ::rtl::OUString& scriptURI )
333*cdf0e10cSrcweir         throw ( provider::ScriptFrameworkErrorException, RuntimeException)
334*cdf0e10cSrcweir     {
335*cdf0e10cSrcweir         // TODO
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir         ::vos::OGuard aGuard( Application::GetSolarMutex() );
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir         Reference< provider::XScript > xScript;
340*cdf0e10cSrcweir         Reference< lang::XMultiComponentFactory > xMcFac ( m_xContext->getServiceManager() );
341*cdf0e10cSrcweir         Reference< uri::XUriReferenceFactory > xFac (
342*cdf0e10cSrcweir             xMcFac->createInstanceWithContext( rtl::OUString::createFromAscii(
343*cdf0e10cSrcweir             "com.sun.star.uri.UriReferenceFactory"), m_xContext ) , UNO_QUERY );
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir         if ( !xFac.is() )
346*cdf0e10cSrcweir         {
347*cdf0e10cSrcweir             throw provider::ScriptFrameworkErrorException(
348*cdf0e10cSrcweir                 OUSTR( "Failed to instantiate UriReferenceFactory" ), Reference< XInterface >(),
349*cdf0e10cSrcweir                 scriptURI, OUSTR("Basic"),
350*cdf0e10cSrcweir                 provider::ScriptFrameworkErrorType::UNKNOWN );
351*cdf0e10cSrcweir         }
352*cdf0e10cSrcweir 
353*cdf0e10cSrcweir         Reference<  uri::XUriReference > uriRef(
354*cdf0e10cSrcweir             xFac->parse( scriptURI ), UNO_QUERY );
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir         Reference < uri::XVndSunStarScriptUrl > sfUri( uriRef, UNO_QUERY );
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir         if ( !uriRef.is() || !sfUri.is() )
359*cdf0e10cSrcweir         {
360*cdf0e10cSrcweir             ::rtl::OUString errorMsg = ::rtl::OUString::createFromAscii( "BasicProviderImpl::getScript: failed to parse URI: " );
361*cdf0e10cSrcweir             errorMsg = errorMsg.concat( scriptURI );
362*cdf0e10cSrcweir             throw provider::ScriptFrameworkErrorException(
363*cdf0e10cSrcweir                 errorMsg, Reference< XInterface >(),
364*cdf0e10cSrcweir                 scriptURI, OUSTR("Basic"),
365*cdf0e10cSrcweir                 provider::ScriptFrameworkErrorType::MALFORMED_URL );
366*cdf0e10cSrcweir         }
367*cdf0e10cSrcweir 
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir         ::rtl::OUString aDescription = sfUri->getName();
370*cdf0e10cSrcweir         ::rtl::OUString aLocation = sfUri->getParameter(
371*cdf0e10cSrcweir             ::rtl::OUString::createFromAscii( "location" ) );
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir         sal_Int32 nIndex = 0;
374*cdf0e10cSrcweir         ::rtl::OUString aLibrary = aDescription.getToken( 0, (sal_Unicode)'.', nIndex );
375*cdf0e10cSrcweir         ::rtl::OUString aModule;
376*cdf0e10cSrcweir         if ( nIndex != -1 )
377*cdf0e10cSrcweir             aModule = aDescription.getToken( 0, (sal_Unicode)'.', nIndex );
378*cdf0e10cSrcweir         ::rtl::OUString aMethod;
379*cdf0e10cSrcweir         if ( nIndex != -1 )
380*cdf0e10cSrcweir             aMethod = aDescription.getToken( 0, (sal_Unicode)'.', nIndex );
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir         if ( aLibrary.getLength() != 0 && aModule.getLength() != 0 && aMethod.getLength() != 0 && aLocation.getLength() != 0 )
383*cdf0e10cSrcweir         {
384*cdf0e10cSrcweir             BasicManager* pBasicMgr =  NULL;
385*cdf0e10cSrcweir             if ( aLocation.equals( ::rtl::OUString::createFromAscii("document") ) )
386*cdf0e10cSrcweir             {
387*cdf0e10cSrcweir                 pBasicMgr = m_pDocBasicManager;
388*cdf0e10cSrcweir             }
389*cdf0e10cSrcweir             else if ( aLocation.equals( ::rtl::OUString::createFromAscii("application") ) )
390*cdf0e10cSrcweir             {
391*cdf0e10cSrcweir                 pBasicMgr = m_pAppBasicManager;
392*cdf0e10cSrcweir             }
393*cdf0e10cSrcweir 
394*cdf0e10cSrcweir             if ( pBasicMgr )
395*cdf0e10cSrcweir             {
396*cdf0e10cSrcweir                 StarBASIC* pBasic = pBasicMgr->GetLib( aLibrary );
397*cdf0e10cSrcweir                 if ( !pBasic )
398*cdf0e10cSrcweir                 {
399*cdf0e10cSrcweir                     sal_uInt16 nId = pBasicMgr->GetLibId( aLibrary );
400*cdf0e10cSrcweir                     if ( nId != LIB_NOTFOUND )
401*cdf0e10cSrcweir                     {
402*cdf0e10cSrcweir                         pBasicMgr->LoadLib( nId );
403*cdf0e10cSrcweir                         pBasic = pBasicMgr->GetLib( aLibrary );
404*cdf0e10cSrcweir                     }
405*cdf0e10cSrcweir                 }
406*cdf0e10cSrcweir                 if ( pBasic )
407*cdf0e10cSrcweir                 {
408*cdf0e10cSrcweir                     SbModule* pModule = pBasic->FindModule( aModule );
409*cdf0e10cSrcweir                     if ( pModule )
410*cdf0e10cSrcweir                     {
411*cdf0e10cSrcweir                         SbxArray* pMethods = pModule->GetMethods();
412*cdf0e10cSrcweir                         if ( pMethods )
413*cdf0e10cSrcweir                         {
414*cdf0e10cSrcweir                             SbMethod* pMethod = static_cast< SbMethod* >( pMethods->Find( aMethod, SbxCLASS_METHOD ) );
415*cdf0e10cSrcweir                             if ( pMethod && !pMethod->IsHidden() )
416*cdf0e10cSrcweir                             {
417*cdf0e10cSrcweir                                 if ( m_pDocBasicManager == pBasicMgr )
418*cdf0e10cSrcweir                                     xScript = new BasicScriptImpl( aDescription, pMethod, *m_pDocBasicManager, m_xInvocationContext );
419*cdf0e10cSrcweir                                 else
420*cdf0e10cSrcweir                                     xScript = new BasicScriptImpl( aDescription, pMethod );
421*cdf0e10cSrcweir                             }
422*cdf0e10cSrcweir                         }
423*cdf0e10cSrcweir                     }
424*cdf0e10cSrcweir                 }
425*cdf0e10cSrcweir             }
426*cdf0e10cSrcweir         }
427*cdf0e10cSrcweir 
428*cdf0e10cSrcweir         if ( !xScript.is() )
429*cdf0e10cSrcweir         {
430*cdf0e10cSrcweir             ::rtl::OUStringBuffer aMessage;
431*cdf0e10cSrcweir             aMessage.appendAscii( "The following Basic script could not be found:\n" );
432*cdf0e10cSrcweir             aMessage.appendAscii( "library: '" ).append( aLibrary ).appendAscii( "'\n" );
433*cdf0e10cSrcweir             aMessage.appendAscii( "module: '" ).append( aModule ).appendAscii( "'\n" );
434*cdf0e10cSrcweir             aMessage.appendAscii( "method: '" ).append( aMethod ).appendAscii( "'\n" );
435*cdf0e10cSrcweir             aMessage.appendAscii( "location: '" ).append( aLocation ).appendAscii( "'\n" );
436*cdf0e10cSrcweir             throw provider::ScriptFrameworkErrorException(
437*cdf0e10cSrcweir                 aMessage.makeStringAndClear(),
438*cdf0e10cSrcweir                 Reference< XInterface >(),
439*cdf0e10cSrcweir                 scriptURI, OUSTR("Basic"),
440*cdf0e10cSrcweir                 provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT );
441*cdf0e10cSrcweir         }
442*cdf0e10cSrcweir 
443*cdf0e10cSrcweir         return xScript;
444*cdf0e10cSrcweir     }
445*cdf0e10cSrcweir 
446*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
447*cdf0e10cSrcweir     // XBrowseNode
448*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
449*cdf0e10cSrcweir 
450*cdf0e10cSrcweir     ::rtl::OUString BasicProviderImpl::getName(  ) throw (RuntimeException)
451*cdf0e10cSrcweir     {
452*cdf0e10cSrcweir         // TODO
453*cdf0e10cSrcweir 
454*cdf0e10cSrcweir         ::vos::OGuard aGuard( Application::GetSolarMutex() );
455*cdf0e10cSrcweir 
456*cdf0e10cSrcweir         return ::rtl::OUString::createFromAscii( "Basic" );
457*cdf0e10cSrcweir     }
458*cdf0e10cSrcweir 
459*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
460*cdf0e10cSrcweir 
461*cdf0e10cSrcweir     Sequence< Reference< browse::XBrowseNode > > BasicProviderImpl::getChildNodes(  ) throw (RuntimeException)
462*cdf0e10cSrcweir     {
463*cdf0e10cSrcweir         ::vos::OGuard aGuard( Application::GetSolarMutex() );
464*cdf0e10cSrcweir 
465*cdf0e10cSrcweir         Reference< script::XLibraryContainer > xLibContainer;
466*cdf0e10cSrcweir         BasicManager* pBasicManager = NULL;
467*cdf0e10cSrcweir 
468*cdf0e10cSrcweir         if ( m_bIsAppScriptCtx )
469*cdf0e10cSrcweir         {
470*cdf0e10cSrcweir             xLibContainer = m_xLibContainerApp;
471*cdf0e10cSrcweir             pBasicManager = m_pAppBasicManager;
472*cdf0e10cSrcweir         }
473*cdf0e10cSrcweir         else
474*cdf0e10cSrcweir         {
475*cdf0e10cSrcweir             xLibContainer = m_xLibContainerDoc;
476*cdf0e10cSrcweir             pBasicManager = m_pDocBasicManager;
477*cdf0e10cSrcweir         }
478*cdf0e10cSrcweir 
479*cdf0e10cSrcweir         Sequence< Reference< browse::XBrowseNode > > aChildNodes;
480*cdf0e10cSrcweir 
481*cdf0e10cSrcweir         if ( pBasicManager && xLibContainer.is() )
482*cdf0e10cSrcweir         {
483*cdf0e10cSrcweir             Sequence< ::rtl::OUString > aLibNames = xLibContainer->getElementNames();
484*cdf0e10cSrcweir             sal_Int32 nLibCount = aLibNames.getLength();
485*cdf0e10cSrcweir             const ::rtl::OUString* pLibNames = aLibNames.getConstArray();
486*cdf0e10cSrcweir             aChildNodes.realloc( nLibCount );
487*cdf0e10cSrcweir             Reference< browse::XBrowseNode >* pChildNodes = aChildNodes.getArray();
488*cdf0e10cSrcweir             sal_Int32 childsFound = 0;
489*cdf0e10cSrcweir 
490*cdf0e10cSrcweir             for ( sal_Int32 i = 0 ; i < nLibCount ; ++i )
491*cdf0e10cSrcweir             {
492*cdf0e10cSrcweir                 bool bCreate = false;
493*cdf0e10cSrcweir                 if ( m_bIsAppScriptCtx )
494*cdf0e10cSrcweir                 {
495*cdf0e10cSrcweir                     bool bShared = isLibraryShared( xLibContainer, pLibNames[i] );
496*cdf0e10cSrcweir                     if ( ( m_bIsUserCtx && !bShared ) || ( !m_bIsUserCtx && bShared ) )
497*cdf0e10cSrcweir                         bCreate = true;
498*cdf0e10cSrcweir                 }
499*cdf0e10cSrcweir                 else
500*cdf0e10cSrcweir                 {
501*cdf0e10cSrcweir                     bCreate = true;
502*cdf0e10cSrcweir                 }
503*cdf0e10cSrcweir                 if ( bCreate )
504*cdf0e10cSrcweir                 {
505*cdf0e10cSrcweir                     pChildNodes[childsFound++] = static_cast< browse::XBrowseNode* >( new BasicLibraryNodeImpl(
506*cdf0e10cSrcweir                         m_xContext, m_sScriptingContext, pBasicManager, xLibContainer, pLibNames[i], m_bIsAppScriptCtx ) );
507*cdf0e10cSrcweir                 }
508*cdf0e10cSrcweir             }
509*cdf0e10cSrcweir 
510*cdf0e10cSrcweir             if ( childsFound != nLibCount )
511*cdf0e10cSrcweir                 aChildNodes.realloc( childsFound );
512*cdf0e10cSrcweir         }
513*cdf0e10cSrcweir 
514*cdf0e10cSrcweir         return aChildNodes;
515*cdf0e10cSrcweir     }
516*cdf0e10cSrcweir 
517*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
518*cdf0e10cSrcweir 
519*cdf0e10cSrcweir     sal_Bool BasicProviderImpl::hasChildNodes(  ) throw (RuntimeException)
520*cdf0e10cSrcweir     {
521*cdf0e10cSrcweir         ::vos::OGuard aGuard( Application::GetSolarMutex() );
522*cdf0e10cSrcweir 
523*cdf0e10cSrcweir         sal_Bool bReturn = sal_False;
524*cdf0e10cSrcweir         Reference< script::XLibraryContainer > xLibContainer;
525*cdf0e10cSrcweir         if ( m_bIsAppScriptCtx )
526*cdf0e10cSrcweir         {
527*cdf0e10cSrcweir             xLibContainer = m_xLibContainerApp;
528*cdf0e10cSrcweir         }
529*cdf0e10cSrcweir         else
530*cdf0e10cSrcweir         {
531*cdf0e10cSrcweir              xLibContainer = m_xLibContainerDoc;
532*cdf0e10cSrcweir         }
533*cdf0e10cSrcweir         if ( xLibContainer.is() )
534*cdf0e10cSrcweir             bReturn = xLibContainer->hasElements();
535*cdf0e10cSrcweir 
536*cdf0e10cSrcweir         return bReturn;
537*cdf0e10cSrcweir     }
538*cdf0e10cSrcweir 
539*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
540*cdf0e10cSrcweir 
541*cdf0e10cSrcweir     sal_Int16 BasicProviderImpl::getType(  ) throw (RuntimeException)
542*cdf0e10cSrcweir     {
543*cdf0e10cSrcweir         ::vos::OGuard aGuard( Application::GetSolarMutex() );
544*cdf0e10cSrcweir 
545*cdf0e10cSrcweir         return browse::BrowseNodeTypes::CONTAINER;
546*cdf0e10cSrcweir     }
547*cdf0e10cSrcweir 
548*cdf0e10cSrcweir     // =============================================================================
549*cdf0e10cSrcweir     // component operations
550*cdf0e10cSrcweir     // =============================================================================
551*cdf0e10cSrcweir 
552*cdf0e10cSrcweir     static Reference< XInterface > SAL_CALL create_BasicProviderImpl(
553*cdf0e10cSrcweir         Reference< XComponentContext > const & xContext )
554*cdf0e10cSrcweir         SAL_THROW( () )
555*cdf0e10cSrcweir     {
556*cdf0e10cSrcweir         return static_cast< lang::XTypeProvider * >( new BasicProviderImpl( xContext ) );
557*cdf0e10cSrcweir     }
558*cdf0e10cSrcweir 
559*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
560*cdf0e10cSrcweir 
561*cdf0e10cSrcweir     static struct ::cppu::ImplementationEntry s_component_entries [] =
562*cdf0e10cSrcweir     {
563*cdf0e10cSrcweir         {
564*cdf0e10cSrcweir             create_BasicProviderImpl, getImplementationName_BasicProviderImpl,
565*cdf0e10cSrcweir             getSupportedServiceNames_BasicProviderImpl, ::cppu::createSingleComponentFactory,
566*cdf0e10cSrcweir             0, 0
567*cdf0e10cSrcweir         },
568*cdf0e10cSrcweir         { 0, 0, 0, 0, 0, 0 }
569*cdf0e10cSrcweir     };
570*cdf0e10cSrcweir 
571*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
572*cdf0e10cSrcweir 
573*cdf0e10cSrcweir //.........................................................................
574*cdf0e10cSrcweir }	// namespace basprov
575*cdf0e10cSrcweir //.........................................................................
576*cdf0e10cSrcweir 
577*cdf0e10cSrcweir 
578*cdf0e10cSrcweir // =============================================================================
579*cdf0e10cSrcweir // component exports
580*cdf0e10cSrcweir // =============================================================================
581*cdf0e10cSrcweir 
582*cdf0e10cSrcweir extern "C"
583*cdf0e10cSrcweir {
584*cdf0e10cSrcweir     SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
585*cdf0e10cSrcweir         const sal_Char ** ppEnvTypeName, uno_Environment ** ppEnv )
586*cdf0e10cSrcweir     {
587*cdf0e10cSrcweir 		(void)ppEnv;
588*cdf0e10cSrcweir 
589*cdf0e10cSrcweir         *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
590*cdf0e10cSrcweir     }
591*cdf0e10cSrcweir 
592*cdf0e10cSrcweir     SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
593*cdf0e10cSrcweir         const sal_Char * pImplName, lang::XMultiServiceFactory * pServiceManager,
594*cdf0e10cSrcweir         registry::XRegistryKey * pRegistryKey )
595*cdf0e10cSrcweir     {
596*cdf0e10cSrcweir         return ::cppu::component_getFactoryHelper(
597*cdf0e10cSrcweir             pImplName, pServiceManager, pRegistryKey, ::basprov::s_component_entries );
598*cdf0e10cSrcweir     }
599*cdf0e10cSrcweir }
600