tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / scripting / source / provider / URIHelper.cxx
blobf1ed31f47f02f8fbaebc558579e62d4e15fa368b
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 <osl/diagnose.h>
27 #include "URIHelper.hxx"
29 namespace func_provider
32 namespace uno = css::uno;
33 namespace ucb = css::ucb;
34 namespace lang = css::lang;
35 namespace uri = css::uri;
37 constexpr OUString SHARE = u"share"_ustr;
39 constexpr OUStringLiteral SHARE_UNO_PACKAGES_URI =
40 u"vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE";
42 constexpr OUString USER = u"user"_ustr;
43 constexpr OUString USER_URI =
44 u"vnd.sun.star.expand:${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE( "bootstrap") "::UserInstallation}"_ustr;
48 ScriptingFrameworkURIHelper::ScriptingFrameworkURIHelper(
49 const uno::Reference< uno::XComponentContext >& xContext)
51 try
53 m_xSimpleFileAccess = ucb::SimpleFileAccess::create(xContext);
55 catch (uno::Exception&)
57 OSL_FAIL("Scripting Framework error initialising XSimpleFileAccess");
60 try
62 m_xUriReferenceFactory = uri::UriReferenceFactory::create( xContext );
64 catch (uno::Exception&)
66 OSL_FAIL("Scripting Framework error initialising XUriReferenceFactory");
70 ScriptingFrameworkURIHelper::~ScriptingFrameworkURIHelper()
72 // currently does nothing
75 void SAL_CALL
76 ScriptingFrameworkURIHelper::initialize(
77 const uno::Sequence < uno::Any >& args )
79 if ( args.getLength() != 2 ||
80 args[0].getValueType() != ::cppu::UnoType<OUString>::get() ||
81 args[1].getValueType() != ::cppu::UnoType<OUString>::get() )
83 throw uno::RuntimeException( u"ScriptingFrameworkURIHelper got invalid argument list"_ustr );
86 if ( !(args[0] >>= m_sLanguage) || !(args[1] >>= m_sLocation) )
88 throw uno::RuntimeException( u"ScriptingFrameworkURIHelper error parsing args"_ustr );
91 SCRIPTS_PART = "/Scripts/" + m_sLanguage.toAsciiLowerCase();
93 if ( !initBaseURI() )
95 throw uno::RuntimeException( u"ScriptingFrameworkURIHelper cannot find script directory"_ustr );
99 bool
100 ScriptingFrameworkURIHelper::initBaseURI()
102 OUString uri, test;
103 bool bAppendScriptsPart = false;
105 if ( m_sLocation == USER )
107 test = USER;
108 uri = USER_URI;
109 bAppendScriptsPart = true;
111 else if ( m_sLocation == "user:uno_packages" )
113 test = "uno_packages";
114 uri = USER_URI + "/user/uno_packages/cache";
116 else if (m_sLocation == SHARE)
118 test = SHARE;
119 uri = "vnd.sun.star.expand:$BRAND_BASE_DIR";
120 bAppendScriptsPart = true;
122 else if (m_sLocation == "share:uno_packages")
124 test = "uno_packages";
125 uri = SHARE_UNO_PACKAGES_URI;
127 else if (m_sLocation.startsWith("vnd.sun.star.tdoc"))
129 m_sBaseURI = m_sLocation + SCRIPTS_PART;
130 m_sLocation = "document";
131 return true;
133 else
135 return false;
138 if ( !m_xSimpleFileAccess->exists( uri ) ||
139 !m_xSimpleFileAccess->isFolder( uri ) )
141 return false;
144 const uno::Sequence< OUString > children =
145 m_xSimpleFileAccess->getFolderContents( uri, true );
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();
151 if (pChild != children.end())
153 if ( bAppendScriptsPart )
155 m_sBaseURI = *pChild + SCRIPTS_PART;
157 else
159 m_sBaseURI = *pChild;
161 return true;
163 return false;
166 OUString
167 ScriptingFrameworkURIHelper::getLanguagePart(std::u16string_view rStorageURI)
169 OUString result;
171 size_t idx = rStorageURI.find(m_sBaseURI);
172 sal_Int32 len = m_sBaseURI.getLength() + 1;
174 if ( idx != std::u16string_view::npos )
176 result = rStorageURI.substr(idx + len);
177 result = result.replace('/', '|');
179 return result;
182 OUString
183 ScriptingFrameworkURIHelper::getLanguagePath(const OUString& rLanguagePart)
185 OUString result = rLanguagePart.replace('|', '/');
186 return result;
189 OUString SAL_CALL
190 ScriptingFrameworkURIHelper::getScriptURI(const OUString& rStorageURI)
192 return
193 "vnd.sun.star.script:" +
194 getLanguagePart(rStorageURI) +
195 "?language=" +
196 m_sLanguage +
197 "&location=" +
198 m_sLocation;
201 OUString SAL_CALL
202 ScriptingFrameworkURIHelper::getStorageURI(const OUString& rScriptURI)
204 OUString sLanguagePart;
207 uno::Reference < uri::XVndSunStarScriptUrl > xURI(
208 m_xUriReferenceFactory->parse( rScriptURI ), uno::UNO_QUERY_THROW );
209 sLanguagePart = xURI->getName();
211 catch ( uno::Exception& )
213 throw lang::IllegalArgumentException(
214 u"Script URI not valid"_ustr,
215 uno::Reference< uno::XInterface >(), 1 );
218 return m_sBaseURI + "/" + getLanguagePath(sLanguagePart);
221 OUString SAL_CALL
222 ScriptingFrameworkURIHelper::getRootStorageURI()
224 return m_sBaseURI;
227 OUString SAL_CALL
228 ScriptingFrameworkURIHelper::getImplementationName()
230 return
231 u"com.sun.star.script.provider.ScriptURIHelper"_ustr;
234 sal_Bool SAL_CALL
235 ScriptingFrameworkURIHelper::supportsService( const OUString& serviceName )
237 return cppu::supportsService( this, serviceName );
240 uno::Sequence< OUString > SAL_CALL
241 ScriptingFrameworkURIHelper::getSupportedServiceNames()
243 return { u"com.sun.star.script.provider.ScriptURIHelper"_ustr };
246 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
247 scripting_ScriptingFrameworkURIHelper_get_implementation(
248 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
250 return cppu::acquire(new ScriptingFrameworkURIHelper(context));
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */