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 "rtl/uri.hxx"
22 : #include "osl/mutex.hxx"
23 : #include "cppuhelper/compbase2.hxx"
24 : #include "cppuhelper/factory.hxx"
25 : #include "cppuhelper/implementationentry.hxx"
26 : #include "ucbhelper/content.hxx"
27 : #include "com/sun/star/uno/XComponentContext.hpp"
28 : #include "com/sun/star/lang/DisposedException.hpp"
29 : #include "com/sun/star/lang/XServiceInfo.hpp"
30 : #include "com/sun/star/lang/XMultiServiceFactory.hpp"
31 : #include "com/sun/star/registry/XRegistryKey.hpp"
32 : #include "com/sun/star/util/XMacroExpander.hpp"
33 : #include "com/sun/star/ucb/XContentProvider.hpp"
34 :
35 : #define EXPAND_PROTOCOL "vnd.sun.star.expand"
36 : #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
37 : #define ARLEN(x) sizeof (x) / sizeof *(x)
38 :
39 :
40 : using namespace ::com::sun::star;
41 : using ::rtl::OUString;
42 :
43 : namespace
44 : {
45 :
46 0 : struct MutexHolder
47 : {
48 : mutable ::osl::Mutex m_mutex;
49 : };
50 :
51 : typedef ::cppu::WeakComponentImplHelper2<
52 : lang::XServiceInfo, ucb::XContentProvider > t_impl_helper;
53 :
54 : //==============================================================================
55 : class ExpandContentProviderImpl : protected MutexHolder, public t_impl_helper
56 : {
57 : uno::Reference< uno::XComponentContext > m_xComponentContext;
58 : uno::Reference< util::XMacroExpander > m_xMacroExpander;
59 : OUString expandUri(
60 : uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) const;
61 :
62 : protected:
63 : inline void check() const;
64 : virtual void SAL_CALL disposing();
65 :
66 : public:
67 0 : inline ExpandContentProviderImpl(
68 : uno::Reference< uno::XComponentContext > const & xComponentContext )
69 : : t_impl_helper( m_mutex ),
70 : m_xComponentContext( xComponentContext ),
71 : m_xMacroExpander(
72 0 : xComponentContext->getValueByName(
73 0 : OUSTR("/singletons/com.sun.star.util.theMacroExpander") ),
74 0 : uno::UNO_QUERY_THROW )
75 0 : {}
76 : virtual ~ExpandContentProviderImpl() throw ();
77 :
78 : // XServiceInfo
79 : virtual OUString SAL_CALL getImplementationName()
80 : throw (uno::RuntimeException);
81 : virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName )
82 : throw (uno::RuntimeException);
83 : virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames()
84 : throw (uno::RuntimeException);
85 :
86 : // XContentProvider
87 : virtual uno::Reference< ucb::XContent > SAL_CALL queryContent(
88 : uno::Reference< ucb::XContentIdentifier > const & xIdentifier )
89 : throw (ucb::IllegalIdentifierException, uno::RuntimeException);
90 : virtual sal_Int32 SAL_CALL compareContentIds(
91 : uno::Reference< ucb::XContentIdentifier > const & xId1,
92 : uno::Reference< ucb::XContentIdentifier > const & xId2 )
93 : throw (uno::RuntimeException);
94 : };
95 :
96 : //______________________________________________________________________________
97 0 : inline void ExpandContentProviderImpl::check() const
98 : {
99 : // xxx todo guard?
100 : // MutexGuard guard( m_mutex );
101 0 : if (rBHelper.bInDispose || rBHelper.bDisposed)
102 : {
103 : throw lang::DisposedException(
104 : OUSTR("expand content provider instance has "
105 : "already been disposed!"),
106 : static_cast< OWeakObject * >(
107 0 : const_cast< ExpandContentProviderImpl * >(this) ) );
108 : }
109 0 : }
110 :
111 : //______________________________________________________________________________
112 0 : ExpandContentProviderImpl::~ExpandContentProviderImpl() throw ()
113 : {
114 0 : }
115 :
116 : //______________________________________________________________________________
117 0 : void ExpandContentProviderImpl::disposing()
118 : {
119 0 : }
120 :
121 : //==============================================================================
122 0 : static uno::Reference< uno::XInterface > SAL_CALL create(
123 : uno::Reference< uno::XComponentContext > const & xComponentContext )
124 : SAL_THROW( (uno::Exception) )
125 : {
126 : return static_cast< ::cppu::OWeakObject * >(
127 0 : new ExpandContentProviderImpl( xComponentContext ) );
128 : }
129 :
130 : //==============================================================================
131 0 : static OUString SAL_CALL implName()
132 : {
133 0 : return OUSTR("com.sun.star.comp.ucb.ExpandContentProvider");
134 : }
135 :
136 : //==============================================================================
137 0 : static uno::Sequence< OUString > SAL_CALL supportedServices()
138 : {
139 : OUString names [] = {
140 : OUSTR("com.sun.star.ucb.ExpandContentProvider"),
141 : OUSTR("com.sun.star.ucb.ContentProvider")
142 0 : };
143 0 : return uno::Sequence< OUString >( names, ARLEN(names) );
144 : }
145 :
146 : // XServiceInfo
147 : //______________________________________________________________________________
148 0 : OUString ExpandContentProviderImpl::getImplementationName()
149 : throw (uno::RuntimeException)
150 : {
151 0 : check();
152 0 : return implName();
153 : }
154 :
155 : //______________________________________________________________________________
156 0 : uno::Sequence< OUString > ExpandContentProviderImpl::getSupportedServiceNames()
157 : throw (uno::RuntimeException)
158 : {
159 0 : check();
160 0 : return supportedServices();
161 : }
162 :
163 : //______________________________________________________________________________
164 0 : sal_Bool ExpandContentProviderImpl::supportsService(
165 : OUString const & serviceName )
166 : throw (uno::RuntimeException)
167 : {
168 : // check();
169 0 : uno::Sequence< OUString > supported_services( getSupportedServiceNames() );
170 0 : OUString const * ar = supported_services.getConstArray();
171 0 : for ( sal_Int32 pos = supported_services.getLength(); pos--; )
172 : {
173 0 : if (ar[ pos ].equals( serviceName ))
174 0 : return true;
175 : }
176 0 : return false;
177 : }
178 :
179 : //______________________________________________________________________________
180 0 : OUString ExpandContentProviderImpl::expandUri(
181 : uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) const
182 : {
183 0 : OUString uri( xIdentifier->getContentIdentifier() );
184 0 : if (uri.compareToAscii(
185 0 : RTL_CONSTASCII_STRINGPARAM(EXPAND_PROTOCOL ":") ) != 0)
186 : {
187 : throw ucb::IllegalIdentifierException(
188 : OUSTR("expected protocol " EXPAND_PROTOCOL "!"),
189 : static_cast< OWeakObject * >(
190 0 : const_cast< ExpandContentProviderImpl * >(this) ) );
191 : }
192 : // cut protocol
193 0 : OUString str( uri.copy( sizeof (EXPAND_PROTOCOL ":") -1 ) );
194 : // decode uric class chars
195 : str = ::rtl::Uri::decode(
196 0 : str, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
197 : // expand macro string
198 0 : return m_xMacroExpander->expandMacros( str );
199 : }
200 :
201 : // XContentProvider
202 : //______________________________________________________________________________
203 0 : uno::Reference< ucb::XContent > ExpandContentProviderImpl::queryContent(
204 : uno::Reference< ucb::XContentIdentifier > const & xIdentifier )
205 : throw (ucb::IllegalIdentifierException, uno::RuntimeException)
206 : {
207 0 : check();
208 0 : OUString uri( expandUri( xIdentifier ) );
209 :
210 0 : ::ucbhelper::Content ucb_content;
211 0 : if (::ucbhelper::Content::create(
212 : uri, uno::Reference< ucb::XCommandEnvironment >(),
213 0 : m_xComponentContext, ucb_content ))
214 : {
215 0 : return ucb_content.get();
216 : }
217 : else
218 : {
219 0 : return uno::Reference< ucb::XContent >();
220 0 : }
221 : }
222 :
223 : //______________________________________________________________________________
224 0 : sal_Int32 ExpandContentProviderImpl::compareContentIds(
225 : uno::Reference< ucb::XContentIdentifier > const & xId1,
226 : uno::Reference< ucb::XContentIdentifier > const & xId2 )
227 : throw (uno::RuntimeException)
228 : {
229 0 : check();
230 : try
231 : {
232 0 : OUString uri1( expandUri( xId1 ) );
233 0 : OUString uri2( expandUri( xId2 ) );
234 0 : return uri1.compareTo( uri2 );
235 : }
236 0 : catch (const ucb::IllegalIdentifierException & exc)
237 : {
238 : (void) exc; // unused
239 : OSL_FAIL(
240 : ::rtl::OUStringToOString(
241 : exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
242 0 : return -1;
243 : }
244 : }
245 :
246 : static const ::cppu::ImplementationEntry s_entries [] =
247 : {
248 : {
249 : create,
250 : implName,
251 : supportedServices,
252 : ::cppu::createSingleComponentFactory,
253 : 0, 0
254 : },
255 : { 0, 0, 0, 0, 0, 0 }
256 : };
257 :
258 : }
259 :
260 : extern "C"
261 : {
262 :
263 0 : SAL_DLLPUBLIC_EXPORT void * SAL_CALL ucpexpand1_component_getFactory(
264 : const sal_Char * pImplName,
265 : lang::XMultiServiceFactory * pServiceManager,
266 : registry::XRegistryKey * pRegistryKey )
267 : {
268 : return ::cppu::component_getFactoryHelper(
269 0 : pImplName, pServiceManager, pRegistryKey, s_entries );
270 : }
271 :
272 : }
273 :
274 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|