bump product version to 6.3.0.0.beta1
[LibreOffice.git] / extensions / source / dbpilots / commonpagesdbp.cxx
blob760999d6e3f0f78c1db230f026ad4d1f00ea5d97
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <strings.hrc>
22 #include <bitmaps.hlst>
23 #include <componentmodule.hxx>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 #include <com/sun/star/sdb/XCompletedConnection.hpp>
26 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
27 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
28 #include <com/sun/star/sdbc/XConnection.hpp>
29 #include <com/sun/star/sdb/SQLContext.hpp>
30 #include <com/sun/star/sdbc/SQLWarning.hpp>
31 #include <com/sun/star/sdb/CommandType.hpp>
32 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
33 #include <tools/debug.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>
43 #include <osl/diagnose.h>
45 namespace dbp
49 using namespace ::com::sun::star;
50 using namespace ::com::sun::star::uno;
51 using namespace ::com::sun::star::lang;
52 using namespace ::com::sun::star::container;
53 using namespace ::com::sun::star::sdb;
54 using namespace ::com::sun::star::sdbc;
55 using namespace ::com::sun::star::sdbcx;
56 using namespace ::com::sun::star::task;
57 using namespace ::comphelper;
59 OTableSelectionPage::OTableSelectionPage(OControlWizard* _pParent)
60 :OControlWizardPage(_pParent, "TableSelectionPage", "modules/sabpilot/ui/tableselectionpage.ui")
62 get(m_pTable,"table");
63 get(m_pDatasource, "datasource");
64 get(m_pDatasourceLabel, "datasourcelabel");
65 get(m_pSearchDatabase, "search");
67 try
69 m_xDSContext = getContext().xDatasourceContext;
70 if (m_xDSContext.is())
71 fillListBox(*m_pDatasource, m_xDSContext->getElementNames());
73 catch (const Exception&)
75 OSL_FAIL("OTableSelectionPage::OTableSelectionPage: could not collect the data source names!");
78 m_pDatasource->SetSelectHdl(LINK(this, OTableSelectionPage, OnListboxSelection));
79 m_pTable->SetSelectHdl(LINK(this, OTableSelectionPage, OnListboxSelection));
80 m_pTable->SetDoubleClickHdl(LINK(this, OTableSelectionPage, OnListboxDoubleClicked));
81 m_pSearchDatabase->SetClickHdl(LINK(this, OTableSelectionPage, OnSearchClicked));
83 m_pDatasource->SetDropDownLineCount(10);
86 OTableSelectionPage::~OTableSelectionPage()
88 disposeOnce();
91 void OTableSelectionPage::dispose()
93 m_pDatasourceLabel.clear();
94 m_pDatasource.clear();
95 m_pSearchDatabase.clear();
96 m_pTable.clear();
97 OControlWizardPage::dispose();
100 void OTableSelectionPage::ActivatePage()
102 OControlWizardPage::ActivatePage();
103 m_pDatasource->GrabFocus();
107 bool OTableSelectionPage::canAdvance() const
109 if (!OControlWizardPage::canAdvance())
110 return false;
112 if (0 == m_pDatasource->GetSelectedEntryCount())
113 return false;
115 if (0 == m_pTable->GetSelectedEntryCount())
116 return false;
118 return true;
122 void OTableSelectionPage::initializePage()
124 OControlWizardPage::initializePage();
126 const OControlWizardContext& rContext = getContext();
129 OUString sDataSourceName;
130 rContext.xForm->getPropertyValue("DataSourceName") >>= sDataSourceName;
132 Reference< XConnection > xConnection;
133 bool bEmbedded = ::dbtools::isEmbeddedInDatabase( rContext.xForm, xConnection );
134 if ( bEmbedded )
136 VclVBox *_pSourceBox = get<VclVBox>("sourcebox");
137 _pSourceBox->Hide();
138 m_pDatasource->InsertEntry(sDataSourceName);
140 m_pDatasource->SelectEntry(sDataSourceName);
142 implFillTables(xConnection);
144 OUString sCommand;
145 OSL_VERIFY( rContext.xForm->getPropertyValue("Command") >>= sCommand );
146 sal_Int32 nCommandType = CommandType::TABLE;
147 OSL_VERIFY( rContext.xForm->getPropertyValue("CommandType") >>= nCommandType );
149 // search the entry of the given type with the given name
150 for ( sal_Int32 nLookup = 0; nLookup < m_pTable->GetEntryCount(); ++nLookup )
152 if (sCommand == m_pTable->GetEntry(nLookup))
154 if ( reinterpret_cast< sal_IntPtr >( m_pTable->GetEntryData( nLookup ) ) == nCommandType )
156 m_pTable->SelectEntryPos( nLookup );
157 break;
162 catch(const Exception&)
164 OSL_FAIL("OTableSelectionPage::initializePage: caught an exception!");
169 bool OTableSelectionPage::commitPage( ::svt::WizardTypes::CommitPageReason _eReason )
171 if (!OControlWizardPage::commitPage(_eReason))
172 return false;
174 const OControlWizardContext& rContext = getContext();
177 Reference< XConnection > xOldConn;
178 if ( !rContext.bEmbedded )
180 xOldConn = getFormConnection();
182 OUString sDataSource = m_pDatasource->GetSelectedEntry();
183 rContext.xForm->setPropertyValue("DataSourceName", makeAny( sDataSource ) );
185 OUString sCommand = m_pTable->GetSelectedEntry();
186 sal_Int32 nCommandType = reinterpret_cast< sal_IntPtr >( m_pTable->GetSelectedEntryData() );
188 rContext.xForm->setPropertyValue("Command", makeAny( sCommand ) );
189 rContext.xForm->setPropertyValue("CommandType", makeAny( nCommandType ) );
191 if ( !rContext.bEmbedded )
192 setFormConnection( xOldConn, false );
194 if (!updateContext())
195 return false;
197 catch(const Exception&)
199 OSL_FAIL("OTableSelectionPage::commitPage: caught an exception!");
202 return true;
206 IMPL_LINK_NOARG( OTableSelectionPage, OnSearchClicked, Button*, void )
208 ::sfx2::FileDialogHelper aFileDlg(
209 ui::dialogs::TemplateDescription::FILEOPEN_READONLY_VERSION,
210 FileDialogFlags::NONE, GetFrameWeld());
211 aFileDlg.SetDisplayDirectory( SvtPathOptions().GetWorkPath() );
213 std::shared_ptr<const SfxFilter> pFilter = SfxFilter::GetFilterByName("StarOffice XML (Base)");
214 OSL_ENSURE(pFilter,"Filter: StarOffice XML (Base) could not be found!");
215 if ( pFilter )
217 aFileDlg.AddFilter(pFilter->GetUIName(),pFilter->GetDefaultExtension());
220 if (ERRCODE_NONE == aFileDlg.Execute())
222 OUString sDataSourceName = aFileDlg.GetPath();
223 ::svt::OFileNotation aFileNotation(sDataSourceName);
224 sDataSourceName = aFileNotation.get(::svt::OFileNotation::N_SYSTEM);
225 m_pDatasource->InsertEntry(sDataSourceName);
226 m_pDatasource->SelectEntry(sDataSourceName);
227 LINK(this, OTableSelectionPage, OnListboxSelection).Call(*m_pDatasource);
231 IMPL_LINK( OTableSelectionPage, OnListboxDoubleClicked, ListBox&, _rBox, void )
233 if (_rBox.GetSelectedEntryCount())
234 getDialog()->travelNext();
238 IMPL_LINK( OTableSelectionPage, OnListboxSelection, ListBox&, _rBox, void )
240 if (m_pDatasource == &_rBox)
241 { // new data source selected
242 implFillTables();
245 updateDialogTravelUI();
249 namespace
251 void lcl_fillEntries( ListBox& _rListBox, const Sequence< OUString >& _rNames, const Image& _rImage, sal_Int32 _nCommandType )
253 for ( auto const & name : _rNames )
255 const sal_Int32 nPos = _rListBox.InsertEntry( name, _rImage );
256 _rListBox.SetEntryData( nPos, reinterpret_cast< void* >( _nCommandType ) );
262 void OTableSelectionPage::implFillTables(const Reference< XConnection >& _rxConn)
264 m_pTable->Clear();
266 WaitObject aWaitCursor(this);
268 // will be the table tables of the selected data source
269 Sequence< OUString > aTableNames;
270 Sequence< OUString > aQueryNames;
272 // connect to the data source
273 Any aSQLException;
274 Reference< XConnection > xConn = _rxConn;
275 if ( !xConn.is() )
277 if (!m_xDSContext.is())
278 return;
279 // connect to the data source
282 OUString sCurrentDatasource = m_pDatasource->GetSelectedEntry();
283 if (!sCurrentDatasource.isEmpty())
285 // obtain the DS object
286 Reference< XCompletedConnection > xDatasource;
287 // check if I know this one otherwise transform it into a file URL
288 if ( !m_xDSContext->hasByName(sCurrentDatasource) )
290 ::svt::OFileNotation aFileNotation(sCurrentDatasource);
291 sCurrentDatasource = aFileNotation.get(::svt::OFileNotation::N_URL);
294 if (m_xDSContext->getByName(sCurrentDatasource) >>= xDatasource)
295 { // connect
296 // get the default SDB interaction handler
297 Reference< XInteractionHandler > xHandler = getDialog()->getInteractionHandler(GetFrameWeld());
298 if (!xHandler.is() )
299 return;
300 xConn = xDatasource->connectWithCompletion(xHandler);
301 setFormConnection( xConn );
303 else
305 OSL_FAIL("OTableSelectionPage::implFillTables: invalid data source object returned by the context");
309 catch(const SQLContext& e) { aSQLException <<= e; }
310 catch(const SQLWarning& e) { aSQLException <<= e; }
311 catch(const SQLException& e) { aSQLException <<= e; }
312 catch (const Exception&)
314 OSL_FAIL("OTableSelectionPage::implFillTables: could not fill the table list!");
318 // will be the table tables of the selected data source
319 if ( xConn.is() )
323 // get the tables
324 Reference< XTablesSupplier > xSupplTables(xConn, UNO_QUERY);
325 if ( xSupplTables.is() )
327 Reference< XNameAccess > xTables(xSupplTables->getTables(), UNO_QUERY);
328 if (xTables.is())
329 aTableNames = xTables->getElementNames();
332 // and the queries
333 Reference< XQueriesSupplier > xSuppQueries( xConn, UNO_QUERY );
334 if ( xSuppQueries.is() )
336 Reference< XNameAccess > xQueries( xSuppQueries->getQueries(), UNO_QUERY );
337 if ( xQueries.is() )
338 aQueryNames = xQueries->getElementNames();
341 catch(const SQLContext& e) { aSQLException <<= e; }
342 catch(const SQLWarning& e) { aSQLException <<= e; }
343 catch(const SQLException& e) { aSQLException <<= e; }
344 catch (const Exception&)
346 OSL_FAIL("OTableSelectionPage::implFillTables: could not fill the table list!");
351 if ( aSQLException.hasValue() )
352 { // an SQLException (or derivee) was thrown ...
353 Reference< XInteractionRequest > xRequest = new OInteractionRequest(aSQLException);
356 // get the default SDB interaction handler
357 Reference< XInteractionHandler > xHandler = getDialog()->getInteractionHandler(GetFrameWeld());
358 if ( xHandler.is() )
359 xHandler->handle(xRequest);
361 catch(const Exception&) { }
362 return;
365 Image aTableImage(StockImage::Yes, BMP_TABLE);
366 Image aQueryImage(StockImage::Yes, BMP_QUERY);
368 lcl_fillEntries( *m_pTable, aTableNames, aTableImage, CommandType::TABLE );
369 lcl_fillEntries( *m_pTable, aQueryNames, aQueryImage, CommandType::QUERY );
373 OMaybeListSelectionPage::OMaybeListSelectionPage( OControlWizard* _pParent, const OString& _rID, const OUString& _rUIXMLDescription )
374 :OControlWizardPage(_pParent, _rID, _rUIXMLDescription)
375 ,m_pYes(nullptr)
376 ,m_pNo(nullptr)
377 ,m_pList(nullptr)
381 OMaybeListSelectionPage::~OMaybeListSelectionPage()
383 disposeOnce();
386 void OMaybeListSelectionPage::dispose()
388 m_pYes.clear();
389 m_pNo.clear();
390 m_pList.clear();
391 OControlWizardPage::dispose();
394 void OMaybeListSelectionPage::announceControls(RadioButton& _rYesButton, RadioButton& _rNoButton, ListBox& _rSelection)
396 m_pYes = &_rYesButton;
397 m_pNo = &_rNoButton;
398 m_pList = &_rSelection;
400 m_pYes->SetClickHdl(LINK(this, OMaybeListSelectionPage, OnRadioSelected));
401 m_pNo->SetClickHdl(LINK(this, OMaybeListSelectionPage, OnRadioSelected));
402 implEnableWindows();
405 IMPL_LINK_NOARG( OMaybeListSelectionPage, OnRadioSelected, Button*, void )
407 implEnableWindows();
411 void OMaybeListSelectionPage::implInitialize(const OUString& _rSelection)
413 DBG_ASSERT(m_pYes, "OMaybeListSelectionPage::implInitialize: no controls announced!");
414 bool bIsSelection = ! _rSelection.isEmpty();
415 m_pYes->Check(bIsSelection);
416 m_pNo->Check(!bIsSelection);
417 m_pList->Enable(bIsSelection);
419 m_pList->SelectEntry(bIsSelection ? _rSelection : OUString());
423 void OMaybeListSelectionPage::implCommit(OUString& _rSelection)
425 _rSelection = m_pYes->IsChecked() ? m_pList->GetSelectedEntry() : OUString();
429 void OMaybeListSelectionPage::implEnableWindows()
431 m_pList->Enable(m_pYes->IsChecked());
435 void OMaybeListSelectionPage::ActivatePage()
437 OControlWizardPage::ActivatePage();
439 DBG_ASSERT(m_pYes, "OMaybeListSelectionPage::ActivatePage: no controls announced!");
440 if (m_pYes->IsChecked())
441 m_pList->GrabFocus();
442 else
443 m_pNo->GrabFocus();
446 ODBFieldPage::ODBFieldPage( OControlWizard* _pParent )
447 :OMaybeListSelectionPage(_pParent, "OptionDBField", "modules/sabpilot/ui/optiondbfieldpage.ui")
449 get(m_pDescription, "explLabel");
450 get(m_pStoreYes, "yesRadiobutton");
451 get(m_pStoreNo, "noRadiobutton");
452 get(m_pStoreWhere, "storeInFieldCombobox");
453 SetText(compmodule::ModuleRes(RID_STR_OPTION_DB_FIELD_TITLE));
455 announceControls(*m_pStoreYes, *m_pStoreNo, *m_pStoreWhere);
456 m_pStoreWhere->SetDropDownLineCount(10);
459 ODBFieldPage::~ODBFieldPage()
461 disposeOnce();
464 void ODBFieldPage::dispose()
466 m_pDescription.clear();
467 m_pStoreYes.clear();
468 m_pStoreNo.clear();
469 m_pStoreWhere.clear();
470 OMaybeListSelectionPage::dispose();
473 void ODBFieldPage::initializePage()
475 OMaybeListSelectionPage::initializePage();
477 // fill the fields page
478 fillListBox(*m_pStoreWhere, getContext().aFieldNames);
480 implInitialize(getDBFieldSetting());
484 bool ODBFieldPage::commitPage( ::svt::WizardTypes::CommitPageReason _eReason )
486 if (!OMaybeListSelectionPage::commitPage(_eReason))
487 return false;
489 implCommit(getDBFieldSetting());
491 return true;
495 } // namespace dbp
498 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */