xref: /AOO41X/main/scripting/source/provider/URIHelper.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 
31*cdf0e10cSrcweir #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
32*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
33*cdf0e10cSrcweir #include "URIHelper.hxx"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #define PRTSTR(x) ::rtl::OUStringToOString(x, RTL_TEXTENCODING_ASCII_US).pData->buffer
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir namespace func_provider
38*cdf0e10cSrcweir {
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir using ::rtl::OUString;
41*cdf0e10cSrcweir namespace uno = ::com::sun::star::uno;
42*cdf0e10cSrcweir namespace ucb = ::com::sun::star::ucb;
43*cdf0e10cSrcweir namespace lang = ::com::sun::star::lang;
44*cdf0e10cSrcweir namespace uri = ::com::sun::star::uri;
45*cdf0e10cSrcweir namespace script = ::com::sun::star::script;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir static const char SHARE[] = "share";
48*cdf0e10cSrcweir static const char SHARE_URI[] =
49*cdf0e10cSrcweir     "vnd.sun.star.expand:${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE( "bootstrap") "::BaseInstallation}";
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir static const char SHARE_UNO_PACKAGES[] = "share:uno_packages";
52*cdf0e10cSrcweir static const char SHARE_UNO_PACKAGES_URI[] =
53*cdf0e10cSrcweir     "vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE";
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir static const char USER[] = "user";
56*cdf0e10cSrcweir static const char USER_URI[] =
57*cdf0e10cSrcweir     "vnd.sun.star.expand:${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE( "bootstrap") "::UserInstallation}";
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir static const char USER_UNO_PACKAGES[] = "user:uno_packages";
60*cdf0e10cSrcweir static const char USER_UNO_PACKAGES_DIR[] =
61*cdf0e10cSrcweir     "/user/uno_packages/cache";
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir static const char DOCUMENT[] = "document";
64*cdf0e10cSrcweir static const char TDOC_SCHEME[] = "vnd.sun.star.tdoc";
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir ScriptingFrameworkURIHelper::ScriptingFrameworkURIHelper(
67*cdf0e10cSrcweir     const uno::Reference< uno::XComponentContext >& xContext)
68*cdf0e10cSrcweir         throw( uno::RuntimeException )
69*cdf0e10cSrcweir {
70*cdf0e10cSrcweir     try
71*cdf0e10cSrcweir     {
72*cdf0e10cSrcweir         m_xSimpleFileAccess = uno::Reference< ucb::XSimpleFileAccess >(
73*cdf0e10cSrcweir             xContext->getServiceManager()->createInstanceWithContext(
74*cdf0e10cSrcweir                 OUString::createFromAscii(
75*cdf0e10cSrcweir                     "com.sun.star.ucb.SimpleFileAccess"),
76*cdf0e10cSrcweir                 xContext), uno::UNO_QUERY_THROW);
77*cdf0e10cSrcweir     }
78*cdf0e10cSrcweir     catch (uno::Exception&)
79*cdf0e10cSrcweir     {
80*cdf0e10cSrcweir         OSL_ENSURE(false,
81*cdf0e10cSrcweir             "Scripting Framework error initialising XSimpleFileAccess");
82*cdf0e10cSrcweir     }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     try
85*cdf0e10cSrcweir     {
86*cdf0e10cSrcweir         m_xUriReferenceFactory = uno::Reference< uri::XUriReferenceFactory >(
87*cdf0e10cSrcweir             xContext->getServiceManager()->createInstanceWithContext(
88*cdf0e10cSrcweir                 OUString::createFromAscii(
89*cdf0e10cSrcweir                     "com.sun.star.uri.UriReferenceFactory"),
90*cdf0e10cSrcweir             xContext ), uno::UNO_QUERY_THROW );
91*cdf0e10cSrcweir     }
92*cdf0e10cSrcweir     catch (uno::Exception&)
93*cdf0e10cSrcweir     {
94*cdf0e10cSrcweir         OSL_ENSURE(false,
95*cdf0e10cSrcweir             "Scripting Framework error initialising XUriReferenceFactory");
96*cdf0e10cSrcweir     }
97*cdf0e10cSrcweir }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir ScriptingFrameworkURIHelper::~ScriptingFrameworkURIHelper()
100*cdf0e10cSrcweir {
101*cdf0e10cSrcweir     // currently does nothing
102*cdf0e10cSrcweir }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir void SAL_CALL
105*cdf0e10cSrcweir ScriptingFrameworkURIHelper::initialize(
106*cdf0e10cSrcweir     const uno::Sequence < uno::Any >& args )
107*cdf0e10cSrcweir throw ( uno::Exception, uno::RuntimeException )
108*cdf0e10cSrcweir {
109*cdf0e10cSrcweir     if ( args.getLength() != 2 ||
110*cdf0e10cSrcweir          args[0].getValueType() != ::getCppuType((const OUString*)NULL) ||
111*cdf0e10cSrcweir          args[1].getValueType() != ::getCppuType((const OUString*)NULL) )
112*cdf0e10cSrcweir     {
113*cdf0e10cSrcweir         throw uno::RuntimeException( OUString::createFromAscii(
114*cdf0e10cSrcweir             "ScriptingFrameworkURIHelper got invalid argument list" ),
115*cdf0e10cSrcweir                 uno::Reference< uno::XInterface >() );
116*cdf0e10cSrcweir     }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     if ( (args[0] >>= m_sLanguage) == sal_False ||
119*cdf0e10cSrcweir          (args[1] >>= m_sLocation) == sal_False )
120*cdf0e10cSrcweir     {
121*cdf0e10cSrcweir         throw uno::RuntimeException( OUString::createFromAscii(
122*cdf0e10cSrcweir             "ScriptingFrameworkURIHelper error parsing args" ),
123*cdf0e10cSrcweir                 uno::Reference< uno::XInterface >() );
124*cdf0e10cSrcweir     }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     SCRIPTS_PART = OUString::createFromAscii( "/Scripts/" );
127*cdf0e10cSrcweir     SCRIPTS_PART = SCRIPTS_PART.concat( m_sLanguage.toAsciiLowerCase() );
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir     if ( !initBaseURI() )
130*cdf0e10cSrcweir     {
131*cdf0e10cSrcweir         throw uno::RuntimeException( OUString::createFromAscii(
132*cdf0e10cSrcweir             "ScriptingFrameworkURIHelper cannot find script directory"),
133*cdf0e10cSrcweir                 uno::Reference< uno::XInterface >() );
134*cdf0e10cSrcweir     }
135*cdf0e10cSrcweir }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir bool
138*cdf0e10cSrcweir ScriptingFrameworkURIHelper::initBaseURI()
139*cdf0e10cSrcweir {
140*cdf0e10cSrcweir     OUString uri, test;
141*cdf0e10cSrcweir     bool bAppendScriptsPart = false;
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     if ( m_sLocation.equalsAscii(USER))
144*cdf0e10cSrcweir     {
145*cdf0e10cSrcweir         test = OUString::createFromAscii(USER);
146*cdf0e10cSrcweir         uri = OUString::createFromAscii(USER_URI);
147*cdf0e10cSrcweir         bAppendScriptsPart = true;
148*cdf0e10cSrcweir     }
149*cdf0e10cSrcweir     else if ( m_sLocation.equalsAscii(USER_UNO_PACKAGES))
150*cdf0e10cSrcweir     {
151*cdf0e10cSrcweir         test = OUString::createFromAscii("uno_packages");
152*cdf0e10cSrcweir         uri = OUString::createFromAscii(USER_URI);
153*cdf0e10cSrcweir         uri = uri.concat(OUString::createFromAscii(USER_UNO_PACKAGES_DIR));
154*cdf0e10cSrcweir     }
155*cdf0e10cSrcweir     else if (m_sLocation.equalsAscii(SHARE))
156*cdf0e10cSrcweir     {
157*cdf0e10cSrcweir         test = OUString::createFromAscii(SHARE);
158*cdf0e10cSrcweir         uri = OUString::createFromAscii(SHARE_URI);
159*cdf0e10cSrcweir         bAppendScriptsPart = true;
160*cdf0e10cSrcweir     }
161*cdf0e10cSrcweir     else if (m_sLocation.equalsAscii(SHARE_UNO_PACKAGES))
162*cdf0e10cSrcweir     {
163*cdf0e10cSrcweir         test = OUString::createFromAscii("uno_packages");
164*cdf0e10cSrcweir         uri = OUString::createFromAscii(SHARE_UNO_PACKAGES_URI);
165*cdf0e10cSrcweir     }
166*cdf0e10cSrcweir     else if (m_sLocation.indexOf(OUString::createFromAscii(TDOC_SCHEME)) == 0)
167*cdf0e10cSrcweir     {
168*cdf0e10cSrcweir         m_sBaseURI = m_sLocation.concat( SCRIPTS_PART );
169*cdf0e10cSrcweir         m_sLocation = OUString::createFromAscii( DOCUMENT );
170*cdf0e10cSrcweir         return true;
171*cdf0e10cSrcweir     }
172*cdf0e10cSrcweir     else
173*cdf0e10cSrcweir     {
174*cdf0e10cSrcweir         return false;
175*cdf0e10cSrcweir     }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir     if ( !m_xSimpleFileAccess->exists( uri ) ||
178*cdf0e10cSrcweir          !m_xSimpleFileAccess->isFolder( uri ) )
179*cdf0e10cSrcweir     {
180*cdf0e10cSrcweir         return false;
181*cdf0e10cSrcweir     }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir     uno::Sequence< OUString > children =
184*cdf0e10cSrcweir         m_xSimpleFileAccess->getFolderContents( uri, true );
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir     for ( sal_Int32 i = 0; i < children.getLength(); i++ )
187*cdf0e10cSrcweir     {
188*cdf0e10cSrcweir         OUString child = children[i];
189*cdf0e10cSrcweir         sal_Int32 idx = child.lastIndexOf(test);
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir         // OSL_TRACE("Trying: %s", PRTSTR(child));
192*cdf0e10cSrcweir         // OSL_TRACE("idx=%d, testlen=%d, children=%d",
193*cdf0e10cSrcweir         //     idx, test.getLength(), child.getLength());
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir         if ( idx != -1 && (idx + test.getLength()) == child.getLength() )
196*cdf0e10cSrcweir         {
197*cdf0e10cSrcweir             // OSL_TRACE("FOUND PATH: %s", PRTSTR(child));
198*cdf0e10cSrcweir             if ( bAppendScriptsPart )
199*cdf0e10cSrcweir             {
200*cdf0e10cSrcweir                 m_sBaseURI = child.concat( SCRIPTS_PART );
201*cdf0e10cSrcweir             }
202*cdf0e10cSrcweir             else
203*cdf0e10cSrcweir             {
204*cdf0e10cSrcweir                 m_sBaseURI = child;
205*cdf0e10cSrcweir             }
206*cdf0e10cSrcweir             return true;
207*cdf0e10cSrcweir         }
208*cdf0e10cSrcweir     }
209*cdf0e10cSrcweir     return false;
210*cdf0e10cSrcweir }
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir OUString
213*cdf0e10cSrcweir ScriptingFrameworkURIHelper::getLanguagePart(const OUString& rStorageURI)
214*cdf0e10cSrcweir {
215*cdf0e10cSrcweir     OUString result;
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir     sal_Int32 idx = rStorageURI.indexOf(m_sBaseURI);
218*cdf0e10cSrcweir     sal_Int32 len = m_sBaseURI.getLength() + 1;
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir     if ( idx != -1 )
221*cdf0e10cSrcweir     {
222*cdf0e10cSrcweir         result = rStorageURI.copy(idx + len);
223*cdf0e10cSrcweir         result = result.replace('/', '|');
224*cdf0e10cSrcweir     }
225*cdf0e10cSrcweir     return result;
226*cdf0e10cSrcweir }
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir OUString
229*cdf0e10cSrcweir ScriptingFrameworkURIHelper::getLanguagePath(const OUString& rLanguagePart)
230*cdf0e10cSrcweir {
231*cdf0e10cSrcweir     OUString result;
232*cdf0e10cSrcweir     result = rLanguagePart.replace('|', '/');
233*cdf0e10cSrcweir     return result;
234*cdf0e10cSrcweir }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir OUString SAL_CALL
237*cdf0e10cSrcweir ScriptingFrameworkURIHelper::getScriptURI(const OUString& rStorageURI)
238*cdf0e10cSrcweir     throw( lang::IllegalArgumentException, uno::RuntimeException )
239*cdf0e10cSrcweir {
240*cdf0e10cSrcweir     ::rtl::OUStringBuffer buf(120);
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir     buf.appendAscii("vnd.sun.star.script:");
243*cdf0e10cSrcweir     buf.append(getLanguagePart(rStorageURI));
244*cdf0e10cSrcweir     buf.appendAscii("?language=");
245*cdf0e10cSrcweir     buf.append(m_sLanguage);
246*cdf0e10cSrcweir     buf.appendAscii("&location=");
247*cdf0e10cSrcweir     buf.append(m_sLocation);
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir     return buf.makeStringAndClear();
250*cdf0e10cSrcweir }
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir OUString SAL_CALL
253*cdf0e10cSrcweir ScriptingFrameworkURIHelper::getStorageURI(const OUString& rScriptURI)
254*cdf0e10cSrcweir     throw( lang::IllegalArgumentException, uno::RuntimeException )
255*cdf0e10cSrcweir {
256*cdf0e10cSrcweir     OUString sLanguagePart;
257*cdf0e10cSrcweir     try
258*cdf0e10cSrcweir     {
259*cdf0e10cSrcweir         uno::Reference < uri::XVndSunStarScriptUrl > xURI(
260*cdf0e10cSrcweir             m_xUriReferenceFactory->parse( rScriptURI ), uno::UNO_QUERY_THROW );
261*cdf0e10cSrcweir         sLanguagePart = xURI->getName();
262*cdf0e10cSrcweir     }
263*cdf0e10cSrcweir     catch ( uno::Exception& )
264*cdf0e10cSrcweir     {
265*cdf0e10cSrcweir         throw lang::IllegalArgumentException(
266*cdf0e10cSrcweir             OUString::createFromAscii( "Script URI not valid" ),
267*cdf0e10cSrcweir                 uno::Reference< uno::XInterface >(), 1 );
268*cdf0e10cSrcweir     }
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir     ::rtl::OUStringBuffer buf(120);
271*cdf0e10cSrcweir     buf.append(m_sBaseURI);
272*cdf0e10cSrcweir     buf.append(OUString::createFromAscii("/"));
273*cdf0e10cSrcweir     buf.append(getLanguagePath(sLanguagePart));
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir     OUString result = buf.makeStringAndClear();
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir     return result;
278*cdf0e10cSrcweir }
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir OUString SAL_CALL
281*cdf0e10cSrcweir ScriptingFrameworkURIHelper::getRootStorageURI()
282*cdf0e10cSrcweir     throw( uno::RuntimeException )
283*cdf0e10cSrcweir {
284*cdf0e10cSrcweir     return m_sBaseURI;
285*cdf0e10cSrcweir }
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir OUString SAL_CALL
288*cdf0e10cSrcweir ScriptingFrameworkURIHelper::getImplementationName()
289*cdf0e10cSrcweir     throw( uno::RuntimeException )
290*cdf0e10cSrcweir {
291*cdf0e10cSrcweir     return OUString::createFromAscii(
292*cdf0e10cSrcweir         "com.sun.star.script.provider.ScriptURIHelper" );
293*cdf0e10cSrcweir }
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir sal_Bool SAL_CALL
296*cdf0e10cSrcweir ScriptingFrameworkURIHelper::supportsService( const OUString& serviceName )
297*cdf0e10cSrcweir     throw( uno::RuntimeException )
298*cdf0e10cSrcweir {
299*cdf0e10cSrcweir     OUString m_sServiceName = OUString::createFromAscii(
300*cdf0e10cSrcweir         "com.sun.star.script.provider.ScriptURIHelper" );
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir     if ( serviceName.equals( m_sServiceName ) )
303*cdf0e10cSrcweir     {
304*cdf0e10cSrcweir         return sal_True;
305*cdf0e10cSrcweir     }
306*cdf0e10cSrcweir     return sal_False;
307*cdf0e10cSrcweir }
308*cdf0e10cSrcweir 
309*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL
310*cdf0e10cSrcweir ScriptingFrameworkURIHelper::getSupportedServiceNames()
311*cdf0e10cSrcweir     throw( uno::RuntimeException )
312*cdf0e10cSrcweir {
313*cdf0e10cSrcweir     ::rtl::OUString serviceNameList[] = {
314*cdf0e10cSrcweir         ::rtl::OUString::createFromAscii(
315*cdf0e10cSrcweir             "com.sun.star.script.provider.ScriptURIHelper" ) };
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > serviceNames = uno::Sequence <
318*cdf0e10cSrcweir         ::rtl::OUString > ( serviceNameList, 1 );
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir     return serviceNames;
321*cdf0e10cSrcweir }
322*cdf0e10cSrcweir }
323