Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / dbaccess / source / ui / dlg / adtabdlg.cxx
blobade72252fda167c172f56d0322eb7c9f5d282b55
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 <adtabdlg.hxx>
21 #include <comphelper/diagnose_ex.hxx>
22 #include <core_resource.hxx>
23 #include <strings.hrc>
24 #include <connectivity/dbtools.hxx>
25 #include <com/sun/star/container/XContainer.hpp>
26 #include <com/sun/star/sdb/application/DatabaseObject.hpp>
27 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
28 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
29 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
30 #include <com/sun/star/container/XNameAccess.hpp>
31 #include <imageprovider.hxx>
32 #include <comphelper/containermultiplexer.hxx>
33 #include <cppuhelper/basemutex.hxx>
34 #include <algorithm>
36 // slot ids
37 using namespace dbaui;
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::container;
41 using namespace ::com::sun::star::sdb;
42 using namespace ::com::sun::star::sdbc;
43 using namespace ::com::sun::star::sdbcx;
44 using namespace dbtools;
46 TableObjectListFacade::~TableObjectListFacade()
50 namespace {
52 class TableListFacade : public ::cppu::BaseMutex
53 , public TableObjectListFacade
54 , public ::comphelper::OContainerListener
56 OTableTreeListBox& m_rTableList;
57 Reference< XConnection > m_xConnection;
58 ::rtl::Reference< comphelper::OContainerListenerAdapter>
59 m_pContainerListener;
60 bool m_bAllowViews;
62 public:
63 TableListFacade(OTableTreeListBox& _rTableList, const Reference< XConnection >& _rxConnection)
64 : ::comphelper::OContainerListener(m_aMutex)
65 ,m_rTableList( _rTableList )
66 ,m_xConnection( _rxConnection )
67 ,m_bAllowViews(true)
70 virtual ~TableListFacade() override;
72 private:
73 virtual void updateTableObjectList( bool _bAllowViews ) override;
74 virtual OUString getSelectedName( OUString& _out_rAliasName ) const override;
75 virtual bool isLeafSelected() const override;
76 // OContainerListener
77 virtual void _elementInserted( const css::container::ContainerEvent& _rEvent ) override;
78 virtual void _elementRemoved( const css::container::ContainerEvent& _rEvent ) override;
79 virtual void _elementReplaced( const css::container::ContainerEvent& _rEvent ) override;
84 TableListFacade::~TableListFacade()
86 if ( m_pContainerListener.is() )
87 m_pContainerListener->dispose();
90 OUString TableListFacade::getSelectedName( OUString& _out_rAliasName ) const
92 weld::TreeView& rTableList = m_rTableList.GetWidget();
93 std::unique_ptr<weld::TreeIter> xEntry(rTableList.make_iterator());
95 if (!rTableList.get_selected(xEntry.get()))
96 return OUString();
98 OUString aCatalog, aSchema, aTableName;
99 std::unique_ptr<weld::TreeIter> xSchema(rTableList.make_iterator(xEntry.get()));
100 if (rTableList.iter_parent(*xSchema))
102 auto xAll = m_rTableList.getAllObjectsEntry();
103 if (!xAll || !xSchema->equal(*xAll))
105 std::unique_ptr<weld::TreeIter> xCatalog(rTableList.make_iterator(xSchema.get()));
106 if (rTableList.iter_parent(*xCatalog))
108 if (!xAll || !xCatalog->equal(*xAll))
109 aCatalog = rTableList.get_text(*xCatalog, 0);
111 aSchema = rTableList.get_text(*xSchema, 0);
114 aTableName = rTableList.get_text(*xEntry, 0);
116 OUString aComposedName;
119 Reference< XDatabaseMetaData > xMeta( m_xConnection->getMetaData(), UNO_SET_THROW );
120 if ( aCatalog.isEmpty()
121 && !aSchema.isEmpty()
122 && xMeta->supportsCatalogsInDataManipulation()
123 && !xMeta->supportsSchemasInDataManipulation() )
125 aCatalog = aSchema;
126 aSchema.clear();
129 aComposedName = ::dbtools::composeTableName(
130 xMeta, aCatalog, aSchema, aTableName, false, ::dbtools::EComposeRule::InDataManipulation );
132 catch ( const Exception& )
134 DBG_UNHANDLED_EXCEPTION("dbaccess");
137 _out_rAliasName = aTableName;
138 return aComposedName;
141 void TableListFacade::_elementInserted( const container::ContainerEvent& /*_rEvent*/ )
143 updateTableObjectList(m_bAllowViews);
146 void TableListFacade::_elementRemoved( const container::ContainerEvent& /*_rEvent*/ )
148 updateTableObjectList(m_bAllowViews);
151 void TableListFacade::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ )
155 void TableListFacade::updateTableObjectList( bool _bAllowViews )
157 m_bAllowViews = _bAllowViews;
158 weld::TreeView& rTableList = m_rTableList.GetWidget();
159 rTableList.clear();
162 Reference< XTablesSupplier > xTableSupp( m_xConnection, UNO_QUERY_THROW );
164 Reference< XViewsSupplier > xViewSupp;
165 Reference< XNameAccess > xTables, xViews;
166 Sequence< OUString > sTables, sViews;
168 xTables = xTableSupp->getTables();
169 if ( xTables.is() )
171 if ( !m_pContainerListener.is() )
173 Reference< XContainer> xContainer(xTables,uno::UNO_QUERY);
174 if ( xContainer.is() )
175 m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
177 sTables = xTables->getElementNames();
180 xViewSupp.set( xTableSupp, UNO_QUERY );
181 if ( xViewSupp.is() )
183 xViews = xViewSupp->getViews();
184 if ( xViews.is() )
185 sViews = xViews->getElementNames();
188 // if no views are allowed remove the views also out the table name filter
189 if ( !_bAllowViews )
191 std::vector<OUString> aTables(sTables.begin(), sTables.end());
193 for (auto& view : sViews)
194 std::erase_if(aTables, [Equal = comphelper::UStringMixEqual(), &view](auto& s)
195 { return Equal(s, view); });
196 sTables = Sequence< OUString>(aTables.data(), aTables.size());
197 sViews = Sequence< OUString>();
200 m_rTableList.UpdateTableList( m_xConnection, sTables, sViews );
202 std::unique_ptr<weld::TreeIter> xEntry(rTableList.make_iterator());
203 bool bEntry = rTableList.get_iter_first(*xEntry);
204 while (bEntry && rTableList.iter_has_child(*xEntry))
206 rTableList.expand_row(*xEntry);
207 bEntry = rTableList.iter_next(*xEntry);
209 if (bEntry)
210 rTableList.select(*xEntry);
212 catch( const Exception& )
214 DBG_UNHANDLED_EXCEPTION("dbaccess");
218 bool TableListFacade::isLeafSelected() const
220 weld::TreeView& rTableList = m_rTableList.GetWidget();
221 std::unique_ptr<weld::TreeIter> xEntry(rTableList.make_iterator());
222 const bool bEntry = rTableList.get_selected(xEntry.get());
223 return bEntry && !rTableList.iter_has_child(*xEntry);
226 namespace {
228 class QueryListFacade : public ::cppu::BaseMutex
229 , public TableObjectListFacade
230 , public ::comphelper::OContainerListener
232 weld::TreeView& m_rQueryList;
233 Reference< XConnection > m_xConnection;
234 ::rtl::Reference< comphelper::OContainerListenerAdapter>
235 m_pContainerListener;
237 public:
238 QueryListFacade( weld::TreeView& _rQueryList, const Reference< XConnection >& _rxConnection )
239 : ::comphelper::OContainerListener(m_aMutex)
240 ,m_rQueryList( _rQueryList )
241 ,m_xConnection( _rxConnection )
244 virtual ~QueryListFacade() override;
246 private:
247 virtual void updateTableObjectList( bool _bAllowViews ) override;
248 virtual OUString getSelectedName( OUString& _out_rAliasName ) const override;
249 virtual bool isLeafSelected() const override;
250 // OContainerListener
251 virtual void _elementInserted( const css::container::ContainerEvent& _rEvent ) override;
252 virtual void _elementRemoved( const css::container::ContainerEvent& _rEvent ) override;
253 virtual void _elementReplaced( const css::container::ContainerEvent& _rEvent ) override;
258 QueryListFacade::~QueryListFacade()
260 if ( m_pContainerListener.is() )
261 m_pContainerListener->dispose();
264 void QueryListFacade::_elementInserted( const container::ContainerEvent& _rEvent )
266 OUString sName;
267 if ( _rEvent.Accessor >>= sName )
269 OUString aQueryImage(ImageProvider::getDefaultImageResourceID(css::sdb::application::DatabaseObject::QUERY));
270 m_rQueryList.append(u""_ustr, sName, aQueryImage);
274 void QueryListFacade::_elementRemoved( const container::ContainerEvent& /*_rEvent*/ )
276 updateTableObjectList(true);
279 void QueryListFacade::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ )
283 void QueryListFacade::updateTableObjectList( bool /*_bAllowViews*/ )
285 m_rQueryList.clear();
288 OUString aQueryImage(ImageProvider::getDefaultImageResourceID(css::sdb::application::DatabaseObject::QUERY));
290 Reference< XQueriesSupplier > xSuppQueries( m_xConnection, UNO_QUERY_THROW );
291 Reference< XNameAccess > xQueries( xSuppQueries->getQueries(), UNO_SET_THROW );
292 if ( !m_pContainerListener.is() )
294 Reference< XContainer> xContainer(xQueries,UNO_QUERY_THROW);
295 m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
297 const Sequence< OUString > aQueryNames = xQueries->getElementNames();
299 for ( auto const & name : aQueryNames )
300 m_rQueryList.append(u""_ustr, name, aQueryImage);
302 catch( const Exception& )
304 DBG_UNHANDLED_EXCEPTION("dbaccess");
308 OUString QueryListFacade::getSelectedName( OUString& _out_rAliasName ) const
310 OUString sSelected;
311 std::unique_ptr<weld::TreeIter> xEntry(m_rQueryList.make_iterator());
312 const bool bEntry = m_rQueryList.get_selected(xEntry.get());
313 if (bEntry)
314 sSelected = _out_rAliasName = m_rQueryList.get_text(*xEntry, 0);
315 return sSelected;
318 bool QueryListFacade::isLeafSelected() const
320 std::unique_ptr<weld::TreeIter> xEntry(m_rQueryList.make_iterator());
321 const bool bEntry = m_rQueryList.get_selected(xEntry.get());
322 return bEntry && !m_rQueryList.iter_has_child(*xEntry);
326 OAddTableDlg::OAddTableDlg(weld::Window* pParent, IAddTableDialogContext& _rContext)
327 : GenericDialogController(pParent, u"dbaccess/ui/tablesjoindialog.ui"_ustr, u"TablesJoinDialog"_ustr)
328 , m_rContext(_rContext)
329 , m_xCaseTables(m_xBuilder->weld_radio_button(u"tables"_ustr))
330 , m_xCaseQueries(m_xBuilder->weld_radio_button(u"queries"_ustr))
331 // false means: do not show any buttons
332 , m_xTableList(new OTableTreeListBox(m_xBuilder->weld_tree_view(u"tablelist"_ustr), false))
333 , m_xQueryList(m_xBuilder->weld_tree_view(u"querylist"_ustr))
334 , m_xAddButton(m_xBuilder->weld_button(u"add"_ustr))
335 , m_xCloseButton(m_xBuilder->weld_button(u"close"_ustr))
337 weld::TreeView& rTableList = m_xTableList->GetWidget();
338 Size aSize(rTableList.get_approximate_digit_width() * 23,
339 rTableList.get_height_rows(15));
340 rTableList.set_size_request(aSize.Width(), aSize.Height());
341 m_xQueryList->set_size_request(aSize.Width(), aSize.Height());
343 m_xCaseTables->connect_toggled(LINK(this, OAddTableDlg, OnTypeSelected));
344 m_xAddButton->connect_clicked( LINK( this, OAddTableDlg, AddClickHdl ) );
345 m_xCloseButton->connect_clicked( LINK( this, OAddTableDlg, CloseClickHdl ) );
346 rTableList.connect_row_activated( LINK( this, OAddTableDlg, TableListDoubleClickHdl ) );
347 rTableList.connect_selection_changed(LINK(this, OAddTableDlg, TableListSelectHdl));
348 m_xQueryList->connect_row_activated( LINK( this, OAddTableDlg, TableListDoubleClickHdl ) );
349 m_xQueryList->connect_selection_changed(LINK(this, OAddTableDlg, TableListSelectHdl));
351 rTableList.set_selection_mode(SelectionMode::Single);
352 m_xTableList->SuppressEmptyFolders();
354 m_xQueryList->set_selection_mode(SelectionMode::Single);
356 if ( !m_rContext.allowQueries() )
358 m_xCaseTables->hide();
359 m_xCaseQueries->hide();
362 m_xDialog->set_title(getDialogTitleForContext(m_rContext));
365 OAddTableDlg::~OAddTableDlg()
369 void OAddTableDlg::impl_switchTo( ObjectList _eList )
371 switch ( _eList )
373 case Tables:
374 m_xTableList->GetWidget().show(); m_xCaseTables->set_active(true);
375 m_xQueryList->hide(); m_xCaseQueries->set_active(false);
376 m_xCurrentList.reset( new TableListFacade( *m_xTableList, m_rContext.getConnection() ) );
377 m_xTableList->GetWidget().grab_focus();
378 break;
380 case Queries:
381 m_xTableList->GetWidget().hide(); m_xCaseTables->set_active(false);
382 m_xQueryList->show(); m_xCaseQueries->set_active(true);
383 m_xCurrentList.reset( new QueryListFacade( *m_xQueryList, m_rContext.getConnection() ) );
384 m_xQueryList->grab_focus();
385 break;
387 m_xCurrentList->updateTableObjectList( m_rContext.allowViews() );
390 void OAddTableDlg::Update()
392 if (!m_xCurrentList)
393 impl_switchTo( Tables );
394 else
395 m_xCurrentList->updateTableObjectList( m_rContext.allowViews() );
398 IMPL_LINK_NOARG( OAddTableDlg, AddClickHdl, weld::Button&, void )
400 TableListDoubleClickHdl(m_xTableList->GetWidget());
403 IMPL_LINK_NOARG(OAddTableDlg, TableListDoubleClickHdl, weld::TreeView&, bool)
405 if ( impl_isAddAllowed() )
407 if ( m_xCurrentList->isLeafSelected() )
409 OUString sSelectedName, sAliasName;
410 sSelectedName = m_xCurrentList->getSelectedName( sAliasName );
412 m_rContext.addTableWindow( sSelectedName, sAliasName );
414 if ( !impl_isAddAllowed() )
415 m_xDialog->response(RET_CLOSE);
417 return true;
420 IMPL_LINK_NOARG( OAddTableDlg, TableListSelectHdl, weld::TreeView&, void )
422 m_xAddButton->set_sensitive( m_xCurrentList->isLeafSelected() );
425 IMPL_LINK_NOARG( OAddTableDlg, CloseClickHdl, weld::Button&, void )
427 m_xDialog->response(RET_CLOSE);
430 IMPL_LINK_NOARG(OAddTableDlg, OnTypeSelected, weld::Toggleable&, void)
432 if ( m_xCaseTables->get_active() )
433 impl_switchTo( Tables );
434 else
435 impl_switchTo( Queries );
438 void OAddTableDlg::OnClose()
440 m_rContext.onWindowClosing();
443 bool OAddTableDlg::impl_isAddAllowed()
445 return m_rContext.allowAddition();
448 OUString OAddTableDlg::getDialogTitleForContext( IAddTableDialogContext const & _rContext )
450 OUString sTitle;
452 if ( _rContext.allowQueries() )
453 sTitle = DBA_RES( STR_ADD_TABLE_OR_QUERY );
454 else
455 sTitle = DBA_RES( STR_ADD_TABLES );
457 return sTitle;
460 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */