merge the formfield patch from ooo-build
[ooovba.git] / connectivity / source / drivers / mozab / MConfigAccess.cxx
blob22d196e3c971841ed858ac4512f13e6c2ca605ba
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: MConfigAccess.cxx,v $
10 * $Revision: 1.9 $
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 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
34 #ifndef CONNECTIVITY_MOZAB_MCONFIGACCESS_HXX
35 #include "MConfigAccess.hxx"
36 #endif
37 #ifndef CONNECTIVITY_MOZAB_MEXTCONFIGACCESS_HXX
38 #include "MExtConfigAccess.hxx"
39 #endif
40 #include "MConnection.hxx"
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::beans;
46 //.........................................................................
47 namespace connectivity
49 namespace mozab
51 //-----------------------------------------------------------------
52 Reference< XPropertySet > createDriverConfigNode( Reference< XMultiServiceFactory > _rxORB )
54 Reference< XPropertySet > xNode;
55 try
57 //=============================================================
58 // create the config provider
59 Reference< XMultiServiceFactory > xConfigProvider(
60 _rxORB->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationProvider" )) ),
61 UNO_QUERY
63 OSL_ENSURE( xConfigProvider.is(), "createDriverConfigNode: could not create the config provider!" );
65 if ( xConfigProvider.is() )
67 ::rtl::OUString sCompleteNodePath = ::rtl::OUString::createFromAscii ("/org.openoffice.Office.DataAccess/DriverSettings/" );
68 sCompleteNodePath += OConnection::getDriverImplementationName();
70 //=========================================================
71 // arguments for creating the config access
72 Sequence< Any > aArguments(2);
73 // the path to the node to open
74 aArguments[0] <<= PropertyValue(
75 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("nodepath")),
77 makeAny( sCompleteNodePath ),
78 PropertyState_DIRECT_VALUE
80 // the depth: -1 means unlimited
81 aArguments[1] <<= PropertyValue(
82 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("depth")),
84 makeAny( (sal_Int32)-1 ),
85 PropertyState_DIRECT_VALUE
88 //=========================================================
89 // create the access
90 Reference< XInterface > xAccess = xConfigProvider->createInstanceWithArguments(
91 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationAccess" )),
92 aArguments
94 OSL_ENSURE( xAccess.is(), "createDriverConfigNode: invalid access returned (should throw an exception instead)!" );
96 xNode = xNode.query( xAccess );
99 catch( const Exception& )
101 OSL_ENSURE( sal_False, "createDriverConfigNode: caught an exception while accessing the driver's config node!" );
104 // outta here
105 return xNode;
108 //-----------------------------------------------------------------
109 namespace
111 // a private helper to accessing the point where we store the reference
112 // to the factory
113 Reference< XMultiServiceFactory >& accessFactoryStorage( )
115 static Reference< XMultiServiceFactory > xMozabServiceFactory;
116 return xMozabServiceFactory;
120 //-----------------------------------------------------------------
121 void setMozabServiceFactory( const Reference< XMultiServiceFactory >& _rxFactory )
123 accessFactoryStorage( ) = _rxFactory;
126 //-----------------------------------------------------------------
127 const Reference< XMultiServiceFactory >& getMozabServiceFactory( )
129 return accessFactoryStorage( );
132 //-----------------------------------------------------------------
133 ::rtl::OUString getDescription(const sal_Char* sNode,const ::rtl::OUString & sDefault)
135 ::rtl::OUString sPreferredName;
136 ::rtl::OUString sDescription;
138 Reference< XMultiServiceFactory > xFactory = getMozabServiceFactory();
139 OSL_ENSURE( xFactory.is(), "getPreferredProfileName: invalid service factory!" );
140 if ( xFactory.is() )
144 Reference< XPropertySet > xDriverNode = createDriverConfigNode( xFactory );
145 Reference< XPropertySet > xMozPrefsNode;
146 if ( xDriverNode.is() )
147 xDriverNode->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("MozillaPreferences" )) ) >>= xMozPrefsNode;
148 OSL_ENSURE( xMozPrefsNode.is(), "getPreferredProfileName: could not access the node for the mozilla preferences!" );
149 if ( xMozPrefsNode.is() )
150 xMozPrefsNode->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ProfileName" )) ) >>= sPreferredName;
151 if ( xMozPrefsNode.is() )
152 xMozPrefsNode->getPropertyValue( ::rtl::OUString::createFromAscii(sNode) ) >>= sDescription;
153 if (sDescription.getLength() == 0)
154 sDescription = sDefault;
156 catch( const Exception& )
158 OSL_ENSURE( sal_False, "getDescription: caught an exception!" );
161 if (sDescription.getLength() == 0)
162 sDescription = sDefault;
163 return sDescription;
165 //-----------------------------------------------------------------
166 ::rtl::OUString getPreferredProfileName( )
168 ::rtl::OUString sPreferredName;
170 Reference< XMultiServiceFactory > xFactory = getMozabServiceFactory();
171 OSL_ENSURE( xFactory.is(), "getPreferredProfileName: invalid service factory!" );
172 if ( xFactory.is() )
176 Reference< XPropertySet > xDriverNode = createDriverConfigNode( xFactory );
177 Reference< XPropertySet > xMozPrefsNode;
178 if ( xDriverNode.is() )
179 xDriverNode->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("MozillaPreferences" )) ) >>= xMozPrefsNode;
180 OSL_ENSURE( xMozPrefsNode.is(), "getPreferredProfileName: could not access the node for the mozilla preferences!" );
181 if ( xMozPrefsNode.is() )
182 xMozPrefsNode->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ProfileName" )) ) >>= sPreferredName;
184 catch( const Exception& )
186 OSL_ENSURE( sal_False, "getPreferredProfileName: caught an exception!" );
189 return sPreferredName;
194 //.........................................................................
196 //-------------------------------------------------------------------------
197 extern "C" const sal_Unicode* SAL_CALL getUserProfile( void )
199 static sal_Bool bReadConfig = sal_False;
200 static ::rtl::OUString sUserProfile;
201 if ( !bReadConfig )
203 sUserProfile = ::connectivity::mozab::getPreferredProfileName( );
204 bReadConfig = sal_True;
207 return sUserProfile.getStr();
209 //------------------------------------------------------------------------
210 extern "C" const sal_Char* SAL_CALL getPabDescription( void )
212 static sal_Bool bReadConfig = sal_False;
213 static ::rtl::OUString usPabDescription;
214 static ::rtl::OString sPabDescription;
216 if ( !bReadConfig )
218 usPabDescription = ::connectivity::mozab::getDescription(
219 "PabDescription" ,
220 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Personal Address Book" )));
221 sPabDescription = ::rtl::OUStringToOString( usPabDescription,
222 RTL_TEXTENCODING_UTF8);
223 bReadConfig = sal_True;
226 return sPabDescription.getStr();
229 //-------------------------------------------------------------------------
230 extern "C" const sal_Char* SAL_CALL getHisDescription( void )
232 static sal_Bool bReadConfig = sal_False;
233 static ::rtl::OUString usHisDescription;
234 static ::rtl::OString sHisDescription;
236 if ( !bReadConfig )
238 usHisDescription = ::connectivity::mozab::getDescription(
239 "HisDescription" ,
240 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Collected Addresses" )));
241 sHisDescription = ::rtl::OUStringToOString( usHisDescription,
242 RTL_TEXTENCODING_UTF8);
243 bReadConfig = sal_True;
246 return sHisDescription.getStr();
249 //-------------------------------------------------------------------------
250 // MConfigAccess was invented to allow non-UNO parts access to the configuration.
251 // Unfortunately, configuration access requires a XMultiServiceFactory - which the
252 // mozilla side does not have.
253 // So we create a "library-local" service factory here: Every need for a service
254 // factory can be fullfilled by this factory (similar to the get/setProcessServiceFactory
255 // in comphelper).
256 // This is halfway valid, as usually, the mozabdrv library is invoked from the mozab library
257 // only. The latter contains the driver class (and only this class and nothing more), and
258 // the driver class is a singleton. The driver itself is created with a service factory,
259 // which (by definition) can and should be used for all subsequent service requests.
260 // And this is exactly what we're allowing with the following functions ....
262 /** _pFactory must point to an XMultiServiceFactory, which must be aquired once
263 for purpose of safely transfering it. The callee will release this interface
264 when it has stored the pointer somewhere else.
266 extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL setMozabServiceFactory(
267 void* _pFactory )
269 Reference< XMultiServiceFactory > xFactory = static_cast< XMultiServiceFactory* >( _pFactory );
270 ::connectivity::mozab::setMozabServiceFactory( xFactory );
272 // by definition, the object behind the interface pointer has been acquired once for purpose
273 // of safely transporting it
274 xFactory->release();