xref: /aoo42x/main/tools/source/generic/svlibrary.cxx (revision 89b56da7)
1*89b56da7SAndrew Rist /**************************************************************
2*89b56da7SAndrew Rist  *
3*89b56da7SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*89b56da7SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*89b56da7SAndrew Rist  * distributed with this work for additional information
6*89b56da7SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*89b56da7SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*89b56da7SAndrew Rist  * "License"); you may not use this file except in compliance
9*89b56da7SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*89b56da7SAndrew Rist  *
11*89b56da7SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*89b56da7SAndrew Rist  *
13*89b56da7SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*89b56da7SAndrew Rist  * software distributed under the License is distributed on an
15*89b56da7SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*89b56da7SAndrew Rist  * KIND, either express or implied.  See the License for the
17*89b56da7SAndrew Rist  * specific language governing permissions and limitations
18*89b56da7SAndrew Rist  * under the License.
19*89b56da7SAndrew Rist  *
20*89b56da7SAndrew Rist  *************************************************************/
21*89b56da7SAndrew Rist 
22*89b56da7SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_tools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <tools/svlibrary.hxx>
28cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
29cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx>
30cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
31cdf0e10cSrcweir #include <com/sun/star/util/XMacroExpander.hpp>
32cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
34cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
35cdf0e10cSrcweir #include <tools/string.hxx>
36cdf0e10cSrcweir #include <rtl/uri.hxx>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using namespace com::sun::star;
39cdf0e10cSrcweir 
GetMultiPaths_Impl()40cdf0e10cSrcweir static uno::Sequence< rtl::OUString > GetMultiPaths_Impl()
41cdf0e10cSrcweir {
42cdf0e10cSrcweir     uno::Sequence< rtl::OUString >   aRes;
43cdf0e10cSrcweir     uno::Sequence< rtl::OUString >   aInternalPaths;
44cdf0e10cSrcweir     uno::Sequence< rtl::OUString >   aUserPaths;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir     bool bSuccess = true;
47cdf0e10cSrcweir     uno::Reference< lang::XMultiServiceFactory >  xMgr( comphelper::getProcessServiceFactory() );
48cdf0e10cSrcweir     if (xMgr.is())
49cdf0e10cSrcweir     {
50cdf0e10cSrcweir         try
51cdf0e10cSrcweir         {
52cdf0e10cSrcweir             String aInternal;
53cdf0e10cSrcweir 			aInternal.AppendAscii("Libraries");
54cdf0e10cSrcweir             String aUser;
55cdf0e10cSrcweir 			aUser.AppendAscii("Libraries");
56cdf0e10cSrcweir             aInternal .AppendAscii( "_internal" );
57cdf0e10cSrcweir             aUser     .AppendAscii( "_user" );
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 			uno::Reference< beans::XPropertySet > xPathSettings( xMgr->createInstance(
60cdf0e10cSrcweir 				rtl::OUString::createFromAscii( "com.sun.star.util.PathSettings" ) ), uno::UNO_QUERY_THROW );
61cdf0e10cSrcweir             xPathSettings->getPropertyValue( aInternal )  >>= aInternalPaths;
62cdf0e10cSrcweir             xPathSettings->getPropertyValue( aUser )      >>= aUserPaths;
63cdf0e10cSrcweir         }
64cdf0e10cSrcweir         catch (uno::Exception &)
65cdf0e10cSrcweir         {
66cdf0e10cSrcweir             bSuccess = false;
67cdf0e10cSrcweir         }
68cdf0e10cSrcweir     }
69cdf0e10cSrcweir     if (bSuccess)
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir         sal_Int32 nMaxEntries = aInternalPaths.getLength() + aUserPaths.getLength();
72cdf0e10cSrcweir         aRes.realloc( nMaxEntries );
73cdf0e10cSrcweir         rtl::OUString *pRes = aRes.getArray();
74cdf0e10cSrcweir         sal_Int32 nCount = 0;   // number of actually added entries
75cdf0e10cSrcweir         for (int i = 0;  i < 2;  ++i)
76cdf0e10cSrcweir         {
77cdf0e10cSrcweir             const uno::Sequence< rtl::OUString > &rPathSeq = i == 0 ? aUserPaths : aInternalPaths;
78cdf0e10cSrcweir             const rtl::OUString *pPathSeq = rPathSeq.getConstArray();
79cdf0e10cSrcweir             for (sal_Int32 k = 0;  k < rPathSeq.getLength();  ++k)
80cdf0e10cSrcweir             {
81cdf0e10cSrcweir                 const bool bAddUser     = (&rPathSeq == &aUserPaths);
82cdf0e10cSrcweir                 const bool bAddInternal = (&rPathSeq == &aInternalPaths);
83cdf0e10cSrcweir                 if ((bAddUser || bAddInternal) && pPathSeq[k].getLength() > 0)
84cdf0e10cSrcweir                     pRes[ nCount++ ] = pPathSeq[k];
85cdf0e10cSrcweir             }
86cdf0e10cSrcweir         }
87cdf0e10cSrcweir         aRes.realloc( nCount );
88cdf0e10cSrcweir     }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     return aRes;
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
LoadModule(osl::Module & rModule,const rtl::OUString & rLibName,::oslGenericFunction baseModule,::sal_Int32 mode)93cdf0e10cSrcweir bool SvLibrary::LoadModule( osl::Module& rModule, const rtl::OUString& rLibName, ::oslGenericFunction baseModule, ::sal_Int32 mode )
94cdf0e10cSrcweir {
95cdf0e10cSrcweir 	static uno::Sequence < rtl::OUString > aPaths = GetMultiPaths_Impl();
96cdf0e10cSrcweir 	bool bLoaded = false;
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 	for (sal_Int32 n=0; n<aPaths.getLength(); n++)
99cdf0e10cSrcweir 	{
100cdf0e10cSrcweir 		rtl::OUString aMod = aPaths[n];
101cdf0e10cSrcweir 		if ( aPaths[n].indexOfAsciiL("vnd.sun.star.expand",19) == 0)
102cdf0e10cSrcweir 		{
103cdf0e10cSrcweir 			uno::Reference< uno::XComponentContext > xComponentContext = comphelper::getProcessComponentContext();
104cdf0e10cSrcweir 			uno::Reference< util::XMacroExpander > xMacroExpander;
105cdf0e10cSrcweir 			xComponentContext->getValueByName(
106cdf0e10cSrcweir 				::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("/singletons/com.sun.star.util.theMacroExpander") ) )
107cdf0e10cSrcweir 					>>= xMacroExpander;
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 			aMod = aMod.copy( sizeof("vnd.sun.star.expand:") -1 );
110cdf0e10cSrcweir 			aMod = ::rtl::Uri::decode( aMod, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
111cdf0e10cSrcweir 			aMod = xMacroExpander->expandMacros( aMod );
112cdf0e10cSrcweir 		}
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 		aMod += ::rtl::OUString( sal_Unicode('/') );
115cdf0e10cSrcweir 		aMod += rLibName;
116cdf0e10cSrcweir 		bLoaded = rModule.load( aMod, mode );
117cdf0e10cSrcweir 		if ( bLoaded )
118cdf0e10cSrcweir 			break;
119cdf0e10cSrcweir 	}
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 	if (!bLoaded )
122cdf0e10cSrcweir 		bLoaded = rModule.loadRelative( baseModule, rLibName, mode );
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 	return bLoaded;
125cdf0e10cSrcweir }
126