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 .
21 #include "com/sun/star/configuration/theDefaultProvider.hpp"
22 #include "comphelper/processfactory.hxx"
24 #include "MConfigAccess.hxx"
25 #include "MExtConfigAccess.hxx"
26 #include "MConnection.hxx"
28 using namespace ::com::sun::star::uno
;
29 using namespace ::com::sun::star::lang
;
30 using namespace ::com::sun::star::beans
;
33 namespace connectivity
38 Reference
< XPropertySet
> createDriverConfigNode( Reference
< XMultiServiceFactory
> _rxORB
)
40 Reference
< XPropertySet
> xNode
;
44 // create the config provider
45 Reference
< XMultiServiceFactory
> xConfigProvider(
46 com::sun::star::configuration::theDefaultProvider::get(
47 comphelper::getComponentContext( _rxORB
) ) );
49 OUString
sCompleteNodePath( "/org.openoffice.Office.DataAccess/DriverSettings/" );
50 sCompleteNodePath
+= OConnection::getDriverImplementationName();
53 // arguments for creating the config access
54 Sequence
< Any
> aArguments(2);
55 // the path to the node to open
56 aArguments
[0] <<= PropertyValue(
59 makeAny( sCompleteNodePath
),
60 PropertyState_DIRECT_VALUE
62 // the depth: -1 means unlimited
63 aArguments
[1] <<= PropertyValue(
66 makeAny( (sal_Int32
)-1 ),
67 PropertyState_DIRECT_VALUE
72 Reference
< XInterface
> xAccess
= xConfigProvider
->createInstanceWithArguments(
73 OUString("com.sun.star.configuration.ConfigurationAccess" ),
76 OSL_ENSURE( xAccess
.is(), "createDriverConfigNode: invalid access returned (should throw an exception instead)!" );
78 xNode
= xNode
.query( xAccess
);
80 catch( const Exception
& )
82 OSL_FAIL( "createDriverConfigNode: caught an exception while accessing the driver's config node!" );
92 // a private helper to accessing the point where we store the reference
94 Reference
< XMultiServiceFactory
>& accessFactoryStorage( )
96 static Reference
< XMultiServiceFactory
> xMozabServiceFactory
;
97 return xMozabServiceFactory
;
102 void setMozabServiceFactory( const Reference
< XMultiServiceFactory
>& _rxFactory
)
104 accessFactoryStorage( ) = _rxFactory
;
108 const Reference
< XMultiServiceFactory
>& getMozabServiceFactory( )
110 return accessFactoryStorage( );
114 OUString
getDescription(const sal_Char
* sNode
,const OUString
& sDefault
)
116 OUString sPreferredName
;
117 OUString sDescription
;
119 Reference
< XMultiServiceFactory
> xFactory
= getMozabServiceFactory();
120 OSL_ENSURE( xFactory
.is(), "getPreferredProfileName: invalid service factory!" );
125 Reference
< XPropertySet
> xDriverNode
= createDriverConfigNode( xFactory
);
126 Reference
< XPropertySet
> xMozPrefsNode
;
127 if ( xDriverNode
.is() )
128 xDriverNode
->getPropertyValue("MozillaPreferences") >>= xMozPrefsNode
;
129 OSL_ENSURE( xMozPrefsNode
.is(), "getPreferredProfileName: could not access the node for the mozilla preferences!" );
130 if ( xMozPrefsNode
.is() )
131 xMozPrefsNode
->getPropertyValue("ProfileName") >>= sPreferredName
;
132 if ( xMozPrefsNode
.is() )
133 xMozPrefsNode
->getPropertyValue( OUString::createFromAscii(sNode
) ) >>= sDescription
;
134 if (sDescription
.getLength() == 0)
135 sDescription
= sDefault
;
137 catch( const Exception
& )
139 OSL_FAIL( "getDescription: caught an exception!" );
142 if (sDescription
.getLength() == 0)
143 sDescription
= sDefault
;
147 OUString
getPreferredProfileName( )
149 OUString sPreferredName
;
151 Reference
< XMultiServiceFactory
> xFactory
= getMozabServiceFactory();
152 OSL_ENSURE( xFactory
.is(), "getPreferredProfileName: invalid service factory!" );
157 Reference
< XPropertySet
> xDriverNode
= createDriverConfigNode( xFactory
);
158 Reference
< XPropertySet
> xMozPrefsNode
;
159 if ( xDriverNode
.is() )
160 xDriverNode
->getPropertyValue("MozillaPreferences") >>= xMozPrefsNode
;
161 OSL_ENSURE( xMozPrefsNode
.is(), "getPreferredProfileName: could not access the node for the mozilla preferences!" );
162 if ( xMozPrefsNode
.is() )
163 xMozPrefsNode
->getPropertyValue("ProfileName") >>= sPreferredName
;
165 catch( const Exception
& )
167 OSL_FAIL( "getPreferredProfileName: caught an exception!" );
170 return sPreferredName
;
178 extern "C" const sal_Unicode
* SAL_CALL
getUserProfile( void )
180 static sal_Bool bReadConfig
= sal_False
;
181 static OUString sUserProfile
;
184 sUserProfile
= ::connectivity::mozab::getPreferredProfileName( );
185 bReadConfig
= sal_True
;
188 return sUserProfile
.getStr();
191 extern "C" const sal_Char
* SAL_CALL
getPabDescription( void )
193 static sal_Bool bReadConfig
= sal_False
;
194 static OUString usPabDescription
;
195 static OString sPabDescription
;
199 usPabDescription
= ::connectivity::mozab::getDescription(
201 OUString("Personal Address Book" ));
202 sPabDescription
= OUStringToOString( usPabDescription
,
203 RTL_TEXTENCODING_UTF8
);
204 bReadConfig
= sal_True
;
207 return sPabDescription
.getStr();
211 extern "C" const sal_Char
* SAL_CALL
getHisDescription( void )
213 static sal_Bool bReadConfig
= sal_False
;
214 static OUString usHisDescription
;
215 static OString sHisDescription
;
219 usHisDescription
= ::connectivity::mozab::getDescription(
221 OUString("Collected Addresses" ));
222 sHisDescription
= OUStringToOString( usHisDescription
,
223 RTL_TEXTENCODING_UTF8
);
224 bReadConfig
= sal_True
;
227 return sHisDescription
.getStr();
231 // MConfigAccess was invented to allow non-UNO parts access to the configuration.
232 // Unfortunately, configuration access requires a XMultiServiceFactory - which the
233 // mozilla side does not have.
234 // So we create a "library-local" service factory here: Every need for a service
235 // factory can be fullfilled by this factory (similar to the get/setProcessServiceFactory
237 // This is halfway valid, as usually, the mozabdrv library is invoked from the mozab library
238 // only. The latter contains the driver class (and only this class and nothing more), and
239 // the driver class is a singleton. The driver itself is created with a service factory,
240 // which (by definition) can and should be used for all subsequent service requests.
241 // And this is exactly what we're allowing with the following functions ....
243 /** _pFactory must point to an XMultiServiceFactory, which must be aquired once
244 for purpose of safely transfering it. The callee will release this interface
245 when it has stored the pointer somewhere else.
247 extern "C" SAL_DLLPUBLIC_EXPORT
void SAL_CALL
setMozabServiceFactory(
250 Reference
< XMultiServiceFactory
> xFactory
= static_cast< XMultiServiceFactory
* >( _pFactory
);
251 ::connectivity::mozab::setMozabServiceFactory( xFactory
);
253 // by definition, the object behind the interface pointer has been acquired once for purpose
254 // of safely transporting it
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */