fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / xmlhelp / source / cxxhelp / provider / provider.cxx
blobe2ee1dbe6b3962a80fb1c79153e8d9c93598019b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 /**************************************************************************
22 TODO
23 **************************************************************************
25 *************************************************************************/
27 #include <stdio.h>
28 #include <osl/file.hxx>
29 #include <osl/diagnose.h>
30 #include <ucbhelper/contentidentifier.hxx>
31 #include <com/sun/star/frame/XConfigManager.hpp>
32 #include <com/sun/star/beans/PropertyAttribute.hpp>
33 #include <com/sun/star/beans/PropertyValue.hpp>
34 #include <com/sun/star/configuration/theDefaultProvider.hpp>
35 #include <com/sun/star/container/XContainer.hpp>
36 #include <com/sun/star/container/XNameAccess.hpp>
37 #include <com/sun/star/container/XNameReplace.hpp>
38 #include <com/sun/star/uno/XComponentContext.hpp>
39 #include <comphelper/processfactory.hxx>
40 #include <unotools/configmgr.hxx>
41 #include <rtl/bootstrap.hxx>
43 #include "databases.hxx"
44 #include "provider.hxx"
45 #include "content.hxx"
47 using namespace com::sun::star;
48 using namespace chelp;
50 //=========================================================================
51 //=========================================================================
53 // ContentProvider Implementation.
55 //=========================================================================
56 //=========================================================================
58 ContentProvider::ContentProvider(
59 const uno::Reference< uno::XComponentContext >& rxContext )
60 : ::ucbhelper::ContentProviderImplHelper( rxContext ),
61 isInitialized( false ),
62 m_aScheme(MYUCP_URL_SCHEME),
63 m_pDatabases( 0 )
67 //=========================================================================
68 // virtual
69 ContentProvider::~ContentProvider()
71 delete m_pDatabases;
74 //=========================================================================
76 // XInterface methods.
78 //=========================================================================
80 XINTERFACE_IMPL_6( ContentProvider,
81 lang::XTypeProvider,
82 lang::XServiceInfo,
83 ucb::XContentProvider,
84 lang::XComponent,
85 lang::XEventListener, /* base of XContainerListener */
86 container::XContainerListener);
88 //=========================================================================
90 // XTypeProvider methods.
92 //=========================================================================
94 XTYPEPROVIDER_IMPL_5( ContentProvider,
95 lang::XTypeProvider,
96 lang::XServiceInfo,
97 ucb::XContentProvider,
98 lang::XComponent,
99 container::XContainerListener);
101 //=========================================================================
103 // XServiceInfo methods.
105 //=========================================================================
107 OUString SAL_CALL ContentProvider::getImplementationName()
108 throw( uno::RuntimeException )
110 return getImplementationName_Static();
113 OUString ContentProvider::getImplementationName_Static()
115 return OUString("CHelpContentProvider" );
118 sal_Bool SAL_CALL
119 ContentProvider::supportsService(const OUString& ServiceName )
120 throw( uno::RuntimeException )
122 uno::Sequence< OUString > aSNL = getSupportedServiceNames();
123 const OUString* pArray = aSNL.getArray();
124 for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
126 if( pArray[ i ] == ServiceName )
127 return sal_True;
130 return sal_False;
133 uno::Sequence< OUString > SAL_CALL
134 ContentProvider::getSupportedServiceNames()
135 throw( uno::RuntimeException )
137 return getSupportedServiceNames_Static();
140 static uno::Reference< uno::XInterface > SAL_CALL
141 ContentProvider_CreateInstance(
142 const uno::Reference< lang::XMultiServiceFactory> & rSMgr )
143 throw( uno::Exception )
145 lang::XServiceInfo * pX = static_cast< lang::XServiceInfo * >(
146 new ContentProvider( comphelper::getComponentContext(rSMgr) ) );
147 return uno::Reference< uno::XInterface >::query( pX );
150 uno::Sequence< OUString >
151 ContentProvider::getSupportedServiceNames_Static()
153 uno::Sequence< OUString > aSNS( 2 );
154 aSNS.getArray()[ 0 ] =
155 OUString(
156 MYUCP_CONTENT_PROVIDER_SERVICE_NAME1 );
157 aSNS.getArray()[ 1 ] =
158 OUString(
159 MYUCP_CONTENT_PROVIDER_SERVICE_NAME2 );
161 return aSNS;
164 //=========================================================================
166 // Service factory implementation.
168 //=========================================================================
170 ONE_INSTANCE_SERVICE_FACTORY_IMPL( ContentProvider );
172 //=========================================================================
174 // XContentProvider methods.
176 //=========================================================================
178 // virtual
179 uno::Reference< ucb::XContent > SAL_CALL
180 ContentProvider::queryContent(
181 const uno::Reference< ucb::XContentIdentifier >& xCanonicId )
182 throw( ucb::IllegalIdentifierException, uno::RuntimeException )
184 if ( !xCanonicId->getContentProviderScheme()
185 .equalsIgnoreAsciiCase( m_aScheme ) )
186 { // Wrong URL-scheme
187 throw ucb::IllegalIdentifierException();
191 osl::MutexGuard aGuard( m_aMutex );
192 if( !isInitialized )
193 init();
196 if( !m_pDatabases )
197 throw uno::RuntimeException();
199 // Check, if a content with given id already exists...
200 uno::Reference< ucb::XContent > xContent
201 = queryExistingContent( xCanonicId ).get();
202 if ( xContent.is() )
203 return xContent;
205 xContent = new Content( m_xContext, this, xCanonicId, m_pDatabases );
207 // register new content
208 registerNewContent( xContent );
210 // Further checks
212 if ( !xContent->getIdentifier().is() )
213 throw ucb::IllegalIdentifierException();
215 return xContent;
218 void SAL_CALL
219 ContentProvider::dispose()
220 throw ( uno::RuntimeException)
222 if(m_xContainer.is())
224 m_xContainer->removeContainerListener(this);
225 m_xContainer.clear();
229 void SAL_CALL
230 ContentProvider::elementReplaced(const container::ContainerEvent& Event)
231 throw (uno::RuntimeException)
233 if(!m_pDatabases)
234 return;
236 OUString accessor;
237 Event.Accessor >>= accessor;
238 if(accessor.compareToAscii("HelpStyleSheet"))
239 return;
241 OUString replacedElement,element;
242 Event.ReplacedElement >>= replacedElement;
243 Event.Element >>= element;
245 if(replacedElement == element)
246 return;
248 m_pDatabases->changeCSS(element);
251 void ContentProvider::init()
253 osl::MutexGuard aGuard( m_aMutex );
255 isInitialized = true;
256 uno::Reference< lang::XMultiServiceFactory > sProvider(
257 getConfiguration() );
258 uno::Reference< container::XHierarchicalNameAccess > xHierAccess(
259 getHierAccess( sProvider,
260 "org.openoffice.Office.Common" ) );
262 OUString instPath( getKey( xHierAccess,"Path/Current/Help" ) );
263 if( instPath.isEmpty() )
264 // try to determine path from default
265 instPath = OUString( "$(instpath)/help" );
266 // replace anything like $(instpath);
267 subst( instPath );
269 OUString stylesheet( getKey( xHierAccess,"Help/HelpStyleSheet" ) );
272 // now adding as configuration change listener for the stylesheet
273 uno::Reference< container::XNameAccess> xAccess(
274 xHierAccess, uno::UNO_QUERY );
275 if( xAccess.is() )
277 uno::Any aAny =
278 xAccess->getByName( OUString( "Help" ) );
279 aAny >>= m_xContainer;
280 if( m_xContainer.is() )
281 m_xContainer->addContainerListener( this );
284 catch( uno::Exception const & )
288 xHierAccess = getHierAccess( sProvider, "org.openoffice.Setup" );
290 OUString setupversion(
291 getKey( xHierAccess,"Product/ooSetupVersion" ) );
292 OUString setupextension;
296 uno::Reference< lang::XMultiServiceFactory > xConfigProvider =
297 configuration::theDefaultProvider::get( m_xContext );
299 uno::Sequence < uno::Any > lParams(1);
300 beans::PropertyValue aParam ;
301 aParam.Name = OUString("nodepath");
302 aParam.Value <<= OUString("/org.openoffice.Setup/Product");
303 lParams[0] = uno::makeAny(aParam);
305 // open it
306 uno::Reference< uno::XInterface > xCFG( xConfigProvider->createInstanceWithArguments(
307 OUString("com.sun.star.configuration.ConfigurationAccess"),
308 lParams) );
310 uno::Reference< container::XNameAccess > xDirectAccess(xCFG, uno::UNO_QUERY);
311 uno::Any aRet = xDirectAccess->getByName(OUString("ooSetupExtension"));
313 aRet >>= setupextension;
315 catch ( uno::Exception& )
319 OUString productversion(
320 setupversion +
321 OUString( " " ) +
322 setupextension );
324 uno::Sequence< OUString > aImagesZipPaths( 2 );
325 xHierAccess = getHierAccess( sProvider, "org.openoffice.Office.Common" );
327 OUString aPath( getKey( xHierAccess, "Path/Current/UserConfig" ) );
328 subst( aPath );
329 aImagesZipPaths[ 0 ] = aPath;
331 aPath = OUString("$BRAND_BASE_DIR/share/config");
332 rtl::Bootstrap::expandMacros(aPath);
333 aImagesZipPaths[ 1 ] = aPath;
335 sal_Bool showBasic = getBooleanKey(xHierAccess,"Help/ShowBasic");
336 m_pDatabases = new Databases( showBasic,
337 instPath,
338 aImagesZipPaths,
339 utl::ConfigManager::getProductName(),
340 productversion,
341 stylesheet,
342 m_xContext );
345 uno::Reference< lang::XMultiServiceFactory >
346 ContentProvider::getConfiguration() const
348 uno::Reference< lang::XMultiServiceFactory > xProvider;
349 if( m_xContext.is() )
353 xProvider = configuration::theDefaultProvider::get( m_xContext );
355 catch( const uno::Exception& )
357 OSL_ENSURE( xProvider.is(), "cant instantiate configuration" );
361 return xProvider;
364 uno::Reference< container::XHierarchicalNameAccess >
365 ContentProvider::getHierAccess(
366 const uno::Reference< lang::XMultiServiceFactory >& sProvider,
367 const char* file ) const
369 uno::Reference< container::XHierarchicalNameAccess > xHierAccess;
371 if( sProvider.is() )
373 uno::Sequence< uno::Any > seq( 1 );
374 OUString sReaderService(
375 OUString(
376 "com.sun.star.configuration.ConfigurationAccess" ) );
378 seq[ 0 ] <<= OUString::createFromAscii( file );
382 xHierAccess =
383 uno::Reference< container::XHierarchicalNameAccess >(
384 sProvider->createInstanceWithArguments(
385 sReaderService, seq ),
386 uno::UNO_QUERY );
388 catch( const uno::Exception& )
392 return xHierAccess;
397 OUString
398 ContentProvider::getKey(
399 const uno::Reference< container::XHierarchicalNameAccess >& xHierAccess,
400 const char* key ) const
402 OUString instPath;
403 if( xHierAccess.is() )
405 uno::Any aAny;
408 aAny =
409 xHierAccess->getByHierarchicalName(
410 OUString::createFromAscii( key ) );
412 catch( const container::NoSuchElementException& )
415 aAny >>= instPath;
417 return instPath;
420 sal_Bool
421 ContentProvider::getBooleanKey(
422 const uno::Reference< container::XHierarchicalNameAccess >& xHierAccess,
423 const char* key ) const
425 sal_Bool ret = sal_False;
426 if( xHierAccess.is() )
428 uno::Any aAny;
431 aAny =
432 xHierAccess->getByHierarchicalName(
433 OUString::createFromAscii( key ) );
435 catch( const container::NoSuchElementException& )
438 aAny >>= ret;
440 return ret;
443 void ContentProvider::subst( OUString& instpath ) const
445 uno::Reference< frame::XConfigManager > xCfgMgr;
446 if( m_xContext.is() )
450 xCfgMgr =
451 uno::Reference< frame::XConfigManager >(
452 m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.config.SpecialConfigManager", m_xContext),
453 uno::UNO_QUERY );
455 catch( const uno::Exception&)
457 OSL_ENSURE( xCfgMgr.is(),
458 "cant instantiate the special config manager " );
462 OSL_ENSURE( xCfgMgr.is(), "specialconfigmanager not found\n" );
464 if( xCfgMgr.is() )
465 instpath = xCfgMgr->substituteVariables( instpath );
468 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */