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>
37 #include <ucbhelper/macros.hxx>
38 #include "hierarchyprovider.hxx"
39 #include "hierarchycontent.hxx"
40 #include "hierarchyuri.hxx"
41 
42 #include "../inc/urihelper.hxx"
43 
44 using namespace com::sun::star;
45 using namespace hierarchy_ucp;
46 
47 
48 // HierarchyContentProvider Implementation.
49 
50 
51 HierarchyContentProvider::HierarchyContentProvider(
52  const uno::Reference< uno::XComponentContext >& rxContext )
53 : ::ucbhelper::ContentProviderImplHelper( rxContext )
54 {
55 }
56 
57 
58 // virtual
60 {
61 }
62 
63 
64 // XInterface methods.
65 
67  throw()
68 {
69  OWeakObject::acquire();
70 }
71 
73  throw()
74 {
75  OWeakObject::release();
76 }
77 
78 css::uno::Any SAL_CALL HierarchyContentProvider::queryInterface( const css::uno::Type & rType )
79 {
80  css::uno::Any aRet = cppu::queryInterface( rType,
81  static_cast< lang::XTypeProvider* >(this),
82  static_cast< lang::XServiceInfo* >(this),
83  static_cast< ucb::XContentProvider* >(this),
84  static_cast< lang::XInitialization* >(this)
85  );
86  return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
87 }
88 
89 // XTypeProvider methods.
90 
91 
93  lang::XTypeProvider,
94  lang::XServiceInfo,
95  ucb::XContentProvider,
96  lang::XInitialization );
97 
98 
99 // XServiceInfo methods.
100 
102  "com.sun.star.comp.ucb.HierarchyContentProvider" )
104 static css::uno::Reference< css::uno::XInterface >
105 HierarchyContentProvider_CreateInstance( const css::uno::Reference< css::lang::XMultiServiceFactory> & rSMgr )
106 {
107  css::lang::XServiceInfo* pX = new HierarchyContentProvider( ucbhelper::getComponentContext(rSMgr) );
108  return css::uno::Reference< css::uno::XInterface >::query( pX );
109 }
110 
111 css::uno::Sequence< OUString >
113 {
114  css::uno::Sequence< OUString > aSNS { "com.sun.star.ucb.HierarchyContentProvider" };
115  return aSNS;
116 }
117 
118 // Service factory implementation.
119 
120 
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: */
static css::uno::Sequence< OUString > getSupportedServiceNames_Static()
XTYPEPROVIDER_IMPL_4(HierarchyContentProvider, lang::XTypeProvider, lang::XServiceInfo, ucb::XContentProvider, lang::XInitialization)
XSERVICEINFO_COMMOM_IMPL(HierarchyContentProvider,"com.sun.star.comp.ucb.HierarchyContentProvider") static css
OUString encodeURI(const OUString &rURI)
Definition: urihelper.hxx:45
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
UCBHELPER_DLLPUBLIC css::uno::Reference< css::uno::XComponentContext > getComponentContext(css::uno::Reference< css::lang::XMultiServiceFactory > const &factory)
css::uno::Reference< css::container::XHierarchicalNameAccess > getRootConfigReadNameAccess(const OUString &rServiceSpecifier)
virtual void SAL_CALL release() override
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)
ONE_INSTANCE_SERVICE_FACTORY_IMPL(HierarchyContentProvider)
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
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)
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