merge the formfield patch from ooo-build
[ooovba.git] / dbaccess / source / ui / dlg / odbcconfig.cxx
blob697fb237b8f9df91a7a2fcaa67ff5513afd8f268
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: odbcconfig.cxx,v $
10 * $Revision: 1.26 $
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_dbaccess.hxx"
34 #ifndef _DBAUI_ODBC_CONFIG_HXX_
35 #include "odbcconfig.hxx"
36 #endif
37 #include <rtl/bootstrap.hxx>
38 #ifndef _RTL_USTRING_HXX_
39 #include <rtl/ustring.hxx>
40 #endif
41 #ifndef _RTL_USTRBUF_HXX_
42 #include <rtl/ustrbuf.hxx>
43 #endif
44 #ifndef _OSL_DIAGNOSE_H_
45 #include <osl/diagnose.h>
46 #endif
47 #ifndef _OSL_PROCESS_H_
48 #include <osl/process.h>
49 #endif
50 #ifndef _THREAD_HXX_
51 #include <osl/thread.hxx>
52 #endif
53 #ifndef _TOOLS_DEBUG_HXX
54 #include <tools/debug.hxx>
55 #endif
56 #ifndef _SV_SVAPP_HXX
57 #include <vcl/svapp.hxx>
58 #endif
60 #ifdef HAVE_ODBC_SUPPORT
62 #if defined(WIN) || defined(OS2)
63 #define ODBC_LIBRARY "ODBC.DLL"
64 #define ODBC_UI_LIBRARY "ODBCINST.DLL"
65 #endif
66 #if defined WNT
67 #define ODBC_LIBRARY "ODBC32.DLL"
68 #define ODBC_UI_LIBRARY "ODBCCP32.DLL"
69 #endif
70 #ifdef UNX
71 #ifdef MACOSX
72 #define ODBC_LIBRARY "libiodbc.dylib"
73 #define ODBC_UI_LIBRARY "libiodbcinst.dylib"
74 #else
75 #define ODBC_LIBRARY_1 "libodbc.so.1"
76 #define ODBC_UI_LIBRARY_1 "libodbcinst.so.1"
77 #define ODBC_LIBRARY "libodbc.so"
78 #define ODBC_UI_LIBRARY "libodbcinst.so"
79 #endif
80 #endif
82 // just to go with calling convention of windows
83 // so don't touch this
84 #if defined(WIN) || defined(WNT)
85 #define SQL_API __stdcall
86 // At least under some circumstances, the below #include <odbc/sqlext.h> re-
87 // defines SQL_API to an empty string, leading to a compiler warning on MSC; to
88 // not break the current behavior, this is worked around by locally disabling
89 // that warning:
90 #if defined _MSC_VER
91 #pragma warning(push)
92 #pragma warning(disable: 4005)
93 #endif
94 #endif // defined(WIN) || defined(WNT)
96 #if defined(OS2)
97 #define ALLREADY_HAVE_OS2_TYPES
98 #define DONT_TD_VOID
99 #endif
101 #ifdef SYSTEM_ODBC_HEADERS
102 #include <sqlext.h>
103 #else
104 #ifndef __SQLEXT_H
105 #include <odbc/sqlext.h>
106 #endif
107 #endif
109 #if defined(WIN) || defined(WNT)
110 #if defined _MSC_VER
111 #pragma warning(pop)
112 #endif
113 #undef SQL_API
114 #define SQL_API __stdcall
115 #endif // defined(WIN) || defined(WNT)
116 // from here on you can do what you want to
118 #if defined(OS2)
119 #define SQL_API _System
120 #endif // defined(OS2)
122 #else
124 #define ODBC_LIBRARY ""
125 #define ODBC_UI_LIBRARY ""
127 #endif // HAVE_ODBC_SUPPORT
129 //.........................................................................
130 namespace dbaui
132 //.........................................................................
135 #ifdef HAVE_ODBC_SUPPORT
136 typedef SQLRETURN (SQL_API* TSQLManageDataSource) (SQLHWND hwndParent);
137 typedef SQLRETURN (SQL_API* TSQLAllocHandle) (SQLSMALLINT HandleType, SQLHANDLE InputHandle, SQLHANDLE* OutputHandlePtr);
138 typedef SQLRETURN (SQL_API* TSQLFreeHandle) (SQLSMALLINT HandleType, SQLHANDLE Handle);
139 typedef SQLRETURN (SQL_API* TSQLSetEnvAttr) (SQLHENV EnvironmentHandle, SQLINTEGER Attribute, SQLPOINTER ValuePtr, SQLINTEGER StringLength);
140 typedef SQLRETURN (SQL_API* TSQLDataSources) (SQLHENV EnvironmentHandle, SQLUSMALLINT Direction, SQLCHAR* ServerName,
141 SQLSMALLINT BufferLength1, SQLSMALLINT* NameLength1Ptr, SQLCHAR* Description, SQLSMALLINT BufferLength2, SQLSMALLINT* NameLength2Ptr);
143 #define NSQLManageDataSource(a) (*(TSQLManageDataSource)(m_pSQLManageDataSource))(a)
144 #define NSQLAllocHandle(a,b,c) (*(TSQLAllocHandle)(m_pAllocHandle))(a,b,c)
145 #define NSQLFreeHandle(a,b) (*(TSQLFreeHandle)(m_pFreeHandle))(a,b)
146 #define NSQLSetEnvAttr(a,b,c,d) (*(TSQLSetEnvAttr)(m_pSetEnvAttr))(a,b,c,d)
147 #define NSQLDataSources(a,b,c,d,e,f,g,h) (*(TSQLDataSources)(m_pDataSources))(a,b,c,d,e,f,g,h)
148 #endif
150 //=========================================================================
151 //= OOdbcLibWrapper
152 //=========================================================================
153 DBG_NAME(OOdbcLibWrapper)
154 //-------------------------------------------------------------------------
155 #ifdef HAVE_ODBC_SUPPORT
156 OOdbcLibWrapper::OOdbcLibWrapper()
157 :m_pOdbcLib(NULL)
159 DBG_CTOR(OOdbcLibWrapper,NULL);
162 #endif
164 //-------------------------------------------------------------------------
165 sal_Bool OOdbcLibWrapper::load(const sal_Char* _pLibPath)
167 m_sLibPath = ::rtl::OUString::createFromAscii(_pLibPath);
168 #ifdef HAVE_ODBC_SUPPORT
169 // load the module
170 m_pOdbcLib = osl_loadModule(m_sLibPath.pData, SAL_LOADMODULE_NOW);
171 return (NULL != m_pOdbcLib);
172 #endif
175 //-------------------------------------------------------------------------
176 void OOdbcLibWrapper::unload()
178 #ifdef HAVE_ODBC_SUPPORT
179 if (isLoaded())
181 osl_unloadModule(m_pOdbcLib);
182 m_pOdbcLib = NULL;
184 #endif
187 //-------------------------------------------------------------------------
188 oslGenericFunction OOdbcLibWrapper::loadSymbol(const sal_Char* _pFunctionName)
190 return osl_getFunctionSymbol(m_pOdbcLib, ::rtl::OUString::createFromAscii(_pFunctionName).pData);
193 //-------------------------------------------------------------------------
194 OOdbcLibWrapper::~OOdbcLibWrapper()
196 unload();
198 DBG_DTOR(OOdbcLibWrapper,NULL);
201 //=========================================================================
202 //= OOdbcEnumeration
203 //=========================================================================
204 struct OdbcTypesImpl
206 #ifdef HAVE_ODBC_SUPPORT
207 SQLHANDLE hEnvironment;
208 OdbcTypesImpl() : hEnvironment(0) { }
209 #else
210 void* pDummy;
211 #endif
213 DBG_NAME(OOdbcEnumeration)
214 //-------------------------------------------------------------------------
215 OOdbcEnumeration::OOdbcEnumeration()
216 #ifdef HAVE_ODBC_SUPPORT
217 :m_pAllocHandle(NULL)
218 ,m_pSetEnvAttr(NULL)
219 ,m_pDataSources(NULL)
220 ,m_pImpl(new OdbcTypesImpl)
221 #endif
223 DBG_CTOR(OOdbcEnumeration,NULL);
225 sal_Bool bLoaded = load(ODBC_LIBRARY);
226 #ifdef ODBC_LIBRARY_1
227 if ( !bLoaded )
228 bLoaded = load(ODBC_LIBRARY_1);
229 #endif
231 if ( bLoaded )
233 #ifdef HAVE_ODBC_SUPPORT
234 // load the generic functions
235 m_pAllocHandle = loadSymbol("SQLAllocHandle");
236 m_pFreeHandle = loadSymbol("SQLFreeHandle");
237 m_pSetEnvAttr = loadSymbol("SQLSetEnvAttr");
238 m_pDataSources = loadSymbol("SQLDataSources");
240 // all or nothing
241 if (!m_pAllocHandle || !m_pSetEnvAttr || !m_pDataSources || !m_pFreeHandle)
243 unload();
244 m_pAllocHandle = m_pFreeHandle = m_pSetEnvAttr = m_pDataSources = NULL;
246 #endif
250 //-------------------------------------------------------------------------
251 OOdbcEnumeration::~OOdbcEnumeration()
253 freeEnv();
254 delete m_pImpl;
256 DBG_DTOR(OOdbcEnumeration,NULL);
259 //-------------------------------------------------------------------------
260 sal_Bool OOdbcEnumeration::allocEnv()
262 OSL_ENSURE(isLoaded(), "OOdbcEnumeration::allocEnv: not loaded!");
263 if (!isLoaded())
264 return sal_False;
266 #ifdef HAVE_ODBC_SUPPORT
267 if (m_pImpl->hEnvironment)
268 // nothing to do
269 return sal_True;
270 SQLRETURN nResult = NSQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &m_pImpl->hEnvironment);
271 if (SQL_SUCCESS != nResult)
272 // can't do anything without environment
273 return sal_False;
275 NSQLSetEnvAttr(m_pImpl->hEnvironment, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER);
276 return sal_True;
277 #else
278 return sal_False;
279 #endif
282 //-------------------------------------------------------------------------
283 void OOdbcEnumeration::freeEnv()
285 #ifdef HAVE_ODBC_SUPPORT
286 if (m_pImpl->hEnvironment)
287 NSQLFreeHandle(SQL_HANDLE_ENV, m_pImpl->hEnvironment);
288 m_pImpl->hEnvironment = 0;
289 #endif
292 //-------------------------------------------------------------------------
293 void OOdbcEnumeration::getDatasourceNames(StringBag& _rNames)
295 OSL_ENSURE(isLoaded(), "OOdbcEnumeration::getDatasourceNames: not loaded!");
296 if (!isLoaded())
297 return;
299 if (!allocEnv())
301 OSL_ENSURE(sal_False, "OOdbcEnumeration::getDatasourceNames: could not allocate an ODBC environment!");
302 return;
305 #ifdef HAVE_ODBC_SUPPORT
306 // now that we have an environment collect the data source names
307 UCHAR szDSN[SQL_MAX_DSN_LENGTH+1];
308 SWORD pcbDSN;
309 UCHAR szDescription[1024+1];
310 SWORD pcbDescription;
311 SQLRETURN nResult = SQL_SUCCESS;
312 rtl_TextEncoding nTextEncoding = osl_getThreadTextEncoding();
314 for ( nResult = NSQLDataSources(m_pImpl->hEnvironment, SQL_FETCH_FIRST, szDSN, sizeof(szDSN), &pcbDSN, szDescription, sizeof(szDescription)-1, &pcbDescription);
316 nResult = NSQLDataSources(m_pImpl->hEnvironment, SQL_FETCH_NEXT, szDSN, sizeof(szDSN), &pcbDSN, szDescription, sizeof(szDescription)-1, &pcbDescription)
319 if (nResult != SQL_SUCCESS)
320 // no further error handling
321 break;
322 else
324 ::rtl::OUString aCurrentDsn(reinterpret_cast<const char*>(szDSN),pcbDSN, nTextEncoding);
325 _rNames.insert(aCurrentDsn);
328 #endif
331 #ifdef HAVE_ODBC_ADMINISTRATION
333 //=========================================================================
334 //= ProcessTerminationWait
335 //=========================================================================
336 class ProcessTerminationWait : public ::osl::Thread
338 oslProcess m_hProcessHandle;
339 Link m_aFinishHdl;
341 public:
342 ProcessTerminationWait( oslProcess _hProcessHandle, const Link& _rFinishHdl )
343 :m_hProcessHandle( _hProcessHandle )
344 ,m_aFinishHdl( _rFinishHdl )
348 protected:
349 virtual void SAL_CALL run()
351 osl_joinProcess( m_hProcessHandle );
352 osl_freeProcessHandle( m_hProcessHandle );
353 Application::PostUserEvent( m_aFinishHdl );
357 //=========================================================================
358 //= OOdbcManagement
359 //=========================================================================
360 //-------------------------------------------------------------------------
361 OOdbcManagement::OOdbcManagement( const Link& _rAsyncFinishCallback )
362 :m_pProcessWait( NULL )
363 ,m_aAsyncFinishCallback( _rAsyncFinishCallback )
367 //-------------------------------------------------------------------------
368 OOdbcManagement::~OOdbcManagement()
370 // wait for our thread to be finished
371 if ( m_pProcessWait.get() )
372 m_pProcessWait->join();
375 //-------------------------------------------------------------------------
376 bool OOdbcManagement::manageDataSources_async()
378 OSL_PRECOND( !isRunning(), "OOdbcManagement::manageDataSources_async: still running from the previous call!" );
379 if ( isRunning() )
380 return false;
382 // this is done in an external process, due to #i78733#
383 // (and note this whole functionality is supported on Windows only, ATM)
384 ::rtl::OUString sExecutableName( RTL_CONSTASCII_USTRINGPARAM( "$OOO_BASE_DIR/program/odbcconfig.exe" ) );
385 ::rtl::Bootstrap::expandMacros( sExecutableName ); //TODO: detect failure
386 oslProcess hProcessHandle(0);
387 oslProcessError eError = osl_executeProcess( sExecutableName.pData, NULL, 0, 0, NULL, NULL, NULL, 0, &hProcessHandle );
388 if ( eError != osl_Process_E_None )
389 return false;
391 m_pProcessWait.reset( new ProcessTerminationWait( hProcessHandle, m_aAsyncFinishCallback ) );
392 m_pProcessWait->create();
393 return true;
396 //-------------------------------------------------------------------------
397 bool OOdbcManagement::isRunning() const
399 return ( m_pProcessWait.get() && m_pProcessWait->isRunning() );
402 #endif // HAVE_ODBC_ADMINISTRATION
404 //.........................................................................
405 } // namespace dbaui
406 //.........................................................................