LibreOffice Module dbaccess (master) 1
adtabdlg.cxx
Go to the documentation of this file.
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>
22#include <core_resource.hxx>
23#include <strings.hrc>
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>
34#include <algorithm>
35
36// slot ids
37using namespace dbaui;
38using namespace ::com::sun::star;
39using namespace ::com::sun::star::uno;
40using namespace ::com::sun::star::container;
41using namespace ::com::sun::star::sdb;
42using namespace ::com::sun::star::sdbc;
43using namespace ::com::sun::star::sdbcx;
44using namespace dbtools;
45
46TableObjectListFacade::~TableObjectListFacade()
47{
48}
49
50namespace {
51
52class TableListFacade : public ::cppu::BaseMutex
55{
56 OTableTreeListBox& m_rTableList;
59 m_pContainerListener;
60 bool m_bAllowViews;
61
62public:
63 TableListFacade(OTableTreeListBox& _rTableList, const Reference< XConnection >& _rxConnection)
65 ,m_rTableList( _rTableList )
66 ,m_xConnection( _rxConnection )
67 ,m_bAllowViews(true)
68 {
69 }
70 virtual ~TableListFacade() override;
71
72private:
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
84TableListFacade::~TableListFacade()
85{
86 if ( m_pContainerListener.is() )
87 m_pContainerListener->dispose();
88}
89
90OUString 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
141void TableListFacade::_elementInserted( const container::ContainerEvent& /*_rEvent*/ )
142{
143 updateTableObjectList(m_bAllowViews);
144}
145
146void TableListFacade::_elementRemoved( const container::ContainerEvent& /*_rEvent*/ )
147{
148 updateTableObjectList(m_bAllowViews);
149}
150
151void TableListFacade::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ )
152{
153}
154
155void 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
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
225bool 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
233namespace {
234
235class QueryListFacade : public ::cppu::BaseMutex
236 , public TableObjectListFacade
238{
239 weld::TreeView& m_rQueryList;
242 m_pContainerListener;
243
244public:
245 QueryListFacade( weld::TreeView& _rQueryList, const Reference< XConnection >& _rxConnection )
247 ,m_rQueryList( _rQueryList )
248 ,m_xConnection( _rxConnection )
249 {
250 }
251 virtual ~QueryListFacade() override;
252
253private:
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
265QueryListFacade::~QueryListFacade()
266{
267 if ( m_pContainerListener.is() )
268 m_pContainerListener->dispose();
269}
270
271void 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
281void QueryListFacade::_elementRemoved( const container::ContainerEvent& /*_rEvent*/ )
282{
283 updateTableObjectList(true);
284}
285
286void QueryListFacade::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ )
287{
288}
289
290void 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
315OUString 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
325bool 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
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
370}
371
373{
374}
375
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
398{
399 if (!m_xCurrentList)
401 else
402 m_xCurrentList->updateTableObjectList( m_rContext.allowViews() );
403}
404
406{
407 TableListDoubleClickHdl(m_xTableList->GetWidget());
408}
409
410IMPL_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
427IMPL_LINK_NOARG( OAddTableDlg, TableListSelectHdl, weld::TreeView&, void )
428{
429 m_xAddButton->set_sensitive( m_xCurrentList->isLeafSelected() );
430}
431
433{
434 m_xDialog->response(RET_CLOSE);
435}
436
438{
439 if ( m_xCaseTables->get_active() )
440 impl_switchTo( Tables );
441 else
442 impl_switchTo( Queries );
443}
444
446{
448}
449
451{
452 return m_rContext.allowAddition();
453}
454
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: */
OptionalString sName
Reference< XExecutableDialog > m_xDialog
constexpr tools::Long Height() const
constexpr tools::Long Width() const
virtual css::uno::Reference< css::sdbc::XConnection > getConnection() const =0
virtual void onWindowClosing()=0
virtual bool allowQueries() const =0
virtual bool allowAddition() const =0
virtual bool allowViews() const =0
static OUString getDialogTitleForContext(IAddTableDialogContext const &_rContext)
Definition: adtabdlg.cxx:455
std::unique_ptr< weld::RadioButton > m_xCaseTables
Definition: adtabdlg.hxx:60
std::unique_ptr< weld::RadioButton > m_xCaseQueries
Definition: adtabdlg.hxx:61
std::unique_ptr< weld::Button > m_xCloseButton
Definition: adtabdlg.hxx:67
IAddTableDialogContext & m_rContext
Definition: adtabdlg.hxx:57
std::unique_ptr< weld::TreeView > m_xQueryList
Definition: adtabdlg.hxx:64
std::unique_ptr< weld::Button > m_xAddButton
Definition: adtabdlg.hxx:66
void impl_switchTo(ObjectList _eList)
Definition: adtabdlg.cxx:376
virtual ~OAddTableDlg() override
Definition: adtabdlg.cxx:372
bool impl_isAddAllowed()
Definition: adtabdlg.cxx:450
OAddTableDlg(weld::Window *_pParent, IAddTableDialogContext &_rContext)
Definition: adtabdlg.cxx:333
std::unique_ptr< OTableTreeListBox > m_xTableList
Definition: adtabdlg.hxx:63
std::unique_ptr< TableObjectListFacade > m_xCurrentList
Definition: adtabdlg.hxx:58
unifies the access to a list of table/query objects
Definition: adtabdlg.hxx:31
std::shared_ptr< weld::Dialog > m_xDialog
virtual std::unique_ptr< TreeIter > make_iterator(const TreeIter *pOrig=nullptr) const=0
virtual bool get_selected(TreeIter *pIter) const=0
virtual void expand_row(const TreeIter &rIter)=0
void connect_row_activated(const Link< TreeView &, bool > &rLink)
virtual OUString get_text(int row, int col=-1) const=0
virtual void set_selection_mode(SelectionMode eMode)=0
virtual void clear()=0
void connect_changed(const Link< TreeView &, void > &rLink)
virtual bool get_iter_first(TreeIter &rIter) const=0
virtual int get_height_rows(int nRows) const=0
virtual void select(int pos)=0
virtual bool iter_parent(TreeIter &rIter) const=0
virtual bool iter_next(TreeIter &rIter) const=0
virtual bool iter_has_child(const TreeIter &rIter) const=0
virtual void set_size_request(int nWidth, int nHeight)=0
virtual float get_approximate_digit_width() const=0
#define DBA_RES(id)
#define DBG_UNHANDLED_EXCEPTION(...)
std::mutex m_aMutex
IMPL_LINK_NOARG(OApplicationController, OnClipboardChanged, TransferableDataHelper *, void)
Reference< XConnection > m_xConnection
Definition: objectnames.cxx:79
const Reference< XComponentContext > & m_rContext
RET_CLOSE