1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
)
57 ContentProvider::~ContentProvider()
62 // XInterface methods.
63 void SAL_CALL
ContentProvider::acquire()
66 OWeakObject::acquire();
69 void SAL_CALL
ContentProvider::release()
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
;
103 osl::Guard
< osl::Mutex
> aGuard( osl::Mutex::getGlobalMutex() );
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" );
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
;
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(
177 ContentProvider::getImplementationName_Static(),
178 ContentProvider_CreateInstance
,
179 ContentProvider::getSupportedServiceNames_Static() ) );
182 // XContentProvider methods.
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
);
203 throw uno::RuntimeException();
205 // Check, if a content with given id already exists...
206 uno::Reference
< ucb::XContent
> xContent
207 = queryExistingContent( xCanonicId
).get();
211 xContent
= new Content( m_xContext
, this, xCanonicId
, m_pDatabases
);
213 // register new content
214 registerNewContent( xContent
);
218 if ( !xContent
->getIdentifier().is() )
219 throw ucb::IllegalIdentifierException();
225 ContentProvider::dispose()
226 throw ( uno::RuntimeException
, std::exception
)
228 if(m_xContainer
.is())
230 m_xContainer
->removeContainerListener(this);
231 m_xContainer
.clear();
236 ContentProvider::elementReplaced(const container::ContainerEvent
& Event
)
237 throw (uno::RuntimeException
, std::exception
)
243 Event
.Accessor
>>= accessor
;
244 if(accessor
!= "HelpStyleSheet")
247 OUString replacedElement
,element
;
248 Event
.ReplacedElement
>>= replacedElement
;
249 Event
.Element
>>= element
;
251 if(replacedElement
== element
)
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);
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
);
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
);
312 uno::Reference
< uno::XInterface
> xCFG( xConfigProvider
->createInstanceWithArguments(
313 OUString("com.sun.star.configuration.ConfigurationAccess"),
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
,
329 utl::ConfigManager::getProductName(),
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" );
354 uno::Reference
< container::XHierarchicalNameAccess
>
355 ContentProvider::getHierAccess(
356 const uno::Reference
< lang::XMultiServiceFactory
>& sProvider
,
359 uno::Reference
< container::XHierarchicalNameAccess
> xHierAccess
;
363 uno::Sequence
< uno::Any
> seq( 1 );
364 OUString
sReaderService(
366 "com.sun.star.configuration.ConfigurationAccess" ) );
368 seq
[ 0 ] <<= OUString::createFromAscii( file
);
373 uno::Reference
< container::XHierarchicalNameAccess
>(
374 sProvider
->createInstanceWithArguments(
375 sReaderService
, seq
),
378 catch( const uno::Exception
& )
386 ContentProvider::getKey(
387 const uno::Reference
< container::XHierarchicalNameAccess
>& xHierAccess
,
391 if( xHierAccess
.is() )
397 xHierAccess
->getByHierarchicalName(
398 OUString::createFromAscii( key
) );
400 catch( const container::NoSuchElementException
& )
409 ContentProvider::getBooleanKey(
410 const uno::Reference
< container::XHierarchicalNameAccess
>& xHierAccess
,
414 if( xHierAccess
.is() )
420 xHierAccess
->getByHierarchicalName(
421 OUString::createFromAscii( key
) );
423 catch( const container::NoSuchElementException
& )
431 void ContentProvider::subst( OUString
& instpath
)
433 SvtPathOptions aOptions
;
434 instpath
= aOptions
.SubstituteVariable( instpath
);
437 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */