LibreOffice Module ucb (master)  1
hierarchyprovider.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 /**************************************************************************
22  TODO
23  **************************************************************************
24 
25  - XInitialization::initialize does not work any longer!
26 
27  *************************************************************************/
28 #include <osl/diagnose.h>
29 #include <com/sun/star/beans/PropertyValue.hpp>
30 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
31 #include <com/sun/star/ucb/IllegalIdentifierException.hpp>
32 #include <com/sun/star/util/theOfficeInstallationDirectories.hpp>
36 #include <ucbhelper/macros.hxx>
37 #include "hierarchyprovider.hxx"
38 #include "hierarchycontent.hxx"
39 #include "hierarchyuri.hxx"
40 
41 #include "../inc/urihelper.hxx"
42 
43 using namespace com::sun::star;
44 using namespace hierarchy_ucp;
45 
46 
47 // HierarchyContentProvider Implementation.
48 
49 
50 HierarchyContentProvider::HierarchyContentProvider(
51  const uno::Reference< uno::XComponentContext >& rxContext )
52 : ::ucbhelper::ContentProviderImplHelper( rxContext )
53 {
54 }
55 
56 
57 // virtual
59 {
60 }
61 
62 
63 // XInterface methods.
64 
66  throw()
67 {
68  OWeakObject::acquire();
69 }
70 
72  throw()
73 {
74  OWeakObject::release();
75 }
76 
77 css::uno::Any SAL_CALL HierarchyContentProvider::queryInterface( const css::uno::Type & rType )
78 {
79  css::uno::Any aRet = cppu::queryInterface( rType,
80  static_cast< lang::XTypeProvider* >(this),
81  static_cast< lang::XServiceInfo* >(this),
82  static_cast< ucb::XContentProvider* >(this),
83  static_cast< lang::XInitialization* >(this)
84  );
85  return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
86 }
87 
88 // XTypeProvider methods.
89 
90 
92  lang::XTypeProvider,
93  lang::XServiceInfo,
94  ucb::XContentProvider,
95  lang::XInitialization );
96 
97 
98 // XServiceInfo methods.
99 
101 {
102  return "com.sun.star.comp.ucb.HierarchyContentProvider";
103 }
104 sal_Bool SAL_CALL HierarchyContentProvider::supportsService( const OUString& ServiceName )
105 {
106  return cppu::supportsService( this, ServiceName );
107 }
109 {
110  return { "com.sun.star.ucb.HierarchyContentProvider" };
111 }
112 
113 // Service factory implementation.
114 
115 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
117  css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
118 {
120  g_Instance->acquire();
121  return static_cast<cppu::OWeakObject*>(g_Instance.get());
122 }
123 
124 // XContentProvider methods.
125 
126 
127 // virtual
128 uno::Reference< ucb::XContent > SAL_CALL
130  const uno::Reference< ucb::XContentIdentifier >& Identifier )
131 {
132  HierarchyUri aUri( Identifier->getContentIdentifier() );
133  if ( !aUri.isValid() )
134  throw ucb::IllegalIdentifierException();
135 
136  // Encode URL and create new Id. This may "correct" user-typed-in URL's.
137  uno::Reference< ucb::XContentIdentifier > xCanonicId
138  = new ::ucbhelper::ContentIdentifier( ::ucb_impl::urihelper::encodeURI( aUri.getUri() ) );
139  osl::MutexGuard aGuard( m_aMutex );
140 
141  // Check, if a content with given id already exists...
142  uno::Reference< ucb::XContent > xContent
143  = queryExistingContent( xCanonicId ).get();
144  if ( xContent.is() )
145  return xContent;
146 
147  // Create a new content.
148  xContent = HierarchyContent::create( m_xContext, this, xCanonicId );
149  registerNewContent( xContent );
150 
151  if ( xContent.is() && !xContent->getIdentifier().is() )
152  throw ucb::IllegalIdentifierException();
153 
154  return xContent;
155 }
156 
157 
158 // XInitialization methods.
159 
160 
161 // virtual
163  const uno::Sequence< uno::Any >& aArguments )
164 {
165  if ( aArguments.hasElements() )
166  OSL_FAIL( "HierarchyContentProvider::initialize : not supported!" );
167 }
168 
169 
170 // Non-interface methods.
171 
172 
173 uno::Reference< lang::XMultiServiceFactory >
175  const OUString & rServiceSpecifier )
176 {
177  osl::MutexGuard aGuard( m_aMutex );
178  ConfigProviderMap::iterator it = m_aConfigProviderMap.find(
179  rServiceSpecifier );
180  if ( it == m_aConfigProviderMap.end() )
181  {
182  try
183  {
184  ConfigProviderMapEntry aEntry;
185  aEntry.xConfigProvider.set(
186  m_xContext->getServiceManager()->createInstanceWithContext(rServiceSpecifier, m_xContext),
187  uno::UNO_QUERY );
188 
189  if ( aEntry.xConfigProvider.is() )
190  {
191  m_aConfigProviderMap[ rServiceSpecifier ] = aEntry;
192  return aEntry.xConfigProvider;
193  }
194  }
195  catch ( uno::Exception const & )
196  {
197 // OSL_FAIL( // "HierarchyContentProvider::getConfigProvider - "
198 // "caught exception!" );
199  }
200 
201  OSL_FAIL( "HierarchyContentProvider::getConfigProvider - "
202  "No config provider!" );
203 
204  return uno::Reference< lang::XMultiServiceFactory >();
205  }
206 
207  return (*it).second.xConfigProvider;
208 }
209 
210 uno::Reference< container::XHierarchicalNameAccess >
212  const OUString & rServiceSpecifier )
213 {
214  osl::MutexGuard aGuard( m_aMutex );
215  ConfigProviderMap::iterator it = m_aConfigProviderMap.find(
216  rServiceSpecifier );
217  if (it == m_aConfigProviderMap.end())
218  return uno::Reference< container::XHierarchicalNameAccess >();
219 
220  if ( !( (*it).second.xRootReadAccess.is() ) )
221  {
222  if ( (*it).second.bTriedToGetRootReadAccess )
223  {
224  OSL_FAIL( "HierarchyContentProvider::getRootConfigReadNameAccess - "
225  "Unable to read any config data! -> #82494#" );
226  return uno::Reference< container::XHierarchicalNameAccess >();
227  }
228 
229  try
230  {
231  uno::Reference< lang::XMultiServiceFactory > xConfigProv
232  = getConfigProvider( rServiceSpecifier );
233 
234  if ( xConfigProv.is() )
235  {
236  uno::Sequence< uno::Any > aArguments( 1 );
237  beans::PropertyValue aProperty;
238  aProperty.Name = "nodepath" ;
239  aProperty.Value <<= OUString(); // root path
240  aArguments[ 0 ] <<= aProperty;
241 
242  (*it).second.bTriedToGetRootReadAccess = true;
243 
244  (*it).second.xRootReadAccess.set(
245  xConfigProv->createInstanceWithArguments(
246  "com.sun.star.ucb.HierarchyDataReadAccess",
247  aArguments ),
248  uno::UNO_QUERY );
249  }
250  }
251  catch ( uno::RuntimeException const & )
252  {
253  throw;
254  }
255  catch ( uno::Exception const & )
256  {
257  // createInstance, createInstanceWithArguments
258 
259  OSL_FAIL( "HierarchyContentProvider::getRootConfigReadNameAccess - "
260  "caught Exception!" );
261  }
262  }
263 
264  return (*it).second.xRootReadAccess;
265 }
266 
267 uno::Reference< util::XOfficeInstallationDirectories >
269 {
270  if ( !m_xOfficeInstDirs.is() )
271  {
272  osl::MutexGuard aGuard( m_aMutex );
273  if ( !m_xOfficeInstDirs.is() )
274  {
275  OSL_ENSURE( m_xContext.is(), "No service manager!" );
276 
277  m_xOfficeInstDirs = util::theOfficeInstallationDirectories::get(m_xContext);
278  }
279  }
280  return m_xOfficeInstDirs;
281 }
282 
283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XTYPEPROVIDER_IMPL_4(HierarchyContentProvider, lang::XTypeProvider, lang::XServiceInfo, ucb::XContentProvider, lang::XInitialization)
OUString encodeURI(const OUString &rURI)
Definition: urihelper.hxx:45
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual OUString SAL_CALL getImplementationName() override
Sequence< PropertyValue > aArguments
css::uno::Reference< css::container::XHierarchicalNameAccess > getRootConfigReadNameAccess(const OUString &rServiceSpecifier)
virtual void SAL_CALL release() override
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
virtual void SAL_CALL acquire() override
static HierarchyContent * create(const css::uno::Reference< css::uno::XComponentContext > &rxContext, HierarchyContentProvider *pProvider, const css::uno::Reference< css::ucb::XContentIdentifier > &Identifier)
css::uno::Reference< css::util::XOfficeInstallationDirectories > m_xOfficeInstDirs
css::uno::Reference< css::util::XOfficeInstallationDirectories > getOfficeInstallationDirectories()
virtual css::uno::Reference< css::ucb::XContent > SAL_CALL queryContent(const css::uno::Reference< css::ucb::XContentIdentifier > &Identifier) override
unsigned char sal_Bool
rtl::Reference< ContentImplHelper > queryExistingContent(const css::uno::Reference< css::ucb::XContentIdentifier > &Identifier)
css::uno::Reference< css::uno::XComponentContext > m_xContext
css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider
void registerNewContent(const css::uno::Reference< css::ucb::XContent > &xContent)
css::uno::Reference< css::lang::XMultiServiceFactory > getConfigProvider(const OUString &rServiceSpecifier)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * ucb_HierarchyContentProvider_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
uno::Reference< ucb::XContent > xContent
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override