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