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