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>
34#include <cppuhelper/weak.hxx>
36#include <ucbhelper/macros.hxx>
37#include "hierarchyprovider.hxx"
38#include "hierarchycontent.hxx"
39#include "hierarchyuri.hxx"
40
41#include "../inc/urihelper.hxx"
42
43using namespace com::sun::star;
44using namespace hierarchy_ucp;
45
46
47// HierarchyContentProvider Implementation.
48
49
50HierarchyContentProvider::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 noexcept
67{
68 OWeakObject::acquire();
69}
70
72 noexcept
73{
74 OWeakObject::release();
75}
76
77css::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}
104sal_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
115extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
117 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
118{
119 return cppu::acquire(new HierarchyContentProvider(context));
120}
121
122// XContentProvider methods.
123
124
125// virtual
126uno::Reference< ucb::XContent > SAL_CALL
128 const uno::Reference< ucb::XContentIdentifier >& Identifier )
129{
130 HierarchyUri aUri( Identifier->getContentIdentifier() );
131 if ( !aUri.isValid() )
132 throw ucb::IllegalIdentifierException();
133
134 // Encode URL and create new Id. This may "correct" user-typed-in URL's.
135 uno::Reference< ucb::XContentIdentifier > xCanonicId
136 = new ::ucbhelper::ContentIdentifier( ::ucb_impl::urihelper::encodeURI( aUri.getUri() ) );
137 osl::MutexGuard aGuard( m_aMutex );
138
139 // Check, if a content with given id already exists...
140 uno::Reference< ucb::XContent > xContent
141 = queryExistingContent( xCanonicId );
142 if ( xContent.is() )
143 return xContent;
144
145 // Create a new content.
146 xContent = HierarchyContent::create( m_xContext, this, xCanonicId );
147 registerNewContent( xContent );
148
149 if ( xContent.is() && !xContent->getIdentifier().is() )
150 throw ucb::IllegalIdentifierException();
151
152 return xContent;
153}
154
155
156// XInitialization methods.
157
158
159// virtual
161 const uno::Sequence< uno::Any >& aArguments )
162{
163 if ( aArguments.hasElements() )
164 OSL_FAIL( "HierarchyContentProvider::initialize : not supported!" );
165}
166
167
168// Non-interface methods.
169
170
171uno::Reference< lang::XMultiServiceFactory >
173 const OUString & rServiceSpecifier )
174{
175 osl::MutexGuard aGuard( m_aMutex );
176 ConfigProviderMap::iterator it = m_aConfigProviderMap.find(
177 rServiceSpecifier );
178 if ( it == m_aConfigProviderMap.end() )
179 {
180 try
181 {
183 aEntry.xConfigProvider.set(
184 m_xContext->getServiceManager()->createInstanceWithContext(rServiceSpecifier, m_xContext),
185 uno::UNO_QUERY );
186
187 if ( aEntry.xConfigProvider.is() )
188 {
189 m_aConfigProviderMap[ rServiceSpecifier ] = aEntry;
190 return aEntry.xConfigProvider;
191 }
192 }
193 catch ( uno::Exception const & )
194 {
195// OSL_FAIL( // "HierarchyContentProvider::getConfigProvider - "
196// "caught exception!" );
197 }
198
199 OSL_FAIL( "HierarchyContentProvider::getConfigProvider - "
200 "No config provider!" );
201
202 return uno::Reference< lang::XMultiServiceFactory >();
203 }
204
205 return (*it).second.xConfigProvider;
206}
207
208uno::Reference< container::XHierarchicalNameAccess >
210 const OUString & rServiceSpecifier )
211{
212 osl::MutexGuard aGuard( m_aMutex );
213 ConfigProviderMap::iterator it = m_aConfigProviderMap.find(
214 rServiceSpecifier );
215 if (it == m_aConfigProviderMap.end())
216 return uno::Reference< container::XHierarchicalNameAccess >();
217
218 if ( !( (*it).second.xRootReadAccess.is() ) )
219 {
220 if ( (*it).second.bTriedToGetRootReadAccess )
221 {
222 OSL_FAIL( "HierarchyContentProvider::getRootConfigReadNameAccess - "
223 "Unable to read any config data! -> #82494#" );
224 return uno::Reference< container::XHierarchicalNameAccess >();
225 }
226
227 try
228 {
229 uno::Reference< lang::XMultiServiceFactory > xConfigProv
230 = getConfigProvider( rServiceSpecifier );
231
232 if ( xConfigProv.is() )
233 {
234 beans::PropertyValue aProperty;
235 aProperty.Name = "nodepath" ;
236 aProperty.Value <<= OUString(); // root path
237 uno::Sequence< uno::Any > aArguments{ uno::Any(aProperty) };
238
239 (*it).second.bTriedToGetRootReadAccess = true;
240
241 (*it).second.xRootReadAccess.set(
242 xConfigProv->createInstanceWithArguments(
243 "com.sun.star.ucb.HierarchyDataReadAccess",
244 aArguments ),
245 uno::UNO_QUERY );
246 }
247 }
248 catch ( uno::RuntimeException const & )
249 {
250 throw;
251 }
252 catch ( uno::Exception const & )
253 {
254 // createInstance, createInstanceWithArguments
255
256 OSL_FAIL( "HierarchyContentProvider::getRootConfigReadNameAccess - "
257 "caught Exception!" );
258 }
259 }
260
261 return (*it).second.xRootReadAccess;
262}
263
264uno::Reference< util::XOfficeInstallationDirectories >
266{
267 if ( !m_xOfficeInstDirs.is() )
268 {
269 osl::MutexGuard aGuard( m_aMutex );
270 if ( !m_xOfficeInstDirs.is() )
271 {
272 OSL_ENSURE( m_xContext.is(), "No service manager!" );
273
274 m_xOfficeInstDirs = util::theOfficeInstallationDirectories::get(m_xContext);
275 }
276 }
277 return m_xOfficeInstDirs;
278}
279
280/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Reference< css::ucb::XContent > SAL_CALL queryContent(const css::uno::Reference< css::ucb::XContentIdentifier > &Identifier) override
virtual void SAL_CALL acquire() noexcept override
css::uno::Reference< css::lang::XMultiServiceFactory > getConfigProvider(const OUString &rServiceSpecifier)
virtual void SAL_CALL release() noexcept override
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
css::uno::Reference< css::util::XOfficeInstallationDirectories > m_xOfficeInstDirs
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
css::uno::Reference< css::container::XHierarchicalNameAccess > getRootConfigReadNameAccess(const OUString &rServiceSpecifier)
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override
virtual OUString SAL_CALL getImplementationName() override
css::uno::Reference< css::util::XOfficeInstallationDirectories > getOfficeInstallationDirectories()
static rtl::Reference< HierarchyContent > create(const css::uno::Reference< css::uno::XComponentContext > &rxContext, HierarchyContentProvider *pProvider, const css::uno::Reference< css::ucb::XContentIdentifier > &Identifier)
const OUString & getUri() const
rtl::Reference< ContentImplHelper > queryExistingContent(const css::uno::Reference< css::ucb::XContentIdentifier > &Identifier)
void registerNewContent(const css::uno::Reference< css::ucb::XContent > &xContent)
css::uno::Reference< css::uno::XComponentContext > m_xContext
XTYPEPROVIDER_IMPL_4(HierarchyContentProvider, lang::XTypeProvider, lang::XServiceInfo, ucb::XContentProvider, lang::XInitialization)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * ucb_HierarchyContentProvider_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Sequence< PropertyValue > aArguments
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
OUString encodeURI(const OUString &rURI)
Definition: urihelper.hxx:44
css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider
unsigned char sal_Bool