merge the formfield patch from ooo-build
[ooovba.git] / desktop / source / app / configinit.cxx
blobe8f48b89a8f2b0777813423115f26e8ff6fa158c
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: configinit.cxx,v $
10 * $Revision: 1.15 $
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 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_desktop.hxx"
35 #include "configinit.hxx"
37 #include "desktop.hrc"
38 #include "app.hxx"
39 #include <comphelper/processfactory.hxx>
40 #include <uno/current_context.hxx>
41 #include <cppuhelper/implbase1.hxx>
42 #include <rtl/ustrbuf.hxx>
43 #include <osl/diagnose.h>
44 #include <stdio.h>
45 #include <map>
46 #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
47 #include <com/sun/star/configuration/CannotLoadConfigurationException.hpp>
48 #include <com/sun/star/configuration/InvalidBootstrapFileException.hpp>
49 #include <com/sun/star/configuration/backend/BackendSetupException.hpp>
50 #include <com/sun/star/configuration/backend/CannotConnectException.hpp>
52 // ----------------------------------------------------------------------------
54 namespace uno = ::com::sun::star::uno;
55 namespace lang = ::com::sun::star::lang;
56 namespace configuration = ::com::sun::star::configuration;
57 namespace backend = ::com::sun::star::configuration::backend;
58 using rtl::OUString;
59 using uno::UNO_QUERY;
60 using desktop::Desktop;
62 // ----------------------------------------------------------------------------
63 static char const CONFIGURATION_PROVIDER[] = "com.sun.star.configuration.ConfigurationProvider";
65 static char const CONFIGURATION_ERROR_HANDLER[] = "com.sun.star.configuration.backend.InteractionHandler";
67 // must be aligned with configmgr/source/misc/configinteractionhandler
68 static char const CONFIG_ERROR_HANDLER[] = "configuration.interaction-handler";
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
72 #define arraysize( arr ) ( sizeof (arr)/sizeof *(arr) )
74 typedef uno::Reference< lang::XMultiServiceFactory > ConfigurationProvider;
76 #define OUSTRING( constascii ) OUString( RTL_CONSTASCII_USTRINGPARAM( constascii ) )
78 #define OU2O( ustr, enc ) rtl::OUStringToOString( (ustr), RTL_TEXTENCODING_ ## enc )
80 #define k_PROVIDER OUSTRING( CONFIGURATION_PROVIDER )
81 #define k_ERRORHANDLER OUSTRING( CONFIGURATION_ERROR_HANDLER )
82 // ----------------------------------------------------------------------------
83 // Get a message string securely. There is a fallback string if the resource
84 // is not available. Adapted from Desktop::GetMsgString()
86 OUString getMsgString( USHORT nId, char const * aFallBackMsg )
88 ResMgr* pResMgr = Desktop::GetDesktopResManager();
89 if ( !pResMgr || !nId )
90 return OUString::createFromAscii(aFallBackMsg);
91 else
92 return OUString( String(ResId( nId, *pResMgr )));
94 // ----------------------------------------------------------------------------
95 /** Creates the normal configuration provider.
98 static
99 ConfigurationProvider createDefaultConfigurationProvider( )
101 uno::Reference< lang::XMultiServiceFactory > xServiceManager = ::comphelper::getProcessServiceFactory();
103 OSL_ENSURE( xServiceManager.is(),"No ServiceManager set for CreateApplicationConfigurationProvider");
105 ConfigurationProvider xProvider;
107 if (xServiceManager.is())
110 xProvider.set( xServiceManager->createInstance(k_PROVIDER), UNO_QUERY);
113 if (!xProvider.is())
115 OUString const sMsg = OUSTRING("Service \"") + k_PROVIDER +
116 OUSTRING("\" is not available at the service manager.");
118 throw lang::ServiceNotRegisteredException(sMsg, xServiceManager);
121 return xProvider;
123 // ----------------------------------------------------------------------------
124 /// @attention this method must be called from a catch statement!
125 static void handleGeneralException(uno::Exception& aException,
126 const rtl::OUString& aMessage)
128 aException.Message = aMessage ;
129 throw ;
131 // ----------------------------------------------------------------------------
133 uno::Reference< lang::XMultiServiceFactory > CreateApplicationConfigurationProvider( )
135 uno::Reference< lang::XMultiServiceFactory > xProvider;
139 xProvider = createDefaultConfigurationProvider( );
141 catch (configuration::InvalidBootstrapFileException & exception)
143 handleGeneralException(exception,
144 getMsgString( STR_CONFIG_ERR_SETTINGS_INCOMPLETE,
145 "The startup settings for your configuration settings are incomplete. "));
147 catch (backend::CannotConnectException & exception)
149 handleGeneralException(exception,
150 getMsgString( STR_CONFIG_ERR_CANNOT_CONNECT,
151 "A connection to your configuration settings could not be established. "));
153 catch (backend::BackendSetupException & exception)
155 handleGeneralException(exception,
156 getMsgString( STR_CONFIG_ERR_CANNOT_CONNECT,
157 "A connection to your configuration settings could not be established. "));
159 catch (configuration::CannotLoadConfigurationException & exception)
161 handleGeneralException(exception,
162 getMsgString( STR_CONFIG_ERR_CANNOT_CONNECT,
163 "A connection to your configuration settings could not be established. "));
165 catch (uno::Exception & exception)
167 handleGeneralException(exception,
168 getMsgString( STR_CONFIG_ERR_ACCESS_GENERAL,
169 "A general error occurred while accessing your configuration settings."));
173 return xProvider ;
175 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
182 // ConfigurationErrorHandler
183 // ----------------------------------------------------------------------------
185 namespace
187 typedef uno::Reference< uno::XCurrentContext > CurrentContext;
188 class SimpleCurrentContext : public cppu::WeakImplHelper1< uno::XCurrentContext >
190 CurrentContext m_xChainedContext;
191 public:
192 explicit
193 SimpleCurrentContext(const CurrentContext & xChainedContext)
194 : m_xChainedContext(xChainedContext)
197 void install() { uno::setCurrentContext(this); }
198 void deinstall() { uno::setCurrentContext(m_xChainedContext); }
200 uno::Any getChainedValueByName( OUString const & aName) const
202 return m_xChainedContext.is()
203 ? m_xChainedContext->getValueByName(aName)
204 : uno::Any();
207 // XCurrentContext
208 virtual uno::Any SAL_CALL
209 getValueByName( OUString const & aName)
210 throw (uno::RuntimeException);
213 uno::Any SAL_CALL
214 SimpleCurrentContext::getValueByName( OUString const & aName)
215 throw (uno::RuntimeException)
217 return getChainedValueByName(aName);
222 // ----------------------------------------------------------------------------
223 class ConfigurationErrorHandler::Context : public SimpleCurrentContext
225 public:
226 Context()
227 : SimpleCurrentContext( uno::getCurrentContext() )
231 ~Context()
235 // XCurrentContext
236 virtual uno::Any SAL_CALL
237 getValueByName( OUString const & aName)
238 throw (uno::RuntimeException);
240 private:
241 InteractionHandler m_xHandler;
244 //------------------------------------------------------------------------------
245 uno::Any SAL_CALL ConfigurationErrorHandler::Context::getValueByName( OUString const & aName)
246 throw (uno::RuntimeException)
248 if ( aName.equalsAscii( CONFIG_ERROR_HANDLER ) )
250 if ( !m_xHandler.is() )
251 m_xHandler = ConfigurationErrorHandler::getDefaultInteractionHandler();
252 return uno::Any( m_xHandler );
254 return SimpleCurrentContext::getValueByName( aName );
257 //------------------------------------------------------------------------------
258 ConfigurationErrorHandler::~ConfigurationErrorHandler()
260 deactivate();
263 //------------------------------------------------------------------------------
264 /// installs the handler into the current context
265 void ConfigurationErrorHandler::activate()
267 if (!m_pContext)
269 m_pContext = new Context;
270 m_pContext->acquire();
272 m_pContext->install();
275 //------------------------------------------------------------------------------
276 /// deinstalls the handler from the current context, restoring the previous context
277 void ConfigurationErrorHandler::deactivate()
279 if (m_pContext)
281 m_pContext->deinstall();
282 m_pContext->release();
283 m_pContext = 0;
286 //------------------------------------------------------------------------------
288 ConfigurationErrorHandler::InteractionHandler ConfigurationErrorHandler::getDefaultInteractionHandler()
290 uno::Reference< lang::XMultiServiceFactory > xServiceManager = ::comphelper::getProcessServiceFactory();
292 OSL_ENSURE( xServiceManager.is(),"No ServiceManager set for ConfigurationErrorHandler");
294 InteractionHandler xHandler;
296 if (xServiceManager.is())
298 xHandler.set( xServiceManager->createInstance(k_ERRORHANDLER), UNO_QUERY );
301 return xHandler;
303 //------------------------------------------------------------------------------