LibreOffice Module ucb (master)  1
ucpexpand.cxx
Go to the documentation of this file.
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>
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:
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 
86 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 
100 void ExpandContentProviderImpl::disposing()
101 {
102 }
103 
104 
105 uno::Reference< uno::XInterface > create(
106  uno::Reference< uno::XComponentContext > const & xComponentContext )
107 {
108  return static_cast< ::cppu::OWeakObject * >(
109  new ExpandContentProviderImpl( xComponentContext ) );
110 }
111 
112 
113 OUString implName()
114 {
115  return "com.sun.star.comp.ucb.ExpandContentProvider";
116 }
117 
118 
119 uno::Sequence< OUString > supportedServices()
120 {
121  return uno::Sequence< OUString > {
122  OUString("com.sun.star.ucb.ExpandContentProvider"),
123  OUString("com.sun.star.ucb.ContentProvider")
124  };
125 }
126 
127 // XServiceInfo
128 
129 OUString ExpandContentProviderImpl::getImplementationName()
130 {
131  check();
132  return implName();
133 }
134 
135 
136 uno::Sequence< OUString > ExpandContentProviderImpl::getSupportedServiceNames()
137 {
138  check();
139  return supportedServices();
140 }
141 
142 sal_Bool ExpandContentProviderImpl::supportsService(OUString const & serviceName )
143 {
144  return cppu::supportsService(this, serviceName);
145 }
146 
147 OUString ExpandContentProviderImpl::expandUri(
148  uno::Reference< ucb::XContentIdentifier > const & xIdentifier ) const
149 {
150  OUString uri( xIdentifier->getContentIdentifier() );
151  if (!uri.startsWith(EXPAND_PROTOCOL ":"))
152  {
153  throw ucb::IllegalIdentifierException(
154  "expected protocol " EXPAND_PROTOCOL "!",
155  static_cast< OWeakObject * >(
156  const_cast< ExpandContentProviderImpl * >(this) ) );
157  }
158  // cut protocol
159  OUString str( uri.copy( sizeof (EXPAND_PROTOCOL ":") -1 ) );
160  // decode uric class chars
161  str = ::rtl::Uri::decode(
162  str, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 );
163  // expand macro string
164  return m_xMacroExpander->expandMacros( str );
165 }
166 
167 // XContentProvider
168 
169 uno::Reference< ucb::XContent > ExpandContentProviderImpl::queryContent(
170  uno::Reference< ucb::XContentIdentifier > const & xIdentifier )
171 {
172  check();
173  OUString uri( expandUri( xIdentifier ) );
174 
175  ::ucbhelper::Content ucb_content;
177  uri, uno::Reference< ucb::XCommandEnvironment >(),
178  m_xComponentContext, ucb_content ))
179  {
180  return ucb_content.get();
181  }
182  else
183  {
184  return uno::Reference< ucb::XContent >();
185  }
186 }
187 
188 
189 sal_Int32 ExpandContentProviderImpl::compareContentIds(
190  uno::Reference< ucb::XContentIdentifier > const & xId1,
191  uno::Reference< ucb::XContentIdentifier > const & xId2 )
192 {
193  check();
194  try
195  {
196  OUString uri1( expandUri( xId1 ) );
197  OUString uri2( expandUri( xId2 ) );
198  return uri1.compareTo( uri2 );
199  }
200  catch (const ucb::IllegalIdentifierException &)
201  {
202  TOOLS_WARN_EXCEPTION( "ucb", "" );
203  return -1;
204  }
205 }
206 
207 static const ::cppu::ImplementationEntry s_entries [] =
208 {
209  {
210  create,
211  implName,
212  supportedServices,
213  ::cppu::createSingleComponentFactory,
214  nullptr, 0
215  },
216  { nullptr, nullptr, nullptr, nullptr, nullptr, 0 }
217 };
218 
219 }
220 
221 extern "C"
222 {
223 
224 SAL_DLLPUBLIC_EXPORT void * ucpexpand1_component_getFactory(
225  const sal_Char * pImplName,
226  void * pServiceManager,
227  void * pRegistryKey )
228 {
229  return ::cppu::component_getFactoryHelper(
230  pImplName, pServiceManager, pRegistryKey, s_entries );
231 }
232 
233 }
234 
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool create(const OUString &rURL, const css::uno::Reference< css::ucb::XCommandEnvironment > &rEnv, const css::uno::Reference< css::uno::XComponentContext > &rCtx, Content &rContent)
OUString getImplementationName()
char sal_Char
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Reference< deployment::XPackageRegistry > create(Reference< deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, Reference< XComponentContext > const &xComponentContext)
SAL_DLLPUBLIC_EXPORT void * ucpexpand1_component_getFactory(const sal_Char *pImplName, void *pServiceManager, void *pRegistryKey)
Definition: ucpexpand.cxx:224
#define TOOLS_WARN_EXCEPTION(area, stream)
css::uno::Sequence< OUString > getSupportedServiceNames()
unsigned char sal_Bool
css::uno::Reference< css::ucb::XContent > get() const
DESKTOP_DEPLOYMENTMISC_DLLPUBLIC css::uno::Sequence< css::uno::Reference< css::xml::dom::XElement > > check(dp_misc::DescriptionInfoset const &infoset)
#define EXPAND_PROTOCOL
Definition: ucpexpand.cxx:36
def expandUri(uri)