xref: /aoo41x/main/dbaccess/source/ui/misc/UITools.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dbaccess.hxx"
30 
31 #ifndef DBAUI_TOOLS_HXX
32 #include "UITools.hxx"
33 #endif
34 #ifndef _SFX_DOCFILT_HACK_HXX
35 #include <sfx2/docfilt.hxx>
36 #endif
37 #ifndef _DBACCESS_UI_CALLBACKS_HXX_
38 #include "callbacks.hxx"
39 #endif
40 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
41 #include "dbustrings.hrc"
42 #endif
43 #ifndef _DBU_RESOURCE_HRC_
44 #include "dbu_resource.hrc"
45 #endif
46 #ifndef DBAUI_DLGSAVE_HXX
47 #include "dlgsave.hxx"
48 #endif
49 #ifndef DBAUI_DBTREELISTBOX_HXX
50 #include "dbtreelistbox.hxx"
51 #endif
52 #ifndef DBACCESS_SOURCE_UI_INC_DEFAULTOBJECTNAMECHECK_HXX
53 #include "defaultobjectnamecheck.hxx"
54 #endif
55 #ifndef _COMPHELPER_EXTRACT_HXX_
56 #include <comphelper/extract.hxx>
57 #endif
58 #ifndef _COM_SUN_STAR_SDB_XSINGLESELECTQUERYANALYZER_HPP_
59 #include <com/sun/star/sdb/XSingleSelectQueryAnalyzer.hpp>
60 #endif
61 #ifndef _COM_SUN_STAR_SDB_XCOMPLETEDCONNECTION_HPP_
62 #include <com/sun/star/sdb/XCompletedConnection.hpp>
63 #endif
64 #ifndef _COM_SUN_STAR_SDBC_XDATASOURCE_HPP_
65 #include <com/sun/star/sdbc/XDataSource.hpp>
66 #endif
67 #ifndef _COM_SUN_STAR_SDB_SQLCONTEXT_HPP_
68 #include <com/sun/star/sdb/SQLContext.hpp>
69 #endif
70 #ifndef _COM_SUN_STAR_SDBCX_XKEYSSUPPLIER_HPP_
71 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
72 #endif
73 #ifndef _COM_SUN_STAR_SDBCX_XCOLUMNSSUPPLIER_HPP_
74 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
75 #endif
76 #ifndef _COM_SUN_STAR_SDBCX_XVIEWSSUPPLIER_HPP_
77 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
78 #endif
79 #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
80 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
81 #endif
82 #ifndef _COM_SUN_STAR_SDBCX_XDATADESCRIPTORFACTORY_HPP_
83 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
84 #endif
85 #ifndef _COM_SUN_STAR_SDBCX_XAPPEND_HPP_
86 #include <com/sun/star/sdbcx/XAppend.hpp>
87 #endif
88 #ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
89 #include <com/sun/star/sdbc/XRow.hpp>
90 #endif
91 #ifndef _COM_SUN_STAR_SDBC_XRESULTSETMETADATASUPPLIER_HPP_
92 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
93 #endif
94 #ifndef _COM_SUN_STAR_SDBC_XRESULTSETMETADATA_HPP_
95 #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
96 #endif
97 #include <com/sun/star/sdbc/ColumnValue.hpp>
98 #ifndef _COM_SUN_STAR_TASK_XINTERACTIONHANDLER_HPP_
99 #include <com/sun/star/task/XInteractionHandler.hpp>
100 #endif
101 #ifndef _COM_SUN_STAR_UCB_XCONTENT_HPP_
102 #include <com/sun/star/ucb/XContent.hpp>
103 #endif
104 #ifndef _COM_SUN_STAR_UI_DIALOGS_XEXECUTABLEDIALOG_HPP_
105 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
106 #endif
107 #ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUE_HPP_
108 #include <com/sun/star/beans/PropertyValue.hpp>
109 #endif
110 #ifndef _COM_SUN_STAR_CONTAINER_XINDEXACCESS_HPP_
111 #include <com/sun/star/container/XIndexAccess.hpp>
112 #endif
113 #ifndef _COM_SUN_STAR_CONTAINER_XNAMECONTAINER_HPP_
114 #include <com/sun/star/container/XNameContainer.hpp>
115 #endif
116 #ifndef _COM_SUN_STAR_UCB_INTERACTIVEIOEXCEPTION_HPP_
117 #include <com/sun/star/ucb/InteractiveIOException.hpp>
118 #endif
119 #ifndef _COM_SUN_STAR_SDB_XDOCUMENTDATASOURCE_HPP_
120 #include <com/sun/star/sdb/XDocumentDataSource.hpp>
121 #endif
122 #ifndef _COM_SUN_STAR_UCB_IOERRORCODE_HPP_
123 #include <com/sun/star/ucb/IOErrorCode.hpp>
124 #endif
125 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
126 #include <toolkit/helper/vclunohelper.hxx>
127 #endif
128 #ifndef _TOOLKIT_AWT_VCLXWINDOW_HXX_
129 #include <toolkit/awt/vclxwindow.hxx>
130 #endif
131 #ifndef _VCL_STDTEXT_HXX
132 #include <vcl/stdtext.hxx>
133 #endif
134 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSETINFO_HPP_
135 #include <com/sun/star/beans/XPropertySetInfo.hpp>
136 #endif
137 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
138 #include <com/sun/star/beans/XPropertySet.hpp>
139 #endif
140 #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_
141 #include <com/sun/star/container/XNameAccess.hpp>
142 #endif
143 #ifndef _COM_SUN_STAR_CONTAINER_XCONTAINER_HPP_
144 #include <com/sun/star/container/XContainer.hpp>
145 #endif
146 #ifndef _COM_SUN_STAR_CONTAINER_XHIERARCHICALNAMECONTAINER_HPP_
147 #include <com/sun/star/container/XHierarchicalNameContainer.hpp>
148 #endif
149 #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
150 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
151 #endif
152 #ifndef _COM_SUN_STAR_AWT_TEXTALIGN_HPP_
153 #include <com/sun/star/awt/TextAlign.hpp>
154 #endif
155 #ifndef _COM_SUN_STAR_AWT_FONTDESCRIPTOR_HPP_
156 #include <com/sun/star/awt/FontDescriptor.hpp>
157 #endif
158 #ifndef _COM_SUN_STAR_AWT_FONTWEIGHT_HPP_
159 #include <com/sun/star/awt/FontWeight.hpp>
160 #endif
161 #ifndef _COM_SUN_STAR_AWT_FONTRELIEF_HPP_
162 #include <com/sun/star/awt/FontRelief.hpp>
163 #endif
164 #ifndef _COM_SUN_STAR_AWT_FONTWIDTH_HPP_
165 #include <com/sun/star/awt/FontWidth.hpp>
166 #endif
167 #ifndef _COM_SUN_STAR_FRAME_XMODEL_HPP_
168 #include <com/sun/star/frame/XModel.hpp>
169 #endif
170 #ifndef DBAUI_SBATTRDLG_HRC
171 #include "dlgattr.hrc"
172 #endif
173 #ifndef DBAUI_TYPEINFO_HXX
174 #include "TypeInfo.hxx"
175 #endif
176 #ifndef DBAUI_FIELDDESCRIPTIONS_HXX
177 #include "FieldDescriptions.hxx"
178 #endif
179 #ifndef _COMPHELPER_STLTYPES_HXX_
180 #include <comphelper/stl_types.hxx>
181 #endif
182 #ifndef COMPHELPER_COMPONENTCONTEXT_HXX
183 #include <comphelper/componentcontext.hxx>
184 #endif
185 
186 #ifndef _SVX_SVXIDS_HRC
187 #include <svx/svxids.hrc>
188 #endif
189 
190 #define ITEMID_HORJUSTIFY		SID_ATTR_ALIGN_HOR_JUSTIFY
191 #define ITEMID_VERJUSTIFY		SID_ATTR_ALIGN_VER_JUSTIFY
192 //#define ITEMID_ORIENTATION     SID_ATTR_ALIGN_ORIENTATION
193 #define ITEMID_LINEBREAK		SID_ATTR_ALIGN_LINEBREAK
194 #define ITEMID_MARGIN			SID_ATTR_ALIGN_MARGIN
195 #define ITEMID_NUMBERINFO		SID_ATTR_NUMBERFORMAT_INFO
196 
197 #ifndef _SFXITEMPOOL_HXX
198 #include <svl/itempool.hxx>
199 #endif
200 #ifndef _STRING_HXX
201 #include <tools/string.hxx>
202 #endif
203 #ifndef _DBA_DBACCESS_HELPID_HRC_
204 #include "dbaccess_helpid.hrc"
205 #endif
206 #ifndef _SFXITEMSET_HXX //autogen wg. SfxItemSet
207 #include <svl/itemset.hxx>
208 #endif
209 #ifndef DBACCESS_SBA_GRID_HRC
210 #include "sbagrid.hrc"
211 #endif
212 #ifndef _SFXRNGITEM_HXX
213 #include <svl/rngitem.hxx>
214 #endif
215 #ifndef _SFXINTITEM_HXX
216 #include <svl/intitem.hxx>
217 #endif
218 #ifndef _SVX_ALGITEM_HXX
219 #include <svx/algitem.hxx>
220 #endif
221 #ifndef _SVX_NUMINF_HXX
222 #include <svx/numinf.hxx>
223 #endif
224 #define _ZFORLIST_DECLARE_TABLE
225 #ifndef _SVX_NUMINF_HXX
226 #include <svx/numinf.hxx>
227 #endif
228 #ifndef _ZFORLIST_HXX
229 #include <svl/zforlist.hxx>
230 #endif
231 #ifndef DBAUI_SBATTRDLG_HXX
232 #include "dlgattr.hxx"
233 #endif
234 #ifndef _SV_MSGBOX_HXX
235 #include <vcl/msgbox.hxx>
236 #endif
237 #ifndef _COM_SUN_STAR_CONTAINER_XCHILD_HPP_
238 #include <com/sun/star/container/XChild.hpp>
239 #endif
240 #ifndef _COM_SUN_STAR_UTIL_XNUMBERFORMATSSUPPLIER_HPP_
241 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
242 #endif
243 #ifndef _COM_SUN_STAR_UTIL_XNUMBERFORMATTER_HPP_
244 #include <com/sun/star/util/XNumberFormatter.hpp>
245 #endif
246 #ifndef _DBU_MISC_HRC_
247 #include "dbu_misc.hrc"
248 #endif
249 #ifndef _DBAUI_SQLMESSAGE_HXX_
250 #include "sqlmessage.hxx"
251 #endif
252 #ifndef _COM_SUN_STAR_UTIL_NUMBERFORMAT_HPP_
253 #include <com/sun/star/util/NumberFormat.hpp>
254 #endif
255 #ifndef _COM_SUN_STAR_UTIL_URL_HPP_
256 #include <com/sun/star/util/URL.hpp>
257 #endif
258 #ifndef _SV_TOOLBOX_HXX
259 #include <vcl/toolbox.hxx>
260 #endif
261 #ifndef _DBAUI_DLGSIZE_HXX
262 #include "dlgsize.hxx"
263 #endif
264 #ifndef _SVTOOLS_EDITBROWSEBOX_HXX_
265 #include <svtools/editbrowsebox.hxx>
266 #endif
267 #ifndef _UTL_CONFIGMGR_HXX_
268 #include <unotools/configmgr.hxx>
269 #endif
270 #ifndef INCLUDED_SVTOOLS_HELPOPT_HXX
271 #include <svtools/helpopt.hxx>
272 #endif
273 #ifndef _UCBHELPER_CONTENT_HXX
274 #include <ucbhelper/content.hxx>
275 #endif
276 #ifndef _URLOBJ_HXX
277 #include <tools/urlobj.hxx>
278 #endif
279 #ifndef TOOLS_DIAGNOSE_EX_H
280 #include <tools/diagnose_ex.h>
281 #endif
282 #ifndef _NUMUNO_HXX
283 #include <svl/numuno.hxx>
284 #endif
285 #ifndef INCLUDED_SVTOOLS_PATHOPTIONS_HXX
286 #include <unotools/pathoptions.hxx>
287 #endif
288 #ifndef SVTOOLS_FILENOTATION_HXX_
289 #include <svl/filenotation.hxx>
290 #endif
291 #ifndef _SVT_FILEVIEW_HXX
292 #include <svtools/fileview.hxx>
293 #endif
294 #ifndef _CONNECTIVITY_FILE_VALUE_HXX_
295 #include <connectivity/FValue.hxx>
296 #endif
297 
298 // .........................................................................
299 namespace dbaui
300 {
301 // .........................................................................
302 using namespace ::dbtools;
303 using namespace ::comphelper;
304 using namespace ::com::sun::star::uno;
305 using namespace ::com::sun::star::task;
306 using namespace ::com::sun::star::sdbcx;
307 using namespace ::com::sun::star::sdbc;
308 using namespace ::com::sun::star::sdb;
309 using namespace ::com::sun::star::util;
310 using namespace ::com::sun::star::ucb;
311 using namespace ::com::sun::star::beans;
312 using namespace ::com::sun::star::container;
313 using namespace ::com::sun::star::lang;
314 using namespace ::com::sun::star::ui::dialogs;
315 using namespace ::svt;
316 using ::com::sun::star::ucb::InteractiveIOException;
317 using ::com::sun::star::ucb::IOErrorCode_NO_FILE;
318 using ::com::sun::star::ucb::IOErrorCode_NOT_EXISTING;
319 using ::com::sun::star::frame::XModel;
320 
321 // -----------------------------------------------------------------------------
322 SQLExceptionInfo createConnection(	const ::rtl::OUString& _rsDataSourceName,
323 	 								const Reference< ::com::sun::star::container::XNameAccess >& _xDatabaseContext,
324 									const Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rMF,
325 									Reference< ::com::sun::star::lang::XEventListener>& _rEvtLst,
326 									Reference< ::com::sun::star::sdbc::XConnection>& _rOUTConnection )
327 {
328 	Reference<XPropertySet> xProp;
329 	try
330 	{
331 		xProp.set(_xDatabaseContext->getByName(_rsDataSourceName),UNO_QUERY);
332 	}
333 	catch(Exception&)
334 	{
335 	}
336 	SQLExceptionInfo aInfo;
337 
338 	return createConnection(xProp,_rMF,_rEvtLst,_rOUTConnection);
339 }
340 // -----------------------------------------------------------------------------
341 SQLExceptionInfo createConnection(	const Reference< ::com::sun::star::beans::XPropertySet>& _xDataSource,
342 									const Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rMF,
343 									Reference< ::com::sun::star::lang::XEventListener>& _rEvtLst,
344 									Reference< ::com::sun::star::sdbc::XConnection>& _rOUTConnection )
345 {
346 	SQLExceptionInfo aInfo;
347 	if ( !_xDataSource.is() )
348 	{
349 		OSL_ENSURE(0,"createConnection: coult not retrieve the data source!");
350 		return aInfo;
351 	}
352 
353 	::rtl::OUString sPwd, sUser;
354 	sal_Bool bPwdReq = sal_False;
355 	try
356 	{
357 		_xDataSource->getPropertyValue(PROPERTY_PASSWORD) >>= sPwd;
358 		bPwdReq = ::cppu::any2bool(_xDataSource->getPropertyValue(PROPERTY_ISPASSWORDREQUIRED));
359 		_xDataSource->getPropertyValue(PROPERTY_USER) >>= sUser;
360 	}
361 	catch(Exception&)
362 	{
363 		OSL_ENSURE(0,"createConnection: error while retrieving data source properties!");
364 	}
365 
366 
367 	try
368 	{
369 		if(bPwdReq && !sPwd.getLength())
370 		{	// password required, but empty -> connect using an interaction handler
371 			Reference<XCompletedConnection> xConnectionCompletion(_xDataSource, UNO_QUERY);
372 			if (!xConnectionCompletion.is())
373 			{
374 				OSL_ENSURE(0,"createConnection: missing an interface ... need an error message here!");
375 			}
376 			else
377 			{	// instantiate the default SDB interaction handler
378 				Reference< XInteractionHandler > xHandler(_rMF->createInstance(SERVICE_TASK_INTERACTION_HANDLER), UNO_QUERY);
379 				if (!xHandler.is())
380 				{
381 					OSL_ENSURE(sal_False, "createConnection: could not instantiate an interaction handler!");
382 					// ShowServiceNotAvailableError(NULL, String(SERVICE_TASK_INTERACTION_HANDLER), sal_True);
383 						// TODO: a real parent!
384 				}
385 				else
386 					_rOUTConnection = xConnectionCompletion->connectWithCompletion(xHandler);
387 			}
388 		}
389 		else
390 		{
391 			Reference<XDataSource> xDataSource(_xDataSource,UNO_QUERY);
392 			_rOUTConnection = xDataSource->getConnection(sUser, sPwd);
393 		}
394 		// be notified when connection is in disposing
395 		Reference< XComponent >  xComponent(_rOUTConnection, UNO_QUERY);
396 		if (xComponent.is() && _rEvtLst.is())
397 			xComponent->addEventListener(_rEvtLst);
398 	}
399 	catch(SQLContext& e) { aInfo = SQLExceptionInfo(e); }
400 	catch(SQLWarning& e) { aInfo = SQLExceptionInfo(e); }
401 	catch(SQLException& e) { aInfo = SQLExceptionInfo(e); }
402 	catch(Exception&) { OSL_ENSURE(0,"SbaTableQueryBrowser::OnExpandEntry: could not connect - unknown exception!"); }
403 
404 	//	showError(aInfo);
405 
406 	return aInfo;
407 }
408 // -----------------------------------------------------------------------------
409 Reference< XDataSource > getDataSourceByName( const ::rtl::OUString& _rDataSourceName,
410     Window* _pErrorMessageParent, Reference< XMultiServiceFactory > _rxORB, ::dbtools::SQLExceptionInfo* _pErrorInfo )
411 {
412     ::comphelper::ComponentContext aContext( _rxORB );
413     Reference< XNameAccess > xDatabaseContext( aContext.createComponent( "com.sun.star.sdb.DatabaseContext" ), UNO_QUERY_THROW );
414 
415     Reference< XDataSource > xDatasource;
416     Any aError;
417     SQLExceptionInfo aSQLError;
418 	try
419 	{
420 		xDatabaseContext->getByName( _rDataSourceName ) >>= xDatasource;
421 	}
422 	catch(const WrappedTargetException& e)
423 	{
424         InteractiveIOException aIOException;
425         if  (   ( e.TargetException >>= aIOException )
426             &&  (   ( aIOException.Code == IOErrorCode_NO_FILE )
427                 ||  ( aIOException.Code == IOErrorCode_NOT_EXISTING )
428                 )
429             )
430         {
431             String sErrorMessage = String( ModuleRes( STR_FILE_DOES_NOT_EXIST ) );
432 		    OFileNotation aTransformer( e.Message );
433 		    sErrorMessage.SearchAndReplaceAscii( "$file$", aTransformer.get( OFileNotation::N_SYSTEM ) );
434             aSQLError = SQLExceptionInfo( sErrorMessage ).get();
435         }
436         else
437         {
438             aSQLError = SQLExceptionInfo( e.TargetException );
439             if ( !aSQLError.isValid() )
440                 aError = e.TargetException;
441         }
442 	}
443     catch( const Exception& )
444     {
445         DBG_UNHANDLED_EXCEPTION();
446     }
447 
448 	if ( xDatasource.is() )
449 		return xDatasource;
450 
451     if ( aSQLError.isValid() )
452     {
453         if ( _pErrorInfo )
454         {
455             *_pErrorInfo = aSQLError;
456         }
457         else
458         {
459             showError( aSQLError, _pErrorMessageParent, _rxORB );
460         }
461     }
462 
463     return Reference<XDataSource>();
464 }
465 // -----------------------------------------------------------------------------
466 Reference< XInterface > getDataSourceOrModel(const Reference< XInterface >& _xObject)
467 {
468 	Reference< XInterface > xRet;
469 	Reference<XDocumentDataSource> xDocumentDataSource(_xObject,UNO_QUERY);
470     if ( xDocumentDataSource.is() )
471         xRet = xDocumentDataSource->getDatabaseDocument();
472 
473 	if ( !xRet.is() )
474 	{
475 		Reference<XOfficeDatabaseDocument> xOfficeDoc(_xObject,UNO_QUERY);
476 		if ( xOfficeDoc.is() )
477 			xRet = xOfficeDoc->getDataSource();
478 	}
479 
480 	return xRet;
481 }
482 // -----------------------------------------------------------------------------
483 void showError(const SQLExceptionInfo& _rInfo,Window* _pParent,const Reference< XMultiServiceFactory >& _xFactory)
484 {
485 	OSL_ENSURE(_pParent,"showError: Parent window must be NOT NULL!");
486 	::dbtools::showError(_rInfo,VCLUnoHelper::GetInterface(_pParent),_xFactory);
487 }
488 
489 // -----------------------------------------------------------------------------
490 ::std::vector< Reference<XNameAccess> > getKeyColumns(const Reference<XIndexAccess >& _rxKeys,
491 													  sal_Int32 _nKeyType)
492 {
493 	// use keys and indexes for excat postioning
494 	// first the keys
495 	::std::vector< Reference<XNameAccess> > vRet;
496 	if(_rxKeys.is())
497 	{
498 		Reference<XPropertySet> xProp;
499 		for(sal_Int32 i=0;i< _rxKeys->getCount();++i)
500 		{
501 			_rxKeys->getByIndex(i) >>= xProp;
502 			sal_Int32 nKeyType = 0;
503 			xProp->getPropertyValue(PROPERTY_TYPE) >>= nKeyType;
504 			if(_nKeyType == nKeyType)
505 			{
506 				Reference<XColumnsSupplier> xKeyColsSup(xProp,UNO_QUERY);
507 				OSL_ENSURE(xKeyColsSup.is(),"Columnsupplier is null!");
508 				vRet.push_back(xKeyColsSup->getColumns());
509 			}
510 		}
511 	}
512 
513 	return vRet;
514 }
515 // -----------------------------------------------------------------------------
516 
517 TOTypeInfoSP getTypeInfoFromType(const OTypeInfoMap& _rTypeInfo,
518 							   sal_Int32 _nType,
519 							   const ::rtl::OUString& _sTypeName,
520 							   const ::rtl::OUString& _sCreateParams,
521 							   sal_Int32 _nPrecision,
522 							   sal_Int32 _nScale,
523 							   sal_Bool _bAutoIncrement,
524 							   sal_Bool& _brForceToType)
525 {
526 	TOTypeInfoSP pTypeInfo;
527 	_brForceToType = sal_False;
528 	// search for type
529 	::std::pair<OTypeInfoMap::const_iterator, OTypeInfoMap::const_iterator> aPair = _rTypeInfo.equal_range(_nType);
530 	OTypeInfoMap::const_iterator aIter = aPair.first;
531 	if(aIter != _rTypeInfo.end()) // compare with end is correct here
532 	{
533 		for(;aIter != aPair.second;++aIter)
534 		{
535 			// search the best matching type
536 	#ifdef DBG_UTIL
537 			::rtl::OUString sDBTypeName			= aIter->second->aTypeName;         (void)sDBTypeName;
538 			sal_Int32		nDBTypePrecision	= aIter->second->nPrecision;        (void)nDBTypePrecision;
539 			sal_Int32		nDBTypeScale		= aIter->second->nMaximumScale;     (void)nDBTypeScale;
540 			sal_Bool		bDBAutoIncrement	= aIter->second->bAutoIncrement;    (void)bDBAutoIncrement;
541 	#endif
542 			if	(	(
543 						!_sTypeName.getLength()
544 					||	(aIter->second->aTypeName.equalsIgnoreAsciiCase(_sTypeName))
545 					)
546 				&&	(
547 						(
548 								!aIter->second->aCreateParams.getLength()
549 							&&	!_sCreateParams.getLength()
550 						)
551 					||	(
552 								(aIter->second->nPrecision		>= _nPrecision)
553 							&&	(aIter->second->nMaximumScale	>= _nScale)
554 							&&	( (_bAutoIncrement && aIter->second->bAutoIncrement) || !_bAutoIncrement )
555 						)
556 					)
557 				)
558 				break;
559 		}
560 
561 		if (aIter == aPair.second)
562 		{
563 			for(aIter = aPair.first; aIter != aPair.second; ++aIter)
564 			{
565 				sal_Int32 nPrec = aIter->second->nPrecision;
566 				sal_Int32 nScale = aIter->second->nMaximumScale;
567 				// search the best matching type (now comparing the local names)
568 				if	(	(aIter->second->aLocalTypeName.equalsIgnoreAsciiCase(_sTypeName))
569 					&&	(nPrec	>= _nPrecision)
570 					&&	(nScale	>= _nScale)
571 					&&	( (_bAutoIncrement && aIter->second->bAutoIncrement) || !_bAutoIncrement )
572 					)
573 				{
574 					OSL_ENSURE(sal_False,
575 						(	::rtl::OString("getTypeInfoFromType: assuming column type ")
576 						+=	::rtl::OString(aIter->second->aTypeName.getStr(), aIter->second->aTypeName.getLength(), gsl_getSystemTextEncoding())
577 						+=	::rtl::OString("\" (expected type name ")
578 						+=	::rtl::OString(_sTypeName.getStr(), _sTypeName.getLength(), gsl_getSystemTextEncoding())
579 						+=	::rtl::OString(" matches the type's local name).")).getStr());
580 					break;
581 				}
582 			}
583 		}
584 
585 		if (aIter == aPair.second)
586 		{	// no match for the names, no match for the local names
587 			// -> drop the precision and the scale restriction, accept any type with the property
588 			// type id (nType)
589 
590 			//OSL_ENSURE(sal_False,
591 			//	(	::rtl::OString("getTypeInfoFromType: did not find a matching type")
592 			//	+=	::rtl::OString(" (expected type name: ")
593 			//	+=	::rtl::OString(_sTypeName.getStr(), _sTypeName.getLength(), gsl_getSystemTextEncoding())
594 			//	+=	::rtl::OString(")! Defaulting to the first matching type.")).getStr());
595 			for(aIter = aPair.first; aIter != aPair.second; ++aIter)
596 			{
597 				// search the best matching type (now comparing the local names)
598 #ifdef DBG_UTIL
599 				::rtl::OUString sDBTypeName = aIter->second->aTypeName;
600 #endif
601 				sal_Int32 nPrec = aIter->second->nPrecision;
602 				sal_Int32 nScale = aIter->second->nMaximumScale;
603 				if	(	(nPrec	>= _nPrecision)
604 					&&	(nScale	>= _nScale)
605 					&&	( (_bAutoIncrement && aIter->second->bAutoIncrement) || !_bAutoIncrement )
606 					)
607 					break;
608 			}
609 		}
610 		if (aIter == aPair.second)
611 		{
612 			if ( _bAutoIncrement )
613 			{
614 				for(aIter = aPair.first; aIter != aPair.second; ++aIter)
615 				{
616 					// search the best matching type (now comparing the local names)
617 #ifdef DBG_UTIL
618 					::rtl::OUString sDBTypeName = aIter->second->aTypeName;
619 #endif
620 					sal_Int32 nScale = aIter->second->nMaximumScale;
621 					if	(	(nScale	>= _nScale)
622 						&&	(aIter->second->bAutoIncrement	== _bAutoIncrement)
623 						)
624 						break;
625 				}
626 				if ( aIter == aPair.second )
627 				{
628 					// try it without the auto increment flag
629 					pTypeInfo = getTypeInfoFromType(_rTypeInfo,
630 								   _nType,
631 								   _sTypeName,
632 								   _sCreateParams,
633 								   _nPrecision,
634 								   _nScale,
635 								   sal_False,
636 								   _brForceToType);
637 				}
638 				else
639 					pTypeInfo = aIter->second;
640 			}
641 			else
642 			{
643 				pTypeInfo = aPair.first->second;
644 				_brForceToType = sal_True;
645 			}
646 		}
647 		else
648 			pTypeInfo = aIter->second;
649 	}
650 	else
651 	{
652 		::comphelper::TStringMixEqualFunctor aCase(sal_False);
653 		// search for typeinfo where the typename is equal _sTypeName
654 		OTypeInfoMap::const_iterator typeInfoLoop = _rTypeInfo.begin();
655 		OTypeInfoMap::const_iterator typeInfoEnd  = _rTypeInfo.end();
656 		for (; typeInfoLoop != typeInfoEnd; ++typeInfoLoop)
657 		{
658 			if ( aCase( typeInfoLoop->second->getDBName() , _sTypeName ) )
659             {
660 			    pTypeInfo = typeInfoLoop->second;
661 				break;
662             }
663 		}
664 	}
665 
666 	OSL_ENSURE(pTypeInfo, "getTypeInfoFromType: no type info found for this type!");
667 	return pTypeInfo;
668 }
669 // -----------------------------------------------------------------------------
670 void fillTypeInfo(	const Reference< ::com::sun::star::sdbc::XConnection>& _rxConnection,
671 					const String& _rsTypeNames,
672 					OTypeInfoMap& _rTypeInfoMap,
673 					::std::vector<OTypeInfoMap::iterator>& _rTypeInfoIters)
674 {
675 	if(!_rxConnection.is())
676 		return;
677 	Reference< XResultSet> xRs = _rxConnection->getMetaData ()->getTypeInfo ();
678 	Reference< XRow> xRow(xRs,UNO_QUERY);
679 	// Information for a single SQL type
680 	if(xRs.is())
681 	{
682 		static const ::rtl::OUString aB1 = ::rtl::OUString::createFromAscii(" [ ");
683 		static const ::rtl::OUString aB2 = ::rtl::OUString::createFromAscii(" ]");
684         Reference<XResultSetMetaData> xResultSetMetaData = Reference<XResultSetMetaDataSupplier>(xRs,UNO_QUERY)->getMetaData();
685         ::connectivity::ORowSetValue aValue;
686         ::std::vector<sal_Int32> aTypes;
687         ::std::vector<sal_Bool> aNullable;
688 		// Loop on the result set until we reach end of file
689 		while (xRs->next())
690 		{
691 			TOTypeInfoSP pInfo(new OTypeInfo());
692             sal_Int32 nPos = 1;
693             if ( aTypes.empty() )
694             {
695                 sal_Int32 nCount = xResultSetMetaData->getColumnCount();
696                 if ( nCount < 1 )
697                     nCount = 18;
698                 aTypes.reserve(nCount+1);
699                 aTypes.push_back(-1);
700                 aNullable.push_back(sal_False);
701                 for (sal_Int32 j = 1; j <= nCount ; ++j)
702                 {
703                     aTypes.push_back(xResultSetMetaData->getColumnType(j));
704                     aNullable.push_back(xResultSetMetaData->isNullable(j) != ColumnValue::NO_NULLS);
705                 }
706             }
707 
708             aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
709 			pInfo->aTypeName		= aValue;
710 			++nPos;
711             aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
712 			pInfo->nType			= aValue;
713 			++nPos;
714             aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
715 			pInfo->nPrecision		= aValue;
716 			++nPos;
717             aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
718 			pInfo->aLiteralPrefix	= aValue;
719 			++nPos;
720             aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
721 			pInfo->aLiteralSuffix	= aValue;
722 			++nPos;
723             aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
724 			pInfo->aCreateParams	= aValue;
725 			++nPos;
726             aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
727 			pInfo->bNullable		= (sal_Int32)aValue == ColumnValue::NULLABLE;
728 			++nPos;
729             aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
730 			pInfo->bCaseSensitive	= (sal_Bool)aValue;
731 			++nPos;
732             aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
733 			pInfo->nSearchType		= aValue;
734 			++nPos;
735             aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
736 			pInfo->bUnsigned		= (sal_Bool)aValue;
737 			++nPos;
738             aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
739 			pInfo->bCurrency		= (sal_Bool)aValue;
740 			++nPos;
741             aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
742 			pInfo->bAutoIncrement	= (sal_Bool)aValue;
743 			++nPos;
744             aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
745 			pInfo->aLocalTypeName	= aValue;
746 			++nPos;
747             aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
748 			pInfo->nMinimumScale	= aValue;
749 			++nPos;
750             aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
751 			pInfo->nMaximumScale	= aValue;
752             nPos = 18;
753             aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
754 
755 			// check if values are less than zero like it happens in a oracle jdbc driver
756 			if( pInfo->nPrecision < 0)
757 				pInfo->nPrecision = 0;
758 			if( pInfo->nMinimumScale < 0)
759 				pInfo->nMinimumScale = 0;
760 			if( pInfo->nMaximumScale < 0)
761 				pInfo->nMaximumScale = 0;
762 
763 			String aName;
764 			switch(pInfo->nType)
765 			{
766 				case DataType::CHAR:
767 					aName = _rsTypeNames.GetToken(TYPE_CHAR);
768 					break;
769 				case DataType::VARCHAR:
770 					aName = _rsTypeNames.GetToken(TYPE_TEXT);
771 					break;
772 				case DataType::DECIMAL:
773 					aName = _rsTypeNames.GetToken(TYPE_DECIMAL);
774 					break;
775 				case DataType::NUMERIC:
776 					aName = _rsTypeNames.GetToken(TYPE_NUMERIC);
777 					break;
778 				case DataType::BIGINT:
779 					aName = _rsTypeNames.GetToken(TYPE_BIGINT);
780 					break;
781 				case DataType::FLOAT:
782 					aName = _rsTypeNames.GetToken(TYPE_FLOAT);
783 					break;
784 				case DataType::DOUBLE:
785 					aName = _rsTypeNames.GetToken(TYPE_DOUBLE);
786 					break;
787 				case DataType::LONGVARCHAR:
788 					aName = _rsTypeNames.GetToken(TYPE_MEMO);
789 					break;
790 				case DataType::LONGVARBINARY:
791 					aName = _rsTypeNames.GetToken(TYPE_IMAGE);
792 					break;
793 				case DataType::DATE:
794 					aName = _rsTypeNames.GetToken(TYPE_DATE);
795 					break;
796 				case DataType::TIME:
797 					aName = _rsTypeNames.GetToken(TYPE_TIME);
798 					break;
799 				case DataType::TIMESTAMP:
800 					aName = _rsTypeNames.GetToken(TYPE_DATETIME);
801 					break;
802 				case DataType::BIT:
803 					if ( pInfo->aCreateParams.getLength() )
804 					{
805 						aName = _rsTypeNames.GetToken(TYPE_BIT);
806 						break;
807 					}
808 					// run through
809 				case DataType::BOOLEAN:
810 					aName = _rsTypeNames.GetToken(TYPE_BOOL);
811 					break;
812 				case DataType::TINYINT:
813 					aName = _rsTypeNames.GetToken(TYPE_TINYINT);
814 					break;
815 				case DataType::SMALLINT:
816 					aName = _rsTypeNames.GetToken(TYPE_SMALLINT);
817 					break;
818 				case DataType::INTEGER:
819 					aName = _rsTypeNames.GetToken(TYPE_INTEGER);
820 					break;
821 				case DataType::REAL:
822 					aName = _rsTypeNames.GetToken(TYPE_REAL);
823 					break;
824 				case DataType::BINARY:
825 					aName = _rsTypeNames.GetToken(TYPE_BINARY);
826 					break;
827 				case DataType::VARBINARY:
828 					aName = _rsTypeNames.GetToken(TYPE_VARBINARY);
829 					break;
830 				case DataType::SQLNULL:
831 					aName = _rsTypeNames.GetToken(TYPE_SQLNULL);
832 					break;
833 				case DataType::OBJECT:
834 					aName = _rsTypeNames.GetToken(TYPE_OBJECT);
835 					break;
836 				case DataType::DISTINCT:
837 					aName = _rsTypeNames.GetToken(TYPE_DISTINCT);
838 					break;
839 				case DataType::STRUCT:
840 					aName = _rsTypeNames.GetToken(TYPE_STRUCT);
841 					break;
842 				case DataType::ARRAY:
843 					aName = _rsTypeNames.GetToken(TYPE_ARRAY);
844 					break;
845 				case DataType::BLOB:
846 					aName = _rsTypeNames.GetToken(TYPE_BLOB);
847 					break;
848 				case DataType::CLOB:
849 					aName = _rsTypeNames.GetToken(TYPE_CLOB);
850 					break;
851 				case DataType::REF:
852 					aName = _rsTypeNames.GetToken(TYPE_REF);
853 					break;
854 				case DataType::OTHER:
855 					aName = _rsTypeNames.GetToken(TYPE_OTHER);
856 					break;
857 			}
858 			if ( aName.Len() )
859 			{
860 				pInfo->aUIName = aName.GetBuffer();
861 				pInfo->aUIName += aB1;
862 			}
863 			pInfo->aUIName += pInfo->aTypeName;
864 			if ( aName.Len() )
865 				pInfo->aUIName += aB2;
866 			// Now that we have the type info, save it in the multimap
867 			_rTypeInfoMap.insert(OTypeInfoMap::value_type(pInfo->nType,pInfo));
868 		}
869 		// for a faster index access
870 		_rTypeInfoIters.reserve(_rTypeInfoMap.size());
871 
872 		OTypeInfoMap::iterator aIter = _rTypeInfoMap.begin();
873         OTypeInfoMap::iterator aEnd = _rTypeInfoMap.end();
874 		for(;aIter != aEnd;++aIter)
875 			_rTypeInfoIters.push_back(aIter);
876 
877 		// Close the result set/statement.
878 
879 		::comphelper::disposeComponent(xRs);
880 	}
881 }
882 // -----------------------------------------------------------------------------
883 void setColumnProperties(const Reference<XPropertySet>& _rxColumn,const OFieldDescription* _pFieldDesc)
884 {
885 	_rxColumn->setPropertyValue(PROPERTY_NAME,makeAny(_pFieldDesc->GetName()));
886 	_rxColumn->setPropertyValue(PROPERTY_TYPENAME,makeAny(_pFieldDesc->getTypeInfo()->aTypeName));
887 	_rxColumn->setPropertyValue(PROPERTY_TYPE,makeAny(_pFieldDesc->GetType()));
888 	_rxColumn->setPropertyValue(PROPERTY_PRECISION,makeAny(_pFieldDesc->GetPrecision()));
889 	_rxColumn->setPropertyValue(PROPERTY_SCALE,makeAny(_pFieldDesc->GetScale()));
890 	_rxColumn->setPropertyValue(PROPERTY_ISNULLABLE, makeAny(_pFieldDesc->GetIsNullable()));
891 	_rxColumn->setPropertyValue(PROPERTY_ISAUTOINCREMENT,::cppu::bool2any(_pFieldDesc->IsAutoIncrement()));
892     _rxColumn->setPropertyValue(PROPERTY_DESCRIPTION,makeAny(_pFieldDesc->GetDescription()));
893 	if ( _rxColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_ISCURRENCY) && _pFieldDesc->IsCurrency() )
894 		_rxColumn->setPropertyValue(PROPERTY_ISCURRENCY,::cppu::bool2any(_pFieldDesc->IsCurrency()));
895 	// set autoincrement value when available
896 	// and only set when the entry is not empty, that lets the value in the column untouched
897 	if ( _pFieldDesc->IsAutoIncrement() && _pFieldDesc->GetAutoIncrementValue().getLength() && _rxColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_AUTOINCREMENTCREATION) )
898 		_rxColumn->setPropertyValue(PROPERTY_AUTOINCREMENTCREATION,makeAny(_pFieldDesc->GetAutoIncrementValue()));
899 }
900 // -----------------------------------------------------------------------------
901 ::rtl::OUString createDefaultName(const Reference< XDatabaseMetaData>& _xMetaData,const Reference<XNameAccess>& _xTables,const ::rtl::OUString& _sName)
902 {
903 	OSL_ENSURE(_xMetaData.is(),"No MetaData!");
904 	::rtl::OUString sDefaultName = _sName;
905 	try
906 	{
907 		::rtl::OUString sCatalog,sSchema,sCompsedName;
908 		if(_xMetaData->supportsCatalogsInTableDefinitions())
909 		{
910 			try
911 			{
912 				Reference< XConnection> xCon = _xMetaData->getConnection();
913 				if ( xCon.is() )
914 					sCatalog = xCon->getCatalog();
915 				if ( !sCatalog.getLength() )
916 				{
917 					Reference<XResultSet> xRes = _xMetaData->getCatalogs();
918 					Reference<XRow> xRow(xRes,UNO_QUERY);
919 					while(xRes.is() && xRes->next())
920 					{
921 						sCatalog = xRow->getString(1);
922 						if(!xRow->wasNull())
923 							break;
924 					}
925 				}
926 			}
927 			catch(const SQLException&)
928 			{
929 			}
930 		}
931 		if(_xMetaData->supportsSchemasInTableDefinitions())
932 		{
933 			sSchema = _xMetaData->getUserName();
934 		}
935 		sCompsedName = ::dbtools::composeTableName( _xMetaData, sCatalog, sSchema, _sName, sal_False, ::dbtools::eInDataManipulation );
936 		sDefaultName = ::dbtools::createUniqueName(_xTables,sCompsedName);
937 	}
938 	catch(const SQLException&)
939 	{
940 	}
941 	return sDefaultName;
942 }
943 // -----------------------------------------------------------------------------
944 sal_Bool checkDataSourceAvailable(const ::rtl::OUString& _sDataSourceName,const Reference< ::com::sun::star::lang::XMultiServiceFactory >& _xORB)
945 {
946 	sal_Bool bRet = sal_False;
947 	Reference< XNameAccess > xDataBaseContext(_xORB->createInstance(SERVICE_SDB_DATABASECONTEXT), UNO_QUERY);
948 	if ( xDataBaseContext.is() )
949 	{
950 		bRet = xDataBaseContext->hasByName(_sDataSourceName);
951 		if ( !bRet )
952 		{ // try if this one is a URL
953 			try
954 			{
955 				bRet = xDataBaseContext->getByName(_sDataSourceName).hasValue();
956 			}
957 			catch(Exception)
958 			{
959 			}
960 		}
961 	}
962 	return bRet;
963 }
964 // -----------------------------------------------------------------------------
965 sal_Int32 mapTextAllign(const SvxCellHorJustify& _eAlignment)
966 {
967 	sal_Int32 nAlignment = com::sun::star::awt::TextAlign::LEFT;
968 	switch (_eAlignment)
969 	{
970 		case SVX_HOR_JUSTIFY_STANDARD:
971 		case SVX_HOR_JUSTIFY_LEFT:		nAlignment = ::com::sun::star::awt::TextAlign::LEFT;	break;
972 		case SVX_HOR_JUSTIFY_CENTER:	nAlignment = ::com::sun::star::awt::TextAlign::CENTER;	break;
973 		case SVX_HOR_JUSTIFY_RIGHT:		nAlignment = ::com::sun::star::awt::TextAlign::RIGHT;	break;
974 		default:
975 			OSL_ENSURE(0,"Invalid TextAlign!");
976 	}
977 	return nAlignment;
978 }
979 // -----------------------------------------------------------------------------
980 SvxCellHorJustify mapTextJustify(const sal_Int32& _nAlignment)
981 {
982 	SvxCellHorJustify eJustify = SVX_HOR_JUSTIFY_LEFT;
983 	switch (_nAlignment)
984 	{
985 		case ::com::sun::star::awt::TextAlign::LEFT		: eJustify = SVX_HOR_JUSTIFY_LEFT; break;
986 		case ::com::sun::star::awt::TextAlign::CENTER	: eJustify = SVX_HOR_JUSTIFY_CENTER; break;
987 		case ::com::sun::star::awt::TextAlign::RIGHT	: eJustify = SVX_HOR_JUSTIFY_RIGHT; break;
988 		default:
989 			OSL_ENSURE(0,"Invalid TextAlign!");
990 	}
991 	return eJustify;
992 }
993 // -----------------------------------------------------------------------------
994 float ConvertFontWeight( ::FontWeight eWeight )
995 {
996 	if( eWeight == WEIGHT_DONTKNOW )
997 		return ::com::sun::star::awt::FontWeight::DONTKNOW;
998 	else if( eWeight == WEIGHT_THIN )
999 		return ::com::sun::star::awt::FontWeight::THIN;
1000 	else if( eWeight == WEIGHT_ULTRALIGHT )
1001 		return ::com::sun::star::awt::FontWeight::ULTRALIGHT;
1002 	else if( eWeight == WEIGHT_LIGHT )
1003 		return ::com::sun::star::awt::FontWeight::LIGHT;
1004 	else if( eWeight == WEIGHT_SEMILIGHT )
1005 		return ::com::sun::star::awt::FontWeight::SEMILIGHT;
1006 	else if( ( eWeight == WEIGHT_NORMAL ) || ( eWeight == WEIGHT_MEDIUM ) )
1007 		return ::com::sun::star::awt::FontWeight::NORMAL;
1008 	else if( eWeight == WEIGHT_SEMIBOLD )
1009 		return ::com::sun::star::awt::FontWeight::SEMIBOLD;
1010 	else if( eWeight == WEIGHT_BOLD )
1011 		return ::com::sun::star::awt::FontWeight::BOLD;
1012 	else if( eWeight == WEIGHT_ULTRABOLD )
1013 		return ::com::sun::star::awt::FontWeight::ULTRABOLD;
1014 	else if( eWeight == WEIGHT_BLACK )
1015 		return ::com::sun::star::awt::FontWeight::BLACK;
1016 
1017 	OSL_ENSURE(0, "Unknown FontWeigth" );
1018 	return ::com::sun::star::awt::FontWeight::DONTKNOW;
1019 }
1020 // -----------------------------------------------------------------------------
1021 float ConvertFontWidth( ::FontWidth eWidth )
1022 {
1023 	if( eWidth == WIDTH_DONTKNOW )
1024 		return ::com::sun::star::awt::FontWidth::DONTKNOW;
1025 	else if( eWidth == WIDTH_ULTRA_CONDENSED )
1026 		return ::com::sun::star::awt::FontWidth::ULTRACONDENSED;
1027 	else if( eWidth == WIDTH_EXTRA_CONDENSED )
1028 		return ::com::sun::star::awt::FontWidth::EXTRACONDENSED;
1029 	else if( eWidth == WIDTH_CONDENSED )
1030 		return ::com::sun::star::awt::FontWidth::CONDENSED;
1031 	else if( eWidth == WIDTH_SEMI_CONDENSED )
1032 		return ::com::sun::star::awt::FontWidth::SEMICONDENSED;
1033 	else if( eWidth == WIDTH_NORMAL )
1034 		return ::com::sun::star::awt::FontWidth::NORMAL;
1035 	else if( eWidth == WIDTH_SEMI_EXPANDED )
1036 		return ::com::sun::star::awt::FontWidth::SEMIEXPANDED;
1037 	else if( eWidth == WIDTH_EXPANDED )
1038 		return ::com::sun::star::awt::FontWidth::EXPANDED;
1039 	else if( eWidth == WIDTH_EXTRA_EXPANDED )
1040 		return ::com::sun::star::awt::FontWidth::EXTRAEXPANDED;
1041 	else if( eWidth == WIDTH_ULTRA_EXPANDED )
1042 		return ::com::sun::star::awt::FontWidth::ULTRAEXPANDED;
1043 
1044 	OSL_ENSURE(0, "Unknown FontWidth" );
1045 	return ::com::sun::star::awt::FontWidth::DONTKNOW;
1046 }
1047 // -----------------------------------------------------------------------------
1048 ::com::sun::star::awt::FontDescriptor CreateFontDescriptor( const Font& rFont )
1049 {
1050 	::com::sun::star::awt::FontDescriptor aFD;
1051 	aFD.Name			= rFont.GetName();
1052 	aFD.StyleName		= rFont.GetStyleName();
1053 	aFD.Height			= (sal_Int16)rFont.GetSize().Height();
1054 	aFD.Width			= (sal_Int16)rFont.GetSize().Width();
1055 	aFD.Family			= sal::static_int_cast< sal_Int16 >(rFont.GetFamily());
1056 	aFD.CharSet			= rFont.GetCharSet();
1057 	aFD.Pitch			= sal::static_int_cast< sal_Int16 >(rFont.GetPitch());
1058 	aFD.CharacterWidth	= ConvertFontWidth( rFont.GetWidthType() );
1059 	aFD.Weight			= ConvertFontWeight( rFont.GetWeight() );
1060 	aFD.Slant			= (::com::sun::star::awt::FontSlant)rFont.GetItalic();
1061 	aFD.Underline		= sal::static_int_cast< sal_Int16 >(
1062         rFont.GetUnderline());
1063 	aFD.Strikeout		= sal::static_int_cast< sal_Int16 >(
1064         rFont.GetStrikeout());
1065 	aFD.Orientation		= rFont.GetOrientation();
1066 	aFD.Kerning			= rFont.IsKerning();
1067 	aFD.WordLineMode	= rFont.IsWordLineMode();
1068 	aFD.Type			= 0;   // ??? => Nur an Metric...
1069 	return aFD;
1070 }
1071 // -----------------------------------------------------------------------------
1072 void callColumnFormatDialog(const Reference<XPropertySet>& xAffectedCol,
1073 							const Reference<XPropertySet>& xField,
1074 							SvNumberFormatter* _pFormatter,
1075 							Window* _pParent)
1076 {
1077 	if (xAffectedCol.is() && xField.is())
1078 	{
1079 		try
1080 		{
1081 			Reference< XPropertySetInfo >  xInfo = xAffectedCol->getPropertySetInfo();
1082 			sal_Bool bHasFormat = xInfo->hasPropertyByName(PROPERTY_FORMATKEY);
1083 			sal_Int32 nDataType = ::comphelper::getINT32(xField->getPropertyValue(PROPERTY_TYPE));
1084 
1085 			SvxCellHorJustify eJustify(SVX_HOR_JUSTIFY_STANDARD);
1086 			Any aAlignment = xAffectedCol->getPropertyValue(PROPERTY_ALIGN);
1087 			if (aAlignment.hasValue())
1088 				eJustify = dbaui::mapTextJustify(::comphelper::getINT16(aAlignment));
1089 			sal_Int32  nFormatKey = 0;
1090 			if ( bHasFormat )
1091 				nFormatKey = ::comphelper::getINT32(xAffectedCol->getPropertyValue(PROPERTY_FORMATKEY));
1092 
1093 			sal_uInt16 nFlags = 0;
1094 			if(callColumnFormatDialog(_pParent,_pFormatter,nDataType,nFormatKey,eJustify,nFlags,bHasFormat))
1095 			{
1096 				xAffectedCol->setPropertyValue(PROPERTY_ALIGN, makeAny((sal_Int16)dbaui::mapTextAllign(eJustify)));
1097 				if (nFlags & TP_ATTR_NUMBER)
1098 					xAffectedCol->setPropertyValue(PROPERTY_FORMATKEY, makeAny(nFormatKey));
1099 
1100 			}
1101 		}
1102         catch( const Exception& )
1103         {
1104             DBG_UNHANDLED_EXCEPTION();
1105         }
1106 	}
1107 }
1108 // -----------------------------------------------------------------------------
1109 sal_Bool callColumnFormatDialog(Window* _pParent,
1110 								SvNumberFormatter* _pFormatter,
1111 								sal_Int32 _nDataType,
1112 								sal_Int32& _nFormatKey,
1113 								SvxCellHorJustify& _eJustify,
1114 								sal_uInt16&	_nFlags,
1115 								sal_Bool  _bHasFormat)
1116 {
1117 	sal_Bool bRet = sal_False;
1118 	// the allowed format changes depend of the type of the field ...
1119 	_nFlags = TP_ATTR_ALIGN;
1120 
1121 	if (_bHasFormat)
1122 		_nFlags |= TP_ATTR_NUMBER;
1123 
1124 	// ------------
1125 	// UNO->ItemSet
1126 	static SfxItemInfo aItemInfos[] =
1127 	{
1128 		{ 0, 0 },
1129 		{ SID_ATTR_NUMBERFORMAT_VALUE,		SFX_ITEM_POOLABLE },
1130 		{ SID_ATTR_ALIGN_HOR_JUSTIFY,		SFX_ITEM_POOLABLE },
1131 		{ SID_ATTR_NUMBERFORMAT_ONE_AREA,	SFX_ITEM_POOLABLE },
1132 		{ SID_ATTR_NUMBERFORMAT_INFO,		SFX_ITEM_POOLABLE }
1133 	};
1134 	static sal_uInt16 aAttrMap[] =
1135 	{
1136 		SBA_DEF_RANGEFORMAT, SBA_ATTR_ALIGN_HOR_JUSTIFY,
1137 		SID_ATTR_NUMBERFORMAT_ONE_AREA, SID_ATTR_NUMBERFORMAT_ONE_AREA,
1138 		SID_ATTR_NUMBERFORMAT_INFO, SID_ATTR_NUMBERFORMAT_INFO,
1139 		0
1140 	};
1141 
1142 	SfxPoolItem* pDefaults[] =
1143 	{
1144 		new SfxRangeItem(SBA_DEF_RANGEFORMAT, SBA_DEF_FMTVALUE, SBA_ATTR_ALIGN_HOR_JUSTIFY),
1145 		new SfxUInt32Item(SBA_DEF_FMTVALUE),
1146 		new SvxHorJustifyItem(SVX_HOR_JUSTIFY_STANDARD, SBA_ATTR_ALIGN_HOR_JUSTIFY),
1147 		new SfxBoolItem(SID_ATTR_NUMBERFORMAT_ONE_AREA, sal_False),
1148 		new SvxNumberInfoItem(SID_ATTR_NUMBERFORMAT_INFO)
1149 	};
1150 
1151 	SfxItemPool* pPool = new SfxItemPool(String::CreateFromAscii("GridBrowserProperties"), SBA_DEF_RANGEFORMAT, SBA_ATTR_ALIGN_HOR_JUSTIFY, aItemInfos, pDefaults);
1152 	pPool->SetDefaultMetric( SFX_MAPUNIT_TWIP );	// ripped, don't understand why
1153 	pPool->FreezeIdRanges();						// the same
1154 
1155 	SfxItemSet* pFormatDescriptor = new SfxItemSet(*pPool, aAttrMap);
1156 	// fill it
1157 	pFormatDescriptor->Put(SvxHorJustifyItem(_eJustify, SBA_ATTR_ALIGN_HOR_JUSTIFY));
1158 	sal_Bool bText = sal_False;
1159 	if (_bHasFormat)
1160 	{
1161 		// if the col is bound to a text field we have to disallow all non-text formats
1162 		if ((DataType::CHAR == _nDataType) || (DataType::VARCHAR == _nDataType) || (DataType::LONGVARCHAR == _nDataType) || (DataType::CLOB == _nDataType))
1163 		{
1164 			bText = sal_True;
1165 			pFormatDescriptor->Put(SfxBoolItem(SID_ATTR_NUMBERFORMAT_ONE_AREA, sal_True));
1166 			if (!_pFormatter->IsTextFormat(_nFormatKey))
1167 				// text fields can only have text formats
1168 				_nFormatKey = _pFormatter->GetStandardFormat(NUMBERFORMAT_TEXT,_pParent->GetSettings().GetLanguage());
1169 		}
1170 
1171 		pFormatDescriptor->Put(SfxUInt32Item(SBA_DEF_FMTVALUE, _nFormatKey));
1172 	}
1173 
1174 	if (!bText)
1175 	{
1176 		double dPreviewVal = 1234.56789;
1177 		SvxNumberInfoItem aFormatter(_pFormatter, dPreviewVal, SID_ATTR_NUMBERFORMAT_INFO);
1178 		pFormatDescriptor->Put(aFormatter);
1179 	}
1180 
1181 	{	// want the dialog to be destroyed before our set
1182 		SbaSbAttrDlg aDlg(_pParent, pFormatDescriptor, _pFormatter, _nFlags);
1183 		if (RET_OK == aDlg.Execute())
1184 		{
1185 			// ------------
1186 			// ItemSet->UNO
1187 			// UNO-properties
1188 			const SfxItemSet* pSet = aDlg.GetExampleSet();
1189 			// (of course we could put the modified items directly into the column, but then the UNO-model
1190 			// won't reflect these changes, and why do we have a model, then ?)
1191 
1192 			// horizontal justify
1193 			SFX_ITEMSET_GET(*pSet, pHorJustify, SvxHorJustifyItem, SBA_ATTR_ALIGN_HOR_JUSTIFY, sal_True);
1194 
1195 			_eJustify = (SvxCellHorJustify)pHorJustify->GetValue();
1196 
1197 			// format key
1198 			if (_nFlags & TP_ATTR_NUMBER)
1199 			{
1200 				SFX_ITEMSET_GET(*pSet, pFormat, SfxUInt32Item, SBA_DEF_FMTVALUE, sal_True);
1201 				_nFormatKey = (sal_Int32)pFormat->GetValue();
1202 			}
1203 			bRet = sal_True;
1204 		}
1205 			// deleted formats
1206 		const SfxItemSet* pResult = aDlg.GetOutputItemSet();
1207 		if (pResult)
1208 		{
1209 			const SfxPoolItem* pItem = pResult->GetItem( SID_ATTR_NUMBERFORMAT_INFO );
1210 			const SvxNumberInfoItem* pInfoItem = static_cast<const SvxNumberInfoItem*>(pItem);
1211 			if (pInfoItem && pInfoItem->GetDelCount())
1212 			{
1213 				const sal_uInt32* pDeletedKeys = pInfoItem->GetDelArray();
1214 
1215 				for (sal_uInt16 i=0; i< pInfoItem->GetDelCount(); ++i, ++pDeletedKeys)
1216 					_pFormatter->DeleteEntry(*pDeletedKeys);
1217 			}
1218 		}
1219 	}
1220 
1221 	delete pFormatDescriptor;
1222     SfxItemPool::Free(pPool);
1223 	for (sal_uInt16 i=0; i<sizeof(pDefaults)/sizeof(pDefaults[0]); ++i)
1224 		delete pDefaults[i];
1225 
1226 	return bRet;
1227 }
1228 
1229 //------------------------------------------------------------------------------
1230 const SfxFilter* getStandardDatabaseFilter()
1231 {
1232 	static const String s_sDatabaseType = String::CreateFromAscii("StarOffice XML (Base)");
1233 	const SfxFilter* pFilter = SfxFilter::GetFilterByName( s_sDatabaseType);
1234 	OSL_ENSURE(pFilter,"Filter: StarOffice XML (Base) could not be found!");
1235 	return pFilter;
1236 }
1237 
1238 
1239 // -----------------------------------------------------------------------------
1240 sal_Bool appendToFilter(const Reference<XConnection>& _xConnection,
1241 						const ::rtl::OUString& _sName,
1242 						const Reference< XMultiServiceFactory >& _xFactory,
1243 						Window* _pParent)
1244 {
1245 	sal_Bool bRet = sal_False;
1246 	Reference< XChild> xChild(_xConnection,UNO_QUERY);
1247 	if(xChild.is())
1248 	{
1249 		Reference< XPropertySet> xProp(xChild->getParent(),UNO_QUERY);
1250 		if(xProp.is())
1251 		{
1252 			Sequence< ::rtl::OUString > aFilter;
1253 			xProp->getPropertyValue(PROPERTY_TABLEFILTER) >>= aFilter;
1254 			// first check if we have something like SCHEMA.%
1255 			sal_Bool bHasToInsert = sal_True;
1256 			static ::rtl::OUString sPattern = ::rtl::OUString::createFromAscii("%");
1257 			const ::rtl::OUString* pBegin = aFilter.getConstArray();
1258 			const ::rtl::OUString* pEnd = pBegin + aFilter.getLength();
1259 			for (;pBegin != pEnd; ++pBegin)
1260 			{
1261 				if(pBegin->indexOf('%') != -1)
1262 				{
1263 					sal_Int32 nLen;
1264 					if((nLen = pBegin->lastIndexOf('.')) != -1 && !pBegin->compareTo(_sName,nLen))
1265 						bHasToInsert = sal_False;
1266 					else if(pBegin->getLength() == 1)
1267 						bHasToInsert = sal_False;
1268 				}
1269 			}
1270 
1271 			bRet = sal_True;
1272 			if(bHasToInsert)
1273 			{
1274 				if(! ::dbaui::checkDataSourceAvailable(::comphelper::getString(xProp->getPropertyValue(PROPERTY_NAME)),_xFactory))
1275 				{
1276 					String aMessage(ModuleRes(STR_TABLEDESIGN_DATASOURCE_DELETED));
1277 					OSQLWarningBox( _pParent, aMessage ).Execute();
1278 					bRet = sal_False;
1279 				}
1280 				else
1281 				{
1282 					aFilter.realloc(aFilter.getLength()+1);
1283 					aFilter.getArray()[aFilter.getLength()-1] = _sName;
1284 					xProp->setPropertyValue(PROPERTY_TABLEFILTER,makeAny(aFilter));
1285 				}
1286 			}
1287 		}
1288 	}
1289 	return bRet;
1290 }
1291 // -----------------------------------------------------------------------------
1292 void notifySystemWindow(Window* _pWindow,Window* _pToRegister, ::comphelper::mem_fun1_t<TaskPaneList,Window*> _rMemFunc)
1293 {
1294 	OSL_ENSURE(_pWindow,"Window can not be null!");
1295     SystemWindow* pSystemWindow = _pWindow ? _pWindow->GetSystemWindow() : NULL;
1296 	if ( pSystemWindow )
1297 	{
1298 		_rMemFunc( pSystemWindow->GetTaskPaneList(), _pToRegister );
1299 	}
1300 }
1301 // -----------------------------------------------------------------------------
1302 void adjustToolBoxSize(ToolBox* _pToolBox)
1303 {
1304 	// adjust the toolbox size, otherwise large bitmaps don't fit into
1305 	Size aOldSize = _pToolBox->GetSizePixel();
1306 	Size aSize = _pToolBox->CalcWindowSizePixel();
1307 	if ( !aSize.Width() )
1308 		aSize.Width() = aOldSize.Width();
1309 	else if ( !aSize.Height() )
1310 		aSize.Height() = aOldSize.Height();
1311 
1312 	Size aTbSize = _pToolBox->GetSizePixel();
1313 	if ( (aSize.Width() && aSize.Width() != aTbSize.Width()) ||
1314 			(aSize.Height() && aSize.Height() != aTbSize.Height()) )
1315 	{
1316 		_pToolBox->SetPosSizePixel( _pToolBox->GetPosPixel(), aSize );
1317 		_pToolBox->Invalidate();
1318 	}
1319 }
1320 // -----------------------------------------------------------------------------
1321 sal_Bool isHiContrast(Window* _pWindow)
1322 {
1323 	OSL_ENSURE(_pWindow,"Window must be not null!");
1324 	Window* pIter = _pWindow;
1325 	//	while( pIter &&  pIter->GetBackground().GetColor().GetColor() == COL_TRANSPARENT )
1326 	while( pIter )
1327 	{
1328 		if ( pIter->GetBackground().GetColor().GetColor() == COL_TRANSPARENT )
1329 			pIter = pIter->GetParent();
1330 		else
1331 			break;
1332 	}
1333 	return pIter && pIter->GetSettings().GetStyleSettings().GetHighContrastMode();
1334 }
1335 
1336 // -----------------------------------------------------------------------------
1337 void adjustBrowseBoxColumnWidth( ::svt::EditBrowseBox* _pBox, sal_uInt16 _nColId )
1338 {
1339 	sal_Int32 nColSize = -1;
1340 	sal_uInt32 nDefaultWidth = _pBox->GetDefaultColumnWidth( _pBox->GetColumnTitle( _nColId ) );
1341 	if ( nDefaultWidth != _pBox->GetColumnWidth( _nColId ) )
1342 	{
1343 		Size aSizeMM = _pBox->PixelToLogic( Size( _pBox->GetColumnWidth( _nColId ), 0 ), MapMode( MAP_MM ) );
1344 		nColSize = aSizeMM.Width() * 10;
1345 	}
1346 
1347 	Size aDefaultMM = _pBox->PixelToLogic( Size( nDefaultWidth, 0 ), MapMode( MAP_MM ) );
1348 
1349 	DlgSize aColumnSizeDlg( _pBox, nColSize, sal_False, aDefaultMM.Width() * 10 );
1350 	if ( aColumnSizeDlg.Execute() )
1351 	{
1352 		sal_Int32 nValue = aColumnSizeDlg.GetValue();
1353 		if ( -1 == nValue )
1354 		{	// default width
1355 			nValue = _pBox->GetDefaultColumnWidth( _pBox->GetColumnTitle( _nColId ) );
1356 		}
1357 		else
1358 		{
1359 			Size aSizeMM( nValue / 10, 0 );
1360 			nValue = _pBox->LogicToPixel( aSizeMM, MapMode( MAP_MM ) ).Width();
1361 		}
1362 		_pBox->SetColumnWidth( _nColId, nValue );
1363 	}
1364 }
1365 // -----------------------------------------------------------------------------
1366 // check if SQL92 name checking is enabled
1367 sal_Bool isSQL92CheckEnabled(const Reference<XConnection>& _xConnection)
1368 {
1369 	return ::dbtools::getBooleanDataSourceSetting( _xConnection, PROPERTY_ENABLESQL92CHECK );
1370 }
1371 // -----------------------------------------------------------------------------
1372 sal_Bool isAppendTableAliasEnabled(const Reference<XConnection>& _xConnection)
1373 {
1374 	return ::dbtools::getBooleanDataSourceSetting( _xConnection, INFO_APPEND_TABLE_ALIAS );
1375 }
1376 
1377 // -----------------------------------------------------------------------------
1378 sal_Bool generateAsBeforeTableAlias(const Reference<XConnection>& _xConnection)
1379 {
1380 	return ::dbtools::getBooleanDataSourceSetting( _xConnection, INFO_AS_BEFORE_CORRELATION_NAME );
1381 }
1382 
1383 // -----------------------------------------------------------------------------
1384 void fillAutoIncrementValue(const Reference<XPropertySet>& _xDatasource,
1385 							sal_Bool& _rAutoIncrementValueEnabled,
1386 							::rtl::OUString& _rsAutoIncrementValue)
1387 {
1388 	if ( _xDatasource.is() )
1389 	{
1390 		OSL_ENSURE(_xDatasource->getPropertySetInfo()->hasPropertyByName(PROPERTY_INFO),"NO datasource supplied!");
1391 		Sequence<PropertyValue> aInfo;
1392 		_xDatasource->getPropertyValue(PROPERTY_INFO) >>= aInfo;
1393 
1394 		// search the right propertyvalue
1395 		const PropertyValue* pValue =::std::find_if(aInfo.getConstArray(),
1396 													aInfo.getConstArray() + aInfo.getLength(),
1397 													::std::bind2nd(TPropertyValueEqualFunctor(),PROPERTY_AUTOINCREMENTCREATION));
1398 		if ( pValue && pValue != (aInfo.getConstArray() + aInfo.getLength()) )
1399 			pValue->Value >>= _rsAutoIncrementValue;
1400 		pValue =::std::find_if(aInfo.getConstArray(),
1401 													aInfo.getConstArray() + aInfo.getLength(),
1402 													::std::bind2nd(TPropertyValueEqualFunctor(),::rtl::OUString::createFromAscii("IsAutoRetrievingEnabled") ));
1403 		if ( pValue && pValue != (aInfo.getConstArray() + aInfo.getLength()) )
1404 			pValue->Value >>= _rAutoIncrementValueEnabled;
1405 	}
1406 }
1407 // -----------------------------------------------------------------------------
1408 void fillAutoIncrementValue(const Reference<XConnection>& _xConnection,
1409 							sal_Bool& _rAutoIncrementValueEnabled,
1410 							::rtl::OUString& _rsAutoIncrementValue)
1411 {
1412 	Reference< XChild> xChild(_xConnection,UNO_QUERY);
1413 	if(xChild.is())
1414 	{
1415 		Reference< XPropertySet> xProp(xChild->getParent(),UNO_QUERY);
1416 		fillAutoIncrementValue(xProp,_rAutoIncrementValueEnabled,_rsAutoIncrementValue);
1417 	}
1418 }
1419 // -----------------------------------------------------------------------------
1420 ::rtl::OUString getStrippedDatabaseName(const Reference<XPropertySet>& _xDataSource,::rtl::OUString& _rsDatabaseName)
1421 {
1422 	if ( !_rsDatabaseName.getLength() && _xDataSource.is() )
1423 	{
1424 		try
1425 		{
1426 			_xDataSource->getPropertyValue(PROPERTY_NAME) >>= _rsDatabaseName;
1427 		}
1428 		catch(const Exception& )
1429 		{
1430             DBG_UNHANDLED_EXCEPTION();
1431 		}
1432 	}
1433 	::rtl::OUString sName = _rsDatabaseName;
1434 	INetURLObject aURL(sName);
1435 	if ( aURL.GetProtocol() != INET_PROT_NOT_VALID )
1436 		sName = aURL.getBase(INetURLObject::LAST_SEGMENT,true,INetURLObject::DECODE_UNAMBIGUOUS);
1437 	return sName;
1438 }
1439 // -----------------------------------------------------------------------------
1440 	void AppendConfigToken( ::rtl::OUString& _rURL, sal_Bool _bQuestionMark )
1441 	{
1442 		Any aLocale =
1443 			::utl::ConfigManager::GetConfigManager()->GetDirectConfigProperty( ::utl::ConfigManager::LOCALE );
1444 		::rtl::OUString sLocaleStr;
1445 		if ( !( aLocale >>= sLocaleStr ) )
1446 			// fallback is english
1447 			sLocaleStr = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("en"));
1448 
1449 		// query part exists?
1450 		if ( _bQuestionMark )
1451 			// no, so start with '?'
1452 			_rURL += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("?"));
1453 		else
1454 			// yes, so only append with '&'
1455 			_rURL += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("&"));
1456 
1457 		// set parameters
1458 		_rURL += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Language="));
1459 		_rURL += sLocaleStr;
1460 		_rURL += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("&System="));
1461 		_rURL += SvtHelpOptions().GetSystem();
1462 	}
1463 
1464 namespace
1465 {
1466 	// -----------------------------------------------------------------------
1467 
1468 	sal_Bool GetHelpAnchor_Impl( const ::rtl::OUString& _rURL, ::rtl::OUString& _rAnchor )
1469 	{
1470 		sal_Bool bRet = sal_False;
1471 		::rtl::OUString sAnchor;
1472 
1473 		try
1474 		{
1475 			::ucbhelper::Content aCnt( INetURLObject( _rURL ).GetMainURL( INetURLObject::NO_DECODE ),
1476 								 Reference< ::com::sun::star::ucb::XCommandEnvironment > () );
1477 			if ( ( aCnt.getPropertyValue( ::rtl::OUString::createFromAscii( "AnchorName" ) ) >>= sAnchor ) )
1478 			{
1479 
1480 				if ( sAnchor.getLength() > 0 )
1481 				{
1482 					_rAnchor = sAnchor;
1483 					bRet = sal_True;
1484 				}
1485 			}
1486 			else
1487 			{
1488 				DBG_ERRORFILE( "Property 'AnchorName' is missing" );
1489 			}
1490 		}
1491 		catch( Exception& )
1492 		{
1493 		}
1494 
1495 		return bRet;
1496 	}
1497 // .........................................................................
1498 } // annonymous
1499 // .........................................................................
1500 // -----------------------------------------------------------------------------
1501 ::com::sun::star::util::URL	createHelpAgentURL(const ::rtl::OUString& _sModuleName, const rtl::OString& sHelpId)
1502 {
1503 	::com::sun::star::util::URL aURL;
1504 	aURL.Complete = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "vnd.sun.star.help://" ) );
1505 	aURL.Complete += _sModuleName;
1506 	aURL.Complete += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) );
1507 	aURL.Complete += ::rtl::OUString(sHelpId, sHelpId.getLength(), RTL_TEXTENCODING_UTF8);
1508 
1509 	::rtl::OUString sAnchor;
1510 	::rtl::OUString sTempURL = aURL.Complete;
1511 	AppendConfigToken( sTempURL, sal_True );
1512 	sal_Bool bHasAnchor = GetHelpAnchor_Impl( sTempURL, sAnchor );
1513 	AppendConfigToken(aURL.Complete,sal_True);
1514 	if ( bHasAnchor )
1515 	{
1516 		aURL.Complete += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("#"));
1517 		aURL.Complete += sAnchor;
1518 	}
1519 	return aURL;
1520 }
1521 // -----------------------------------------------------------------------------
1522 void setEvalDateFormatForFormatter(Reference< ::com::sun::star::util::XNumberFormatter >& _rxFormatter)
1523 {
1524 	OSL_ENSURE( _rxFormatter.is(),"setEvalDateFormatForFormatter: Formatter is NULL!");
1525 	if ( _rxFormatter.is() )
1526 	{
1527 		Reference< ::com::sun::star::util::XNumberFormatsSupplier >  xSupplier = _rxFormatter->getNumberFormatsSupplier();
1528 
1529 		Reference< XUnoTunnel > xTunnel(xSupplier,UNO_QUERY);
1530 		SvNumberFormatsSupplierObj* pSupplierImpl = reinterpret_cast<SvNumberFormatsSupplierObj*>(xTunnel->getSomething(SvNumberFormatsSupplierObj::getUnoTunnelId()));
1531 		OSL_ENSURE(pSupplierImpl,"No Supplier!");
1532 
1533 		if ( pSupplierImpl )
1534 		{
1535 			SvNumberFormatter* pFormatter = pSupplierImpl->GetNumberFormatter();
1536 			pFormatter->SetEvalDateFormat(NF_EVALDATEFORMAT_FORMAT);
1537 		}
1538 	}
1539 }
1540 // -----------------------------------------------------------------------------
1541 TOTypeInfoSP queryPrimaryKeyType(const OTypeInfoMap& _rTypeInfo)
1542 {
1543 	TOTypeInfoSP pTypeInfo;
1544 	// first we search for a type which supports autoIncrement
1545 	OTypeInfoMap::const_iterator aIter = _rTypeInfo.begin();
1546 	OTypeInfoMap::const_iterator aEnd  = _rTypeInfo.end();
1547 	for(;aIter != aEnd;++aIter)
1548 	{
1549 		// OJ: we don't want to set an autoincrement column to be key
1550 		// because we don't have the possiblity to know how to create
1551 		// such auto increment column later on
1552 		// so until we know how to do it, we create a column without autoincrement
1553 		//	if ( !aIter->second->bAutoIncrement )
1554 		{	// therefor we have searched
1555 			if ( aIter->second->nType == DataType::INTEGER )
1556 			{
1557 				pTypeInfo = aIter->second; // alternative
1558 				break;
1559 			}
1560 			else if ( !pTypeInfo.get() && aIter->second->nType == DataType::DOUBLE )
1561 				pTypeInfo = aIter->second; // alternative
1562 			else if ( !pTypeInfo.get() && aIter->second->nType == DataType::REAL )
1563 				pTypeInfo = aIter->second; // alternative
1564 		}
1565 	}
1566 	if ( !pTypeInfo.get() ) // just a fallback
1567 		pTypeInfo = queryTypeInfoByType(DataType::VARCHAR,_rTypeInfo);
1568 
1569 	OSL_ENSURE(pTypeInfo.get(),"checkColumns: cann't find a type which is useable as a key!");
1570 	return pTypeInfo;
1571 }
1572 // -----------------------------------------------------------------------------
1573 TOTypeInfoSP queryTypeInfoByType(sal_Int32 _nDataType,const OTypeInfoMap& _rTypeInfo)
1574 {
1575 	OTypeInfoMap::const_iterator aIter = _rTypeInfo.find(_nDataType);
1576 	if(aIter != _rTypeInfo.end())
1577 		return aIter->second;
1578 	// fall back if the type is unknown
1579 	TOTypeInfoSP pTypeInfo;
1580 	switch(_nDataType)
1581 	{
1582 		case DataType::TINYINT:
1583 			if( pTypeInfo = queryTypeInfoByType(DataType::SMALLINT,_rTypeInfo))
1584 				break;
1585 			// run through
1586 		case DataType::SMALLINT:
1587 			if( pTypeInfo = queryTypeInfoByType(DataType::INTEGER,_rTypeInfo))
1588 				break;
1589 			// run through
1590 		case DataType::INTEGER:
1591 			if( pTypeInfo = queryTypeInfoByType(DataType::FLOAT,_rTypeInfo))
1592 				break;
1593 			// run through
1594 		case DataType::FLOAT:
1595 			if( pTypeInfo = queryTypeInfoByType(DataType::REAL,_rTypeInfo))
1596 				break;
1597 			// run through
1598 		case DataType::DATE:
1599 		case DataType::TIME:
1600 			if( DataType::DATE == _nDataType || DataType::TIME == _nDataType )
1601 			{
1602 				if( pTypeInfo = queryTypeInfoByType(DataType::TIMESTAMP,_rTypeInfo))
1603 					break;
1604 			}
1605 			// run through
1606 		case DataType::TIMESTAMP:
1607 		case DataType::REAL:
1608 		case DataType::BIGINT:
1609 			if (  pTypeInfo = queryTypeInfoByType(DataType::DOUBLE,_rTypeInfo) )
1610 				break;
1611 			// run through
1612 		case DataType::DOUBLE:
1613 			if (  pTypeInfo = queryTypeInfoByType(DataType::NUMERIC,_rTypeInfo) )
1614 				break;
1615 			// run through
1616 		case DataType::NUMERIC:
1617 			 pTypeInfo = queryTypeInfoByType(DataType::DECIMAL,_rTypeInfo);
1618 			break;
1619 		case DataType::DECIMAL:
1620 			if (  pTypeInfo = queryTypeInfoByType(DataType::NUMERIC,_rTypeInfo) )
1621 				break;
1622 			if (  pTypeInfo = queryTypeInfoByType(DataType::DOUBLE,_rTypeInfo) )
1623 				break;
1624 			break;
1625 		case DataType::VARCHAR:
1626 			if (  pTypeInfo = queryTypeInfoByType(DataType::LONGVARCHAR,_rTypeInfo) )
1627 				break;
1628 			break;
1629 		case DataType::LONGVARCHAR:
1630 			if (  pTypeInfo = queryTypeInfoByType(DataType::CLOB,_rTypeInfo) )
1631 				break;
1632 			break;
1633 		default:
1634 			;
1635 	} // switch(_nDataType)
1636 	if ( !pTypeInfo )
1637 	{
1638 		::rtl::OUString sCreate(RTL_CONSTASCII_USTRINGPARAM("x")),sTypeName;
1639 		sal_Bool bForce = sal_True;
1640 		pTypeInfo = ::dbaui::getTypeInfoFromType(_rTypeInfo,DataType::VARCHAR,sTypeName,sCreate,50,0,sal_False,bForce);
1641 	} // if ( !pTypeInfo )
1642 	OSL_ENSURE(pTypeInfo,"Wrong DataType supplied!");
1643 	return pTypeInfo;
1644 }
1645 // -----------------------------------------------------------------------------
1646 sal_Int32 askForUserAction(Window* _pParent,sal_uInt16 _nTitle,sal_uInt16 _nText,sal_Bool _bAll,const ::rtl::OUString& _sName)
1647 {
1648 	vos::OGuard aGuard( Application::GetSolarMutex() );
1649 	String aMsg = String(ModuleRes(_nText));
1650 	aMsg.SearchAndReplace(String::CreateFromAscii("%1"),String(_sName));
1651 	OSQLMessageBox aAsk(_pParent,String(ModuleRes(_nTitle )),aMsg,WB_YES_NO | WB_DEF_YES,OSQLMessageBox::Query);
1652 	if ( _bAll )
1653 	{
1654 		aAsk.AddButton(String(ModuleRes(STR_BUTTON_TEXT_ALL)), RET_ALL, 0);
1655 		aAsk.GetPushButton(RET_ALL)->SetHelpId(HID_CONFIRM_DROP_BUTTON_ALL);
1656 	}
1657 	return aAsk.Execute();
1658 }
1659 
1660 // -----------------------------------------------------------------------------
1661 namespace
1662 {
1663     static ::rtl::OUString lcl_createSDBCLevelStatement( const ::rtl::OUString& _rStatement, const Reference< XConnection >& _rxConnection )
1664     {
1665         ::rtl::OUString sSDBCLevelStatement( _rStatement );
1666         try
1667         {
1668             Reference< XMultiServiceFactory > xAnalyzerFactory( _rxConnection, UNO_QUERY_THROW );
1669             Reference< XSingleSelectQueryAnalyzer > xAnalyzer( xAnalyzerFactory->createInstance( SERVICE_NAME_SINGLESELECTQUERYCOMPOSER ), UNO_QUERY_THROW );
1670             xAnalyzer->setQuery( _rStatement );
1671             sSDBCLevelStatement = xAnalyzer->getQueryWithSubstitution();
1672         }
1673         catch( const Exception& )
1674         {
1675             DBG_UNHANDLED_EXCEPTION();
1676         }
1677         return sSDBCLevelStatement;
1678     }
1679 }
1680 
1681 // -----------------------------------------------------------------------------
1682 Reference< XPropertySet > createView( const ::rtl::OUString& _rName, const Reference< XConnection >& _rxConnection,
1683                                     const ::rtl::OUString& _rCommand )
1684 {
1685 	Reference<XViewsSupplier> xSup(_rxConnection,UNO_QUERY);
1686 	Reference< XNameAccess > xViews;
1687 	if(xSup.is())
1688 		xViews = xSup->getViews();
1689 	Reference<XDataDescriptorFactory> xFact(xViews,UNO_QUERY);
1690 	OSL_ENSURE(xFact.is(),"No XDataDescriptorFactory available!");
1691 	if(!xFact.is())
1692 		return NULL;
1693 
1694 	Reference<XPropertySet> xView = xFact->createDataDescriptor();
1695 	if ( !xView.is() )
1696 		return NULL;
1697 
1698 	::rtl::OUString sCatalog,sSchema,sTable;
1699 	::dbtools::qualifiedNameComponents(_rxConnection->getMetaData(),
1700 										_rName,
1701 										sCatalog,
1702 										sSchema,
1703 										sTable,
1704 										::dbtools::eInDataManipulation);
1705 
1706 	xView->setPropertyValue(PROPERTY_CATALOGNAME,makeAny(sCatalog));
1707 	xView->setPropertyValue(PROPERTY_SCHEMANAME,makeAny(sSchema));
1708 	xView->setPropertyValue(PROPERTY_NAME,makeAny(sTable));
1709 
1710 	xView->setPropertyValue( PROPERTY_COMMAND, makeAny( _rCommand ) );
1711 
1712 	Reference<XAppend> xAppend(xViews,UNO_QUERY);
1713 	if(xAppend.is())
1714 		xAppend->appendByDescriptor(xView);
1715 
1716 	xView = NULL;
1717 	// we need to reget the view because after appending it it is no longer valid
1718 	// but this time it isn't a view object it is a table object with type "VIEW"
1719 	Reference<XTablesSupplier> xTabSup(_rxConnection,UNO_QUERY);
1720 	Reference< XNameAccess > xTables;
1721 	if ( xTabSup.is() )
1722 	{
1723 		xTables = xTabSup->getTables();
1724 		if ( xTables.is() && xTables->hasByName( _rName ) )
1725 			xTables->getByName( _rName ) >>= xView;
1726 	}
1727 
1728 	return xView;
1729 }
1730 
1731 // -----------------------------------------------------------------------------
1732 Reference<XPropertySet> createView( const ::rtl::OUString& _rName, const Reference< XConnection >& _rxConnection
1733 								   ,const Reference<XPropertySet>& _rxSourceObject)
1734 {
1735 	::rtl::OUString sCommand;
1736     Reference< XPropertySetInfo > xPSI( _rxSourceObject->getPropertySetInfo(), UNO_SET_THROW );
1737 	if ( xPSI->hasPropertyByName( PROPERTY_COMMAND ) )
1738 	{
1739 		_rxSourceObject->getPropertyValue( PROPERTY_COMMAND ) >>= sCommand;
1740 
1741         sal_Bool bEscapeProcessing( sal_False );
1742         OSL_VERIFY( _rxSourceObject->getPropertyValue( PROPERTY_ESCAPE_PROCESSING ) >>= bEscapeProcessing );
1743         if ( bEscapeProcessing )
1744             sCommand = lcl_createSDBCLevelStatement( sCommand, _rxConnection );
1745 	}
1746 	else
1747 	{
1748 		sCommand = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SELECT * FROM " ) );
1749 		sCommand += composeTableNameForSelect( _rxConnection, _rxSourceObject );
1750 	}
1751     return createView( _rName, _rxConnection, sCommand );
1752 }
1753 
1754 // -----------------------------------------------------------------------------
1755 sal_Bool insertHierachyElement( Window* _pParent, const Reference< XMultiServiceFactory >& _rxORB,
1756 						   const Reference<XHierarchicalNameContainer>& _xNames,
1757 						   const String& _sParentFolder,
1758 						   sal_Bool _bForm,
1759 						   sal_Bool _bCollection,
1760 						   const Reference<XContent>& _xContent,
1761 						   sal_Bool _bMove)
1762 {
1763     OSL_ENSURE( _xNames.is(), "insertHierachyElement: illegal name container!" );
1764 	if ( !_xNames.is() )
1765         return sal_False;
1766 
1767     Reference<XNameAccess> xNameAccess( _xNames, UNO_QUERY );
1768 	::rtl::OUString sName = _sParentFolder;
1769 	if ( _xNames->hasByHierarchicalName(sName) )
1770 	{
1771 		Reference<XChild> xChild(_xNames->getByHierarchicalName(sName),UNO_QUERY);
1772 		xNameAccess.set(xChild,UNO_QUERY);
1773 		if ( !xNameAccess.is() && xChild.is() )
1774 			xNameAccess.set(xChild->getParent(),UNO_QUERY);
1775 	}
1776 
1777     OSL_ENSURE( xNameAccess.is(), "insertHierachyElement: could not find the proper name container!" );
1778 	if ( !xNameAccess.is() )
1779         return sal_False;
1780 
1781     ::rtl::OUString sNewName;
1782 	Reference<XPropertySet> xProp(_xContent,UNO_QUERY);
1783 	if ( xProp.is() )
1784 		xProp->getPropertyValue(PROPERTY_NAME) >>= sNewName;
1785 
1786 	if ( !_bMove || !sNewName.getLength() )
1787 	{
1788 		String sTargetName,sLabel;
1789 		if ( !sNewName.getLength() || xNameAccess->hasByName(sNewName) )
1790 		{
1791 			if ( sNewName.getLength() )
1792 				sTargetName = sNewName;
1793 			else
1794 				sTargetName = String(ModuleRes( _bCollection ? STR_NEW_FOLDER : ((_bForm) ? RID_STR_FORM : RID_STR_REPORT)));
1795 			sLabel = String(ModuleRes( _bCollection ? STR_FOLDER_LABEL  : ((_bForm) ? STR_FRM_LABEL : STR_RPT_LABEL)));
1796 			sTargetName = ::dbtools::createUniqueName(xNameAccess,sTargetName);
1797 
1798 
1799 			// here we have everything needed to create a new query object ...
1800             HierarchicalNameCheck aNameChecker( _xNames.get(), sName );
1801 			// ... ehm, except a new name
1802 			OSaveAsDlg aAskForName(	_pParent,
1803                                     _rxORB,
1804 									sTargetName,
1805 									sLabel,
1806 									aNameChecker,
1807 									SAD_ADDITIONAL_DESCRIPTION | SAD_TITLE_PASTE_AS);
1808 			if ( RET_OK != aAskForName.Execute() )
1809 				// cancelled by the user
1810 				return sal_False;
1811 
1812 			sNewName = aAskForName.getName();
1813 		}
1814 	}
1815 	else if ( xNameAccess->hasByName(sNewName) )
1816 	{
1817 		String sError(ModuleRes(STR_NAME_ALREADY_EXISTS));
1818 		sError.SearchAndReplaceAscii("#",sNewName);
1819 		throw SQLException(sError,NULL,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("S1000")) ,0,Any());
1820 	}
1821 
1822     try
1823 	{
1824 		Reference<XMultiServiceFactory> xORB( xNameAccess, UNO_QUERY_THROW );
1825 		Sequence< Any > aArguments(3);
1826 		PropertyValue aValue;
1827 		// set as folder
1828 		aValue.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Name"));
1829 		aValue.Value <<= sNewName;
1830 		aArguments[0] <<= aValue;
1831 		//parent
1832 		aValue.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Parent"));
1833 		aValue.Value <<= xNameAccess;
1834 		aArguments[1] <<= aValue;
1835 
1836 		aValue.Name = PROPERTY_EMBEDDEDOBJECT;
1837 		aValue.Value <<= _xContent;
1838 		aArguments[2] <<= aValue;
1839 
1840 		::rtl::OUString sServiceName(_bCollection ? ((_bForm) ? SERVICE_NAME_FORM_COLLECTION : SERVICE_NAME_REPORT_COLLECTION) : SERVICE_SDB_DOCUMENTDEFINITION);
1841 
1842 		Reference<XContent > xNew( xORB->createInstanceWithArguments( sServiceName, aArguments ), UNO_QUERY_THROW );
1843 		Reference< XNameContainer > xNameContainer( xNameAccess, UNO_QUERY_THROW );
1844 		xNameContainer->insertByName( sNewName, makeAny( xNew ) );
1845 	}
1846     catch( const IllegalArgumentException& e )
1847     {
1848         ::dbtools::throwGenericSQLException( e.Message, e.Context );
1849     }
1850 	catch( const Exception& )
1851 	{
1852         DBG_UNHANDLED_EXCEPTION();
1853         return sal_False;
1854 	}
1855 
1856     return sal_True;
1857 }
1858 // -----------------------------------------------------------------------------
1859 Reference< XNumberFormatter > getNumberFormatter(const Reference< XConnection >& _rxConnection,const Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rMF )
1860 {
1861 	// ---------------------------------------------------------------
1862 	// create a formatter working with the connections format supplier
1863 	Reference< XNumberFormatter > xFormatter;
1864 
1865 	try
1866 	{
1867 		Reference< ::com::sun::star::util::XNumberFormatsSupplier >  xSupplier(::dbtools::getNumberFormats(_rxConnection, sal_True,_rMF));
1868 
1869 		if ( xSupplier.is() )
1870 		{
1871 			// create a new formatter
1872 			xFormatter = Reference< ::com::sun::star::util::XNumberFormatter > (
1873 				_rMF->createInstance(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.util.NumberFormatter"))), UNO_QUERY);
1874 			if ( xFormatter.is() )
1875 				xFormatter->attachNumberFormatsSupplier(xSupplier);
1876 		}
1877 	}
1878 	catch(const Exception&)
1879 	{
1880         DBG_UNHANDLED_EXCEPTION();
1881 	}
1882 	return xFormatter;
1883 }
1884 
1885 
1886 // .........................................................................
1887 } // dbaui
1888 // .........................................................................
1889