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 <osl/diagnose.h>
27 : #include "URIHelper.hxx"
28 :
29 : namespace func_provider
30 : {
31 :
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/" LIBO_ETC_FOLDER "/" 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, std::exception )
88 : {
89 0 : if ( args.getLength() != 2 ||
90 0 : args[0].getValueType() != ::cppu::UnoType<OUString>::get() ||
91 0 : args[1].getValueType() != ::cppu::UnoType<OUString>::get() )
92 : {
93 0 : throw uno::RuntimeException( "ScriptingFrameworkURIHelper got invalid argument list" );
94 : }
95 :
96 0 : if ( !(args[0] >>= m_sLanguage) || !(args[1] >>= m_sLocation) )
97 : {
98 0 : throw uno::RuntimeException( "ScriptingFrameworkURIHelper error parsing args" );
99 : }
100 :
101 0 : SCRIPTS_PART = "/Scripts/";
102 0 : SCRIPTS_PART = SCRIPTS_PART.concat( m_sLanguage.toAsciiLowerCase() );
103 :
104 0 : if ( !initBaseURI() )
105 : {
106 0 : throw uno::RuntimeException( "ScriptingFrameworkURIHelper cannot find script directory" );
107 : }
108 0 : }
109 :
110 : bool
111 0 : ScriptingFrameworkURIHelper::initBaseURI()
112 : {
113 0 : OUString uri, test;
114 0 : bool bAppendScriptsPart = false;
115 :
116 0 : if ( m_sLocation == USER )
117 : {
118 0 : test = USER;
119 0 : uri = USER_URI;
120 0 : bAppendScriptsPart = true;
121 : }
122 0 : else if ( m_sLocation == USER_UNO_PACKAGES )
123 : {
124 0 : test = "uno_packages";
125 0 : uri = OUStringLiteral(USER_URI) + USER_UNO_PACKAGES_DIR;
126 : }
127 0 : else if (m_sLocation == SHARE)
128 : {
129 0 : test = SHARE;
130 0 : uri = SHARE_URI;
131 0 : bAppendScriptsPart = true;
132 : }
133 0 : else if (m_sLocation == SHARE_UNO_PACKAGES)
134 : {
135 0 : test = "uno_packages";
136 0 : uri = SHARE_UNO_PACKAGES_URI;
137 : }
138 0 : else if (m_sLocation.startsWith(TDOC_SCHEME))
139 : {
140 0 : m_sBaseURI = m_sLocation.concat( SCRIPTS_PART );
141 0 : m_sLocation = DOCUMENT;
142 0 : return true;
143 : }
144 : else
145 : {
146 0 : return false;
147 : }
148 :
149 0 : if ( !m_xSimpleFileAccess->exists( uri ) ||
150 0 : !m_xSimpleFileAccess->isFolder( uri ) )
151 : {
152 0 : return false;
153 : }
154 :
155 : uno::Sequence< OUString > children =
156 0 : m_xSimpleFileAccess->getFolderContents( uri, true );
157 :
158 0 : for ( sal_Int32 i = 0; i < children.getLength(); i++ )
159 : {
160 0 : OUString child = children[i];
161 0 : sal_Int32 idx = child.lastIndexOf(test);
162 :
163 : // OSL_TRACE("Trying: %s", PRTSTR(child));
164 : // OSL_TRACE("idx=%d, testlen=%d, children=%d",
165 : // idx, test.getLength(), child.getLength());
166 :
167 0 : if ( idx != -1 && (idx + test.getLength()) == child.getLength() )
168 : {
169 : // OSL_TRACE("FOUND PATH: %s", PRTSTR(child));
170 0 : if ( bAppendScriptsPart )
171 : {
172 0 : m_sBaseURI = child.concat( SCRIPTS_PART );
173 : }
174 : else
175 : {
176 0 : m_sBaseURI = child;
177 : }
178 0 : return true;
179 : }
180 0 : }
181 0 : return false;
182 : }
183 :
184 : OUString
185 0 : ScriptingFrameworkURIHelper::getLanguagePart(const OUString& rStorageURI)
186 : {
187 0 : OUString result;
188 :
189 0 : sal_Int32 idx = rStorageURI.indexOf(m_sBaseURI);
190 0 : sal_Int32 len = m_sBaseURI.getLength() + 1;
191 :
192 0 : if ( idx != -1 )
193 : {
194 0 : result = rStorageURI.copy(idx + len);
195 0 : result = result.replace('/', '|');
196 : }
197 0 : return result;
198 : }
199 :
200 : OUString
201 0 : ScriptingFrameworkURIHelper::getLanguagePath(const OUString& rLanguagePart)
202 : {
203 0 : OUString result;
204 0 : result = rLanguagePart.replace('|', '/');
205 0 : return result;
206 : }
207 :
208 : OUString SAL_CALL
209 0 : ScriptingFrameworkURIHelper::getScriptURI(const OUString& rStorageURI)
210 : throw( lang::IllegalArgumentException, uno::RuntimeException, std::exception )
211 : {
212 0 : OUStringBuffer buf(120);
213 :
214 0 : buf.appendAscii("vnd.sun.star.script:");
215 0 : buf.append(getLanguagePart(rStorageURI));
216 0 : buf.appendAscii("?language=");
217 0 : buf.append(m_sLanguage);
218 0 : buf.appendAscii("&location=");
219 0 : buf.append(m_sLocation);
220 :
221 0 : return buf.makeStringAndClear();
222 : }
223 :
224 : OUString SAL_CALL
225 0 : ScriptingFrameworkURIHelper::getStorageURI(const OUString& rScriptURI)
226 : throw( lang::IllegalArgumentException, uno::RuntimeException, std::exception )
227 : {
228 0 : OUString sLanguagePart;
229 : try
230 : {
231 : uno::Reference < uri::XVndSunStarScriptUrl > xURI(
232 0 : m_xUriReferenceFactory->parse( rScriptURI ), uno::UNO_QUERY_THROW );
233 0 : sLanguagePart = xURI->getName();
234 : }
235 0 : catch ( uno::Exception& )
236 : {
237 : throw lang::IllegalArgumentException(
238 : OUString("Script URI not valid"),
239 0 : uno::Reference< uno::XInterface >(), 1 );
240 : }
241 :
242 0 : OUStringBuffer buf(120);
243 0 : buf.append(m_sBaseURI);
244 0 : buf.append("/");
245 0 : buf.append(getLanguagePath(sLanguagePart));
246 :
247 0 : OUString result = buf.makeStringAndClear();
248 :
249 0 : return result;
250 : }
251 :
252 : OUString SAL_CALL
253 0 : ScriptingFrameworkURIHelper::getRootStorageURI()
254 : throw( uno::RuntimeException, std::exception )
255 : {
256 0 : return m_sBaseURI;
257 : }
258 :
259 : OUString SAL_CALL
260 0 : ScriptingFrameworkURIHelper::getImplementationName()
261 : throw( uno::RuntimeException, std::exception )
262 : {
263 : return OUString(
264 0 : "com.sun.star.script.provider.ScriptURIHelper" );
265 : }
266 :
267 : sal_Bool SAL_CALL
268 0 : ScriptingFrameworkURIHelper::supportsService( const OUString& serviceName )
269 : throw( uno::RuntimeException, std::exception )
270 : {
271 0 : return cppu::supportsService( this, serviceName );
272 : }
273 :
274 : uno::Sequence< OUString > SAL_CALL
275 0 : ScriptingFrameworkURIHelper::getSupportedServiceNames()
276 : throw( uno::RuntimeException, std::exception )
277 : {
278 : OUString serviceNameList[] = {
279 : OUString(
280 0 : "com.sun.star.script.provider.ScriptURIHelper" ) };
281 :
282 : uno::Sequence< OUString > serviceNames = uno::Sequence <
283 0 : OUString > ( serviceNameList, 1 );
284 :
285 0 : return serviceNames;
286 : }
287 : }
288 :
289 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|