1*2722ceddSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*2722ceddSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*2722ceddSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*2722ceddSAndrew Rist * distributed with this work for additional information
6*2722ceddSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*2722ceddSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*2722ceddSAndrew Rist * "License"); you may not use this file except in compliance
9*2722ceddSAndrew Rist * with the License. You may obtain a copy of the License at
10*2722ceddSAndrew Rist *
11*2722ceddSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*2722ceddSAndrew Rist *
13*2722ceddSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*2722ceddSAndrew Rist * software distributed under the License is distributed on an
15*2722ceddSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2722ceddSAndrew Rist * KIND, either express or implied. See the License for the
17*2722ceddSAndrew Rist * specific language governing permissions and limitations
18*2722ceddSAndrew Rist * under the License.
19*2722ceddSAndrew Rist *
20*2722ceddSAndrew Rist *************************************************************/
21*2722ceddSAndrew Rist
22*2722ceddSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir
25cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
26cdf0e10cSrcweir #include "precompiled_desktop.hxx"
27cdf0e10cSrcweir
28cdf0e10cSrcweir #include "configinit.hxx"
29cdf0e10cSrcweir
30cdf0e10cSrcweir #include "desktop.hrc"
31cdf0e10cSrcweir #include "app.hxx"
32cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
33cdf0e10cSrcweir #include <uno/current_context.hxx>
34cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
35cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
36cdf0e10cSrcweir #include <osl/diagnose.h>
37cdf0e10cSrcweir #include <stdio.h>
38cdf0e10cSrcweir #include <map>
39cdf0e10cSrcweir #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
40cdf0e10cSrcweir #include <com/sun/star/configuration/CannotLoadConfigurationException.hpp>
41cdf0e10cSrcweir #include <com/sun/star/configuration/InvalidBootstrapFileException.hpp>
42cdf0e10cSrcweir #include <com/sun/star/configuration/backend/BackendSetupException.hpp>
43cdf0e10cSrcweir #include <com/sun/star/configuration/backend/CannotConnectException.hpp>
44cdf0e10cSrcweir
45cdf0e10cSrcweir // ----------------------------------------------------------------------------
46cdf0e10cSrcweir
47cdf0e10cSrcweir namespace uno = ::com::sun::star::uno;
48cdf0e10cSrcweir namespace lang = ::com::sun::star::lang;
49cdf0e10cSrcweir namespace configuration = ::com::sun::star::configuration;
50cdf0e10cSrcweir namespace backend = ::com::sun::star::configuration::backend;
51cdf0e10cSrcweir using rtl::OUString;
52cdf0e10cSrcweir using uno::UNO_QUERY;
53cdf0e10cSrcweir using desktop::Desktop;
54cdf0e10cSrcweir
55cdf0e10cSrcweir // ----------------------------------------------------------------------------
56cdf0e10cSrcweir static char const CONFIGURATION_PROVIDER[] = "com.sun.star.configuration.ConfigurationProvider";
57cdf0e10cSrcweir
58cdf0e10cSrcweir static char const CONFIGURATION_ERROR_HANDLER[] = "com.sun.star.configuration.backend.InteractionHandler";
59cdf0e10cSrcweir
60cdf0e10cSrcweir // must be aligned with configmgr/source/misc/configinteractionhandler
61cdf0e10cSrcweir static char const CONFIG_ERROR_HANDLER[] = "configuration.interaction-handler";
62cdf0e10cSrcweir // ----------------------------------------------------------------------------
63cdf0e10cSrcweir
64cdf0e10cSrcweir // ----------------------------------------------------------------------------
65cdf0e10cSrcweir #define arraysize( arr ) ( sizeof (arr)/sizeof *(arr) )
66cdf0e10cSrcweir
67cdf0e10cSrcweir typedef uno::Reference< lang::XMultiServiceFactory > ConfigurationProvider;
68cdf0e10cSrcweir
69cdf0e10cSrcweir #define OUSTRING( constascii ) OUString( RTL_CONSTASCII_USTRINGPARAM( constascii ) )
70cdf0e10cSrcweir
71cdf0e10cSrcweir #define OU2O( ustr, enc ) rtl::OUStringToOString( (ustr), RTL_TEXTENCODING_ ## enc )
72cdf0e10cSrcweir
73cdf0e10cSrcweir #define k_PROVIDER OUSTRING( CONFIGURATION_PROVIDER )
74cdf0e10cSrcweir #define k_ERRORHANDLER OUSTRING( CONFIGURATION_ERROR_HANDLER )
75cdf0e10cSrcweir // ----------------------------------------------------------------------------
76cdf0e10cSrcweir // Get a message string securely. There is a fallback string if the resource
77cdf0e10cSrcweir // is not available. Adapted from Desktop::GetMsgString()
78cdf0e10cSrcweir
getMsgString(sal_uInt16 nId,char const * aFallBackMsg)79cdf0e10cSrcweir OUString getMsgString( sal_uInt16 nId, char const * aFallBackMsg )
80cdf0e10cSrcweir {
81cdf0e10cSrcweir ResMgr* pResMgr = Desktop::GetDesktopResManager();
82cdf0e10cSrcweir if ( !pResMgr || !nId )
83cdf0e10cSrcweir return OUString::createFromAscii(aFallBackMsg);
84cdf0e10cSrcweir else
85cdf0e10cSrcweir return OUString( String(ResId( nId, *pResMgr )));
86cdf0e10cSrcweir }
87cdf0e10cSrcweir // ----------------------------------------------------------------------------
88cdf0e10cSrcweir /** Creates the normal configuration provider.
89cdf0e10cSrcweir
90cdf0e10cSrcweir */
91cdf0e10cSrcweir static
createDefaultConfigurationProvider()92cdf0e10cSrcweir ConfigurationProvider createDefaultConfigurationProvider( )
93cdf0e10cSrcweir {
94cdf0e10cSrcweir uno::Reference< lang::XMultiServiceFactory > xServiceManager = ::comphelper::getProcessServiceFactory();
95cdf0e10cSrcweir
96cdf0e10cSrcweir OSL_ENSURE( xServiceManager.is(),"No ServiceManager set for CreateApplicationConfigurationProvider");
97cdf0e10cSrcweir
98cdf0e10cSrcweir ConfigurationProvider xProvider;
99cdf0e10cSrcweir
100cdf0e10cSrcweir if (xServiceManager.is())
101cdf0e10cSrcweir {
102cdf0e10cSrcweir
103cdf0e10cSrcweir xProvider.set( xServiceManager->createInstance(k_PROVIDER), UNO_QUERY);
104cdf0e10cSrcweir }
105cdf0e10cSrcweir
106cdf0e10cSrcweir if (!xProvider.is())
107cdf0e10cSrcweir {
108cdf0e10cSrcweir OUString const sMsg = OUSTRING("Service \"") + k_PROVIDER +
109cdf0e10cSrcweir OUSTRING("\" is not available at the service manager.");
110cdf0e10cSrcweir
111cdf0e10cSrcweir throw lang::ServiceNotRegisteredException(sMsg, xServiceManager);
112cdf0e10cSrcweir }
113cdf0e10cSrcweir
114cdf0e10cSrcweir return xProvider;
115cdf0e10cSrcweir }
116cdf0e10cSrcweir // ----------------------------------------------------------------------------
117cdf0e10cSrcweir /// @attention this method must be called from a catch statement!
handleGeneralException(uno::Exception & aException,const rtl::OUString & aMessage)118cdf0e10cSrcweir static void handleGeneralException(uno::Exception& aException,
119cdf0e10cSrcweir const rtl::OUString& aMessage)
120cdf0e10cSrcweir {
121cdf0e10cSrcweir aException.Message = aMessage ;
122cdf0e10cSrcweir throw ;
123cdf0e10cSrcweir }
124cdf0e10cSrcweir // ----------------------------------------------------------------------------
125cdf0e10cSrcweir
CreateApplicationConfigurationProvider()126cdf0e10cSrcweir uno::Reference< lang::XMultiServiceFactory > CreateApplicationConfigurationProvider( )
127cdf0e10cSrcweir {
128cdf0e10cSrcweir uno::Reference< lang::XMultiServiceFactory > xProvider;
129cdf0e10cSrcweir
130cdf0e10cSrcweir try
131cdf0e10cSrcweir {
132cdf0e10cSrcweir xProvider = createDefaultConfigurationProvider( );
133cdf0e10cSrcweir }
134cdf0e10cSrcweir catch (configuration::InvalidBootstrapFileException & exception)
135cdf0e10cSrcweir {
136cdf0e10cSrcweir handleGeneralException(exception,
137cdf0e10cSrcweir getMsgString( STR_CONFIG_ERR_SETTINGS_INCOMPLETE,
138cdf0e10cSrcweir "The startup settings for your configuration settings are incomplete. "));
139cdf0e10cSrcweir }
140cdf0e10cSrcweir catch (backend::CannotConnectException & exception)
141cdf0e10cSrcweir {
142cdf0e10cSrcweir handleGeneralException(exception,
143cdf0e10cSrcweir getMsgString( STR_CONFIG_ERR_CANNOT_CONNECT,
144cdf0e10cSrcweir "A connection to your configuration settings could not be established. "));
145cdf0e10cSrcweir }
146cdf0e10cSrcweir catch (backend::BackendSetupException & exception)
147cdf0e10cSrcweir {
148cdf0e10cSrcweir handleGeneralException(exception,
149cdf0e10cSrcweir getMsgString( STR_CONFIG_ERR_CANNOT_CONNECT,
150cdf0e10cSrcweir "A connection to your configuration settings could not be established. "));
151cdf0e10cSrcweir }
152cdf0e10cSrcweir catch (configuration::CannotLoadConfigurationException & exception)
153cdf0e10cSrcweir {
154cdf0e10cSrcweir handleGeneralException(exception,
155cdf0e10cSrcweir getMsgString( STR_CONFIG_ERR_CANNOT_CONNECT,
156cdf0e10cSrcweir "A connection to your configuration settings could not be established. "));
157cdf0e10cSrcweir }
158cdf0e10cSrcweir catch (uno::Exception & exception)
159cdf0e10cSrcweir {
160cdf0e10cSrcweir handleGeneralException(exception,
161cdf0e10cSrcweir getMsgString( STR_CONFIG_ERR_ACCESS_GENERAL,
162cdf0e10cSrcweir "A general error occurred while accessing your configuration settings."));
163cdf0e10cSrcweir }
164cdf0e10cSrcweir
165cdf0e10cSrcweir
166cdf0e10cSrcweir return xProvider ;
167cdf0e10cSrcweir }
168cdf0e10cSrcweir // ----------------------------------------------------------------------------
169cdf0e10cSrcweir
170cdf0e10cSrcweir
171cdf0e10cSrcweir
172cdf0e10cSrcweir
173cdf0e10cSrcweir // ----------------------------------------------------------------------------
174cdf0e10cSrcweir // ----------------------------------------------------------------------------
175cdf0e10cSrcweir // ConfigurationErrorHandler
176cdf0e10cSrcweir // ----------------------------------------------------------------------------
177cdf0e10cSrcweir
178cdf0e10cSrcweir namespace
179cdf0e10cSrcweir {
180cdf0e10cSrcweir typedef uno::Reference< uno::XCurrentContext > CurrentContext;
181cdf0e10cSrcweir class SimpleCurrentContext : public cppu::WeakImplHelper1< uno::XCurrentContext >
182cdf0e10cSrcweir {
183cdf0e10cSrcweir CurrentContext m_xChainedContext;
184cdf0e10cSrcweir public:
185cdf0e10cSrcweir explicit
SimpleCurrentContext(const CurrentContext & xChainedContext)186cdf0e10cSrcweir SimpleCurrentContext(const CurrentContext & xChainedContext)
187cdf0e10cSrcweir : m_xChainedContext(xChainedContext)
188cdf0e10cSrcweir {}
189cdf0e10cSrcweir
install()190cdf0e10cSrcweir void install() { uno::setCurrentContext(this); }
deinstall()191cdf0e10cSrcweir void deinstall() { uno::setCurrentContext(m_xChainedContext); }
192cdf0e10cSrcweir
getChainedValueByName(OUString const & aName) const193cdf0e10cSrcweir uno::Any getChainedValueByName( OUString const & aName) const
194cdf0e10cSrcweir {
195cdf0e10cSrcweir return m_xChainedContext.is()
196cdf0e10cSrcweir ? m_xChainedContext->getValueByName(aName)
197cdf0e10cSrcweir : uno::Any();
198cdf0e10cSrcweir }
199cdf0e10cSrcweir
200cdf0e10cSrcweir // XCurrentContext
201cdf0e10cSrcweir virtual uno::Any SAL_CALL
202cdf0e10cSrcweir getValueByName( OUString const & aName)
203cdf0e10cSrcweir throw (uno::RuntimeException);
204cdf0e10cSrcweir };
205cdf0e10cSrcweir
206cdf0e10cSrcweir uno::Any SAL_CALL
getValueByName(OUString const & aName)207cdf0e10cSrcweir SimpleCurrentContext::getValueByName( OUString const & aName)
208cdf0e10cSrcweir throw (uno::RuntimeException)
209cdf0e10cSrcweir {
210cdf0e10cSrcweir return getChainedValueByName(aName);
211cdf0e10cSrcweir }
212cdf0e10cSrcweir
213cdf0e10cSrcweir }
214cdf0e10cSrcweir
215cdf0e10cSrcweir // ----------------------------------------------------------------------------
216cdf0e10cSrcweir class ConfigurationErrorHandler::Context : public SimpleCurrentContext
217cdf0e10cSrcweir {
218cdf0e10cSrcweir public:
Context()219cdf0e10cSrcweir Context()
220cdf0e10cSrcweir : SimpleCurrentContext( uno::getCurrentContext() )
221cdf0e10cSrcweir {
222cdf0e10cSrcweir }
223cdf0e10cSrcweir
~Context()224cdf0e10cSrcweir ~Context()
225cdf0e10cSrcweir {
226cdf0e10cSrcweir }
227cdf0e10cSrcweir
228cdf0e10cSrcweir // XCurrentContext
229cdf0e10cSrcweir virtual uno::Any SAL_CALL
230cdf0e10cSrcweir getValueByName( OUString const & aName)
231cdf0e10cSrcweir throw (uno::RuntimeException);
232cdf0e10cSrcweir
233cdf0e10cSrcweir private:
234cdf0e10cSrcweir InteractionHandler m_xHandler;
235cdf0e10cSrcweir };
236cdf0e10cSrcweir
237cdf0e10cSrcweir //------------------------------------------------------------------------------
getValueByName(OUString const & aName)238cdf0e10cSrcweir uno::Any SAL_CALL ConfigurationErrorHandler::Context::getValueByName( OUString const & aName)
239cdf0e10cSrcweir throw (uno::RuntimeException)
240cdf0e10cSrcweir {
241cdf0e10cSrcweir if ( aName.equalsAscii( CONFIG_ERROR_HANDLER ) )
242cdf0e10cSrcweir {
243cdf0e10cSrcweir if ( !m_xHandler.is() )
244cdf0e10cSrcweir m_xHandler = ConfigurationErrorHandler::getDefaultInteractionHandler();
245cdf0e10cSrcweir return uno::Any( m_xHandler );
246cdf0e10cSrcweir }
247cdf0e10cSrcweir return SimpleCurrentContext::getValueByName( aName );
248cdf0e10cSrcweir }
249cdf0e10cSrcweir
250cdf0e10cSrcweir //------------------------------------------------------------------------------
~ConfigurationErrorHandler()251cdf0e10cSrcweir ConfigurationErrorHandler::~ConfigurationErrorHandler()
252cdf0e10cSrcweir {
253cdf0e10cSrcweir deactivate();
254cdf0e10cSrcweir }
255cdf0e10cSrcweir
256cdf0e10cSrcweir //------------------------------------------------------------------------------
257cdf0e10cSrcweir /// installs the handler into the current context
activate()258cdf0e10cSrcweir void ConfigurationErrorHandler::activate()
259cdf0e10cSrcweir {
260cdf0e10cSrcweir if (!m_pContext)
261cdf0e10cSrcweir {
262cdf0e10cSrcweir m_pContext = new Context;
263cdf0e10cSrcweir m_pContext->acquire();
264cdf0e10cSrcweir }
265cdf0e10cSrcweir m_pContext->install();
266cdf0e10cSrcweir }
267cdf0e10cSrcweir
268cdf0e10cSrcweir //------------------------------------------------------------------------------
269cdf0e10cSrcweir /// deinstalls the handler from the current context, restoring the previous context
deactivate()270cdf0e10cSrcweir void ConfigurationErrorHandler::deactivate()
271cdf0e10cSrcweir {
272cdf0e10cSrcweir if (m_pContext)
273cdf0e10cSrcweir {
274cdf0e10cSrcweir m_pContext->deinstall();
275cdf0e10cSrcweir m_pContext->release();
276cdf0e10cSrcweir m_pContext = 0;
277cdf0e10cSrcweir }
278cdf0e10cSrcweir }
279cdf0e10cSrcweir //------------------------------------------------------------------------------
280cdf0e10cSrcweir
getDefaultInteractionHandler()281cdf0e10cSrcweir ConfigurationErrorHandler::InteractionHandler ConfigurationErrorHandler::getDefaultInteractionHandler()
282cdf0e10cSrcweir {
283cdf0e10cSrcweir uno::Reference< lang::XMultiServiceFactory > xServiceManager = ::comphelper::getProcessServiceFactory();
284cdf0e10cSrcweir
285cdf0e10cSrcweir OSL_ENSURE( xServiceManager.is(),"No ServiceManager set for ConfigurationErrorHandler");
286cdf0e10cSrcweir
287cdf0e10cSrcweir InteractionHandler xHandler;
288cdf0e10cSrcweir
289cdf0e10cSrcweir if (xServiceManager.is())
290cdf0e10cSrcweir {
291cdf0e10cSrcweir xHandler.set( xServiceManager->createInstance(k_ERRORHANDLER), UNO_QUERY );
292cdf0e10cSrcweir }
293cdf0e10cSrcweir
294cdf0e10cSrcweir return xHandler;
295cdf0e10cSrcweir }
296cdf0e10cSrcweir //------------------------------------------------------------------------------
297cdf0e10cSrcweir
298cdf0e10cSrcweir
299cdf0e10cSrcweir
300