merged tag ooo/OOO330_m14
[LibreOffice.git] / extensions / source / dbpilots / commonpagesdbp.cxx
blob88d8e2eb352e93b50c6a747983fdb31968144579
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 #include "commonpagesdbp.hxx"
31 #ifndef _EXTENSIONS_DBP_DBPRESID_HRC_
32 #include "dbpresid.hrc"
33 #endif
34 #include "componentmodule.hxx"
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/sdb/XCompletedConnection.hpp>
37 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
38 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
39 #include <com/sun/star/sdbc/XConnection.hpp>
40 #include <com/sun/star/sdb/SQLContext.hpp>
41 #include <com/sun/star/sdbc/SQLWarning.hpp>
42 #include <com/sun/star/sdb/CommandType.hpp>
43 #include <tools/debug.hxx>
44 #include <svtools/localresaccess.hxx>
45 #include <comphelper/interaction.hxx>
46 #include <connectivity/dbtools.hxx>
47 #include <vcl/stdtext.hxx>
48 #include <vcl/waitobj.hxx>
49 #include <sfx2/docfilt.hxx>
50 #include <unotools/pathoptions.hxx>
51 #include <sfx2/filedlghelper.hxx>
52 #ifndef SVTOOLS_FILENOTATION_HXX_
53 #include <svl/filenotation.hxx>
54 #endif
55 //.........................................................................
56 namespace dbp
58 //.........................................................................
60 using namespace ::com::sun::star::uno;
61 using namespace ::com::sun::star::lang;
62 using namespace ::com::sun::star::container;
63 using namespace ::com::sun::star::sdb;
64 using namespace ::com::sun::star::sdbc;
65 using namespace ::com::sun::star::sdbcx;
66 using namespace ::com::sun::star::task;
67 using namespace ::comphelper;
69 //=====================================================================
70 //= OTableSelectionPage
71 //=====================================================================
72 //---------------------------------------------------------------------
73 OTableSelectionPage::OTableSelectionPage(OControlWizard* _pParent)
74 :OControlWizardPage(_pParent, ModuleRes(RID_PAGE_TABLESELECTION))
75 ,m_aData (this, ModuleRes(FL_DATA))
76 ,m_aExplanation (this, ModuleRes(FT_EXPLANATION))
77 ,m_aDatasourceLabel (this, ModuleRes(FT_DATASOURCE))
78 ,m_aDatasource (this, ModuleRes(LB_DATASOURCE))
79 ,m_aSearchDatabase (this, ModuleRes(PB_FORMDATASOURCE))
80 ,m_aTableLabel (this, ModuleRes(FT_TABLE))
81 ,m_aTable (this, ModuleRes(LB_TABLE))
83 FreeResource();
85 implCollectDatasource();
87 m_aDatasource.SetSelectHdl(LINK(this, OTableSelectionPage, OnListboxSelection));
88 m_aTable.SetSelectHdl(LINK(this, OTableSelectionPage, OnListboxSelection));
89 m_aTable.SetDoubleClickHdl(LINK(this, OTableSelectionPage, OnListboxDoubleClicked));
90 m_aSearchDatabase.SetClickHdl(LINK(this, OTableSelectionPage, OnSearchClicked));
92 m_aDatasource.SetDropDownLineCount(10);
95 //---------------------------------------------------------------------
96 void OTableSelectionPage::ActivatePage()
98 OControlWizardPage::ActivatePage();
99 m_aDatasource.GrabFocus();
102 //---------------------------------------------------------------------
103 bool OTableSelectionPage::canAdvance() const
105 if (!OControlWizardPage::canAdvance())
106 return false;
108 if (0 == m_aDatasource.GetSelectEntryCount())
109 return false;
111 if (0 == m_aTable.GetSelectEntryCount())
112 return false;
114 return sal_True;
117 //---------------------------------------------------------------------
118 void OTableSelectionPage::initializePage()
120 OControlWizardPage::initializePage();
122 const OControlWizardContext& rContext = getContext();
125 ::rtl::OUString sDataSourceName;
126 rContext.xForm->getPropertyValue(::rtl::OUString::createFromAscii("DataSourceName")) >>= sDataSourceName;
128 Reference< XConnection > xConnection;
129 bool bEmbedded = ::dbtools::isEmbeddedInDatabase( rContext.xForm, xConnection );
130 if ( bEmbedded )
132 m_aDatasource.Hide();
133 m_aDatasourceLabel.Hide();
134 m_aSearchDatabase.Hide();
135 m_aTableLabel.SetPosPixel(m_aDatasourceLabel.GetPosPixel());
136 m_aTable.SetPosPixel(m_aDatasource.GetPosPixel());
137 m_aDatasource.InsertEntry(sDataSourceName);
139 m_aDatasource.SelectEntry(sDataSourceName);
141 implFillTables(xConnection);
143 ::rtl::OUString sCommand;
144 OSL_VERIFY( rContext.xForm->getPropertyValue( ::rtl::OUString::createFromAscii("Command") ) >>= sCommand );
145 sal_Int32 nCommandType = CommandType::TABLE;
146 OSL_VERIFY( rContext.xForm->getPropertyValue( ::rtl::OUString::createFromAscii("CommandType") ) >>= nCommandType );
148 // search the entry of the given type with the given name
149 XubString sLookup( sCommand );
150 for ( USHORT nLookup = 0; nLookup < m_aTable.GetEntryCount(); ++nLookup )
152 if ( m_aTable.GetEntry( nLookup ) == sLookup )
153 if ( reinterpret_cast< sal_IntPtr >( m_aTable.GetEntryData( nLookup ) ) == nCommandType )
155 m_aTable.SelectEntryPos( nLookup );
156 break;
160 catch(Exception&)
162 DBG_ERROR("OTableSelectionPage::initializePage: caught an exception!");
166 //---------------------------------------------------------------------
167 sal_Bool OTableSelectionPage::commitPage( ::svt::WizardTypes::CommitPageReason _eReason )
169 if (!OControlWizardPage::commitPage(_eReason))
170 return sal_False;
172 const OControlWizardContext& rContext = getContext();
175 Reference< XConnection > xOldConn;
176 if ( !rContext.bEmbedded )
178 xOldConn = getFormConnection();
180 ::rtl::OUString sDataSource = m_aDatasource.GetSelectEntry();
181 rContext.xForm->setPropertyValue( ::rtl::OUString::createFromAscii("DataSourceName"), makeAny( sDataSource ) );
183 ::rtl::OUString sCommand = m_aTable.GetSelectEntry();
184 sal_Int32 nCommandType = reinterpret_cast< sal_IntPtr >( m_aTable.GetEntryData( m_aTable.GetSelectEntryPos() ) );
186 rContext.xForm->setPropertyValue( ::rtl::OUString::createFromAscii("Command"), makeAny( sCommand ) );
187 rContext.xForm->setPropertyValue( ::rtl::OUString::createFromAscii("CommandType"), makeAny( nCommandType ) );
189 if ( !rContext.bEmbedded )
190 setFormConnection( xOldConn, sal_False );
192 if (!updateContext())
193 return sal_False;
195 catch(Exception&)
197 DBG_ERROR("OTableSelectionPage::commitPage: caught an exception!");
200 return sal_True;
203 //---------------------------------------------------------------------
204 IMPL_LINK( OTableSelectionPage, OnSearchClicked, PushButton*, /*_pButton*/ )
206 ::sfx2::FileDialogHelper aFileDlg(WB_3DLOOK);
207 aFileDlg.SetDisplayDirectory( SvtPathOptions().GetWorkPath() );
209 static const String s_sDatabaseType = String::CreateFromAscii("StarOffice XML (Base)");
210 const SfxFilter* pFilter = SfxFilter::GetFilterByName( s_sDatabaseType);
211 OSL_ENSURE(pFilter,"Filter: StarOffice XML (Base) could not be found!");
212 if ( pFilter )
214 aFileDlg.AddFilter(pFilter->GetUIName(),pFilter->GetDefaultExtension());
217 if (0 == aFileDlg.Execute())
219 String sDataSourceName = aFileDlg.GetPath();
220 ::svt::OFileNotation aFileNotation(sDataSourceName);
221 sDataSourceName = aFileNotation.get(::svt::OFileNotation::N_SYSTEM);
222 m_aDatasource.InsertEntry(sDataSourceName);
223 m_aDatasource.SelectEntry(sDataSourceName);
224 LINK(this, OTableSelectionPage, OnListboxSelection).Call(&m_aDatasource);
226 return 0L;
228 //---------------------------------------------------------------------
229 IMPL_LINK( OTableSelectionPage, OnListboxDoubleClicked, ListBox*, _pBox )
231 if (_pBox->GetSelectEntryCount())
232 getDialog()->travelNext();
233 return 0L;
236 //---------------------------------------------------------------------
237 IMPL_LINK( OTableSelectionPage, OnListboxSelection, ListBox*, _pBox )
239 if (&m_aDatasource == _pBox)
240 { // new data source selected
241 implFillTables();
243 else
247 updateDialogTravelUI();
249 return 0L;
252 //---------------------------------------------------------------------
253 namespace
255 void lcl_fillEntries( ListBox& _rListBox, const Sequence< ::rtl::OUString >& _rNames, const Image& _rImage, sal_Int32 _nCommandType )
257 const ::rtl::OUString* pNames = _rNames.getConstArray();
258 const ::rtl::OUString* pNamesEnd = _rNames.getConstArray() + _rNames.getLength();
259 sal_uInt16 nPos = 0;
260 while ( pNames != pNamesEnd )
262 nPos = _rListBox.InsertEntry( *pNames++, _rImage );
263 _rListBox.SetEntryData( nPos, reinterpret_cast< void* >( _nCommandType ) );
268 //---------------------------------------------------------------------
269 void OTableSelectionPage::implFillTables(const Reference< XConnection >& _rxConn)
271 m_aTable.Clear();
273 WaitObject aWaitCursor(this);
275 // will be the table tables of the selected data source
276 Sequence< ::rtl::OUString > aTableNames;
277 Sequence< ::rtl::OUString > aQueryNames;
279 // connect to the data source
280 Any aSQLException;
281 Reference< XConnection > xConn = _rxConn;
282 if ( !xConn.is() )
284 if (!m_xDSContext.is())
285 return;
286 // connect to the data source
289 ::rtl::OUString sCurrentDatasource = m_aDatasource.GetSelectEntry();
290 if (sCurrentDatasource.getLength())
292 // obtain the DS object
293 Reference< XCompletedConnection > xDatasource;
294 // check if I know this one otherwise transform it into a file URL
295 if ( !m_xDSContext->hasByName(sCurrentDatasource) )
297 ::svt::OFileNotation aFileNotation(sCurrentDatasource);
298 sCurrentDatasource = aFileNotation.get(::svt::OFileNotation::N_URL);
301 if (m_xDSContext->getByName(sCurrentDatasource) >>= xDatasource)
302 { // connect
303 // get the default SDB interaction handler
304 Reference< XInteractionHandler > xHandler = getDialog()->getInteractionHandler(this);
305 if (!xHandler.is() )
306 return;
307 xConn = xDatasource->connectWithCompletion(xHandler);
308 setFormConnection( xConn );
310 else
312 DBG_ERROR("OTableSelectionPage::implFillTables: invalid data source object returned by the context");
316 catch(SQLContext& e) { aSQLException <<= e; }
317 catch(SQLWarning& e) { aSQLException <<= e; }
318 catch(SQLException& e) { aSQLException <<= e; }
319 catch (Exception&)
321 DBG_ERROR("OTableSelectionPage::implFillTables: could not fill the table list!");
325 // will be the table tables of the selected data source
326 if ( xConn.is() )
330 // get the tables
331 Reference< XTablesSupplier > xSupplTables(xConn, UNO_QUERY);
332 if ( xSupplTables.is() )
334 Reference< XNameAccess > xTables(xSupplTables->getTables(), UNO_QUERY);
335 if (xTables.is())
336 aTableNames = xTables->getElementNames();
339 // and the queries
340 Reference< XQueriesSupplier > xSuppQueries( xConn, UNO_QUERY );
341 if ( xSuppQueries.is() )
343 Reference< XNameAccess > xQueries( xSuppQueries->getQueries(), UNO_QUERY );
344 if ( xQueries.is() )
345 aQueryNames = xQueries->getElementNames();
348 catch(SQLContext& e) { aSQLException <<= e; }
349 catch(SQLWarning& e) { aSQLException <<= e; }
350 catch(SQLException& e) { aSQLException <<= e; }
351 catch (Exception&)
353 DBG_ERROR("OTableSelectionPage::implFillTables: could not fill the table list!");
358 if ( aSQLException.hasValue() )
359 { // an SQLException (or derivee) was thrown ...
360 Reference< XInteractionRequest > xRequest = new OInteractionRequest(aSQLException);
363 // get the default SDB interaction handler
364 Reference< XInteractionHandler > xHandler = getDialog()->getInteractionHandler(this);
365 if ( xHandler.is() )
366 xHandler->handle(xRequest);
368 catch(Exception&) { }
369 return;
372 Image aTableImage, aQueryImage;
374 ::svt::OLocalResourceAccess aLocalResAccess( ModuleRes( RID_PAGE_TABLESELECTION ), RSC_TABPAGE );
376 bool bIsHiContrast = m_aTable.GetSettings().GetStyleSettings().GetHighContrastMode();
377 aTableImage = Image( ModuleRes( bIsHiContrast ? IMG_TABLE_HC : IMG_TABLE ) );
378 aQueryImage = Image( ModuleRes( bIsHiContrast ? IMG_QUERY_HC : IMG_QUERY ) );
380 lcl_fillEntries( m_aTable, aTableNames, aTableImage, CommandType::TABLE );
381 lcl_fillEntries( m_aTable, aQueryNames, aQueryImage, CommandType::QUERY );
384 //---------------------------------------------------------------------
385 void OTableSelectionPage::implCollectDatasource()
389 m_xDSContext = getContext().xDatasourceContext;
390 if (m_xDSContext.is())
391 fillListBox(m_aDatasource, m_xDSContext->getElementNames());
393 catch (Exception&)
395 DBG_ERROR("OTableSelectionPage::implCollectDatasource: could not collect the data source names!");
399 //=====================================================================
400 //= OMaybeListSelectionPage
401 //=====================================================================
402 //---------------------------------------------------------------------
403 OMaybeListSelectionPage::OMaybeListSelectionPage( OControlWizard* _pParent, const ResId& _rId )
404 :OControlWizardPage(_pParent, _rId)
405 ,m_pYes(NULL)
406 ,m_pNo(NULL)
407 ,m_pList(NULL)
411 //---------------------------------------------------------------------
412 void OMaybeListSelectionPage::announceControls(RadioButton& _rYesButton, RadioButton& _rNoButton, ListBox& _rSelection)
414 m_pYes = &_rYesButton;
415 m_pNo = &_rNoButton;
416 m_pList = &_rSelection;
418 m_pYes->SetClickHdl(LINK(this, OMaybeListSelectionPage, OnRadioSelected));
419 m_pNo->SetClickHdl(LINK(this, OMaybeListSelectionPage, OnRadioSelected));
420 implEnableWindows();
423 //---------------------------------------------------------------------
424 IMPL_LINK( OMaybeListSelectionPage, OnRadioSelected, RadioButton*, /*NOTINTERESTEDIN*/ )
426 implEnableWindows();
427 return 0L;
430 //---------------------------------------------------------------------
431 void OMaybeListSelectionPage::implInitialize(const String& _rSelection)
433 DBG_ASSERT(m_pYes, "OMaybeListSelectionPage::implInitialize: no controls announced!");
434 sal_Bool bIsSelection = (0 != _rSelection.Len());
435 m_pYes->Check(bIsSelection);
436 m_pNo->Check(!bIsSelection);
437 m_pList->Enable(bIsSelection);
439 m_pList->SelectEntry(bIsSelection ? _rSelection : String());
442 //---------------------------------------------------------------------
443 void OMaybeListSelectionPage::implCommit(String& _rSelection)
445 _rSelection = m_pYes->IsChecked() ? m_pList->GetSelectEntry() : String();
448 //---------------------------------------------------------------------
449 void OMaybeListSelectionPage::implEnableWindows()
451 m_pList->Enable(m_pYes->IsChecked());
454 //---------------------------------------------------------------------
455 void OMaybeListSelectionPage::ActivatePage()
457 OControlWizardPage::ActivatePage();
459 DBG_ASSERT(m_pYes, "OMaybeListSelectionPage::ActivatePage: no controls announced!");
460 if (m_pYes->IsChecked())
461 m_pList->GrabFocus();
462 else
463 m_pNo->GrabFocus();
466 //=====================================================================
467 //= ODBFieldPage
468 //=====================================================================
469 //---------------------------------------------------------------------
470 ODBFieldPage::ODBFieldPage( OControlWizard* _pParent )
471 :OMaybeListSelectionPage(_pParent, ModuleRes(RID_PAGE_OPTION_DBFIELD))
472 ,m_aFrame (this, ModuleRes(FL_DATABASEFIELD_EXPL))
473 ,m_aDescription (this, ModuleRes(FT_DATABASEFIELD_EXPL))
474 ,m_aQuestion (this, ModuleRes(FT_DATABASEFIELD_QUEST))
475 ,m_aStoreYes (this, ModuleRes(RB_STOREINFIELD_YES))
476 ,m_aStoreNo (this, ModuleRes(LB_STOREINFIELD))
477 ,m_aStoreWhere (this, ModuleRes(RB_STOREINFIELD_NO))
479 FreeResource();
480 announceControls(m_aStoreYes, m_aStoreNo, m_aStoreWhere);
481 m_aStoreWhere.SetDropDownLineCount(10);
484 //---------------------------------------------------------------------
485 void ODBFieldPage::initializePage()
487 OMaybeListSelectionPage::initializePage();
489 // fill the fields page
490 fillListBox(m_aStoreWhere, getContext().aFieldNames);
492 implInitialize(getDBFieldSetting());
495 //---------------------------------------------------------------------
496 sal_Bool ODBFieldPage::commitPage( ::svt::WizardTypes::CommitPageReason _eReason )
498 if (!OMaybeListSelectionPage::commitPage(_eReason))
499 return sal_False;
501 implCommit(getDBFieldSetting());
503 return sal_True;
506 //.........................................................................
507 } // namespace dbp
508 //.........................................................................