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