1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "commonpagesdbp.hxx"
21 #include "dbpresid.hrc"
22 #include "componentmodule.hxx"
23 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
24 #include <com/sun/star/sdb/XCompletedConnection.hpp>
25 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
26 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
27 #include <com/sun/star/sdbc/XConnection.hpp>
28 #include <com/sun/star/sdb/SQLContext.hpp>
29 #include <com/sun/star/sdbc/SQLWarning.hpp>
30 #include <com/sun/star/sdb/CommandType.hpp>
31 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
32 #include <tools/debug.hxx>
33 #include <svtools/localresaccess.hxx>
34 #include <comphelper/interaction.hxx>
35 #include <connectivity/dbtools.hxx>
36 #include <vcl/stdtext.hxx>
37 #include <vcl/waitobj.hxx>
38 #include <vcl/layout.hxx>
39 #include <sfx2/docfilt.hxx>
40 #include <unotools/pathoptions.hxx>
41 #include <sfx2/filedlghelper.hxx>
42 #include <svl/filenotation.hxx>
48 using namespace ::com::sun::star
;
49 using namespace ::com::sun::star::uno
;
50 using namespace ::com::sun::star::lang
;
51 using namespace ::com::sun::star::container
;
52 using namespace ::com::sun::star::sdb
;
53 using namespace ::com::sun::star::sdbc
;
54 using namespace ::com::sun::star::sdbcx
;
55 using namespace ::com::sun::star::task
;
56 using namespace ::comphelper
;
58 OTableSelectionPage::OTableSelectionPage(OControlWizard
* _pParent
)
59 :OControlWizardPage(_pParent
, "TableSelectionPage", "modules/sabpilot/ui/tableselectionpage.ui")
61 get(m_pTable
,"table");
62 get(m_pDatasource
, "datasource");
63 get(m_pDatasourceLabel
, "datasourcelabel");
64 get(m_pSearchDatabase
, "search");
66 implCollectDatasource();
68 m_pDatasource
->SetSelectHdl(LINK(this, OTableSelectionPage
, OnListboxSelection
));
69 m_pTable
->SetSelectHdl(LINK(this, OTableSelectionPage
, OnListboxSelection
));
70 m_pTable
->SetDoubleClickHdl(LINK(this, OTableSelectionPage
, OnListboxDoubleClicked
));
71 m_pSearchDatabase
->SetClickHdl(LINK(this, OTableSelectionPage
, OnSearchClicked
));
73 m_pDatasource
->SetDropDownLineCount(10);
76 OTableSelectionPage::~OTableSelectionPage()
81 void OTableSelectionPage::dispose()
83 m_pDatasourceLabel
.clear();
84 m_pDatasource
.clear();
85 m_pSearchDatabase
.clear();
87 OControlWizardPage::dispose();
90 void OTableSelectionPage::ActivatePage()
92 OControlWizardPage::ActivatePage();
93 m_pDatasource
->GrabFocus();
97 bool OTableSelectionPage::canAdvance() const
99 if (!OControlWizardPage::canAdvance())
102 if (0 == m_pDatasource
->GetSelectEntryCount())
105 if (0 == m_pTable
->GetSelectEntryCount())
112 void OTableSelectionPage::initializePage()
114 OControlWizardPage::initializePage();
116 const OControlWizardContext
& rContext
= getContext();
119 OUString sDataSourceName
;
120 rContext
.xForm
->getPropertyValue("DataSourceName") >>= sDataSourceName
;
122 Reference
< XConnection
> xConnection
;
123 bool bEmbedded
= ::dbtools::isEmbeddedInDatabase( rContext
.xForm
, xConnection
);
126 VclVBox
*_pSourceBox
= get
<VclVBox
>("sourcebox");
128 m_pDatasource
->InsertEntry(sDataSourceName
);
130 m_pDatasource
->SelectEntry(sDataSourceName
);
132 implFillTables(xConnection
);
135 OSL_VERIFY( rContext
.xForm
->getPropertyValue("Command") >>= sCommand
);
136 sal_Int32 nCommandType
= CommandType::TABLE
;
137 OSL_VERIFY( rContext
.xForm
->getPropertyValue("CommandType") >>= nCommandType
);
139 // search the entry of the given type with the given name
140 for ( sal_uInt16 nLookup
= 0; nLookup
< m_pTable
->GetEntryCount(); ++nLookup
)
142 if (sCommand
.equals(m_pTable
->GetEntry(nLookup
)))
144 if ( reinterpret_cast< sal_IntPtr
>( m_pTable
->GetEntryData( nLookup
) ) == nCommandType
)
146 m_pTable
->SelectEntryPos( nLookup
);
152 catch(const Exception
&)
154 OSL_FAIL("OTableSelectionPage::initializePage: caught an exception!");
159 bool OTableSelectionPage::commitPage( ::svt::WizardTypes::CommitPageReason _eReason
)
161 if (!OControlWizardPage::commitPage(_eReason
))
164 const OControlWizardContext
& rContext
= getContext();
167 Reference
< XConnection
> xOldConn
;
168 if ( !rContext
.bEmbedded
)
170 xOldConn
= getFormConnection();
172 OUString sDataSource
= m_pDatasource
->GetSelectEntry();
173 rContext
.xForm
->setPropertyValue("DataSourceName", makeAny( sDataSource
) );
175 OUString sCommand
= m_pTable
->GetSelectEntry();
176 sal_Int32 nCommandType
= reinterpret_cast< sal_IntPtr
>( m_pTable
->GetSelectEntryData() );
178 rContext
.xForm
->setPropertyValue("Command", makeAny( sCommand
) );
179 rContext
.xForm
->setPropertyValue("CommandType", makeAny( nCommandType
) );
181 if ( !rContext
.bEmbedded
)
182 setFormConnection( xOldConn
, false );
184 if (!updateContext())
187 catch(const Exception
&)
189 OSL_FAIL("OTableSelectionPage::commitPage: caught an exception!");
196 IMPL_LINK( OTableSelectionPage
, OnSearchClicked
, PushButton
*, /*_pButton*/ )
198 ::sfx2::FileDialogHelper
aFileDlg(
199 ui::dialogs::TemplateDescription::FILEOPEN_READONLY_VERSION
, 0);
200 aFileDlg
.SetDisplayDirectory( SvtPathOptions().GetWorkPath() );
202 const SfxFilter
* pFilter
= SfxFilter::GetFilterByName(OUString("StarOffice XML (Base)"));
203 OSL_ENSURE(pFilter
,"Filter: StarOffice XML (Base) could not be found!");
206 aFileDlg
.AddFilter(pFilter
->GetUIName(),pFilter
->GetDefaultExtension());
209 if (0 == aFileDlg
.Execute())
211 OUString sDataSourceName
= aFileDlg
.GetPath();
212 ::svt::OFileNotation
aFileNotation(sDataSourceName
);
213 sDataSourceName
= aFileNotation
.get(::svt::OFileNotation::N_SYSTEM
);
214 m_pDatasource
->InsertEntry(sDataSourceName
);
215 m_pDatasource
->SelectEntry(sDataSourceName
);
216 LINK(this, OTableSelectionPage
, OnListboxSelection
).Call(m_pDatasource
);
221 IMPL_LINK( OTableSelectionPage
, OnListboxDoubleClicked
, ListBox
*, _pBox
)
223 if (_pBox
->GetSelectEntryCount())
224 getDialog()->travelNext();
229 IMPL_LINK( OTableSelectionPage
, OnListboxSelection
, ListBox
*, _pBox
)
231 if (m_pDatasource
== _pBox
)
232 { // new data source selected
239 updateDialogTravelUI();
247 void lcl_fillEntries( ListBox
& _rListBox
, const Sequence
< OUString
>& _rNames
, const Image
& _rImage
, sal_Int32 _nCommandType
)
249 const OUString
* pNames
= _rNames
.getConstArray();
250 const OUString
* pNamesEnd
= _rNames
.getConstArray() + _rNames
.getLength();
252 while ( pNames
!= pNamesEnd
)
254 nPos
= _rListBox
.InsertEntry( *pNames
++, _rImage
);
255 _rListBox
.SetEntryData( nPos
, reinterpret_cast< void* >( _nCommandType
) );
261 void OTableSelectionPage::implFillTables(const Reference
< XConnection
>& _rxConn
)
265 WaitObject
aWaitCursor(this);
267 // will be the table tables of the selected data source
268 Sequence
< OUString
> aTableNames
;
269 Sequence
< OUString
> aQueryNames
;
271 // connect to the data source
273 Reference
< XConnection
> xConn
= _rxConn
;
276 if (!m_xDSContext
.is())
278 // connect to the data source
281 OUString sCurrentDatasource
= m_pDatasource
->GetSelectEntry();
282 if (!sCurrentDatasource
.isEmpty())
284 // obtain the DS object
285 Reference
< XCompletedConnection
> xDatasource
;
286 // check if I know this one otherwise transform it into a file URL
287 if ( !m_xDSContext
->hasByName(sCurrentDatasource
) )
289 ::svt::OFileNotation
aFileNotation(sCurrentDatasource
);
290 sCurrentDatasource
= aFileNotation
.get(::svt::OFileNotation::N_URL
);
293 if (m_xDSContext
->getByName(sCurrentDatasource
) >>= xDatasource
)
295 // get the default SDB interaction handler
296 Reference
< XInteractionHandler
> xHandler
= getDialog()->getInteractionHandler(this);
299 xConn
= xDatasource
->connectWithCompletion(xHandler
);
300 setFormConnection( xConn
);
304 OSL_FAIL("OTableSelectionPage::implFillTables: invalid data source object returned by the context");
308 catch(const SQLContext
& e
) { aSQLException
<<= e
; }
309 catch(const SQLWarning
& e
) { aSQLException
<<= e
; }
310 catch(const SQLException
& e
) { aSQLException
<<= e
; }
311 catch (const Exception
&)
313 OSL_FAIL("OTableSelectionPage::implFillTables: could not fill the table list!");
317 // will be the table tables of the selected data source
323 Reference
< XTablesSupplier
> xSupplTables(xConn
, UNO_QUERY
);
324 if ( xSupplTables
.is() )
326 Reference
< XNameAccess
> xTables(xSupplTables
->getTables(), UNO_QUERY
);
328 aTableNames
= xTables
->getElementNames();
332 Reference
< XQueriesSupplier
> xSuppQueries( xConn
, UNO_QUERY
);
333 if ( xSuppQueries
.is() )
335 Reference
< XNameAccess
> xQueries( xSuppQueries
->getQueries(), UNO_QUERY
);
337 aQueryNames
= xQueries
->getElementNames();
340 catch(const SQLContext
& e
) { aSQLException
<<= e
; }
341 catch(const SQLWarning
& e
) { aSQLException
<<= e
; }
342 catch(const SQLException
& e
) { aSQLException
<<= e
; }
343 catch (const Exception
&)
345 OSL_FAIL("OTableSelectionPage::implFillTables: could not fill the table list!");
350 if ( aSQLException
.hasValue() )
351 { // an SQLException (or derivee) was thrown ...
352 Reference
< XInteractionRequest
> xRequest
= new OInteractionRequest(aSQLException
);
355 // get the default SDB interaction handler
356 Reference
< XInteractionHandler
> xHandler
= getDialog()->getInteractionHandler(this);
358 xHandler
->handle(xRequest
);
360 catch(const Exception
&) { }
364 Image aTableImage
, aQueryImage
;
365 aTableImage
= Image( ModuleRes( IMG_TABLE
) );
366 aQueryImage
= Image( ModuleRes( IMG_QUERY
) );
368 lcl_fillEntries( *m_pTable
, aTableNames
, aTableImage
, CommandType::TABLE
);
369 lcl_fillEntries( *m_pTable
, aQueryNames
, aQueryImage
, CommandType::QUERY
);
373 void OTableSelectionPage::implCollectDatasource()
377 m_xDSContext
= getContext().xDatasourceContext
;
378 if (m_xDSContext
.is())
379 fillListBox(*m_pDatasource
, m_xDSContext
->getElementNames());
381 catch (const Exception
&)
383 OSL_FAIL("OTableSelectionPage::implCollectDatasource: could not collect the data source names!");
387 OMaybeListSelectionPage::OMaybeListSelectionPage( OControlWizard
* _pParent
, const OString
& _rID
, const OUString
& _rUIXMLDescription
)
388 :OControlWizardPage(_pParent
, _rID
, _rUIXMLDescription
)
395 OMaybeListSelectionPage::~OMaybeListSelectionPage()
400 void OMaybeListSelectionPage::dispose()
405 OControlWizardPage::dispose();
408 void OMaybeListSelectionPage::announceControls(RadioButton
& _rYesButton
, RadioButton
& _rNoButton
, ListBox
& _rSelection
)
410 m_pYes
= &_rYesButton
;
412 m_pList
= &_rSelection
;
414 m_pYes
->SetClickHdl(LINK(this, OMaybeListSelectionPage
, OnRadioSelected
));
415 m_pNo
->SetClickHdl(LINK(this, OMaybeListSelectionPage
, OnRadioSelected
));
419 IMPL_LINK( OMaybeListSelectionPage
, OnRadioSelected
, RadioButton
*, /*NOTINTERESTEDIN*/ )
426 void OMaybeListSelectionPage::implInitialize(const OUString
& _rSelection
)
428 DBG_ASSERT(m_pYes
, "OMaybeListSelectionPage::implInitialize: no controls announced!");
429 bool bIsSelection
= ! _rSelection
.isEmpty();
430 m_pYes
->Check(bIsSelection
);
431 m_pNo
->Check(!bIsSelection
);
432 m_pList
->Enable(bIsSelection
);
434 m_pList
->SelectEntry(bIsSelection
? _rSelection
: OUString());
438 void OMaybeListSelectionPage::implCommit(OUString
& _rSelection
)
440 _rSelection
= m_pYes
->IsChecked() ? m_pList
->GetSelectEntry() : OUString();
444 void OMaybeListSelectionPage::implEnableWindows()
446 m_pList
->Enable(m_pYes
->IsChecked());
450 void OMaybeListSelectionPage::ActivatePage()
452 OControlWizardPage::ActivatePage();
454 DBG_ASSERT(m_pYes
, "OMaybeListSelectionPage::ActivatePage: no controls announced!");
455 if (m_pYes
->IsChecked())
456 m_pList
->GrabFocus();
461 ODBFieldPage::ODBFieldPage( OControlWizard
* _pParent
)
462 :OMaybeListSelectionPage(_pParent
, "OptionDBField", "modules/sabpilot/ui/optiondbfieldpage.ui")
464 get(m_pDescription
, "explLabel");
465 get(m_pStoreYes
, "yesRadiobutton");
466 get(m_pStoreNo
, "noRadiobutton");
467 get(m_pStoreWhere
, "storeInFieldCombobox");
468 SetText(ModuleRes(RID_STR_OPTION_DB_FIELD_TITLE
));
470 announceControls(*m_pStoreYes
, *m_pStoreNo
, *m_pStoreWhere
);
471 m_pStoreWhere
->SetDropDownLineCount(10);
474 ODBFieldPage::~ODBFieldPage()
479 void ODBFieldPage::dispose()
481 m_pDescription
.clear();
484 m_pStoreWhere
.clear();
485 OMaybeListSelectionPage::dispose();
488 void ODBFieldPage::initializePage()
490 OMaybeListSelectionPage::initializePage();
492 // fill the fields page
493 fillListBox(*m_pStoreWhere
, getContext().aFieldNames
);
495 implInitialize(getDBFieldSetting());
499 bool ODBFieldPage::commitPage( ::svt::WizardTypes::CommitPageReason _eReason
)
501 if (!OMaybeListSelectionPage::commitPage(_eReason
))
504 implCommit(getDBFieldSetting());
513 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */