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