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

Generated by: LCOV version 1.10