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