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/compbase.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/util/theMacroExpander.hpp>
32 #include <com/sun/star/ucb/IllegalIdentifierException.hpp>
33 #include <com/sun/star/ucb/XContentProvider.hpp>
34 #include <tools/diagnose_ex.h>
35
36 #define EXPAND_PROTOCOL "vnd.sun.star.expand"
37
38
39 using namespace ::com::sun::star;
40
41 namespace
42 {
43
44 struct MutexHolder
45 {
46 mutable ::osl::Mutex m_mutex;
47 };
48
49 typedef ::cppu::WeakComponentImplHelper<
50 lang::XServiceInfo, ucb::XContentProvider > t_impl_helper;
51
52
53 class ExpandContentProviderImpl : protected MutexHolder, public t_impl_helper
54 {
55 uno::Reference< uno::XComponentContext > m_xComponentContext;
56 uno::Reference< util::XMacroExpander > m_xMacroExpander;
57 OUString expandUri(
58 uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) const;
59
60 protected:
61 void check() const;
62 virtual void SAL_CALL disposing() override;
63
64 public:
ExpandContentProviderImpl(uno::Reference<uno::XComponentContext> const & xComponentContext)65 explicit 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) )
70 {}
71
72 // XServiceInfo
73 virtual OUString SAL_CALL getImplementationName() override;
74 virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName ) override;
75 virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
76
77 // XContentProvider
78 virtual uno::Reference< ucb::XContent > SAL_CALL queryContent(
79 uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) override;
80 virtual sal_Int32 SAL_CALL compareContentIds(
81 uno::Reference< ucb::XContentIdentifier > const & xId1,
82 uno::Reference< ucb::XContentIdentifier > const & xId2 ) override;
83 };
84
85
check() const86 void ExpandContentProviderImpl::check() const
87 {
88 // xxx todo guard?
89 // MutexGuard guard( m_mutex );
90 if (rBHelper.bInDispose || rBHelper.bDisposed)
91 {
92 throw lang::DisposedException(
93 "expand content provider instance has "
94 "already been disposed!",
95 static_cast< OWeakObject * >(
96 const_cast< ExpandContentProviderImpl * >(this) ) );
97 }
98 }
99
disposing()100 void ExpandContentProviderImpl::disposing()
101 {
102 }
103
104
105 // XServiceInfo
106
getImplementationName()107 OUString ExpandContentProviderImpl::getImplementationName()
108 {
109 check();
110 return "com.sun.star.comp.ucb.ExpandContentProvider";
111 }
112
113
getSupportedServiceNames()114 uno::Sequence< OUString > ExpandContentProviderImpl::getSupportedServiceNames()
115 {
116 check();
117 return {
118 "com.sun.star.ucb.ExpandContentProvider",
119 "com.sun.star.ucb.ContentProvider"
120 };
121 }
122
supportsService(OUString const & serviceName)123 sal_Bool ExpandContentProviderImpl::supportsService(OUString const & serviceName )
124 {
125 return cppu::supportsService(this, serviceName);
126 }
127
expandUri(uno::Reference<ucb::XContentIdentifier> const & xIdentifier) const128 OUString ExpandContentProviderImpl::expandUri(
129 uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) const
130 {
131 OUString uri( xIdentifier->getContentIdentifier() );
132 if (!uri.startsWith(EXPAND_PROTOCOL ":"))
133 {
134 throw ucb::IllegalIdentifierException(
135 "expected protocol " EXPAND_PROTOCOL "!",
136 static_cast< OWeakObject * >(
137 const_cast< ExpandContentProviderImpl * >(this) ) );
138 }
139 // cut protocol
140 OUString str( uri.copy( sizeof (EXPAND_PROTOCOL ":") -1 ) );
141 // decode uric class chars
142 str = ::rtl::Uri::decode(
143 str, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
144 // expand macro string
145 return m_xMacroExpander->expandMacros( str );
146 }
147
148 // XContentProvider
149
queryContent(uno::Reference<ucb::XContentIdentifier> const & xIdentifier)150 uno::Reference< ucb::XContent > ExpandContentProviderImpl::queryContent(
151 uno::Reference< ucb::XContentIdentifier > const & xIdentifier )
152 {
153 check();
154 OUString uri( expandUri( xIdentifier ) );
155
156 ::ucbhelper::Content ucb_content;
157 if (::ucbhelper::Content::create(
158 uri, uno::Reference< ucb::XCommandEnvironment >(),
159 m_xComponentContext, ucb_content ))
160 {
161 return ucb_content.get();
162 }
163 else
164 {
165 return uno::Reference< ucb::XContent >();
166 }
167 }
168
169
compareContentIds(uno::Reference<ucb::XContentIdentifier> const & xId1,uno::Reference<ucb::XContentIdentifier> const & xId2)170 sal_Int32 ExpandContentProviderImpl::compareContentIds(
171 uno::Reference< ucb::XContentIdentifier > const & xId1,
172 uno::Reference< ucb::XContentIdentifier > const & xId2 )
173 {
174 check();
175 try
176 {
177 OUString uri1( expandUri( xId1 ) );
178 OUString uri2( expandUri( xId2 ) );
179 return uri1.compareTo( uri2 );
180 }
181 catch (const ucb::IllegalIdentifierException &)
182 {
183 TOOLS_WARN_EXCEPTION( "ucb", "" );
184 return -1;
185 }
186 }
187
188
189 }
190
191 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
ucb_expand_ExpandContentProviderImpl_get_implementation(css::uno::XComponentContext * context,css::uno::Sequence<css::uno::Any> const &)192 ucb_expand_ExpandContentProviderImpl_get_implementation(
193 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
194 {
195 return cppu::acquire(new ExpandContentProviderImpl(context));
196 }
197
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
199