tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / dbaccess / source / ui / dlg / tablespage.cxx
blob06ff0c0e43730501a945c4725db770b000b49da2
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 "tablespage.hxx"
21 #include <dsitems.hxx>
22 #include <datasourceconnector.hxx>
23 #include <comphelper/types.hxx>
24 #include <connectivity/dbtools.hxx>
25 #include <connectivity/dbexception.hxx>
26 #include <stringlistitem.hxx>
27 #include <svl/stritem.hxx>
28 #include <strings.hxx>
29 #include <com/sun/star/sdbc/SQLException.hpp>
30 #include <com/sun/star/util/XModifiable.hpp>
31 #include <sqlmessage.hxx>
32 #include <UITools.hxx>
33 #include <osl/diagnose.h>
34 #include <TablesSingleDlg.hxx>
35 #include <comphelper/diagnose_ex.hxx>
36 #include <cppuhelper/exc_hlp.hxx>
38 namespace dbaui
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::sdbc;
43 using namespace ::com::sun::star::beans;
44 using namespace ::com::sun::star::lang;
45 using namespace ::com::sun::star::util;
46 using namespace ::dbtools;
47 using namespace ::comphelper;
49 // OTableSubscriptionPage
50 OTableSubscriptionPage::OTableSubscriptionPage(weld::Container* pPage, OTableSubscriptionDialog* pTablesDlg, const SfxItemSet& _rCoreAttrs)
51 : OGenericAdministrationPage(pPage, pTablesDlg, u"dbaccess/ui/tablesfilterpage.ui"_ustr, u"TablesFilterPage"_ustr, _rCoreAttrs)
52 , m_bCatalogAtStart(true)
53 , m_pTablesDlg(pTablesDlg)
54 , m_xTables(m_xBuilder->weld_widget(u"TablesFilterPage"_ustr))
55 , m_xTablesList(new OTableTreeListBox(m_xBuilder->weld_tree_view(u"treeview"_ustr), true))
57 m_xTablesList->init();
59 weld::TreeView& rWidget = m_xTablesList->GetWidget();
61 rWidget.set_size_request(rWidget.get_approximate_digit_width() * 48,
62 rWidget.get_height_rows(12));
64 // initialize the TabListBox
65 rWidget.set_selection_mode(SelectionMode::Multiple);
67 rWidget.connect_toggled(LINK(this, OTableSubscriptionPage, OnTreeEntryChecked));
70 OTableSubscriptionPage::~OTableSubscriptionPage()
72 // just to make sure that our connection will be removed
73 try
75 ::comphelper::disposeComponent(m_xCurrentConnection);
77 catch (RuntimeException&) { }
80 void OTableSubscriptionPage::implCheckTables(const Sequence< OUString >& _rTables)
82 // the meta data for the current connection, used for splitting up table names
83 Reference< XDatabaseMetaData > xMeta;
84 try
86 if (m_xCurrentConnection.is())
87 xMeta = m_xCurrentConnection->getMetaData();
89 catch(SQLException&)
91 OSL_FAIL("OTableSubscriptionPage::implCheckTables : could not retrieve the current connection's meta data!");
94 // uncheck all
95 CheckAll(false);
97 // check the ones which are in the list
98 OUString sCatalog, sSchema, sName;
100 std::unique_ptr<weld::TreeIter> xRootEntry(m_xTablesList->getAllObjectsEntry());
102 for (const OUString& rIncludeTable : _rTables)
104 if (xMeta.is())
105 qualifiedNameComponents(xMeta, rIncludeTable, sCatalog, sSchema, sName,::dbtools::EComposeRule::InDataManipulation);
106 else
107 sName = rIncludeTable;
109 bool bAllTables = (1 == sName.getLength()) && ('%' == sName[0]);
110 bool bAllSchemas = (1 == sSchema.getLength()) && ('%' == sSchema[0]);
112 // the catalog entry
113 std::unique_ptr<weld::TreeIter> xCatalog(m_xTablesList->GetEntryPosByName(sCatalog, xRootEntry.get()));
114 if (!(xCatalog || sCatalog.isEmpty()))
115 // the table (resp. its catalog) referred in this filter entry does not exist anymore
116 continue;
118 if (bAllSchemas && xCatalog)
120 m_xTablesList->checkWildcard(*xCatalog);
121 continue;
124 // the schema entry
125 std::unique_ptr<weld::TreeIter> xSchema = m_xTablesList->GetEntryPosByName(sSchema, (xCatalog ? xCatalog.get() : xRootEntry.get()));
126 if (!(xSchema || sSchema.isEmpty()))
127 // the table (resp. its schema) referred in this filter entry does not exist anymore
128 continue;
130 if (bAllTables && xSchema)
132 m_xTablesList->checkWildcard(*xSchema);
133 continue;
136 std::unique_ptr<weld::TreeIter> xEntry(m_xTablesList->GetEntryPosByName(sName, xSchema ? xSchema.get() : (xCatalog ? xCatalog.get() : xRootEntry.get())));
137 if (xEntry)
138 m_xTablesList->GetWidget().set_toggle(*xEntry, TRISTATE_TRUE);
140 m_xTablesList->CheckButtons();
143 void OTableSubscriptionPage::implCompleteTablesCheck( const css::uno::Sequence< OUString >& _rTableFilter )
145 if (!_rTableFilter.hasElements())
146 { // no tables visible
147 CheckAll(false);
149 else
151 if ((1 == _rTableFilter.getLength()) && _rTableFilter[0] == "%")
152 { // all tables visible
153 CheckAll();
155 else
156 implCheckTables( _rTableFilter );
160 void OTableSubscriptionPage::implInitControls(const SfxItemSet& _rSet, bool _bSaveValue)
162 // check whether or not the selection is invalid or readonly (invalid implies readonly, but not vice versa)
163 bool bValid, bReadonly;
164 getFlags(_rSet, bValid, bReadonly);
166 // get the name of the data source we're working for
167 const SfxStringItem* pNameItem = _rSet.GetItem<SfxStringItem>(DSID_NAME);
168 OSL_ENSURE(pNameItem, "OTableSubscriptionPage::implInitControls: missing the name attribute!");
169 OUString sDSName = pNameItem->GetValue();
171 if (bValid && !sDSName.isEmpty() && !m_xCurrentConnection.is() )
172 { // get the current table list from the connection for the current settings
174 // the PropertyValues for the current dialog settings
175 Sequence< PropertyValue > aConnectionParams;
176 OSL_ENSURE(m_pTablesDlg, "OTableSubscriptionPage::implInitControls: need a parent dialog doing the translation!");
177 if ( m_pTablesDlg )
179 if (!m_pTablesDlg->getCurrentSettings(aConnectionParams))
181 m_xTablesList->GetWidget().clear();
182 m_pTablesDlg->endExecution();
183 return;
187 // fill the table list with this connection information
188 SQLExceptionInfo aErrorInfo;
192 weld::WaitObject aWaitCursor(GetFrameWeld());
194 Reference<XPropertySet> xProp = m_pTablesDlg->getCurrentDataSource();
195 OSL_ENSURE(xProp.is(),"No data source set!");
196 if ( xProp.is() )
198 Any aTableFilter = xProp->getPropertyValue(PROPERTY_TABLEFILTER);
199 Any aTableTypeFilter = xProp->getPropertyValue(PROPERTY_TABLETYPEFILTER);
201 Reference<XModifiable> xModi(getDataSourceOrModel(xProp),UNO_QUERY);
202 bool bModified = ( xModi.is() && xModi->isModified() );
204 Sequence< OUString > aNewTableFilter { u"%"_ustr };
205 xProp->setPropertyValue(PROPERTY_TABLEFILTER,Any(aNewTableFilter));
207 xProp->setPropertyValue( PROPERTY_TABLETYPEFILTER, Any( Sequence< OUString >() ) );
208 Reference< css::lang::XEventListener> xEvt;
209 aErrorInfo = ::dbaui::createConnection(xProp, m_xORB, xEvt, m_xCurrentConnection);
211 xProp->setPropertyValue(PROPERTY_TABLEFILTER,aTableFilter);
212 xProp->setPropertyValue(PROPERTY_TABLETYPEFILTER,aTableTypeFilter);
214 if ( xModi.is() && !bModified )
215 xModi->setModified(false);
219 if ( m_xCurrentConnection.is() )
221 m_xTablesList->UpdateTableList( m_xCurrentConnection );
222 if (m_pTablesDlg)
223 m_pTablesDlg->successfullyConnected();
226 catch (const SQLException&)
228 aErrorInfo = ::cppu::getCaughtException();
231 if (aErrorInfo.isValid())
233 // establishing the connection failed. Show an error window and exit.
234 OSQLMessageBox aMessageBox(GetFrameWeld(), aErrorInfo);
235 aMessageBox.run();
236 m_xTables->set_sensitive(false);
237 m_xTablesList->GetWidget().clear();
239 if ( m_pTablesDlg )
241 m_pTablesDlg->clearPassword();
242 m_pTablesDlg->endExecution();
245 else
247 // in addition, we need some infos about the connection used
248 m_sCatalogSeparator = "."; // (default)
249 m_bCatalogAtStart = true; // (default)
252 Reference< XDatabaseMetaData > xMeta;
253 if (m_xCurrentConnection.is())
254 xMeta = m_xCurrentConnection->getMetaData();
255 if (xMeta.is() && xMeta->supportsCatalogsInDataManipulation())
257 m_sCatalogSeparator = xMeta->getCatalogSeparator();
258 m_bCatalogAtStart = xMeta->isCatalogAtStart();
261 catch(Exception&)
263 DBG_UNHANDLED_EXCEPTION("dbaccess");
268 // get the current table filter
269 const OStringListItem* pTableFilter = _rSet.GetItem<OStringListItem>(DSID_TABLEFILTER);
270 Sequence< OUString > aTableFilter;
271 if (pTableFilter)
272 aTableFilter = pTableFilter->getList();
274 implCompleteTablesCheck( aTableFilter );
276 // expand the first entry by default
277 std::unique_ptr<weld::TreeIter> xExpand = m_xTablesList->getAllObjectsEntry();
278 while (xExpand)
280 m_xTablesList->GetWidget().expand_row(*xExpand);
281 if (!m_xTablesList->GetWidget().iter_children(*xExpand))
282 break;
283 std::unique_ptr<weld::TreeIter> xSibling(m_xTablesList->GetWidget().make_iterator(xExpand.get()));
284 if (m_xTablesList->GetWidget().iter_next_sibling(*xSibling))
285 xExpand.reset();
288 // update the toolbox according the current selection and check state
289 OGenericAdministrationPage::implInitControls(_rSet, _bSaveValue);
292 void OTableSubscriptionPage::CheckAll( bool _bCheck )
294 std::unique_ptr<weld::TreeIter> xEntry(m_xTablesList->GetWidget().make_iterator());
295 if (m_xTablesList->GetWidget().get_iter_first(*xEntry))
299 m_xTablesList->GetWidget().set_toggle(*xEntry, _bCheck ? TRISTATE_TRUE : TRISTATE_FALSE);
301 while (m_xTablesList->GetWidget().iter_next(*xEntry));
304 if (_bCheck)
306 auto xRoot = m_xTablesList->getAllObjectsEntry();
307 if (xRoot)
308 m_xTablesList->checkWildcard(*xRoot);
312 DeactivateRC OTableSubscriptionPage::DeactivatePage(SfxItemSet* _pSet)
314 DeactivateRC nResult = OGenericAdministrationPage::DeactivatePage(_pSet);
316 // dispose the connection, we don't need it anymore, so we're not wasting resources
319 ::comphelper::disposeComponent(m_xCurrentConnection);
321 catch (RuntimeException&) { }
323 return nResult;
326 IMPL_LINK(OTableSubscriptionPage, OnTreeEntryChecked, const weld::TreeView::iter_col&, rRowCol, void)
328 m_xTablesList->checkedButton_noBroadcast(rRowCol.first);
329 callModifiedHdl();
332 Sequence< OUString > OTableSubscriptionPage::collectDetailedSelection() const
334 Sequence< OUString > aTableFilter;
335 static constexpr OUString sWildcard = u"%"_ustr;
337 std::unique_ptr<weld::TreeIter> xAllObjectsEntry(m_xTablesList->getAllObjectsEntry());
338 if (!xAllObjectsEntry)
339 return aTableFilter;
340 std::unique_ptr<weld::TreeIter> xEntry(m_xTablesList->GetWidget().make_iterator(xAllObjectsEntry.get()));
341 if (!m_xTablesList->GetWidget().iter_next(*xEntry))
342 xEntry.reset();
343 while (xEntry)
345 bool bCatalogWildcard = false;
346 bool bSchemaWildcard = false;
347 std::unique_ptr<weld::TreeIter> xSchema;
348 std::unique_ptr<weld::TreeIter> xCatalog;
350 if (m_xTablesList->GetWidget().get_toggle(*xEntry) == TRISTATE_TRUE && !m_xTablesList->GetWidget().iter_has_child(*xEntry))
351 { // checked and a leaf, which means it's no catalog, no schema, but a real table
352 OUStringBuffer sComposedName;
353 OUString sCatalog;
354 if (m_xTablesList->GetWidget().get_iter_depth(*xEntry))
356 xSchema = m_xTablesList->GetWidget().make_iterator(xEntry.get());
357 m_xTablesList->GetWidget().iter_parent(*xSchema);
358 if (xAllObjectsEntry->equal(*xSchema))
360 // do not want to have the root entry
361 xSchema.reset();
364 if (xSchema)
365 { // it's a real schema entry, not the "all objects" root
366 if (m_xTablesList->GetWidget().get_iter_depth(*xSchema))
368 xCatalog = m_xTablesList->GetWidget().make_iterator(xSchema.get());
369 m_xTablesList->GetWidget().iter_parent(*xCatalog);
370 if (xAllObjectsEntry->equal(*xCatalog))
372 // do not want to have the root entry
373 xCatalog.reset();
376 if (xCatalog)
377 { // it's a real catalog entry, not the "all objects" root
378 bCatalogWildcard = m_xTablesList->isWildcardChecked(*xCatalog);
379 if (m_bCatalogAtStart)
381 sComposedName.append(m_xTablesList->GetWidget().get_text(*xCatalog) + m_sCatalogSeparator);
382 if (bCatalogWildcard)
383 sComposedName.append(sWildcard);
385 else
387 if (bCatalogWildcard)
388 sCatalog = sWildcard;
389 else
390 sCatalog.clear();
391 sCatalog += m_sCatalogSeparator + m_xTablesList->GetWidget().get_text(*xCatalog) ;
395 bSchemaWildcard = m_xTablesList->isWildcardChecked(*xSchema);
396 sComposedName.append(m_xTablesList->GetWidget().get_text(*xSchema) + ".");
399 if (bSchemaWildcard)
400 sComposedName.append(sWildcard);
402 if (!bSchemaWildcard && !bCatalogWildcard)
403 sComposedName.append(m_xTablesList->GetWidget().get_text(*xEntry));
405 if (!m_bCatalogAtStart && !bCatalogWildcard)
406 sComposedName.append(sCatalog);
408 // need some space
409 sal_Int32 nOldLen = aTableFilter.getLength();
410 aTableFilter.realloc(nOldLen + 1);
411 // add the new name
412 aTableFilter.getArray()[nOldLen] = sComposedName.makeStringAndClear();
415 if (bCatalogWildcard)
416 xEntry = implNextSibling(xCatalog.get());
417 else if (bSchemaWildcard)
418 xEntry = implNextSibling(xSchema.get());
419 else
421 if (!m_xTablesList->GetWidget().iter_next(*xEntry))
422 xEntry.reset();
426 return aTableFilter;
429 std::unique_ptr<weld::TreeIter> OTableSubscriptionPage::implNextSibling(const weld::TreeIter* pEntry) const
431 std::unique_ptr<weld::TreeIter> xReturn;
432 if (pEntry)
434 xReturn = m_xTablesList->GetWidget().make_iterator(pEntry);
435 if (!m_xTablesList->GetWidget().iter_next_sibling(*xReturn))
437 std::unique_ptr<weld::TreeIter> xParent = m_xTablesList->GetWidget().make_iterator(pEntry);
438 if (m_xTablesList->GetWidget().iter_parent(*xParent))
439 xReturn = implNextSibling(xParent.get());
440 else
441 xReturn.reset();
444 return xReturn;
447 bool OTableSubscriptionPage::FillItemSet( SfxItemSet* _rCoreAttrs )
449 bool bValid, bReadonly;
450 getFlags(*_rCoreAttrs, bValid, bReadonly);
452 if (!bValid || bReadonly)
453 // don't store anything if the data we're working with is invalid or readonly
454 return true;
456 // create the output string which contains all the table names
457 if ( m_xCurrentConnection.is() )
458 { // collect the table filter data only if we have a connection - else no tables are displayed at all
459 Sequence< OUString > aTableFilter;
460 auto xRoot = m_xTablesList->getAllObjectsEntry();
461 if (xRoot && m_xTablesList->isWildcardChecked(*xRoot))
463 aTableFilter = { u"%"_ustr };
465 else
467 aTableFilter = collectDetailedSelection();
469 _rCoreAttrs->Put( OStringListItem(DSID_TABLEFILTER, aTableFilter) );
472 return true;
475 void OTableSubscriptionPage::fillControls(std::vector< std::unique_ptr<ISaveValueWrapper> >& /*_rControlList*/)
479 void OTableSubscriptionPage::fillWindows(std::vector< std::unique_ptr<ISaveValueWrapper> >& _rControlList)
481 _rControlList.emplace_back(new ODisableWidgetWrapper<weld::Widget>(m_xTables.get()));
483 } // namespace dbaui
485 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */