LCOV - code coverage report
Current view: top level - sc/source/ui/docshell - dbdocimp.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 194 307 63.2 %
Date: 2014-11-03 Functions: 3 5 60.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 <comphelper/processfactory.hxx>
      21             : #include <comphelper/types.hxx>
      22             : #include <vcl/msgbox.hxx>
      23             : #include <svx/dataaccessdescriptor.hxx>
      24             : #include <sfx2/viewfrm.hxx>
      25             : 
      26             : #include <com/sun/star/sdb/CommandType.hpp>
      27             : #include <com/sun/star/sdb/XCompletedExecution.hpp>
      28             : #include <com/sun/star/sdbc/XRow.hpp>
      29             : #include <com/sun/star/sdbc/XRowSet.hpp>
      30             : #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
      31             : #include <com/sun/star/sdbcx/XRowLocate.hpp>
      32             : #include <com/sun/star/task/InteractionHandler.hpp>
      33             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      34             : #include <com/sun/star/beans/XPropertySet.hpp>
      35             : #include <com/sun/star/frame/XDispatchProvider.hpp>
      36             : #include <com/sun/star/frame/FrameSearchFlag.hpp>
      37             : #include <com/sun/star/view/XSelectionSupplier.hpp>
      38             : 
      39             : #include "dbdocfun.hxx"
      40             : #include "docsh.hxx"
      41             : #include "globstr.hrc"
      42             : #include "scerrors.hxx"
      43             : #include "dbdata.hxx"
      44             : #include "markdata.hxx"
      45             : #include "undodat.hxx"
      46             : #include "progress.hxx"
      47             : #include "patattr.hxx"
      48             : #include "docpool.hxx"
      49             : #include "attrib.hxx"
      50             : #include "dbdocutl.hxx"
      51             : #include "editable.hxx"
      52             : #include "hints.hxx"
      53             : #include "miscuno.hxx"
      54             : #include "chgtrack.hxx"
      55             : #include <refupdatecontext.hxx>
      56             : 
      57             : using namespace com::sun::star;
      58             : 
      59             : #define SC_SERVICE_ROWSET           "com.sun.star.sdb.RowSet"
      60             : 
      61             : //! move to a header file?
      62             : #define SC_DBPROP_DATASOURCENAME    "DataSourceName"
      63             : #define SC_DBPROP_COMMAND           "Command"
      64             : #define SC_DBPROP_COMMANDTYPE       "CommandType"
      65             : 
      66           0 : void ScDBDocFunc::ShowInBeamer( const ScImportParam& rParam, SfxViewFrame* pFrame )
      67             : {
      68             :     //  called after opening the database beamer
      69             : 
      70           0 :     if ( !pFrame || !rParam.bImport )
      71           0 :         return;
      72             : 
      73           0 :     uno::Reference<frame::XFrame> xFrame = pFrame->GetFrame().GetFrameInterface();
      74           0 :     uno::Reference<frame::XDispatchProvider> xDP(xFrame, uno::UNO_QUERY);
      75             : 
      76           0 :     uno::Reference<frame::XFrame> xBeamerFrame = xFrame->findFrame(
      77             :                                         OUString("_beamer"),
      78           0 :                                         frame::FrameSearchFlag::CHILDREN);
      79           0 :     if (xBeamerFrame.is())
      80             :     {
      81           0 :         uno::Reference<frame::XController> xController = xBeamerFrame->getController();
      82           0 :         uno::Reference<view::XSelectionSupplier> xControllerSelection(xController, uno::UNO_QUERY);
      83           0 :         if (xControllerSelection.is())
      84             :         {
      85             :             sal_Int32 nType = rParam.bSql ? sdb::CommandType::COMMAND :
      86           0 :                         ( (rParam.nType == ScDbQuery) ? sdb::CommandType::QUERY :
      87           0 :                                                         sdb::CommandType::TABLE );
      88             : 
      89           0 :             ::svx::ODataAccessDescriptor aSelection;
      90           0 :             aSelection.setDataSource(rParam.aDBName);
      91           0 :             aSelection[svx::daCommand]      <<= rParam.aStatement;
      92           0 :             aSelection[svx::daCommandType]  <<= nType;
      93             : 
      94           0 :             xControllerSelection->select(uno::makeAny(aSelection.createPropertyValueSequence()));
      95             :         }
      96             :         else
      97             :         {
      98             :             OSL_FAIL("no selection supplier in the beamer!");
      99           0 :         }
     100           0 :     }
     101             : }
     102             : 
     103           0 : bool ScDBDocFunc::DoImportUno( const ScAddress& rPos,
     104             :                                 const uno::Sequence<beans::PropertyValue>& aArgs )
     105             : {
     106           0 :     svx::ODataAccessDescriptor aDesc( aArgs );      // includes selection and result set
     107             : 
     108             :     //  create database range
     109           0 :     ScDBData* pDBData = rDocShell.GetDBData( ScRange(rPos), SC_DB_IMPORT, SC_DBSEL_KEEP );
     110             :     DBG_ASSERT(pDBData, "can't create DB data");
     111           0 :     OUString sTarget = pDBData->GetName();
     112             : 
     113           0 :     UpdateImport( sTarget, aDesc );
     114             : 
     115           0 :     return true;
     116             : }
     117             : 
     118           8 : bool ScDBDocFunc::DoImport( SCTAB nTab, const ScImportParam& rParam,
     119             :         const svx::ODataAccessDescriptor* pDescriptor, bool bRecord, bool bAddrInsert )
     120             : {
     121           8 :     ScDocument& rDoc = rDocShell.GetDocument();
     122           8 :     ScChangeTrack *pChangeTrack = NULL;
     123           8 :     ScRange aChangedRange;
     124             : 
     125           8 :     if (bRecord && !rDoc.IsUndoEnabled())
     126           0 :         bRecord = false;
     127             : 
     128           8 :     ScDBData* pDBData = NULL;
     129           8 :     if ( !bAddrInsert )
     130             :     {
     131             :         pDBData = rDoc.GetDBAtArea( nTab, rParam.nCol1, rParam.nRow1,
     132           8 :                                             rParam.nCol2, rParam.nRow2 );
     133           8 :         if (!pDBData)
     134             :         {
     135             :             OSL_FAIL( "DoImport: no DBData" );
     136           0 :             return false;
     137             :         }
     138             :     }
     139             : 
     140           8 :     vcl::Window* pWaitWin = rDocShell.GetActiveDialogParent();
     141           8 :     if (pWaitWin)
     142           8 :         pWaitWin->EnterWait();
     143           8 :     ScDocShellModificator aModificator( rDocShell );
     144             : 
     145           8 :     bool bSuccess = false;
     146           8 :     bool bApi = false;                      //! pass as argument
     147           8 :     bool bTruncated = false;                // for warning
     148           8 :     sal_uInt16 nErrStringId = 0;
     149          16 :     OUString aErrorMessage;
     150             : 
     151           8 :     SCCOL nCol = rParam.nCol1;
     152           8 :     SCROW nRow = rParam.nRow1;
     153           8 :     SCCOL nEndCol = nCol;                   // end of resulting database area
     154           8 :     SCROW nEndRow = nRow;
     155             :     long i;
     156             : 
     157           8 :     bool bDoSelection = false;
     158           8 :     bool bRealSelection = false;            // sal_True if not everything is selected
     159           8 :     bool bBookmarkSelection = false;
     160           8 :     sal_Int32 nListPos = 0;
     161           8 :     sal_Int32 nRowsRead = 0;
     162           8 :     sal_Int32 nListCount = 0;
     163             : 
     164          16 :     uno::Sequence<uno::Any> aSelection;
     165           8 :     if ( pDescriptor && pDescriptor->has(svx::daSelection) )
     166             :     {
     167           0 :         (*pDescriptor)[svx::daSelection] >>= aSelection;
     168           0 :         nListCount = aSelection.getLength();
     169           0 :         if ( nListCount > 0 )
     170             :         {
     171           0 :             bDoSelection = true;
     172           0 :             if ( pDescriptor->has(svx::daBookmarkSelection) )
     173           0 :                 bBookmarkSelection = ScUnoHelpFunctions::GetBoolFromAny( (*pDescriptor)[svx::daBookmarkSelection] );
     174           0 :             if ( bBookmarkSelection )
     175             :             {
     176             :                 // From bookmarks, there's no way to detect if all records are selected.
     177             :                 // Rely on base to pass no selection in that case.
     178           0 :                 bRealSelection = true;
     179             :             }
     180             :         }
     181             :     }
     182             : 
     183          16 :     uno::Reference<sdbc::XResultSet> xResultSet;
     184           8 :     if ( pDescriptor && pDescriptor->has(svx::daCursor) )
     185           0 :         xResultSet.set((*pDescriptor)[svx::daCursor], uno::UNO_QUERY);
     186             : 
     187             :     // ImportDoc - also used for Redo
     188           8 :     ScDocument* pImportDoc = new ScDocument( SCDOCMODE_UNDO );
     189           8 :     pImportDoc->InitUndo( &rDoc, nTab, nTab );
     190             : 
     191             :     //  get data from database into import document
     192             : 
     193             :     try
     194             :     {
     195             :         //  progress bar
     196             :         //  only text (title is still needed, for the cancel button)
     197           8 :         ScProgress aProgress( &rDocShell, ScGlobal::GetRscString(STR_UNDO_IMPORTDATA), 0 );
     198             : 
     199             :         uno::Reference<sdbc::XRowSet> xRowSet = uno::Reference<sdbc::XRowSet>(
     200          16 :                 xResultSet, uno::UNO_QUERY );
     201           8 :         bool bDispose = false;
     202           8 :         if ( !xRowSet.is() )
     203             :         {
     204           8 :             bDispose = true;
     205          16 :             xRowSet = uno::Reference<sdbc::XRowSet>(
     206          16 :                     comphelper::getProcessServiceFactory()->createInstance(
     207           8 :                         OUString( SC_SERVICE_ROWSET ) ),
     208           8 :                     uno::UNO_QUERY);
     209           8 :             uno::Reference<beans::XPropertySet> xRowProp( xRowSet, uno::UNO_QUERY );
     210             :             OSL_ENSURE( xRowProp.is(), "can't get RowSet" );
     211           8 :             if ( xRowProp.is() )
     212             :             {
     213             : 
     214             :                 //  set source parameters
     215             : 
     216             :                 sal_Int32 nType = rParam.bSql ? sdb::CommandType::COMMAND :
     217           2 :                             ( (rParam.nType == ScDbQuery) ? sdb::CommandType::QUERY :
     218          10 :                                                             sdb::CommandType::TABLE );
     219           8 :                 uno::Any aAny;
     220             : 
     221           8 :                 aAny <<= rParam.aDBName;
     222           8 :                 xRowProp->setPropertyValue(
     223           8 :                             OUString(SC_DBPROP_DATASOURCENAME), aAny );
     224             : 
     225           8 :                 aAny <<= rParam.aStatement;
     226           8 :                 xRowProp->setPropertyValue(
     227           8 :                             OUString(SC_DBPROP_COMMAND), aAny );
     228             : 
     229           8 :                 aAny <<= nType;
     230           8 :                 xRowProp->setPropertyValue(
     231           8 :                             OUString(SC_DBPROP_COMMANDTYPE), aAny );
     232             : 
     233          16 :                 uno::Reference<sdb::XCompletedExecution> xExecute( xRowSet, uno::UNO_QUERY );
     234           8 :                 if ( xExecute.is() )
     235             :                 {
     236             :                     uno::Reference<task::XInteractionHandler> xHandler(
     237             :                         task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
     238           8 :                         uno::UNO_QUERY_THROW);
     239           8 :                     xExecute->executeWithCompletion( xHandler );
     240             :                 }
     241             :                 else
     242           8 :                     xRowSet->execute();
     243           8 :             }
     244             :         }
     245           8 :         if ( xRowSet.is() )
     246             :         {
     247             : 
     248             :             //  get column descriptions
     249             : 
     250           8 :             long nColCount = 0;
     251           8 :             uno::Reference<sdbc::XResultSetMetaData> xMeta;
     252          16 :             uno::Reference<sdbc::XResultSetMetaDataSupplier> xMetaSupp( xRowSet, uno::UNO_QUERY );
     253           8 :             if ( xMetaSupp.is() )
     254           8 :                 xMeta = xMetaSupp->getMetaData();
     255           8 :             if ( xMeta.is() )
     256           8 :                 nColCount = xMeta->getColumnCount();    // this is the number of real columns
     257             : 
     258           8 :             if ( rParam.nCol1 + nColCount - 1 > MAXCOL )
     259             :             {
     260           0 :                 nColCount = 0;
     261             :                 //! error message
     262             :             }
     263             : 
     264          16 :             uno::Reference<sdbcx::XRowLocate> xLocate;
     265           8 :             if ( bBookmarkSelection )
     266             :             {
     267           0 :                 xLocate.set( xRowSet, uno::UNO_QUERY );
     268           0 :                 if ( !xLocate.is() )
     269             :                 {
     270             :                     SAL_WARN( "sc.ui","can't get XRowLocate");
     271           0 :                     bDoSelection = bRealSelection = bBookmarkSelection = false;
     272             :                 }
     273             :             }
     274             : 
     275          16 :             uno::Reference<sdbc::XRow> xRow( xRowSet, uno::UNO_QUERY );
     276           8 :             if ( nColCount > 0 && xRow.is() )
     277             :             {
     278           8 :                 nEndCol = (SCCOL)( rParam.nCol1 + nColCount - 1 );
     279             : 
     280           8 :                 uno::Sequence<sal_Int32> aColTypes( nColCount );    // column types
     281          16 :                 uno::Sequence<sal_Bool> aColCurr( nColCount );      // currency flag is not in types
     282           8 :                 sal_Int32* pTypeArr = aColTypes.getArray();
     283           8 :                 sal_Bool* pCurrArr = aColCurr.getArray();
     284          76 :                 for (i=0; i<nColCount; i++)
     285             :                 {
     286          68 :                     pTypeArr[i] = xMeta->getColumnType( i+1 );
     287          68 :                     pCurrArr[i] = xMeta->isCurrency( i+1 );
     288             :                 }
     289             : 
     290           8 :                 if ( !bAddrInsert )                 // read column names
     291             :                 {
     292           8 :                     nCol = rParam.nCol1;
     293          76 :                     for (i=0; i<nColCount; i++)
     294             :                     {
     295             :                         pImportDoc->SetString( nCol, nRow, nTab,
     296          68 :                                                 xMeta->getColumnLabel( i+1 ) );
     297          68 :                         ++nCol;
     298             :                     }
     299           8 :                     ++nRow;
     300             :                 }
     301             : 
     302           8 :                 bool bEnd = false;
     303           8 :                 if ( !bDoSelection )
     304           8 :                     xRowSet->beforeFirst();
     305           8 :                 sal_uInt16 nInserted = 0;
     306         184 :                 while ( !bEnd )
     307             :                 {
     308             :                     //  skip rows that are not selected
     309         168 :                     if ( !bDoSelection )
     310             :                     {
     311         168 :                         if ( !(bEnd = !xRowSet->next()) )
     312         160 :                             ++nRowsRead;
     313             :                     }
     314             :                     else
     315             :                     {
     316           0 :                         if (nListPos < nListCount)
     317             :                         {
     318           0 :                             if ( bBookmarkSelection )
     319             :                             {
     320           0 :                                 bEnd = !xLocate->moveToBookmark(aSelection[nListPos]);
     321             :                             }
     322             :                             else    // use record numbers
     323             :                             {
     324           0 :                                 sal_Int32 nNextRow = 0;
     325           0 :                                 aSelection[nListPos] >>= nNextRow;
     326           0 :                                 if ( nRowsRead+1 < nNextRow )
     327           0 :                                     bRealSelection = true;
     328           0 :                                 bEnd = !xRowSet->absolute(nRowsRead = nNextRow);
     329             :                             }
     330           0 :                             ++nListPos;
     331             :                         }
     332             :                         else
     333             :                         {
     334           0 :                             if ( !bBookmarkSelection && xRowSet->next() )
     335           0 :                                 bRealSelection = true;                      // more data available but not used
     336           0 :                             bEnd = true;
     337             :                         }
     338             :                     }
     339             : 
     340         168 :                     if ( !bEnd )
     341             :                     {
     342         160 :                         if ( ValidRow(nRow) )
     343             :                         {
     344         160 :                             nCol = rParam.nCol1;
     345        1520 :                             for (i=0; i<nColCount; i++)
     346             :                             {
     347             :                                 ScDatabaseDocUtil::PutData( pImportDoc, nCol, nRow, nTab,
     348        1360 :                                                 xRow, i+1, pTypeArr[i], pCurrArr[i] );
     349        1360 :                                 ++nCol;
     350             :                             }
     351         160 :                             nEndRow = nRow;
     352         160 :                             ++nRow;
     353             : 
     354             :                             //  progress bar
     355             : 
     356         160 :                             ++nInserted;
     357         160 :                             if (!(nInserted & 15))
     358             :                             {
     359           8 :                                 OUString aPict = ScGlobal::GetRscString( STR_PROGRESS_IMPORT );
     360          16 :                                 OUString aText = aPict.getToken(0,'#');
     361           8 :                                 aText += OUString::number( nInserted );
     362           8 :                                 aText += aPict.getToken(1,'#');
     363             : 
     364           8 :                                 if (!aProgress.SetStateText( 0, aText ))    // stopped by user?
     365             :                                 {
     366           0 :                                     bEnd = true;
     367           0 :                                     bSuccess = false;
     368           0 :                                     nErrStringId = STR_DATABASE_ABORTED;
     369           8 :                                 }
     370             :                             }
     371             :                         }
     372             :                         else        // past the end of the spreadsheet
     373             :                         {
     374           0 :                             bEnd = true;            // don't continue
     375           0 :                             bTruncated = true;      // warning flag
     376             :                         }
     377             :                     }
     378             :                 }
     379             : 
     380          16 :                 bSuccess = true;
     381             :             }
     382             : 
     383           8 :             if ( bDispose )
     384          16 :                 ::comphelper::disposeComponent( xRowSet );
     385           8 :         }
     386             :     }
     387           0 :     catch ( const sdbc::SQLException& rError )
     388             :     {
     389           0 :         aErrorMessage = rError.Message;
     390             :     }
     391           0 :     catch ( uno::Exception& )
     392             :     {
     393             :         OSL_FAIL("Unexpected exception in database");
     394             :     }
     395             : 
     396             :     //  test for cell protection
     397             : 
     398           8 :     bool bKeepFormat = !bAddrInsert && pDBData->IsKeepFmt();
     399           8 :     bool bMoveCells = !bAddrInsert && pDBData->IsDoSize();
     400           8 :     SCCOL nFormulaCols = 0; // columns to be filled with formulas
     401           8 :     if (bMoveCells && nEndCol == rParam.nCol2)
     402             :     {
     403             :         //  if column count changes, formulas would become invalid anyway
     404             :         //  -> only set nFormulaCols for unchanged column count
     405             : 
     406           0 :         SCCOL nTestCol = rParam.nCol2 + 1;      // right of the data
     407           0 :         SCROW nTestRow = rParam.nRow1 + 1;      // below the title row
     408           0 :         while ( nTestCol <= MAXCOL &&
     409           0 :                 rDoc.GetCellType(ScAddress( nTestCol, nTestRow, nTab )) == CELLTYPE_FORMULA )
     410           0 :             ++nTestCol, ++nFormulaCols;
     411             :     }
     412             : 
     413           8 :     if (bSuccess)
     414             :     {
     415             :         //  old and new range editable?
     416           8 :         ScEditableTester aTester;
     417           8 :         aTester.TestBlock( &rDoc, nTab, rParam.nCol1,rParam.nRow1,rParam.nCol2,rParam.nRow2 );
     418           8 :         aTester.TestBlock( &rDoc, nTab, rParam.nCol1,rParam.nRow1,nEndCol,nEndRow );
     419           8 :         if ( !aTester.IsEditable() )
     420             :         {
     421           0 :             nErrStringId = aTester.GetMessageId();
     422           0 :             bSuccess = false;
     423             :         }
     424           8 :         else if ( (pChangeTrack = rDoc.GetChangeTrack()) != NULL )
     425           0 :             aChangedRange = ScRange(rParam.nCol1, rParam.nRow1, nTab,
     426           0 :                         nEndCol+nFormulaCols, nEndRow, nTab );
     427             :     }
     428             : 
     429           8 :     if ( bSuccess && bMoveCells )
     430             :     {
     431             :         ScRange aOld( rParam.nCol1, rParam.nRow1, nTab,
     432           0 :                         rParam.nCol2+nFormulaCols, rParam.nRow2, nTab );
     433             :         ScRange aNew( rParam.nCol1, rParam.nRow1, nTab,
     434           0 :                         nEndCol+nFormulaCols, nEndRow, nTab );
     435           0 :         if (!rDoc.CanFitBlock( aOld, aNew ))
     436             :         {
     437           0 :             nErrStringId = STR_MSSG_DOSUBTOTALS_2;      // can't insert cells
     438           0 :             bSuccess = false;
     439             :         }
     440             :     }
     441             : 
     442             :     //  copy data from import doc into real document
     443             : 
     444           8 :     if ( bSuccess )
     445             :     {
     446           8 :         if (bKeepFormat)
     447             :         {
     448             :             //  keep formatting of title and first data row from the document
     449             :             //  CopyToDocument also copies styles, Apply... needs separate calls
     450             : 
     451           0 :             SCCOL nMinEndCol = std::min( rParam.nCol2, nEndCol );    // not too much
     452           0 :             nMinEndCol = sal::static_int_cast<SCCOL>( nMinEndCol + nFormulaCols );  // only if column count unchanged
     453           0 :             pImportDoc->DeleteAreaTab( 0,0, MAXCOL,MAXROW, nTab, IDF_ATTRIB );
     454             :             rDoc.CopyToDocument( rParam.nCol1, rParam.nRow1, nTab,
     455             :                                     nMinEndCol, rParam.nRow1, nTab,
     456           0 :                                     IDF_ATTRIB, false, pImportDoc );
     457             : 
     458           0 :             SCROW nDataStartRow = rParam.nRow1+1;
     459           0 :             for (SCCOL nCopyCol=rParam.nCol1; nCopyCol<=nMinEndCol; nCopyCol++)
     460             :             {
     461             :                 const ScPatternAttr* pSrcPattern = rDoc.GetPattern(
     462           0 :                                                     nCopyCol, nDataStartRow, nTab );
     463             :                 pImportDoc->ApplyPatternAreaTab( nCopyCol, nDataStartRow, nCopyCol, nEndRow,
     464           0 :                                                     nTab, *pSrcPattern );
     465           0 :                 const ScStyleSheet* pStyle = pSrcPattern->GetStyleSheet();
     466           0 :                 if (pStyle)
     467             :                     pImportDoc->ApplyStyleAreaTab( nCopyCol, nDataStartRow, nCopyCol, nEndRow,
     468           0 :                                                     nTab, *pStyle );
     469             :             }
     470             :         }
     471             : 
     472             :         //  don't set cell protection attribute if table is protected
     473           8 :         if (rDoc.IsTabProtected(nTab))
     474             :         {
     475           0 :             ScPatternAttr aPattern(pImportDoc->GetPool());
     476           0 :             aPattern.GetItemSet().Put( ScProtectionAttr( false,false,false,false ) );
     477           0 :             pImportDoc->ApplyPatternAreaTab( 0,0,MAXCOL,MAXROW, nTab, aPattern );
     478             :         }
     479             : 
     480             :         //  copy old data for undo
     481             : 
     482           8 :         SCCOL nUndoEndCol = std::max( nEndCol, rParam.nCol2 );       // rParam = old end
     483           8 :         SCROW nUndoEndRow = std::max( nEndRow, rParam.nRow2 );
     484             : 
     485           8 :         ScDocument* pUndoDoc = NULL;
     486           8 :         ScDBData* pUndoDBData = NULL;
     487           8 :         if ( bRecord )
     488             :         {
     489           8 :             pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
     490           8 :             pUndoDoc->InitUndo( &rDoc, nTab, nTab );
     491             : 
     492           8 :             if ( !bAddrInsert )
     493           8 :                 pUndoDBData = new ScDBData( *pDBData );
     494             :         }
     495             : 
     496           8 :         ScMarkData aNewMark;
     497           8 :         aNewMark.SelectOneTable( nTab );
     498             : 
     499           8 :         if (bRecord)
     500             :         {
     501             :             // do not touch notes (ScUndoImportData does not support drawing undo)
     502           8 :             InsertDeleteFlags nCopyFlags = IDF_ALL & ~IDF_NOTE;
     503             : 
     504             :             //  nFormulaCols is set only if column count is unchanged
     505             :             rDoc.CopyToDocument( rParam.nCol1, rParam.nRow1, nTab,
     506             :                                     nEndCol+nFormulaCols, nEndRow, nTab,
     507           8 :                                     nCopyFlags, false, pUndoDoc );
     508           8 :             if ( rParam.nCol2 > nEndCol )
     509             :                 rDoc.CopyToDocument( nEndCol+1, rParam.nRow1, nTab,
     510             :                                         nUndoEndCol, nUndoEndRow, nTab,
     511           6 :                                         nCopyFlags, false, pUndoDoc );
     512           8 :             if ( rParam.nRow2 > nEndRow )
     513             :                 rDoc.CopyToDocument( rParam.nCol1, nEndRow+1, nTab,
     514             :                                         nUndoEndCol+nFormulaCols, nUndoEndRow, nTab,
     515           4 :                                         nCopyFlags, false, pUndoDoc );
     516             :         }
     517             : 
     518             :         //  move new data
     519             : 
     520           8 :         if (bMoveCells)
     521             :         {
     522             :             //  clear only the range without the formulas,
     523             :             //  so the formula title and first row are preserved
     524             : 
     525             :             ScRange aDelRange( rParam.nCol1, rParam.nRow1, nTab,
     526           0 :                                 rParam.nCol2, rParam.nRow2, nTab );
     527           0 :             rDoc.DeleteAreaTab( aDelRange, IDF_ALL & ~IDF_NOTE );  // ohne die Formeln
     528             : 
     529             :             ScRange aOld( rParam.nCol1, rParam.nRow1, nTab,
     530           0 :                             rParam.nCol2+nFormulaCols, rParam.nRow2, nTab );
     531             :             ScRange aNew( rParam.nCol1, rParam.nRow1, nTab,
     532           0 :                             nEndCol+nFormulaCols, nEndRow, nTab );
     533           0 :             rDoc.FitBlock( aOld, aNew, false );        // Formeln nicht loeschen
     534             :         }
     535           8 :         else if ( nEndCol < rParam.nCol2 )      // DeleteArea calls PutInOrder
     536             :             rDoc.DeleteArea( nEndCol+1, rParam.nRow1, rParam.nCol2, rParam.nRow2,
     537           6 :                                 aNewMark, IDF_CONTENTS & ~IDF_NOTE );
     538             : 
     539             :         //  CopyToDocument doesn't remove contents
     540           8 :         rDoc.DeleteAreaTab( rParam.nCol1, rParam.nRow1, nEndCol, nEndRow, nTab, IDF_CONTENTS & ~IDF_NOTE );
     541             : 
     542             :         //  remove each column from ImportDoc after copying to reduce memory usage
     543           8 :         bool bOldAutoCalc = rDoc.GetAutoCalc();
     544           8 :         rDoc.SetAutoCalc( false );             // outside of the loop
     545          76 :         for (SCCOL nCopyCol = rParam.nCol1; nCopyCol <= nEndCol; nCopyCol++)
     546             :         {
     547             :             pImportDoc->CopyToDocument( nCopyCol, rParam.nRow1, nTab, nCopyCol, nEndRow, nTab,
     548          68 :                                         IDF_ALL, false, &rDoc );
     549          68 :             pImportDoc->DeleteAreaTab( nCopyCol, rParam.nRow1, nCopyCol, nEndRow, nTab, IDF_CONTENTS );
     550             :         }
     551           8 :         rDoc.SetAutoCalc( bOldAutoCalc );
     552             : 
     553           8 :         if (nFormulaCols > 0)               // copy formulas
     554             :         {
     555           0 :             if (bKeepFormat)            // formats for formulas
     556             :                 pImportDoc->CopyToDocument( nEndCol+1, rParam.nRow1, nTab,
     557             :                                             nEndCol+nFormulaCols, nEndRow, nTab,
     558           0 :                                             IDF_ATTRIB, false, &rDoc );
     559             :             // fill formulas
     560           0 :             ScMarkData aMark;
     561           0 :             aMark.SelectOneTable(nTab);
     562             : 
     563           0 :             sal_uLong nProgCount = nFormulaCols;
     564           0 :             nProgCount *= nEndRow-rParam.nRow1-1;
     565             :             ScProgress aProgress( rDoc.GetDocumentShell(),
     566           0 :                     ScGlobal::GetRscString(STR_FILL_SERIES_PROGRESS), nProgCount );
     567             : 
     568             :             rDoc.Fill( nEndCol+1, rParam.nRow1+1, nEndCol+nFormulaCols, rParam.nRow1+1,
     569           0 :                             &aProgress, aMark, nEndRow-rParam.nRow1-1, FILL_TO_BOTTOM, FILL_SIMPLE );
     570             :         }
     571             : 
     572             :         //  if new range is smaller, clear old contents
     573             : 
     574           8 :         if (!bMoveCells)        // move has happened above
     575             :         {
     576           8 :             if ( rParam.nCol2 > nEndCol )
     577             :                 rDoc.DeleteArea( nEndCol+1, rParam.nRow1, rParam.nCol2, rParam.nRow2,
     578           6 :                                     aNewMark, IDF_CONTENTS );
     579           8 :             if ( rParam.nRow2 > nEndRow )
     580             :                 rDoc.DeleteArea( rParam.nCol1, nEndRow+1, rParam.nCol2, rParam.nRow2,
     581           4 :                                     aNewMark, IDF_CONTENTS );
     582             :         }
     583             : 
     584           8 :         if( !bAddrInsert )      // update database range
     585             :         {
     586           8 :             pDBData->SetImportParam( rParam );
     587           8 :             pDBData->SetHeader( true );
     588           8 :             pDBData->SetByRow( true );
     589           8 :             pDBData->SetArea( nTab, rParam.nCol1,rParam.nRow1, nEndCol,nEndRow );
     590           8 :             pDBData->SetImportSelection( bRealSelection );
     591           8 :             rDoc.CompileDBFormula();
     592             :         }
     593             : 
     594           8 :         if (bRecord)
     595             :         {
     596           8 :             ScDocument* pRedoDoc = pImportDoc;
     597           8 :             pImportDoc = NULL;
     598             : 
     599           8 :             if (nFormulaCols > 0)                   // include filled formulas for redo
     600             :                 rDoc.CopyToDocument( rParam.nCol1, rParam.nRow1, nTab,
     601             :                                         nEndCol+nFormulaCols, nEndRow, nTab,
     602           0 :                                         IDF_ALL & ~IDF_NOTE, false, pRedoDoc );
     603             : 
     604           8 :             ScDBData* pRedoDBData = pDBData ? new ScDBData( *pDBData ) : NULL;
     605             : 
     606           8 :             rDocShell.GetUndoManager()->AddUndoAction(
     607             :                 new ScUndoImportData( &rDocShell, nTab,
     608             :                                         rParam, nUndoEndCol, nUndoEndRow,
     609             :                                         nFormulaCols,
     610           8 :                                         pUndoDoc, pRedoDoc, pUndoDBData, pRedoDBData ) );
     611             :         }
     612             : 
     613           8 :         sc::SetFormulaDirtyContext aCxt;
     614           8 :         rDoc.SetAllFormulasDirty(aCxt);
     615           8 :         rDocShell.PostPaint(ScRange(0, 0, nTab, MAXCOL, MAXROW, nTab), PAINT_GRID);
     616           8 :         aModificator.SetDocumentModified();
     617             : 
     618          16 :         ScDBRangeRefreshedHint aHint( rParam );
     619           8 :         rDoc.BroadcastUno( aHint );
     620             : 
     621           8 :         if (pWaitWin)
     622           8 :             pWaitWin->LeaveWait();
     623             : 
     624           8 :         if ( bTruncated && !bApi )          // show warning
     625           8 :             ErrorHandler::HandleError(SCWARN_IMPORT_RANGE_OVERFLOW);
     626             :     }
     627           0 :     else if ( !bApi )
     628             :     {
     629           0 :         if (pWaitWin)
     630           0 :             pWaitWin->LeaveWait();
     631             : 
     632           0 :         if (aErrorMessage.isEmpty())
     633             :         {
     634           0 :             if (!nErrStringId)
     635           0 :                 nErrStringId = STR_MSSG_IMPORTDATA_0;
     636           0 :             aErrorMessage = ScGlobal::GetRscString( nErrStringId );
     637             :         }
     638           0 :         InfoBox aInfoBox( rDocShell.GetActiveDialogParent(), aErrorMessage );
     639           0 :         aInfoBox.Execute();
     640             :     }
     641             : 
     642           8 :     delete pImportDoc;
     643             : 
     644           8 :     if (bSuccess && pChangeTrack)
     645           0 :         pChangeTrack->AppendInsert ( aChangedRange );
     646             : 
     647          16 :     return bSuccess;
     648         228 : }
     649             : 
     650             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10