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 <adtabdlg.hxx>
21 #include <tools/diagnose_ex.h>
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>
35
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;
45
~TableObjectListFacade()46 TableObjectListFacade::~TableObjectListFacade()
47 {
48 }
49
50 namespace {
51
52 class TableListFacade : public ::cppu::BaseMutex
53 , public TableObjectListFacade
54 , public ::comphelper::OContainerListener
55 {
56 OTableTreeListBox& m_rTableList;
57 Reference< XConnection > m_xConnection;
58 ::rtl::Reference< comphelper::OContainerListenerAdapter>
59 m_pContainerListener;
60 bool m_bAllowViews;
61
62 public:
TableListFacade(OTableTreeListBox & _rTableList,const Reference<XConnection> & _rxConnection)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)
68 {
69 }
70 virtual ~TableListFacade() override;
71
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;
80 };
81
82 }
83
~TableListFacade()84 TableListFacade::~TableListFacade()
85 {
86 if ( m_pContainerListener.is() )
87 m_pContainerListener->dispose();
88 }
89
getSelectedName(OUString & _out_rAliasName) const90 OUString TableListFacade::getSelectedName( OUString& _out_rAliasName ) const
91 {
92 weld::TreeView& rTableList = m_rTableList.GetWidget();
93 std::unique_ptr<weld::TreeIter> xEntry(rTableList.make_iterator());
94
95 if (!rTableList.get_selected(xEntry.get()))
96 return OUString();
97
98 OUString aCatalog, aSchema, aTableName;
99 std::unique_ptr<weld::TreeIter> xSchema(rTableList.make_iterator(xEntry.get()));
100 if (rTableList.iter_parent(*xSchema))
101 {
102 auto xAll = m_rTableList.getAllObjectsEntry();
103 if (!xAll || !xSchema->equal(*xAll))
104 {
105 std::unique_ptr<weld::TreeIter> xCatalog(rTableList.make_iterator(xSchema.get()));
106 if (rTableList.iter_parent(*xCatalog))
107 {
108 if (!xAll || !xCatalog->equal(*xAll))
109 aCatalog = rTableList.get_text(*xCatalog, 0);
110 }
111 aSchema = rTableList.get_text(*xSchema, 0);
112 }
113 }
114 aTableName = rTableList.get_text(*xEntry, 0);
115
116 OUString aComposedName;
117 try
118 {
119 Reference< XDatabaseMetaData > xMeta( m_xConnection->getMetaData(), UNO_SET_THROW );
120 if ( aCatalog.isEmpty()
121 && !aSchema.isEmpty()
122 && xMeta->supportsCatalogsInDataManipulation()
123 && !xMeta->supportsSchemasInDataManipulation() )
124 {
125 aCatalog = aSchema;
126 aSchema.clear();
127 }
128
129 aComposedName = ::dbtools::composeTableName(
130 xMeta, aCatalog, aSchema, aTableName, false, ::dbtools::EComposeRule::InDataManipulation );
131 }
132 catch ( const Exception& )
133 {
134 DBG_UNHANDLED_EXCEPTION("dbaccess");
135 }
136
137 _out_rAliasName = aTableName;
138 return aComposedName;
139 }
140
_elementInserted(const container::ContainerEvent &)141 void TableListFacade::_elementInserted( const container::ContainerEvent& /*_rEvent*/ )
142 {
143 updateTableObjectList(m_bAllowViews);
144 }
145
_elementRemoved(const container::ContainerEvent &)146 void TableListFacade::_elementRemoved( const container::ContainerEvent& /*_rEvent*/ )
147 {
148 updateTableObjectList(m_bAllowViews);
149 }
150
_elementReplaced(const container::ContainerEvent &)151 void TableListFacade::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ )
152 {
153 }
154
updateTableObjectList(bool _bAllowViews)155 void TableListFacade::updateTableObjectList( bool _bAllowViews )
156 {
157 m_bAllowViews = _bAllowViews;
158 weld::TreeView& rTableList = m_rTableList.GetWidget();
159 rTableList.clear();
160 try
161 {
162 Reference< XTablesSupplier > xTableSupp( m_xConnection, UNO_QUERY_THROW );
163
164 Reference< XViewsSupplier > xViewSupp;
165 Reference< XNameAccess > xTables, xViews;
166 Sequence< OUString > sTables, sViews;
167
168 xTables = xTableSupp->getTables();
169 if ( xTables.is() )
170 {
171 if ( !m_pContainerListener.is() )
172 {
173 Reference< XContainer> xContainer(xTables,uno::UNO_QUERY);
174 if ( xContainer.is() )
175 m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
176 }
177 sTables = xTables->getElementNames();
178 }
179
180 xViewSupp.set( xTableSupp, UNO_QUERY );
181 if ( xViewSupp.is() )
182 {
183 xViews = xViewSupp->getViews();
184 if ( xViews.is() )
185 sViews = xViews->getElementNames();
186 }
187
188 // if no views are allowed remove the views also out the table name filter
189 if ( !_bAllowViews )
190 {
191 const OUString* pTableBegin = sTables.getConstArray();
192 const OUString* pTableEnd = pTableBegin + sTables.getLength();
193 std::vector< OUString > aTables(pTableBegin,pTableEnd);
194
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>();
205 }
206
207 m_rTableList.UpdateTableList( m_xConnection, sTables, sViews );
208
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))
212 {
213 rTableList.expand_row(*xEntry);
214 bEntry = rTableList.iter_next(*xEntry);
215 }
216 if (bEntry)
217 rTableList.select(*xEntry);
218 }
219 catch( const Exception& )
220 {
221 DBG_UNHANDLED_EXCEPTION("dbaccess");
222 }
223 }
224
isLeafSelected() const225 bool TableListFacade::isLeafSelected() const
226 {
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);
231 }
232
233 namespace {
234
235 class QueryListFacade : public ::cppu::BaseMutex
236 , public TableObjectListFacade
237 , public ::comphelper::OContainerListener
238 {
239 weld::TreeView& m_rQueryList;
240 Reference< XConnection > m_xConnection;
241 ::rtl::Reference< comphelper::OContainerListenerAdapter>
242 m_pContainerListener;
243
244 public:
QueryListFacade(weld::TreeView & _rQueryList,const Reference<XConnection> & _rxConnection)245 QueryListFacade( weld::TreeView& _rQueryList, const Reference< XConnection >& _rxConnection )
246 : ::comphelper::OContainerListener(m_aMutex)
247 ,m_rQueryList( _rQueryList )
248 ,m_xConnection( _rxConnection )
249 {
250 }
251 virtual ~QueryListFacade() override;
252
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;
261 };
262
263 }
264
~QueryListFacade()265 QueryListFacade::~QueryListFacade()
266 {
267 if ( m_pContainerListener.is() )
268 m_pContainerListener->dispose();
269 }
270
_elementInserted(const container::ContainerEvent & _rEvent)271 void QueryListFacade::_elementInserted( const container::ContainerEvent& _rEvent )
272 {
273 OUString sName;
274 if ( _rEvent.Accessor >>= sName )
275 {
276 OUString aQueryImage(ImageProvider::getDefaultImageResourceID(css::sdb::application::DatabaseObject::QUERY));
277 m_rQueryList.append("", sName, aQueryImage);
278 }
279 }
280
_elementRemoved(const container::ContainerEvent &)281 void QueryListFacade::_elementRemoved( const container::ContainerEvent& /*_rEvent*/ )
282 {
283 updateTableObjectList(true);
284 }
285
_elementReplaced(const container::ContainerEvent &)286 void QueryListFacade::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ )
287 {
288 }
289
updateTableObjectList(bool)290 void QueryListFacade::updateTableObjectList( bool /*_bAllowViews*/ )
291 {
292 m_rQueryList.clear();
293 try
294 {
295 OUString aQueryImage(ImageProvider::getDefaultImageResourceID(css::sdb::application::DatabaseObject::QUERY));
296
297 Reference< XQueriesSupplier > xSuppQueries( m_xConnection, UNO_QUERY_THROW );
298 Reference< XNameAccess > xQueries( xSuppQueries->getQueries(), UNO_SET_THROW );
299 if ( !m_pContainerListener.is() )
300 {
301 Reference< XContainer> xContainer(xQueries,UNO_QUERY_THROW);
302 m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
303 }
304 const Sequence< OUString > aQueryNames = xQueries->getElementNames();
305
306 for ( auto const & name : aQueryNames )
307 m_rQueryList.append("", name, aQueryImage);
308 }
309 catch( const Exception& )
310 {
311 DBG_UNHANDLED_EXCEPTION("dbaccess");
312 }
313 }
314
getSelectedName(OUString & _out_rAliasName) const315 OUString QueryListFacade::getSelectedName( OUString& _out_rAliasName ) const
316 {
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;
323 }
324
isLeafSelected() const325 bool QueryListFacade::isLeafSelected() const
326 {
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);
330
331 }
332
OAddTableDlg(weld::Window * pParent,IAddTableDialogContext & _rContext)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"))
343 {
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());
349
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 ) );
357
358 rTableList.set_selection_mode(SelectionMode::Single);
359 m_xTableList->SuppressEmptyFolders();
360
361 m_xQueryList->set_selection_mode(SelectionMode::Single);
362
363 if ( !m_rContext.allowQueries() )
364 {
365 m_xCaseTables->hide();
366 m_xCaseQueries->hide();
367 }
368
369 m_xDialog->set_title(getDialogTitleForContext(m_rContext));
370 }
371
~OAddTableDlg()372 OAddTableDlg::~OAddTableDlg()
373 {
374 }
375
impl_switchTo(ObjectList _eList)376 void OAddTableDlg::impl_switchTo( ObjectList _eList )
377 {
378 switch ( _eList )
379 {
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;
386
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;
393 }
394 m_xCurrentList->updateTableObjectList( m_rContext.allowViews() );
395 }
396
Update()397 void OAddTableDlg::Update()
398 {
399 if (!m_xCurrentList)
400 impl_switchTo( Tables );
401 else
402 m_xCurrentList->updateTableObjectList( m_rContext.allowViews() );
403 }
404
IMPL_LINK_NOARG(OAddTableDlg,AddClickHdl,weld::Button &,void)405 IMPL_LINK_NOARG( OAddTableDlg, AddClickHdl, weld::Button&, void )
406 {
407 TableListDoubleClickHdl(m_xTableList->GetWidget());
408 }
409
IMPL_LINK_NOARG(OAddTableDlg,TableListDoubleClickHdl,weld::TreeView &,bool)410 IMPL_LINK_NOARG(OAddTableDlg, TableListDoubleClickHdl, weld::TreeView&, bool)
411 {
412 if ( impl_isAddAllowed() )
413 {
414 if ( m_xCurrentList->isLeafSelected() )
415 {
416 OUString sSelectedName, sAliasName;
417 sSelectedName = m_xCurrentList->getSelectedName( sAliasName );
418
419 m_rContext.addTableWindow( sSelectedName, sAliasName );
420 }
421 if ( !impl_isAddAllowed() )
422 m_xDialog->response(RET_CLOSE);
423 }
424 return true;
425 }
426
IMPL_LINK_NOARG(OAddTableDlg,TableListSelectHdl,weld::TreeView &,void)427 IMPL_LINK_NOARG( OAddTableDlg, TableListSelectHdl, weld::TreeView&, void )
428 {
429 m_xAddButton->set_sensitive( m_xCurrentList->isLeafSelected() );
430 }
431
IMPL_LINK_NOARG(OAddTableDlg,CloseClickHdl,weld::Button &,void)432 IMPL_LINK_NOARG( OAddTableDlg, CloseClickHdl, weld::Button&, void )
433 {
434 m_xDialog->response(RET_CLOSE);
435 }
436
IMPL_LINK_NOARG(OAddTableDlg,OnTypeSelected,weld::Toggleable &,void)437 IMPL_LINK_NOARG(OAddTableDlg, OnTypeSelected, weld::Toggleable&, void)
438 {
439 if ( m_xCaseTables->get_active() )
440 impl_switchTo( Tables );
441 else
442 impl_switchTo( Queries );
443 }
444
OnClose()445 void OAddTableDlg::OnClose()
446 {
447 m_rContext.onWindowClosing();
448 }
449
impl_isAddAllowed()450 bool OAddTableDlg::impl_isAddAllowed()
451 {
452 return m_rContext.allowAddition();
453 }
454
getDialogTitleForContext(IAddTableDialogContext const & _rContext)455 OUString OAddTableDlg::getDialogTitleForContext( IAddTableDialogContext const & _rContext )
456 {
457 OUString sTitle;
458
459 if ( _rContext.allowQueries() )
460 sTitle = DBA_RES( STR_ADD_TABLE_OR_QUERY );
461 else
462 sTitle = DBA_RES( STR_ADD_TABLES );
463
464 return sTitle;
465 }
466
467 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
468