xref: /trunk/main/dbaccess/source/ui/dlg/dlgsave.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_DLGSAVE_HXX
32 #include "dlgsave.hxx"
33 #endif
34 #ifndef DBAUI_DLGSAVE_HRC
35 #include "dlgsave.hrc"
36 #endif
37 #ifndef _SV_MSGBOX_HXX //autogen
38 #include <vcl/msgbox.hxx>
39 #endif
40 #ifndef _DBU_DLG_HRC_
41 #include "dbu_dlg.hrc"
42 #endif
43 #ifndef _COM_SUN_STAR_SDB_COMMANDTYPE_HPP_
44 #include <com/sun/star/sdb/CommandType.hpp>
45 #endif
46 #ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
47 #include <com/sun/star/sdbc/XRow.hpp>
48 #endif
49 #ifndef _DBAUI_SQLMESSAGE_HXX_
50 #include "sqlmessage.hxx"
51 #endif
52 #ifndef _CONNECTIVITY_DBTOOLS_HXX_
53 #include <connectivity/dbtools.hxx>
54 #endif
55 #ifndef DBAUI_TOOLS_HXX
56 #include "UITools.hxx"
57 #endif
58 #ifndef _DBA_DBACCESS_HELPID_HRC_
59 #include "dbaccess_helpid.hrc"
60 #endif
61 #ifndef DBAUI_SQLNAMEEDIT_HXX
62 #include "SqlNameEdit.hxx"
63 #endif
64 #ifndef _BUTTON_HXX //autogen
65 #include <vcl/button.hxx>
66 #endif
67 #ifndef _FIXED_HXX //autogen
68 #include <vcl/fixed.hxx>
69 #endif
70 #ifndef _EDIT_HXX //autogen
71 #include <vcl/edit.hxx>
72 #endif
73 #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_
74 #include <com/sun/star/container/XNameAccess.hpp>
75 #endif
76 #ifndef _COM_SUN_STAR_CONTAINER_XHIERARCHICALNAMEACCESS_HPP_
77 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
78 #endif
79 #ifndef _COM_SUN_STAR_SDBC_XDATABASEMETADATA_HPP_
80 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
81 #endif
82 #ifndef _DBAUI_MODULE_DBU_HXX_
83 #include "moduledbu.hxx"
84 #endif
85 #ifndef DBACCESS_SOURCE_UI_INC_OBJECTNAMECHECK_HXX
86 #include "objectnamecheck.hxx"
87 #endif
88 #ifndef TOOLS_DIAGNOSE_EX_H
89 #include <tools/diagnose_ex.h>
90 #endif
91 
92 
93 using namespace dbaui;
94 using namespace dbtools;
95 using namespace ::com::sun::star::uno;
96 using namespace ::com::sun::star::container;
97 using namespace ::com::sun::star::sdb;
98 using namespace ::com::sun::star::sdbc;
99 namespace dbaui
100 {
101 class OSaveAsDlgImpl
102 {
103 public:
104 	FixedText			m_aDescription;
105 	FixedText			m_aCatalogLbl;
106 	OSQLNameComboBox	m_aCatalog;
107 	FixedText			m_aSchemaLbl;
108 	OSQLNameComboBox	m_aSchema;
109 	FixedText			m_aLabel;
110 	OSQLNameEdit		m_aTitle;
111 	OKButton			m_aPB_OK;
112 	CancelButton		m_aPB_CANCEL;
113 	HelpButton			m_aPB_HELP;
114 	String				m_aQryLabel;
115 	String				m_sTblLabel;
116 	rtl::OUString       m_sCatalog;
117 	rtl::OUString       m_sSchema;
118 	String				m_aName;
119 	const IObjectNameCheck&
120 						m_rObjectNameCheck;
121 	String				m_sParentURL;
122 	::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData>			m_xMetaData;
123 	sal_Int32			m_nType;
124 	sal_Int32			m_nFlags;
125 
126 	OSaveAsDlgImpl(	Window * pParent,const sal_Int32& _rType,
127 					const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>& _xConnection,
128 					const String& rDefault,
129 					const IObjectNameCheck& _rObjectNameCheck,
130 					sal_Int32 _nFlags);
131 	OSaveAsDlgImpl(	Window * pParent,
132 					const String& rDefault,
133 					const IObjectNameCheck& _rObjectNameCheck,
134 					sal_Int32 _nFlags);
135 };
136 // -----------------------------------------------------------------------------
137 } // dbaui
138 // -----------------------------------------------------------------------------
139 OSaveAsDlgImpl::OSaveAsDlgImpl( Window * _pParent,
140 						const sal_Int32& _rType,
141 						const Reference< XConnection>& _xConnection,
142 						const String& rDefault,
143 						const IObjectNameCheck& _rObjectNameCheck,
144 						sal_Int32 _nFlags)
145     :m_aDescription(_pParent, ModuleRes (FT_DESCRIPTION))
146     ,m_aCatalogLbl(_pParent, ModuleRes (FT_CATALOG))
147     ,m_aCatalog(_pParent, ModuleRes (ET_CATALOG), ::rtl::OUString())
148     ,m_aSchemaLbl(_pParent, ModuleRes (FT_SCHEMA))
149     ,m_aSchema(_pParent, ModuleRes (ET_SCHEMA), ::rtl::OUString())
150     ,m_aLabel(_pParent, ModuleRes (FT_TITLE))
151     ,m_aTitle(_pParent, ModuleRes (ET_TITLE), ::rtl::OUString())
152     ,m_aPB_OK(_pParent, ModuleRes( PB_OK ) )
153     ,m_aPB_CANCEL(_pParent, ModuleRes( PB_CANCEL ))
154     ,m_aPB_HELP(_pParent, ModuleRes( PB_HELP))
155     ,m_aQryLabel(ModuleRes(STR_QRY_LABEL))
156     ,m_sTblLabel(ModuleRes(STR_TBL_LABEL))
157     ,m_aName(rDefault)
158     ,m_rObjectNameCheck( _rObjectNameCheck )
159 			 ,m_nType(_rType)
160 			 ,m_nFlags(_nFlags)
161 {
162     if ( _xConnection.is() )
163 	    m_xMetaData = _xConnection->getMetaData();
164 
165     if ( m_xMetaData.is() )
166     {
167         ::rtl::OUString sExtraNameChars( m_xMetaData->getExtraNameCharacters() );
168         m_aCatalog.setAllowedChars( sExtraNameChars );
169         m_aSchema.setAllowedChars( sExtraNameChars );
170         m_aTitle.setAllowedChars( sExtraNameChars );
171     }
172 
173     m_aCatalog.SetDropDownLineCount( 10 );
174     m_aSchema.SetDropDownLineCount( 10 );
175 }
176 // -----------------------------------------------------------------------------
177 OSaveAsDlgImpl::OSaveAsDlgImpl( Window * _pParent,
178 						const String& rDefault,
179 						const IObjectNameCheck& _rObjectNameCheck,
180 						sal_Int32 _nFlags)
181 			 :m_aDescription(_pParent, ModuleRes (FT_DESCRIPTION))
182 			 ,m_aCatalogLbl(_pParent, ModuleRes (FT_CATALOG))
183 			 ,m_aCatalog(_pParent, ModuleRes (ET_CATALOG))
184 			 ,m_aSchemaLbl(_pParent, ModuleRes (FT_SCHEMA))
185 			 ,m_aSchema(_pParent, ModuleRes (ET_SCHEMA))
186 			 ,m_aLabel(_pParent, ModuleRes (FT_TITLE))
187 			 ,m_aTitle(_pParent, ModuleRes (ET_TITLE))
188 			 ,m_aPB_OK(_pParent, ModuleRes( PB_OK ) )
189 			 ,m_aPB_CANCEL(_pParent, ModuleRes( PB_CANCEL ))
190 			 ,m_aPB_HELP(_pParent, ModuleRes( PB_HELP))
191 			 ,m_aQryLabel(ModuleRes(STR_QRY_LABEL))
192 			 ,m_sTblLabel(ModuleRes(STR_TBL_LABEL))
193 			 ,m_aName(rDefault)
194              ,m_rObjectNameCheck( _rObjectNameCheck )
195 			 ,m_nType(CommandType::COMMAND)
196 			 ,m_nFlags(_nFlags)
197 {
198     m_aCatalog.SetDropDownLineCount( 10 );
199     m_aSchema.SetDropDownLineCount( 10 );
200 }
201 
202 // -----------------------------------------------------------------------------
203 using namespace ::com::sun::star::lang;
204 
205 //==================================================================
206 namespace
207 {
208     typedef Reference< XResultSet > (SAL_CALL XDatabaseMetaData::*FGetMetaStrings)();
209 
210     void lcl_fillComboList( ComboBox& _rList, const Reference< XConnection >& _rxConnection,
211         FGetMetaStrings _GetAll, const ::rtl::OUString& _rCurrent )
212     {
213         try
214         {
215             Reference< XDatabaseMetaData > xMetaData( _rxConnection->getMetaData(), UNO_QUERY_THROW );
216 
217             Reference< XResultSet > xRes = (xMetaData.get()->*_GetAll)();
218 		    Reference< XRow > xRow( xRes, UNO_QUERY_THROW );
219 		    ::rtl::OUString sValue;
220 		    while ( xRes->next() )
221 		    {
222 			    sValue = xRow->getString( 1 );
223 			    if ( !xRow->wasNull() )
224 				    _rList.InsertEntry( sValue );
225 		    }
226 
227 			sal_uInt16 nPos = _rList.GetEntryPos( String( _rCurrent ) );
228 			if ( nPos != COMBOBOX_ENTRY_NOTFOUND )
229 				_rList.SelectEntryPos( nPos );
230             else
231                 _rList.SelectEntryPos( 0 );
232         }
233         catch( const Exception& )
234         {
235         	DBG_UNHANDLED_EXCEPTION();
236         }
237     }
238 }
239 
240 //==================================================================
241 OSaveAsDlg::OSaveAsDlg( Window * pParent,
242 						const sal_Int32& _rType,
243                         const Reference< XMultiServiceFactory >& _rxORB,
244 						const Reference< XConnection>& _xConnection,
245 						const String& rDefault,
246                         const IObjectNameCheck& _rObjectNameCheck,
247 						sal_Int32 _nFlags)
248 	:ModalDialog( pParent, ModuleRes(DLG_SAVE_AS))
249     ,m_xORB( _rxORB )
250 {
251 	m_pImpl = new OSaveAsDlgImpl(this,_rType,_xConnection,rDefault,_rObjectNameCheck,_nFlags);
252 
253     switch (_rType)
254 	{
255 		case CommandType::QUERY:
256 			implInitOnlyTitle(m_pImpl->m_aQryLabel);
257 			break;
258 
259         case CommandType::TABLE:
260             OSL_ENSURE( m_pImpl->m_xMetaData.is(), "OSaveAsDlg::OSaveAsDlg: no meta data for entering table names: this will crash!" );
261 			{
262 				m_pImpl->m_aLabel.SetText(m_pImpl->m_sTblLabel);
263 				Point aPos(m_pImpl->m_aPB_OK.GetPosPixel());
264 				if(m_pImpl->m_xMetaData.is() && !m_pImpl->m_xMetaData->supportsCatalogsInTableDefinitions())
265 				{
266 					m_pImpl->m_aCatalogLbl.Hide();
267 					m_pImpl->m_aCatalog.Hide();
268 
269 					aPos = m_pImpl->m_aLabel.GetPosPixel();
270 
271 					m_pImpl->m_aLabel.SetPosPixel(m_pImpl->m_aSchemaLbl.GetPosPixel());
272 					m_pImpl->m_aTitle.SetPosPixel(m_pImpl->m_aSchema.GetPosPixel());
273 
274 					m_pImpl->m_aSchemaLbl.SetPosPixel(m_pImpl->m_aCatalogLbl.GetPosPixel());
275 					m_pImpl->m_aSchema.SetPosPixel(m_pImpl->m_aCatalog.GetPosPixel());
276 				}
277 				else
278 				{
279 					// now fill the catalogs
280                     lcl_fillComboList( m_pImpl->m_aCatalog, _xConnection,
281                         &XDatabaseMetaData::getCatalogs, _xConnection->getCatalog() );
282 				}
283 
284 				if ( !m_pImpl->m_xMetaData->supportsSchemasInTableDefinitions())
285 				{
286 					m_pImpl->m_aSchemaLbl.Hide();
287 					m_pImpl->m_aSchema.Hide();
288 
289 					aPos = m_pImpl->m_aLabel.GetPosPixel();
290 
291 					m_pImpl->m_aLabel.SetPosPixel(m_pImpl->m_aSchemaLbl.GetPosPixel());
292 					m_pImpl->m_aTitle.SetPosPixel(m_pImpl->m_aSchema.GetPosPixel());
293 				}
294 				else
295 				{
296                     lcl_fillComboList( m_pImpl->m_aSchema, _xConnection,
297                         &XDatabaseMetaData::getSchemas, m_pImpl->m_xMetaData->getUserName() );
298 				}
299 
300 				OSL_ENSURE(m_pImpl->m_xMetaData.is(),"The metadata can not be null!");
301 				if(m_pImpl->m_aName.Search('.') != STRING_NOTFOUND)
302 				{
303 					::rtl::OUString sCatalog,sSchema,sTable;
304 					::dbtools::qualifiedNameComponents(m_pImpl->m_xMetaData,
305 														m_pImpl->m_aName,
306 														sCatalog,
307 														sSchema,
308 														sTable,
309 														::dbtools::eInDataManipulation);
310 
311 					sal_uInt16 nPos = m_pImpl->m_aCatalog.GetEntryPos(String(sCatalog));
312 					if ( nPos != COMBOBOX_ENTRY_NOTFOUND )
313 						m_pImpl->m_aCatalog.SelectEntryPos(nPos);
314 
315 					if ( sSchema.getLength() )
316 					{
317 						nPos = m_pImpl->m_aSchema.GetEntryPos(String(sSchema));
318 						if ( nPos != COMBOBOX_ENTRY_NOTFOUND )
319 							m_pImpl->m_aSchema.SelectEntryPos(nPos);
320 					}
321 					m_pImpl->m_aTitle.SetText(sTable);
322 				}
323 				else
324 					m_pImpl->m_aTitle.SetText(m_pImpl->m_aName);
325                 m_pImpl->m_aTitle.SetSelection( Selection( SELECTION_MIN, SELECTION_MAX ) );
326 
327 				m_pImpl->m_aPB_OK.SetPosPixel(Point(m_pImpl->m_aPB_OK.GetPosPixel().X(),aPos.Y()));
328 				m_pImpl->m_aPB_CANCEL.SetPosPixel(Point(m_pImpl->m_aPB_CANCEL.GetPosPixel().X(),aPos.Y()));
329 				m_pImpl->m_aPB_HELP.SetPosPixel(Point(m_pImpl->m_aPB_HELP.GetPosPixel().X(),aPos.Y()));
330 
331 				sal_uInt16 nLength =  m_pImpl->m_xMetaData.is() ? static_cast<sal_uInt16>(m_pImpl->m_xMetaData->getMaxTableNameLength()) : 0;
332 				nLength = nLength ? nLength : EDIT_NOLIMIT;
333 
334 				m_pImpl->m_aTitle.SetMaxTextLen(nLength);
335 				m_pImpl->m_aSchema.SetMaxTextLen(nLength);
336 				m_pImpl->m_aCatalog.SetMaxTextLen(nLength);
337 
338 				sal_Bool bCheck = _xConnection.is() && isSQL92CheckEnabled(_xConnection);
339 				m_pImpl->m_aTitle.setCheck(bCheck); // enable non valid sql chars as well
340 				m_pImpl->m_aSchema.setCheck(bCheck); // enable non valid sql chars as well
341 				m_pImpl->m_aCatalog.setCheck(bCheck); // enable non valid sql chars as well
342 
343 				Size aSize = GetSizePixel();
344 				aSize.Height() =
345 					aPos.Y() + m_pImpl->m_aPB_OK.GetSizePixel().Height() + m_pImpl->m_aTitle.GetSizePixel().Height() / 2;
346 				SetSizePixel(aSize);
347 			}
348 			break;
349 
350         default:
351             OSL_ENSURE( false, "OSaveAsDlg::OSaveAsDlg: Type not supported yet!" );
352 	}
353 
354 	implInit();
355 }
356 // -----------------------------------------------------------------------------
357 OSaveAsDlg::OSaveAsDlg( Window * pParent,
358                         const Reference< XMultiServiceFactory >& _rxORB,
359 						const String& rDefault,
360 						const String& _sLabel,
361 						const IObjectNameCheck& _rObjectNameCheck,
362 						sal_Int32 _nFlags)
363 			 :ModalDialog( pParent, ModuleRes(DLG_SAVE_AS))
364              ,m_xORB( _rxORB )
365 {
366 	m_pImpl = new OSaveAsDlgImpl(this,rDefault,_rObjectNameCheck,_nFlags);
367 	implInitOnlyTitle(_sLabel);
368 	implInit();
369 }
370 // -----------------------------------------------------------------------------
371 OSaveAsDlg::~OSaveAsDlg()
372 {
373 	DELETEZ(m_pImpl);
374 }
375 // -----------------------------------------------------------------------------
376 IMPL_LINK(OSaveAsDlg, ButtonClickHdl, Button *, pButton)
377 {
378 	if (pButton == &m_pImpl->m_aPB_OK)
379 	{
380 		m_pImpl->m_aName = m_pImpl->m_aTitle.GetText();
381 
382         ::rtl::OUString sNameToCheck( m_pImpl->m_aName );
383 
384 		if ( m_pImpl->m_nType == CommandType::TABLE )
385 		{
386 			sNameToCheck = ::dbtools::composeTableName(
387                 m_pImpl->m_xMetaData,
388                 getCatalog(),
389                 getSchema(),
390                 sNameToCheck,
391                 sal_False,  // no quoting
392                 ::dbtools::eInDataManipulation
393             );
394         }
395 
396         SQLExceptionInfo aNameError;
397         if ( m_pImpl->m_rObjectNameCheck.isNameValid( sNameToCheck, aNameError ) )
398 			EndDialog( RET_OK );
399 
400         showError( aNameError, this, m_xORB );
401         m_pImpl->m_aTitle.GrabFocus();
402 	}
403 	return 0;
404 }
405 // -----------------------------------------------------------------------------
406 
407 IMPL_LINK(OSaveAsDlg, EditModifyHdl, Edit *, pEdit )
408 {
409 	if (pEdit == &m_pImpl->m_aTitle)
410 		m_pImpl->m_aPB_OK.Enable(0 != m_pImpl->m_aTitle.GetText().Len());
411 	return 0;
412 }
413 // -----------------------------------------------------------------------------
414 void OSaveAsDlg::implInitOnlyTitle(const String& _rLabel)
415 {
416 	m_pImpl->m_aLabel.SetText(_rLabel);
417 	m_pImpl->m_aCatalogLbl.Hide();
418 	m_pImpl->m_aCatalog.Hide();
419 	m_pImpl->m_aSchemaLbl.Hide();
420 	m_pImpl->m_aSchema.Hide();
421 
422 	Point aPos(m_pImpl->m_aSchemaLbl.GetPosPixel());
423 	m_pImpl->m_aLabel.SetPosPixel(m_pImpl->m_aCatalogLbl.GetPosPixel());
424 	m_pImpl->m_aTitle.SetPosPixel(m_pImpl->m_aCatalog.GetPosPixel());
425 
426 	m_pImpl->m_aPB_OK.SetPosPixel(Point(m_pImpl->m_aPB_OK.GetPosPixel().X(),aPos.Y()));
427 	m_pImpl->m_aPB_CANCEL.SetPosPixel(Point(m_pImpl->m_aPB_CANCEL.GetPosPixel().X(),aPos.Y()));
428 	m_pImpl->m_aPB_HELP.SetPosPixel(Point(m_pImpl->m_aPB_HELP.GetPosPixel().X(),aPos.Y()));
429 
430 	sal_Int32 nNewHeight =
431 		aPos.Y() + m_pImpl->m_aPB_OK.GetSizePixel().Height() + m_pImpl->m_aTitle.GetSizePixel().Height() / 2;
432 
433 	SetSizePixel(Size(GetSizePixel().Width(), nNewHeight));
434 
435 	m_pImpl->m_aTitle.SetText(m_pImpl->m_aName);
436 	m_pImpl->m_aTitle.setCheck(sal_False); // enable non valid sql chars as well
437 }
438 // -----------------------------------------------------------------------------
439 void OSaveAsDlg::implInit()
440 {
441 	if ( 0 == ( m_pImpl->m_nFlags & SAD_ADDITIONAL_DESCRIPTION ) )
442 	{
443 		// hide the description window
444 		m_pImpl->m_aDescription.Hide();
445 
446 		// the number of pixels we have to move the other controls
447 		sal_Int32 nMoveUp = m_pImpl->m_aCatalog.GetPosPixel().Y() - m_pImpl->m_aDescription.GetPosPixel().Y();
448 
449 		// loop to all controls and move them ...
450 		for	(	Window* pChildControl = GetWindow( WINDOW_FIRSTCHILD );
451 				pChildControl;
452 				pChildControl= pChildControl->GetWindow( WINDOW_NEXT )
453 			)
454 		{
455 			if ( &m_pImpl->m_aDescription != pChildControl )
456 			{
457 				Point aPos = pChildControl->GetPosPixel();
458 				aPos.Y() -= nMoveUp;
459 				pChildControl->SetPosPixel(aPos);
460 			}
461 		}
462 
463 		// change our own size accordingly
464 		Size aSize = GetSizePixel();
465 		aSize.Height() -= nMoveUp;
466 		SetSizePixel(aSize);
467 	}
468 
469 	if ( SAD_TITLE_PASTE_AS == ( m_pImpl->m_nFlags & SAD_TITLE_PASTE_AS ) )
470 		SetText( String( ModuleRes( STR_TITLE_PASTE_AS ) ) );
471 	else if ( SAD_TITLE_RENAME == ( m_pImpl->m_nFlags & SAD_TITLE_RENAME ) )
472 	{
473 		SetText( String( ModuleRes( STR_TITLE_RENAME ) ) );
474 		m_pImpl->m_aTitle.SetHelpId(HID_DLG_RENAME);
475 	}
476 
477 	m_pImpl->m_aPB_OK.SetClickHdl(LINK(this,OSaveAsDlg,ButtonClickHdl));
478 	m_pImpl->m_aTitle.SetModifyHdl(LINK(this,OSaveAsDlg,EditModifyHdl));
479 	m_pImpl->m_aTitle.GrabFocus();
480 	FreeResource();
481 }
482 // -----------------------------------------------------------------------------
483 String OSaveAsDlg::getName() const		{ return m_pImpl->m_aName; }
484 String OSaveAsDlg::getCatalog() const	{ return m_pImpl->m_aCatalog.IsVisible() ? m_pImpl->m_aCatalog.GetText() : String(); }
485 String OSaveAsDlg::getSchema() const	{ return m_pImpl->m_aSchema.IsVisible() ? m_pImpl->m_aSchema.GetText() : String(); }
486 
487