1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_scripting.hxx" 26 27 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp> 28 #include <rtl/ustrbuf.hxx> 29 #include "URIHelper.hxx" 30 31 #define PRTSTR(x) ::rtl::OUStringToOString(x, RTL_TEXTENCODING_ASCII_US).pData->buffer 32 33 namespace func_provider 34 { 35 36 using ::rtl::OUString; 37 namespace uno = ::com::sun::star::uno; 38 namespace ucb = ::com::sun::star::ucb; 39 namespace lang = ::com::sun::star::lang; 40 namespace uri = ::com::sun::star::uri; 41 namespace script = ::com::sun::star::script; 42 43 static const char SHARE[] = "share"; 44 static const char SHARE_URI[] = 45 "vnd.sun.star.expand:$$OOO_BASE_DIR"; 46 // "vnd.sun.star.expand:${$OOO_BASE_DIR/program/" SAL_CONFIGFILE( "bootstrap") "::BaseInstallation}"; 47 48 static const char SHARE_UNO_PACKAGES[] = "share:uno_packages"; 49 static const char SHARE_UNO_PACKAGES_URI[] = 50 "vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE"; 51 52 static const char USER[] = "user"; 53 static const char USER_URI[] = 54 "vnd.sun.star.expand:${$OOO_BASE_DIR/program/" SAL_CONFIGFILE( "bootstrap") "::UserInstallation}"; 55 56 static const char USER_UNO_PACKAGES[] = "user:uno_packages"; 57 static const char USER_UNO_PACKAGES_DIR[] = 58 "/user/uno_packages/cache"; 59 60 static const char DOCUMENT[] = "document"; 61 static const char TDOC_SCHEME[] = "vnd.sun.star.tdoc"; 62 63 ScriptingFrameworkURIHelper::ScriptingFrameworkURIHelper( 64 const uno::Reference< uno::XComponentContext >& xContext) 65 { 66 try 67 { 68 m_xSimpleFileAccess = uno::Reference< ucb::XSimpleFileAccess >( 69 xContext->getServiceManager()->createInstanceWithContext( 70 OUString::createFromAscii( 71 "com.sun.star.ucb.SimpleFileAccess"), 72 xContext), uno::UNO_QUERY_THROW); 73 } 74 catch (uno::Exception&) 75 { 76 OSL_ENSURE(false, 77 "Scripting Framework error initialising XSimpleFileAccess"); 78 } 79 80 try 81 { 82 m_xUriReferenceFactory = uno::Reference< uri::XUriReferenceFactory >( 83 xContext->getServiceManager()->createInstanceWithContext( 84 OUString::createFromAscii( 85 "com.sun.star.uri.UriReferenceFactory"), 86 xContext ), uno::UNO_QUERY_THROW ); 87 } 88 catch (uno::Exception&) 89 { 90 OSL_ENSURE(false, 91 "Scripting Framework error initialising XUriReferenceFactory"); 92 } 93 } 94 95 ScriptingFrameworkURIHelper::~ScriptingFrameworkURIHelper() 96 { 97 // currently does nothing 98 } 99 100 void SAL_CALL 101 ScriptingFrameworkURIHelper::initialize( 102 const uno::Sequence < uno::Any >& args ) 103 { 104 if ( args.getLength() != 2 || 105 args[0].getValueType() != ::getCppuType((const OUString*)NULL) || 106 args[1].getValueType() != ::getCppuType((const OUString*)NULL) ) 107 { 108 throw uno::RuntimeException( OUString::createFromAscii( 109 "ScriptingFrameworkURIHelper got invalid argument list" ), 110 uno::Reference< uno::XInterface >() ); 111 } 112 113 if ( (args[0] >>= m_sLanguage) == sal_False || 114 (args[1] >>= m_sLocation) == sal_False ) 115 { 116 throw uno::RuntimeException( OUString::createFromAscii( 117 "ScriptingFrameworkURIHelper error parsing args" ), 118 uno::Reference< uno::XInterface >() ); 119 } 120 121 SCRIPTS_PART = OUString::createFromAscii( "/Scripts/" ); 122 SCRIPTS_PART = SCRIPTS_PART.concat( m_sLanguage.toAsciiLowerCase() ); 123 124 if ( !initBaseURI() ) 125 { 126 throw uno::RuntimeException( OUString::createFromAscii( 127 "ScriptingFrameworkURIHelper cannot find script directory"), 128 uno::Reference< uno::XInterface >() ); 129 } 130 } 131 132 bool 133 ScriptingFrameworkURIHelper::initBaseURI() 134 { 135 OUString uri, test; 136 bool bAppendScriptsPart = false; 137 138 if ( m_sLocation.equalsAscii(USER)) 139 { 140 test = OUString::createFromAscii(USER); 141 uri = OUString::createFromAscii(USER_URI); 142 bAppendScriptsPart = true; 143 } 144 else if ( m_sLocation.equalsAscii(USER_UNO_PACKAGES)) 145 { 146 test = OUString::createFromAscii("uno_packages"); 147 uri = OUString::createFromAscii(USER_URI); 148 uri = uri.concat(OUString::createFromAscii(USER_UNO_PACKAGES_DIR)); 149 } 150 else if (m_sLocation.equalsAscii(SHARE)) 151 { 152 test = OUString::createFromAscii(SHARE); 153 uri = OUString::createFromAscii(SHARE_URI); 154 bAppendScriptsPart = true; 155 } 156 else if (m_sLocation.equalsAscii(SHARE_UNO_PACKAGES)) 157 { 158 test = OUString::createFromAscii("uno_packages"); 159 uri = OUString::createFromAscii(SHARE_UNO_PACKAGES_URI); 160 } 161 else if (m_sLocation.indexOf(OUString::createFromAscii(TDOC_SCHEME)) == 0) 162 { 163 m_sBaseURI = m_sLocation.concat( SCRIPTS_PART ); 164 m_sLocation = OUString::createFromAscii( DOCUMENT ); 165 return true; 166 } 167 else 168 { 169 return false; 170 } 171 172 if ( !m_xSimpleFileAccess->exists( uri ) || 173 !m_xSimpleFileAccess->isFolder( uri ) ) 174 { 175 return false; 176 } 177 178 uno::Sequence< OUString > children = 179 m_xSimpleFileAccess->getFolderContents( uri, true ); 180 181 for ( sal_Int32 i = 0; i < children.getLength(); i++ ) 182 { 183 OUString child = children[i]; 184 sal_Int32 idx = child.lastIndexOf(test); 185 186 // OSL_TRACE("Trying: %s", PRTSTR(child)); 187 // OSL_TRACE("idx=%d, testlen=%d, children=%d", 188 // idx, test.getLength(), child.getLength()); 189 190 if ( idx != -1 && (idx + test.getLength()) == child.getLength() ) 191 { 192 // OSL_TRACE("FOUND PATH: %s", PRTSTR(child)); 193 if ( bAppendScriptsPart ) 194 { 195 m_sBaseURI = child.concat( SCRIPTS_PART ); 196 } 197 else 198 { 199 m_sBaseURI = child; 200 } 201 return true; 202 } 203 } 204 return false; 205 } 206 207 OUString 208 ScriptingFrameworkURIHelper::getLanguagePart(const OUString& rStorageURI) 209 { 210 OUString result; 211 212 sal_Int32 idx = rStorageURI.indexOf(m_sBaseURI); 213 sal_Int32 len = m_sBaseURI.getLength() + 1; 214 215 if ( idx != -1 ) 216 { 217 result = rStorageURI.copy(idx + len); 218 result = result.replace('/', '|'); 219 } 220 return result; 221 } 222 223 OUString 224 ScriptingFrameworkURIHelper::getLanguagePath(const OUString& rLanguagePart) 225 { 226 OUString result; 227 result = rLanguagePart.replace('|', '/'); 228 return result; 229 } 230 231 OUString SAL_CALL 232 ScriptingFrameworkURIHelper::getScriptURI(const OUString& rStorageURI) 233 { 234 ::rtl::OUStringBuffer buf(120); 235 236 buf.appendAscii("vnd.sun.star.script:"); 237 buf.append(getLanguagePart(rStorageURI)); 238 buf.appendAscii("?language="); 239 buf.append(m_sLanguage); 240 buf.appendAscii("&location="); 241 buf.append(m_sLocation); 242 243 return buf.makeStringAndClear(); 244 } 245 246 OUString SAL_CALL 247 ScriptingFrameworkURIHelper::getStorageURI(const OUString& rScriptURI) 248 { 249 OUString sLanguagePart; 250 try 251 { 252 uno::Reference < uri::XVndSunStarScriptUrl > xURI( 253 m_xUriReferenceFactory->parse( rScriptURI ), uno::UNO_QUERY_THROW ); 254 sLanguagePart = xURI->getName(); 255 } 256 catch ( uno::Exception& ) 257 { 258 throw lang::IllegalArgumentException( 259 OUString::createFromAscii( "Script URI not valid" ), 260 uno::Reference< uno::XInterface >(), 1 ); 261 } 262 263 ::rtl::OUStringBuffer buf(120); 264 buf.append(m_sBaseURI); 265 buf.append(OUString::createFromAscii("/")); 266 buf.append(getLanguagePath(sLanguagePart)); 267 268 OUString result = buf.makeStringAndClear(); 269 270 return result; 271 } 272 273 OUString SAL_CALL 274 ScriptingFrameworkURIHelper::getRootStorageURI() 275 { 276 return m_sBaseURI; 277 } 278 279 OUString SAL_CALL 280 ScriptingFrameworkURIHelper::getImplementationName() 281 { 282 return OUString::createFromAscii( 283 "com.sun.star.script.provider.ScriptURIHelper" ); 284 } 285 286 sal_Bool SAL_CALL 287 ScriptingFrameworkURIHelper::supportsService( const OUString& serviceName ) 288 { 289 OUString m_sServiceName = OUString::createFromAscii( 290 "com.sun.star.script.provider.ScriptURIHelper" ); 291 292 if ( serviceName.equals( m_sServiceName ) ) 293 { 294 return sal_True; 295 } 296 return sal_False; 297 } 298 299 uno::Sequence< ::rtl::OUString > SAL_CALL 300 ScriptingFrameworkURIHelper::getSupportedServiceNames() 301 { 302 ::rtl::OUString serviceNameList[] = { 303 ::rtl::OUString::createFromAscii( 304 "com.sun.star.script.provider.ScriptURIHelper" ) }; 305 306 uno::Sequence< ::rtl::OUString > serviceNames = uno::Sequence < 307 ::rtl::OUString > ( serviceNameList, 1 ); 308 309 return serviceNames; 310 } 311 } 312