Line data Source code
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 :
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"
25 :
26 : namespace func_provider
27 : {
28 :
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;
34 :
35 : static const char SHARE[] = "share";
36 : static const char SHARE_URI[] = "vnd.sun.star.expand:$BRAND_BASE_DIR";
37 :
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";
41 :
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}";
45 :
46 : static const char USER_UNO_PACKAGES[] = "user:uno_packages";
47 : static const char USER_UNO_PACKAGES_DIR[] =
48 : "/user/uno_packages/cache";
49 :
50 : static const char DOCUMENT[] = "document";
51 : static const char TDOC_SCHEME[] = "vnd.sun.star.tdoc";
52 :
53 0 : ScriptingFrameworkURIHelper::ScriptingFrameworkURIHelper(
54 : const uno::Reference< uno::XComponentContext >& xContext)
55 0 : throw( uno::RuntimeException )
56 : {
57 : try
58 : {
59 0 : m_xSimpleFileAccess = ucb::SimpleFileAccess::create(xContext);
60 : }
61 0 : catch (uno::Exception&)
62 : {
63 : OSL_FAIL("Scripting Framework error initialising XSimpleFileAccess");
64 : }
65 :
66 : try
67 : {
68 0 : m_xUriReferenceFactory = uri::UriReferenceFactory::create( xContext );
69 : }
70 0 : catch (uno::Exception&)
71 : {
72 : OSL_FAIL("Scripting Framework error initialising XUriReferenceFactory");
73 : }
74 0 : }
75 :
76 0 : ScriptingFrameworkURIHelper::~ScriptingFrameworkURIHelper()
77 : {
78 : // currently does nothing
79 0 : }
80 :
81 : void SAL_CALL
82 0 : ScriptingFrameworkURIHelper::initialize(
83 : const uno::Sequence < uno::Any >& args )
84 : throw ( uno::Exception, uno::RuntimeException )
85 : {
86 0 : if ( args.getLength() != 2 ||
87 0 : args[0].getValueType() != ::getCppuType((const OUString*)NULL) ||
88 0 : args[1].getValueType() != ::getCppuType((const OUString*)NULL) )
89 : {
90 : throw uno::RuntimeException( OUString(
91 : "ScriptingFrameworkURIHelper got invalid argument list" ),
92 0 : uno::Reference< uno::XInterface >() );
93 : }
94 :
95 0 : if ( (args[0] >>= m_sLanguage) == sal_False ||
96 0 : (args[1] >>= m_sLocation) == sal_False )
97 : {
98 : throw uno::RuntimeException( OUString(
99 : "ScriptingFrameworkURIHelper error parsing args" ),
100 0 : uno::Reference< uno::XInterface >() );
101 : }
102 :
103 0 : SCRIPTS_PART = OUString("/Scripts/");
104 0 : SCRIPTS_PART = SCRIPTS_PART.concat( m_sLanguage.toAsciiLowerCase() );
105 :
106 0 : if ( !initBaseURI() )
107 : {
108 : throw uno::RuntimeException( OUString(
109 : "ScriptingFrameworkURIHelper cannot find script directory"),
110 0 : uno::Reference< uno::XInterface >() );
111 : }
112 0 : }
113 :
114 : bool
115 0 : ScriptingFrameworkURIHelper::initBaseURI()
116 : {
117 0 : OUString uri, test;
118 0 : bool bAppendScriptsPart = false;
119 :
120 0 : if ( m_sLocation.equalsAscii(USER))
121 : {
122 0 : test = OUString(USER);
123 0 : uri = OUString(USER_URI);
124 0 : bAppendScriptsPart = true;
125 : }
126 0 : else if ( m_sLocation.equalsAscii(USER_UNO_PACKAGES))
127 : {
128 0 : test = OUString("uno_packages");
129 0 : uri = OUString(USER_URI);
130 0 : uri = uri.concat(OUString(USER_UNO_PACKAGES_DIR));
131 : }
132 0 : else if (m_sLocation.equalsAscii(SHARE))
133 : {
134 0 : test = OUString(SHARE);
135 0 : uri = OUString(SHARE_URI);
136 0 : bAppendScriptsPart = true;
137 : }
138 0 : else if (m_sLocation.equalsAscii(SHARE_UNO_PACKAGES))
139 : {
140 0 : test = OUString("uno_packages");
141 0 : uri = OUString(SHARE_UNO_PACKAGES_URI);
142 : }
143 0 : else if (m_sLocation.indexOf(TDOC_SCHEME) == 0)
144 : {
145 0 : m_sBaseURI = m_sLocation.concat( SCRIPTS_PART );
146 0 : m_sLocation = OUString(DOCUMENT );
147 0 : return true;
148 : }
149 : else
150 : {
151 0 : return false;
152 : }
153 :
154 0 : if ( !m_xSimpleFileAccess->exists( uri ) ||
155 0 : !m_xSimpleFileAccess->isFolder( uri ) )
156 : {
157 0 : return false;
158 : }
159 :
160 : uno::Sequence< OUString > children =
161 0 : m_xSimpleFileAccess->getFolderContents( uri, true );
162 :
163 0 : for ( sal_Int32 i = 0; i < children.getLength(); i++ )
164 : {
165 0 : OUString child = children[i];
166 0 : sal_Int32 idx = child.lastIndexOf(test);
167 :
168 : // OSL_TRACE("Trying: %s", PRTSTR(child));
169 : // OSL_TRACE("idx=%d, testlen=%d, children=%d",
170 : // idx, test.getLength(), child.getLength());
171 :
172 0 : if ( idx != -1 && (idx + test.getLength()) == child.getLength() )
173 : {
174 : // OSL_TRACE("FOUND PATH: %s", PRTSTR(child));
175 0 : if ( bAppendScriptsPart )
176 : {
177 0 : m_sBaseURI = child.concat( SCRIPTS_PART );
178 : }
179 : else
180 : {
181 0 : m_sBaseURI = child;
182 : }
183 0 : return true;
184 : }
185 0 : }
186 0 : return false;
187 : }
188 :
189 : OUString
190 0 : ScriptingFrameworkURIHelper::getLanguagePart(const OUString& rStorageURI)
191 : {
192 0 : OUString result;
193 :
194 0 : sal_Int32 idx = rStorageURI.indexOf(m_sBaseURI);
195 0 : sal_Int32 len = m_sBaseURI.getLength() + 1;
196 :
197 0 : if ( idx != -1 )
198 : {
199 0 : result = rStorageURI.copy(idx + len);
200 0 : result = result.replace('/', '|');
201 : }
202 0 : return result;
203 : }
204 :
205 : OUString
206 0 : ScriptingFrameworkURIHelper::getLanguagePath(const OUString& rLanguagePart)
207 : {
208 0 : OUString result;
209 0 : result = rLanguagePart.replace('|', '/');
210 0 : return result;
211 : }
212 :
213 : OUString SAL_CALL
214 0 : ScriptingFrameworkURIHelper::getScriptURI(const OUString& rStorageURI)
215 : throw( lang::IllegalArgumentException, uno::RuntimeException )
216 : {
217 0 : OUStringBuffer buf(120);
218 :
219 0 : buf.appendAscii("vnd.sun.star.script:");
220 0 : buf.append(getLanguagePart(rStorageURI));
221 0 : buf.appendAscii("?language=");
222 0 : buf.append(m_sLanguage);
223 0 : buf.appendAscii("&location=");
224 0 : buf.append(m_sLocation);
225 :
226 0 : return buf.makeStringAndClear();
227 : }
228 :
229 : OUString SAL_CALL
230 0 : ScriptingFrameworkURIHelper::getStorageURI(const OUString& rScriptURI)
231 : throw( lang::IllegalArgumentException, uno::RuntimeException )
232 : {
233 0 : OUString sLanguagePart;
234 : try
235 : {
236 : uno::Reference < uri::XVndSunStarScriptUrl > xURI(
237 0 : m_xUriReferenceFactory->parse( rScriptURI ), uno::UNO_QUERY_THROW );
238 0 : sLanguagePart = xURI->getName();
239 : }
240 0 : catch ( uno::Exception& )
241 : {
242 : throw lang::IllegalArgumentException(
243 : OUString("Script URI not valid"),
244 0 : uno::Reference< uno::XInterface >(), 1 );
245 : }
246 :
247 0 : OUStringBuffer buf(120);
248 0 : buf.append(m_sBaseURI);
249 0 : buf.append(OUString("/"));
250 0 : buf.append(getLanguagePath(sLanguagePart));
251 :
252 0 : OUString result = buf.makeStringAndClear();
253 :
254 0 : return result;
255 : }
256 :
257 : OUString SAL_CALL
258 0 : ScriptingFrameworkURIHelper::getRootStorageURI()
259 : throw( uno::RuntimeException )
260 : {
261 0 : return m_sBaseURI;
262 : }
263 :
264 : OUString SAL_CALL
265 0 : ScriptingFrameworkURIHelper::getImplementationName()
266 : throw( uno::RuntimeException )
267 : {
268 : return OUString(
269 0 : "com.sun.star.script.provider.ScriptURIHelper" );
270 : }
271 :
272 : sal_Bool SAL_CALL
273 0 : ScriptingFrameworkURIHelper::supportsService( const OUString& serviceName )
274 : throw( uno::RuntimeException )
275 : {
276 : OUString m_sServiceName(
277 0 : "com.sun.star.script.provider.ScriptURIHelper" );
278 :
279 0 : if ( serviceName.equals( m_sServiceName ) )
280 : {
281 0 : return sal_True;
282 : }
283 0 : return sal_False;
284 : }
285 :
286 : uno::Sequence< OUString > SAL_CALL
287 0 : ScriptingFrameworkURIHelper::getSupportedServiceNames()
288 : throw( uno::RuntimeException )
289 : {
290 : OUString serviceNameList[] = {
291 : OUString(
292 0 : "com.sun.star.script.provider.ScriptURIHelper" ) };
293 :
294 : uno::Sequence< OUString > serviceNames = uno::Sequence <
295 0 : OUString > ( serviceNameList, 1 );
296 :
297 0 : return serviceNames;
298 : }
299 : }
300 :
301 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|