tdf#117212 sw mailmerge: merge only the selected range

All records were merged before saving or printing the
record range selected in “From 'X' To 'Y' of the
“Save Merged Documents” or “Print Merged Documents”
dialog windows (opened via the Mail merge toolbar).
This was a waste of time in the case of selecting
only a few records.

Change-Id: I52c47d3c5707b09ed7f9b05a1a0f4d20a5595b8d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/112922
Tested-by: László Németh <nemeth@numbertext.org>
Reviewed-by: László Németh <nemeth@numbertext.org>
diff --git a/sw/source/ui/dbui/mmresultdialogs.cxx b/sw/source/ui/dbui/mmresultdialogs.cxx
index ef10e4f..2c9d890 100644
--- a/sw/source/ui/dbui/mmresultdialogs.cxx
+++ b/sw/source/ui/dbui/mmresultdialogs.cxx
@@ -551,6 +551,15 @@
    SwView* pView = ::GetActiveView();
    std::shared_ptr<SwMailMergeConfigItem> xConfigItem = pView->GetMailMergeConfigItem();
    assert(xConfigItem);

    sal_uInt32 nBegin = static_cast<sal_Int32>(m_xFromNF->get_value() - 1);
    sal_uInt32 nEnd = static_cast<sal_Int32>(m_xToNF->get_value());
    sal_uInt32 nMax = static_cast<sal_Int32>(m_xToNF->get_max());
    if (nEnd > nMax)
        nEnd = nMax;

    xConfigItem->SetBeginEnd(nBegin, nEnd);

    if (!xConfigItem->GetTargetView())
        SwDBManager::PerformMailMerge(pView);

@@ -594,18 +603,6 @@
    }
    else
    {
        const sal_uInt32 nDocumentCount = xConfigItem->GetMergedDocumentCount();
        sal_uInt32 nBegin = 0;
        sal_uInt32 nEnd = nDocumentCount;

        if (!m_xSaveIndividualRB->get_active())
        {
            nBegin  = static_cast< sal_Int32 >(m_xFromNF->get_value() - 1);
            nEnd    = static_cast< sal_Int32 >(m_xToNF->get_value());
            if(nEnd > nDocumentCount)
                nEnd = nDocumentCount;
        }

        OUString sTargetTempURL = URIHelper::SmartRel2Abs(
            INetURLObject(), utl::TempFile::CreateTempName(),
            URIHelper::GetMaybeFileHdl());
@@ -649,7 +646,7 @@
            xSaveMonitor.reset();
        });

        for(sal_uInt32 nDoc = nBegin; nDoc < nEnd && !m_bCancelSaving; ++nDoc)
        for(sal_uInt32 nDoc = 0; nDoc < nEnd - nBegin && !m_bCancelSaving; ++nDoc)
        {
            INetURLObject aURL(sPath);
            OUString sExtension = aURL.getExtension();
@@ -658,7 +655,7 @@
                sExtension = pSfxFlt->GetWildcard().getGlob().getToken(1, '.');
                sPath += "." + sExtension;
            }
            OUString sStat = SwResId(STR_STATSTR_LETTER) + " " + OUString::number( nDoc );
            OUString sStat = SwResId(STR_STATSTR_LETTER) + " " + OUString::number(nBegin + nDoc);
            xSaveMonitor->m_xPrintInfo->set_label(sStat);

            //now extract a document from the target document
@@ -686,7 +683,7 @@
            pTargetView->GetWrtShell().EndAction();
            //then save it
            OUString sOutPath = aURL.GetMainURL(INetURLObject::DecodeMechanism::ToIUri);
            OUString sCounter = "_" + OUString::number(nDoc + 1);
            OUString sCounter = "_" + OUString::number(nBegin + nDoc + 1);
            sOutPath = sOutPath.replaceAt( sOutPath.getLength() - sExtension.getLength() - 1, 0, sCounter);

            while(true)
@@ -775,24 +772,21 @@
    SwView* pView = ::GetActiveView();
    std::shared_ptr<SwMailMergeConfigItem> xConfigItem = pView->GetMailMergeConfigItem();
    assert(xConfigItem);

    sal_uInt32 nBegin = static_cast<sal_Int32>(m_xFromNF->get_value() - 1);
    sal_uInt32 nEnd = static_cast<sal_Int32>(m_xToNF->get_value());
    sal_uInt32 nMax = static_cast<sal_Int32>(m_xToNF->get_max());
    if (nEnd > nMax)
        nEnd = nMax;

    xConfigItem->SetBeginEnd(nBegin, nEnd);

    if(!xConfigItem->GetTargetView())
        SwDBManager::PerformMailMerge(pView);

    SwView* pTargetView = xConfigItem->GetTargetView();
    assert(pTargetView);

    const sal_uInt32 nDocumentCount = xConfigItem->GetMergedDocumentCount();
    sal_uInt32 nBegin = 0;
    sal_uInt32 nEnd = nDocumentCount;

    if (!m_xPrintAllRB->get_active())
    {
        nBegin  = m_xFromNF->get_value() - 1;
        nEnd    = m_xToNF->get_value();
        if(nEnd > nDocumentCount)
            nEnd = nDocumentCount;
    }

    // If we skip autoinserted blanks, then the page numbers used in the print range string
    // refer to the non-blank pages as they appear in the document (see tdf#89708).
    const bool bIgnoreEmptyPages =
@@ -889,6 +883,15 @@
    SwView* pView = ::GetActiveView();
    std::shared_ptr<SwMailMergeConfigItem> xConfigItem = pView->GetMailMergeConfigItem();
    assert(xConfigItem);

    sal_uInt32 nBegin = static_cast<sal_Int32>(m_xFromNF->get_value() - 1);
    sal_uInt32 nEnd = static_cast<sal_Int32>(m_xToNF->get_value());
    sal_uInt32 nMax = static_cast<sal_Int32>(m_xToNF->get_max());
    if (nEnd > nMax)
        nEnd = nMax;

    xConfigItem->SetBeginEnd(nBegin, nEnd);

    if (!xConfigItem->GetTargetView())
        SwDBManager::PerformMailMerge(pView);

@@ -920,16 +923,6 @@
        lcl_UpdateEmailSettingsFromGlobalConfig(*xConfigItem);
    }
    //add the documents
    const sal_uInt32 nDocumentCount = xConfigItem->GetMergedDocumentCount();
    sal_uInt32 nBegin = 0;
    sal_uInt32 nEnd = nDocumentCount;
    if (!m_xSendAllRB->get_active())
    {
        nBegin  = static_cast< sal_Int32 >(m_xFromNF->get_value() - 1);
        nEnd    = static_cast< sal_Int32 >(m_xToNF->get_value());
        if(nEnd > nDocumentCount)
            nEnd = nDocumentCount;
    }
    bool bAsBody = false;
    rtl_TextEncoding eEncoding = ::osl_getThreadTextEncoding();
    SfxFilterContainer* pFilterContainer = SwDocShell::Factory().GetFilterContainer();
diff --git a/sw/source/uibase/dbui/mmconfigitem.cxx b/sw/source/uibase/dbui/mmconfigitem.cxx
index ae4f5b6..083e1a7 100644
--- a/sw/source/uibase/dbui/mmconfigitem.cxx
+++ b/sw/source/uibase/dbui/mmconfigitem.cxx
@@ -688,6 +688,8 @@
    m_bAddressInserted(false),
    m_bGreetingInserted(false),
    m_nGreetingMoves(0),
    m_nBegin(0),
    m_nEnd(0),
    m_pSourceView(nullptr),
    m_pTargetView(nullptr)
{
@@ -982,7 +984,10 @@
    return m_pImpl->m_nResultSetCursorPos;
}

bool SwMailMergeConfigItem::IsRecordExcluded(sal_Int32 nRecord) const
bool SwMailMergeConfigItem::IsRecordIncluded(sal_uInt32 nRecord) const
    { return nRecord > m_nBegin && nRecord <= m_nEnd; }

bool SwMailMergeConfigItem::IsRecordExcluded(sal_uInt32 nRecord) const
    { return m_aExcludedRecords.find(nRecord) != m_aExcludedRecords.end(); }

void SwMailMergeConfigItem::ExcludeRecord(sal_Int32 nRecord, bool bExclude)
@@ -1000,12 +1005,11 @@
    if(!m_pImpl->m_xResultSet.is())
        return {};
    m_pImpl->m_xResultSet->last();
    sal_Int32 nResultSetSize = m_pImpl->m_xResultSet->getRow()+1;
    sal_uInt32 nResultSetSize = m_pImpl->m_xResultSet->getRow()+1;
    std::vector<uno::Any> vResult;
    vResult.reserve(nResultSetSize);
    for(sal_Int32 nIdx=1; nIdx<nResultSetSize;++nIdx)
        if(!IsRecordExcluded(nIdx))
            vResult.push_back(uno::makeAny<sal_Int32>(nIdx));
    for(sal_uInt32 nIdx=1; nIdx<nResultSetSize;++nIdx)
        if( !IsRecordExcluded(nIdx) && IsRecordIncluded(nIdx) )
            vResult.push_back(uno::makeAny<sal_uInt32>(nIdx));
    return comphelper::containerToSequence(vResult);
}

diff --git a/sw/source/uibase/inc/mmconfigitem.hxx b/sw/source/uibase/inc/mmconfigitem.hxx
index 66441d3..d1296df 100644
--- a/sw/source/uibase/inc/mmconfigitem.hxx
+++ b/sw/source/uibase/inc/mmconfigitem.hxx
@@ -56,6 +56,8 @@
    bool m_bAddressInserted;
    bool m_bGreetingInserted;
    sal_Int32 m_nGreetingMoves;
    sal_uInt32 m_nBegin;
    sal_uInt32 m_nEnd;
    std::set<sal_Int32> m_aExcludedRecords;
    css::uno::Reference<css::view::XSelectionChangeListener> m_xDBChangedListener;

@@ -105,7 +107,8 @@
    sal_Int32 GetResultSetPosition()const;
    bool IsResultSetFirstLast(bool& bIsFirst, bool& bIsLast);

    bool IsRecordExcluded(sal_Int32 nRecord) const;
    bool IsRecordIncluded(sal_uInt32 nRecord) const;
    bool IsRecordExcluded(sal_uInt32 nRecord) const;
    void ExcludeRecord(sal_Int32 nRecord, bool bExclude);
    css::uno::Sequence< css::uno::Any> GetSelection() const;

@@ -216,6 +219,9 @@
    void SetGreetingInserted()
        { m_bGreetingInserted = true; }

    void SetBeginEnd(sal_uInt32 nBegin, sal_uInt32 nEnd)
        { m_nBegin = nBegin; m_nEnd = nEnd; }

    // counts the moves in the layout page
    void MoveGreeting( sal_Int32 nMove) { m_nGreetingMoves += nMove;}
    sal_Int32 GetGreetingMoves() const { return m_nGreetingMoves;}