LCOV - code coverage report
Current view: top level - sc/source/ui/docshell - dbdocimp.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 194 307 63.2 %
Date: 2015-06-13 12:38:46 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           4 : bool ScDBDocFunc::DoImport( SCTAB nTab, const ScImportParam& rParam,
     119             :         const svx::ODataAccessDescriptor* pDescriptor, bool bRecord, bool bAddrInsert )
     120             : {
     121           4 :     ScDocument& rDoc = rDocShell.GetDocument();
     122           4 :     ScChangeTrack *pChangeTrack = NULL;
     123           4 :     ScRange aChangedRange;
     124             : 
     125           4 :     if (bRecord && !rDoc.IsUndoEnabled())
     126           0 :         bRecord = false;
     127             : 
     128           4 :     ScDBData* pDBData = NULL;
     129           4 :     if ( !bAddrInsert )
     130             :     {
     131             :         pDBData = rDoc.GetDBAtArea( nTab, rParam.nCol1, rParam.nRow1,
     132           4 :                                             rParam.nCol2, rParam.nRow2 );
     133           4 :         if (!pDBData)
     134             :         {
     135             :             OSL_FAIL( "DoImport: no DBData" );
     136           0 :             return false;
     137             :         }
     138             :     }
     139             : 
     140           4 :     vcl::Window* pWaitWin = ScDocShell::GetActiveDialogParent();
     141           4 :     if (pWaitWin)
     142           4 :         pWaitWin->EnterWait();
     143           4 :     ScDocShellModificator aModificator( rDocShell );
     144             : 
     145           4 :     bool bSuccess = false;
     146           4 :     bool bApi = false;                      //! pass as argument
     147           4 :     bool bTruncated = false;                // for warning
     148           4 :     sal_uInt16 nErrStringId = 0;
     149           8 :     OUString aErrorMessage;
     150             : 
     151           4 :     SCCOL nCol = rParam.nCol1;
     152           4 :     SCROW nRow = rParam.nRow1;
     153           4 :     SCCOL nEndCol = nCol;                   // end of resulting database area
     154           4 :     SCROW nEndRow = nRow;
     155             :     long i;
     156             : 
     157           4 :     bool bDoSelection = false;
     158           4 :     bool bRealSelection = false;            // sal_True if not everything is selected
     159           4 :     bool bBookmarkSelection = false;
     160           4 :     sal_Int32 nListPos = 0;
     161           4 :     sal_Int32 nRowsRead = 0;
     162           4 :     sal_Int32 nListCount = 0;
     163             : 
     164           8 :     uno::Sequence<uno::Any> aSelection;
     165           4 :     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           8 :     uno::Reference<sdbc::XResultSet> xResultSet;
     184           4 :     if ( pDescriptor && pDescriptor->has(svx::daCursor) )
     185           0 :         xResultSet.set((*pDescriptor)[svx::daCursor], uno::UNO_QUERY);
     186             : 
     187             :     // ImportDoc - also used for Redo
     188           4 :     ScDocument* pImportDoc = new ScDocument( SCDOCMODE_UNDO );
     189           4 :     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           4 :         ScProgress aProgress( &rDocShell, ScGlobal::GetRscString(STR_UNDO_IMPORTDATA), 0 );
     198             : 
     199             :         uno::Reference<sdbc::XRowSet> xRowSet = uno::Reference<sdbc::XRowSet>(
     200           8 :                 xResultSet, uno::UNO_QUERY );
     201           4 :         bool bDispose = false;
     202           4 :         if ( !xRowSet.is() )
     203             :         {
     204           4 :             bDispose = true;
     205           8 :             xRowSet = uno::Reference<sdbc::XRowSet>(
     206           8 :                     comphelper::getProcessServiceFactory()->createInstance(
     207           4 :                         OUString( SC_SERVICE_ROWSET ) ),
     208           4 :                     uno::UNO_QUERY);
     209           4 :             uno::Reference<beans::XPropertySet> xRowProp( xRowSet, uno::UNO_QUERY );
     210             :             OSL_ENSURE( xRowProp.is(), "can't get RowSet" );
     211           4 :             if ( xRowProp.is() )
     212             :             {
     213             : 
     214             :                 //  set source parameters
     215             : 
     216             :                 sal_Int32 nType = rParam.bSql ? sdb::CommandType::COMMAND :
     217           1 :                             ( (rParam.nType == ScDbQuery) ? sdb::CommandType::QUERY :
     218           5 :                                                             sdb::CommandType::TABLE );
     219           4 :                 uno::Any aAny;
     220             : 
     221           4 :                 aAny <<= rParam.aDBName;
     222           4 :                 xRowProp->setPropertyValue(
     223           4 :                             OUString(SC_DBPROP_DATASOURCENAME), aAny );
     224             : 
     225           4 :                 aAny <<= rParam.aStatement;
     226           4 :                 xRowProp->setPropertyValue(
     227           4 :                             OUString(SC_DBPROP_COMMAND), aAny );
     228             : 
     229           4 :                 aAny <<= nType;
     230           4 :                 xRowProp->setPropertyValue(
     231           4 :                             OUString(SC_DBPROP_COMMANDTYPE), aAny );
     232             : 
     233           8 :                 uno::Reference<sdb::XCompletedExecution> xExecute( xRowSet, uno::UNO_QUERY );
     234           4 :                 if ( xExecute.is() )
     235             :                 {
     236             :                     uno::Reference<task::XInteractionHandler> xHandler(
     237             :                         task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), 0),
     238           4 :                         uno::UNO_QUERY_THROW);
     239           4 :                     xExecute->executeWithCompletion( xHandler );
     240             :                 }
     241             :                 else
     242           4 :                     xRowSet->execute();
     243           4 :             }
     244             :         }
     245           4 :         if ( xRowSet.is() )
     246             :         {
     247             : 
     248             :             //  get column descriptions
     249             : 
     250           4 :             long nColCount = 0;
     251           4 :             uno::Reference<sdbc::XResultSetMetaData> xMeta;
     252           8 :             uno::Reference<sdbc::XResultSetMetaDataSupplier> xMetaSupp( xRowSet, uno::UNO_QUERY );
     253           4 :             if ( xMetaSupp.is() )
     254           4 :                 xMeta = xMetaSupp->getMetaData();
     255           4 :             if ( xMeta.is() )
     256           4 :                 nColCount = xMeta->getColumnCount();    // this is the number of real columns
     257             : 
     258           4 :             if ( rParam.nCol1 + nColCount - 1 > MAXCOL )
     259             :             {
     260           0 :                 nColCount = 0;
     261             :                 //! error message
     262             :             }
     263             : 
     264           8 :             uno::Reference<sdbcx::XRowLocate> xLocate;
     265           4 :             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           8 :             uno::Reference<sdbc::XRow> xRow( xRowSet, uno::UNO_QUERY );
     276           4 :             if ( nColCount > 0 && xRow.is() )
     277             :             {
     278           4 :                 nEndCol = (SCCOL)( rParam.nCol1 + nColCount - 1 );
     279             : 
     280           4 :                 uno::Sequence<sal_Int32> aColTypes( nColCount );    // column types
     281           8 :                 uno::Sequence<sal_Bool> aColCurr( nColCount );      // currency flag is not in types
     282           4 :                 sal_Int32* pTypeArr = aColTypes.getArray();
     283           4 :                 sal_Bool* pCurrArr = aColCurr.getArray();
     284          38 :                 for (i=0; i<nColCount; i++)
     285             :                 {
     286          34 :                     pTypeArr[i] = xMeta->getColumnType( i+1 );
     287          34 :                     pCurrArr[i] = xMeta->isCurrency( i+1 );
     288             :                 }
     289             : 
     290           4 :                 if ( !bAddrInsert )                 // read column names
     291             :                 {
     292           4 :                     nCol = rParam.nCol1;
     293          38 :                     for (i=0; i<nColCount; i++)
     294             :                     {
     295             :                         pImportDoc->SetString( nCol, nRow, nTab,
     296          34 :                                                 xMeta->getColumnLabel( i+1 ) );
     297          34 :                         ++nCol;
     298             :                     }
     299           4 :                     ++nRow;
     300             :                 }
     301             : 
     302           4 :                 bool bEnd = false;
     303           4 :                 if ( !bDoSelection )
     304           4 :                     xRowSet->beforeFirst();
     305           4 :                 sal_uInt16 nInserted = 0;
     306          92 :                 while ( !bEnd )
     307             :                 {
     308             :                     //  skip rows that are not selected
     309          84 :                     if ( !bDoSelection )
     310             :                     {
     311          84 :                         if ( !(bEnd = !xRowSet->next()) )
     312          80 :                             ++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          84 :                     if ( !bEnd )
     341             :                     {
     342          80 :                         if ( ValidRow(nRow) )
     343             :                         {
     344          80 :                             nCol = rParam.nCol1;
     345         760 :                             for (i=0; i<nColCount; i++)
     346             :                             {
     347             :                                 ScDatabaseDocUtil::PutData( pImportDoc, nCol, nRow, nTab,
     348         680 :                                                 xRow, i+1, pTypeArr[i], pCurrArr[i] );
     349         680 :                                 ++nCol;
     350             :                             }
     351          80 :                             nEndRow = nRow;
     352          80 :                             ++nRow;
     353             : 
     354             :                             //  progress bar
     355             : 
     356          80 :                             ++nInserted;
     357          80 :                             if (!(nInserted & 15))
     358             :                             {
     359           4 :                                 OUString aPict = ScGlobal::GetRscString( STR_PROGRESS_IMPORT );
     360           8 :                                 OUString aText = aPict.getToken(0,'#');
     361           4 :                                 aText += OUString::number( nInserted );
     362           4 :                                 aText += aPict.getToken(1,'#');
     363             : 
     364           4 :                                 if (!aProgress.SetStateText( 0, aText ))    // stopped by user?
     365             :                                 {
     366           0 :                                     bEnd = true;
     367           0 :                                     bSuccess = false;
     368           0 :                                     nErrStringId = STR_DATABASE_ABORTED;
     369           4 :                                 }
     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           8 :                 bSuccess = true;
     381             :             }
     382             : 
     383           4 :             if ( bDispose )
     384           8 :                 ::comphelper::disposeComponent( xRowSet );
     385           4 :         }
     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           4 :     bool bKeepFormat = !bAddrInsert && pDBData->IsKeepFmt();
     399           4 :     bool bMoveCells = !bAddrInsert && pDBData->IsDoSize();
     400           4 :     SCCOL nFormulaCols = 0; // columns to be filled with formulas
     401           4 :     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           4 :     if (bSuccess)
     414             :     {
     415             :         //  old and new range editable?
     416           4 :         ScEditableTester aTester;
     417           4 :         aTester.TestBlock( &rDoc, nTab, rParam.nCol1,rParam.nRow1,rParam.nCol2,rParam.nRow2 );
     418           4 :         aTester.TestBlock( &rDoc, nTab, rParam.nCol1,rParam.nRow1,nEndCol,nEndRow );
     419           4 :         if ( !aTester.IsEditable() )
     420             :         {
     421           0 :             nErrStringId = aTester.GetMessageId();
     422           0 :             bSuccess = false;
     423             :         }
     424           4 :         else if ( (pChangeTrack = rDoc.GetChangeTrack()) != NULL )
     425           0 :             aChangedRange = ScRange(rParam.nCol1, rParam.nRow1, nTab,
     426           0 :                         nEndCol+nFormulaCols, nEndRow, nTab );
     427             :     }
     428             : 
     429           4 :     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           4 :     if ( bSuccess )
     445             :     {
     446           4 :         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           4 :         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           4 :         SCCOL nUndoEndCol = std::max( nEndCol, rParam.nCol2 );       // rParam = old end
     483           4 :         SCROW nUndoEndRow = std::max( nEndRow, rParam.nRow2 );
     484             : 
     485           4 :         ScDocument* pUndoDoc = NULL;
     486           4 :         ScDBData* pUndoDBData = NULL;
     487           4 :         if ( bRecord )
     488             :         {
     489           4 :             pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
     490           4 :             pUndoDoc->InitUndo( &rDoc, nTab, nTab );
     491             : 
     492           4 :             if ( !bAddrInsert )
     493           4 :                 pUndoDBData = new ScDBData( *pDBData );
     494             :         }
     495             : 
     496           4 :         ScMarkData aNewMark;
     497           4 :         aNewMark.SelectOneTable( nTab );
     498             : 
     499           4 :         if (bRecord)
     500             :         {
     501             :             // do not touch notes (ScUndoImportData does not support drawing undo)
     502           4 :             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           4 :                                     nCopyFlags, false, pUndoDoc );
     508           4 :             if ( rParam.nCol2 > nEndCol )
     509             :                 rDoc.CopyToDocument( nEndCol+1, rParam.nRow1, nTab,
     510             :                                         nUndoEndCol, nUndoEndRow, nTab,
     511           3 :                                         nCopyFlags, false, pUndoDoc );
     512           4 :             if ( rParam.nRow2 > nEndRow )
     513             :                 rDoc.CopyToDocument( rParam.nCol1, nEndRow+1, nTab,
     514             :                                         nUndoEndCol+nFormulaCols, nUndoEndRow, nTab,
     515           2 :                                         nCopyFlags, false, pUndoDoc );
     516             :         }
     517             : 
     518             :         //  move new data
     519             : 
     520           4 :         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           4 :         else if ( nEndCol < rParam.nCol2 )      // DeleteArea calls PutInOrder
     536             :             rDoc.DeleteArea( nEndCol+1, rParam.nRow1, rParam.nCol2, rParam.nRow2,
     537           3 :                                 aNewMark, IDF_CONTENTS & ~IDF_NOTE );
     538             : 
     539             :         //  CopyToDocument doesn't remove contents
     540           4 :         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           4 :         bool bOldAutoCalc = rDoc.GetAutoCalc();
     544           4 :         rDoc.SetAutoCalc( false );             // outside of the loop
     545          38 :         for (SCCOL nCopyCol = rParam.nCol1; nCopyCol <= nEndCol; nCopyCol++)
     546             :         {
     547             :             pImportDoc->CopyToDocument( nCopyCol, rParam.nRow1, nTab, nCopyCol, nEndRow, nTab,
     548          34 :                                         IDF_ALL, false, &rDoc );
     549          34 :             pImportDoc->DeleteAreaTab( nCopyCol, rParam.nRow1, nCopyCol, nEndRow, nTab, IDF_CONTENTS );
     550             :         }
     551           4 :         rDoc.SetAutoCalc( bOldAutoCalc );
     552             : 
     553           4 :         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           4 :         if (!bMoveCells)        // move has happened above
     575             :         {
     576           4 :             if ( rParam.nCol2 > nEndCol )
     577             :                 rDoc.DeleteArea( nEndCol+1, rParam.nRow1, rParam.nCol2, rParam.nRow2,
     578           3 :                                     aNewMark, IDF_CONTENTS );
     579           4 :             if ( rParam.nRow2 > nEndRow )
     580             :                 rDoc.DeleteArea( rParam.nCol1, nEndRow+1, rParam.nCol2, rParam.nRow2,
     581           2 :                                     aNewMark, IDF_CONTENTS );
     582             :         }
     583             : 
     584           4 :         if( !bAddrInsert )      // update database range
     585             :         {
     586           4 :             pDBData->SetImportParam( rParam );
     587           4 :             pDBData->SetHeader( true );
     588           4 :             pDBData->SetByRow( true );
     589           4 :             pDBData->SetArea( nTab, rParam.nCol1,rParam.nRow1, nEndCol,nEndRow );
     590           4 :             pDBData->SetImportSelection( bRealSelection );
     591           4 :             rDoc.CompileDBFormula();
     592             :         }
     593             : 
     594           4 :         if (bRecord)
     595             :         {
     596           4 :             ScDocument* pRedoDoc = pImportDoc;
     597           4 :             pImportDoc = NULL;
     598             : 
     599           4 :             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           4 :             ScDBData* pRedoDBData = pDBData ? new ScDBData( *pDBData ) : NULL;
     605             : 
     606           4 :             rDocShell.GetUndoManager()->AddUndoAction(
     607             :                 new ScUndoImportData( &rDocShell, nTab,
     608             :                                         rParam, nUndoEndCol, nUndoEndRow,
     609             :                                         nFormulaCols,
     610           4 :                                         pUndoDoc, pRedoDoc, pUndoDBData, pRedoDBData ) );
     611             :         }
     612             : 
     613           4 :         sc::SetFormulaDirtyContext aCxt;
     614           4 :         rDoc.SetAllFormulasDirty(aCxt);
     615           4 :         rDocShell.PostPaint(ScRange(0, 0, nTab, MAXCOL, MAXROW, nTab), PAINT_GRID);
     616           4 :         aModificator.SetDocumentModified();
     617             : 
     618           8 :         ScDBRangeRefreshedHint aHint( rParam );
     619           4 :         rDoc.BroadcastUno( aHint );
     620             : 
     621           4 :         if (pWaitWin)
     622           4 :             pWaitWin->LeaveWait();
     623             : 
     624           4 :         if ( bTruncated && !bApi )          // show warning
     625           4 :             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 :         ScopedVclPtrInstance< InfoBox > aInfoBox( ScDocShell::GetActiveDialogParent(), aErrorMessage );
     639           0 :         aInfoBox->Execute();
     640             :     }
     641             : 
     642           4 :     delete pImportDoc;
     643             : 
     644           4 :     if (bSuccess && pChangeTrack)
     645           0 :         pChangeTrack->AppendInsert ( aChangedRange );
     646             : 
     647           8 :     return bSuccess;
     648         156 : }
     649             : 
     650             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11