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/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" 28 29 namespace func_provider 30 { 31 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 37 static const char SHARE[] = "share"; 38 39 static const char SHARE_UNO_PACKAGES_URI[] = 40 "vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE"; 41 42 static const char USER[] = "user"; 43 static const char USER_URI[] = 44 "vnd.sun.star.expand:${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE( "bootstrap") "::UserInstallation}"; 45 46 47 48 ScriptingFrameworkURIHelper::ScriptingFrameworkURIHelper( 49 const uno::Reference< uno::XComponentContext >& xContext) 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 70 ScriptingFrameworkURIHelper::~ScriptingFrameworkURIHelper() 71 { 72 // currently does nothing 73 } 74 75 void SAL_CALL 76 ScriptingFrameworkURIHelper::initialize( 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 99 bool 100 ScriptingFrameworkURIHelper::initBaseURI() 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 = OUStringLiteral(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"; 125 uri = SHARE_UNO_PACKAGES_URI; 126 } 127 else if (m_sLocation.startsWith("vnd.sun.star.tdoc")) 128 { 129 m_sBaseURI = m_sLocation.concat( SCRIPTS_PART ); 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 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->concat( SCRIPTS_PART ); 156 } 157 else 158 { 159 m_sBaseURI = *pChild; 160 } 161 return true; 162 } 163 return false; 164 } 165 166 OUString 167 ScriptingFrameworkURIHelper::getLanguagePart(const OUString& rStorageURI) 168 { 169 OUString result; 170 171 sal_Int32 idx = rStorageURI.indexOf(m_sBaseURI); 172 sal_Int32 len = m_sBaseURI.getLength() + 1; 173 174 if ( idx != -1 ) 175 { 176 result = rStorageURI.copy(idx + len); 177 result = result.replace('/', '|'); 178 } 179 return result; 180 } 181 182 OUString 183 ScriptingFrameworkURIHelper::getLanguagePath(const OUString& rLanguagePart) 184 { 185 OUString result = rLanguagePart.replace('|', '/'); 186 return result; 187 } 188 189 OUString SAL_CALL 190 ScriptingFrameworkURIHelper::getScriptURI(const OUString& rStorageURI) 191 { 192 OUStringBuffer buf(120); 193 194 buf.append("vnd.sun.star.script:"); 195 buf.append(getLanguagePart(rStorageURI)); 196 buf.append("?language="); 197 buf.append(m_sLanguage); 198 buf.append("&location="); 199 buf.append(m_sLocation); 200 201 return buf.makeStringAndClear(); 202 } 203 204 OUString SAL_CALL 205 ScriptingFrameworkURIHelper::getStorageURI(const OUString& rScriptURI) 206 { 207 OUString sLanguagePart; 208 try 209 { 210 uno::Reference < uri::XVndSunStarScriptUrl > xURI( 211 m_xUriReferenceFactory->parse( rScriptURI ), uno::UNO_QUERY_THROW ); 212 sLanguagePart = xURI->getName(); 213 } 214 catch ( uno::Exception& ) 215 { 216 throw lang::IllegalArgumentException( 217 "Script URI not valid", 218 uno::Reference< uno::XInterface >(), 1 ); 219 } 220 221 OUStringBuffer buf(120); 222 buf.append(m_sBaseURI); 223 buf.append("/"); 224 buf.append(getLanguagePath(sLanguagePart)); 225 226 OUString result = buf.makeStringAndClear(); 227 228 return result; 229 } 230 231 OUString SAL_CALL 232 ScriptingFrameworkURIHelper::getRootStorageURI() 233 { 234 return m_sBaseURI; 235 } 236 237 OUString SAL_CALL 238 ScriptingFrameworkURIHelper::getImplementationName() 239 { 240 return 241 "com.sun.star.script.provider.ScriptURIHelper"; 242 } 243 244 sal_Bool SAL_CALL 245 ScriptingFrameworkURIHelper::supportsService( const OUString& serviceName ) 246 { 247 return cppu::supportsService( this, serviceName ); 248 } 249 250 uno::Sequence< OUString > SAL_CALL 251 ScriptingFrameworkURIHelper::getSupportedServiceNames() 252 { 253 return { "com.sun.star.script.provider.ScriptURIHelper" }; 254 } 255 } 256 257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 258
