1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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/theMacroExpander.hpp"
33 #include "com/sun/star/ucb/XContentProvider.hpp"
35 #define EXPAND_PROTOCOL "vnd.sun.star.expand"
36 #define ARLEN(x) sizeof (x) / sizeof *(x)
39 using namespace ::com::sun::star
;
46 mutable ::osl::Mutex m_mutex
;
49 typedef ::cppu::WeakComponentImplHelper2
<
50 lang::XServiceInfo
, ucb::XContentProvider
> t_impl_helper
;
52 //==============================================================================
53 class ExpandContentProviderImpl
: protected MutexHolder
, public t_impl_helper
55 uno::Reference
< uno::XComponentContext
> m_xComponentContext
;
56 uno::Reference
< util::XMacroExpander
> m_xMacroExpander
;
58 uno::Reference
< ucb::XContentIdentifier
> const & xIdentifier
) const;
61 inline void check() const;
62 virtual void SAL_CALL
disposing();
65 inline ExpandContentProviderImpl(
66 uno::Reference
< uno::XComponentContext
> const & xComponentContext
)
67 : t_impl_helper( m_mutex
),
68 m_xComponentContext( xComponentContext
),
69 m_xMacroExpander( util::theMacroExpander::get(xComponentContext
) )
71 virtual ~ExpandContentProviderImpl() throw ();
74 virtual OUString SAL_CALL
getImplementationName()
75 throw (uno::RuntimeException
);
76 virtual sal_Bool SAL_CALL
supportsService( OUString
const & serviceName
)
77 throw (uno::RuntimeException
);
78 virtual uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames()
79 throw (uno::RuntimeException
);
82 virtual uno::Reference
< ucb::XContent
> SAL_CALL
queryContent(
83 uno::Reference
< ucb::XContentIdentifier
> const & xIdentifier
)
84 throw (ucb::IllegalIdentifierException
, uno::RuntimeException
);
85 virtual sal_Int32 SAL_CALL
compareContentIds(
86 uno::Reference
< ucb::XContentIdentifier
> const & xId1
,
87 uno::Reference
< ucb::XContentIdentifier
> const & xId2
)
88 throw (uno::RuntimeException
);
91 //______________________________________________________________________________
92 inline void ExpandContentProviderImpl::check() const
95 // MutexGuard guard( m_mutex );
96 if (rBHelper
.bInDispose
|| rBHelper
.bDisposed
)
98 throw lang::DisposedException(
99 "expand content provider instance has "
100 "already been disposed!",
101 static_cast< OWeakObject
* >(
102 const_cast< ExpandContentProviderImpl
* >(this) ) );
106 //______________________________________________________________________________
107 ExpandContentProviderImpl::~ExpandContentProviderImpl() throw ()
111 //______________________________________________________________________________
112 void ExpandContentProviderImpl::disposing()
116 //==============================================================================
117 static uno::Reference
< uno::XInterface
> SAL_CALL
create(
118 uno::Reference
< uno::XComponentContext
> const & xComponentContext
)
119 SAL_THROW( (uno::Exception
) )
121 return static_cast< ::cppu::OWeakObject
* >(
122 new ExpandContentProviderImpl( xComponentContext
) );
125 //==============================================================================
126 static OUString SAL_CALL
implName()
128 return OUString("com.sun.star.comp.ucb.ExpandContentProvider");
131 //==============================================================================
132 static uno::Sequence
< OUString
> SAL_CALL
supportedServices()
134 OUString names
[] = {
135 OUString("com.sun.star.ucb.ExpandContentProvider"),
136 OUString("com.sun.star.ucb.ContentProvider")
138 return uno::Sequence
< OUString
>( names
, ARLEN(names
) );
142 //______________________________________________________________________________
143 OUString
ExpandContentProviderImpl::getImplementationName()
144 throw (uno::RuntimeException
)
150 //______________________________________________________________________________
151 uno::Sequence
< OUString
> ExpandContentProviderImpl::getSupportedServiceNames()
152 throw (uno::RuntimeException
)
155 return supportedServices();
158 //______________________________________________________________________________
159 sal_Bool
ExpandContentProviderImpl::supportsService(
160 OUString
const & serviceName
)
161 throw (uno::RuntimeException
)
164 uno::Sequence
< OUString
> supported_services( getSupportedServiceNames() );
165 OUString
const * ar
= supported_services
.getConstArray();
166 for ( sal_Int32 pos
= supported_services
.getLength(); pos
--; )
168 if (ar
[ pos
].equals( serviceName
))
174 //______________________________________________________________________________
175 OUString
ExpandContentProviderImpl::expandUri(
176 uno::Reference
< ucb::XContentIdentifier
> const & xIdentifier
) const
178 OUString
uri( xIdentifier
->getContentIdentifier() );
179 if (!uri
.startsWith(EXPAND_PROTOCOL
":"))
181 throw ucb::IllegalIdentifierException(
182 "expected protocol " EXPAND_PROTOCOL
"!",
183 static_cast< OWeakObject
* >(
184 const_cast< ExpandContentProviderImpl
* >(this) ) );
187 OUString
str( uri
.copy( sizeof (EXPAND_PROTOCOL
":") -1 ) );
188 // decode uric class chars
189 str
= ::rtl::Uri::decode(
190 str
, rtl_UriDecodeWithCharset
, RTL_TEXTENCODING_UTF8
);
191 // expand macro string
192 return m_xMacroExpander
->expandMacros( str
);
196 //______________________________________________________________________________
197 uno::Reference
< ucb::XContent
> ExpandContentProviderImpl::queryContent(
198 uno::Reference
< ucb::XContentIdentifier
> const & xIdentifier
)
199 throw (ucb::IllegalIdentifierException
, uno::RuntimeException
)
202 OUString
uri( expandUri( xIdentifier
) );
204 ::ucbhelper::Content ucb_content
;
205 if (::ucbhelper::Content::create(
206 uri
, uno::Reference
< ucb::XCommandEnvironment
>(),
207 m_xComponentContext
, ucb_content
))
209 return ucb_content
.get();
213 return uno::Reference
< ucb::XContent
>();
217 //______________________________________________________________________________
218 sal_Int32
ExpandContentProviderImpl::compareContentIds(
219 uno::Reference
< ucb::XContentIdentifier
> const & xId1
,
220 uno::Reference
< ucb::XContentIdentifier
> const & xId2
)
221 throw (uno::RuntimeException
)
226 OUString
uri1( expandUri( xId1
) );
227 OUString
uri2( expandUri( xId2
) );
228 return uri1
.compareTo( uri2
);
230 catch (const ucb::IllegalIdentifierException
& exc
)
232 (void) exc
; // unused
235 exc
.Message
, RTL_TEXTENCODING_UTF8
).getStr() );
240 static const ::cppu::ImplementationEntry s_entries
[] =
246 ::cppu::createSingleComponentFactory
,
257 SAL_DLLPUBLIC_EXPORT
void * SAL_CALL
ucpexpand1_component_getFactory(
258 const sal_Char
* pImplName
,
259 lang::XMultiServiceFactory
* pServiceManager
,
260 registry::XRegistryKey
* pRegistryKey
)
262 return ::cppu::component_getFactoryHelper(
263 pImplName
, pServiceManager
, pRegistryKey
, s_entries
);
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */