LibreOffice Module extensions (master) 1
commonpagesdbp.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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>
37#include <sfx2/docfilt.hxx>
40#include <svl/filenotation.hxx>
41#include <osl/diagnose.h>
42
43namespace dbp
44{
45
46
47 using namespace ::com::sun::star;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::container;
51 using namespace ::com::sun::star::sdb;
52 using namespace ::com::sun::star::sdbc;
53 using namespace ::com::sun::star::sdbcx;
54 using namespace ::com::sun::star::task;
55 using namespace ::comphelper;
56
58 : OControlWizardPage(pPage, pWizard, "modules/sabpilot/ui/tableselectionpage.ui", "TableSelectionPage")
59 , m_xTable(m_xBuilder->weld_tree_view("table"))
60 , m_xDatasource(m_xBuilder->weld_tree_view("datasource"))
61 , m_xSearchDatabase(m_xBuilder->weld_button("search"))
62 , m_xSourceBox(m_xBuilder->weld_container("sourcebox"))
63 {
64 try
65 {
67 if (m_xDSContext.is())
68 fillListBox(*m_xDatasource, m_xDSContext->getElementNames());
69 }
70 catch (const Exception&)
71 {
72 OSL_FAIL("OTableSelectionPage::OTableSelectionPage: could not collect the data source names!");
73 }
74
75 m_xDatasource->connect_changed(LINK(this, OTableSelectionPage, OnListboxSelection));
76 m_xTable->connect_changed(LINK(this, OTableSelectionPage, OnListboxSelection));
77 m_xTable->connect_row_activated(LINK(this, OTableSelectionPage, OnListboxDoubleClicked));
78 m_xSearchDatabase->connect_clicked(LINK(this, OTableSelectionPage, OnSearchClicked));
79 }
80
82 {
83 }
84
86 {
88 m_xDatasource->grab_focus();
89 }
90
92 {
94 return false;
95
96 if (0 == m_xDatasource->count_selected_rows())
97 return false;
98
99 if (0 == m_xTable->count_selected_rows())
100 return false;
101
102 return true;
103 }
104
106 {
108
109 const OControlWizardContext& rContext = getContext();
110 try
111 {
112 OUString sDataSourceName;
113 rContext.xForm->getPropertyValue("DataSourceName") >>= sDataSourceName;
114
115 Reference< XConnection > xConnection;
116 bool bEmbedded = ::dbtools::isEmbeddedInDatabase( rContext.xForm, xConnection );
117 if ( bEmbedded )
118 {
119 m_xSourceBox->hide();
120 m_xDatasource->append_text(sDataSourceName);
121 }
122 m_xDatasource->select_text(sDataSourceName);
123
124 implFillTables(xConnection);
125
126 OUString sCommand;
127 OSL_VERIFY( rContext.xForm->getPropertyValue("Command") >>= sCommand );
128 sal_Int32 nCommandType = CommandType::TABLE;
129 OSL_VERIFY( rContext.xForm->getPropertyValue("CommandType") >>= nCommandType );
130
131 // search the entry of the given type with the given name
132 for (sal_Int32 nLookup = 0; nLookup < m_xTable->n_children(); ++nLookup)
133 {
134 if (sCommand == m_xTable->get_text(nLookup))
135 {
136 if (m_xTable->get_id(nLookup).toInt32() == nCommandType)
137 {
138 m_xTable->select( nLookup );
139 break;
140 }
141 }
142 }
143 }
144 catch(const Exception&)
145 {
146 TOOLS_WARN_EXCEPTION("extensions.abpilot", "OTableSelectionPage::initializePage");
147 }
148 }
149
151 {
152 if (!OControlWizardPage::commitPage(_eReason))
153 return false;
154
155 const OControlWizardContext& rContext = getContext();
156 try
157 {
158 Reference< XConnection > xOldConn;
159 if ( !rContext.bEmbedded )
160 {
161 xOldConn = getFormConnection();
162
163 OUString sDataSource = m_xDatasource->get_selected_text();
164 rContext.xForm->setPropertyValue("DataSourceName", Any( sDataSource ) );
165 }
166 OUString sCommand = m_xTable->get_selected_text();
167 sal_Int32 nCommandType = m_xTable->get_selected_id().toInt32();
168
169 rContext.xForm->setPropertyValue("Command", Any( sCommand ) );
170 rContext.xForm->setPropertyValue("CommandType", Any( nCommandType ) );
171
172 if ( !rContext.bEmbedded )
173 setFormConnection( xOldConn, false );
174
175 if (!updateContext())
176 return false;
177 }
178 catch(const Exception&)
179 {
180 TOOLS_WARN_EXCEPTION("extensions.dbpilots", "OTableSelectionPage::commitPage");
181 }
182
183 return true;
184 }
185
187 {
189 ui::dialogs::TemplateDescription::FILEOPEN_READONLY_VERSION,
190 FileDialogFlags::NONE, getDialog()->getDialog());
191 aFileDlg.SetDisplayDirectory( SvtPathOptions().GetWorkPath() );
192
193 std::shared_ptr<const SfxFilter> pFilter = SfxFilter::GetFilterByName("StarOffice XML (Base)");
194 OSL_ENSURE(pFilter,"Filter: StarOffice XML (Base) could not be found!");
195 if ( pFilter )
196 {
197 aFileDlg.AddFilter(pFilter->GetUIName(),pFilter->GetDefaultExtension());
198 }
199
200 if (ERRCODE_NONE == aFileDlg.Execute())
201 {
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);
208 }
209 }
210
211 IMPL_LINK(OTableSelectionPage, OnListboxDoubleClicked, weld::TreeView&, _rBox, bool)
212 {
213 if (_rBox.count_selected_rows())
214 getDialog()->travelNext();
215 return true;
216 }
217
218 IMPL_LINK(OTableSelectionPage, OnListboxSelection, weld::TreeView&, _rBox, void)
219 {
220 if (m_xDatasource.get() == &_rBox)
221 { // new data source selected
222 implFillTables();
223 }
224
225 updateDialogTravelUI();
226 }
227
228 namespace
229 {
230 void lcl_fillEntries(weld::TreeView& rListBox, const Sequence<OUString>& rNames, const OUString& rImage, sal_Int32 nCommandType)
231 {
232 for (auto const & name : rNames)
233 {
234 rListBox.append(OUString::number(nCommandType), name, rImage);
235 }
236 }
237 }
238
239 void OTableSelectionPage::implFillTables(const Reference< XConnection >& _rxConn)
240 {
241 m_xTable->clear();
242
243 weld::WaitObject aWaitCursor(getDialog()->getDialog());
244
245 // will be the table tables of the selected data source
246 Sequence< OUString > aTableNames;
247 Sequence< OUString > aQueryNames;
248
249 // connect to the data source
250 Any aSQLException;
251 Reference< XConnection > xConn = _rxConn;
252 if ( !xConn.is() )
253 {
254 if (!m_xDSContext.is())
255 return;
256 // connect to the data source
257 try
258 {
259 OUString sCurrentDatasource = m_xDatasource->get_selected_text();
260 if (!sCurrentDatasource.isEmpty())
261 {
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) )
266 {
267 ::svt::OFileNotation aFileNotation(sCurrentDatasource);
268 sCurrentDatasource = aFileNotation.get(::svt::OFileNotation::N_URL);
269 }
270
271 if (m_xDSContext->getByName(sCurrentDatasource) >>= xDatasource)
272 { // connect
273 // get the default SDB interaction handler
274 Reference< XInteractionHandler > xHandler = getDialog()->getInteractionHandler(getDialog()->getDialog());
275 if (!xHandler.is() )
276 return;
277 xConn = xDatasource->connectWithCompletion(xHandler);
278 setFormConnection( xConn );
279 }
280 else
281 {
282 OSL_FAIL("OTableSelectionPage::implFillTables: invalid data source object returned by the context");
283 }
284 }
285 }
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&)
290 {
291 OSL_FAIL("OTableSelectionPage::implFillTables: could not fill the table list!");
292 }
293 }
294
295 // will be the table tables of the selected data source
296 if ( xConn.is() )
297 {
298 try
299 {
300 // get the tables
301 Reference< XTablesSupplier > xSupplTables(xConn, UNO_QUERY);
302 if ( xSupplTables.is() )
303 {
304 Reference< XNameAccess > xTables = xSupplTables->getTables();
305 if (xTables.is())
306 aTableNames = xTables->getElementNames();
307 }
308
309 // and the queries
310 Reference< XQueriesSupplier > xSuppQueries( xConn, UNO_QUERY );
311 if ( xSuppQueries.is() )
312 {
313 Reference< XNameAccess > xQueries = xSuppQueries->getQueries();
314 if ( xQueries.is() )
315 aQueryNames = xQueries->getElementNames();
316 }
317 }
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&)
322 {
323 OSL_FAIL("OTableSelectionPage::implFillTables: could not fill the table list!");
324 }
325 }
326
327
328 if ( aSQLException.hasValue() )
329 { // an SQLException (or derivee) was thrown ...
330 Reference< XInteractionRequest > xRequest = new OInteractionRequest(aSQLException);
331 try
332 {
333 // get the default SDB interaction handler
334 Reference< XInteractionHandler > xHandler = getDialog()->getInteractionHandler(getDialog()->getDialog());
335 if ( xHandler.is() )
336 xHandler->handle(xRequest);
337 }
338 catch(const Exception&) { }
339 return;
340 }
341
342 lcl_fillEntries(*m_xTable, aTableNames, BMP_TABLE, CommandType::TABLE);
343 lcl_fillEntries(*m_xTable, aQueryNames, BMP_QUERY, CommandType::QUERY);
344 }
345
346 OMaybeListSelectionPage::OMaybeListSelectionPage(weld::Container* pPage, OControlWizard* pWizard, const OUString& rUIXMLDescription, const OUString& rID)
347 : OControlWizardPage(pPage, pWizard, rUIXMLDescription, rID)
348 , m_pYes(nullptr)
349 , m_pNo(nullptr)
350 , m_pList(nullptr)
351 {
352 }
353
355 {
356 }
357
359 {
360 m_pYes = &_rYesButton;
361 m_pNo = &_rNoButton;
362 m_pList = &_rSelection;
363
364 m_pYes->connect_toggled(LINK(this, OMaybeListSelectionPage, OnRadioSelected));
365 m_pNo->connect_toggled(LINK(this, OMaybeListSelectionPage, OnRadioSelected));
367 }
368
369 IMPL_LINK(OMaybeListSelectionPage, OnRadioSelected, weld::Toggleable&, rButton, void)
370 {
371 if (!rButton.get_active())
372 return;
373 implEnableWindows();
374 }
375
376 void OMaybeListSelectionPage::implInitialize(const OUString& _rSelection)
377 {
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);
383
384 m_pList->set_active_text(bIsSelection ? _rSelection : OUString());
385 }
386
387 void OMaybeListSelectionPage::implCommit(OUString& _rSelection)
388 {
389 _rSelection = m_pYes->get_active() ? m_pList->get_active_text() : OUString();
390 }
391
393 {
395 }
396
398 {
400
401 DBG_ASSERT(m_pYes, "OMaybeListSelectionPage::Activate: no controls announced!");
402 if (m_pYes->get_active())
404 else
405 m_pNo->grab_focus();
406 }
407
409 : OMaybeListSelectionPage(pPage, pWizard, "modules/sabpilot/ui/optiondbfieldpage.ui", "OptionDBField")
410 , m_xDescription(m_xBuilder->weld_label("explLabel"))
411 , m_xStoreYes(m_xBuilder->weld_radio_button("yesRadiobutton"))
412 , m_xStoreNo(m_xBuilder->weld_radio_button("noRadiobutton"))
413 , m_xStoreWhere(m_xBuilder->weld_combo_box("storeInFieldCombobox"))
414 {
415 SetPageTitle(compmodule::ModuleRes(RID_STR_OPTION_DB_FIELD_TITLE));
416
418 }
419
421 {
422 }
423
425 {
427
428 // fill the fields page
430
432 }
433
435 {
437 return false;
438
440
441 return true;
442 }
443
444
445} // namespace dbp
446
447
448/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const char *const aFieldNames[]
void SetPageTitle(const OUString &rPageTitle)
static std::shared_ptr< const SfxFilter > GetFilterByName(const OUString &rName)
OControlWizard * getDialog()
css::uno::Reference< css::sdbc::XConnection > getFormConnection() const
virtual void initializePage() override
const OControlWizardContext & getContext() const
void setFormConnection(const css::uno::Reference< css::sdbc::XConnection > &_rxConn, bool _bAutoDispose=true)
static void fillListBox(weld::TreeView &_rList, const css::uno::Sequence< OUString > &_rItems)
css::uno::Reference< css::task::XInteractionHandler > getInteractionHandler(weld::Window *_pWindow) const
returns the com.sun.star.task.InteractionHandler
virtual ~ODBFieldPage() override
virtual bool commitPage(::vcl::WizardTypes::CommitPageReason _eReason) override
std::unique_ptr< weld::RadioButton > m_xStoreYes
std::unique_ptr< weld::ComboBox > m_xStoreWhere
ODBFieldPage(weld::Container *pPage, OControlWizard *pWizard)
virtual void initializePage() override
std::unique_ptr< weld::RadioButton > m_xStoreNo
virtual OUString & getDBFieldSetting()=0
void implCommit(OUString &_rSelection)
virtual ~OMaybeListSelectionPage() override
void announceControls(weld::RadioButton &_rYesButton, weld::RadioButton &_rNoButton, weld::ComboBox &_rSelection)
OMaybeListSelectionPage(weld::Container *pPage, OControlWizard *pWizard, const OUString &rUIXMLDescription, const OUString &rID)
void implInitialize(const OUString &_rSelection)
weld::RadioButton * m_pYes
virtual bool commitPage(::vcl::WizardTypes::CommitPageReason _eReason) override
std::unique_ptr< weld::Container > m_xSourceBox
std::unique_ptr< weld::TreeView > m_xTable
virtual ~OTableSelectionPage() override
virtual bool canAdvance() const override
OTableSelectionPage(weld::Container *pPage, OControlWizard *pParent)
css::uno::Reference< css::sdb::XDatabaseContext > m_xDSContext
std::unique_ptr< weld::Button > m_xSearchDatabase
virtual void initializePage() override
void implFillTables(const css::uno::Reference< css::sdbc::XConnection > &_rxConn=css::uno::Reference< css::sdbc::XConnection >())
std::unique_ptr< weld::TreeView > m_xDatasource
OUString GetPath() const
void AddFilter(const OUString &rFilterName, const OUString &rExtension)
void SetDisplayDirectory(const OUString &rPath)
OUString get(NOTATION _eOutputNotation) const
virtual bool canAdvance() const override
virtual bool commitPage(WizardTypes::CommitPageReason _eReason) override
virtual void Activate() override
virtual OUString get_active_text() const=0
void set_active_text(const OUString &rStr)
virtual void connect_toggled(const Link< Toggleable &, void > &rLink)
virtual bool get_active() const=0
virtual void set_active(bool active)=0
void append(TreeIter *pRet=nullptr)
virtual void grab_focus()=0
virtual void set_sensitive(bool sensitive)=0
#define DBG_ASSERT(sCon, aError)
#define TOOLS_WARN_EXCEPTION(area, stream)
#define ERRCODE_NONE
@ Exception
OUString ModuleRes(TranslateId pId)
IMPL_LINK_NOARG(OTableSelectionPage, OnSearchClicked, weld::Button &, void)
IMPL_LINK(OTableSelectionPage, OnListboxDoubleClicked, weld::TreeView &, _rBox, bool)
css::uno::Reference< css::sdb::XDatabaseContext > xDatasourceContext
css::uno::Reference< css::beans::XPropertySet > xForm