build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / dbaccess / source / ui / control / tabletree.cxx
blob4e686720f1694dc4c59bf6bc032dcdd070208239
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 "tabletree.hxx"
21 #include "imageprovider.hxx"
22 #include "moduledbu.hxx"
23 #include "dbu_control.hrc"
24 #include <vcl/layout.hxx>
25 #include <vcl/menu.hxx>
26 #include <vcl/builderfactory.hxx>
27 #include <connectivity/dbtools.hxx>
28 #include <comphelper/types.hxx>
29 #include "dbustrings.hrc"
30 #include <com/sun/star/sdb/application/DatabaseObject.hpp>
31 #include <com/sun/star/sdb/application/DatabaseObjectContainer.hpp>
32 #include <com/sun/star/sdbc/XDriverAccess.hpp>
33 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
34 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
35 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
36 #include <com/sun/star/sdb/SQLContext.hpp>
37 #include <com/sun/star/sdbc/XRow.hpp>
38 #include <com/sun/star/beans/XPropertySet.hpp>
39 #include "commontypes.hxx"
40 #include "listviewitems.hxx"
41 #include <tools/diagnose_ex.h>
42 #include <osl/diagnose.h>
43 #include <rtl/ustrbuf.hxx>
44 #include <connectivity/dbmetadata.hxx>
45 #include "svtools/treelistentry.hxx"
46 #include <o3tl/make_unique.hxx>
48 #include <algorithm>
50 namespace dbaui
53 using namespace ::com::sun::star::uno;
54 using namespace ::com::sun::star::sdb;
55 using namespace ::com::sun::star::lang;
56 using namespace ::com::sun::star::sdbc;
57 using namespace ::com::sun::star::sdbcx;
58 using namespace ::com::sun::star::beans;
59 using namespace ::com::sun::star::container;
60 using namespace ::com::sun::star::sdb::application;
62 using namespace ::dbtools;
63 using namespace ::comphelper;
65 namespace DatabaseObject = ::com::sun::star::sdb::application::DatabaseObject;
66 namespace DatabaseObjectContainer = ::com::sun::star::sdb::application::DatabaseObjectContainer;
68 // OTableTreeListBox
69 OTableTreeListBox::OTableTreeListBox(vcl::Window* pParent, WinBits nWinStyle)
70 :OMarkableTreeListBox(pParent, nWinStyle)
71 ,m_xImageProvider( new ImageProvider )
72 ,m_bVirtualRoot(false)
73 ,m_bNoEmptyFolders( false )
75 implSetDefaultImages();
78 VCL_BUILDER_FACTORY_CONSTRUCTOR(OTableTreeListBox, 0)
80 void OTableTreeListBox::implSetDefaultImages()
82 ImageProvider aImageProvider;
83 SetDefaultExpandedEntryBmp( ImageProvider::getFolderImage( DatabaseObject::TABLE ) );
84 SetDefaultCollapsedEntryBmp( ImageProvider::getFolderImage( DatabaseObject::TABLE ) );
87 bool OTableTreeListBox::isFolderEntry( const SvTreeListEntry* _pEntry )
89 sal_Int32 nEntryType = reinterpret_cast< sal_IntPtr >( _pEntry->GetUserData() );
90 if ( ( nEntryType == DatabaseObjectContainer::TABLES )
91 || ( nEntryType == DatabaseObjectContainer::CATALOG )
92 || ( nEntryType == DatabaseObjectContainer::SCHEMA )
94 return true;
95 return false;
98 void OTableTreeListBox::notifyHiContrastChanged()
100 implSetDefaultImages();
102 SvTreeListEntry* pEntryLoop = First();
103 while (pEntryLoop)
105 size_t nCount = pEntryLoop->ItemCount();
106 for (size_t i=0;i<nCount;++i)
108 SvLBoxItem& rItem = pEntryLoop->GetItem(i);
109 if (rItem.GetType() == SvLBoxItemType::ContextBmp)
111 SvLBoxContextBmp& rContextBitmapItem = static_cast< SvLBoxContextBmp& >( rItem );
113 Image aImage;
114 if ( isFolderEntry( pEntryLoop ) )
116 aImage = ImageProvider::getFolderImage( DatabaseObject::TABLE );
118 else
120 OUString sCompleteName( getQualifiedTableName( pEntryLoop ) );
121 m_xImageProvider->getImages( sCompleteName, DatabaseObject::TABLE, aImage );
124 rContextBitmapItem.SetBitmap1( aImage );
125 rContextBitmapItem.SetBitmap2( aImage );
126 break;
129 pEntryLoop = Next(pEntryLoop);
133 void OTableTreeListBox::implOnNewConnection( const Reference< XConnection >& _rxConnection )
135 m_xConnection = _rxConnection;
136 m_xImageProvider.reset( new ImageProvider( m_xConnection ) );
139 void OTableTreeListBox::UpdateTableList( const Reference< XConnection >& _rxConnection ) throw(SQLException, std::exception)
141 Sequence< OUString > sTables, sViews;
143 OUString sCurrentActionError;
146 Reference< XTablesSupplier > xTableSupp( _rxConnection, UNO_QUERY_THROW );
147 sCurrentActionError = ModuleRes(STR_NOTABLEINFO);
149 Reference< XNameAccess > xTables,xViews;
151 Reference< XViewsSupplier > xViewSupp( _rxConnection, UNO_QUERY );
152 if ( xViewSupp.is() )
154 xViews = xViewSupp->getViews();
155 if (xViews.is())
156 sViews = xViews->getElementNames();
159 xTables = xTableSupp->getTables();
160 if (xTables.is())
161 sTables = xTables->getElementNames();
163 catch(RuntimeException&)
165 OSL_FAIL("OTableTreeListBox::UpdateTableList : caught an RuntimeException!");
167 catch ( const SQLException& )
169 throw;
171 catch(Exception&)
173 // a non-SQLException exception occurred ... simply throw an SQLException
174 SQLException aInfo;
175 aInfo.Message = sCurrentActionError;
176 throw aInfo;
179 UpdateTableList( _rxConnection, sTables, sViews );
182 namespace
184 struct OViewSetter : public ::std::unary_function< OTableTreeListBox::TNames::value_type, bool>
186 const Sequence< OUString> m_aViews;
187 ::comphelper::UStringMixEqual m_aEqualFunctor;
189 OViewSetter(const Sequence< OUString>& _rViews,bool _bCase) : m_aViews(_rViews),m_aEqualFunctor(_bCase){}
190 OTableTreeListBox::TNames::value_type operator() (const OUString& lhs)
192 OTableTreeListBox::TNames::value_type aRet;
193 aRet.first = lhs;
194 const OUString* pIter = m_aViews.getConstArray();
195 const OUString* pEnd = m_aViews.getConstArray() + m_aViews.getLength();
196 aRet.second = ::std::any_of(pIter,pEnd,::std::bind2nd(m_aEqualFunctor,lhs));
198 return aRet;
204 void OTableTreeListBox::UpdateTableList(
205 const Reference< XConnection >& _rxConnection,
206 const Sequence< OUString>& _rTables,
207 const Sequence< OUString>& _rViews
210 TNames aTables;
211 aTables.resize(_rTables.getLength());
212 const OUString* pIter = _rTables.getConstArray();
213 const OUString* pEnd = _rTables.getConstArray() + _rTables.getLength();
216 Reference< XDatabaseMetaData > xMeta( _rxConnection->getMetaData(), UNO_QUERY_THROW );
217 ::std::transform( pIter, pEnd,
218 aTables.begin(), OViewSetter( _rViews, xMeta->supportsMixedCaseQuotedIdentifiers() ) );
220 catch(Exception&)
222 DBG_UNHANDLED_EXCEPTION();
224 UpdateTableList( _rxConnection, aTables );
227 namespace
229 ::std::vector< OUString > lcl_getMetaDataStrings_throw( const Reference< XResultSet >& _rxMetaDataResult, sal_Int32 _nColumnIndex )
231 ::std::vector< OUString > aStrings;
232 Reference< XRow > xRow( _rxMetaDataResult, UNO_QUERY_THROW );
233 while ( _rxMetaDataResult->next() )
234 aStrings.push_back( xRow->getString( _nColumnIndex ) );
235 return aStrings;
238 bool lcl_shouldDisplayEmptySchemasAndCatalogs( const Reference< XConnection >& _rxConnection )
240 ::dbtools::DatabaseMetaData aMetaData( _rxConnection );
241 return aMetaData.displayEmptyTableFolders();
245 void OTableTreeListBox::UpdateTableList( const Reference< XConnection >& _rxConnection, const TNames& _rTables )
247 implOnNewConnection( _rxConnection );
249 // throw away all the old stuff
250 Clear();
254 if (haveVirtualRoot())
256 OUString sRootEntryText;
257 if ( ::std::none_of(_rTables.begin(),_rTables.end(),
258 [] (const TNames::value_type& name) { return !name.second; }) )
259 sRootEntryText = ModuleRes(STR_ALL_TABLES);
260 else if ( ::std::none_of(_rTables.begin(),_rTables.end(),
261 [] (const TNames::value_type& name) { return name.second; }) )
262 sRootEntryText = ModuleRes(STR_ALL_VIEWS);
263 else
264 sRootEntryText = ModuleRes(STR_ALL_TABLES_AND_VIEWS);
265 InsertEntry( sRootEntryText, nullptr, false, TREELIST_APPEND, reinterpret_cast< void* >( DatabaseObjectContainer::TABLES ) );
268 if ( _rTables.empty() )
269 // nothing to do (besides inserting the root entry)
270 return;
272 // get the table/view names
273 TNames::const_iterator aIter = _rTables.begin();
274 TNames::const_iterator aEnd = _rTables.end();
276 Reference< XDatabaseMetaData > xMeta( _rxConnection->getMetaData(), UNO_QUERY_THROW );
277 for ( ; aIter != aEnd; ++aIter )
279 // add the entry
280 implAddEntry(
281 xMeta,
282 aIter->first,
283 false
287 if ( !m_bNoEmptyFolders && lcl_shouldDisplayEmptySchemasAndCatalogs( _rxConnection ) )
289 bool bSupportsCatalogs = xMeta->supportsCatalogsInDataManipulation();
290 bool bSupportsSchemas = xMeta->supportsSchemasInDataManipulation();
292 if ( bSupportsCatalogs || bSupportsSchemas )
294 // we display empty catalogs if the DB supports catalogs, and they're noted at the beginning of a
295 // composed name. Otherwise, we display empty schematas. (also see the tree structure explained in
296 // implAddEntry)
297 bool bCatalogs = bSupportsCatalogs && xMeta->isCatalogAtStart();
299 ::std::vector< OUString > aFolderNames( lcl_getMetaDataStrings_throw(
300 bCatalogs ? xMeta->getCatalogs() : xMeta->getSchemas(), 1 ) );
301 sal_Int32 nFolderType = bCatalogs ? DatabaseObjectContainer::CATALOG : DatabaseObjectContainer::SCHEMA;
303 SvTreeListEntry* pRootEntry = getAllObjectsEntry();
304 for ( ::std::vector< OUString >::const_iterator folder = aFolderNames.begin();
305 folder != aFolderNames.end();
306 ++folder
309 SvTreeListEntry* pFolder = GetEntryPosByName( *folder, pRootEntry );
310 if ( !pFolder )
311 InsertEntry( *folder, pRootEntry, false, TREELIST_APPEND, reinterpret_cast< void* >( nFolderType ) );
316 catch ( const Exception& )
318 DBG_UNHANDLED_EXCEPTION();
322 bool OTableTreeListBox::isWildcardChecked(SvTreeListEntry* _pEntry)
324 if (_pEntry)
326 OBoldListboxString* pTextItem = static_cast<OBoldListboxString*>(_pEntry->GetFirstItem(SV_ITEM_ID_BOLDLBSTRING));
327 if (pTextItem)
328 return pTextItem->isEmphasized();
330 return false;
333 void OTableTreeListBox::checkWildcard(SvTreeListEntry* _pEntry)
335 SetCheckButtonState(_pEntry, SvButtonState::Checked);
336 checkedButton_noBroadcast(_pEntry);
339 SvTreeListEntry* OTableTreeListBox::getAllObjectsEntry() const
341 return haveVirtualRoot() ? First() : nullptr;
344 void OTableTreeListBox::checkedButton_noBroadcast(SvTreeListEntry* _pEntry)
346 OMarkableTreeListBox::checkedButton_noBroadcast(_pEntry);
348 // if an entry has children, it makes a difference if the entry is checked
349 // because all children are checked or if the user checked it explicitly.
350 // So we track explicit (un)checking
352 SvButtonState eState = GetCheckButtonState(_pEntry);
353 OSL_ENSURE(SvButtonState::Tristate != eState, "OTableTreeListBox::CheckButtonHdl: user action which lead to TRISTATE?");
354 implEmphasize(_pEntry, SvButtonState::Checked == eState);
357 void OTableTreeListBox::implEmphasize(SvTreeListEntry* _pEntry, bool _bChecked, bool _bUpdateDescendants, bool _bUpdateAncestors)
359 OSL_ENSURE(_pEntry, "OTableTreeListBox::implEmphasize: invalid entry (NULL)!");
361 // special emphasizing handling for the "all objects" entry
362 bool bAllObjectsEntryAffected = haveVirtualRoot() && (getAllObjectsEntry() == _pEntry);
363 if ( GetModel()->HasChildren(_pEntry) // the entry has children
364 || bAllObjectsEntryAffected // or it is the "all objects" entry
367 OBoldListboxString* pTextItem = static_cast<OBoldListboxString*>(_pEntry->GetFirstItem(SV_ITEM_ID_BOLDLBSTRING));
368 if (pTextItem)
369 pTextItem->emphasize(_bChecked);
371 if (bAllObjectsEntryAffected)
372 InvalidateEntry(_pEntry);
375 if (_bUpdateDescendants)
377 // remove the mark for all children of the checked entry
378 SvTreeListEntry* pChildLoop = FirstChild(_pEntry);
379 while (pChildLoop)
381 if (GetModel()->HasChildren(pChildLoop))
382 implEmphasize(pChildLoop, false, true, false);
383 pChildLoop = NextSibling(pChildLoop);
387 if (_bUpdateAncestors)
389 // remove the mark for all ancestors of the entry
390 if (GetModel()->HasParent(_pEntry))
391 implEmphasize(GetParent(_pEntry), false, false);
395 void OTableTreeListBox::InitEntry(SvTreeListEntry* _pEntry, const OUString& _rString, const Image& _rCollapsedBitmap, const Image& _rExpandedBitmap, SvLBoxButtonKind _eButtonKind)
397 OMarkableTreeListBox::InitEntry(_pEntry, _rString, _rCollapsedBitmap, _rExpandedBitmap, _eButtonKind);
399 // replace the text item with our own one
400 SvLBoxItem* pTextItem = _pEntry->GetFirstItem(SvLBoxItemType::String);
401 OSL_ENSURE(pTextItem, "OTableTreeListBox::InitEntry: no text item!?");
402 size_t nTextPos = _pEntry->GetPos(pTextItem);
403 OSL_ENSURE(SvTreeListEntry::ITEM_NOT_FOUND != nTextPos, "OTableTreeListBox::InitEntry: no text item pos!");
405 _pEntry->ReplaceItem(o3tl::make_unique<OBoldListboxString>(_rString), nTextPos);
408 SvTreeListEntry* OTableTreeListBox::implAddEntry(
409 const Reference< XDatabaseMetaData >& _rxMeta,
410 const OUString& _rTableName,
411 bool _bCheckName
414 OSL_PRECOND( _rxMeta.is(), "OTableTreeListBox::implAddEntry: invalid meta data!" );
415 if ( !_rxMeta.is() )
416 return nullptr;
418 // split the complete name into its components
419 OUString sCatalog, sSchema, sName;
420 qualifiedNameComponents( _rxMeta, _rTableName, sCatalog, sSchema, sName, ::dbtools::EComposeRule::InDataManipulation );
422 SvTreeListEntry* pParentEntry = getAllObjectsEntry();
424 // if the DB uses catalog at the start of identifiers, then our hierarchy is
425 // catalog
426 // +- schema
427 // +- table
428 // else it is
429 // schema
430 // +- catalog
431 // +- table
432 bool bCatalogAtStart = _rxMeta->isCatalogAtStart();
433 const OUString& rFirstName = bCatalogAtStart ? sCatalog : sSchema;
434 const sal_Int32 nFirstFolderType = bCatalogAtStart ? DatabaseObjectContainer::CATALOG : DatabaseObjectContainer::SCHEMA;
435 const OUString& rSecondName = bCatalogAtStart ? sSchema : sCatalog;
436 const sal_Int32 nSecondFolderType = bCatalogAtStart ? DatabaseObjectContainer::SCHEMA : DatabaseObjectContainer::CATALOG;
438 if ( !rFirstName.isEmpty() )
440 SvTreeListEntry* pFolder = GetEntryPosByName( rFirstName, pParentEntry );
441 if ( !pFolder )
442 pFolder = InsertEntry( rFirstName, pParentEntry, false, TREELIST_APPEND, reinterpret_cast< void* >( nFirstFolderType ) );
443 pParentEntry = pFolder;
446 if ( !rSecondName.isEmpty() )
448 SvTreeListEntry* pFolder = GetEntryPosByName( rSecondName, pParentEntry );
449 if ( !pFolder )
450 pFolder = InsertEntry( rSecondName, pParentEntry, false, TREELIST_APPEND, reinterpret_cast< void* >( nSecondFolderType ) );
451 pParentEntry = pFolder;
454 SvTreeListEntry* pRet = nullptr;
455 if ( !_bCheckName || !GetEntryPosByName( sName, pParentEntry ) )
457 pRet = InsertEntry( sName, pParentEntry );
459 Image aImage;
460 m_xImageProvider->getImages( _rTableName, DatabaseObject::TABLE, aImage );
462 SetExpandedEntryBmp( pRet, aImage );
463 SetCollapsedEntryBmp( pRet, aImage );
465 return pRet;
468 NamedDatabaseObject OTableTreeListBox::describeObject( SvTreeListEntry* _pEntry )
470 NamedDatabaseObject aObject;
472 sal_Int32 nEntryType = reinterpret_cast< sal_IntPtr >( _pEntry->GetUserData() );
474 if ( nEntryType == DatabaseObjectContainer::TABLES )
476 aObject.Type = DatabaseObjectContainer::TABLES;
478 else if ( ( nEntryType == DatabaseObjectContainer::CATALOG )
479 || ( nEntryType == DatabaseObjectContainer::SCHEMA )
482 SvTreeListEntry* pParent = GetParent( _pEntry );
483 sal_Int32 nParentEntryType = pParent ? reinterpret_cast< sal_IntPtr >( pParent->GetUserData() ) : -1;
485 OUStringBuffer buffer;
486 if ( nEntryType == DatabaseObjectContainer::CATALOG )
488 if ( nParentEntryType == DatabaseObjectContainer::SCHEMA )
490 buffer.append( GetEntryText( pParent ) );
491 buffer.append( '.' );
493 buffer.append( GetEntryText( _pEntry ) );
495 else if ( nEntryType == DatabaseObjectContainer::SCHEMA )
497 if ( nParentEntryType == DatabaseObjectContainer::CATALOG )
499 buffer.append( GetEntryText( pParent ) );
500 buffer.append( '.' );
502 buffer.append( GetEntryText( _pEntry ) );
505 else
507 aObject.Type = DatabaseObject::TABLE;
508 aObject.Name = getQualifiedTableName( _pEntry );
511 return aObject;
514 SvTreeListEntry* OTableTreeListBox::addedTable( const OUString& _rName )
518 Reference< XDatabaseMetaData > xMeta;
519 if ( impl_getAndAssertMetaData( xMeta ) )
520 return implAddEntry( xMeta, _rName );
522 catch( const Exception& )
524 DBG_UNHANDLED_EXCEPTION();
526 return nullptr;
529 bool OTableTreeListBox::impl_getAndAssertMetaData( Reference< XDatabaseMetaData >& _out_rMetaData ) const
531 if ( m_xConnection.is() )
532 _out_rMetaData = m_xConnection->getMetaData();
533 OSL_PRECOND( _out_rMetaData.is(), "OTableTreeListBox::impl_getAndAssertMetaData: invalid current connection!" );
534 return _out_rMetaData.is();
537 OUString OTableTreeListBox::getQualifiedTableName( SvTreeListEntry* _pEntry ) const
539 OSL_PRECOND( !isFolderEntry( _pEntry ), "OTableTreeListBox::getQualifiedTableName: folder entries not allowed here!" );
543 Reference< XDatabaseMetaData > xMeta;
544 if ( !impl_getAndAssertMetaData( xMeta ) )
545 return OUString();
547 OUString sCatalog;
548 OUString sSchema;
549 OUString sTable;
551 SvTreeListEntry* pSchema = GetParent( _pEntry );
552 if ( pSchema )
554 SvTreeListEntry* pCatalog = GetParent( pSchema );
555 if ( pCatalog
556 || ( xMeta->supportsCatalogsInDataManipulation()
557 && !xMeta->supportsSchemasInDataManipulation()
558 ) // here we support catalog but no schema
561 if ( pCatalog == nullptr )
563 pCatalog = pSchema;
564 pSchema = nullptr;
566 sCatalog = GetEntryText( pCatalog );
568 if ( pSchema )
569 sSchema = GetEntryText(pSchema);
571 sTable = GetEntryText( _pEntry );
573 return ::dbtools::composeTableName( xMeta, sCatalog, sSchema, sTable, false, ::dbtools::EComposeRule::InDataManipulation );
575 catch( const Exception& )
577 DBG_UNHANDLED_EXCEPTION();
579 return OUString();
582 SvTreeListEntry* OTableTreeListBox::getEntryByQualifiedName( const OUString& _rName )
586 Reference< XDatabaseMetaData > xMeta;
587 if ( !impl_getAndAssertMetaData( xMeta ) )
588 return nullptr;
590 // split the complete name into its components
591 OUString sCatalog, sSchema, sName;
592 qualifiedNameComponents(xMeta, _rName, sCatalog, sSchema, sName,::dbtools::EComposeRule::InDataManipulation);
594 SvTreeListEntry* pParent = getAllObjectsEntry();
595 SvTreeListEntry* pCat = nullptr;
596 SvTreeListEntry* pSchema = nullptr;
597 if ( !sCatalog.isEmpty() )
599 pCat = GetEntryPosByName(sCatalog, pParent);
600 if ( pCat )
601 pParent = pCat;
604 if ( !sSchema.isEmpty() )
606 pSchema = GetEntryPosByName(sSchema, pParent);
607 if ( pSchema )
608 pParent = pSchema;
611 return GetEntryPosByName(sName, pParent);
613 catch( const Exception& )
615 DBG_UNHANDLED_EXCEPTION();
617 return nullptr;
620 void OTableTreeListBox::removedTable( const OUString& _rName )
624 SvTreeListEntry* pEntry = getEntryByQualifiedName( _rName );
625 if ( pEntry )
626 GetModel()->Remove( pEntry );
628 catch( const Exception& )
630 DBG_UNHANDLED_EXCEPTION();
634 } // namespace dbaui
636 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */