diff options
author | Jan-Marek Glogowski <glogow@fbihome.de> | 2016-01-08 09:41:55 +0100 |
---|---|---|
committer | Jan-Marek Glogowski <glogow@fbihome.de> | 2016-03-24 22:40:38 +0100 |
commit | c103bdedfe5c6042414209c536d731bbe6138ff5 (patch) | |
tree | 2e029f10e19b7b24da32abe519ffa9f567dc4a75 | |
parent | dea0abc58c558d3c62e806d06035b1a30420264e (diff) |
MM: refactor DBManager related duplicated code
Moves the duplicated code for saving documents, configuring the
printer and DB cursor forwarding (next record) into static local
functions.
Change-Id: I75f183a2a515ecbae276461613a05ab50c56e600
-rw-r--r-- | sw/inc/dbmgr.hxx | 4 | ||||
-rw-r--r-- | sw/source/uibase/dbui/dbmgr.cxx | 349 |
2 files changed, 171 insertions, 182 deletions
diff --git a/sw/inc/dbmgr.hxx b/sw/inc/dbmgr.hxx index 7802771bc9c5..ac3deb1d9a9b 100644 --- a/sw/inc/dbmgr.hxx +++ b/sw/inc/dbmgr.hxx @@ -223,7 +223,6 @@ friend class SwConnectionDisposedListener_Impl; /// merge to file _and_ merge to e-Mail SAL_DLLPRIVATE bool MergeMailFiles(SwWrtShell* pSh, const SwMergeDescriptor& rMergeDescriptor, vcl::Window* pParent ); - SAL_DLLPRIVATE bool ToNextRecord(SwDSParam* pParam); SwDBManager(SwDBManager const&) = delete; SwDBManager& operator=(SwDBManager const&) = delete; @@ -251,7 +250,6 @@ public: /// Merging of data records into fields. bool MergeNew( const SwMergeDescriptor& rMergeDesc, vcl::Window* pParent = nullptr ); - static bool Merge(SwWrtShell* pSh); void MergeCancel(); /// Initialize data fields that lack name of database. @@ -270,7 +268,7 @@ public: const OUString& rDBName, const OUString& rTableName); static void GetColumnNames(ListBox* pListBox, css::uno::Reference< css::sdbc::XConnection> xConnection, - const OUString& rTableName, bool bAppend = false); + const OUString& rTableName); static sal_uLong GetColumnFormat( css::uno::Reference< css::sdbc::XDataSource> xSource, css::uno::Reference< css::sdbc::XConnection> xConnection, diff --git a/sw/source/uibase/dbui/dbmgr.cxx b/sw/source/uibase/dbui/dbmgr.cxx index 2eb6b4336893..367287d29ff9 100644 --- a/sw/source/uibase/dbui/dbmgr.cxx +++ b/sw/source/uibase/dbui/dbmgr.cxx @@ -154,10 +154,20 @@ const sal_Char cDataSourceName[] = "DataSourceName"; const sal_Char cSelection[] = "Selection"; const sal_Char cActiveConnection[] = "ActiveConnection"; -namespace -{ +#define EMIT_SW_EVENT(type, shell) \ + SfxGetpApp()->NotifyEvent(SfxEventHint(SW_EVENT_##type, \ + SwDocShell::GetEventName(STR_SW_EVENT_##type), shell)) + +#define RESCHEDULE_GUI \ + do { \ + for( sal_uInt16 i = 0; i < 25; i++) \ + Application::Reschedule(); \ + } while( 0 ) -bool lcl_getCountFromResultSet( sal_Int32& rCount, const uno::Reference<sdbc::XResultSet>& xResultSet ) +enum class SwDBNextRecord { NEXT, FIRST }; +static bool lcl_ToNextRecord( SwDSParam* pParam, const SwDBNextRecord action = SwDBNextRecord::NEXT ); + +static bool lcl_getCountFromResultSet( sal_Int32& rCount, const uno::Reference<sdbc::XResultSet>& xResultSet ) { uno::Reference<beans::XPropertySet> xPrSet(xResultSet, uno::UNO_QUERY); if(xPrSet.is()) @@ -182,7 +192,6 @@ bool lcl_getCountFromResultSet( sal_Int32& rCount, const uno::Reference<sdbc::XR } return false; } -} class SwConnectionDisposedListener_Impl : public cppu::WeakImplHelper < lang::XEventListener > @@ -463,29 +472,7 @@ bool SwDBManager::MergeNew( const SwMergeDescriptor& rMergeDesc, vcl::Window* pP pImpl->pMergeData->xConnection = xConnection; // add an XEventListener - try{ - //set to start position - if(pImpl->pMergeData->aSelection.getLength()) - { - sal_Int32 nPos = 0; - pImpl->pMergeData->aSelection.getConstArray()[ pImpl->pMergeData->nSelectionIndex++ ] >>= nPos; - pImpl->pMergeData->bEndOfDB = !pImpl->pMergeData->xResultSet->absolute( nPos ); - pImpl->pMergeData->CheckEndOfDB(); - if(pImpl->pMergeData->nSelectionIndex >= pImpl->pMergeData->aSelection.getLength()) - pImpl->pMergeData->bEndOfDB = true; - } - else - { - pImpl->pMergeData->bEndOfDB = !pImpl->pMergeData->xResultSet->first(); - pImpl->pMergeData->CheckEndOfDB(); - } - } - catch (const uno::Exception& e) - { - pImpl->pMergeData->bEndOfDB = true; - pImpl->pMergeData->CheckEndOfDB(); - SAL_WARN("sw.mailmerge", "exception in MergeNew(): " << e.Message); - } + lcl_ToNextRecord(pImpl->pMergeData, SwDBNextRecord::FIRST); uno::Reference<sdbc::XDataSource> xSource = SwDBManager::getDataSourceAsParent(xConnection,aData.sDataSource); @@ -513,7 +500,10 @@ bool SwDBManager::MergeNew( const SwMergeDescriptor& rMergeDesc, vcl::Window* pP switch(rMergeDesc.nMergeType) { case DBMGR_MERGE: - bRet = Merge(&rMergeDesc.rSh); + rMergeDesc.rSh.StartAllAction(); + rMergeDesc.rSh.SwViewShell::UpdateFields( true ); + rMergeDesc.rSh.SetModified(); + rMergeDesc.rSh.EndAllAction(); break; case DBMGR_MERGE_PRINTER: @@ -521,8 +511,7 @@ bool SwDBManager::MergeNew( const SwMergeDescriptor& rMergeDesc, vcl::Window* pP case DBMGR_MERGE_FILE: case DBMGR_MERGE_SHELL: // save files and send them as e-Mail if required - bRet = MergeMailFiles(&rMergeDesc.rSh, - rMergeDesc, pParent); + bRet = MergeMailFiles(&rMergeDesc.rSh, rMergeDesc, pParent); break; default: @@ -539,19 +528,6 @@ bool SwDBManager::MergeNew( const SwMergeDescriptor& rMergeDesc, vcl::Window* pP return bRet; } -// import data -bool SwDBManager::Merge(SwWrtShell* pSh) -{ - pSh->StartAllAction(); - - pSh->SwViewShell::UpdateFields(true); - pSh->SetModified(); - - pSh->EndAllAction(); - - return true; -} - void SwDBManager::ImportFromConnection( SwWrtShell* pSh ) { if(pImpl->pMergeData && !pImpl->pMergeData->bEndOfDB) @@ -738,7 +714,6 @@ bool SwDBManager::GetTableNames(ListBox* pListBox, const OUString& rDBName) void SwDBManager::GetColumnNames(ListBox* pListBox, const OUString& rDBName, const OUString& rTableName) { - pListBox->Clear(); SwDBData aData; aData.sDataSource = rDBName; aData.sCommand = rTableName; @@ -752,27 +727,14 @@ void SwDBManager::GetColumnNames(ListBox* pListBox, OUString sDBName(rDBName); xConnection = RegisterConnection( sDBName ); } - - uno::Reference< sdbcx::XColumnsSupplier> xColsSupp = SwDBManager::GetColumnSupplier(xConnection, rTableName); - if(xColsSupp.is()) - { - uno::Reference<container::XNameAccess> xCols = xColsSupp->getColumns(); - const uno::Sequence<OUString> aColNames = xCols->getElementNames(); - const OUString* pColNames = aColNames.getConstArray(); - for(int nCol = 0; nCol < aColNames.getLength(); nCol++) - { - pListBox->InsertEntry(pColNames[nCol]); - } - ::comphelper::disposeComponent( xColsSupp ); - } + GetColumnNames(pListBox, xConnection, rTableName); } void SwDBManager::GetColumnNames(ListBox* pListBox, uno::Reference< sdbc::XConnection> xConnection, - const OUString& rTableName, bool bAppend) + const OUString& rTableName) { - if (!bAppend) - pListBox->Clear(); + pListBox->Clear(); uno::Reference< sdbcx::XColumnsSupplier> xColsSupp = SwDBManager::GetColumnSupplier(xConnection, rTableName); if(xColsSupp.is()) { @@ -877,8 +839,8 @@ static void lcl_RemoveSectionLinks( SwWrtShell& rWorkShell ) rWorkShell.SetLabelDoc( false ); } -static void lcl_SaveDoc( SfxObjectShell *xTargetDocShell, - const char *name, int no = 0 ) +static void lcl_SaveDebugDoc( SfxObjectShell *xTargetDocShell, + const char *name, int no = 0 ) { const OUString sExt( ".odt" ); OUString basename = OUString::createFromAscii( name ); @@ -897,6 +859,83 @@ static void lcl_SaveDoc( SfxObjectShell *xTargetDocShell, delete pDstMed; } +static bool lcl_SaveDoc( + const INetURLObject* pFileURL, + const std::shared_ptr<const SfxFilter> pStoreToFilter, + const OUString* pStoreToFilterOptions, + const uno::Sequence< beans::PropertyValue >* pSaveToFilterData, + const bool bIsPDFexport, + SfxObjectShell* xObjectShell, + SwWrtShell& rWorkShell, + OUString * const decodedURL = nullptr ) +{ + OUString url = pFileURL->GetMainURL( INetURLObject::NO_DECODE ); + if( decodedURL ) + (*decodedURL) = url; + + SfxMedium* pDstMed = new SfxMedium( url, STREAM_STD_READWRITE ); + pDstMed->SetFilter( pStoreToFilter ); + if( pDstMed->GetItemSet() ) + { + if( pStoreToFilterOptions ) + pDstMed->GetItemSet()->Put( SfxStringItem(SID_FILE_FILTEROPTIONS, + *pStoreToFilterOptions)); + if( pSaveToFilterData->getLength() ) + pDstMed->GetItemSet()->Put( SfxUsrAnyItem(SID_FILTER_DATA, + uno::makeAny(*pSaveToFilterData))); + } + + // convert fields to text if we are exporting to PDF. + // this prevents a second merge while updating the fields + // in SwXTextDocument::getRendererCount() + if( bIsPDFexport ) + rWorkShell.ConvertFieldsToText(); + + bool bAnyError = !xObjectShell->DoSaveAs(*pDstMed); + xObjectShell->DoSaveCompleted(pDstMed); + bAnyError = bAnyError || xObjectShell->GetError(); + if( bAnyError ) + { + // error message ?? + ErrorHandler::HandleError( xObjectShell->GetError() ); + } + return !bAnyError; +} + +static void lcl_PreparePrinterOptions( + const uno::Sequence< beans::PropertyValue >& rInPrintOptions, + const bool bVisibleMonitor, + uno::Sequence< beans::PropertyValue >& rOutPrintOptions) +{ + // printing should be done synchronously otherwise the document + // might already become invalid during the process + + const sal_Int32 nOffset = !bVisibleMonitor ? 2 : 1; + rOutPrintOptions.realloc( nOffset ); + rOutPrintOptions[ 0 ].Name = "Wait"; + rOutPrintOptions[ 0 ].Value <<= sal_True; + if( !bVisibleMonitor ) + { + rOutPrintOptions[ 1 ].Name = "MonitorVisible"; + rOutPrintOptions[ 1 ].Value <<= sal_False; + } + + // copy print options + const beans::PropertyValue* pOptions = rInPrintOptions.getConstArray(); + for( sal_Int32 n = 0, nIndex = nOffset ; n < rInPrintOptions.getLength(); ++n) + { + if( pOptions[n].Name == "CopyCount" || pOptions[n].Name == "FileName" + || pOptions[n].Name == "Collate" || pOptions[n].Name == "Pages" + || pOptions[n].Name == "Wait" || pOptions[n].Name == "PrinterName" ) + { + // add an option + rOutPrintOptions.realloc( nIndex + 1 ); + rOutPrintOptions[ nIndex ].Name = pOptions[n].Name; + rOutPrintOptions[ nIndex++ ].Value = pOptions[n].Value ; + } + } +} + bool SwDBManager::MergeMailFiles(SwWrtShell* pSourceShell, const SwMergeDescriptor& rMergeDescriptor, vcl::Window* pParent) @@ -1023,8 +1062,7 @@ bool SwDBManager::MergeMailFiles(SwWrtShell* pSourceShell, pProgressDlg->SetCancelHdl( LINK(this, SwDBManager, PrtCancelHdl) ); pProgressDlg->Show(); - for( sal_uInt16 i = 0; i < 25; i++) - Application::Reschedule(); + RESCHEDULE_GUI; } if( bCreateSingleFile && !pTargetView ) @@ -1033,7 +1071,7 @@ bool SwDBManager::MergeMailFiles(SwWrtShell* pSourceShell, xTargetDocShell = new SwDocShell( SfxObjectCreateMode::STANDARD ); xTargetDocShell->DoInitNew( ); if (nMaxDumpDocs) - lcl_SaveDoc( xTargetDocShell, "MergeDoc" ); + lcl_SaveDebugDoc( xTargetDocShell, "MergeDoc" ); SfxViewFrame* pTargetFrame = SfxViewFrame::LoadHiddenDocument( *xTargetDocShell, 0 ); if (bMergeShell && pSourceWindow) { @@ -1181,9 +1219,7 @@ bool SwDBManager::MergeMailFiles(SwWrtShell* pSourceShell, pProgressDlg->Update(); } - // Computation time for the GUI - for( sal_uInt16 i = 0; i < 25; i++ ) - Application::Reschedule(); + RESCHEDULE_GUI; // Create a copy of the source document and work with that one instead of the source. // If we're not in the single file mode (which requires modifying the document for the merging), @@ -1203,7 +1239,7 @@ bool SwDBManager::MergeMailFiles(SwWrtShell* pSourceShell, pWorkDoc = rWorkShell.GetDoc(); pWorkDoc->ReplaceDocumentProperties( *pSourceDocSh->GetDoc()); if ( (nMaxDumpDocs < 0) || (nDocNo <= nMaxDumpDocs) ) - lcl_SaveDoc( xWorkDocSh, "WorkDoc", nDocNo ); + lcl_SaveDebugDoc( xWorkDocSh, "WorkDoc", nDocNo ); pOldDBManager = pWorkDoc->GetDBManager(); pWorkDoc->SetDBManager( this ); pWorkDoc->getIDocumentLinksAdministration().EmbedAllLinks(); @@ -1216,7 +1252,9 @@ bool SwDBManager::MergeMailFiles(SwWrtShell* pSourceShell, } SwWrtShell& rWorkShell = pWorkView->GetWrtShell(); - SfxGetpApp()->NotifyEvent(SfxEventHint(SW_EVENT_FIELD_MERGE, SwDocShell::GetEventName(STR_SW_EVENT_FIELD_MERGE), xWorkDocSh)); + + EMIT_SW_EVENT(FIELD_MERGE, xWorkDocSh); + // tdf#92324: Allow ExpFields update only by explicit instruction to avoid // database cursor movement on any other fields update, for example during // print preview and other operations @@ -1224,13 +1262,15 @@ bool SwDBManager::MergeMailFiles(SwWrtShell* pSourceShell, rWorkShell.UnlockExpFields(); rWorkShell.SwViewShell::UpdateFields(); rWorkShell.LockExpFields(); - SfxGetpApp()->NotifyEvent(SfxEventHint(SW_EVENT_FIELD_MERGE_FINISHED, SwDocShell::GetEventName(STR_SW_EVENT_FIELD_MERGE_FINISHED), xWorkDocSh)); - // launch MailMergeEvent if required + EMIT_SW_EVENT(FIELD_MERGE_FINISHED, xWorkDocSh); + + // also emit MailMergeEvent on XInterface if possible const SwXMailMerge *pEvtSrc = GetMailMergeEvtSrc(); if(pEvtSrc) { - uno::Reference< uno::XInterface > xRef( const_cast<text::XMailMergeBroadcaster *>(static_cast<text::XMailMergeBroadcaster const *>(pEvtSrc)) ); + uno::Reference< uno::XInterface > xRef( + static_cast<text::XMailMergeBroadcaster*>(const_cast<SwXMailMerge*>(pEvtSrc)) ); text::MailMergeEvent aEvt( xRef, xWorkDocSh->GetModel() ); pEvtSrc->LaunchMailMergeEvent( aEvt ); } @@ -1271,15 +1311,16 @@ bool SwDBManager::MergeMailFiles(SwWrtShell* pSourceShell, else pTargetPageDesc = pTargetShell->FindPageDescByName( sModifiedStartingPageDesc ); + // insert the document into the target document if ( (nMaxDumpDocs < 0) || (nDocNo <= nMaxDumpDocs) ) - lcl_SaveDoc( xWorkDocSh, "WorkDoc", nDocNo ); + lcl_SaveDebugDoc( xWorkDocSh, "WorkDoc", nDocNo ); if( targetDocPageCount % 2 == 1 ) ++targetDocPageCount; // Docs always start on odd pages (so offset must be even). SwNodeIndex appendedDocStart = pTargetDoc->AppendDoc(*rWorkShell.GetDoc(), nStartingPageNo, pTargetPageDesc, nDocNo == 1, targetDocPageCount); targetDocPageCount += rWorkShell.GetPageCnt(); if ( (nMaxDumpDocs < 0) || (nDocNo <= nMaxDumpDocs) ) - lcl_SaveDoc( xTargetDocShell, "MergeDoc" ); + lcl_SaveDebugDoc( xTargetDocShell, "MergeDoc" ); if (bMergeShell) { SwDocMergeInfo aMergeInfo; @@ -1294,29 +1335,9 @@ bool SwDBManager::MergeMailFiles(SwWrtShell* pSourceShell, { if( 1 == nDocNo ) // set up printing only once at the beginning { - // printing should be done synchronously otherwise the document - // might already become invalid during the process uno::Sequence< beans::PropertyValue > aOptions( rMergeDescriptor.aPrintOptions ); + lcl_PreparePrinterOptions( rMergeDescriptor.aPrintOptions, false, aOptions ); - aOptions.realloc( 2 ); - aOptions[ 0 ].Name = "Wait"; - aOptions[ 0 ].Value <<= sal_True; - aOptions[ 1 ].Name = "MonitorVisible"; - aOptions[ 1 ].Value <<= sal_False; - // move print options - const beans::PropertyValue* pPrintOptions = rMergeDescriptor.aPrintOptions.getConstArray(); - for( sal_Int32 nOption = 0, nIndex = 1 ; nOption < rMergeDescriptor.aPrintOptions.getLength(); ++nOption) - { - if( pPrintOptions[nOption].Name == "CopyCount" || pPrintOptions[nOption].Name == "FileName" - || pPrintOptions[nOption].Name == "Collate" || pPrintOptions[nOption].Name == "Pages" - || pPrintOptions[nOption].Name == "Wait" || pPrintOptions[nOption].Name == "PrinterName" ) - { - // add an option - aOptions.realloc( nIndex + 1 ); - aOptions[ nIndex ].Name = pPrintOptions[nOption].Name; - aOptions[ nIndex++ ].Value = pPrintOptions[nOption].Value ; - } - } pWorkView->StartPrint( aOptions, IsMergeSilent(), rMergeDescriptor.bPrintAsync ); // some GetPrinter functions have a true default, so keep the false SfxPrinter* pDocPrt = pWorkView->GetPrinter( false ); @@ -1329,29 +1350,12 @@ bool SwDBManager::MergeMailFiles(SwWrtShell* pSourceShell, else { assert( createTempFile ); - OUString sFileURL = aTempFileURL->GetMainURL( INetURLObject::NO_DECODE ); - SfxMedium* pDstMed = new SfxMedium( - sFileURL, - STREAM_STD_READWRITE ); - pDstMed->SetFilter( pStoreToFilter ); - if(pDstMed->GetItemSet()) - { - if(pStoreToFilterOptions ) - pDstMed->GetItemSet()->Put(SfxStringItem(SID_FILE_FILTEROPTIONS, *pStoreToFilterOptions)); - if(rMergeDescriptor.aSaveToFilterData.getLength()) - pDstMed->GetItemSet()->Put(SfxUsrAnyItem(SID_FILTER_DATA, uno::makeAny(rMergeDescriptor.aSaveToFilterData))); - } - - //convert fields to text if we are exporting to PDF - //this prevents a second merge while updating the fields in SwXTextDocument::getRendererCount() - if( bIsPDFexport ) - rWorkShell.ConvertFieldsToText(); - xWorkDocSh->DoSaveAs(*pDstMed); - xWorkDocSh->DoSaveCompleted(pDstMed); - if( xWorkDocSh->GetError() ) + // save merged document + OUString sFileURL; + if( !lcl_SaveDoc( aTempFileURL.get(), pStoreToFilter, pStoreToFilterOptions, + &rMergeDescriptor.aSaveToFilterData, bIsPDFexport, + xWorkDocSh, rWorkShell, &sFileURL ) ) { - // error message ?? - ErrorHandler::HandleError( xWorkDocSh->GetError() ); bCancel = true; bNoError = false; } @@ -1480,8 +1484,7 @@ bool SwDBManager::MergeMailFiles(SwWrtShell* pSourceShell, pTargetDoc->SetAllUniqueFlyNames(); } - for( sal_uInt16 i = 0; i < 25; i++) - Application::Reschedule(); + RESCHEDULE_GUI; // Unfreeze target document layouts and correct all PageDescs. if(bCreateSingleFile) @@ -1507,56 +1510,20 @@ bool SwDBManager::MergeMailFiles(SwWrtShell* pSourceShell, { if( !bCancel ) { - assert( aTempFile.get()); + // save merged document + assert( aTempFile.get() ); INetURLObject aTempFileURL( rMergeDescriptor.bSubjectIsFilename ? sSubject : aTempFile->GetURL()); - SfxMedium* pDstMed = new SfxMedium( - aTempFileURL.GetMainURL( INetURLObject::NO_DECODE ), - STREAM_STD_READWRITE ); - pDstMed->SetFilter( pStoreToFilter ); - if(pDstMed->GetItemSet()) - { - if(pStoreToFilterOptions ) - pDstMed->GetItemSet()->Put(SfxStringItem(SID_FILE_FILTEROPTIONS, *pStoreToFilterOptions)); - if(rMergeDescriptor.aSaveToFilterData.getLength()) - pDstMed->GetItemSet()->Put(SfxUsrAnyItem(SID_FILTER_DATA, uno::makeAny(rMergeDescriptor.aSaveToFilterData))); - } - - xTargetDocShell->DoSaveAs(*pDstMed); - xTargetDocShell->DoSaveCompleted(pDstMed); - if( xTargetDocShell->GetError() ) - { - // error message ?? - ErrorHandler::HandleError( xTargetDocShell->GetError() ); - bNoError = false; - } + bNoError = lcl_SaveDoc( &aTempFileURL, pStoreToFilter, + pStoreToFilterOptions, &rMergeDescriptor.aSaveToFilterData, + bIsPDFexport, xTargetDocShell, *pTargetShell ); } } else if( pTargetView ) // must be available! { - //print the target document - // printing should be done synchronously otherwise the document - // might already become invalid during the process + // print the target document uno::Sequence< beans::PropertyValue > aOptions( rMergeDescriptor.aPrintOptions ); - - aOptions.realloc( 1 ); - aOptions[ 0 ].Name = "Wait"; - aOptions[ 0 ].Value <<= sal_True ; - // move print options - const beans::PropertyValue* pPrintOptions = rMergeDescriptor.aPrintOptions.getConstArray(); - for( sal_Int32 nOption = 0, nIndex = 1 ; nOption < rMergeDescriptor.aPrintOptions.getLength(); ++nOption) - { - if( pPrintOptions[nOption].Name == "CopyCount" || pPrintOptions[nOption].Name == "FileName" - || pPrintOptions[nOption].Name == "Collate" || pPrintOptions[nOption].Name == "Pages" - || pPrintOptions[nOption].Name == "Wait" || pPrintOptions[nOption].Name == "PrinterName" ) - { - // add an option - aOptions.realloc( nIndex + 1 ); - aOptions[ nIndex ].Name = pPrintOptions[nOption].Name; - aOptions[ nIndex++ ].Value = pPrintOptions[nOption].Value ; - } - } - - pTargetView->ExecPrint( aOptions, IsMergeSilent(), rMergeDescriptor.bPrintAsync ); + lcl_PreparePrinterOptions( rMergeDescriptor.aPrintOptions, true, aOptions ); + pTargetView->ExecPrint( aOptions, IsMergeSilent(), rMergeDescriptor.bPrintAsync ); } // Leave docshell available for caller (e.g. MM wizard) @@ -2057,8 +2024,8 @@ bool SwDBManager::GetMergeColumnCnt(const OUString& rColumnName, sal_uInt16 n bool SwDBManager::ToNextMergeRecord() { - OSL_ENSURE(pImpl->pMergeData && pImpl->pMergeData->xResultSet.is(), "no data source in merge"); - return ToNextRecord(pImpl->pMergeData); + assert( pImpl->pMergeData && pImpl->pMergeData->xResultSet.is() && "no data source in merge" ); + return lcl_ToNextRecord( pImpl->pMergeData ); } bool SwDBManager::FillCalcWithMergeData( SvNumberFormatter *pDocFormatter, @@ -2136,7 +2103,9 @@ bool SwDBManager::ToNextRecord( if(pImpl->pMergeData && rDataSource == pImpl->pMergeData->sDataSource && rCommand == pImpl->pMergeData->sCommand) + { pFound = pImpl->pMergeData; + } else { SwDBData aData; @@ -2145,48 +2114,69 @@ bool SwDBManager::ToNextRecord( aData.nCommandType = -1; pFound = FindDSData(aData, false); } - return ToNextRecord(pFound); + return lcl_ToNextRecord( pFound ); } -bool SwDBManager::ToNextRecord(SwDSParam* pParam) +static bool lcl_ToNextRecord( SwDSParam* pParam, const SwDBNextRecord action ) { bool bRet = true; - if(!pParam || !pParam->xResultSet.is() || pParam->bEndOfDB || - (pParam->aSelection.getLength() && pParam->aSelection.getLength() <= pParam->nSelectionIndex)) + + assert( action == SwDBNextRecord::NEXT || action == SwDBNextRecord::FIRST ); + + if( action == SwDBNextRecord::FIRST ) + { + pParam->nSelectionIndex = 0; + pParam->bEndOfDB = + pParam->bAfterSelection = false; + } + + if( !pParam || !pParam->xResultSet.is() || pParam->bEndOfDB || + (pParam->aSelection.getLength() && + pParam->aSelection.getLength() <= pParam->nSelectionIndex) ) { - if(pParam) + if( pParam ) pParam->CheckEndOfDB(); return false; } try { - if(pParam->aSelection.getLength()) + if( pParam->aSelection.getLength() ) { sal_Int32 nPos = 0; pParam->aSelection.getConstArray()[ pParam->nSelectionIndex++ ] >>= nPos; pParam->bEndOfDB = !pParam->xResultSet->absolute( nPos ); pParam->CheckEndOfDB(); bRet = !pParam->bEndOfDB; - if(pParam->nSelectionIndex >= pParam->aSelection.getLength()) + if( pParam->nSelectionIndex >= pParam->aSelection.getLength() ) pParam->bEndOfDB = true; } + else if( action == SwDBNextRecord::FIRST ) + { + pParam->bEndOfDB = !pParam->xResultSet->first(); + pParam->CheckEndOfDB(); + bRet = !pParam->bEndOfDB; + } else { sal_Int32 nBefore = pParam->xResultSet->getRow(); pParam->bEndOfDB = !pParam->xResultSet->next(); - if( !pParam->bEndOfDB && nBefore == pParam->xResultSet->getRow()) + if( !pParam->bEndOfDB && nBefore == pParam->xResultSet->getRow() ) { - //next returned true but it didn't move + // next returned true but it didn't move pParam->bEndOfDB = true; } - pParam->CheckEndOfDB(); bRet = !pParam->bEndOfDB; ++pParam->nSelectionIndex; } } - catch(const uno::Exception&) + catch( const uno::Exception &e ) { + pParam->bEndOfDB = true; + pParam->CheckEndOfDB(); + // we allow merging with empty databases, so don't warn on init + SAL_WARN_IF(action == SwDBNextRecord::NEXT, + "sw.mailmerge", "exception in ToNextRecord(): " << e.Message); } return bRet; } @@ -2823,7 +2813,8 @@ void SwDBManager::ExecuteFormLetter( SwWrtShell& rSh, // SfxObjectShellRef is ok, since there should be no control over the document lifetime here SfxObjectShellRef xDocShell = rSh.GetView().GetViewFrame()->GetObjectShell(); - SfxGetpApp()->NotifyEvent(SfxEventHint(SW_EVENT_MAIL_MERGE, SwDocShell::GetEventName(STR_SW_EVENT_MAIL_MERGE), xDocShell)); + + EMIT_SW_EVENT(MAIL_MERGE, xDocShell); { //copy rSh to aTempFile OUString sTempURL; @@ -2898,7 +2889,7 @@ void SwDBManager::ExecuteFormLetter( SwWrtShell& rSh, //remove the temporary file SWUnoHelper::UCB_DeleteFile( sTempURL ); } - SfxGetpApp()->NotifyEvent(SfxEventHint(SW_EVENT_MAIL_MERGE_END, SwDocShell::GetEventName(STR_SW_EVENT_MAIL_MERGE_END), rSh.GetView().GetViewFrame()->GetObjectShell())); + EMIT_SW_EVENT(MAIL_MERGE_END, xDocShell); // reset the cursor inside xResSet = nullptr; |