LibreOffice Module scripting (master) 1
URIHelper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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>
26#include <osl/diagnose.h>
27#include "URIHelper.hxx"
28
29namespace func_provider
30{
31
32namespace uno = ::com::sun::star::uno;
33namespace ucb = ::com::sun::star::ucb;
34namespace lang = ::com::sun::star::lang;
35namespace uri = ::com::sun::star::uri;
36
37constexpr OUStringLiteral SHARE = u"share";
38
39constexpr OUStringLiteral SHARE_UNO_PACKAGES_URI =
40 u"vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE";
41
42constexpr OUStringLiteral USER = u"user";
43constexpr OUStringLiteral USER_URI =
44 u"vnd.sun.star.expand:${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE( "bootstrap") "::UserInstallation}";
45
46
47
50{
51 try
52 {
53 m_xSimpleFileAccess = ucb::SimpleFileAccess::create(xContext);
54 }
55 catch (uno::Exception&)
56 {
57 OSL_FAIL("Scripting Framework error initialising XSimpleFileAccess");
58 }
59
60 try
61 {
62 m_xUriReferenceFactory = uri::UriReferenceFactory::create( xContext );
63 }
64 catch (uno::Exception&)
65 {
66 OSL_FAIL("Scripting Framework error initialising XUriReferenceFactory");
67 }
68}
69
71{
72 // currently does nothing
73}
74
75void SAL_CALL
77 const uno::Sequence < uno::Any >& args )
78{
79 if ( args.getLength() != 2 ||
80 args[0].getValueType() != ::cppu::UnoType<OUString>::get() ||
81 args[1].getValueType() != ::cppu::UnoType<OUString>::get() )
82 {
83 throw uno::RuntimeException( "ScriptingFrameworkURIHelper got invalid argument list" );
84 }
85
86 if ( !(args[0] >>= m_sLanguage) || !(args[1] >>= m_sLocation) )
87 {
88 throw uno::RuntimeException( "ScriptingFrameworkURIHelper error parsing args" );
89 }
90
91 SCRIPTS_PART = "/Scripts/" + m_sLanguage.toAsciiLowerCase();
92
93 if ( !initBaseURI() )
94 {
95 throw uno::RuntimeException( "ScriptingFrameworkURIHelper cannot find script directory" );
96 }
97}
98
99bool
101{
102 OUString uri, test;
103 bool bAppendScriptsPart = false;
104
105 if ( m_sLocation == USER )
106 {
107 test = USER;
108 uri = USER_URI;
109 bAppendScriptsPart = true;
110 }
111 else if ( m_sLocation == "user:uno_packages" )
112 {
113 test = "uno_packages";
114 uri = USER_URI + "/user/uno_packages/cache";
115 }
116 else if (m_sLocation == SHARE)
117 {
118 test = SHARE;
119 uri = "vnd.sun.star.expand:$BRAND_BASE_DIR";
120 bAppendScriptsPart = true;
121 }
122 else if (m_sLocation == "share:uno_packages")
123 {
124 test = "uno_packages";
126 }
127 else if (m_sLocation.startsWith("vnd.sun.star.tdoc"))
128 {
130 m_sLocation = "document";
131 return true;
132 }
133 else
134 {
135 return false;
136 }
137
138 if ( !m_xSimpleFileAccess->exists( uri ) ||
139 !m_xSimpleFileAccess->isFolder( uri ) )
140 {
141 return false;
142 }
143
144 const uno::Sequence< OUString > children =
145 m_xSimpleFileAccess->getFolderContents( uri, true );
146
147 auto pChild = std::find_if(children.begin(), children.end(), [&test](const OUString& child) {
148 sal_Int32 idx = child.lastIndexOf(test);
149 return idx != -1 && (idx + test.getLength()) == child.getLength();
150 });
151 if (pChild != children.end())
152 {
153 if ( bAppendScriptsPart )
154 {
155 m_sBaseURI = *pChild + SCRIPTS_PART;
156 }
157 else
158 {
159 m_sBaseURI = *pChild;
160 }
161 return true;
162 }
163 return false;
164}
165
166OUString
167ScriptingFrameworkURIHelper::getLanguagePart(std::u16string_view rStorageURI)
168{
169 OUString result;
170
171 size_t idx = rStorageURI.find(m_sBaseURI);
172 sal_Int32 len = m_sBaseURI.getLength() + 1;
173
174 if ( idx != std::u16string_view::npos )
175 {
176 result = rStorageURI.substr(idx + len);
177 result = result.replace('/', '|');
178 }
179 return result;
180}
181
182OUString
184{
185 OUString result = rLanguagePart.replace('|', '/');
186 return result;
187}
188
189OUString SAL_CALL
191{
192 return
193 "vnd.sun.star.script:" +
194 getLanguagePart(rStorageURI) +
195 "?language=" +
197 "&location=" +
199}
200
201OUString SAL_CALL
203{
204 OUString sLanguagePart;
205 try
206 {
208 m_xUriReferenceFactory->parse( rScriptURI ), uno::UNO_QUERY_THROW );
209 sLanguagePart = xURI->getName();
210 }
211 catch ( uno::Exception& )
212 {
213 throw lang::IllegalArgumentException(
214 "Script URI not valid",
216 }
217
218 return m_sBaseURI + "/" + getLanguagePath(sLanguagePart);
219}
220
221OUString SAL_CALL
223{
224 return m_sBaseURI;
225}
226
227OUString SAL_CALL
229{
230 return
231 "com.sun.star.script.provider.ScriptURIHelper";
232}
233
234sal_Bool SAL_CALL
236{
237 return cppu::supportsService( this, serviceName );
238}
239
242{
243 return { "com.sun.star.script.provider.ScriptURIHelper" };
244}
245
246extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
248 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
249{
250 return cppu::acquire(new ScriptingFrameworkURIHelper(context));
251}
252
253}
254
255
256/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual OUString SAL_CALL getStorageURI(const OUString &rScriptURI) override
Definition: URIHelper.cxx:202
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &args) override
Definition: URIHelper.cxx:76
virtual OUString SAL_CALL getRootStorageURI() override
Definition: URIHelper.cxx:222
virtual OUString SAL_CALL getImplementationName() override
Definition: URIHelper.cxx:228
virtual ~ScriptingFrameworkURIHelper() override
Definition: URIHelper.cxx:70
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: URIHelper.cxx:241
css::uno::Reference< css::uri::XUriReferenceFactory > m_xUriReferenceFactory
Definition: URIHelper.hxx:44
virtual OUString SAL_CALL getScriptURI(const OUString &rStorageURI) override
Definition: URIHelper.cxx:190
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: URIHelper.cxx:235
OUString getLanguagePart(std::u16string_view rStorageURI)
Definition: URIHelper.cxx:167
static OUString getLanguagePath(const OUString &rLanguagePart)
Definition: URIHelper.cxx:183
css::uno::Reference< css::ucb::XSimpleFileAccess3 > m_xSimpleFileAccess
Definition: URIHelper.hxx:43
ScriptingFrameworkURIHelper(const css::uno::Reference< css::uno::XComponentContext > &xContext)
Definition: URIHelper.cxx:48
#define SAL_CONFIGFILE(name)
float u
const sal_uInt16 idx[]
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * scripting_ScriptingFrameworkURIHelper_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: URIHelper.cxx:247
constexpr OUStringLiteral SHARE_UNO_PACKAGES_URI
Definition: URIHelper.cxx:39
constexpr OUStringLiteral USER
Definition: URIHelper.cxx:42
constexpr OUStringLiteral USER_URI
Definition: URIHelper.cxx:43
constexpr OUStringLiteral SHARE
Definition: URIHelper.cxx:37
args
unsigned char sal_Bool
Any result