bump product version to 5.0.4.1
[LibreOffice.git] / xmlhelp / source / cxxhelp / provider / provider.cxx
blob95b1c6de4d7ba9b232fee421aaebaf3404d86c24
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 .
20 #include <config_folders.h>
22 #include <stdio.h>
23 #include <osl/file.hxx>
24 #include <osl/diagnose.h>
25 #include <ucbhelper/contentidentifier.hxx>
26 #include <com/sun/star/frame/XConfigManager.hpp>
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
28 #include <com/sun/star/beans/PropertyValue.hpp>
29 #include <com/sun/star/configuration/theDefaultProvider.hpp>
30 #include <com/sun/star/container/XContainer.hpp>
31 #include <com/sun/star/container/XNameAccess.hpp>
32 #include <com/sun/star/container/XNameReplace.hpp>
33 #include <com/sun/star/uno/XComponentContext.hpp>
34 #include <comphelper/processfactory.hxx>
35 #include <cppuhelper/supportsservice.hxx>
36 #include <unotools/configmgr.hxx>
37 #include <unotools/pathoptions.hxx>
39 #include "databases.hxx"
40 #include "provider.hxx"
41 #include "content.hxx"
43 using namespace com::sun::star;
44 using namespace chelp;
46 // ContentProvider Implementation.
48 ContentProvider::ContentProvider( const uno::Reference< uno::XComponentContext >& rxContext )
49 : ::ucbhelper::ContentProviderImplHelper( rxContext )
50 , isInitialized( false )
51 , m_aScheme(MYUCP_URL_SCHEME)
52 , m_pDatabases( 0 )
56 // virtual
57 ContentProvider::~ContentProvider()
59 delete m_pDatabases;
62 // XInterface methods.
63 void SAL_CALL ContentProvider::acquire()
64 throw()
66 OWeakObject::acquire();
69 void SAL_CALL ContentProvider::release()
70 throw()
72 OWeakObject::release();
75 css::uno::Any SAL_CALL ContentProvider::queryInterface( const css::uno::Type & rType )
76 throw( css::uno::RuntimeException, std::exception )
78 css::uno::Any aRet = cppu::queryInterface( rType,
79 (static_cast< lang::XTypeProvider* >(this)),
80 (static_cast< lang::XServiceInfo* >(this)),
81 (static_cast< ucb::XContentProvider* >(this)),
82 (static_cast< lang::XComponent* >(this)),
83 (static_cast< lang::XEventListener* >(this)),
84 (static_cast< container::XContainerListener* >(this))
86 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
89 // XTypeProvider methods.
91 css::uno::Sequence< sal_Int8 > SAL_CALL ContentProvider::getImplementationId()
92 throw( css::uno::RuntimeException, std::exception )
94 return css::uno::Sequence<sal_Int8>();
97 css::uno::Sequence< css::uno::Type > SAL_CALL ContentProvider::getTypes()
98 throw( css::uno::RuntimeException, std::exception )
100 static cppu::OTypeCollection* pCollection = NULL;
101 if ( !pCollection )
103 osl::Guard< osl::Mutex > aGuard( osl::Mutex::getGlobalMutex() );
104 if ( !pCollection )
106 static cppu::OTypeCollection collection(
107 cppu::UnoType<lang::XTypeProvider>::get(),
108 cppu::UnoType<lang::XServiceInfo>::get(),
109 cppu::UnoType<ucb::XContentProvider>::get(),
110 cppu::UnoType<lang::XComponent>::get(),
111 cppu::UnoType<container::XContainerListener>::get()
113 pCollection = &collection;
116 return (*pCollection).getTypes();
120 // XServiceInfo methods.
122 OUString SAL_CALL ContentProvider::getImplementationName()
123 throw( uno::RuntimeException, std::exception )
125 return getImplementationName_Static();
128 OUString ContentProvider::getImplementationName_Static()
130 return OUString("CHelpContentProvider" );
133 sal_Bool SAL_CALL
134 ContentProvider::supportsService(const OUString& ServiceName )
135 throw( uno::RuntimeException, std::exception )
137 return cppu::supportsService(this, ServiceName);
140 uno::Sequence< OUString > SAL_CALL
141 ContentProvider::getSupportedServiceNames()
142 throw( uno::RuntimeException, std::exception )
144 return getSupportedServiceNames_Static();
147 static uno::Reference< uno::XInterface > SAL_CALL
148 ContentProvider_CreateInstance(
149 const uno::Reference< lang::XMultiServiceFactory> & rSMgr )
150 throw( uno::Exception )
152 lang::XServiceInfo * pX = static_cast< lang::XServiceInfo * >(
153 new ContentProvider( comphelper::getComponentContext(rSMgr) ) );
154 return uno::Reference< uno::XInterface >::query( pX );
157 uno::Sequence< OUString >
158 ContentProvider::getSupportedServiceNames_Static()
160 uno::Sequence< OUString > aSNS( 2 );
161 aSNS.getArray()[ 0 ] = MYUCP_CONTENT_PROVIDER_SERVICE_NAME1;
162 aSNS.getArray()[ 1 ] = MYUCP_CONTENT_PROVIDER_SERVICE_NAME2;
164 return aSNS;
167 // Service factory implementation.
169 css::uno::Reference< css::lang::XSingleServiceFactory >
170 ContentProvider::createServiceFactory( const css::uno::Reference<
171 css::lang::XMultiServiceFactory >& rxServiceMgr )
173 return css::uno::Reference<
174 css::lang::XSingleServiceFactory >(
175 cppu::createOneInstanceFactory(
176 rxServiceMgr,
177 ContentProvider::getImplementationName_Static(),
178 ContentProvider_CreateInstance,
179 ContentProvider::getSupportedServiceNames_Static() ) );
182 // XContentProvider methods.
184 // virtual
185 uno::Reference< ucb::XContent > SAL_CALL
186 ContentProvider::queryContent(
187 const uno::Reference< ucb::XContentIdentifier >& xCanonicId )
188 throw( ucb::IllegalIdentifierException, uno::RuntimeException, std::exception )
190 if ( !xCanonicId->getContentProviderScheme()
191 .equalsIgnoreAsciiCase( m_aScheme ) )
192 { // Wrong URL-scheme
193 throw ucb::IllegalIdentifierException();
197 osl::MutexGuard aGuard( m_aMutex );
198 if( !isInitialized )
199 init();
202 if( !m_pDatabases )
203 throw uno::RuntimeException();
205 // Check, if a content with given id already exists...
206 uno::Reference< ucb::XContent > xContent
207 = queryExistingContent( xCanonicId ).get();
208 if ( xContent.is() )
209 return xContent;
211 xContent = new Content( m_xContext, this, xCanonicId, m_pDatabases );
213 // register new content
214 registerNewContent( xContent );
216 // Further checks
218 if ( !xContent->getIdentifier().is() )
219 throw ucb::IllegalIdentifierException();
221 return xContent;
224 void SAL_CALL
225 ContentProvider::dispose()
226 throw ( uno::RuntimeException, std::exception)
228 if(m_xContainer.is())
230 m_xContainer->removeContainerListener(this);
231 m_xContainer.clear();
235 void SAL_CALL
236 ContentProvider::elementReplaced(const container::ContainerEvent& Event)
237 throw (uno::RuntimeException, std::exception)
239 if(!m_pDatabases)
240 return;
242 OUString accessor;
243 Event.Accessor >>= accessor;
244 if(accessor != "HelpStyleSheet")
245 return;
247 OUString replacedElement,element;
248 Event.ReplacedElement >>= replacedElement;
249 Event.Element >>= element;
251 if(replacedElement == element)
252 return;
254 m_pDatabases->changeCSS(element);
257 void ContentProvider::init()
259 osl::MutexGuard aGuard( m_aMutex );
261 isInitialized = true;
262 uno::Reference< lang::XMultiServiceFactory > sProvider(
263 getConfiguration() );
264 uno::Reference< container::XHierarchicalNameAccess > xHierAccess(
265 getHierAccess( sProvider,
266 "org.openoffice.Office.Common" ) );
268 OUString instPath( getKey( xHierAccess,"Path/Current/Help" ) );
269 if( instPath.isEmpty() )
270 // try to determine path from default
271 instPath = "$(instpath)/" LIBO_SHARE_HELP_FOLDER;
272 // replace anything like $(instpath);
273 subst( instPath );
275 OUString stylesheet( getKey( xHierAccess,"Help/HelpStyleSheet" ) );
278 // now adding as configuration change listener for the stylesheet
279 uno::Reference< container::XNameAccess> xAccess(
280 xHierAccess, uno::UNO_QUERY );
281 if( xAccess.is() )
283 uno::Any aAny =
284 xAccess->getByName("Help");
285 aAny >>= m_xContainer;
286 if( m_xContainer.is() )
287 m_xContainer->addContainerListener( this );
290 catch( uno::Exception const & )
294 xHierAccess = getHierAccess( sProvider, "org.openoffice.Setup" );
296 OUString setupversion(
297 getKey( xHierAccess,"Product/ooSetupVersion" ) );
298 OUString setupextension;
302 uno::Reference< lang::XMultiServiceFactory > xConfigProvider =
303 configuration::theDefaultProvider::get( m_xContext );
305 uno::Sequence < uno::Any > lParams(1);
306 beans::PropertyValue aParam ;
307 aParam.Name = "nodepath";
308 aParam.Value <<= OUString("/org.openoffice.Setup/Product");
309 lParams[0] = uno::makeAny(aParam);
311 // open it
312 uno::Reference< uno::XInterface > xCFG( xConfigProvider->createInstanceWithArguments(
313 OUString("com.sun.star.configuration.ConfigurationAccess"),
314 lParams) );
316 uno::Reference< container::XNameAccess > xDirectAccess(xCFG, uno::UNO_QUERY);
317 uno::Any aRet = xDirectAccess->getByName("ooSetupExtension");
319 aRet >>= setupextension;
321 catch ( uno::Exception& )
325 OUString productversion( setupversion + " " + setupextension );
326 bool showBasic = getBooleanKey(xHierAccess,"Help/ShowBasic");
327 m_pDatabases = new Databases( showBasic,
328 instPath,
329 utl::ConfigManager::getProductName(),
330 productversion,
331 stylesheet,
332 m_xContext );
335 uno::Reference< lang::XMultiServiceFactory >
336 ContentProvider::getConfiguration() const
338 uno::Reference< lang::XMultiServiceFactory > xProvider;
339 if( m_xContext.is() )
343 xProvider = configuration::theDefaultProvider::get( m_xContext );
345 catch( const uno::Exception& )
347 OSL_ENSURE( xProvider.is(), "can not instantiate configuration" );
351 return xProvider;
354 uno::Reference< container::XHierarchicalNameAccess >
355 ContentProvider::getHierAccess(
356 const uno::Reference< lang::XMultiServiceFactory >& sProvider,
357 const char* file )
359 uno::Reference< container::XHierarchicalNameAccess > xHierAccess;
361 if( sProvider.is() )
363 uno::Sequence< uno::Any > seq( 1 );
364 OUString sReaderService(
365 OUString(
366 "com.sun.star.configuration.ConfigurationAccess" ) );
368 seq[ 0 ] <<= OUString::createFromAscii( file );
372 xHierAccess =
373 uno::Reference< container::XHierarchicalNameAccess >(
374 sProvider->createInstanceWithArguments(
375 sReaderService, seq ),
376 uno::UNO_QUERY );
378 catch( const uno::Exception& )
382 return xHierAccess;
385 OUString
386 ContentProvider::getKey(
387 const uno::Reference< container::XHierarchicalNameAccess >& xHierAccess,
388 const char* key )
390 OUString instPath;
391 if( xHierAccess.is() )
393 uno::Any aAny;
396 aAny =
397 xHierAccess->getByHierarchicalName(
398 OUString::createFromAscii( key ) );
400 catch( const container::NoSuchElementException& )
403 aAny >>= instPath;
405 return instPath;
408 bool
409 ContentProvider::getBooleanKey(
410 const uno::Reference< container::XHierarchicalNameAccess >& xHierAccess,
411 const char* key )
413 bool ret = false;
414 if( xHierAccess.is() )
416 uno::Any aAny;
419 aAny =
420 xHierAccess->getByHierarchicalName(
421 OUString::createFromAscii( key ) );
423 catch( const container::NoSuchElementException& )
426 aAny >>= ret;
428 return ret;
431 void ContentProvider::subst( OUString& instpath )
433 SvtPathOptions aOptions;
434 instpath = aOptions.SubstituteVariable( instpath );
437 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */