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 : #include <config_folders.h>
21 :
22 : #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
23 : #include <com/sun/star/uri/UriReferenceFactory.hpp>
24 : #include <cppuhelper/supportsservice.hxx>
25 : #include <rtl/ustrbuf.hxx>
26 : #include "URIHelper.hxx"
27 :
28 : namespace func_provider
29 : {
30 :
31 : namespace uno = ::com::sun::star::uno;
32 : namespace ucb = ::com::sun::star::ucb;
33 : namespace lang = ::com::sun::star::lang;
34 : namespace uri = ::com::sun::star::uri;
35 : namespace script = ::com::sun::star::script;
36 :
37 : static const char SHARE[] = "share";
38 : static const char SHARE_URI[] = "vnd.sun.star.expand:$BRAND_BASE_DIR";
39 :
40 : static const char SHARE_UNO_PACKAGES[] = "share:uno_packages";
41 : static const char SHARE_UNO_PACKAGES_URI[] =
42 : "vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE";
43 :
44 : static const char USER[] = "user";
45 : static const char USER_URI[] =
46 : "vnd.sun.star.expand:${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE( "bootstrap") "::UserInstallation}";
47 :
48 : static const char USER_UNO_PACKAGES[] = "user:uno_packages";
49 : static const char USER_UNO_PACKAGES_DIR[] =
50 : "/user/uno_packages/cache";
51 :
52 : static const char DOCUMENT[] = "document";
53 : static const char TDOC_SCHEME[] = "vnd.sun.star.tdoc";
54 :
55 0 : ScriptingFrameworkURIHelper::ScriptingFrameworkURIHelper(
56 : const uno::Reference< uno::XComponentContext >& xContext)
57 0 : throw( uno::RuntimeException )
58 : {
59 : try
60 : {
61 0 : m_xSimpleFileAccess = ucb::SimpleFileAccess::create(xContext);
62 : }
63 0 : catch (uno::Exception&)
64 : {
65 : OSL_FAIL("Scripting Framework error initialising XSimpleFileAccess");
66 : }
67 :
68 : try
69 : {
70 0 : m_xUriReferenceFactory = uri::UriReferenceFactory::create( xContext );
71 : }
72 0 : catch (uno::Exception&)
73 : {
74 : OSL_FAIL("Scripting Framework error initialising XUriReferenceFactory");
75 : }
76 0 : }
77 :
78 0 : ScriptingFrameworkURIHelper::~ScriptingFrameworkURIHelper()
79 : {
80 : // currently does nothing
81 0 : }
82 :
83 : void SAL_CALL
84 0 : ScriptingFrameworkURIHelper::initialize(
85 : const uno::Sequence < uno::Any >& args )
86 : throw ( uno::Exception, uno::RuntimeException, std::exception )
87 : {
88 0 : if ( args.getLength() != 2 ||
89 0 : args[0].getValueType() != ::getCppuType((const OUString*)NULL) ||
90 0 : args[1].getValueType() != ::getCppuType((const OUString*)NULL) )
91 : {
92 : throw uno::RuntimeException( OUString(
93 : "ScriptingFrameworkURIHelper got invalid argument list" ),
94 0 : uno::Reference< uno::XInterface >() );
95 : }
96 :
97 0 : if ( !(args[0] >>= m_sLanguage) || !(args[1] >>= m_sLocation) )
98 : {
99 : throw uno::RuntimeException( OUString(
100 : "ScriptingFrameworkURIHelper error parsing args" ),
101 0 : uno::Reference< uno::XInterface >() );
102 : }
103 :
104 0 : SCRIPTS_PART = "/Scripts/";
105 0 : SCRIPTS_PART = SCRIPTS_PART.concat( m_sLanguage.toAsciiLowerCase() );
106 :
107 0 : if ( !initBaseURI() )
108 : {
109 : throw uno::RuntimeException( OUString(
110 : "ScriptingFrameworkURIHelper cannot find script directory"),
111 0 : uno::Reference< uno::XInterface >() );
112 : }
113 0 : }
114 :
115 : bool
116 0 : ScriptingFrameworkURIHelper::initBaseURI()
117 : {
118 0 : OUString uri, test;
119 0 : bool bAppendScriptsPart = false;
120 :
121 0 : if ( m_sLocation.equalsAscii(USER))
122 : {
123 0 : test = USER;
124 0 : uri = USER_URI;
125 0 : bAppendScriptsPart = true;
126 : }
127 0 : else if ( m_sLocation.equalsAscii(USER_UNO_PACKAGES))
128 : {
129 0 : test = "uno_packages";
130 0 : uri = OUString( USER_URI ) + USER_UNO_PACKAGES_DIR;
131 : }
132 0 : else if (m_sLocation.equalsAscii(SHARE))
133 : {
134 0 : test = SHARE;
135 0 : uri = SHARE_URI;
136 0 : bAppendScriptsPart = true;
137 : }
138 0 : else if (m_sLocation.equalsAscii(SHARE_UNO_PACKAGES))
139 : {
140 0 : test = "uno_packages";
141 0 : uri = SHARE_UNO_PACKAGES_URI;
142 : }
143 0 : else if (m_sLocation.startsWith(TDOC_SCHEME))
144 : {
145 0 : m_sBaseURI = m_sLocation.concat( SCRIPTS_PART );
146 0 : m_sLocation = 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, std::exception )
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, std::exception )
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("/");
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, std::exception )
260 : {
261 0 : return m_sBaseURI;
262 : }
263 :
264 : OUString SAL_CALL
265 0 : ScriptingFrameworkURIHelper::getImplementationName()
266 : throw( uno::RuntimeException, std::exception )
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, std::exception )
275 : {
276 0 : return cppu::supportsService( this, serviceName );
277 : }
278 :
279 : uno::Sequence< OUString > SAL_CALL
280 0 : ScriptingFrameworkURIHelper::getSupportedServiceNames()
281 : throw( uno::RuntimeException, std::exception )
282 : {
283 : OUString serviceNameList[] = {
284 : OUString(
285 0 : "com.sun.star.script.provider.ScriptURIHelper" ) };
286 :
287 : uno::Sequence< OUString > serviceNames = uno::Sequence <
288 0 : OUString > ( serviceNameList, 1 );
289 :
290 0 : return serviceNames;
291 : }
292 : }
293 :
294 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|