nss: upgrade to release 3.73
[LibreOffice.git] / scripting / source / provider / URIHelper.cxx
blob09fedf5762fd9892d4ac56b840a6f96fb32b3b95
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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)
52 try
54 m_xSimpleFileAccess = ucb::SimpleFileAccess::create(xContext);
56 catch (uno::Exception&)
58 OSL_FAIL("Scripting Framework error initialising XSimpleFileAccess");
61 try
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
76 void SAL_CALL
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();
94 if ( !initBaseURI() )
96 throw uno::RuntimeException( "ScriptingFrameworkURIHelper cannot find script directory" );
100 bool
101 ScriptingFrameworkURIHelper::initBaseURI()
103 OUString uri, test;
104 bool bAppendScriptsPart = false;
106 if ( m_sLocation == USER )
108 test = USER;
109 uri = USER_URI;
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)
119 test = 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";
132 return true;
134 else
136 return false;
139 if ( !m_xSimpleFileAccess->exists( uri ) ||
140 !m_xSimpleFileAccess->isFolder( uri ) )
142 return false;
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;
158 else
160 m_sBaseURI = *pChild;
162 return true;
164 return false;
167 OUString
168 ScriptingFrameworkURIHelper::getLanguagePart(const OUString& rStorageURI)
170 OUString result;
172 sal_Int32 idx = rStorageURI.indexOf(m_sBaseURI);
173 sal_Int32 len = m_sBaseURI.getLength() + 1;
175 if ( idx != -1 )
177 result = rStorageURI.copy(idx + len);
178 result = result.replace('/', '|');
180 return result;
183 OUString
184 ScriptingFrameworkURIHelper::getLanguagePath(const OUString& rLanguagePart)
186 OUString result = rLanguagePart.replace('|', '/');
187 return result;
190 OUString SAL_CALL
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();
205 OUString SAL_CALL
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);
224 buf.append("/");
225 buf.append(getLanguagePath(sLanguagePart));
227 OUString result = buf.makeStringAndClear();
229 return result;
232 OUString SAL_CALL
233 ScriptingFrameworkURIHelper::getRootStorageURI()
235 return m_sBaseURI;
238 OUString SAL_CALL
239 ScriptingFrameworkURIHelper::getImplementationName()
241 return
242 "com.sun.star.script.provider.ScriptURIHelper";
245 sal_Bool SAL_CALL
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: */