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 <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"
36 #define EXPAND_PROTOCOL "vnd.sun.star.expand"
37 #define ARLEN(x) sizeof (x) / sizeof *(x)
40 using namespace ::com::sun::star
;
47 mutable ::osl::Mutex m_mutex
;
50 typedef ::cppu::WeakComponentImplHelper2
<
51 lang::XServiceInfo
, ucb::XContentProvider
> t_impl_helper
;
54 class ExpandContentProviderImpl
: protected MutexHolder
, public t_impl_helper
56 uno::Reference
< uno::XComponentContext
> m_xComponentContext
;
57 uno::Reference
< util::XMacroExpander
> m_xMacroExpander
;
59 uno::Reference
< ucb::XContentIdentifier
> const & xIdentifier
) const;
62 inline void check() const;
63 virtual void SAL_CALL
disposing() SAL_OVERRIDE
;
66 inline ExpandContentProviderImpl(
67 uno::Reference
< uno::XComponentContext
> const & xComponentContext
)
68 : t_impl_helper( m_mutex
),
69 m_xComponentContext( xComponentContext
),
70 m_xMacroExpander( util::theMacroExpander::get(xComponentContext
) )
72 virtual ~ExpandContentProviderImpl() throw ();
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
;
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
;
93 inline void ExpandContentProviderImpl::check() const
96 // MutexGuard guard( m_mutex );
97 if (rBHelper
.bInDispose
|| rBHelper
.bDisposed
)
99 throw lang::DisposedException(
100 "expand content provider instance has "
101 "already been disposed!",
102 static_cast< OWeakObject
* >(
103 const_cast< ExpandContentProviderImpl
* >(this) ) );
108 ExpandContentProviderImpl::~ExpandContentProviderImpl() throw ()
113 void ExpandContentProviderImpl::disposing()
118 static uno::Reference
< uno::XInterface
> SAL_CALL
create(
119 uno::Reference
< uno::XComponentContext
> const & xComponentContext
)
121 return static_cast< ::cppu::OWeakObject
* >(
122 new ExpandContentProviderImpl( xComponentContext
) );
126 static OUString SAL_CALL
implName()
128 return OUString("com.sun.star.comp.ucb.ExpandContentProvider");
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
) );
143 OUString
ExpandContentProviderImpl::getImplementationName()
144 throw (uno::RuntimeException
, std::exception
)
151 uno::Sequence
< OUString
> ExpandContentProviderImpl::getSupportedServiceNames()
152 throw (uno::RuntimeException
, std::exception
)
155 return supportedServices();
158 sal_Bool
ExpandContentProviderImpl::supportsService(OUString
const & serviceName
)
159 throw (uno::RuntimeException
, std::exception
)
161 return cppu::supportsService(this, serviceName
);
164 OUString
ExpandContentProviderImpl::expandUri(
165 uno::Reference
< ucb::XContentIdentifier
> const & xIdentifier
) const
167 OUString
uri( xIdentifier
->getContentIdentifier() );
168 if (!uri
.startsWith(EXPAND_PROTOCOL
":"))
170 throw ucb::IllegalIdentifierException(
171 "expected protocol " EXPAND_PROTOCOL
"!",
172 static_cast< OWeakObject
* >(
173 const_cast< ExpandContentProviderImpl
* >(this) ) );
176 OUString
str( uri
.copy( sizeof (EXPAND_PROTOCOL
":") -1 ) );
177 // decode uric class chars
178 str
= ::rtl::Uri::decode(
179 str
, rtl_UriDecodeWithCharset
, RTL_TEXTENCODING_UTF8
);
180 // expand macro string
181 return m_xMacroExpander
->expandMacros( str
);
186 uno::Reference
< ucb::XContent
> ExpandContentProviderImpl::queryContent(
187 uno::Reference
< ucb::XContentIdentifier
> const & xIdentifier
)
188 throw (ucb::IllegalIdentifierException
, uno::RuntimeException
, std::exception
)
191 OUString
uri( expandUri( xIdentifier
) );
193 ::ucbhelper::Content ucb_content
;
194 if (::ucbhelper::Content::create(
195 uri
, uno::Reference
< ucb::XCommandEnvironment
>(),
196 m_xComponentContext
, ucb_content
))
198 return ucb_content
.get();
202 return uno::Reference
< ucb::XContent
>();
207 sal_Int32
ExpandContentProviderImpl::compareContentIds(
208 uno::Reference
< ucb::XContentIdentifier
> const & xId1
,
209 uno::Reference
< ucb::XContentIdentifier
> const & xId2
)
210 throw (uno::RuntimeException
, std::exception
)
215 OUString
uri1( expandUri( xId1
) );
216 OUString
uri2( expandUri( xId2
) );
217 return uri1
.compareTo( uri2
);
219 catch (const ucb::IllegalIdentifierException
& exc
)
221 (void) exc
; // unused
224 exc
.Message
, RTL_TEXTENCODING_UTF8
).getStr() );
229 static const ::cppu::ImplementationEntry s_entries
[] =
235 ::cppu::createSingleComponentFactory
,
246 SAL_DLLPUBLIC_EXPORT
void * SAL_CALL
ucpexpand1_component_getFactory(
247 const sal_Char
* pImplName
,
248 void * pServiceManager
,
249 void * pRegistryKey
)
251 return ::cppu::component_getFactoryHelper(
252 pImplName
, pServiceManager
, pRegistryKey
, s_entries
);
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */