update emoji autocorrect entries from po-files
[LibreOffice.git] / connectivity / source / drivers / mozab / MDriver.cxx
blobc17fd3d130770a7d805cb9b4e4a9e0778508f1bb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <sal/macros.h>
21 #include "MDriver.hxx"
22 #include "MConnection.hxx"
23 #include <connectivity/dbexception.hxx>
24 #include <cppuhelper/supportsservice.hxx>
25 #include "resource/mozab_res.hrc"
26 #include "resource/common_res.hrc"
28 #include <tools/solar.h>
29 using namespace com::sun::star::uno;
30 using namespace com::sun::star::lang;
31 using namespace com::sun::star::beans;
32 using namespace com::sun::star::sdbc;
33 using namespace connectivity::mozab;
35 namespace connectivity
37 namespace mozab
40 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL MozabDriver_CreateInstance(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory) throw( ::com::sun::star::uno::Exception )
42 return *(new MozabDriver( _rxFactory ));
47 MozabDriver::MozabDriver(
48 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory)
49 : ODriver_BASE(m_aMutex), m_xMSFactory( _rxFactory )
50 ,m_hModule(NULL)
51 ,m_pCreationFunc(NULL)
55 MozabDriver::~MozabDriver()
59 void MozabDriver::disposing()
61 ::osl::MutexGuard aGuard(m_aMutex);
63 // when driver will be destroied so all our connections have to be destroied as well
64 for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i)
66 Reference< XComponent > xComp(i->get(), UNO_QUERY);
67 if (xComp.is())
68 xComp->dispose();
70 m_xConnections.clear();
71 connectivity::OWeakRefArray().swap(m_xConnections); // this really clears
73 ODriver_BASE::disposing();
74 if(m_hModule)
76 m_pCreationFunc = NULL;
77 osl_unloadModule(m_hModule);
78 m_hModule = NULL;
82 // static ServiceInfo
84 OUString MozabDriver::getImplementationName_Static( ) throw(RuntimeException)
86 return OUString(MOZAB_DRIVER_IMPL_NAME);
87 // this name is referenced in the configuration and in the mozab.xml
88 // Please take care when changing it.
91 Sequence< OUString > MozabDriver::getSupportedServiceNames_Static( ) throw (RuntimeException)
93 // which service is supported
94 // for more information @see com.sun.star.sdbc.Driver
95 Sequence< OUString > aSNS( 1 );
96 aSNS[0] = "com.sun.star.sdbc.Driver";
97 return aSNS;
101 OUString SAL_CALL MozabDriver::getImplementationName( ) throw(RuntimeException)
103 return getImplementationName_Static();
106 sal_Bool SAL_CALL MozabDriver::supportsService( const OUString& _rServiceName ) throw(RuntimeException)
108 return cppu::supportsService(this, _rServiceName);
112 Sequence< OUString > SAL_CALL MozabDriver::getSupportedServiceNames( ) throw(RuntimeException)
114 return getSupportedServiceNames_Static();
118 Reference< XConnection > SAL_CALL MozabDriver::connect( const OUString& url, const Sequence< PropertyValue >& info ) throw(SQLException, RuntimeException)
120 if ( !ensureInit() )
121 return NULL;
123 if ( ! acceptsURL( url ) )
124 return NULL;
125 // create a new connection with the given properties and append it to our vector
126 Reference< XConnection > xCon;
127 if (m_pCreationFunc)
129 ::osl::MutexGuard aGuard(m_aMutex);
130 //We must make sure we create an com.sun.star.mozilla.MozillaBootstrap brfore call any mozilla codes
131 Reference<XInterface> xInstance = m_xMSFactory->createInstance("com.sun.star.mozilla.MozillaBootstrap");
132 OSL_ENSURE( xInstance.is(), "failed to create instance" );
134 OConnection* pCon = reinterpret_cast<OConnection*>((*m_pCreationFunc)(this));
135 xCon = pCon; // important here because otherwise the connection could be deleted inside (refcount goes -> 0)
136 pCon->construct(url,info); // late constructor call which can throw exception and allows a correct dtor call when so
137 m_xConnections.push_back(WeakReferenceHelper(*pCon));
140 else
142 ::connectivity::SharedResources aResources;
143 const OUString sError( aResources.getResourceStringWithSubstitution(
144 STR_COULD_NOT_LOAD_LIB,
145 "$libname$", OUString( SVLIBRARY( "mozabdrv" ) )
146 ) );
148 ::dbtools::throwGenericSQLException(sError,*this);
151 return xCon;
154 sal_Bool SAL_CALL MozabDriver::acceptsURL( const OUString& url )
155 throw(SQLException, RuntimeException)
157 if ( !ensureInit() )
158 return sal_False;
160 // here we have to look if we support this url format
161 return impl_classifyURL(url) != Unknown;
164 Sequence< DriverPropertyInfo > SAL_CALL MozabDriver::getPropertyInfo( const OUString& url, const Sequence< PropertyValue >& /*info*/ ) throw(SQLException, RuntimeException)
166 if ( !ensureInit() )
167 return Sequence< DriverPropertyInfo >();
169 if ( acceptsURL(url) )
171 if ( impl_classifyURL(url) != LDAP )
172 return Sequence< DriverPropertyInfo >();
174 ::std::vector< DriverPropertyInfo > aDriverInfo;
175 aDriverInfo.push_back(DriverPropertyInfo(
176 OUString("BaseDN")
177 ,OUString("Base DN.")
178 ,sal_False
179 ,OUString()
180 ,Sequence< OUString >())
182 aDriverInfo.push_back(DriverPropertyInfo(
183 OUString("MaxRowCount")
184 ,OUString("Records (max.)")
185 ,sal_False
186 ,OUString("100")
187 ,Sequence< OUString >())
189 return Sequence< DriverPropertyInfo >(&aDriverInfo[0],aDriverInfo.size());
191 ::connectivity::SharedResources aResources;
192 const OUString sMessage = aResources.getResourceString(STR_URI_SYNTAX_ERROR);
193 ::dbtools::throwGenericSQLException(sMessage ,*this);
194 // if you have something special to say return it here :-)
195 return Sequence< DriverPropertyInfo >();
198 sal_Int32 SAL_CALL MozabDriver::getMajorVersion( ) throw(RuntimeException)
200 return 1; // depends on you
203 sal_Int32 SAL_CALL MozabDriver::getMinorVersion( ) throw(RuntimeException)
205 return 0; // depends on you
208 EDriverType MozabDriver::impl_classifyURL( const OUString& url )
210 // Skip 'sdbc:mozab: part of URL
212 sal_Int32 nLen = url.indexOf(':');
213 nLen = url.indexOf(':',nLen+1);
214 OUString aAddrbookURI(url.copy(nLen+1));
215 // Get Scheme
216 nLen = aAddrbookURI.indexOf(':');
217 OUString aAddrbookScheme;
218 if ( nLen == -1 )
220 // There isn't any subschema: - but could be just subschema
221 if ( !aAddrbookURI.isEmpty() )
222 aAddrbookScheme= aAddrbookURI;
223 else if(url == "sdbc:address:" )
224 return Unknown; // TODO check
225 else
226 return Unknown;
228 else
229 aAddrbookScheme = aAddrbookURI.copy(0, nLen);
231 struct __scheme_map
233 EDriverType eType;
234 const sal_Char* pScheme;
235 } aSchemeMap[] =
237 #if defined(WNT)
238 { Outlook, "outlook" },
239 { OutlookExpress, "outlookexp" },
240 #endif
241 { Mozilla, "mozilla" },
242 { ThunderBird, "thunderbird" },
243 { LDAP, "ldap" }
246 for ( size_t i=0; i < sizeof( aSchemeMap ) / sizeof( aSchemeMap[0] ); ++i )
248 if ( aAddrbookScheme.equalsAscii( aSchemeMap[i].pScheme ) )
249 return aSchemeMap[i].eType;
252 return Unknown;
256 namespace
258 template< typename FUNCTION >
259 void lcl_getFunctionFromModuleOrUnload( oslModule& _rModule, const sal_Char* _pAsciiSymbolName, FUNCTION& _rFunction )
261 _rFunction = NULL;
262 if ( _rModule )
264 const OUString sSymbolName = OUString::createFromAscii( _pAsciiSymbolName );
265 _rFunction = (FUNCTION)( osl_getFunctionSymbol( _rModule, sSymbolName.pData ) );
267 if ( !_rFunction )
268 { // did not find the symbol
269 OUStringBuffer aBuf;
270 aBuf.append( "lcl_getFunctionFromModuleOrUnload: could not find the symbol " );
271 aBuf.append( sSymbolName );
272 OSL_FAIL( aBuf.makeStringAndClear().getStr() );
273 osl_unloadModule( _rModule );
274 _rModule = NULL;
281 extern "C" { static void SAL_CALL thisModule() {} }
283 bool MozabDriver::ensureInit()
285 if ( m_hModule )
286 return true;
288 OSL_ENSURE(NULL == m_pCreationFunc, "MozabDriver::ensureInit: inconsistence: already have a factory function!");
290 const OUString sModuleName(SVLIBRARY( "mozabdrv" ));
292 // load the mozabdrv library
293 m_hModule = osl_loadModuleRelative(&thisModule, sModuleName.pData, 0);
294 OSL_ENSURE(NULL != m_hModule, "MozabDriver::ensureInit: could not load the mozabdrv library!");
295 if ( !m_hModule )
296 return false;
298 OSetMozabServiceFactory pSetFactoryFunc( NULL );
300 lcl_getFunctionFromModuleOrUnload( m_hModule, "setMozabServiceFactory", pSetFactoryFunc );
301 lcl_getFunctionFromModuleOrUnload( m_hModule, "OMozabConnection_CreateInstance", m_pCreationFunc );
303 if ( !m_hModule )
304 // one of the symbols did not exist
305 return false;
307 if ( m_xMSFactory.is() )
309 // for purpose of transfer safety, the interface needs to be acuired once
310 // (will be release by the callee)
311 m_xMSFactory->acquire();
312 ( *pSetFactoryFunc )( m_xMSFactory.get() );
315 return true;
319 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */