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 <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/diagnose_ex.hxx>
35 #include <comphelper/interaction.hxx>
36 #include <connectivity/dbtools.hxx>
37 #include <sfx2/docfilt.hxx>
38 #include <unotools/pathoptions.hxx>
39 #include <sfx2/filedlghelper.hxx>
40 #include <svl/filenotation.hxx>
41 #include <osl/diagnose.h>
47 using namespace ::com::sun::star
;
48 using namespace ::com::sun::star::uno
;
49 using namespace ::com::sun::star::container
;
50 using namespace ::com::sun::star::sdb
;
51 using namespace ::com::sun::star::sdbc
;
52 using namespace ::com::sun::star::sdbcx
;
53 using namespace ::com::sun::star::task
;
54 using namespace ::comphelper
;
56 OTableSelectionPage::OTableSelectionPage(weld::Container
* pPage
, OControlWizard
* pWizard
)
57 : OControlWizardPage(pPage
, pWizard
, u
"modules/sabpilot/ui/tableselectionpage.ui"_ustr
, u
"TableSelectionPage"_ustr
)
58 , m_xTable(m_xBuilder
->weld_tree_view(u
"table"_ustr
))
59 , m_xDatasource(m_xBuilder
->weld_tree_view(u
"datasource"_ustr
))
60 , m_xSearchDatabase(m_xBuilder
->weld_button(u
"search"_ustr
))
61 , m_xSourceBox(m_xBuilder
->weld_container(u
"sourcebox"_ustr
))
65 m_xDSContext
= getContext().xDatasourceContext
;
66 if (m_xDSContext
.is())
67 fillListBox(*m_xDatasource
, m_xDSContext
->getElementNames());
69 catch (const Exception
&)
71 OSL_FAIL("OTableSelectionPage::OTableSelectionPage: could not collect the data source names!");
74 m_xDatasource
->connect_selection_changed(
75 LINK(this, OTableSelectionPage
, OnListboxSelection
));
76 m_xTable
->connect_selection_changed(LINK(this, OTableSelectionPage
, OnListboxSelection
));
77 m_xTable
->connect_row_activated(LINK(this, OTableSelectionPage
, OnListboxDoubleClicked
));
78 m_xSearchDatabase
->connect_clicked(LINK(this, OTableSelectionPage
, OnSearchClicked
));
81 OTableSelectionPage::~OTableSelectionPage()
85 void OTableSelectionPage::Activate()
87 OControlWizardPage::Activate();
88 m_xDatasource
->grab_focus();
91 bool OTableSelectionPage::canAdvance() const
93 if (!OControlWizardPage::canAdvance())
96 if (0 == m_xDatasource
->count_selected_rows())
99 if (0 == m_xTable
->count_selected_rows())
105 void OTableSelectionPage::initializePage()
107 OControlWizardPage::initializePage();
109 const OControlWizardContext
& rContext
= getContext();
112 OUString sDataSourceName
;
113 rContext
.xForm
->getPropertyValue(u
"DataSourceName"_ustr
) >>= sDataSourceName
;
115 Reference
< XConnection
> xConnection
;
116 bool bEmbedded
= ::dbtools::isEmbeddedInDatabase( rContext
.xForm
, xConnection
);
119 m_xSourceBox
->hide();
120 m_xDatasource
->append_text(sDataSourceName
);
122 m_xDatasource
->select_text(sDataSourceName
);
124 implFillTables(xConnection
);
127 OSL_VERIFY( rContext
.xForm
->getPropertyValue(u
"Command"_ustr
) >>= sCommand
);
128 sal_Int32 nCommandType
= CommandType::TABLE
;
129 OSL_VERIFY( rContext
.xForm
->getPropertyValue(u
"CommandType"_ustr
) >>= nCommandType
);
131 // search the entry of the given type with the given name
132 for (sal_Int32 nLookup
= 0; nLookup
< m_xTable
->n_children(); ++nLookup
)
134 if (sCommand
== m_xTable
->get_text(nLookup
))
136 if (m_xTable
->get_id(nLookup
).toInt32() == nCommandType
)
138 m_xTable
->select( nLookup
);
144 catch(const Exception
&)
146 TOOLS_WARN_EXCEPTION("extensions.abpilot", "OTableSelectionPage::initializePage");
150 bool OTableSelectionPage::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason
)
152 if (!OControlWizardPage::commitPage(_eReason
))
155 const OControlWizardContext
& rContext
= getContext();
158 Reference
< XConnection
> xOldConn
;
159 if ( !rContext
.bEmbedded
)
161 xOldConn
= getFormConnection();
163 OUString sDataSource
= m_xDatasource
->get_selected_text();
164 rContext
.xForm
->setPropertyValue(u
"DataSourceName"_ustr
, Any( sDataSource
) );
166 OUString sCommand
= m_xTable
->get_selected_text();
167 sal_Int32 nCommandType
= m_xTable
->get_selected_id().toInt32();
169 rContext
.xForm
->setPropertyValue(u
"Command"_ustr
, Any( sCommand
) );
170 rContext
.xForm
->setPropertyValue(u
"CommandType"_ustr
, Any( nCommandType
) );
172 if ( !rContext
.bEmbedded
)
173 setFormConnection( xOldConn
, false );
175 if (!updateContext())
178 catch(const Exception
&)
180 TOOLS_WARN_EXCEPTION("extensions.dbpilots", "OTableSelectionPage::commitPage");
186 IMPL_LINK_NOARG( OTableSelectionPage
, OnSearchClicked
, weld::Button
&, void )
188 ::sfx2::FileDialogHelper
aFileDlg(
189 ui::dialogs::TemplateDescription::FILEOPEN_READONLY_VERSION
,
190 FileDialogFlags::NONE
, getDialog()->getDialog());
191 aFileDlg
.SetDisplayDirectory( SvtPathOptions().GetWorkPath() );
193 std::shared_ptr
<const SfxFilter
> pFilter
= SfxFilter::GetFilterByName(u
"StarOffice XML (Base)"_ustr
);
194 OSL_ENSURE(pFilter
,"Filter: StarOffice XML (Base) could not be found!");
197 aFileDlg
.AddFilter(pFilter
->GetUIName(),pFilter
->GetDefaultExtension());
200 if (ERRCODE_NONE
== aFileDlg
.Execute())
202 OUString sDataSourceName
= aFileDlg
.GetPath();
203 ::svt::OFileNotation
aFileNotation(sDataSourceName
);
204 sDataSourceName
= aFileNotation
.get(::svt::OFileNotation::N_SYSTEM
);
205 m_xDatasource
->append_text(sDataSourceName
);
206 m_xDatasource
->select_text(sDataSourceName
);
207 LINK(this, OTableSelectionPage
, OnListboxSelection
).Call(*m_xDatasource
);
211 IMPL_LINK(OTableSelectionPage
, OnListboxDoubleClicked
, weld::TreeView
&, _rBox
, bool)
213 if (_rBox
.count_selected_rows())
214 getDialog()->travelNext();
218 IMPL_LINK(OTableSelectionPage
, OnListboxSelection
, weld::TreeView
&, _rBox
, void)
220 if (m_xDatasource
.get() == &_rBox
)
221 { // new data source selected
225 updateDialogTravelUI();
230 void lcl_fillEntries(weld::TreeView
& rListBox
, const Sequence
<OUString
>& rNames
, const OUString
& rImage
, sal_Int32 nCommandType
)
232 for (auto const & name
: rNames
)
234 rListBox
.append(OUString::number(nCommandType
), name
, rImage
);
239 void OTableSelectionPage::implFillTables(const Reference
< XConnection
>& _rxConn
)
243 weld::WaitObject
aWaitCursor(getDialog()->getDialog());
245 // will be the table tables of the selected data source
246 Sequence
< OUString
> aTableNames
;
247 Sequence
< OUString
> aQueryNames
;
249 // connect to the data source
251 Reference
< XConnection
> xConn
= _rxConn
;
254 if (!m_xDSContext
.is())
256 // connect to the data source
259 OUString sCurrentDatasource
= m_xDatasource
->get_selected_text();
260 if (!sCurrentDatasource
.isEmpty())
262 // obtain the DS object
263 Reference
< XCompletedConnection
> xDatasource
;
264 // check if I know this one otherwise transform it into a file URL
265 if ( !m_xDSContext
->hasByName(sCurrentDatasource
) )
267 ::svt::OFileNotation
aFileNotation(sCurrentDatasource
);
268 sCurrentDatasource
= aFileNotation
.get(::svt::OFileNotation::N_URL
);
271 if (m_xDSContext
->getByName(sCurrentDatasource
) >>= xDatasource
)
273 // get the default SDB interaction handler
274 Reference
< XInteractionHandler
> xHandler
= getDialog()->getInteractionHandler(getDialog()->getDialog());
277 xConn
= xDatasource
->connectWithCompletion(xHandler
);
278 setFormConnection( xConn
);
282 OSL_FAIL("OTableSelectionPage::implFillTables: invalid data source object returned by the context");
286 catch(const SQLContext
& e
) { aSQLException
<<= e
; }
287 catch(const SQLWarning
& e
) { aSQLException
<<= e
; }
288 catch(const SQLException
& e
) { aSQLException
<<= e
; }
289 catch (const Exception
&)
291 OSL_FAIL("OTableSelectionPage::implFillTables: could not fill the table list!");
295 // will be the table tables of the selected data source
301 Reference
< XTablesSupplier
> xSupplTables(xConn
, UNO_QUERY
);
302 if ( xSupplTables
.is() )
304 Reference
< XNameAccess
> xTables
= xSupplTables
->getTables();
306 aTableNames
= xTables
->getElementNames();
310 Reference
< XQueriesSupplier
> xSuppQueries( xConn
, UNO_QUERY
);
311 if ( xSuppQueries
.is() )
313 Reference
< XNameAccess
> xQueries
= xSuppQueries
->getQueries();
315 aQueryNames
= xQueries
->getElementNames();
318 catch(const SQLContext
& e
) { aSQLException
<<= e
; }
319 catch(const SQLWarning
& e
) { aSQLException
<<= e
; }
320 catch(const SQLException
& e
) { aSQLException
<<= e
; }
321 catch (const Exception
&)
323 OSL_FAIL("OTableSelectionPage::implFillTables: could not fill the table list!");
328 if ( aSQLException
.hasValue() )
329 { // an SQLException (or derivee) was thrown ...
330 Reference
< XInteractionRequest
> xRequest
= new OInteractionRequest(aSQLException
);
333 // get the default SDB interaction handler
334 Reference
< XInteractionHandler
> xHandler
= getDialog()->getInteractionHandler(getDialog()->getDialog());
336 xHandler
->handle(xRequest
);
338 catch(const Exception
&) { }
342 lcl_fillEntries(*m_xTable
, aTableNames
, BMP_TABLE
, CommandType::TABLE
);
343 lcl_fillEntries(*m_xTable
, aQueryNames
, BMP_QUERY
, CommandType::QUERY
);
346 OMaybeListSelectionPage::OMaybeListSelectionPage(weld::Container
* pPage
, OControlWizard
* pWizard
, const OUString
& rUIXMLDescription
, const OUString
& rID
)
347 : OControlWizardPage(pPage
, pWizard
, rUIXMLDescription
, rID
)
354 OMaybeListSelectionPage::~OMaybeListSelectionPage()
358 void OMaybeListSelectionPage::announceControls(weld::RadioButton
& _rYesButton
, weld::RadioButton
& _rNoButton
, weld::ComboBox
& _rSelection
)
360 m_pYes
= &_rYesButton
;
362 m_pList
= &_rSelection
;
364 m_pYes
->connect_toggled(LINK(this, OMaybeListSelectionPage
, OnRadioSelected
));
365 m_pNo
->connect_toggled(LINK(this, OMaybeListSelectionPage
, OnRadioSelected
));
369 IMPL_LINK(OMaybeListSelectionPage
, OnRadioSelected
, weld::Toggleable
&, rButton
, void)
371 if (!rButton
.get_active())
376 void OMaybeListSelectionPage::implInitialize(const OUString
& _rSelection
)
378 DBG_ASSERT(m_pYes
, "OMaybeListSelectionPage::implInitialize: no controls announced!");
379 bool bIsSelection
= ! _rSelection
.isEmpty();
380 m_pYes
->set_active(bIsSelection
);
381 m_pNo
->set_active(!bIsSelection
);
382 m_pList
->set_sensitive(bIsSelection
);
384 m_pList
->set_active_text(bIsSelection
? _rSelection
: OUString());
387 void OMaybeListSelectionPage::implCommit(OUString
& _rSelection
)
389 _rSelection
= m_pYes
->get_active() ? m_pList
->get_active_text() : OUString();
392 void OMaybeListSelectionPage::implEnableWindows()
394 m_pList
->set_sensitive(m_pYes
->get_active());
397 void OMaybeListSelectionPage::Activate()
399 OControlWizardPage::Activate();
401 assert(m_pYes
&& "OMaybeListSelectionPage::Activate: no controls announced!");
402 if (m_pYes
->get_active())
403 m_pList
->grab_focus();
408 ODBFieldPage::ODBFieldPage(weld::Container
* pPage
, OControlWizard
* pWizard
)
409 : OMaybeListSelectionPage(pPage
, pWizard
, u
"modules/sabpilot/ui/optiondbfieldpage.ui"_ustr
, u
"OptionDBField"_ustr
)
410 , m_xDescription(m_xBuilder
->weld_label(u
"explLabel"_ustr
))
411 , m_xStoreYes(m_xBuilder
->weld_radio_button(u
"yesRadiobutton"_ustr
))
412 , m_xStoreNo(m_xBuilder
->weld_radio_button(u
"noRadiobutton"_ustr
))
413 , m_xStoreWhere(m_xBuilder
->weld_combo_box(u
"storeInFieldCombobox"_ustr
))
415 SetPageTitle(compmodule::ModuleRes(RID_STR_OPTION_DB_FIELD_TITLE
));
417 announceControls(*m_xStoreYes
, *m_xStoreNo
, *m_xStoreWhere
);
420 ODBFieldPage::~ODBFieldPage()
424 void ODBFieldPage::initializePage()
426 OMaybeListSelectionPage::initializePage();
428 // fill the fields page
429 fillListBox(*m_xStoreWhere
, getContext().aFieldNames
);
431 implInitialize(getDBFieldSetting());
434 bool ODBFieldPage::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason
)
436 if (!OMaybeListSelectionPage::commitPage(_eReason
))
439 implCommit(getDBFieldSetting());
448 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */