xref: /trunk/main/tools/source/generic/svlibrary.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_tools.hxx"
30 
31 #include <tools/svlibrary.hxx>
32 #include <com/sun/star/uno/Sequence.hxx>
33 #include <com/sun/star/uno/Reference.hxx>
34 #include <com/sun/star/uno/XComponentContext.hpp>
35 #include <com/sun/star/util/XMacroExpander.hpp>
36 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 #include <com/sun/star/beans/XPropertySet.hpp>
38 #include <comphelper/processfactory.hxx>
39 #include <tools/string.hxx>
40 #include <rtl/uri.hxx>
41 
42 using namespace com::sun::star;
43 
44 static uno::Sequence< rtl::OUString > GetMultiPaths_Impl()
45 {
46     uno::Sequence< rtl::OUString >   aRes;
47     uno::Sequence< rtl::OUString >   aInternalPaths;
48     uno::Sequence< rtl::OUString >   aUserPaths;
49 
50     bool bSuccess = true;
51     uno::Reference< lang::XMultiServiceFactory >  xMgr( comphelper::getProcessServiceFactory() );
52     if (xMgr.is())
53     {
54         try
55         {
56             String aInternal;
57 			aInternal.AppendAscii("Libraries");
58             String aUser;
59 			aUser.AppendAscii("Libraries");
60             aInternal .AppendAscii( "_internal" );
61             aUser     .AppendAscii( "_user" );
62 
63 			uno::Reference< beans::XPropertySet > xPathSettings( xMgr->createInstance(
64 				rtl::OUString::createFromAscii( "com.sun.star.util.PathSettings" ) ), uno::UNO_QUERY_THROW );
65             xPathSettings->getPropertyValue( aInternal )  >>= aInternalPaths;
66             xPathSettings->getPropertyValue( aUser )      >>= aUserPaths;
67         }
68         catch (uno::Exception &)
69         {
70             bSuccess = false;
71         }
72     }
73     if (bSuccess)
74     {
75         sal_Int32 nMaxEntries = aInternalPaths.getLength() + aUserPaths.getLength();
76         aRes.realloc( nMaxEntries );
77         rtl::OUString *pRes = aRes.getArray();
78         sal_Int32 nCount = 0;   // number of actually added entries
79         for (int i = 0;  i < 2;  ++i)
80         {
81             const uno::Sequence< rtl::OUString > &rPathSeq = i == 0 ? aUserPaths : aInternalPaths;
82             const rtl::OUString *pPathSeq = rPathSeq.getConstArray();
83             for (sal_Int32 k = 0;  k < rPathSeq.getLength();  ++k)
84             {
85                 const bool bAddUser     = (&rPathSeq == &aUserPaths);
86                 const bool bAddInternal = (&rPathSeq == &aInternalPaths);
87                 if ((bAddUser || bAddInternal) && pPathSeq[k].getLength() > 0)
88                     pRes[ nCount++ ] = pPathSeq[k];
89             }
90         }
91         aRes.realloc( nCount );
92     }
93 
94     return aRes;
95 }
96 
97 bool SvLibrary::LoadModule( osl::Module& rModule, const rtl::OUString& rLibName, ::oslGenericFunction baseModule, ::sal_Int32 mode )
98 {
99 	static uno::Sequence < rtl::OUString > aPaths = GetMultiPaths_Impl();
100 	bool bLoaded = false;
101 
102 	for (sal_Int32 n=0; n<aPaths.getLength(); n++)
103 	{
104 		rtl::OUString aMod = aPaths[n];
105 		if ( aPaths[n].indexOfAsciiL("vnd.sun.star.expand",19) == 0)
106 		{
107 			uno::Reference< uno::XComponentContext > xComponentContext = comphelper::getProcessComponentContext();
108 			uno::Reference< util::XMacroExpander > xMacroExpander;
109 			xComponentContext->getValueByName(
110 				::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("/singletons/com.sun.star.util.theMacroExpander") ) )
111 					>>= xMacroExpander;
112 
113 			aMod = aMod.copy( sizeof("vnd.sun.star.expand:") -1 );
114 			aMod = ::rtl::Uri::decode( aMod, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
115 			aMod = xMacroExpander->expandMacros( aMod );
116 		}
117 
118 		aMod += ::rtl::OUString( sal_Unicode('/') );
119 		aMod += rLibName;
120 		bLoaded = rModule.load( aMod, mode );
121 		if ( bLoaded )
122 			break;
123 	}
124 
125 	if (!bLoaded )
126 		bLoaded = rModule.loadRelative( baseModule, rLibName, mode );
127 
128 	return bLoaded;
129 }
130