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