android: Update app icon to new startcenter icon
[LibreOffice.git] / dbaccess / source / ui / dlg / adtabdlg.cxx
blob4ff90945fc6a93229be8cb1f14ae18527a280e79
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 const OUString* pTableBegin = sTables.getConstArray();
192 const OUString* pTableEnd = pTableBegin + sTables.getLength();
193 std::vector< OUString > aTables(pTableBegin,pTableEnd);
195 const OUString* pViewBegin = sViews.getConstArray();
196 const OUString* pViewEnd = pViewBegin + sViews.getLength();
197 ::comphelper::UStringMixEqual aEqualFunctor;
198 for(;pViewBegin != pViewEnd;++pViewBegin)
199 aTables.erase(std::remove_if(aTables.begin(),aTables.end(),
200 [&aEqualFunctor, pViewBegin](const OUString& lhs)
201 { return aEqualFunctor(lhs, *pViewBegin); } )
202 , aTables.end());
203 sTables = Sequence< OUString>(aTables.data(), aTables.size());
204 sViews = Sequence< OUString>();
207 m_rTableList.UpdateTableList( m_xConnection, sTables, sViews );
209 std::unique_ptr<weld::TreeIter> xEntry(rTableList.make_iterator());
210 bool bEntry = rTableList.get_iter_first(*xEntry);
211 while (bEntry && rTableList.iter_has_child(*xEntry))
213 rTableList.expand_row(*xEntry);
214 bEntry = rTableList.iter_next(*xEntry);
216 if (bEntry)
217 rTableList.select(*xEntry);
219 catch( const Exception& )
221 DBG_UNHANDLED_EXCEPTION("dbaccess");
225 bool TableListFacade::isLeafSelected() const
227 weld::TreeView& rTableList = m_rTableList.GetWidget();
228 std::unique_ptr<weld::TreeIter> xEntry(rTableList.make_iterator());
229 const bool bEntry = rTableList.get_selected(xEntry.get());
230 return bEntry && !rTableList.iter_has_child(*xEntry);
233 namespace {
235 class QueryListFacade : public ::cppu::BaseMutex
236 , public TableObjectListFacade
237 , public ::comphelper::OContainerListener
239 weld::TreeView& m_rQueryList;
240 Reference< XConnection > m_xConnection;
241 ::rtl::Reference< comphelper::OContainerListenerAdapter>
242 m_pContainerListener;
244 public:
245 QueryListFacade( weld::TreeView& _rQueryList, const Reference< XConnection >& _rxConnection )
246 : ::comphelper::OContainerListener(m_aMutex)
247 ,m_rQueryList( _rQueryList )
248 ,m_xConnection( _rxConnection )
251 virtual ~QueryListFacade() override;
253 private:
254 virtual void updateTableObjectList( bool _bAllowViews ) override;
255 virtual OUString getSelectedName( OUString& _out_rAliasName ) const override;
256 virtual bool isLeafSelected() const override;
257 // OContainerListener
258 virtual void _elementInserted( const css::container::ContainerEvent& _rEvent ) override;
259 virtual void _elementRemoved( const css::container::ContainerEvent& _rEvent ) override;
260 virtual void _elementReplaced( const css::container::ContainerEvent& _rEvent ) override;
265 QueryListFacade::~QueryListFacade()
267 if ( m_pContainerListener.is() )
268 m_pContainerListener->dispose();
271 void QueryListFacade::_elementInserted( const container::ContainerEvent& _rEvent )
273 OUString sName;
274 if ( _rEvent.Accessor >>= sName )
276 OUString aQueryImage(ImageProvider::getDefaultImageResourceID(css::sdb::application::DatabaseObject::QUERY));
277 m_rQueryList.append("", sName, aQueryImage);
281 void QueryListFacade::_elementRemoved( const container::ContainerEvent& /*_rEvent*/ )
283 updateTableObjectList(true);
286 void QueryListFacade::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ )
290 void QueryListFacade::updateTableObjectList( bool /*_bAllowViews*/ )
292 m_rQueryList.clear();
295 OUString aQueryImage(ImageProvider::getDefaultImageResourceID(css::sdb::application::DatabaseObject::QUERY));
297 Reference< XQueriesSupplier > xSuppQueries( m_xConnection, UNO_QUERY_THROW );
298 Reference< XNameAccess > xQueries( xSuppQueries->getQueries(), UNO_SET_THROW );
299 if ( !m_pContainerListener.is() )
301 Reference< XContainer> xContainer(xQueries,UNO_QUERY_THROW);
302 m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
304 const Sequence< OUString > aQueryNames = xQueries->getElementNames();
306 for ( auto const & name : aQueryNames )
307 m_rQueryList.append("", name, aQueryImage);
309 catch( const Exception& )
311 DBG_UNHANDLED_EXCEPTION("dbaccess");
315 OUString QueryListFacade::getSelectedName( OUString& _out_rAliasName ) const
317 OUString sSelected;
318 std::unique_ptr<weld::TreeIter> xEntry(m_rQueryList.make_iterator());
319 const bool bEntry = m_rQueryList.get_selected(xEntry.get());
320 if (bEntry)
321 sSelected = _out_rAliasName = m_rQueryList.get_text(*xEntry, 0);
322 return sSelected;
325 bool QueryListFacade::isLeafSelected() const
327 std::unique_ptr<weld::TreeIter> xEntry(m_rQueryList.make_iterator());
328 const bool bEntry = m_rQueryList.get_selected(xEntry.get());
329 return bEntry && !m_rQueryList.iter_has_child(*xEntry);
333 OAddTableDlg::OAddTableDlg(weld::Window* pParent, IAddTableDialogContext& _rContext)
334 : GenericDialogController(pParent, "dbaccess/ui/tablesjoindialog.ui", "TablesJoinDialog")
335 , m_rContext(_rContext)
336 , m_xCaseTables(m_xBuilder->weld_radio_button("tables"))
337 , m_xCaseQueries(m_xBuilder->weld_radio_button("queries"))
338 // false means: do not show any buttons
339 , m_xTableList(new OTableTreeListBox(m_xBuilder->weld_tree_view("tablelist"), false))
340 , m_xQueryList(m_xBuilder->weld_tree_view("querylist"))
341 , m_xAddButton(m_xBuilder->weld_button("add"))
342 , m_xCloseButton(m_xBuilder->weld_button("close"))
344 weld::TreeView& rTableList = m_xTableList->GetWidget();
345 Size aSize(rTableList.get_approximate_digit_width() * 23,
346 rTableList.get_height_rows(15));
347 rTableList.set_size_request(aSize.Width(), aSize.Height());
348 m_xQueryList->set_size_request(aSize.Width(), aSize.Height());
350 m_xCaseTables->connect_toggled(LINK(this, OAddTableDlg, OnTypeSelected));
351 m_xAddButton->connect_clicked( LINK( this, OAddTableDlg, AddClickHdl ) );
352 m_xCloseButton->connect_clicked( LINK( this, OAddTableDlg, CloseClickHdl ) );
353 rTableList.connect_row_activated( LINK( this, OAddTableDlg, TableListDoubleClickHdl ) );
354 rTableList.connect_changed( LINK( this, OAddTableDlg, TableListSelectHdl ) );
355 m_xQueryList->connect_row_activated( LINK( this, OAddTableDlg, TableListDoubleClickHdl ) );
356 m_xQueryList->connect_changed( LINK( this, OAddTableDlg, TableListSelectHdl ) );
358 rTableList.set_selection_mode(SelectionMode::Single);
359 m_xTableList->SuppressEmptyFolders();
361 m_xQueryList->set_selection_mode(SelectionMode::Single);
363 if ( !m_rContext.allowQueries() )
365 m_xCaseTables->hide();
366 m_xCaseQueries->hide();
369 m_xDialog->set_title(getDialogTitleForContext(m_rContext));
372 OAddTableDlg::~OAddTableDlg()
376 void OAddTableDlg::impl_switchTo( ObjectList _eList )
378 switch ( _eList )
380 case Tables:
381 m_xTableList->GetWidget().show(); m_xCaseTables->set_active(true);
382 m_xQueryList->hide(); m_xCaseQueries->set_active(false);
383 m_xCurrentList.reset( new TableListFacade( *m_xTableList, m_rContext.getConnection() ) );
384 m_xTableList->GetWidget().grab_focus();
385 break;
387 case Queries:
388 m_xTableList->GetWidget().hide(); m_xCaseTables->set_active(false);
389 m_xQueryList->show(); m_xCaseQueries->set_active(true);
390 m_xCurrentList.reset( new QueryListFacade( *m_xQueryList, m_rContext.getConnection() ) );
391 m_xQueryList->grab_focus();
392 break;
394 m_xCurrentList->updateTableObjectList( m_rContext.allowViews() );
397 void OAddTableDlg::Update()
399 if (!m_xCurrentList)
400 impl_switchTo( Tables );
401 else
402 m_xCurrentList->updateTableObjectList( m_rContext.allowViews() );
405 IMPL_LINK_NOARG( OAddTableDlg, AddClickHdl, weld::Button&, void )
407 TableListDoubleClickHdl(m_xTableList->GetWidget());
410 IMPL_LINK_NOARG(OAddTableDlg, TableListDoubleClickHdl, weld::TreeView&, bool)
412 if ( impl_isAddAllowed() )
414 if ( m_xCurrentList->isLeafSelected() )
416 OUString sSelectedName, sAliasName;
417 sSelectedName = m_xCurrentList->getSelectedName( sAliasName );
419 m_rContext.addTableWindow( sSelectedName, sAliasName );
421 if ( !impl_isAddAllowed() )
422 m_xDialog->response(RET_CLOSE);
424 return true;
427 IMPL_LINK_NOARG( OAddTableDlg, TableListSelectHdl, weld::TreeView&, void )
429 m_xAddButton->set_sensitive( m_xCurrentList->isLeafSelected() );
432 IMPL_LINK_NOARG( OAddTableDlg, CloseClickHdl, weld::Button&, void )
434 m_xDialog->response(RET_CLOSE);
437 IMPL_LINK_NOARG(OAddTableDlg, OnTypeSelected, weld::Toggleable&, void)
439 if ( m_xCaseTables->get_active() )
440 impl_switchTo( Tables );
441 else
442 impl_switchTo( Queries );
445 void OAddTableDlg::OnClose()
447 m_rContext.onWindowClosing();
450 bool OAddTableDlg::impl_isAddAllowed()
452 return m_rContext.allowAddition();
455 OUString OAddTableDlg::getDialogTitleForContext( IAddTableDialogContext const & _rContext )
457 OUString sTitle;
459 if ( _rContext.allowQueries() )
460 sTitle = DBA_RES( STR_ADD_TABLE_OR_QUERY );
461 else
462 sTitle = DBA_RES( STR_ADD_TABLES );
464 return sTitle;
467 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */