xref: /aoo41x/main/tools/source/generic/svlibrary.cxx (revision 89b56da7)
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_tools.hxx"
26 
27 #include <tools/svlibrary.hxx>
28 #include <com/sun/star/uno/Sequence.hxx>
29 #include <com/sun/star/uno/Reference.hxx>
30 #include <com/sun/star/uno/XComponentContext.hpp>
31 #include <com/sun/star/util/XMacroExpander.hpp>
32 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <comphelper/processfactory.hxx>
35 #include <tools/string.hxx>
36 #include <rtl/uri.hxx>
37 
38 using namespace com::sun::star;
39 
GetMultiPaths_Impl()40 static uno::Sequence< rtl::OUString > GetMultiPaths_Impl()
41 {
42     uno::Sequence< rtl::OUString >   aRes;
43     uno::Sequence< rtl::OUString >   aInternalPaths;
44     uno::Sequence< rtl::OUString >   aUserPaths;
45 
46     bool bSuccess = true;
47     uno::Reference< lang::XMultiServiceFactory >  xMgr( comphelper::getProcessServiceFactory() );
48     if (xMgr.is())
49     {
50         try
51         {
52             String aInternal;
53 			aInternal.AppendAscii("Libraries");
54             String aUser;
55 			aUser.AppendAscii("Libraries");
56             aInternal .AppendAscii( "_internal" );
57             aUser     .AppendAscii( "_user" );
58 
59 			uno::Reference< beans::XPropertySet > xPathSettings( xMgr->createInstance(
60 				rtl::OUString::createFromAscii( "com.sun.star.util.PathSettings" ) ), uno::UNO_QUERY_THROW );
61             xPathSettings->getPropertyValue( aInternal )  >>= aInternalPaths;
62             xPathSettings->getPropertyValue( aUser )      >>= aUserPaths;
63         }
64         catch (uno::Exception &)
65         {
66             bSuccess = false;
67         }
68     }
69     if (bSuccess)
70     {
71         sal_Int32 nMaxEntries = aInternalPaths.getLength() + aUserPaths.getLength();
72         aRes.realloc( nMaxEntries );
73         rtl::OUString *pRes = aRes.getArray();
74         sal_Int32 nCount = 0;   // number of actually added entries
75         for (int i = 0;  i < 2;  ++i)
76         {
77             const uno::Sequence< rtl::OUString > &rPathSeq = i == 0 ? aUserPaths : aInternalPaths;
78             const rtl::OUString *pPathSeq = rPathSeq.getConstArray();
79             for (sal_Int32 k = 0;  k < rPathSeq.getLength();  ++k)
80             {
81                 const bool bAddUser     = (&rPathSeq == &aUserPaths);
82                 const bool bAddInternal = (&rPathSeq == &aInternalPaths);
83                 if ((bAddUser || bAddInternal) && pPathSeq[k].getLength() > 0)
84                     pRes[ nCount++ ] = pPathSeq[k];
85             }
86         }
87         aRes.realloc( nCount );
88     }
89 
90     return aRes;
91 }
92 
LoadModule(osl::Module & rModule,const rtl::OUString & rLibName,::oslGenericFunction baseModule,::sal_Int32 mode)93 bool SvLibrary::LoadModule( osl::Module& rModule, const rtl::OUString& rLibName, ::oslGenericFunction baseModule, ::sal_Int32 mode )
94 {
95 	static uno::Sequence < rtl::OUString > aPaths = GetMultiPaths_Impl();
96 	bool bLoaded = false;
97 
98 	for (sal_Int32 n=0; n<aPaths.getLength(); n++)
99 	{
100 		rtl::OUString aMod = aPaths[n];
101 		if ( aPaths[n].indexOfAsciiL("vnd.sun.star.expand",19) == 0)
102 		{
103 			uno::Reference< uno::XComponentContext > xComponentContext = comphelper::getProcessComponentContext();
104 			uno::Reference< util::XMacroExpander > xMacroExpander;
105 			xComponentContext->getValueByName(
106 				::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("/singletons/com.sun.star.util.theMacroExpander") ) )
107 					>>= xMacroExpander;
108 
109 			aMod = aMod.copy( sizeof("vnd.sun.star.expand:") -1 );
110 			aMod = ::rtl::Uri::decode( aMod, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
111 			aMod = xMacroExpander->expandMacros( aMod );
112 		}
113 
114 		aMod += ::rtl::OUString( sal_Unicode('/') );
115 		aMod += rLibName;
116 		bLoaded = rModule.load( aMod, mode );
117 		if ( bLoaded )
118 			break;
119 	}
120 
121 	if (!bLoaded )
122 		bLoaded = rModule.loadRelative( baseModule, rLibName, mode );
123 
124 	return bLoaded;
125 }
126