Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / dbaccess / source / ui / dlg / dlgsave.cxx
blobb0160469cff922d7d63aea109758427e079f4aa7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "dlgsave.hxx"
31 #include "dlgsave.hrc"
32 #include <vcl/msgbox.hxx>
33 #include "dbu_dlg.hrc"
34 #include <com/sun/star/sdb/CommandType.hpp>
35 #include <com/sun/star/sdbc/XRow.hpp>
36 #include "sqlmessage.hxx"
37 #include <connectivity/dbtools.hxx>
38 #include "UITools.hxx"
39 #include "dbaccess_helpid.hrc"
40 #include "SqlNameEdit.hxx"
41 #include <vcl/button.hxx>
42 #include <vcl/fixed.hxx>
43 #include <vcl/edit.hxx>
44 #include <com/sun/star/container/XNameAccess.hpp>
45 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
46 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
47 #include "moduledbu.hxx"
48 #include "objectnamecheck.hxx"
49 #include <tools/diagnose_ex.h>
52 using namespace dbaui;
53 using namespace dbtools;
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star::container;
56 using namespace ::com::sun::star::sdb;
57 using namespace ::com::sun::star::sdbc;
58 namespace dbaui
60 class OSaveAsDlgImpl
62 public:
63 FixedText m_aDescription;
64 FixedText m_aCatalogLbl;
65 OSQLNameComboBox m_aCatalog;
66 FixedText m_aSchemaLbl;
67 OSQLNameComboBox m_aSchema;
68 FixedText m_aLabel;
69 OSQLNameEdit m_aTitle;
70 OKButton m_aPB_OK;
71 CancelButton m_aPB_CANCEL;
72 HelpButton m_aPB_HELP;
73 String m_aQryLabel;
74 String m_sTblLabel;
75 String m_aName;
76 const IObjectNameCheck&
77 m_rObjectNameCheck;
78 String m_sParentURL;
79 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData> m_xMetaData;
80 sal_Int32 m_nType;
81 sal_Int32 m_nFlags;
83 OSaveAsDlgImpl( Window * pParent,const sal_Int32& _rType,
84 const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>& _xConnection,
85 const String& rDefault,
86 const IObjectNameCheck& _rObjectNameCheck,
87 sal_Int32 _nFlags);
88 OSaveAsDlgImpl( Window * pParent,
89 const String& rDefault,
90 const IObjectNameCheck& _rObjectNameCheck,
91 sal_Int32 _nFlags);
93 // -----------------------------------------------------------------------------
94 } // dbaui
95 // -----------------------------------------------------------------------------
96 OSaveAsDlgImpl::OSaveAsDlgImpl( Window * _pParent,
97 const sal_Int32& _rType,
98 const Reference< XConnection>& _xConnection,
99 const String& rDefault,
100 const IObjectNameCheck& _rObjectNameCheck,
101 sal_Int32 _nFlags)
102 :m_aDescription(_pParent, ModuleRes (FT_DESCRIPTION))
103 ,m_aCatalogLbl(_pParent, ModuleRes (FT_CATALOG))
104 ,m_aCatalog(_pParent, ModuleRes (ET_CATALOG), ::rtl::OUString())
105 ,m_aSchemaLbl(_pParent, ModuleRes (FT_SCHEMA))
106 ,m_aSchema(_pParent, ModuleRes (ET_SCHEMA), ::rtl::OUString())
107 ,m_aLabel(_pParent, ModuleRes (FT_TITLE))
108 ,m_aTitle(_pParent, ModuleRes (ET_TITLE), ::rtl::OUString())
109 ,m_aPB_OK(_pParent, ModuleRes( PB_OK ) )
110 ,m_aPB_CANCEL(_pParent, ModuleRes( PB_CANCEL ))
111 ,m_aPB_HELP(_pParent, ModuleRes( PB_HELP))
112 ,m_aQryLabel(ModuleRes(STR_QRY_LABEL))
113 ,m_sTblLabel(ModuleRes(STR_TBL_LABEL))
114 ,m_aName(rDefault)
115 ,m_rObjectNameCheck( _rObjectNameCheck )
116 ,m_nType(_rType)
117 ,m_nFlags(_nFlags)
119 if ( _xConnection.is() )
120 m_xMetaData = _xConnection->getMetaData();
122 if ( m_xMetaData.is() )
124 ::rtl::OUString sExtraNameChars( m_xMetaData->getExtraNameCharacters() );
125 m_aCatalog.setAllowedChars( sExtraNameChars );
126 m_aSchema.setAllowedChars( sExtraNameChars );
127 m_aTitle.setAllowedChars( sExtraNameChars );
130 m_aCatalog.SetDropDownLineCount( 10 );
131 m_aSchema.SetDropDownLineCount( 10 );
133 // -----------------------------------------------------------------------------
134 OSaveAsDlgImpl::OSaveAsDlgImpl( Window * _pParent,
135 const String& rDefault,
136 const IObjectNameCheck& _rObjectNameCheck,
137 sal_Int32 _nFlags)
138 :m_aDescription(_pParent, ModuleRes (FT_DESCRIPTION))
139 ,m_aCatalogLbl(_pParent, ModuleRes (FT_CATALOG))
140 ,m_aCatalog(_pParent, ModuleRes (ET_CATALOG))
141 ,m_aSchemaLbl(_pParent, ModuleRes (FT_SCHEMA))
142 ,m_aSchema(_pParent, ModuleRes (ET_SCHEMA))
143 ,m_aLabel(_pParent, ModuleRes (FT_TITLE))
144 ,m_aTitle(_pParent, ModuleRes (ET_TITLE))
145 ,m_aPB_OK(_pParent, ModuleRes( PB_OK ) )
146 ,m_aPB_CANCEL(_pParent, ModuleRes( PB_CANCEL ))
147 ,m_aPB_HELP(_pParent, ModuleRes( PB_HELP))
148 ,m_aQryLabel(ModuleRes(STR_QRY_LABEL))
149 ,m_sTblLabel(ModuleRes(STR_TBL_LABEL))
150 ,m_aName(rDefault)
151 ,m_rObjectNameCheck( _rObjectNameCheck )
152 ,m_nType(CommandType::COMMAND)
153 ,m_nFlags(_nFlags)
155 m_aCatalog.SetDropDownLineCount( 10 );
156 m_aSchema.SetDropDownLineCount( 10 );
159 // -----------------------------------------------------------------------------
160 using namespace ::com::sun::star::lang;
162 //==================================================================
163 namespace
165 typedef Reference< XResultSet > (SAL_CALL XDatabaseMetaData::*FGetMetaStrings)();
167 void lcl_fillComboList( ComboBox& _rList, const Reference< XConnection >& _rxConnection,
168 FGetMetaStrings _GetAll, const ::rtl::OUString& _rCurrent )
172 Reference< XDatabaseMetaData > xMetaData( _rxConnection->getMetaData(), UNO_QUERY_THROW );
174 Reference< XResultSet > xRes = (xMetaData.get()->*_GetAll)();
175 Reference< XRow > xRow( xRes, UNO_QUERY_THROW );
176 ::rtl::OUString sValue;
177 while ( xRes->next() )
179 sValue = xRow->getString( 1 );
180 if ( !xRow->wasNull() )
181 _rList.InsertEntry( sValue );
184 sal_uInt16 nPos = _rList.GetEntryPos( String( _rCurrent ) );
185 if ( nPos != COMBOBOX_ENTRY_NOTFOUND )
186 _rList.SelectEntryPos( nPos );
187 else
188 _rList.SelectEntryPos( 0 );
190 catch( const Exception& )
192 DBG_UNHANDLED_EXCEPTION();
197 //==================================================================
198 OSaveAsDlg::OSaveAsDlg( Window * pParent,
199 const sal_Int32& _rType,
200 const Reference< XMultiServiceFactory >& _rxORB,
201 const Reference< XConnection>& _xConnection,
202 const String& rDefault,
203 const IObjectNameCheck& _rObjectNameCheck,
204 sal_Int32 _nFlags)
205 :ModalDialog( pParent, ModuleRes(DLG_SAVE_AS))
206 ,m_xORB( _rxORB )
208 m_pImpl = new OSaveAsDlgImpl(this,_rType,_xConnection,rDefault,_rObjectNameCheck,_nFlags);
210 switch (_rType)
212 case CommandType::QUERY:
213 implInitOnlyTitle(m_pImpl->m_aQryLabel);
214 break;
216 case CommandType::TABLE:
217 OSL_ENSURE( m_pImpl->m_xMetaData.is(), "OSaveAsDlg::OSaveAsDlg: no meta data for entering table names: this will crash!" );
219 m_pImpl->m_aLabel.SetText(m_pImpl->m_sTblLabel);
220 Point aPos(m_pImpl->m_aPB_OK.GetPosPixel());
221 if(m_pImpl->m_xMetaData.is() && !m_pImpl->m_xMetaData->supportsCatalogsInTableDefinitions())
223 m_pImpl->m_aCatalogLbl.Hide();
224 m_pImpl->m_aCatalog.Hide();
226 aPos = m_pImpl->m_aLabel.GetPosPixel();
228 m_pImpl->m_aLabel.SetPosPixel(m_pImpl->m_aSchemaLbl.GetPosPixel());
229 m_pImpl->m_aTitle.SetPosPixel(m_pImpl->m_aSchema.GetPosPixel());
231 m_pImpl->m_aSchemaLbl.SetPosPixel(m_pImpl->m_aCatalogLbl.GetPosPixel());
232 m_pImpl->m_aSchema.SetPosPixel(m_pImpl->m_aCatalog.GetPosPixel());
234 else
236 // now fill the catalogs
237 lcl_fillComboList( m_pImpl->m_aCatalog, _xConnection,
238 &XDatabaseMetaData::getCatalogs, _xConnection->getCatalog() );
241 if ( !m_pImpl->m_xMetaData->supportsSchemasInTableDefinitions())
243 m_pImpl->m_aSchemaLbl.Hide();
244 m_pImpl->m_aSchema.Hide();
246 aPos = m_pImpl->m_aLabel.GetPosPixel();
248 m_pImpl->m_aLabel.SetPosPixel(m_pImpl->m_aSchemaLbl.GetPosPixel());
249 m_pImpl->m_aTitle.SetPosPixel(m_pImpl->m_aSchema.GetPosPixel());
251 else
253 lcl_fillComboList( m_pImpl->m_aSchema, _xConnection,
254 &XDatabaseMetaData::getSchemas, m_pImpl->m_xMetaData->getUserName() );
257 OSL_ENSURE(m_pImpl->m_xMetaData.is(),"The metadata can not be null!");
258 if(m_pImpl->m_aName.Search('.') != STRING_NOTFOUND)
260 ::rtl::OUString sCatalog,sSchema,sTable;
261 ::dbtools::qualifiedNameComponents(m_pImpl->m_xMetaData,
262 m_pImpl->m_aName,
263 sCatalog,
264 sSchema,
265 sTable,
266 ::dbtools::eInDataManipulation);
268 sal_uInt16 nPos = m_pImpl->m_aCatalog.GetEntryPos(String(sCatalog));
269 if ( nPos != COMBOBOX_ENTRY_NOTFOUND )
270 m_pImpl->m_aCatalog.SelectEntryPos(nPos);
272 if ( !sSchema.isEmpty() )
274 nPos = m_pImpl->m_aSchema.GetEntryPos(String(sSchema));
275 if ( nPos != COMBOBOX_ENTRY_NOTFOUND )
276 m_pImpl->m_aSchema.SelectEntryPos(nPos);
278 m_pImpl->m_aTitle.SetText(sTable);
280 else
281 m_pImpl->m_aTitle.SetText(m_pImpl->m_aName);
282 m_pImpl->m_aTitle.SetSelection( Selection( SELECTION_MIN, SELECTION_MAX ) );
284 m_pImpl->m_aPB_OK.SetPosPixel(Point(m_pImpl->m_aPB_OK.GetPosPixel().X(),aPos.Y()));
285 m_pImpl->m_aPB_CANCEL.SetPosPixel(Point(m_pImpl->m_aPB_CANCEL.GetPosPixel().X(),aPos.Y()));
286 m_pImpl->m_aPB_HELP.SetPosPixel(Point(m_pImpl->m_aPB_HELP.GetPosPixel().X(),aPos.Y()));
288 sal_uInt16 nLength = m_pImpl->m_xMetaData.is() ? static_cast<sal_uInt16>(m_pImpl->m_xMetaData->getMaxTableNameLength()) : 0;
289 nLength = nLength ? nLength : EDIT_NOLIMIT;
291 m_pImpl->m_aTitle.SetMaxTextLen(nLength);
292 m_pImpl->m_aSchema.SetMaxTextLen(nLength);
293 m_pImpl->m_aCatalog.SetMaxTextLen(nLength);
295 sal_Bool bCheck = _xConnection.is() && isSQL92CheckEnabled(_xConnection);
296 m_pImpl->m_aTitle.setCheck(bCheck); // enable non valid sql chars as well
297 m_pImpl->m_aSchema.setCheck(bCheck); // enable non valid sql chars as well
298 m_pImpl->m_aCatalog.setCheck(bCheck); // enable non valid sql chars as well
300 Size aSize = GetSizePixel();
301 aSize.Height() =
302 aPos.Y() + m_pImpl->m_aPB_OK.GetSizePixel().Height() + m_pImpl->m_aTitle.GetSizePixel().Height() / 2;
303 SetSizePixel(aSize);
305 break;
307 default:
308 OSL_FAIL( "OSaveAsDlg::OSaveAsDlg: Type not supported yet!" );
311 implInit();
313 // -----------------------------------------------------------------------------
314 OSaveAsDlg::OSaveAsDlg( Window * pParent,
315 const Reference< XMultiServiceFactory >& _rxORB,
316 const String& rDefault,
317 const String& _sLabel,
318 const IObjectNameCheck& _rObjectNameCheck,
319 sal_Int32 _nFlags)
320 :ModalDialog( pParent, ModuleRes(DLG_SAVE_AS))
321 ,m_xORB( _rxORB )
323 m_pImpl = new OSaveAsDlgImpl(this,rDefault,_rObjectNameCheck,_nFlags);
324 implInitOnlyTitle(_sLabel);
325 implInit();
327 // -----------------------------------------------------------------------------
328 OSaveAsDlg::~OSaveAsDlg()
330 DELETEZ(m_pImpl);
332 // -----------------------------------------------------------------------------
333 IMPL_LINK(OSaveAsDlg, ButtonClickHdl, Button *, pButton)
335 if (pButton == &m_pImpl->m_aPB_OK)
337 m_pImpl->m_aName = m_pImpl->m_aTitle.GetText();
339 ::rtl::OUString sNameToCheck( m_pImpl->m_aName );
341 if ( m_pImpl->m_nType == CommandType::TABLE )
343 sNameToCheck = ::dbtools::composeTableName(
344 m_pImpl->m_xMetaData,
345 getCatalog(),
346 getSchema(),
347 sNameToCheck,
348 sal_False, // no quoting
349 ::dbtools::eInDataManipulation
353 SQLExceptionInfo aNameError;
354 if ( m_pImpl->m_rObjectNameCheck.isNameValid( sNameToCheck, aNameError ) )
355 EndDialog( RET_OK );
357 showError( aNameError, this, m_xORB );
358 m_pImpl->m_aTitle.GrabFocus();
360 return 0;
362 // -----------------------------------------------------------------------------
364 IMPL_LINK(OSaveAsDlg, EditModifyHdl, Edit *, pEdit )
366 if (pEdit == &m_pImpl->m_aTitle)
367 m_pImpl->m_aPB_OK.Enable(0 != m_pImpl->m_aTitle.GetText().Len());
368 return 0;
370 // -----------------------------------------------------------------------------
371 void OSaveAsDlg::implInitOnlyTitle(const String& _rLabel)
373 m_pImpl->m_aLabel.SetText(_rLabel);
374 m_pImpl->m_aCatalogLbl.Hide();
375 m_pImpl->m_aCatalog.Hide();
376 m_pImpl->m_aSchemaLbl.Hide();
377 m_pImpl->m_aSchema.Hide();
379 Point aPos(m_pImpl->m_aSchemaLbl.GetPosPixel());
380 m_pImpl->m_aLabel.SetPosPixel(m_pImpl->m_aCatalogLbl.GetPosPixel());
381 m_pImpl->m_aTitle.SetPosPixel(m_pImpl->m_aCatalog.GetPosPixel());
383 m_pImpl->m_aPB_OK.SetPosPixel(Point(m_pImpl->m_aPB_OK.GetPosPixel().X(),aPos.Y()));
384 m_pImpl->m_aPB_CANCEL.SetPosPixel(Point(m_pImpl->m_aPB_CANCEL.GetPosPixel().X(),aPos.Y()));
385 m_pImpl->m_aPB_HELP.SetPosPixel(Point(m_pImpl->m_aPB_HELP.GetPosPixel().X(),aPos.Y()));
387 sal_Int32 nNewHeight =
388 aPos.Y() + m_pImpl->m_aPB_OK.GetSizePixel().Height() + m_pImpl->m_aTitle.GetSizePixel().Height() / 2;
390 SetSizePixel(Size(GetSizePixel().Width(), nNewHeight));
392 m_pImpl->m_aTitle.SetText(m_pImpl->m_aName);
393 m_pImpl->m_aTitle.setCheck(sal_False); // enable non valid sql chars as well
395 // -----------------------------------------------------------------------------
396 void OSaveAsDlg::implInit()
398 if ( 0 == ( m_pImpl->m_nFlags & SAD_ADDITIONAL_DESCRIPTION ) )
400 // hide the description window
401 m_pImpl->m_aDescription.Hide();
403 // the number of pixels we have to move the other controls
404 sal_Int32 nMoveUp = m_pImpl->m_aCatalog.GetPosPixel().Y() - m_pImpl->m_aDescription.GetPosPixel().Y();
406 // loop to all controls and move them ...
407 for ( Window* pChildControl = GetWindow( WINDOW_FIRSTCHILD );
408 pChildControl;
409 pChildControl= pChildControl->GetWindow( WINDOW_NEXT )
412 if ( &m_pImpl->m_aDescription != pChildControl )
414 Point aPos = pChildControl->GetPosPixel();
415 aPos.Y() -= nMoveUp;
416 pChildControl->SetPosPixel(aPos);
420 // change our own size accordingly
421 Size aSize = GetSizePixel();
422 aSize.Height() -= nMoveUp;
423 SetSizePixel(aSize);
426 if ( SAD_TITLE_PASTE_AS == ( m_pImpl->m_nFlags & SAD_TITLE_PASTE_AS ) )
427 SetText( String( ModuleRes( STR_TITLE_PASTE_AS ) ) );
428 else if ( SAD_TITLE_RENAME == ( m_pImpl->m_nFlags & SAD_TITLE_RENAME ) )
430 SetText( String( ModuleRes( STR_TITLE_RENAME ) ) );
431 m_pImpl->m_aTitle.SetHelpId(HID_DLG_RENAME);
434 m_pImpl->m_aPB_OK.SetClickHdl(LINK(this,OSaveAsDlg,ButtonClickHdl));
435 m_pImpl->m_aTitle.SetModifyHdl(LINK(this,OSaveAsDlg,EditModifyHdl));
436 m_pImpl->m_aTitle.GrabFocus();
437 FreeResource();
439 // -----------------------------------------------------------------------------
440 String OSaveAsDlg::getName() const { return m_pImpl->m_aName; }
441 String OSaveAsDlg::getCatalog() const { return m_pImpl->m_aCatalog.IsVisible() ? m_pImpl->m_aCatalog.GetText() : String(); }
442 String OSaveAsDlg::getSchema() const { return m_pImpl->m_aSchema.IsVisible() ? m_pImpl->m_aSchema.GetText() : String(); }
444 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */