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 "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>
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
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
;
86 if (m_xCurrentConnection
.is())
87 xMeta
= m_xCurrentConnection
->getMetaData();
91 OSL_FAIL("OTableSubscriptionPage::implCheckTables : could not retrieve the current connection's meta data!");
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
)
105 qualifiedNameComponents(xMeta
, rIncludeTable
, sCatalog
, sSchema
, sName
,::dbtools::EComposeRule::InDataManipulation
);
107 sName
= rIncludeTable
;
109 bool bAllTables
= (1 == sName
.getLength()) && ('%' == sName
[0]);
110 bool bAllSchemas
= (1 == sSchema
.getLength()) && ('%' == sSchema
[0]);
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
118 if (bAllSchemas
&& xCatalog
)
120 m_xTablesList
->checkWildcard(*xCatalog
);
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
130 if (bAllTables
&& xSchema
)
132 m_xTablesList
->checkWildcard(*xSchema
);
136 std::unique_ptr
<weld::TreeIter
> xEntry(m_xTablesList
->GetEntryPosByName(sName
, xSchema
? xSchema
.get() : (xCatalog
? xCatalog
.get() : xRootEntry
.get())));
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
151 if ((1 == _rTableFilter
.getLength()) && _rTableFilter
[0] == "%")
152 { // all tables visible
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!");
179 if (!m_pTablesDlg
->getCurrentSettings(aConnectionParams
))
181 m_xTablesList
->GetWidget().clear();
182 m_pTablesDlg
->endExecution();
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!");
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
);
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
);
236 m_xTables
->set_sensitive(false);
237 m_xTablesList
->GetWidget().clear();
241 m_pTablesDlg
->clearPassword();
242 m_pTablesDlg
->endExecution();
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();
263 DBG_UNHANDLED_EXCEPTION("dbaccess");
268 // get the current table filter
269 const OStringListItem
* pTableFilter
= _rSet
.GetItem
<OStringListItem
>(DSID_TABLEFILTER
);
270 Sequence
< OUString
> aTableFilter
;
272 aTableFilter
= pTableFilter
->getList();
274 implCompleteTablesCheck( aTableFilter
);
276 // expand the first entry by default
277 std::unique_ptr
<weld::TreeIter
> xExpand
= m_xTablesList
->getAllObjectsEntry();
280 m_xTablesList
->GetWidget().expand_row(*xExpand
);
281 if (!m_xTablesList
->GetWidget().iter_children(*xExpand
))
283 std::unique_ptr
<weld::TreeIter
> xSibling(m_xTablesList
->GetWidget().make_iterator(xExpand
.get()));
284 if (m_xTablesList
->GetWidget().iter_next_sibling(*xSibling
))
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
));
306 auto xRoot
= m_xTablesList
->getAllObjectsEntry();
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
&) { }
326 IMPL_LINK(OTableSubscriptionPage
, OnTreeEntryChecked
, const weld::TreeView::iter_col
&, rRowCol
, void)
328 m_xTablesList
->checkedButton_noBroadcast(rRowCol
.first
);
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
)
340 std::unique_ptr
<weld::TreeIter
> xEntry(m_xTablesList
->GetWidget().make_iterator(xAllObjectsEntry
.get()));
341 if (!m_xTablesList
->GetWidget().iter_next(*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
;
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
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
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
);
387 if (bCatalogWildcard
)
388 sCatalog
= sWildcard
;
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
) + ".");
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
);
409 sal_Int32 nOldLen
= aTableFilter
.getLength();
410 aTableFilter
.realloc(nOldLen
+ 1);
412 aTableFilter
.getArray()[nOldLen
] = sComposedName
.makeStringAndClear();
415 if (bCatalogWildcard
)
416 xEntry
= implNextSibling(xCatalog
.get());
417 else if (bSchemaWildcard
)
418 xEntry
= implNextSibling(xSchema
.get());
421 if (!m_xTablesList
->GetWidget().iter_next(*xEntry
))
429 std::unique_ptr
<weld::TreeIter
> OTableSubscriptionPage::implNextSibling(const weld::TreeIter
* pEntry
) const
431 std::unique_ptr
<weld::TreeIter
> xReturn
;
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());
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
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
};
467 aTableFilter
= collectDetailedSelection();
469 _rCoreAttrs
->Put( OStringListItem(DSID_TABLEFILTER
, aTableFilter
) );
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()));
485 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */