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  "com.sun.star.comp.ucb.HierarchyContentProvider" )
103 static css::uno::Reference< css::uno::XInterface >
104 HierarchyContentProvider_CreateInstance( const css::uno::Reference< css::lang::XMultiServiceFactory> & rSMgr )
105 {
106  css::lang::XServiceInfo* pX = new HierarchyContentProvider( ucbhelper::getComponentContext(rSMgr) );
107  return css::uno::Reference< css::uno::XInterface >::query( pX );
108 }
109 
110 css::uno::Sequence< OUString >
112 {
113  css::uno::Sequence< OUString > aSNS { "com.sun.star.ucb.HierarchyContentProvider" };
114  return aSNS;
115 }
116 
117 // Service factory implementation.
118 
119 
121 
122 
123 // XContentProvider methods.
124 
125 
126 // virtual
127 uno::Reference< ucb::XContent > SAL_CALL
129  const uno::Reference< ucb::XContentIdentifier >& Identifier )
130 {
131  HierarchyUri aUri( Identifier->getContentIdentifier() );
132  if ( !aUri.isValid() )
133  throw ucb::IllegalIdentifierException();
134 
135  // Encode URL and create new Id. This may "correct" user-typed-in URL's.
136  uno::Reference< ucb::XContentIdentifier > xCanonicId
137  = new ::ucbhelper::ContentIdentifier( ::ucb_impl::urihelper::encodeURI( aUri.getUri() ) );
138  osl::MutexGuard aGuard( m_aMutex );
139 
140  // Check, if a content with given id already exists...
141  uno::Reference< ucb::XContent > xContent
142  = queryExistingContent( xCanonicId ).get();
143  if ( xContent.is() )
144  return xContent;
145 
146  // Create a new content.
147  xContent = HierarchyContent::create( m_xContext, this, xCanonicId );
148  registerNewContent( xContent );
149 
150  if ( xContent.is() && !xContent->getIdentifier().is() )
151  throw ucb::IllegalIdentifierException();
152 
153  return xContent;
154 }
155 
156 
157 // XInitialization methods.
158 
159 
160 // virtual
162  const uno::Sequence< uno::Any >& aArguments )
163 {
164  if ( aArguments.hasElements() )
165  OSL_FAIL( "HierarchyContentProvider::initialize : not supported!" );
166 }
167 
168 
169 // Non-interface methods.
170 
171 
172 uno::Reference< lang::XMultiServiceFactory >
174  const OUString & rServiceSpecifier )
175 {
176  osl::MutexGuard aGuard( m_aMutex );
177  ConfigProviderMap::iterator it = m_aConfigProviderMap.find(
178  rServiceSpecifier );
179  if ( it == m_aConfigProviderMap.end() )
180  {
181  try
182  {
183  ConfigProviderMapEntry aEntry;
184  aEntry.xConfigProvider.set(
185  m_xContext->getServiceManager()->createInstanceWithContext(rServiceSpecifier, m_xContext),
186  uno::UNO_QUERY );
187 
188  if ( aEntry.xConfigProvider.is() )
189  {
190  m_aConfigProviderMap[ rServiceSpecifier ] = aEntry;
191  return aEntry.xConfigProvider;
192  }
193  }
194  catch ( uno::Exception const & )
195  {
196 // OSL_FAIL( // "HierarchyContentProvider::getConfigProvider - "
197 // "caught exception!" );
198  }
199 
200  OSL_FAIL( "HierarchyContentProvider::getConfigProvider - "
201  "No config provider!" );
202 
203  return uno::Reference< lang::XMultiServiceFactory >();
204  }
205 
206  return (*it).second.xConfigProvider;
207 }
208 
209 uno::Reference< container::XHierarchicalNameAccess >
211  const OUString & rServiceSpecifier )
212 {
213  osl::MutexGuard aGuard( m_aMutex );
214  ConfigProviderMap::iterator it = m_aConfigProviderMap.find(
215  rServiceSpecifier );
216  if (it == m_aConfigProviderMap.end())
217  return uno::Reference< container::XHierarchicalNameAccess >();
218 
219  if ( !( (*it).second.xRootReadAccess.is() ) )
220  {
221  if ( (*it).second.bTriedToGetRootReadAccess )
222  {
223  OSL_FAIL( "HierarchyContentProvider::getRootConfigReadNameAccess - "
224  "Unable to read any config data! -> #82494#" );
225  return uno::Reference< container::XHierarchicalNameAccess >();
226  }
227 
228  try
229  {
230  uno::Reference< lang::XMultiServiceFactory > xConfigProv
231  = getConfigProvider( rServiceSpecifier );
232 
233  if ( xConfigProv.is() )
234  {
235  uno::Sequence< uno::Any > aArguments( 1 );
236  beans::PropertyValue aProperty;
237  aProperty.Name = "nodepath" ;
238  aProperty.Value <<= OUString(); // root path
239  aArguments[ 0 ] <<= aProperty;
240 
241  (*it).second.bTriedToGetRootReadAccess = true;
242 
243  (*it).second.xRootReadAccess.set(
244  xConfigProv->createInstanceWithArguments(
245  "com.sun.star.ucb.HierarchyDataReadAccess",
246  aArguments ),
247  uno::UNO_QUERY );
248  }
249  }
250  catch ( uno::RuntimeException const & )
251  {
252  throw;
253  }
254  catch ( uno::Exception const & )
255  {
256  // createInstance, createInstanceWithArguments
257 
258  OSL_FAIL( "HierarchyContentProvider::getRootConfigReadNameAccess - "
259  "caught Exception!" );
260  }
261  }
262 
263  return (*it).second.xRootReadAccess;
264 }
265 
266 uno::Reference< util::XOfficeInstallationDirectories >
268 {
269  if ( !m_xOfficeInstDirs.is() )
270  {
271  osl::MutexGuard aGuard( m_aMutex );
272  if ( !m_xOfficeInstDirs.is() )
273  {
274  OSL_ENSURE( m_xContext.is(), "No service manager!" );
275 
276  m_xOfficeInstDirs = util::theOfficeInstallationDirectories::get(m_xContext);
277  }
278  }
279  return m_xOfficeInstDirs;
280 }
281 
282 /* 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