diff options
author | Jan-Marek Glogowski <glogow@fbihome.de> | 2016-10-04 10:13:24 +0000 |
---|---|---|
committer | Jan-Marek Glogowski <glogow@fbihome.de> | 2016-11-25 16:27:06 +0100 |
commit | bbf246e40c7814bfc4038d456d388d1f90048287 (patch) | |
tree | c2c1d4cced69abc186de3f5ecf95eefe956af3f8 | |
parent | 660159a68acd54a8f213920457c1aeed79cffea5 (diff) |
tdf#102010 Never overwrite MM files via UNO
Fixes the regression introduced by
commit e637b6743a506ef74c93ccbe15ab6642f3baa34f
This commit removed the crazy bSubjectIsFilename handling,
where I didn't understood the case of bSubjectIsFilename
and a user supplied prefix.
Mail merge to files never overwrites an existing document,
but there is the special case, when a user selects a target
filename in the MM dialog for single file MM.
Should be fixed by a successive commmit, reverting this and
removing an existing file before starting the MM job.
Change-Id: Idda487023e6984de9c1e701fc088a6b7f92e9847
-rw-r--r-- | sw/CppunitTest_sw_mailmerge.mk | 1 | ||||
-rw-r--r-- | sw/inc/dbmgr.hxx | 19 | ||||
-rw-r--r-- | sw/qa/extras/mailmerge/mailmerge.cxx | 46 | ||||
-rw-r--r-- | sw/source/uibase/dbui/dbmgr.cxx | 17 | ||||
-rw-r--r-- | sw/source/uibase/uno/unomailmerge.cxx | 2 |
5 files changed, 72 insertions, 13 deletions
diff --git a/sw/CppunitTest_sw_mailmerge.mk b/sw/CppunitTest_sw_mailmerge.mk index 693a5e2aa7df..8d2a3f8ac75e 100644 --- a/sw/CppunitTest_sw_mailmerge.mk +++ b/sw/CppunitTest_sw_mailmerge.mk @@ -21,6 +21,7 @@ $(eval $(call gb_CppunitTest_use_libraries,sw_mailmerge, \ sfx \ sw \ test \ + tl \ unotest \ utl \ )) diff --git a/sw/inc/dbmgr.hxx b/sw/inc/dbmgr.hxx index 6078b2782d21..b124106e5e9f 100644 --- a/sw/inc/dbmgr.hxx +++ b/sw/inc/dbmgr.hxx @@ -162,7 +162,23 @@ struct SwMergeDescriptor * @defgroup file Mail merge as File settings * @addtogroup file * @{ */ - OUString sPath; + + /** + * Basename incl. the path for the generated files. + * + * The final filename will be created by concating a number to prevent + * overwriting an existing file and the extension based on the filter + * settings. + */ + OUString sPrefix; + /** + * Use the sPrefix as the target filename also overwriting an existing + * target file. + * + * Just used for the internal mail merge dialogs as mail merge never + * overwrites existing files (see SwDBManager::ExecuteFormLetter). + */ + bool bPrefixIsFilename; /** @} */ /** @@ -205,6 +221,7 @@ struct SwMergeDescriptor rSh(rShell), rDescriptor(rDesc), bCreateSingleFile( false ), + bPrefixIsFilename( false ), bSendAsHTML( true ), bSendAsAttachment( false ), pMailMergeConfigItem( nullptr ) diff --git a/sw/qa/extras/mailmerge/mailmerge.cxx b/sw/qa/extras/mailmerge/mailmerge.cxx index 7d45b1730006..0c356526de26 100644 --- a/sw/qa/extras/mailmerge/mailmerge.cxx +++ b/sw/qa/extras/mailmerge/mailmerge.cxx @@ -25,6 +25,8 @@ #include <com/sun/star/sdbc/XRowSet.hpp> #include <com/sun/star/sdbcx/XRowLocate.hpp> +#include <tools/urlobj.hxx> + #include <wrtsh.hxx> #include <ndtxt.hxx> #include <swdtflvr.hxx> @@ -145,7 +147,8 @@ public: ( OUString( m_directories.getURLFromSrc(mpTestDocumentPath) + OUString::createFromAscii(filename)) ) ) ) ); mMMargs.push_back( beans::NamedValue( OUString( UNO_NAME_DATA_SOURCE_NAME ), uno::Any( aDBName ) ) ); mMMargs.push_back( beans::NamedValue( OUString( UNO_NAME_OUTPUT_URL ), uno::Any( aWorkDir ) ) ); - mMMargs.push_back( beans::NamedValue( OUString( UNO_NAME_FILE_NAME_PREFIX ), uno::Any( aPrefix )) ); + if (file) + mMMargs.push_back( beans::NamedValue( OUString( UNO_NAME_FILE_NAME_PREFIX ), uno::Any( aPrefix )) ); if (bPrefixIsColumn) mMMargs.push_back( beans::NamedValue( OUString( UNO_NAME_FILE_NAME_FROM_COLUMN ), uno::Any( true ) ) ); @@ -173,7 +176,7 @@ public: } - void executeMailMerge() + void executeMailMerge( bool bDontLoadResult = false ) { uno::Sequence< beans::NamedValue > aSeqMailMergeArgs = comphelper::containerToSequence( mMMargs ); uno::Any res = mxJob->execute( aSeqMailMergeArgs ); @@ -196,6 +199,8 @@ public: bOk &= rValue >>= mnCurOutputType; else if (rName == UNO_NAME_FILE_NAME_FROM_COLUMN) bOk &= rValue >>= bMMFilenameFromColumn; + else if (rName == UNO_NAME_DOCUMENT_URL) + bOk &= rValue >>= msMailMergeDocumentURL; } CPPUNIT_ASSERT(bOk); @@ -214,7 +219,7 @@ public: else { CPPUNIT_ASSERT(res == true); - if( !bMMFilenameFromColumn ) + if( !bMMFilenameFromColumn && !bDontLoadResult ) loadMailMergeDocument( 0 ); } } @@ -252,7 +257,17 @@ public: */ void loadMailMergeDocument( int number ) { - OUString name = msMailMergeOutputPrefix + OUString::number( number ) + ".odt"; + OUString name; + if (!msMailMergeOutputPrefix.isEmpty()) + name = msMailMergeOutputPrefix; + else + { + INetURLObject aURLObj; + aURLObj.SetSmartProtocol( INetProtocol::File ); + aURLObj.SetSmartURL( msMailMergeDocumentURL ); + name = aURLObj.GetBase(); + } + name += OUString::number( number ) + ".odt"; loadMailMergeDocument( name ); } @@ -262,6 +277,7 @@ protected: uno::Reference< css::task::XJob > mxJob; std::vector< beans::NamedValue > mMMargs; + OUString msMailMergeDocumentURL; OUString msMailMergeOutputURL; OUString msMailMergeOutputPrefix; sal_Int16 mnCurOutputType; @@ -603,5 +619,27 @@ DECLARE_FILE_MAILMERGE_TEST_COLUMN(testDirMailMerge, "simple-mail-merge.odt", "1 } } +DECLARE_FILE_MAILMERGE_TEST(testTdf102010, "empty.odt", "10-testing-addresses.ods", "testing-addresses") +{ + // Create "correct" automatic filename for non-user-supplied-prefix + for (auto aNamedValueIter = mMMargs.begin(); aNamedValueIter != mMMargs.end();) + { + if ( aNamedValueIter->Name == UNO_NAME_FILE_NAME_PREFIX ) + aNamedValueIter = mMMargs.erase( aNamedValueIter ); + else + { + std::cout << aNamedValueIter->Name << ": " << aNamedValueIter->Value << std::endl; + ++aNamedValueIter; + } + } + mMMargs.push_back( beans::NamedValue( OUString( UNO_NAME_SAVE_AS_SINGLE_FILE ), uno::Any( true ) ) ); + + // Generate correct mail merge result filename + executeMailMerge(); + // Don't overwrite previous result + executeMailMerge( true ); + loadMailMergeDocument( 1 ); +} + CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/uibase/dbui/dbmgr.cxx b/sw/source/uibase/dbui/dbmgr.cxx index 1a3e03537d7d..398486cfc5c1 100644 --- a/sw/source/uibase/dbui/dbmgr.cxx +++ b/sw/source/uibase/dbui/dbmgr.cxx @@ -1098,11 +1098,13 @@ bool SwDBManager::MergeMailFiles(SwWrtShell* pSourceShell, bool bCheckSingleFile_ = rMergeDescriptor.bCreateSingleFile; if( bMT_EMAIL ) { + assert( !rMergeDescriptor.bPrefixIsFilename ); assert( bMT_EMAIL && !bCheckSingleFile_ ); bCheckSingleFile_ = false; } else if( bMT_SHELL || bMT_PRINTER ) { + assert( !rMergeDescriptor.bPrefixIsFilename ); assert( (bMT_SHELL || bMT_PRINTER) && bCheckSingleFile_ ); bCheckSingleFile_ = true; } @@ -1318,7 +1320,7 @@ bool SwDBManager::MergeMailFiles(SwWrtShell* pSourceShell, // create a new temporary file name - only done once in case of bCreateSingleFile if( bNeedsTempFiles && ( !bWorkDocInitialized || !bCreateSingleFile )) { - OUString sPath = rMergeDescriptor.sPath; + OUString sPrefix = rMergeDescriptor.sPrefix; OUString sLeading; //#i97667# if the name is from a database field then it will be used _as is_ @@ -1331,14 +1333,14 @@ bool SwDBManager::MergeMailFiles(SwWrtShell* pSourceShell, } else { - INetURLObject aEntry( sPath ); + INetURLObject aEntry( sPrefix ); sLeading = aEntry.GetBase(); aEntry.removeSegment(); - sPath = aEntry.GetMainURL( INetURLObject::NO_DECODE ); + sPrefix = aEntry.GetMainURL( INetURLObject::NO_DECODE ); } OUString sExt(comphelper::string::stripStart(pStoreToFilter->GetDefaultExtension(), '*')); - aTempFile.reset( new utl::TempFile(sLeading, sColumnData.isEmpty(), &sExt, &sPath, true) ); + aTempFile.reset( new utl::TempFile(sLeading, sColumnData.isEmpty(), &sExt, &sPrefix, true) ); if( !aTempFile->IsValid() ) { ErrorHandler::HandleError( ERRCODE_IO_NOTSUPPORTED ); @@ -1559,11 +1561,11 @@ bool SwDBManager::MergeMailFiles(SwWrtShell* pSourceShell, // save merged document assert( aTempFile.get() ); INetURLObject aTempFileURL; - if( rMergeDescriptor.sPath.isEmpty() ) + if( rMergeDescriptor.sPrefix.isEmpty() || !rMergeDescriptor.bPrefixIsFilename ) aTempFileURL.SetURL( aTempFile->GetURL() ); else { - aTempFileURL.SetURL( rMergeDescriptor.sPath ); + aTempFileURL.SetURL( rMergeDescriptor.sPrefix ); // remove the unneeded temporary file aTempFile->EnableKillingFile(); } @@ -2863,7 +2865,8 @@ void SwDBManager::ExecuteFormLetter( SwWrtShell& rSh, SwMergeDescriptor aMergeDesc( pImpl->pMergeDialog->GetMergeType(), rSh, aDescriptor ); aMergeDesc.sSaveToFilter = pImpl->pMergeDialog->GetSaveFilter(); aMergeDesc.bCreateSingleFile = pImpl->pMergeDialog->IsSaveSingleDoc(); - aMergeDesc.sPath = pImpl->pMergeDialog->GetTargetURL(); + aMergeDesc.bPrefixIsFilename = aMergeDesc.bCreateSingleFile; + aMergeDesc.sPrefix = pImpl->pMergeDialog->GetTargetURL(); if( !aMergeDesc.bCreateSingleFile && pImpl->pMergeDialog->IsGenerateFromDataBase() ) { aMergeDesc.sDBcolumn = pImpl->pMergeDialog->GetColumnName(); diff --git a/sw/source/uibase/uno/unomailmerge.cxx b/sw/source/uibase/uno/unomailmerge.cxx index 49aaacc1a422..010749627b71 100644 --- a/sw/source/uibase/uno/unomailmerge.cxx +++ b/sw/source/uibase/uno/unomailmerge.cxx @@ -738,7 +738,7 @@ uno::Any SAL_CALL SwXMailMerge::execute( aPath += aCurFileNamePrefix; } - aMergeDesc.sPath = aPath; + aMergeDesc.sPrefix = aPath; aMergeDesc.sSaveToFilter = m_sSaveFilter; aMergeDesc.sSaveToFilterOptions = m_sSaveFilterOptions; aMergeDesc.aSaveToFilterData = m_aSaveFilterData; |