LCOV - code coverage report
Current view: top level - dbaccess/source/ui/app - AppControllerDnD.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 15 360 4.2 %
Date: 2014-11-03 Functions: 5 18 27.8 %
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 "AppController.hxx"
      21             : #include <comphelper/sequence.hxx>
      22             : #include <comphelper/property.hxx>
      23             : #include <comphelper/processfactory.hxx>
      24             : #include "dbustrings.hrc"
      25             : #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
      26             : #include <com/sun/star/sdbcx/XAppend.hpp>
      27             : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
      28             : #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
      29             : #include <com/sun/star/container/XNameContainer.hpp>
      30             : #include <com/sun/star/uno/XNamingService.hpp>
      31             : #include <com/sun/star/sdbc/XDataSource.hpp>
      32             : #include <com/sun/star/frame/XStorable.hpp>
      33             : #include <com/sun/star/container/XChild.hpp>
      34             : #include <com/sun/star/container/XHierarchicalNameContainer.hpp>
      35             : #include <com/sun/star/sdbc/DataType.hpp>
      36             : #include <com/sun/star/sdb/CommandType.hpp>
      37             : #include <com/sun/star/sdb/XBookmarksSupplier.hpp>
      38             : #include <com/sun/star/sdb/SQLContext.hpp>
      39             : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
      40             : #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
      41             : #include <com/sun/star/sdb/XQueryDefinitionsSupplier.hpp>
      42             : #include <com/sun/star/sdbcx/XDrop.hpp>
      43             : #include <unotools/ucbhelper.hxx>
      44             : #include "dlgsave.hxx"
      45             : #include <comphelper/types.hxx>
      46             : #include <vcl/layout.hxx>
      47             : #include <cppuhelper/typeprovider.hxx>
      48             : #include <cppuhelper/exc_hlp.hxx>
      49             : #include <connectivity/dbexception.hxx>
      50             : #include <vcl/waitobj.hxx>
      51             : #include <rtl/ustrbuf.hxx>
      52             : #include "AppView.hxx"
      53             : #include <svx/dataaccessdescriptor.hxx>
      54             : #include <svx/dbaobjectex.hxx>
      55             : #include "browserids.hxx"
      56             : #include "dbu_reghelper.hxx"
      57             : #include "dbu_app.hrc"
      58             : #include <vcl/menu.hxx>
      59             : #include <comphelper/uno3.hxx>
      60             : #include <vcl/svapp.hxx>
      61             : #include <svtools/svlbitm.hxx>
      62             : #include "listviewitems.hxx"
      63             : #include "AppDetailView.hxx"
      64             : #include "linkeddocuments.hxx"
      65             : #include <vcl/lstbox.hxx>
      66             : #include <connectivity/dbtools.hxx>
      67             : #include "sqlmessage.hxx"
      68             : #include "dbexchange.hxx"
      69             : #include "UITools.hxx"
      70             : #include <algorithm>
      71             : #include <svtools/treelistbox.hxx>
      72             : #include <com/sun/star/sdb/XReportDocumentsSupplier.hpp>
      73             : #include <com/sun/star/sdb/XFormDocumentsSupplier.hpp>
      74             : #include <unotools/pathoptions.hxx>
      75             : #include <sfx2/docfilt.hxx>
      76             : #include <svtools/fileview.hxx>
      77             : #include <tools/diagnose_ex.h>
      78             : #include <osl/diagnose.h>
      79             : #include "defaultobjectnamecheck.hxx"
      80             : #include <osl/mutex.hxx>
      81             : #include "subcomponentmanager.hxx"
      82             : 
      83             : namespace dbaui
      84             : {
      85             : using namespace ::dbtools;
      86             : using namespace ::svx;
      87             : using namespace ::svtools;
      88             : using namespace ::com::sun::star::uno;
      89             : using namespace ::com::sun::star::task;
      90             : using namespace ::com::sun::star::beans;
      91             : using namespace ::com::sun::star::lang;
      92             : using namespace ::com::sun::star::container;
      93             : using namespace ::com::sun::star::sdb;
      94             : using namespace ::com::sun::star::sdbc;
      95             : using namespace ::com::sun::star::sdbcx;
      96             : using namespace ::com::sun::star::frame;
      97             : using namespace ::com::sun::star::ucb;
      98             : using namespace ::com::sun::star::util;
      99             : 
     100           0 : void OApplicationController::deleteTables(const ::std::vector< OUString>& _rList)
     101             : {
     102           0 :     SharedConnection xConnection( ensureConnection() );
     103             : 
     104           0 :     Reference<XTablesSupplier> xSup(xConnection,UNO_QUERY);
     105             :     OSL_ENSURE(xSup.is(),"OApplicationController::deleteTable: no XTablesSuppier!");
     106           0 :     if ( xSup.is() )
     107             :     {
     108           0 :         Reference<XNameAccess> xTables = xSup->getTables();
     109           0 :         Reference<XDrop> xDrop(xTables,UNO_QUERY);
     110           0 :         if ( xDrop.is() )
     111             :         {
     112           0 :             bool bConfirm = true;
     113           0 :             ::std::vector< OUString>::const_iterator aEnd = _rList.end();
     114           0 :             for (::std::vector< OUString>::const_iterator aIter = _rList.begin(); aIter != aEnd; ++aIter)
     115             :             {
     116           0 :                 OUString sTableName = *aIter;
     117             : 
     118           0 :                 sal_Int32 nResult = RET_YES;
     119           0 :                 if ( bConfirm )
     120           0 :                     nResult = ::dbaui::askForUserAction(getView(),STR_TITLE_CONFIRM_DELETION ,STR_QUERY_DELETE_TABLE,_rList.size() > 1 && (aIter+1) != _rList.end(),sTableName);
     121             : 
     122             :                 bool bUserConfirmedDelete =
     123             :                             ( RET_YES == nResult )
     124           0 :                         ||  ( RET_ALL == nResult );
     125           0 :                 if ( bUserConfirmedDelete && m_pSubComponentManager->closeSubFrames( sTableName, E_TABLE ) )
     126             :                 {
     127           0 :                     SQLExceptionInfo aErrorInfo;
     128             :                     try
     129             :                     {
     130           0 :                         if ( xTables->hasByName(sTableName) )
     131           0 :                             xDrop->dropByName(sTableName);
     132             :                         else
     133             :                         {// could be a view
     134           0 :                             Reference<XViewsSupplier> xViewsSup(xConnection,UNO_QUERY);
     135             : 
     136           0 :                             Reference<XNameAccess> xViews;
     137           0 :                             if ( xViewsSup.is() )
     138             :                             {
     139           0 :                                 xViews = xViewsSup->getViews();
     140           0 :                                 if ( xViews.is() && xViews->hasByName(sTableName) )
     141             :                                 {
     142           0 :                                     xDrop.set(xViews,UNO_QUERY);
     143           0 :                                     if ( xDrop.is() )
     144           0 :                                         xDrop->dropByName(sTableName);
     145             :                                 }
     146           0 :                             }
     147             :                         }
     148             :                     }
     149           0 :                     catch(SQLContext& e) { aErrorInfo = e; }
     150           0 :                     catch(SQLWarning& e) { aErrorInfo = e; }
     151           0 :                     catch(SQLException& e) { aErrorInfo = e; }
     152           0 :                     catch(WrappedTargetException& e)
     153             :                     {
     154           0 :                         SQLException aSql;
     155           0 :                         if(e.TargetException >>= aSql)
     156           0 :                             aErrorInfo = aSql;
     157             :                         else
     158           0 :                             OSL_FAIL("OApplicationController::implDropTable: something strange happened!");
     159             :                     }
     160           0 :                     catch( const Exception& )
     161             :                     {
     162             :                         DBG_UNHANDLED_EXCEPTION();
     163             :                     }
     164             : 
     165           0 :                     if ( aErrorInfo.isValid() )
     166           0 :                         showError(aErrorInfo);
     167             : 
     168           0 :                     if ( RET_ALL == nResult )
     169           0 :                         bConfirm = false;
     170             :                 }
     171             :                 else
     172           0 :                     break;
     173           0 :             }
     174             :         }
     175             :         else
     176             :         {
     177           0 :             OUString sMessage(ModuleRes(STR_MISSING_TABLES_XDROP));
     178           0 :             MessageDialog aError(getView(), sMessage);
     179           0 :             aError.Execute();
     180           0 :         }
     181           0 :     }
     182           0 : }
     183             : 
     184           0 : void OApplicationController::deleteObjects( ElementType _eType, const ::std::vector< OUString>& _rList, bool _bConfirm )
     185             : {
     186           0 :     Reference< XNameContainer > xNames( getElements( _eType ), UNO_QUERY );
     187           0 :     Reference< XHierarchicalNameContainer > xHierarchyName( xNames, UNO_QUERY );
     188           0 :     if ( xNames.is() )
     189             :     {
     190           0 :         OString sDialogPosition;
     191           0 :         short eResult = _bConfirm ? svtools::QUERYDELETE_YES : svtools::QUERYDELETE_ALL;
     192             : 
     193             :         // The list of elements to delete is allowed to contain related elements: A given element may
     194             :         // be the ancestor or child of another element from the list.
     195             :         // We want to ensure that ancestors get deleted first, so we normalize the list in this respect.
     196             :         // #i33353#
     197           0 :         ::std::set< OUString > aDeleteNames;
     198             :             // Note that this implicitly uses ::std::less< OUString > a comparison operation, which
     199             :             // results in lexicographical order, which is exactly what we need, because "foo" is *before*
     200             :             // any "foo/bar" in this order.
     201             :         ::std::copy(
     202             :             _rList.begin(), _rList.end(),
     203             :             ::std::insert_iterator< ::std::set< OUString > >( aDeleteNames, aDeleteNames.begin() )
     204           0 :         );
     205             : 
     206           0 :         ::std::set< OUString >::size_type nCount = aDeleteNames.size();
     207           0 :         for ( ::std::set< OUString >::size_type nObjectsLeft = nCount; !aDeleteNames.empty(); )
     208             :         {
     209           0 :             ::std::set< OUString >::iterator  aThisRound = aDeleteNames.begin();
     210             : 
     211           0 :             if ( eResult != svtools::QUERYDELETE_ALL )
     212             :             {
     213           0 :                 svtools::QueryDeleteDlg_Impl aDlg( getView(), *aThisRound );
     214             : 
     215           0 :                 if ( !sDialogPosition.isEmpty() )
     216           0 :                     aDlg.SetWindowState( sDialogPosition );
     217             : 
     218           0 :                 if ( nObjectsLeft > 1 )
     219           0 :                     aDlg.EnableAllButton();
     220             : 
     221           0 :                 eResult = aDlg.Execute();
     222           0 :                 if (eResult == svtools::QUERYDELETE_CANCEL)
     223           0 :                     return;
     224             : 
     225           0 :                 sDialogPosition = aDlg.GetWindowState( );
     226             :             }
     227             : 
     228           0 :             bool bSuccess = false;
     229             : 
     230             :             bool bUserConfirmedDelete =
     231             :                         ( eResult == svtools::QUERYDELETE_ALL )
     232           0 :                     ||  ( eResult == svtools::QUERYDELETE_YES );
     233             : 
     234           0 :             if  (   bUserConfirmedDelete
     235           0 :                 &&  (   ( _eType == E_QUERY ) ? m_pSubComponentManager->closeSubFrames( *aThisRound, _eType ) : true )
     236             :                 )
     237             :             {
     238             :                 try
     239             :                 {
     240           0 :                     if ( xHierarchyName.is() )
     241           0 :                         xHierarchyName->removeByHierarchicalName( *aThisRound );
     242             :                     else
     243           0 :                         xNames->removeByName( *aThisRound );
     244             : 
     245           0 :                     bSuccess = true;
     246             : 
     247             :                     // now that we removed the element, care for all its child elements
     248             :                     // which may also be a part of the list
     249             :                     // #i33353#
     250             :                     OSL_ENSURE( aThisRound->getLength() - 1 >= 0, "OApplicationController::deleteObjects: empty name?" );
     251           0 :                     OUStringBuffer sSmallestSiblingName( *aThisRound );
     252           0 :                     sSmallestSiblingName.append( (sal_Unicode)( '/' + 1) );
     253             : 
     254           0 :                     ::std::set< OUString >::iterator aUpperChildrenBound = aDeleteNames.lower_bound( sSmallestSiblingName.makeStringAndClear() );
     255           0 :                     for ( ::std::set< OUString >::iterator aObsolete = aThisRound;
     256             :                           aObsolete != aUpperChildrenBound;
     257             :                         )
     258             :                     {
     259           0 :                         ::std::set< OUString >::iterator aNextObsolete = aObsolete; ++aNextObsolete;
     260           0 :                         aDeleteNames.erase( aObsolete );
     261           0 :                         --nObjectsLeft;
     262           0 :                         aObsolete = aNextObsolete;
     263           0 :                     }
     264             :                 }
     265           0 :                 catch(const SQLException&)
     266             :                 {
     267           0 :                     showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
     268             :                 }
     269           0 :                 catch(const WrappedTargetException& e)
     270             :                 {
     271           0 :                     SQLException aSql;
     272           0 :                     if ( e.TargetException >>= aSql )
     273           0 :                         showError( SQLExceptionInfo( e.TargetException ) );
     274             :                     else
     275           0 :                         OSL_FAIL( "OApplicationController::deleteObjects: something strange happened!" );
     276             :                 }
     277           0 :                 catch( const Exception& )
     278             :                 {
     279             :                     DBG_UNHANDLED_EXCEPTION();
     280             :                 }
     281             :             }
     282             : 
     283           0 :             if ( !bSuccess )
     284             :             {
     285             :                 // okay, this object could not be deleted (or the user did not want to delete it),
     286             :                 // but continue with the rest
     287           0 :                 aDeleteNames.erase( aThisRound );
     288           0 :                 --nObjectsLeft;
     289             :             }
     290           0 :         }
     291           0 :     }
     292             : }
     293             : 
     294           0 : void OApplicationController::deleteEntries()
     295             : {
     296           0 :     SolarMutexGuard aSolarGuard;
     297           0 :     ::osl::MutexGuard aGuard( getMutex() );
     298             : 
     299           0 :     if ( getContainer() )
     300             :     {
     301           0 :         ::std::vector< OUString> aList;
     302           0 :         getSelectionElementNames(aList);
     303           0 :         ElementType eType = getContainer()->getElementType();
     304           0 :         switch(eType)
     305             :         {
     306             :         case E_TABLE:
     307           0 :             deleteTables(aList);
     308           0 :             break;
     309             :         case E_QUERY:
     310           0 :             deleteObjects( E_QUERY, aList, true );
     311           0 :             break;
     312             :         case E_FORM:
     313           0 :             deleteObjects( E_FORM, aList, true );
     314           0 :             break;
     315             :         case E_REPORT:
     316           0 :             deleteObjects( E_REPORT, aList, true );
     317           0 :             break;
     318             :         case E_NONE:
     319           0 :             break;
     320           0 :         }
     321           0 :     }
     322           0 : }
     323             : 
     324             : // DO NOT CALL with getMutex() held!!
     325           0 : const SharedConnection& OApplicationController::ensureConnection( ::dbtools::SQLExceptionInfo* _pErrorInfo )
     326             : {
     327             : 
     328             :     // This looks like double checked locking, but it is not,
     329             :     // because every access (read *or* write) to  m_xDataSourceConnection
     330             :     // is mutexed.
     331             :     // See http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
     332             :     // for what I'm referring to.
     333             :     // We cannot use the TLS (thread-local storage) solution
     334             :     // since support for TLS is not up to the snuff on Windows :-(
     335             : 
     336             :     {
     337           0 :         ::osl::MutexGuard aGuard( getMutex() );
     338             : 
     339           0 :         if ( m_xDataSourceConnection.is() )
     340           0 :             return m_xDataSourceConnection;
     341             :     }
     342             : 
     343           0 :     WaitObject aWO(getView());
     344           0 :     Reference<XConnection> conn;
     345             :     {
     346           0 :         SolarMutexGuard aSolarGuard;
     347             : 
     348           0 :         OUString sConnectingContext( ModuleRes( STR_COULDNOTCONNECT_DATASOURCE ) );
     349           0 :         sConnectingContext = sConnectingContext.replaceFirst("$name$", getStrippedDatabaseName());
     350             : 
     351             :         // do the connection *without* holding getMutex() to avoid deadlock
     352             :         // when we are not in the main thread and we need username/password
     353             :         // (and thus to display a dialog, which will be done by the main thread)
     354             :         // and there is an event that needs getMutex() *before* us in the main thread's queue
     355             :         // See fdo#63391
     356           0 :         conn.set( connect( getDatabaseName(), sConnectingContext, _pErrorInfo ) );
     357             :     }
     358             : 
     359           0 :     if (conn.is())
     360             :     {
     361           0 :         ::osl::MutexGuard aGuard( getMutex() );
     362           0 :         if ( m_xDataSourceConnection.is() )
     363             :         {
     364           0 :             Reference< XComponent > comp (conn, UNO_QUERY);
     365           0 :             if(comp.is())
     366             :             {
     367             :                 try
     368             :                 {
     369           0 :                     comp->dispose();
     370             :                 }
     371           0 :                 catch( const Exception& )
     372             :                 {
     373             :                     OSL_FAIL( "dbaui::OApplicationController::ensureConnection could not dispose of temporary unused connection" );
     374             :                 }
     375             :             }
     376           0 :             conn.clear();
     377             :         }
     378             :         else
     379             :         {
     380           0 :             m_xDataSourceConnection.reset(conn);
     381           0 :             SQLExceptionInfo aError;
     382             :             try
     383             :             {
     384           0 :                 m_xMetaData = m_xDataSourceConnection->getMetaData();
     385             :             }
     386           0 :             catch( const SQLException& )
     387             :             {
     388           0 :                 aError = ::cppu::getCaughtException();
     389             :             }
     390           0 :             catch( const Exception& )
     391             :             {
     392             :                 DBG_UNHANDLED_EXCEPTION();
     393             :             }
     394           0 :             if ( aError.isValid() )
     395             :             {
     396           0 :                 if ( _pErrorInfo )
     397             :                 {
     398           0 :                     *_pErrorInfo = aError;
     399             :                 }
     400             :                 else
     401             :                 {
     402           0 :                     SolarMutexGuard aSolarGuard;
     403           0 :                     showError( aError );
     404             :                 }
     405           0 :             }
     406           0 :         }
     407             :     }
     408             : 
     409           0 :     return m_xDataSourceConnection;
     410             : }
     411             : 
     412         504 : bool OApplicationController::isDataSourceReadOnly() const
     413             : {
     414         504 :     Reference<XStorable> xStore(m_xModel,UNO_QUERY);
     415         504 :     return !xStore.is() || xStore->isReadonly();
     416             : }
     417             : 
     418          54 : bool OApplicationController::isConnectionReadOnly() const
     419             : {
     420          54 :     bool bIsConnectionReadOnly = true;
     421          54 :     if ( m_xMetaData.is() )
     422             :     {
     423             :         try
     424             :         {
     425           0 :             bIsConnectionReadOnly = m_xMetaData->isReadOnly();
     426             :         }
     427           0 :         catch(const SQLException&)
     428             :         {
     429             :             DBG_UNHANDLED_EXCEPTION();
     430             :         }
     431             :     }
     432             :     // TODO check configuration
     433          54 :     return bIsConnectionReadOnly;
     434             : }
     435             : 
     436           6 : Reference< XNameAccess > OApplicationController::getElements( ElementType _eType )
     437             : {
     438           6 :     Reference< XNameAccess > xElements;
     439             :     try
     440             :     {
     441           6 :         switch ( _eType )
     442             :         {
     443             :         case E_REPORT:
     444             :         {
     445           0 :             Reference< XReportDocumentsSupplier > xSupp( m_xModel, UNO_QUERY_THROW );
     446           0 :             xElements.set( xSupp->getReportDocuments(), UNO_SET_THROW );
     447             :         }
     448           0 :         break;
     449             : 
     450             :         case E_FORM:
     451             :         {
     452           6 :             Reference< XFormDocumentsSupplier > xSupp( m_xModel, UNO_QUERY_THROW );
     453           6 :             xElements.set( xSupp->getFormDocuments(), UNO_SET_THROW );
     454             :         }
     455           6 :         break;
     456             : 
     457             :         case E_QUERY:
     458             :         {
     459           0 :             xElements.set( getQueryDefintions(), UNO_QUERY_THROW );
     460             :         }
     461           0 :         break;
     462             : 
     463             :         case E_TABLE:
     464             :         {
     465           0 :             if ( m_xDataSourceConnection.is() )
     466             :             {
     467           0 :                 Reference< XTablesSupplier > xSup( getConnection(), UNO_QUERY_THROW );
     468           0 :                 xElements.set( xSup->getTables(), UNO_SET_THROW );
     469             :             }
     470             :         }
     471           0 :         break;
     472             : 
     473             :         default:
     474           0 :             break;
     475             :         }
     476             :     }
     477           0 :     catch(const Exception&)
     478             :     {
     479             :         DBG_UNHANDLED_EXCEPTION();
     480             :     }
     481             : 
     482           6 :     return xElements;
     483             : }
     484             : 
     485           0 : void OApplicationController::getSelectionElementNames(::std::vector< OUString>& _rNames) const
     486             : {
     487           0 :     SolarMutexGuard aSolarGuard;
     488           0 :     ::osl::MutexGuard aGuard( getMutex() );
     489             : 
     490             :     OSL_ENSURE(getContainer(),"View isn't valid! -> GPF");
     491             : 
     492           0 :     getContainer()->getSelectionElementNames( _rNames );
     493           0 : }
     494             : 
     495           0 : ::std::unique_ptr< OLinkedDocumentsAccess > OApplicationController::getDocumentsAccess( ElementType _eType )
     496             : {
     497             :     OSL_ENSURE( ( _eType == E_TABLE ) || ( _eType == E_QUERY ) || ( _eType == E_FORM ) || ( _eType == E_REPORT ),
     498             :         "OApplicationController::getDocumentsAccess: only forms and reports are supported here!" );
     499             : 
     500           0 :     SharedConnection xConnection( ensureConnection() );
     501           0 :     Reference< XNameAccess > xDocContainer;
     502             : 
     503           0 :     if ( ( _eType == E_FORM ) || ( _eType == E_REPORT ) )
     504             :     {
     505           0 :         xDocContainer.set( getElements( _eType ) );
     506             :         OSL_ENSURE( xDocContainer.is(), "OApplicationController::getDocumentsAccess: invalid container!" );
     507             :     }
     508             : 
     509             :     ::std::unique_ptr< OLinkedDocumentsAccess > pDocuments( new OLinkedDocumentsAccess(
     510           0 :         getView(), this, getORB(), xDocContainer, xConnection, getDatabaseName()
     511           0 :     ) );
     512           0 :     return pDocuments;
     513             : }
     514             : 
     515           0 : TransferableHelper* OApplicationController::copyObject()
     516             : {
     517             :     try
     518             :     {
     519           0 :         SolarMutexGuard aSolarGuard;
     520           0 :         ::osl::MutexGuard aGuard( getMutex() );
     521             : 
     522           0 :         ElementType eType = getContainer()->getElementType();
     523           0 :         TransferableHelper* pData = NULL;
     524           0 :         switch( eType )
     525             :         {
     526             :             case E_TABLE:
     527             :             case E_QUERY:
     528             :             {
     529           0 :                 SharedConnection xConnection( ensureConnection() );
     530           0 :                 Reference< XDatabaseMetaData> xMetaData;
     531           0 :                 if ( xConnection.is() )
     532           0 :                     xMetaData = xConnection->getMetaData();
     533             : 
     534           0 :                 OUString sName = getContainer()->getQualifiedName( NULL );
     535           0 :                 if ( !sName.isEmpty() )
     536             :                 {
     537           0 :                     OUString sDataSource = getDatabaseName();
     538             : 
     539           0 :                     if ( eType == E_TABLE )
     540             :                     {
     541           0 :                         pData = new ODataClipboard(sDataSource, CommandType::TABLE, sName, xConnection, getNumberFormatter(xConnection, getORB()), getORB());
     542             :                     }
     543             :                     else
     544             :                     {
     545           0 :                         pData = new ODataClipboard(sDataSource, CommandType::QUERY, sName, getNumberFormatter(xConnection, getORB()), getORB());
     546           0 :                     }
     547           0 :                 }
     548             :             }
     549           0 :                 break;
     550             :             case E_FORM:
     551             :             case E_REPORT:
     552             :             {
     553           0 :                 ::std::vector< OUString> aList;
     554           0 :                 getSelectionElementNames(aList);
     555           0 :                 Reference< XHierarchicalNameAccess > xElements(getElements(eType),UNO_QUERY);
     556           0 :                 if ( xElements.is() && !aList.empty() )
     557             :                 {
     558           0 :                     Reference< XContent> xContent(xElements->getByHierarchicalName(*aList.begin()),UNO_QUERY);
     559           0 :                     pData = new OComponentTransferable( getDatabaseName(), xContent );
     560           0 :                 }
     561             :             }
     562           0 :             break;
     563             :             default:
     564           0 :                 break;
     565             :         }
     566             : 
     567             :         // the owner ship goes to ODataClipboards
     568           0 :         return pData;
     569             :     }
     570           0 :     catch(const SQLException&)
     571             :     {
     572           0 :         showError( SQLExceptionInfo( ::cppu::getCaughtException() ) );
     573             :     }
     574           0 :     catch( const Exception& )
     575             :     {
     576             :         DBG_UNHANDLED_EXCEPTION();
     577             :     }
     578           0 :     return NULL;
     579             : }
     580             : 
     581           0 : bool OApplicationController::paste( ElementType _eType, const ::svx::ODataAccessDescriptor& _rPasteData, const OUString& _sParentFolder, bool _bMove)
     582             : {
     583             :     try
     584             :     {
     585           0 :         if ( _eType == E_QUERY )
     586             :         {
     587           0 :             sal_Int32 nCommandType = CommandType::TABLE;
     588           0 :             if ( _rPasteData.has(daCommandType) )
     589           0 :                 _rPasteData[daCommandType]      >>= nCommandType;
     590             : 
     591           0 :             if ( CommandType::QUERY == nCommandType || CommandType::COMMAND == nCommandType )
     592             :             {
     593             :                 // read all necessary data
     594             : 
     595           0 :                 OUString sCommand;
     596           0 :                 bool bEscapeProcessing = true;
     597             : 
     598           0 :                 _rPasteData[daCommand] >>= sCommand;
     599           0 :                 if ( _rPasteData.has(daEscapeProcessing) )
     600           0 :                     _rPasteData[daEscapeProcessing] >>= bEscapeProcessing;
     601             : 
     602             :                 // plausibility check
     603           0 :                 bool bValidDescriptor = false;
     604           0 :                 OUString sDataSourceName = _rPasteData.getDataSource();
     605           0 :                 if (CommandType::QUERY == nCommandType)
     606           0 :                     bValidDescriptor = sDataSourceName.getLength() && sCommand.getLength();
     607           0 :                 else if (CommandType::COMMAND == nCommandType)
     608           0 :                     bValidDescriptor = !sCommand.isEmpty();
     609           0 :                 if (!bValidDescriptor)
     610             :                 {
     611             :                     OSL_FAIL("OApplicationController::paste: invalid descriptor!");
     612           0 :                     return false;
     613             :                 }
     614             : 
     615             :                 // the target object name (as we'll suggest it to the user)
     616           0 :                 OUString sTargetName;
     617             :                 try
     618             :                 {
     619           0 :                     if ( CommandType::QUERY == nCommandType )
     620           0 :                         sTargetName = sCommand;
     621             : 
     622           0 :                     if ( sTargetName.isEmpty() )
     623             :                     {
     624           0 :                         OUString sDefaultName = OUString( ModuleRes( STR_QRY_TITLE ) );
     625           0 :                         sDefaultName = sDefaultName.getToken( 0, ' ' );
     626             : 
     627           0 :                         Reference< XNameAccess > xQueries( getQueryDefintions(), UNO_QUERY_THROW );
     628           0 :                         sTargetName = ::dbtools::createUniqueName( xQueries, sDefaultName, false );
     629             :                     }
     630             :                 }
     631           0 :                 catch(const Exception&)
     632             :                 {
     633             :                     DBG_UNHANDLED_EXCEPTION();
     634             :                 }
     635             : 
     636           0 :                 Reference< XPropertySet > xQuery;
     637           0 :                 if (CommandType::QUERY == nCommandType)
     638             :                 {
     639             :                     // need to extract the statement and the escape processing flag from the query object
     640           0 :                     bool bSuccess = false;
     641             :                     try
     642             :                     {
     643             :                         // the concrete query
     644             :                         Reference< XQueryDefinitionsSupplier > xSourceQuerySup(
     645           0 :                             getDataSourceByName( sDataSourceName, getView(), getORB(), NULL ),
     646           0 :                             UNO_QUERY_THROW );
     647           0 :                         Reference< XNameAccess > xQueries( xSourceQuerySup->getQueryDefinitions(), UNO_SET_THROW );
     648           0 :                         if ( xQueries->hasByName( sCommand ) )
     649             :                         {
     650           0 :                             xQuery.set( xQueries->getByName(sCommand), UNO_QUERY_THROW );
     651           0 :                             bSuccess = true;
     652           0 :                         }
     653             :                     }
     654           0 :                     catch(SQLException&) { throw; } // caught and handled by the outer catch
     655           0 :                     catch( const Exception& )
     656             :                     {
     657             :                         DBG_UNHANDLED_EXCEPTION();
     658             :                     }
     659             : 
     660           0 :                     if (!bSuccess)
     661             :                     {
     662             :                         OSL_FAIL("OApplicationController::paste: could not extract the source query object!");
     663             :                         // TODO: maybe this is worth an error message to be displayed to the user ....
     664           0 :                         return false;
     665             :                     }
     666             :                 }
     667             : 
     668           0 :                 Reference< XNameContainer > xDestQueries(getQueryDefintions(), UNO_QUERY);
     669           0 :                 Reference< XSingleServiceFactory > xQueryFactory(xDestQueries, UNO_QUERY);
     670           0 :                 if (!xQueryFactory.is())
     671             :                 {
     672             :                     OSL_FAIL("OApplicationController::paste: invalid destination query container!");
     673           0 :                     return false;
     674             :                 }
     675             : 
     676             :                 // here we have everything needed to create a new query object ...
     677             :                 // ... ehm, except a new name
     678           0 :                 ensureConnection();
     679             : 
     680           0 :                 DynamicTableOrQueryNameCheck aNameChecker( getConnection(), CommandType::QUERY );
     681           0 :                 ::dbtools::SQLExceptionInfo aDummy;
     682           0 :                 bool bNeedAskForName =  ( sCommand.isEmpty() )
     683             :                                             /* we did not have a source name, so the target name was auto-generated */
     684           0 :                                     ||  ( !aNameChecker.isNameValid( sTargetName, aDummy ) );
     685             :                                             /*  name is invalid in the target DB (e.g. because it already
     686             :                                                 has a /table/ with that name) */
     687           0 :                 if ( bNeedAskForName )
     688             :                 {
     689           0 :                     OSaveAsDlg aAskForName( getView(),
     690             :                                             CommandType::QUERY,
     691             :                                             getORB(),
     692           0 :                                             getConnection(),
     693             :                                             sTargetName,
     694             :                                             aNameChecker,
     695           0 :                                             SAD_ADDITIONAL_DESCRIPTION | SAD_TITLE_PASTE_AS);
     696           0 :                     if ( RET_OK != aAskForName.Execute() )
     697             :                         // cancelled by the user
     698           0 :                         return false;
     699           0 :                     sTargetName = aAskForName.getName();
     700             :                 }
     701             : 
     702             :                 // create a new object
     703           0 :                 Reference< XPropertySet > xNewQuery(xQueryFactory->createInstance(), UNO_QUERY);
     704             :                 OSL_ENSURE(xNewQuery.is(), "OApplicationController::paste: invalid object created by factory!");
     705           0 :                 if (xNewQuery.is())
     706             :                 {
     707             :                     // initialize
     708           0 :                     if ( xQuery.is() )
     709           0 :                         ::comphelper::copyProperties(xQuery,xNewQuery);
     710             :                     else
     711             :                     {
     712           0 :                         xNewQuery->setPropertyValue(PROPERTY_COMMAND,makeAny(sCommand));
     713           0 :                         xNewQuery->setPropertyValue(PROPERTY_ESCAPE_PROCESSING,makeAny(bEscapeProcessing));
     714             :                     }
     715             :                     // insert
     716           0 :                     xDestQueries->insertByName( sTargetName, makeAny(xNewQuery) );
     717           0 :                     xNewQuery.set(xDestQueries->getByName( sTargetName),UNO_QUERY);
     718           0 :                     if ( xQuery.is() && xNewQuery.is() )
     719             :                     {
     720           0 :                         Reference<XColumnsSupplier> xSrcColSup(xQuery,UNO_QUERY);
     721           0 :                         Reference<XColumnsSupplier> xDstColSup(xNewQuery,UNO_QUERY);
     722           0 :                         if ( xSrcColSup.is() && xDstColSup.is() )
     723             :                         {
     724           0 :                             Reference<XNameAccess> xSrcNameAccess = xSrcColSup->getColumns();
     725           0 :                             Reference<XNameAccess> xDstNameAccess = xDstColSup->getColumns();
     726           0 :                             Reference<XDataDescriptorFactory> xFac(xDstNameAccess,UNO_QUERY);
     727           0 :                             Reference<XAppend> xAppend(xFac,UNO_QUERY);
     728           0 :                             if ( xSrcNameAccess.is() && xDstNameAccess.is() && xSrcNameAccess->hasElements() && xAppend.is() )
     729             :                             {
     730           0 :                                 Reference<XPropertySet> xDstProp(xFac->createDataDescriptor());
     731             : 
     732           0 :                                 Sequence< OUString> aSeq = xSrcNameAccess->getElementNames();
     733           0 :                                 const OUString* pIter = aSeq.getConstArray();
     734           0 :                                 const OUString* pEnd   = pIter + aSeq.getLength();
     735           0 :                                 for( ; pIter != pEnd ; ++pIter)
     736             :                                 {
     737           0 :                                     Reference<XPropertySet> xSrcProp(xSrcNameAccess->getByName(*pIter),UNO_QUERY);
     738           0 :                                     ::comphelper::copyProperties(xSrcProp,xDstProp);
     739           0 :                                     xAppend->appendByDescriptor(xDstProp);
     740           0 :                                 }
     741           0 :                             }
     742           0 :                         }
     743             :                     }
     744           0 :                 }
     745             :             }
     746             :             else
     747             :                 OSL_TRACE("There should be a sequence in it!");
     748           0 :             return true;
     749             :         }
     750           0 :         else if ( _rPasteData.has(daComponent) ) // forms or reports
     751             :         {
     752           0 :             Reference<XContent> xContent;
     753           0 :             _rPasteData[daComponent] >>= xContent;
     754           0 :             return insertHierachyElement(_eType,_sParentFolder,Reference<XNameAccess>(xContent,UNO_QUERY).is(),xContent,_bMove);
     755             :         }
     756             :     }
     757           0 :     catch(const SQLException&) { showError( SQLExceptionInfo( ::cppu::getCaughtException() ) ); }
     758           0 :     catch(const Exception& )
     759             :     {
     760             :         DBG_UNHANDLED_EXCEPTION();
     761             :     }
     762           0 :     return false;
     763             : }
     764             : 
     765           0 : Reference<XNameContainer> OApplicationController::getQueryDefintions() const
     766             : {
     767           0 :     Reference<XQueryDefinitionsSupplier> xSet(m_xDataSource,UNO_QUERY);
     768           0 :     Reference<XNameContainer> xNames;
     769           0 :     if ( xSet.is() )
     770             :     {
     771           0 :         xNames.set(xSet->getQueryDefinitions(),UNO_QUERY);
     772             :     }
     773           0 :     return xNames;
     774             : }
     775             : 
     776           0 : void OApplicationController::getSupportedFormats(ElementType _eType,::std::vector<SotFormatStringId>& _rFormatIds) const
     777             : {
     778           0 :     switch( _eType )
     779             :     {
     780             :         case E_TABLE:
     781           0 :             _rFormatIds.push_back(SOT_FORMATSTR_ID_DBACCESS_TABLE);
     782           0 :             _rFormatIds.push_back(SOT_FORMAT_RTF);
     783           0 :             _rFormatIds.push_back(SOT_FORMATSTR_ID_HTML);
     784             :             // run through
     785             :         case E_QUERY:
     786           0 :             _rFormatIds.push_back(SOT_FORMATSTR_ID_DBACCESS_QUERY);
     787           0 :             break;
     788             :         default:
     789           0 :             break;
     790             :     }
     791           0 : }
     792             : 
     793           0 : bool OApplicationController::isTableFormat()  const
     794             : {
     795           0 :     return m_aTableCopyHelper.isTableFormat(getViewClipboard());
     796             : }
     797             : 
     798           0 : IMPL_LINK( OApplicationController, OnAsyncDrop, void*, /*NOTINTERESTEDIN*/ )
     799             : {
     800           0 :     m_nAsyncDrop = 0;
     801           0 :     SolarMutexGuard aSolarGuard;
     802           0 :     ::osl::MutexGuard aGuard( getMutex() );
     803             : 
     804           0 :     if ( m_aAsyncDrop.nType == E_TABLE )
     805             :     {
     806           0 :         SharedConnection xConnection( ensureConnection() );
     807           0 :         if ( xConnection.is() )
     808           0 :             m_aTableCopyHelper.asyncCopyTagTable( m_aAsyncDrop, getDatabaseName(), xConnection );
     809             :     }
     810             :     else
     811             :     {
     812           0 :         if ( paste(m_aAsyncDrop.nType,m_aAsyncDrop.aDroppedData,m_aAsyncDrop.aUrl,m_aAsyncDrop.nAction == DND_ACTION_MOVE)
     813           0 :             && m_aAsyncDrop.nAction == DND_ACTION_MOVE )
     814             :         {
     815           0 :             Reference<XContent> xContent;
     816           0 :             m_aAsyncDrop.aDroppedData[daComponent] >>= xContent;
     817           0 :             ::std::vector< OUString> aList;
     818           0 :             sal_Int32 nIndex = 0;
     819           0 :             OUString sName = xContent->getIdentifier()->getContentIdentifier();
     820           0 :             OUString sErase = sName.getToken(0,'/',nIndex); // we don't want to have the "private:forms" part
     821           0 :             if ( nIndex != -1 )
     822             :             {
     823           0 :                 aList.push_back(sName.copy(sErase.getLength() + 1));
     824           0 :                 deleteObjects( m_aAsyncDrop.nType, aList, false );
     825           0 :             }
     826             :         }
     827             :     }
     828             : 
     829           0 :     m_aAsyncDrop.aDroppedData.clear();
     830             : 
     831           0 :     return 0L;
     832             : }
     833             : 
     834          72 : }   // namespace dbaui
     835             : 
     836             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10