1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
22 #include <com/sun/star/uri/UriReferenceFactory.hpp>
23 #include <rtl/ustrbuf.hxx>
24 #include "URIHelper.hxx"
26 namespace func_provider
29 namespace uno
= ::com::sun::star::uno
;
30 namespace ucb
= ::com::sun::star::ucb
;
31 namespace lang
= ::com::sun::star::lang
;
32 namespace uri
= ::com::sun::star::uri
;
33 namespace script
= ::com::sun::star::script
;
35 static const char SHARE
[] = "share";
36 static const char SHARE_URI
[] = "vnd.sun.star.expand:$BRAND_BASE_DIR";
38 static const char SHARE_UNO_PACKAGES
[] = "share:uno_packages";
39 static const char SHARE_UNO_PACKAGES_URI
[] =
40 "vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE";
42 static const char USER
[] = "user";
43 static const char USER_URI
[] =
44 "vnd.sun.star.expand:${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE( "bootstrap") "::UserInstallation}";
46 static const char USER_UNO_PACKAGES
[] = "user:uno_packages";
47 static const char USER_UNO_PACKAGES_DIR
[] =
48 "/user/uno_packages/cache";
50 static const char DOCUMENT
[] = "document";
51 static const char TDOC_SCHEME
[] = "vnd.sun.star.tdoc";
53 ScriptingFrameworkURIHelper::ScriptingFrameworkURIHelper(
54 const uno::Reference
< uno::XComponentContext
>& xContext
)
55 throw( uno::RuntimeException
)
59 m_xSimpleFileAccess
= ucb::SimpleFileAccess::create(xContext
);
61 catch (uno::Exception
&)
63 OSL_FAIL("Scripting Framework error initialising XSimpleFileAccess");
68 m_xUriReferenceFactory
= uri::UriReferenceFactory::create( xContext
);
70 catch (uno::Exception
&)
72 OSL_FAIL("Scripting Framework error initialising XUriReferenceFactory");
76 ScriptingFrameworkURIHelper::~ScriptingFrameworkURIHelper()
78 // currently does nothing
82 ScriptingFrameworkURIHelper::initialize(
83 const uno::Sequence
< uno::Any
>& args
)
84 throw ( uno::Exception
, uno::RuntimeException
)
86 if ( args
.getLength() != 2 ||
87 args
[0].getValueType() != ::getCppuType((const OUString
*)NULL
) ||
88 args
[1].getValueType() != ::getCppuType((const OUString
*)NULL
) )
90 throw uno::RuntimeException( OUString(
91 "ScriptingFrameworkURIHelper got invalid argument list" ),
92 uno::Reference
< uno::XInterface
>() );
95 if ( (args
[0] >>= m_sLanguage
) == sal_False
||
96 (args
[1] >>= m_sLocation
) == sal_False
)
98 throw uno::RuntimeException( OUString(
99 "ScriptingFrameworkURIHelper error parsing args" ),
100 uno::Reference
< uno::XInterface
>() );
103 SCRIPTS_PART
= OUString("/Scripts/");
104 SCRIPTS_PART
= SCRIPTS_PART
.concat( m_sLanguage
.toAsciiLowerCase() );
106 if ( !initBaseURI() )
108 throw uno::RuntimeException( OUString(
109 "ScriptingFrameworkURIHelper cannot find script directory"),
110 uno::Reference
< uno::XInterface
>() );
115 ScriptingFrameworkURIHelper::initBaseURI()
118 bool bAppendScriptsPart
= false;
120 if ( m_sLocation
.equalsAscii(USER
))
122 test
= OUString(USER
);
123 uri
= OUString(USER_URI
);
124 bAppendScriptsPart
= true;
126 else if ( m_sLocation
.equalsAscii(USER_UNO_PACKAGES
))
128 test
= OUString("uno_packages");
129 uri
= OUString(USER_URI
);
130 uri
= uri
.concat(OUString(USER_UNO_PACKAGES_DIR
));
132 else if (m_sLocation
.equalsAscii(SHARE
))
134 test
= OUString(SHARE
);
135 uri
= OUString(SHARE_URI
);
136 bAppendScriptsPart
= true;
138 else if (m_sLocation
.equalsAscii(SHARE_UNO_PACKAGES
))
140 test
= OUString("uno_packages");
141 uri
= OUString(SHARE_UNO_PACKAGES_URI
);
143 else if (m_sLocation
.indexOf(TDOC_SCHEME
) == 0)
145 m_sBaseURI
= m_sLocation
.concat( SCRIPTS_PART
);
146 m_sLocation
= OUString(DOCUMENT
);
154 if ( !m_xSimpleFileAccess
->exists( uri
) ||
155 !m_xSimpleFileAccess
->isFolder( uri
) )
160 uno::Sequence
< OUString
> children
=
161 m_xSimpleFileAccess
->getFolderContents( uri
, true );
163 for ( sal_Int32 i
= 0; i
< children
.getLength(); i
++ )
165 OUString child
= children
[i
];
166 sal_Int32 idx
= child
.lastIndexOf(test
);
168 // OSL_TRACE("Trying: %s", PRTSTR(child));
169 // OSL_TRACE("idx=%d, testlen=%d, children=%d",
170 // idx, test.getLength(), child.getLength());
172 if ( idx
!= -1 && (idx
+ test
.getLength()) == child
.getLength() )
174 // OSL_TRACE("FOUND PATH: %s", PRTSTR(child));
175 if ( bAppendScriptsPart
)
177 m_sBaseURI
= child
.concat( SCRIPTS_PART
);
190 ScriptingFrameworkURIHelper::getLanguagePart(const OUString
& rStorageURI
)
194 sal_Int32 idx
= rStorageURI
.indexOf(m_sBaseURI
);
195 sal_Int32 len
= m_sBaseURI
.getLength() + 1;
199 result
= rStorageURI
.copy(idx
+ len
);
200 result
= result
.replace('/', '|');
206 ScriptingFrameworkURIHelper::getLanguagePath(const OUString
& rLanguagePart
)
209 result
= rLanguagePart
.replace('|', '/');
214 ScriptingFrameworkURIHelper::getScriptURI(const OUString
& rStorageURI
)
215 throw( lang::IllegalArgumentException
, uno::RuntimeException
)
217 OUStringBuffer
buf(120);
219 buf
.appendAscii("vnd.sun.star.script:");
220 buf
.append(getLanguagePart(rStorageURI
));
221 buf
.appendAscii("?language=");
222 buf
.append(m_sLanguage
);
223 buf
.appendAscii("&location=");
224 buf
.append(m_sLocation
);
226 return buf
.makeStringAndClear();
230 ScriptingFrameworkURIHelper::getStorageURI(const OUString
& rScriptURI
)
231 throw( lang::IllegalArgumentException
, uno::RuntimeException
)
233 OUString sLanguagePart
;
236 uno::Reference
< uri::XVndSunStarScriptUrl
> xURI(
237 m_xUriReferenceFactory
->parse( rScriptURI
), uno::UNO_QUERY_THROW
);
238 sLanguagePart
= xURI
->getName();
240 catch ( uno::Exception
& )
242 throw lang::IllegalArgumentException(
243 OUString("Script URI not valid"),
244 uno::Reference
< uno::XInterface
>(), 1 );
247 OUStringBuffer
buf(120);
248 buf
.append(m_sBaseURI
);
249 buf
.append(OUString("/"));
250 buf
.append(getLanguagePath(sLanguagePart
));
252 OUString result
= buf
.makeStringAndClear();
258 ScriptingFrameworkURIHelper::getRootStorageURI()
259 throw( uno::RuntimeException
)
265 ScriptingFrameworkURIHelper::getImplementationName()
266 throw( uno::RuntimeException
)
269 "com.sun.star.script.provider.ScriptURIHelper" );
273 ScriptingFrameworkURIHelper::supportsService( const OUString
& serviceName
)
274 throw( uno::RuntimeException
)
276 OUString
m_sServiceName(
277 "com.sun.star.script.provider.ScriptURIHelper" );
279 if ( serviceName
.equals( m_sServiceName
) )
286 uno::Sequence
< OUString
> SAL_CALL
287 ScriptingFrameworkURIHelper::getSupportedServiceNames()
288 throw( uno::RuntimeException
)
290 OUString serviceNameList
[] = {
292 "com.sun.star.script.provider.ScriptURIHelper" ) };
294 uno::Sequence
< OUString
> serviceNames
= uno::Sequence
<
295 OUString
> ( serviceNameList
, 1 );
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */