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>
21 #include <com/sun/star/lang/IllegalArgumentException.hpp>
22 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
23 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
24 #include <com/sun/star/uri/UriReferenceFactory.hpp>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <rtl/ustrbuf.hxx>
27 #include <osl/diagnose.h>
28 #include "URIHelper.hxx"
30 namespace func_provider
33 namespace uno
= ::com::sun::star::uno
;
34 namespace ucb
= ::com::sun::star::ucb
;
35 namespace lang
= ::com::sun::star::lang
;
36 namespace uri
= ::com::sun::star::uri
;
38 const char SHARE
[] = "share";
40 const char SHARE_UNO_PACKAGES_URI
[] =
41 "vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE";
43 const char USER
[] = "user";
44 constexpr OUStringLiteral USER_URI
=
45 u
"vnd.sun.star.expand:${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER
"/" SAL_CONFIGFILE( "bootstrap") "::UserInstallation}";
49 ScriptingFrameworkURIHelper::ScriptingFrameworkURIHelper(
50 const uno::Reference
< uno::XComponentContext
>& xContext
)
54 m_xSimpleFileAccess
= ucb::SimpleFileAccess::create(xContext
);
56 catch (uno::Exception
&)
58 OSL_FAIL("Scripting Framework error initialising XSimpleFileAccess");
63 m_xUriReferenceFactory
= uri::UriReferenceFactory::create( xContext
);
65 catch (uno::Exception
&)
67 OSL_FAIL("Scripting Framework error initialising XUriReferenceFactory");
71 ScriptingFrameworkURIHelper::~ScriptingFrameworkURIHelper()
73 // currently does nothing
77 ScriptingFrameworkURIHelper::initialize(
78 const uno::Sequence
< uno::Any
>& args
)
80 if ( args
.getLength() != 2 ||
81 args
[0].getValueType() != ::cppu::UnoType
<OUString
>::get() ||
82 args
[1].getValueType() != ::cppu::UnoType
<OUString
>::get() )
84 throw uno::RuntimeException( "ScriptingFrameworkURIHelper got invalid argument list" );
87 if ( !(args
[0] >>= m_sLanguage
) || !(args
[1] >>= m_sLocation
) )
89 throw uno::RuntimeException( "ScriptingFrameworkURIHelper error parsing args" );
92 SCRIPTS_PART
= "/Scripts/" + m_sLanguage
.toAsciiLowerCase();
96 throw uno::RuntimeException( "ScriptingFrameworkURIHelper cannot find script directory" );
101 ScriptingFrameworkURIHelper::initBaseURI()
104 bool bAppendScriptsPart
= false;
106 if ( m_sLocation
== USER
)
110 bAppendScriptsPart
= true;
112 else if ( m_sLocation
== "user:uno_packages" )
114 test
= "uno_packages";
115 uri
= USER_URI
+ "/user/uno_packages/cache";
117 else if (m_sLocation
== SHARE
)
120 uri
= "vnd.sun.star.expand:$BRAND_BASE_DIR";
121 bAppendScriptsPart
= true;
123 else if (m_sLocation
== "share:uno_packages")
125 test
= "uno_packages";
126 uri
= SHARE_UNO_PACKAGES_URI
;
128 else if (m_sLocation
.startsWith("vnd.sun.star.tdoc"))
130 m_sBaseURI
= m_sLocation
+ SCRIPTS_PART
;
131 m_sLocation
= "document";
139 if ( !m_xSimpleFileAccess
->exists( uri
) ||
140 !m_xSimpleFileAccess
->isFolder( uri
) )
145 uno::Sequence
< OUString
> children
=
146 m_xSimpleFileAccess
->getFolderContents( uri
, true );
148 auto pChild
= std::find_if(children
.begin(), children
.end(), [&test
](const OUString
& child
) {
149 sal_Int32 idx
= child
.lastIndexOf(test
);
150 return idx
!= -1 && (idx
+ test
.getLength()) == child
.getLength();
152 if (pChild
!= children
.end())
154 if ( bAppendScriptsPart
)
156 m_sBaseURI
= *pChild
+ SCRIPTS_PART
;
160 m_sBaseURI
= *pChild
;
168 ScriptingFrameworkURIHelper::getLanguagePart(const OUString
& rStorageURI
)
172 sal_Int32 idx
= rStorageURI
.indexOf(m_sBaseURI
);
173 sal_Int32 len
= m_sBaseURI
.getLength() + 1;
177 result
= rStorageURI
.copy(idx
+ len
);
178 result
= result
.replace('/', '|');
184 ScriptingFrameworkURIHelper::getLanguagePath(const OUString
& rLanguagePart
)
186 OUString result
= rLanguagePart
.replace('|', '/');
191 ScriptingFrameworkURIHelper::getScriptURI(const OUString
& rStorageURI
)
193 OUStringBuffer
buf(120);
195 buf
.append("vnd.sun.star.script:");
196 buf
.append(getLanguagePart(rStorageURI
));
197 buf
.append("?language=");
198 buf
.append(m_sLanguage
);
199 buf
.append("&location=");
200 buf
.append(m_sLocation
);
202 return buf
.makeStringAndClear();
206 ScriptingFrameworkURIHelper::getStorageURI(const OUString
& rScriptURI
)
208 OUString sLanguagePart
;
211 uno::Reference
< uri::XVndSunStarScriptUrl
> xURI(
212 m_xUriReferenceFactory
->parse( rScriptURI
), uno::UNO_QUERY_THROW
);
213 sLanguagePart
= xURI
->getName();
215 catch ( uno::Exception
& )
217 throw lang::IllegalArgumentException(
218 "Script URI not valid",
219 uno::Reference
< uno::XInterface
>(), 1 );
222 OUStringBuffer
buf(120);
223 buf
.append(m_sBaseURI
);
225 buf
.append(getLanguagePath(sLanguagePart
));
227 OUString result
= buf
.makeStringAndClear();
233 ScriptingFrameworkURIHelper::getRootStorageURI()
239 ScriptingFrameworkURIHelper::getImplementationName()
242 "com.sun.star.script.provider.ScriptURIHelper";
246 ScriptingFrameworkURIHelper::supportsService( const OUString
& serviceName
)
248 return cppu::supportsService( this, serviceName
);
251 uno::Sequence
< OUString
> SAL_CALL
252 ScriptingFrameworkURIHelper::getSupportedServiceNames()
254 return { "com.sun.star.script.provider.ScriptURIHelper" };
257 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
258 scripting_ScriptingFrameworkURIHelper_get_implementation(
259 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
261 return cppu::acquire(new ScriptingFrameworkURIHelper(context
));
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */