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