Update ooo320-m1
[ooovba.git] / extensions / source / dbpilots / commonpagesdbp.cxx
blob0cf18061d547c6a999c58bc32865bfcc4d45287b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: commonpagesdbp.cxx,v $
10 * $Revision: 1.18 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include "commonpagesdbp.hxx"
34 #ifndef _EXTENSIONS_DBP_DBPRESID_HRC_
35 #include "dbpresid.hrc"
36 #endif
37 #include "componentmodule.hxx"
38 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 #include <com/sun/star/sdb/XCompletedConnection.hpp>
40 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
41 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
42 #include <com/sun/star/sdbc/XConnection.hpp>
43 #include <com/sun/star/sdb/SQLContext.hpp>
44 #include <com/sun/star/sdbc/SQLWarning.hpp>
45 #include <com/sun/star/sdb/CommandType.hpp>
46 #include <tools/debug.hxx>
47 #include <svtools/localresaccess.hxx>
48 #include <comphelper/interaction.hxx>
49 #include <connectivity/dbtools.hxx>
50 #include <vcl/stdtext.hxx>
51 #include <vcl/waitobj.hxx>
52 #include <sfx2/docfilt.hxx>
53 #include <svtools/pathoptions.hxx>
54 #include <sfx2/filedlghelper.hxx>
55 #ifndef SVTOOLS_FILENOTATION_HXX_
56 #include <svtools/filenotation.hxx>
57 #endif
58 //.........................................................................
59 namespace dbp
61 //.........................................................................
63 using namespace ::com::sun::star::uno;
64 using namespace ::com::sun::star::lang;
65 using namespace ::com::sun::star::container;
66 using namespace ::com::sun::star::sdb;
67 using namespace ::com::sun::star::sdbc;
68 using namespace ::com::sun::star::sdbcx;
69 using namespace ::com::sun::star::task;
70 using namespace ::comphelper;
72 //=====================================================================
73 //= OTableSelectionPage
74 //=====================================================================
75 //---------------------------------------------------------------------
76 OTableSelectionPage::OTableSelectionPage(OControlWizard* _pParent)
77 :OControlWizardPage(_pParent, ModuleRes(RID_PAGE_TABLESELECTION))
78 ,m_aData (this, ModuleRes(FL_DATA))
79 ,m_aExplanation (this, ModuleRes(FT_EXPLANATION))
80 ,m_aDatasourceLabel (this, ModuleRes(FT_DATASOURCE))
81 ,m_aDatasource (this, ModuleRes(LB_DATASOURCE))
82 ,m_aSearchDatabase (this, ModuleRes(PB_FORMDATASOURCE))
83 ,m_aTableLabel (this, ModuleRes(FT_TABLE))
84 ,m_aTable (this, ModuleRes(LB_TABLE))
86 FreeResource();
88 implCollectDatasource();
90 m_aDatasource.SetSelectHdl(LINK(this, OTableSelectionPage, OnListboxSelection));
91 m_aTable.SetSelectHdl(LINK(this, OTableSelectionPage, OnListboxSelection));
92 m_aTable.SetDoubleClickHdl(LINK(this, OTableSelectionPage, OnListboxDoubleClicked));
93 m_aSearchDatabase.SetClickHdl(LINK(this, OTableSelectionPage, OnSearchClicked));
95 m_aDatasource.SetDropDownLineCount(10);
98 //---------------------------------------------------------------------
99 void OTableSelectionPage::ActivatePage()
101 OControlWizardPage::ActivatePage();
102 m_aDatasource.GrabFocus();
105 //---------------------------------------------------------------------
106 bool OTableSelectionPage::canAdvance() const
108 if (!OControlWizardPage::canAdvance())
109 return false;
111 if (0 == m_aDatasource.GetSelectEntryCount())
112 return false;
114 if (0 == m_aTable.GetSelectEntryCount())
115 return false;
117 return sal_True;
120 //---------------------------------------------------------------------
121 void OTableSelectionPage::initializePage()
123 OControlWizardPage::initializePage();
125 const OControlWizardContext& rContext = getContext();
128 ::rtl::OUString sDataSourceName;
129 rContext.xForm->getPropertyValue(::rtl::OUString::createFromAscii("DataSourceName")) >>= sDataSourceName;
131 Reference< XConnection > xConnection;
132 bool bEmbedded = ::dbtools::isEmbeddedInDatabase( rContext.xForm, xConnection );
133 if ( bEmbedded )
135 m_aDatasource.Hide();
136 m_aDatasourceLabel.Hide();
137 m_aSearchDatabase.Hide();
138 m_aTableLabel.SetPosPixel(m_aDatasourceLabel.GetPosPixel());
139 m_aTable.SetPosPixel(m_aDatasource.GetPosPixel());
140 m_aDatasource.InsertEntry(sDataSourceName);
142 m_aDatasource.SelectEntry(sDataSourceName);
144 implFillTables(xConnection);
146 ::rtl::OUString sCommand;
147 OSL_VERIFY( rContext.xForm->getPropertyValue( ::rtl::OUString::createFromAscii("Command") ) >>= sCommand );
148 sal_Int32 nCommandType = CommandType::TABLE;
149 OSL_VERIFY( rContext.xForm->getPropertyValue( ::rtl::OUString::createFromAscii("CommandType") ) >>= nCommandType );
151 // search the entry of the given type with the given name
152 XubString sLookup( sCommand );
153 for ( USHORT nLookup = 0; nLookup < m_aTable.GetEntryCount(); ++nLookup )
155 if ( m_aTable.GetEntry( nLookup ) == sLookup )
156 if ( reinterpret_cast< sal_IntPtr >( m_aTable.GetEntryData( nLookup ) ) == nCommandType )
158 m_aTable.SelectEntryPos( nLookup );
159 break;
163 catch(Exception&)
165 DBG_ERROR("OTableSelectionPage::initializePage: caught an exception!");
169 //---------------------------------------------------------------------
170 sal_Bool OTableSelectionPage::commitPage( CommitPageReason _eReason )
172 if (!OControlWizardPage::commitPage(_eReason))
173 return sal_False;
175 const OControlWizardContext& rContext = getContext();
178 Reference< XConnection > xOldConn;
179 if ( !rContext.bEmbedded )
181 xOldConn = getFormConnection();
183 ::rtl::OUString sDataSource = m_aDatasource.GetSelectEntry();
184 rContext.xForm->setPropertyValue( ::rtl::OUString::createFromAscii("DataSourceName"), makeAny( sDataSource ) );
186 ::rtl::OUString sCommand = m_aTable.GetSelectEntry();
187 sal_Int32 nCommandType = reinterpret_cast< sal_IntPtr >( m_aTable.GetEntryData( m_aTable.GetSelectEntryPos() ) );
189 rContext.xForm->setPropertyValue( ::rtl::OUString::createFromAscii("Command"), makeAny( sCommand ) );
190 rContext.xForm->setPropertyValue( ::rtl::OUString::createFromAscii("CommandType"), makeAny( nCommandType ) );
192 if ( !rContext.bEmbedded )
193 setFormConnection( xOldConn, sal_False );
195 if (!updateContext())
196 return sal_False;
198 catch(Exception&)
200 DBG_ERROR("OTableSelectionPage::commitPage: caught an exception!");
203 return sal_True;
206 //---------------------------------------------------------------------
207 IMPL_LINK( OTableSelectionPage, OnSearchClicked, PushButton*, /*_pButton*/ )
209 ::sfx2::FileDialogHelper aFileDlg(WB_3DLOOK);
210 aFileDlg.SetDisplayDirectory( SvtPathOptions().GetWorkPath() );
212 static const String s_sDatabaseType = String::CreateFromAscii("StarOffice XML (Base)");
213 const SfxFilter* pFilter = SfxFilter::GetFilterByName( s_sDatabaseType);
214 OSL_ENSURE(pFilter,"Filter: StarOffice XML (Base) could not be found!");
215 if ( pFilter )
217 aFileDlg.AddFilter(pFilter->GetUIName(),pFilter->GetDefaultExtension());
220 if (0 == aFileDlg.Execute())
222 String sDataSourceName = aFileDlg.GetPath();
223 ::svt::OFileNotation aFileNotation(sDataSourceName);
224 sDataSourceName = aFileNotation.get(::svt::OFileNotation::N_SYSTEM);
225 m_aDatasource.InsertEntry(sDataSourceName);
226 m_aDatasource.SelectEntry(sDataSourceName);
227 LINK(this, OTableSelectionPage, OnListboxSelection).Call(&m_aDatasource);
229 return 0L;
231 //---------------------------------------------------------------------
232 IMPL_LINK( OTableSelectionPage, OnListboxDoubleClicked, ListBox*, _pBox )
234 if (_pBox->GetSelectEntryCount())
235 getDialog()->travelNext();
236 return 0L;
239 //---------------------------------------------------------------------
240 IMPL_LINK( OTableSelectionPage, OnListboxSelection, ListBox*, _pBox )
242 if (&m_aDatasource == _pBox)
243 { // new data source selected
244 implFillTables();
246 else
250 updateDialogTravelUI();
252 return 0L;
255 //---------------------------------------------------------------------
256 namespace
258 void lcl_fillEntries( ListBox& _rListBox, const Sequence< ::rtl::OUString >& _rNames, const Image& _rImage, sal_Int32 _nCommandType )
260 const ::rtl::OUString* pNames = _rNames.getConstArray();
261 const ::rtl::OUString* pNamesEnd = _rNames.getConstArray() + _rNames.getLength();
262 sal_uInt16 nPos = 0;
263 while ( pNames != pNamesEnd )
265 nPos = _rListBox.InsertEntry( *pNames++, _rImage );
266 _rListBox.SetEntryData( nPos, reinterpret_cast< void* >( _nCommandType ) );
271 //---------------------------------------------------------------------
272 void OTableSelectionPage::implFillTables(const Reference< XConnection >& _rxConn)
274 m_aTable.Clear();
276 WaitObject aWaitCursor(this);
278 // will be the table tables of the selected data source
279 Sequence< ::rtl::OUString > aTableNames;
280 Sequence< ::rtl::OUString > aQueryNames;
282 // connect to the data source
283 Any aSQLException;
284 Reference< XConnection > xConn = _rxConn;
285 if ( !xConn.is() )
287 if (!m_xDSContext.is())
288 return;
289 // connect to the data source
292 ::rtl::OUString sCurrentDatasource = m_aDatasource.GetSelectEntry();
293 if (sCurrentDatasource.getLength())
295 // obtain the DS object
296 Reference< XCompletedConnection > xDatasource;
297 // check if I know this one otherwise transform it into a file URL
298 if ( !m_xDSContext->hasByName(sCurrentDatasource) )
300 ::svt::OFileNotation aFileNotation(sCurrentDatasource);
301 sCurrentDatasource = aFileNotation.get(::svt::OFileNotation::N_URL);
304 if (m_xDSContext->getByName(sCurrentDatasource) >>= xDatasource)
305 { // connect
306 // get the default SDB interaction handler
307 Reference< XInteractionHandler > xHandler = getDialog()->getInteractionHandler(this);
308 if (!xHandler.is() )
309 return;
310 xConn = xDatasource->connectWithCompletion(xHandler);
311 setFormConnection( xConn );
313 else
315 DBG_ERROR("OTableSelectionPage::implFillTables: invalid data source object returned by the context");
319 catch(SQLContext& e) { aSQLException <<= e; }
320 catch(SQLWarning& e) { aSQLException <<= e; }
321 catch(SQLException& e) { aSQLException <<= e; }
322 catch (Exception&)
324 DBG_ERROR("OTableSelectionPage::implFillTables: could not fill the table list!");
328 // will be the table tables of the selected data source
329 if ( xConn.is() )
333 // get the tables
334 Reference< XTablesSupplier > xSupplTables(xConn, UNO_QUERY);
335 if ( xSupplTables.is() )
337 Reference< XNameAccess > xTables(xSupplTables->getTables(), UNO_QUERY);
338 if (xTables.is())
339 aTableNames = xTables->getElementNames();
342 // and the queries
343 Reference< XQueriesSupplier > xSuppQueries( xConn, UNO_QUERY );
344 if ( xSuppQueries.is() )
346 Reference< XNameAccess > xQueries( xSuppQueries->getQueries(), UNO_QUERY );
347 if ( xQueries.is() )
348 aQueryNames = xQueries->getElementNames();
351 catch(SQLContext& e) { aSQLException <<= e; }
352 catch(SQLWarning& e) { aSQLException <<= e; }
353 catch(SQLException& e) { aSQLException <<= e; }
354 catch (Exception&)
356 DBG_ERROR("OTableSelectionPage::implFillTables: could not fill the table list!");
361 if ( aSQLException.hasValue() )
362 { // an SQLException (or derivee) was thrown ...
363 Reference< XInteractionRequest > xRequest = new OInteractionRequest(aSQLException);
366 // get the default SDB interaction handler
367 Reference< XInteractionHandler > xHandler = getDialog()->getInteractionHandler(this);
368 if ( xHandler.is() )
369 xHandler->handle(xRequest);
371 catch(Exception&) { }
372 return;
375 Image aTableImage, aQueryImage;
377 ::svt::OLocalResourceAccess aLocalResAccess( ModuleRes( RID_PAGE_TABLESELECTION ), RSC_TABPAGE );
379 bool bIsHiContrast = m_aTable.GetBackground().GetColor().IsDark();
380 aTableImage = Image( ModuleRes( bIsHiContrast ? IMG_TABLE_HC : IMG_TABLE ) );
381 aQueryImage = Image( ModuleRes( bIsHiContrast ? IMG_QUERY_HC : IMG_QUERY ) );
383 lcl_fillEntries( m_aTable, aTableNames, aTableImage, CommandType::TABLE );
384 lcl_fillEntries( m_aTable, aQueryNames, aQueryImage, CommandType::QUERY );
387 //---------------------------------------------------------------------
388 void OTableSelectionPage::implCollectDatasource()
392 m_xDSContext = getContext().xDatasourceContext;
393 if (m_xDSContext.is())
394 fillListBox(m_aDatasource, m_xDSContext->getElementNames());
396 catch (Exception&)
398 DBG_ERROR("OTableSelectionPage::implCollectDatasource: could not collect the data source names!");
402 //=====================================================================
403 //= OMaybeListSelectionPage
404 //=====================================================================
405 //---------------------------------------------------------------------
406 OMaybeListSelectionPage::OMaybeListSelectionPage( OControlWizard* _pParent, const ResId& _rId )
407 :OControlWizardPage(_pParent, _rId)
408 ,m_pYes(NULL)
409 ,m_pNo(NULL)
410 ,m_pList(NULL)
414 //---------------------------------------------------------------------
415 void OMaybeListSelectionPage::announceControls(RadioButton& _rYesButton, RadioButton& _rNoButton, ListBox& _rSelection)
417 m_pYes = &_rYesButton;
418 m_pNo = &_rNoButton;
419 m_pList = &_rSelection;
421 m_pYes->SetClickHdl(LINK(this, OMaybeListSelectionPage, OnRadioSelected));
422 m_pNo->SetClickHdl(LINK(this, OMaybeListSelectionPage, OnRadioSelected));
423 implEnableWindows();
426 //---------------------------------------------------------------------
427 IMPL_LINK( OMaybeListSelectionPage, OnRadioSelected, RadioButton*, /*NOTINTERESTEDIN*/ )
429 implEnableWindows();
430 return 0L;
433 //---------------------------------------------------------------------
434 void OMaybeListSelectionPage::implInitialize(const String& _rSelection)
436 DBG_ASSERT(m_pYes, "OMaybeListSelectionPage::implInitialize: no controls announced!");
437 sal_Bool bIsSelection = (0 != _rSelection.Len());
438 m_pYes->Check(bIsSelection);
439 m_pNo->Check(!bIsSelection);
440 m_pList->Enable(bIsSelection);
442 m_pList->SelectEntry(bIsSelection ? _rSelection : String());
445 //---------------------------------------------------------------------
446 void OMaybeListSelectionPage::implCommit(String& _rSelection)
448 _rSelection = m_pYes->IsChecked() ? m_pList->GetSelectEntry() : String();
451 //---------------------------------------------------------------------
452 void OMaybeListSelectionPage::implEnableWindows()
454 m_pList->Enable(m_pYes->IsChecked());
457 //---------------------------------------------------------------------
458 void OMaybeListSelectionPage::ActivatePage()
460 OControlWizardPage::ActivatePage();
462 DBG_ASSERT(m_pYes, "OMaybeListSelectionPage::ActivatePage: no controls announced!");
463 if (m_pYes->IsChecked())
464 m_pList->GrabFocus();
465 else
466 m_pNo->GrabFocus();
469 //=====================================================================
470 //= ODBFieldPage
471 //=====================================================================
472 //---------------------------------------------------------------------
473 ODBFieldPage::ODBFieldPage( OControlWizard* _pParent )
474 :OMaybeListSelectionPage(_pParent, ModuleRes(RID_PAGE_OPTION_DBFIELD))
475 ,m_aFrame (this, ModuleRes(FL_DATABASEFIELD_EXPL))
476 ,m_aDescription (this, ModuleRes(FT_DATABASEFIELD_EXPL))
477 ,m_aQuestion (this, ModuleRes(FT_DATABASEFIELD_QUEST))
478 ,m_aStoreYes (this, ModuleRes(RB_STOREINFIELD_YES))
479 ,m_aStoreNo (this, ModuleRes(LB_STOREINFIELD))
480 ,m_aStoreWhere (this, ModuleRes(RB_STOREINFIELD_NO))
482 FreeResource();
483 announceControls(m_aStoreYes, m_aStoreNo, m_aStoreWhere);
484 m_aStoreWhere.SetDropDownLineCount(10);
487 //---------------------------------------------------------------------
488 void ODBFieldPage::initializePage()
490 OMaybeListSelectionPage::initializePage();
492 // fill the fields page
493 fillListBox(m_aStoreWhere, getContext().aFieldNames);
495 implInitialize(getDBFieldSetting());
498 //---------------------------------------------------------------------
499 sal_Bool ODBFieldPage::commitPage( CommitPageReason _eReason )
501 if (!OMaybeListSelectionPage::commitPage(_eReason))
502 return sal_False;
504 implCommit(getDBFieldSetting());
506 return sal_True;
509 //.........................................................................
510 } // namespace dbp
511 //.........................................................................