update dev300-m58
[ooovba.git] / xmlhelp / source / cxxhelp / provider / provider.cxx
blob8b01c47aee30c2f5a39d60fdd947aeb60dad1f2a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: provider.cxx,v $
10 * $Revision: 1.28 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlhelp.hxx"
34 /**************************************************************************
35 TODO
36 **************************************************************************
38 *************************************************************************/
40 #include <stdio.h>
41 #include <osl/file.hxx>
42 #ifndef _VOS_DIAGNOSE_HXX_
43 #include <vos/diagnose.hxx>
44 #endif
45 #include <ucbhelper/contentidentifier.hxx>
46 #include <com/sun/star/frame/XConfigManager.hpp>
47 #ifndef _COM_SUN_STAR_BEANS_PROPERTYATTRIBBUTE_HPP_
48 #include <com/sun/star/beans/PropertyAttribute.hpp>
49 #endif
50 #include <com/sun/star/beans/PropertyValue.hpp>
51 #include <com/sun/star/beans/PropertyState.hpp>
52 #include <com/sun/star/container/XContainer.hpp>
53 #include <com/sun/star/container/XNameAccess.hpp>
54 #include <com/sun/star/container/XNameReplace.hpp>
55 #include <com/sun/star/uno/XComponentContext.hpp>
56 #include <com/sun/star/beans/XPropertySet.hpp>
57 #include <rtl/bootstrap.hxx>
58 #include <unotools/configmgr.hxx>
60 #include "databases.hxx"
61 #include "provider.hxx"
62 #include "content.hxx"
63 #include "databases.hxx"
65 using namespace com::sun::star;
66 using namespace chelp;
68 //=========================================================================
69 //=========================================================================
71 // ContentProvider Implementation.
73 //=========================================================================
74 //=========================================================================
76 ContentProvider::ContentProvider(
77 const uno::Reference< lang::XMultiServiceFactory >& rSMgr )
78 : ::ucbhelper::ContentProviderImplHelper( rSMgr ),
79 isInitialized( false ),
80 m_aScheme( rtl::OUString::createFromAscii( MYUCP_URL_SCHEME ) ),
81 m_pDatabases( 0 )
85 //=========================================================================
86 // virtual
87 ContentProvider::~ContentProvider()
89 delete m_pDatabases;
92 //=========================================================================
94 // XInterface methods.
96 //=========================================================================
98 XINTERFACE_IMPL_6( ContentProvider,
99 lang::XTypeProvider,
100 lang::XServiceInfo,
101 ucb::XContentProvider,
102 lang::XComponent,
103 lang::XEventListener, /* base of XContainerListener */
104 container::XContainerListener);
106 //=========================================================================
108 // XTypeProvider methods.
110 //=========================================================================
112 XTYPEPROVIDER_IMPL_5( ContentProvider,
113 lang::XTypeProvider,
114 lang::XServiceInfo,
115 ucb::XContentProvider,
116 lang::XComponent,
117 container::XContainerListener);
119 //=========================================================================
121 // XServiceInfo methods.
123 //=========================================================================
125 rtl::OUString SAL_CALL ContentProvider::getImplementationName()
126 throw( uno::RuntimeException )
128 return getImplementationName_Static();
131 rtl::OUString ContentProvider::getImplementationName_Static()
133 return rtl::OUString::createFromAscii("CHelpContentProvider" );
136 sal_Bool SAL_CALL
137 ContentProvider::supportsService(const rtl::OUString& ServiceName )
138 throw( uno::RuntimeException )
140 uno::Sequence< rtl::OUString > aSNL = getSupportedServiceNames();
141 const rtl::OUString* pArray = aSNL.getArray();
142 for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
144 if( pArray[ i ] == ServiceName )
145 return sal_True;
148 return sal_False;
151 uno::Sequence< rtl::OUString > SAL_CALL
152 ContentProvider::getSupportedServiceNames()
153 throw( uno::RuntimeException )
155 return getSupportedServiceNames_Static();
158 static uno::Reference< uno::XInterface > SAL_CALL
159 ContentProvider_CreateInstance(
160 const uno::Reference< lang::XMultiServiceFactory> & rSMgr )
161 throw( uno::Exception )
163 lang::XServiceInfo * pX = static_cast< lang::XServiceInfo * >(
164 new ContentProvider( rSMgr ) );
165 return uno::Reference< uno::XInterface >::query( pX );
168 uno::Sequence< rtl::OUString >
169 ContentProvider::getSupportedServiceNames_Static()
171 uno::Sequence< rtl::OUString > aSNS( 2 );
172 aSNS.getArray()[ 0 ] =
173 rtl::OUString::createFromAscii(
174 MYUCP_CONTENT_PROVIDER_SERVICE_NAME1 );
175 aSNS.getArray()[ 1 ] =
176 rtl::OUString::createFromAscii(
177 MYUCP_CONTENT_PROVIDER_SERVICE_NAME2 );
179 return aSNS;
182 //=========================================================================
184 // Service factory implementation.
186 //=========================================================================
188 ONE_INSTANCE_SERVICE_FACTORY_IMPL( ContentProvider );
190 //=========================================================================
192 // XContentProvider methods.
194 //=========================================================================
196 // virtual
197 uno::Reference< ucb::XContent > SAL_CALL
198 ContentProvider::queryContent(
199 const uno::Reference< ucb::XContentIdentifier >& xCanonicId )
200 throw( ucb::IllegalIdentifierException, uno::RuntimeException )
202 if ( !xCanonicId->getContentProviderScheme()
203 .equalsIgnoreAsciiCase( m_aScheme ) )
204 { // Wrong URL-scheme
205 throw ucb::IllegalIdentifierException();
209 osl::MutexGuard aGuard( m_aMutex );
210 if( !isInitialized )
211 init();
214 if( !m_pDatabases )
215 throw uno::RuntimeException();
217 rtl::OUString aOUString( m_pDatabases->getInstallPathAsURL() );
218 rtl::OString aOString( aOUString.getStr(),
219 aOUString.getLength(),
220 RTL_TEXTENCODING_UTF8 );
222 // Check, if a content with given id already exists...
223 uno::Reference< ucb::XContent > xContent
224 = queryExistingContent( xCanonicId ).get();
225 if ( xContent.is() )
226 return xContent;
228 xContent = new Content( m_xSMgr, this, xCanonicId, m_pDatabases );
230 // register new content
231 registerNewContent( xContent );
233 // Further checks
235 if ( !xContent->getIdentifier().is() )
236 throw ucb::IllegalIdentifierException();
238 return xContent;
241 void SAL_CALL
242 ContentProvider::dispose()
243 throw ( uno::RuntimeException)
245 if(m_xContainer.is())
247 m_xContainer->removeContainerListener(this);
248 m_xContainer.clear();
252 void SAL_CALL
253 ContentProvider::elementReplaced(const container::ContainerEvent& Event)
254 throw (uno::RuntimeException)
256 if(!m_pDatabases)
257 return;
259 rtl::OUString accessor;
260 Event.Accessor >>= accessor;
261 if(accessor.compareToAscii("HelpStyleSheet"))
262 return;
264 rtl::OUString replacedElement,element;
265 Event.ReplacedElement >>= replacedElement;
266 Event.Element >>= element;
268 if(replacedElement == element)
269 return;
271 m_pDatabases->changeCSS(element);
274 void ContentProvider::init()
276 osl::MutexGuard aGuard( m_aMutex );
278 isInitialized = true;
279 uno::Reference< lang::XMultiServiceFactory > sProvider(
280 getConfiguration() );
281 uno::Reference< container::XHierarchicalNameAccess > xHierAccess(
282 getHierAccess( sProvider,
283 "org.openoffice.Office.Common" ) );
285 rtl::OUString instPath( getKey( xHierAccess,"Path/Current/Help" ) );
286 if( ! instPath.getLength() )
287 // try to determine path from default
288 instPath = rtl::OUString::createFromAscii( "$(instpath)/help" );
289 // replace anything like $(instpath);
290 subst( instPath );
292 rtl::OUString stylesheet( getKey( xHierAccess,"Help/HelpStyleSheet" ) );
293 try
295 // now adding as configuration change listener for the stylesheet
296 uno::Reference< container::XNameAccess> xAccess(
297 xHierAccess, uno::UNO_QUERY );
298 if( xAccess.is() )
300 uno::Any aAny =
301 xAccess->getByName( rtl::OUString::createFromAscii( "Help" ) );
302 aAny >>= m_xContainer;
303 if( m_xContainer.is() )
304 m_xContainer->addContainerListener( this );
307 catch( uno::Exception const & )
312 * now determing
313 * productname,
314 * productversion,
315 * vendorname,
316 * vendorversion,
317 * vendorshort
320 xHierAccess = getHierAccess( sProvider, "org.openoffice.Setup" );
321 rtl::OUString productname(
322 getKey( xHierAccess,"Product/ooName" ) );
324 rtl::OUString setupversion(
325 getKey( xHierAccess,"Product/ooSetupVersion" ) );
326 rtl::OUString setupextension;
327 utl::ConfigManager * mgr = utl::ConfigManager::GetConfigManager();
328 if (mgr != NULL) {
329 mgr->GetDirectConfigProperty(utl::ConfigManager::PRODUCTEXTENSION) >>=
330 setupextension;
332 rtl::OUString productversion(
333 setupversion +
334 rtl::OUString::createFromAscii( " " ) +
335 setupextension );
337 xHierAccess = getHierAccess( sProvider, "org.openoffice.Webtop.Common" );
338 rtl::OUString vendorname(
339 getKey( xHierAccess,"Product/ooName" ) );
341 setupversion = rtl::OUString(
342 getKey( xHierAccess,"Product/ooSetupVersion" ) );
343 setupextension = rtl::OUString(
344 getKey( xHierAccess,"Product/ooSetupExtension") );
345 rtl::OUString vendorversion(
346 setupversion +
347 rtl::OUString::createFromAscii( " " ) +
348 setupextension );
349 rtl::OUString vendorshort = vendorname;
351 uno::Sequence< rtl::OUString > aImagesZipPaths( 3 );
352 xHierAccess = getHierAccess( sProvider, "org.openoffice.Office.Common" );
354 rtl::OUString aPath( getKey( xHierAccess, "Path/Current/UserConfig" ) );
355 subst( aPath );
356 aImagesZipPaths[ 0 ] = aPath;
358 aPath = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("$OOO_BASE_DIR/share/config"));
359 rtl::Bootstrap::expandMacros(aPath);
360 aImagesZipPaths[ 2 ] = aPath;
362 // try also /usr/share and even before /usr/lib
363 ::rtl::OUString aDataDir = ::rtl::OUString::createFromAscii ( "/usr/share/" );
364 #ifdef X86_64
365 ::rtl::OUString aLibDir = ::rtl::OUString::createFromAscii ( "/usr/lib64/" );
366 #else
367 ::rtl::OUString aLibDir = ::rtl::OUString::createFromAscii ( "/usr/lib/" );
368 #endif
369 sal_Int32 nLibDirPos = aPath.indexOf( aLibDir );
371 if ( nLibDirPos >= 0 )
372 aPath = aPath.replaceAt( nLibDirPos, aLibDir.getLength(), aDataDir );
373 aImagesZipPaths[ 1 ] = aPath;
375 uno::Reference< uno::XComponentContext > xContext;
376 uno::Reference< beans::XPropertySet > xProps( m_xSMgr, uno::UNO_QUERY );
377 OSL_ASSERT( xProps.is() );
378 if (xProps.is())
380 xProps->getPropertyValue(
381 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("DefaultContext") ) ) >>= xContext;
382 OSL_ASSERT( xContext.is() );
385 sal_Bool showBasic = getBooleanKey(xHierAccess,"Help/ShowBasic");
386 m_pDatabases = new Databases( showBasic,
387 instPath,
388 aImagesZipPaths,
389 productname,
390 productversion,
391 vendorname,
392 vendorversion,
393 vendorshort,
394 stylesheet,
395 xContext );
398 uno::Reference< lang::XMultiServiceFactory >
399 ContentProvider::getConfiguration() const
401 uno::Reference< lang::XMultiServiceFactory > sProvider;
402 if( m_xSMgr.is() )
404 uno::Any aAny;
405 aAny <<= rtl::OUString::createFromAscii( "plugin" );
406 beans::PropertyValue aProp(
407 rtl::OUString::createFromAscii( "servertype" ),
409 aAny,
410 beans::PropertyState_DIRECT_VALUE );
412 uno::Sequence< uno::Any > seq(1);
413 seq[0] <<= aProp;
417 rtl::OUString sProviderService =
418 rtl::OUString::createFromAscii(
419 "com.sun.star.configuration.ConfigurationProvider" );
420 sProvider =
421 uno::Reference< lang::XMultiServiceFactory >(
422 m_xSMgr->createInstanceWithArguments(
423 sProviderService,seq ),
424 uno::UNO_QUERY );
426 catch( const uno::Exception& )
428 OSL_ENSURE( sProvider.is(), "cant instantiate configuration" );
432 return sProvider;
435 uno::Reference< container::XHierarchicalNameAccess >
436 ContentProvider::getHierAccess(
437 const uno::Reference< lang::XMultiServiceFactory >& sProvider,
438 const char* file ) const
440 uno::Reference< container::XHierarchicalNameAccess > xHierAccess;
442 if( sProvider.is() )
444 uno::Sequence< uno::Any > seq( 1 );
445 rtl::OUString sReaderService(
446 rtl::OUString::createFromAscii(
447 "com.sun.star.configuration.ConfigurationAccess" ) );
449 seq[ 0 ] <<= rtl::OUString::createFromAscii( file );
453 xHierAccess =
454 uno::Reference< container::XHierarchicalNameAccess >(
455 sProvider->createInstanceWithArguments(
456 sReaderService, seq ),
457 uno::UNO_QUERY );
459 catch( const uno::Exception& )
463 return xHierAccess;
468 rtl::OUString
469 ContentProvider::getKey(
470 const uno::Reference< container::XHierarchicalNameAccess >& xHierAccess,
471 const char* key ) const
473 rtl::OUString instPath;
474 if( xHierAccess.is() )
476 uno::Any aAny;
479 aAny =
480 xHierAccess->getByHierarchicalName(
481 rtl::OUString::createFromAscii( key ) );
483 catch( const container::NoSuchElementException& )
486 aAny >>= instPath;
488 return instPath;
491 sal_Bool
492 ContentProvider::getBooleanKey(
493 const uno::Reference< container::XHierarchicalNameAccess >& xHierAccess,
494 const char* key ) const
496 sal_Bool ret = sal_False;
497 if( xHierAccess.is() )
499 uno::Any aAny;
502 aAny =
503 xHierAccess->getByHierarchicalName(
504 rtl::OUString::createFromAscii( key ) );
506 catch( const container::NoSuchElementException& )
509 aAny >>= ret;
511 return ret;
514 void ContentProvider::subst( rtl::OUString& instpath ) const
516 uno::Reference< frame::XConfigManager > xCfgMgr;
517 if( m_xSMgr.is() )
521 xCfgMgr =
522 uno::Reference< frame::XConfigManager >(
523 m_xSMgr->createInstance(
524 rtl::OUString::createFromAscii(
525 "com.sun.star.config.SpecialConfigManager" ) ),
526 uno::UNO_QUERY );
528 catch( const uno::Exception&)
530 OSL_ENSURE( xCfgMgr.is(),
531 "cant instantiate the special config manager " );
535 OSL_ENSURE( xCfgMgr.is(), "specialconfigmanager not found\n" );
537 if( xCfgMgr.is() )
538 instpath = xCfgMgr->substituteVariables( instpath );