Resolves: tdf#122404 unlock just the toplevels that were locked

push what toplevels got locked to just unlock those ones. otherwise the just
dismissed toplevel may still be present in the Application toplevel list.

merge all the similar examples of this.

Change-Id: I77c0d55d1e4b3bcc3b8d88fef00ba289edd1e831
Reviewed-on: https://gerrit.libreoffice.org/66080
Tested-by: Jenkins
Tested-by: Xisco Faulí <xiscofauli@libreoffice.org>
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
Tested-by: Caolán McNamara <caolanm@redhat.com>
diff --git a/desktop/source/deployment/gui/dp_gui_dialog2.cxx b/desktop/source/deployment/gui/dp_gui_dialog2.cxx
index fc0f955..4fa481a 100644
--- a/desktop/source/deployment/gui/dp_gui_dialog2.cxx
+++ b/desktop/source/deployment/gui/dp_gui_dialog2.cxx
@@ -339,7 +339,6 @@ DialogHelper::DialogHelper(const uno::Reference< uno::XComponentContext > &xCont
                           Dialog *pWindow)
    : m_xVCLWindow(pWindow)
    , m_nEventID(nullptr)
    , m_nBusy(0)
{
    m_xContext = xContext;
}
@@ -461,34 +460,6 @@ void DialogHelper::PostUserEvent( const Link<void*,void>& rLink, void* pCaller )
    m_nEventID = Application::PostUserEvent( rLink, pCaller, true/*bReferenceLink*/ );
}

void DialogHelper::incBusy()
{
    ++m_nBusy;
    // lock any toplevel windows from being closed until busy is over
    // ensure any dialogs are reset before entering
    vcl::Window *xTopWin = Application::GetFirstTopLevelWindow();
    while (xTopWin)
    {
        if (xTopWin != m_xVCLWindow)
            xTopWin->IncModalCount();
        xTopWin = Application::GetNextTopLevelWindow(xTopWin);
    }
}

void DialogHelper::decBusy()
{
    --m_nBusy;
    // unlock any toplevel windows from being closed until busy is over
    // ensure any dialogs are reset before entering
    vcl::Window *xTopWin = Application::GetFirstTopLevelWindow();
    while (xTopWin)
    {
        if (xTopWin != m_xVCLWindow)
            xTopWin->DecModalCount();
        xTopWin = Application::GetNextTopLevelWindow(xTopWin);
    }
}

//                             ExtMgrDialog
ExtMgrDialog::ExtMgrDialog(vcl::Window *pParent, TheExtensionManager *pManager, Dialog::InitFlag eFlag)
    : ModelessDialog(pParent, "ExtensionManagerDialog", "desktop/ui/extensionmanager.ui", eFlag)
diff --git a/desktop/source/deployment/gui/dp_gui_dialog2.hxx b/desktop/source/deployment/gui/dp_gui_dialog2.hxx
index dd0dc8b..a2d88c0f 100644
--- a/desktop/source/deployment/gui/dp_gui_dialog2.hxx
+++ b/desktop/source/deployment/gui/dp_gui_dialog2.hxx
@@ -29,6 +29,7 @@
#include <vcl/prgsbar.hxx>
#include <vcl/timer.hxx>
#include <vcl/idle.hxx>
#include <vcl/waitobj.hxx>
#include <vcl/weld.hxx>

#include <svtools/svmedit.hxx>
@@ -47,6 +48,9 @@
#include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
#include <com/sun/star/util/XModifyListener.hpp>

#include <stack>
#include <vector>

namespace dp_gui {


@@ -60,7 +64,7 @@ class DialogHelper
    css::uno::Reference< css::uno::XComponentContext > m_xContext;
    VclPtr<Dialog>  m_xVCLWindow;
    ImplSVEvent *   m_nEventID;
    int             m_nBusy;
    TopLevelWindowLocker m_aBusy;

public:
                    DialogHelper( const css::uno::Reference< css::uno::XComponentContext > &,
@@ -91,14 +95,13 @@ public:
                                               const char* pResID,
                                               bool &bHadWarning );

    void            incBusy();
    void            decBusy();
    bool            isBusy() const { return m_nBusy > 0; }
    void            incBusy() { m_aBusy.incBusy(m_xVCLWindow); }
    void            decBusy() { m_aBusy.decBusy(); }
    bool            isBusy() const { return m_aBusy.isBusy(); }
    bool            installExtensionWarn(const OUString &rExtensionURL);
    bool            installForAllUsers(bool &bInstallForAll);
};


class ExtMgrDialog : public ModelessDialog,
                     public DialogHelper
{
diff --git a/filter/source/xsltdialog/xmlfiltersettingsdialog.cxx b/filter/source/xsltdialog/xmlfiltersettingsdialog.cxx
index 3af2023..a62ab15 100644
--- a/filter/source/xsltdialog/xmlfiltersettingsdialog.cxx
+++ b/filter/source/xsltdialog/xmlfiltersettingsdialog.cxx
@@ -131,32 +131,6 @@ void XMLFilterSettingsDialog::dispose()
    ModelessDialog::dispose();
}

void XMLFilterSettingsDialog::incBusy()
{
    // lock any toplevel windows from being closed until busy is over
    // ensure any dialogs are reset before entering
    vcl::Window *xTopWin = Application::GetFirstTopLevelWindow();
    while (xTopWin)
    {
        if (xTopWin != this)
            xTopWin->IncModalCount();
        xTopWin = Application::GetNextTopLevelWindow(xTopWin);
    }
}

void XMLFilterSettingsDialog::decBusy()
{
    // unlock any toplevel windows from being closed until busy is over
    // ensure any dialogs are reset before entering
    vcl::Window *xTopWin = Application::GetFirstTopLevelWindow();
    while (xTopWin)
    {
        if (xTopWin != this)
            xTopWin->DecModalCount();
        xTopWin = Application::GetNextTopLevelWindow(xTopWin);
    }
}

IMPL_LINK(XMLFilterSettingsDialog, ClickHdl_Impl, Button *, pButton, void )
{
    // tdf#122171 block closing libreoffice until the following dialog is dismissed
diff --git a/filter/source/xsltdialog/xmlfiltersettingsdialog.hxx b/filter/source/xsltdialog/xmlfiltersettingsdialog.hxx
index 37ed80f..1a8ddd6 100644
--- a/filter/source/xsltdialog/xmlfiltersettingsdialog.hxx
+++ b/filter/source/xsltdialog/xmlfiltersettingsdialog.hxx
@@ -26,6 +26,7 @@
#include <vcl/button.hxx>
#include <vcl/dialog.hxx>
#include <vcl/layout.hxx>
#include <vcl/waitobj.hxx>
#include <svtools/svtabbx.hxx>
#include <svl/poolitem.hxx>
#include <unotools/moduleoptions.hxx>
@@ -109,8 +110,8 @@ private:
    void    initFilterList();
    void    disposeFilterList();

    void    incBusy();
    void    decBusy();
    void    incBusy() { maBusy.incBusy(this); }
    void    decBusy() { maBusy.decBusy(); }

    bool    insertOrEdit( filter_info_impl* pNewInfo, const filter_info_impl* pOldInfo = nullptr );

@@ -126,6 +127,7 @@ private:

    std::vector< filter_info_impl* > maFilterVector;

    TopLevelWindowLocker maBusy;
    VclPtr<XMLFilterListBox>   m_pFilterListBox;
    VclPtr<SvxPathControl> m_pCtrlFilterList;
    VclPtr<PushButton> m_pPBNew;
diff --git a/include/sfx2/sfxhelp.hxx b/include/sfx2/sfxhelp.hxx
index cda688c..bdfaaf8 100644
--- a/include/sfx2/sfxhelp.hxx
+++ b/include/sfx2/sfxhelp.hxx
@@ -40,8 +40,6 @@ private:
    SAL_DLLPRIVATE virtual bool Start(const OUString& rURL, weld::Widget* pWidget) override;
    SAL_DLLPRIVATE static OUString GetHelpModuleName_Impl(const OUString &rHelpId);
    SAL_DLLPRIVATE static OUString CreateHelpURL_Impl( const OUString& aCommandURL, const OUString& rModuleName );
    SAL_DLLPRIVATE static void incBusy(const vcl::Window* pParent);
    SAL_DLLPRIVATE static void decBusy(const vcl::Window* pParent);

public:
    SfxHelp();
diff --git a/include/vcl/waitobj.hxx b/include/vcl/waitobj.hxx
index daf0e2a..858f49d 100644
--- a/include/vcl/waitobj.hxx
+++ b/include/vcl/waitobj.hxx
@@ -23,6 +23,9 @@
#include <vcl/dllapi.h>
#include <vcl/window.hxx>

#include <stack>
#include <vector>

class VCL_DLLPUBLIC WaitObject
{
private:
@@ -37,6 +40,18 @@ public:
                ~WaitObject();
};

class VCL_DLLPUBLIC TopLevelWindowLocker
{
private:
    std::stack<std::vector<VclPtr<vcl::Window>>> m_aBusyStack;
public:
    // lock all toplevels, except the argument
    void incBusy(const vcl::Window* pIgnore);
    // unlock previous lock
    void decBusy();
    bool isBusy() const { return !m_aBusyStack.empty(); }
};

#endif // INCLUDED_VCL_WAITOBJ_HXX

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sfx2/source/appl/sfxhelp.cxx b/sfx2/source/appl/sfxhelp.cxx
index ec8f073..36a7565 100644
--- a/sfx2/source/appl/sfxhelp.cxx
+++ b/sfx2/source/appl/sfxhelp.cxx
@@ -55,6 +55,7 @@
#include <rtl/uri.hxx>
#include <vcl/commandinfoprovider.hxx>
#include <vcl/layout.hxx>
#include <vcl/waitobj.hxx>
#include <vcl/weld.hxx>
#include <svtools/ehdl.hxx>
#include <svtools/sfxecode.hxx>
@@ -739,32 +740,6 @@ namespace
    }
}

void SfxHelp::incBusy(const vcl::Window* pParent)
{
    // lock any toplevel windows from being closed until busy is over
    // ensure any dialogs are reset before entering
    vcl::Window *xTopWin = Application::GetFirstTopLevelWindow();
    while (xTopWin)
    {
        if (xTopWin != pParent)
            xTopWin->IncModalCount();
        xTopWin = Application::GetNextTopLevelWindow(xTopWin);
    }
}

void SfxHelp::decBusy(const vcl::Window* pParent)
{
    // unlock any toplevel windows from being closed until busy is over
    // ensure any dialogs are reset before entering
    vcl::Window *xTopWin = Application::GetFirstTopLevelWindow();
    while (xTopWin)
    {
        if (xTopWin != pParent)
            xTopWin->DecModalCount();
        xTopWin = Application::GetNextTopLevelWindow(xTopWin);
    }
}

bool SfxHelp::Start_Impl(const OUString& rURL, const vcl::Window* pWindow, const OUString& rKeyword)
{
    OUStringBuffer aHelpRootURL("vnd.sun.star.help://");
@@ -869,7 +844,9 @@ bool SfxHelp::Start_Impl(const OUString& rURL, const vcl::Window* pWindow, const

        if ( !impl_hasHelpInstalled() )
        {
            incBusy(pWindow);
            TopLevelWindowLocker aBusy;

            aBusy.incBusy(pWindow);
            std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(pWindow ? pWindow->GetFrameWeld() : nullptr, "sfx/ui/helpmanual.ui"));
            std::unique_ptr<weld::MessageDialog> xQueryBox(xBuilder->weld_message_dialog("onlinehelpmanual"));

@@ -878,18 +855,19 @@ bool SfxHelp::Start_Impl(const OUString& rURL, const vcl::Window* pWindow, const
            OUString sPrimText = xQueryBox->get_primary_text();
            xQueryBox->set_primary_text(sPrimText.replaceAll("$UILOCALE", sLocaleString));
            short OnlineHelpBox = xQueryBox->run();
            decBusy(pWindow);
            xQueryBox.reset();
            aBusy.decBusy();

            if(OnlineHelpBox == RET_OK)
            if (OnlineHelpBox == RET_OK)
            {
                if ( impl_showOnlineHelp( aHelpURL ) )
                    return true;
                else
                {
                    incBusy(pWindow);
                    aBusy.incBusy(pWindow);
                    NoHelpErrorBox aErrBox(pWindow ? pWindow->GetFrameWeld() : nullptr);
                    aErrBox.run();
                    decBusy(pWindow);
                    aBusy.decBusy();
                    return false;
                }
            }
diff --git a/vcl/source/window/dialog.cxx b/vcl/source/window/dialog.cxx
index 285b7c2..a1d8cf1 100644
--- a/vcl/source/window/dialog.cxx
+++ b/vcl/source/window/dialog.cxx
@@ -43,6 +43,7 @@
#include <vcl/layout.hxx>
#include <vcl/svapp.hxx>
#include <vcl/event.hxx>
#include <vcl/waitobj.hxx>
#include <vcl/wrkwin.hxx>
#include <vcl/button.hxx>
#include <vcl/mnemonic.hxx>
@@ -1532,4 +1533,28 @@ void ModelessDialog::Activate()
    Dialog::Activate();
}

void TopLevelWindowLocker::incBusy(const vcl::Window* pIgnore)
{
    // lock any toplevel windows from being closed until busy is over
    std::vector<VclPtr<vcl::Window>> aTopLevels;
    vcl::Window *pTopWin = Application::GetFirstTopLevelWindow();
    while (pTopWin)
    {
        if (pTopWin != pIgnore)
            aTopLevels.push_back(pTopWin);
        pTopWin = Application::GetNextTopLevelWindow(pTopWin);
    }
    for (auto& a : aTopLevels)
        a->IncModalCount();
    m_aBusyStack.push(aTopLevels);
}

void TopLevelWindowLocker::decBusy()
{
    // unlock locked toplevel windows from being closed now busy is over
    for (auto& a : m_aBusyStack.top())
        a->DecModalCount();
    m_aBusyStack.pop();
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */