LCOV - code coverage report
Current view: top level - dbaccess/source/ui/querydesign - JoinController.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 13 198 6.6 %
Date: 2015-06-13 12:38:46 Functions: 5 32 15.6 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <sfx2/sfxsids.hrc>
      21             : #include "dbu_qry.hrc"
      22             : #include "browserids.hxx"
      23             : #include <comphelper/types.hxx>
      24             : #include "dbustrings.hrc"
      25             : #include <connectivity/dbtools.hxx>
      26             : #include <com/sun/star/frame/FrameSearchFlag.hpp>
      27             : #include <comphelper/extract.hxx>
      28             : #include <com/sun/star/container/XChild.hpp>
      29             : #include <com/sun/star/container/XNameContainer.hpp>
      30             : #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
      31             : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
      32             : #include <com/sun/star/sdbcx/KeyType.hpp>
      33             : #include <com/sun/star/sdbcx/XDrop.hpp>
      34             : #include <com/sun/star/sdbcx/XAlterTable.hpp>
      35             : #include <com/sun/star/sdbcx/XAppend.hpp>
      36             : #include <com/sun/star/sdb/SQLContext.hpp>
      37             : #include <com/sun/star/sdbc/SQLWarning.hpp>
      38             : #include <com/sun/star/sdbc/ColumnValue.hpp>
      39             : #include <com/sun/star/sdbc/XRow.hpp>
      40             : #include <connectivity/dbexception.hxx>
      41             : #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
      42             : #include <comphelper/streamsection.hxx>
      43             : #include <comphelper/seqstream.hxx>
      44             : #include <com/sun/star/io/XActiveDataSource.hpp>
      45             : #include <com/sun/star/io/XActiveDataSink.hpp>
      46             : #include "sqlmessage.hxx"
      47             : #include "JoinController.hxx"
      48             : #include <vcl/msgbox.hxx>
      49             : #include "TableWindowData.hxx"
      50             : #include "TableWindow.hxx"
      51             : #include "TableConnectionData.hxx"
      52             : #include "adtabdlg.hxx"
      53             : #include <vcl/waitobj.hxx>
      54             : #include <vcl/svapp.hxx>
      55             : #include <osl/mutex.hxx>
      56             : #include "UITools.hxx"
      57             : #include <osl/diagnose.h>
      58             : 
      59             : #include <boost/optional.hpp>
      60             : #include <boost/scoped_ptr.hpp>
      61             : 
      62             : using namespace ::com::sun::star::uno;
      63             : using namespace ::com::sun::star::io;
      64             : using namespace ::com::sun::star::beans;
      65             : using namespace ::com::sun::star::frame;
      66             : using namespace ::com::sun::star::util;
      67             : using namespace ::com::sun::star::lang;
      68             : using namespace ::com::sun::star::container;
      69             : using namespace ::com::sun::star::sdbcx;
      70             : using namespace ::com::sun::star::sdbc;
      71             : using namespace ::com::sun::star::sdb;
      72             : using namespace ::com::sun::star::ui::dialogs;
      73             : using namespace ::dbtools;
      74             : using namespace ::comphelper;
      75             : 
      76             : namespace dbaui
      77             : {
      78             : 
      79             : // AddTableDialogContext
      80             : class AddTableDialogContext : public IAddTableDialogContext
      81             : {
      82             :     OJoinController& m_rController;
      83             : 
      84             : public:
      85           0 :     AddTableDialogContext( OJoinController& _rController )
      86           0 :         :m_rController( _rController )
      87             :     {
      88           0 :     }
      89             : 
      90           0 :     virtual ~AddTableDialogContext() {}
      91             : 
      92             :     // IAddTableDialogContext
      93             :     virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >
      94             :                     getConnection() const SAL_OVERRIDE;
      95             :     virtual bool    allowViews() const SAL_OVERRIDE;
      96             :     virtual bool    allowQueries() const SAL_OVERRIDE;
      97             :     virtual bool    allowAddition() const SAL_OVERRIDE;
      98             :     virtual void    addTableWindow( const OUString& _rQualifiedTableName, const OUString& _rAliasName ) SAL_OVERRIDE;
      99             :     virtual void    onWindowClosing( const vcl::Window* _pWindow ) SAL_OVERRIDE;
     100             : 
     101             : private:
     102             :     OJoinTableView* getTableView() const;
     103             : };
     104             : 
     105           0 : Reference< XConnection > AddTableDialogContext::getConnection() const
     106             : {
     107           0 :     return m_rController.getConnection();
     108             : }
     109             : 
     110           0 : bool AddTableDialogContext::allowViews() const
     111             : {
     112           0 :     return m_rController.allowViews();
     113             : }
     114             : 
     115           0 : bool AddTableDialogContext::allowQueries() const
     116             : {
     117           0 :     return m_rController.allowQueries();
     118             : }
     119             : 
     120           0 : bool AddTableDialogContext::allowAddition() const
     121             : {
     122           0 :     return const_cast< OJoinController& >( m_rController ).getJoinView()->getTableView()->IsAddAllowed();
     123             : }
     124             : 
     125           0 : void AddTableDialogContext::addTableWindow( const OUString& _rQualifiedTableName, const OUString& _rAliasName )
     126             : {
     127           0 :     getTableView()->AddTabWin( _rQualifiedTableName, _rAliasName, true );
     128           0 : }
     129             : 
     130           0 : void AddTableDialogContext::onWindowClosing( const vcl::Window* _pWindow )
     131             : {
     132           0 :     if ( !m_rController.getView() )
     133           0 :         return;
     134             : 
     135             :     ::dbaui::notifySystemWindow(
     136           0 :         m_rController.getView(), const_cast< vcl::Window* >( _pWindow ), ::comphelper::mem_fun( &TaskPaneList::RemoveWindow ) );
     137             : 
     138           0 :     m_rController.InvalidateFeature( ID_BROWSER_ADDTABLE );
     139           0 :     m_rController.getView()->GrabFocus();
     140             : }
     141             : 
     142           0 : OJoinTableView* AddTableDialogContext::getTableView() const
     143             : {
     144           0 :     if ( m_rController.getJoinView() )
     145           0 :         return m_rController.getJoinView()->getTableView();
     146           0 :     return NULL;
     147             : }
     148             : 
     149             : // OJoinController
     150             : 
     151           2 : OJoinController::OJoinController(const Reference< XComponentContext >& _rM)
     152             :     :OJoinController_BASE(_rM)
     153           2 :     ,m_pAddTableDialog(NULL)
     154             : {
     155           2 : }
     156             : 
     157           2 : OJoinController::~OJoinController()
     158             : {
     159           2 : }
     160             : 
     161           0 : void SAL_CALL OJoinController::disposing( const EventObject& _rSource ) throw(RuntimeException, std::exception)
     162             : {
     163           0 :     OJoinController_BASE::disposing( _rSource );
     164           0 : }
     165             : 
     166           0 : OJoinDesignView* OJoinController::getJoinView()
     167             : {
     168           0 :     return static_cast< OJoinDesignView* >( getView() );
     169             : }
     170             : 
     171           2 : void OJoinController::disposing()
     172             : {
     173           2 :     m_pAddTableDialog.disposeAndClear();
     174             : 
     175           2 :     OJoinController_BASE::disposing();
     176             : 
     177           2 :     clearView();
     178             : 
     179           2 :     m_vTableConnectionData.clear();
     180           2 :     m_vTableData.clear();
     181           2 : }
     182             : 
     183           0 : void OJoinController::reconnect( bool _bUI )
     184             : {
     185           0 :     OJoinController_BASE::reconnect( _bUI );
     186           0 :     if ( isConnected() && m_pAddTableDialog )
     187           0 :         m_pAddTableDialog->Update();
     188           0 : }
     189             : 
     190           0 : void OJoinController::impl_onModifyChanged()
     191             : {
     192           0 :     OJoinController_BASE::impl_onModifyChanged();
     193           0 :     InvalidateFeature( SID_RELATION_ADD_RELATION );
     194           0 : }
     195             : 
     196           0 : void OJoinController::SaveTabWinPosSize(OTableWindow* pTabWin, long nOffsetX, long nOffsetY)
     197             : {
     198             :     // the data for the window
     199           0 :     TTableWindowData::value_type pData = pTabWin->GetData();
     200             :     OSL_ENSURE(pData != 0, "SaveTabWinPosSize : TabWin hat keine Daten !");
     201             : 
     202             :     // set Position & Size of data anew (with current window parameters)
     203           0 :     Point aPos = pTabWin->GetPosPixel();
     204           0 :     aPos.X() += nOffsetX;
     205           0 :     aPos.Y() += nOffsetY;
     206           0 :     pData->SetPosition(aPos);
     207           0 :     pData->SetSize(pTabWin->GetSizePixel());
     208             : 
     209           0 : }
     210             : 
     211           0 : FeatureState OJoinController::GetState(sal_uInt16 _nId) const
     212             : {
     213           0 :     FeatureState aReturn;
     214             :         // (disabled automatically)
     215           0 :     aReturn.bEnabled = true;
     216             : 
     217           0 :     switch (_nId)
     218             :     {
     219             :         case ID_BROWSER_EDITDOC:
     220           0 :             aReturn.bChecked = isEditable();
     221           0 :             break;
     222             :         case ID_BROWSER_ADDTABLE:
     223           0 :             aReturn.bEnabled = ( getView() != NULL )
     224           0 :                             && const_cast< OJoinController* >( this )->getJoinView()->getTableView()->IsAddAllowed();
     225           0 :             aReturn.bChecked = aReturn.bEnabled && m_pAddTableDialog != nullptr && m_pAddTableDialog->IsVisible() ;
     226           0 :             if ( aReturn.bEnabled )
     227           0 :                 aReturn.sTitle = OAddTableDlg::getDialogTitleForContext( impl_getDialogContext() );
     228           0 :             break;
     229             : 
     230             :         default:
     231           0 :             aReturn = OJoinController_BASE::GetState(_nId);
     232             :     }
     233           0 :     return aReturn;
     234             : }
     235             : 
     236           0 : AddTableDialogContext& OJoinController::impl_getDialogContext() const
     237             : {
     238           0 :     if ( !m_pDialogContext.get() )
     239             :     {
     240           0 :         OJoinController* pNonConstThis = const_cast< OJoinController* >( this );
     241           0 :         pNonConstThis->m_pDialogContext.reset( new AddTableDialogContext( *pNonConstThis ) );
     242             :     }
     243           0 :     return *m_pDialogContext;
     244             : }
     245             : 
     246           0 : void OJoinController::Execute(sal_uInt16 _nId, const Sequence< PropertyValue >& aArgs)
     247             : {
     248           0 :     switch(_nId)
     249             :     {
     250             :         case ID_BROWSER_EDITDOC:
     251           0 :             if(isEditable())
     252             :             { // the state should be changed to not editable
     253           0 :                 switch (saveModified())
     254             :                 {
     255             :                     case RET_CANCEL:
     256             :                         // don't change anything here so return
     257           0 :                         return;
     258             :                     case RET_NO:
     259           0 :                         reset();
     260           0 :                         setModified(sal_False);     // and we are not modified yet
     261           0 :                         break;
     262             :                     default:
     263           0 :                         break;
     264             :                 }
     265             :             }
     266           0 :             setEditable(!isEditable());
     267           0 :             getJoinView()->setReadOnly(!isEditable());
     268           0 :             InvalidateAll();
     269           0 :             return;
     270             :         case ID_BROWSER_ADDTABLE:
     271           0 :             if ( !m_pAddTableDialog )
     272           0 :                 m_pAddTableDialog = VclPtr<OAddTableDlg>::Create( getView(), impl_getDialogContext() );
     273             : 
     274           0 :             if ( m_pAddTableDialog->IsVisible() )
     275             :             {
     276           0 :                 m_pAddTableDialog->Show( false );
     277           0 :                 getView()->GrabFocus();
     278             :             }
     279             :             else
     280             :             {
     281             :                 {
     282           0 :                     WaitObject aWaitCursor( getView() );
     283           0 :                     m_pAddTableDialog->Update();
     284             :                 }
     285           0 :                 m_pAddTableDialog->Show( true );
     286           0 :                 ::dbaui::notifySystemWindow(getView(),m_pAddTableDialog,::comphelper::mem_fun(&TaskPaneList::AddWindow));
     287             :             }
     288           0 :             break;
     289             :         default:
     290           0 :             OJoinController_BASE::Execute(_nId,aArgs);
     291             :     }
     292           0 :     InvalidateFeature(_nId);
     293             : }
     294             : 
     295           0 : void OJoinController::SaveTabWinsPosSize( OJoinTableView::OTableWindowMap* pTabWinList, long nOffsetX, long nOffsetY )
     296             : {
     297             :     // Deletion and recreation of the old implementation with the current model is not correct anymore:
     298             :     // The TabWins have a pointer to their data, but they are managed by me. When I delete the old ones, the TabWins suddenly have a pointer to objects, which no longer exist.
     299             :     // If the TabWins had a SetData, I could save that effort... but they don't, further I also would still have to set information anew, which actually didn't change.
     300             :     // So I don't delete the TabWinDatas, but only update them.
     301             :     OSL_ENSURE(m_vTableData.size() == pTabWinList->size(),
     302             :         "OJoinController::SaveTabWinsPosSize : inkonsistenter Zustand : sollte genausviel TabWinDatas haben wie TabWins !");
     303             : 
     304           0 :     OJoinTableView::OTableWindowMap::iterator aIter = pTabWinList->begin();
     305           0 :     OJoinTableView::OTableWindowMap::iterator aEnd = pTabWinList->end();
     306           0 :     for(;aIter != aEnd;++aIter)
     307           0 :         SaveTabWinPosSize(aIter->second, nOffsetX, nOffsetY);
     308           0 : }
     309             : 
     310           0 : void OJoinController::removeConnectionData(const TTableConnectionData::value_type& _pData)
     311             : {
     312           0 :     m_vTableConnectionData.erase( ::std::remove(m_vTableConnectionData.begin(),m_vTableConnectionData.end(),_pData),m_vTableConnectionData.end());
     313           0 : }
     314             : 
     315           0 : void OJoinController::describeSupportedFeatures()
     316             : {
     317           0 :     OJoinController_BASE::describeSupportedFeatures();
     318           0 :     implDescribeSupportedFeature( ".uno:Redo",      ID_BROWSER_REDO,    CommandGroup::EDIT );
     319           0 :     implDescribeSupportedFeature( ".uno:Save",      ID_BROWSER_SAVEDOC, CommandGroup::DOCUMENT );
     320           0 :     implDescribeSupportedFeature( ".uno:Undo",      ID_BROWSER_UNDO,    CommandGroup::EDIT );
     321           0 :     implDescribeSupportedFeature( ".uno:AddTable",  ID_BROWSER_ADDTABLE,CommandGroup::EDIT );
     322           0 :     implDescribeSupportedFeature( ".uno:EditDoc",   ID_BROWSER_EDITDOC, CommandGroup::EDIT );
     323           0 : }
     324             : 
     325           0 : sal_Bool SAL_CALL OJoinController::suspend(sal_Bool _bSuspend) throw( RuntimeException, std::exception )
     326             : {
     327           0 :     if ( getBroadcastHelper().bInDispose || getBroadcastHelper().bDisposed )
     328           0 :         return sal_True;
     329             : 
     330           0 :     SolarMutexGuard aSolarGuard;
     331           0 :     ::osl::MutexGuard aGuard( getMutex() );
     332           0 :     if ( getView() && getView()->IsInModalMode() )
     333           0 :         return sal_False;
     334           0 :     bool bCheck = true;
     335           0 :     if ( _bSuspend )
     336             :     {
     337           0 :         bCheck = saveModified() != RET_CANCEL;
     338           0 :         if ( bCheck )
     339           0 :             OSingleDocumentController::suspend(_bSuspend);
     340             :     }
     341           0 :     return bCheck;
     342             : }
     343             : 
     344           0 : void OJoinController::loadTableWindows( const ::comphelper::NamedValueCollection& i_rViewSettings )
     345             : {
     346           0 :     m_vTableData.clear();
     347             : 
     348           0 :     m_aMinimumTableViewSize = Point();
     349             : 
     350           0 :     Sequence< PropertyValue > aWindowData;
     351           0 :     aWindowData = i_rViewSettings.getOrDefault( "Tables", aWindowData );
     352             : 
     353           0 :     const PropertyValue* pTablesIter = aWindowData.getConstArray();
     354           0 :     const PropertyValue* pTablesEnd = pTablesIter + aWindowData.getLength();
     355           0 :     for ( ; pTablesIter != pTablesEnd; ++pTablesIter )
     356             :     {
     357           0 :         ::comphelper::NamedValueCollection aSingleTableData( pTablesIter->Value );
     358           0 :         loadTableWindow( aSingleTableData );
     359           0 :     }
     360           0 :     if ( m_aMinimumTableViewSize != Point() )
     361             :     {
     362           0 :         getJoinView()->getScrollHelper()->resetRange( m_aMinimumTableViewSize );
     363           0 :     }
     364           0 : }
     365             : 
     366           0 : void OJoinController::loadTableWindow( const ::comphelper::NamedValueCollection& i_rTableWindowSettings )
     367             : {
     368           0 :     sal_Int32 nX = -1, nY = -1, nHeight = -1, nWidth = -1;
     369             : 
     370           0 :     OUString sComposedName,sTableName,sWindowName;
     371           0 :     bool bShowAll = false;
     372             : 
     373           0 :     sComposedName = i_rTableWindowSettings.getOrDefault( "ComposedName", sComposedName );
     374           0 :     sTableName = i_rTableWindowSettings.getOrDefault( "TableName", sTableName );
     375           0 :     sWindowName = i_rTableWindowSettings.getOrDefault( "WindowName", sWindowName );
     376           0 :     nY = i_rTableWindowSettings.getOrDefault( "WindowTop", nY );
     377           0 :     nX = i_rTableWindowSettings.getOrDefault( "WindowLeft", nX );
     378           0 :     nWidth = i_rTableWindowSettings.getOrDefault( "WindowWidth", nWidth );
     379           0 :     nHeight = i_rTableWindowSettings.getOrDefault( "WindowHeight", nHeight );
     380           0 :     bShowAll = i_rTableWindowSettings.getOrDefault( "ShowAll", bShowAll );
     381             : 
     382           0 :     TTableWindowData::value_type pData = createTableWindowData(sComposedName,sTableName,sWindowName);
     383           0 :     if ( pData )
     384             :     {
     385           0 :         pData->SetPosition(Point(nX,nY));
     386           0 :         pData->SetSize( Size( nWidth, nHeight ) );
     387           0 :         pData->ShowAll(bShowAll);
     388           0 :         m_vTableData.push_back(pData);
     389           0 :         if ( m_aMinimumTableViewSize.X() < (nX+nWidth) )
     390           0 :             m_aMinimumTableViewSize.X() = (nX+nWidth);
     391           0 :         if ( m_aMinimumTableViewSize.Y() < (nY+nHeight) )
     392           0 :             m_aMinimumTableViewSize.Y() = (nY+nHeight);
     393           0 :     }
     394           0 : }
     395             : 
     396           0 : void OJoinController::saveTableWindows( ::comphelper::NamedValueCollection& o_rViewSettings ) const
     397             : {
     398           0 :     if ( !m_vTableData.empty() )
     399             :     {
     400           0 :         ::comphelper::NamedValueCollection aAllTablesData;
     401             : 
     402           0 :         TTableWindowData::const_iterator aIter = m_vTableData.begin();
     403           0 :         TTableWindowData::const_iterator aEnd = m_vTableData.end();
     404           0 :         for ( sal_Int32 i = 1; aIter != aEnd; ++aIter, ++i )
     405             :         {
     406           0 :             ::comphelper::NamedValueCollection aWindowData;
     407           0 :             aWindowData.put( "ComposedName", (*aIter)->GetComposedName() );
     408           0 :             aWindowData.put( "TableName", (*aIter)->GetTableName() );
     409           0 :             aWindowData.put( "WindowName", (*aIter)->GetWinName() );
     410           0 :             aWindowData.put( "WindowTop", static_cast<sal_Int32>((*aIter)->GetPosition().Y()) );
     411           0 :             aWindowData.put( "WindowLeft", static_cast<sal_Int32>((*aIter)->GetPosition().X()) );
     412           0 :             aWindowData.put( "WindowWidth", static_cast<sal_Int32>((*aIter)->GetSize().Width()) );
     413           0 :             aWindowData.put( "WindowHeight", static_cast<sal_Int32>((*aIter)->GetSize().Height()) );
     414           0 :             aWindowData.put( "ShowAll", (*aIter)->IsShowAll() );
     415             : 
     416           0 :             const OUString sTableName( "Table" + OUString::number( i ) );
     417           0 :             aAllTablesData.put( sTableName, aWindowData.getPropertyValues() );
     418           0 :         }
     419             : 
     420           0 :         o_rViewSettings.put( "Tables", aAllTablesData.getPropertyValues() );
     421             :     }
     422           0 : }
     423             : 
     424           0 : TTableWindowData::value_type OJoinController::createTableWindowData(const OUString& _sComposedName,const OUString& _sTableName,const OUString& _sWindowName)
     425             : {
     426           0 :     OJoinDesignView* pView = getJoinView();
     427           0 :     if( pView )
     428           0 :         return pView->getTableView()->createTableWindowData(_sComposedName,_sTableName,_sWindowName);
     429             :     OSL_FAIL("We should never ever reach this point!");
     430             : 
     431           0 :     return TTableWindowData::value_type();
     432             : }
     433             : 
     434          36 : }   // namespace dbaui
     435             : 
     436             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11