1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <config_folders.h>
22 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
23 #include <com/sun/star/uri/UriReferenceFactory.hpp>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <rtl/ustrbuf.hxx>
26 #include <osl/diagnose.h>
27 #include "URIHelper.hxx"
29 namespace func_provider
32 namespace uno
= ::com::sun::star::uno
;
33 namespace ucb
= ::com::sun::star::ucb
;
34 namespace lang
= ::com::sun::star::lang
;
35 namespace uri
= ::com::sun::star::uri
;
36 namespace script
= ::com::sun::star::script
;
38 static const char SHARE
[] = "share";
39 static const char SHARE_URI
[] = "vnd.sun.star.expand:$BRAND_BASE_DIR";
41 static const char SHARE_UNO_PACKAGES
[] = "share:uno_packages";
42 static const char SHARE_UNO_PACKAGES_URI
[] =
43 "vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE";
45 static const char USER
[] = "user";
46 static const char USER_URI
[] =
47 "vnd.sun.star.expand:${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER
"/" SAL_CONFIGFILE( "bootstrap") "::UserInstallation}";
49 static const char USER_UNO_PACKAGES
[] = "user:uno_packages";
50 static const char USER_UNO_PACKAGES_DIR
[] =
51 "/user/uno_packages/cache";
53 static const char DOCUMENT
[] = "document";
54 static const char TDOC_SCHEME
[] = "vnd.sun.star.tdoc";
56 ScriptingFrameworkURIHelper::ScriptingFrameworkURIHelper(
57 const uno::Reference
< uno::XComponentContext
>& xContext
)
58 throw( uno::RuntimeException
)
62 m_xSimpleFileAccess
= ucb::SimpleFileAccess::create(xContext
);
64 catch (uno::Exception
&)
66 OSL_FAIL("Scripting Framework error initialising XSimpleFileAccess");
71 m_xUriReferenceFactory
= uri::UriReferenceFactory::create( xContext
);
73 catch (uno::Exception
&)
75 OSL_FAIL("Scripting Framework error initialising XUriReferenceFactory");
79 ScriptingFrameworkURIHelper::~ScriptingFrameworkURIHelper()
81 // currently does nothing
85 ScriptingFrameworkURIHelper::initialize(
86 const uno::Sequence
< uno::Any
>& args
)
87 throw ( uno::Exception
, uno::RuntimeException
, std::exception
)
89 if ( args
.getLength() != 2 ||
90 args
[0].getValueType() != ::cppu::UnoType
<OUString
>::get() ||
91 args
[1].getValueType() != ::cppu::UnoType
<OUString
>::get() )
93 throw uno::RuntimeException( "ScriptingFrameworkURIHelper got invalid argument list" );
96 if ( !(args
[0] >>= m_sLanguage
) || !(args
[1] >>= m_sLocation
) )
98 throw uno::RuntimeException( "ScriptingFrameworkURIHelper error parsing args" );
101 SCRIPTS_PART
= "/Scripts/";
102 SCRIPTS_PART
= SCRIPTS_PART
.concat( m_sLanguage
.toAsciiLowerCase() );
104 if ( !initBaseURI() )
106 throw uno::RuntimeException( "ScriptingFrameworkURIHelper cannot find script directory" );
111 ScriptingFrameworkURIHelper::initBaseURI()
114 bool bAppendScriptsPart
= false;
116 if ( m_sLocation
== USER
)
120 bAppendScriptsPart
= true;
122 else if ( m_sLocation
== USER_UNO_PACKAGES
)
124 test
= "uno_packages";
125 uri
= OUStringLiteral(USER_URI
) + USER_UNO_PACKAGES_DIR
;
127 else if (m_sLocation
== SHARE
)
131 bAppendScriptsPart
= true;
133 else if (m_sLocation
== SHARE_UNO_PACKAGES
)
135 test
= "uno_packages";
136 uri
= SHARE_UNO_PACKAGES_URI
;
138 else if (m_sLocation
.startsWith(TDOC_SCHEME
))
140 m_sBaseURI
= m_sLocation
.concat( SCRIPTS_PART
);
141 m_sLocation
= DOCUMENT
;
149 if ( !m_xSimpleFileAccess
->exists( uri
) ||
150 !m_xSimpleFileAccess
->isFolder( uri
) )
155 uno::Sequence
< OUString
> children
=
156 m_xSimpleFileAccess
->getFolderContents( uri
, true );
158 for ( sal_Int32 i
= 0; i
< children
.getLength(); i
++ )
160 OUString child
= children
[i
];
161 sal_Int32 idx
= child
.lastIndexOf(test
);
163 // OSL_TRACE("Trying: %s", PRTSTR(child));
164 // OSL_TRACE("idx=%d, testlen=%d, children=%d",
165 // idx, test.getLength(), child.getLength());
167 if ( idx
!= -1 && (idx
+ test
.getLength()) == child
.getLength() )
169 // OSL_TRACE("FOUND PATH: %s", PRTSTR(child));
170 if ( bAppendScriptsPart
)
172 m_sBaseURI
= child
.concat( SCRIPTS_PART
);
185 ScriptingFrameworkURIHelper::getLanguagePart(const OUString
& rStorageURI
)
189 sal_Int32 idx
= rStorageURI
.indexOf(m_sBaseURI
);
190 sal_Int32 len
= m_sBaseURI
.getLength() + 1;
194 result
= rStorageURI
.copy(idx
+ len
);
195 result
= result
.replace('/', '|');
201 ScriptingFrameworkURIHelper::getLanguagePath(const OUString
& rLanguagePart
)
204 result
= rLanguagePart
.replace('|', '/');
209 ScriptingFrameworkURIHelper::getScriptURI(const OUString
& rStorageURI
)
210 throw( lang::IllegalArgumentException
, uno::RuntimeException
, std::exception
)
212 OUStringBuffer
buf(120);
214 buf
.appendAscii("vnd.sun.star.script:");
215 buf
.append(getLanguagePart(rStorageURI
));
216 buf
.appendAscii("?language=");
217 buf
.append(m_sLanguage
);
218 buf
.appendAscii("&location=");
219 buf
.append(m_sLocation
);
221 return buf
.makeStringAndClear();
225 ScriptingFrameworkURIHelper::getStorageURI(const OUString
& rScriptURI
)
226 throw( lang::IllegalArgumentException
, uno::RuntimeException
, std::exception
)
228 OUString sLanguagePart
;
231 uno::Reference
< uri::XVndSunStarScriptUrl
> xURI(
232 m_xUriReferenceFactory
->parse( rScriptURI
), uno::UNO_QUERY_THROW
);
233 sLanguagePart
= xURI
->getName();
235 catch ( uno::Exception
& )
237 throw lang::IllegalArgumentException(
238 OUString("Script URI not valid"),
239 uno::Reference
< uno::XInterface
>(), 1 );
242 OUStringBuffer
buf(120);
243 buf
.append(m_sBaseURI
);
245 buf
.append(getLanguagePath(sLanguagePart
));
247 OUString result
= buf
.makeStringAndClear();
253 ScriptingFrameworkURIHelper::getRootStorageURI()
254 throw( uno::RuntimeException
, std::exception
)
260 ScriptingFrameworkURIHelper::getImplementationName()
261 throw( uno::RuntimeException
, std::exception
)
264 "com.sun.star.script.provider.ScriptURIHelper" );
268 ScriptingFrameworkURIHelper::supportsService( const OUString
& serviceName
)
269 throw( uno::RuntimeException
, std::exception
)
271 return cppu::supportsService( this, serviceName
);
274 uno::Sequence
< OUString
> SAL_CALL
275 ScriptingFrameworkURIHelper::getSupportedServiceNames()
276 throw( uno::RuntimeException
, std::exception
)
278 OUString serviceNameList
[] = {
280 "com.sun.star.script.provider.ScriptURIHelper" ) };
282 uno::Sequence
< OUString
> serviceNames
= uno::Sequence
<
283 OUString
> ( serviceNameList
, 1 );
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */