cool#7492 sfx2 lok: set language/locale on async sidebar update

Create two Calc views, set the first view language to English, second
view language to German. Type in the English view, double-click on a
chart in the German view. The sidebar in the German view will have
English strings in it. This doesn't happen if there is no typing right
after the chart activation in the English view.

What happens is that the sidebar update is async, and
sfx2::sidebar::SidebarController::notifyContextChangeEvent() gets
called, which registers an aync event when it calls
AsynchronousCall::RequestCall(). Then later this job gets scheduled, but
possibly by that time the active view is the English one, leading to
English strings when chart::ColumnChartDialogController::getName() calls
SchResId(), which works from the language of the current view.

Fix the problem similar to what commit
fb7b0b944741e4efae8d92a6e305036aff906c7a (cool#7492 sfx2 lok: just set
language/locale on async binding update, 2024-01-09), did: set the
language/locale from the current view before executing the async job and
restore the old value once we're done.

Extract the now duplicated code to a new SfxLokLanguageGuard, so in case
more places have a problem with incorrect l10n, then it's meant to be a
one-liner to fix further places.

Change-Id: I52724a24d93fb753175a3b9b99bc33178519d981
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/161920
Reviewed-by: Miklos Vajna <[email protected]>
Tested-by: Jenkins
diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx b/desktop/qa/desktop_lib/test_desktop_lib.cxx
index cc43608..d9809cf 100644
--- a/desktop/qa/desktop_lib/test_desktop_lib.cxx
+++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx
@@ -68,6 +68,7 @@
#include <vcl/filter/PDFiumLibrary.hxx>
#include <svtools/colorcfg.hxx>
#include <sal/types.h>
#include <test/lokcallback.hxx>

#if USE_TLS_NSS
#include <nss.h>
@@ -3266,33 +3267,11 @@ void DesktopLOKTest::testMultiDocuments()
    }
}

namespace
{
    SfxChildWindow* lcl_initializeSidebar()
    {
        // in init.cxx we do setupSidebar which creates the controller, do it here

        SfxViewShell* pViewShell = SfxViewShell::Current();
        CPPUNIT_ASSERT(pViewShell);

        SfxViewFrame& rViewFrame = pViewShell->GetViewFrame();
        SfxChildWindow* pSideBar = rViewFrame.GetChildWindow(SID_SIDEBAR);
        CPPUNIT_ASSERT(pSideBar);

        auto pDockingWin = dynamic_cast<sfx2::sidebar::SidebarDockingWindow *>(pSideBar->GetWindow());
        CPPUNIT_ASSERT(pDockingWin);

        pDockingWin->GetOrCreateSidebarController(); // just to create the controller

        return pSideBar;
    }
};

void DesktopLOKTest::testControlState()
{
    LibLODocument_Impl* pDocument = loadDoc("search.ods");
    pDocument->pClass->postUnoCommand(pDocument, ".uno:StarShapes", nullptr, false);
    lcl_initializeSidebar();
    TestLokCallbackWrapper::InitializeSidebar();
    Scheduler::ProcessEventsToIdle();

    boost::property_tree::ptree aState;
@@ -3306,7 +3285,7 @@ void DesktopLOKTest::testMetricField()
{
    LibLODocument_Impl* pDocument = loadDoc("search.ods");
    pDocument->pClass->postUnoCommand(pDocument, ".uno:StarShapes", nullptr, false);
    SfxChildWindow* pSideBar = lcl_initializeSidebar();
    SfxChildWindow* pSideBar = TestLokCallbackWrapper::InitializeSidebar();
    Scheduler::ProcessEventsToIdle();

    vcl::Window* pWin = pSideBar->GetWindow();
diff --git a/include/sfx2/lokhelper.hxx b/include/sfx2/lokhelper.hxx
index 338dda1..dc9558a 100644
--- a/include/sfx2/lokhelper.hxx
+++ b/include/sfx2/lokhelper.hxx
@@ -250,6 +250,18 @@ void SfxLokHelper::forEachOtherView(ViewShellType* pThisViewShell, FunctionType 
    }
}

/// If LOK is active, switch to the language/locale of the provided shell and back on delete.
class SfxLokLanguageGuard
{
    bool m_bSetLanguage;
    const SfxViewShell* m_pOldShell;
    const SfxViewShell* m_pNewShell;

public:
    SfxLokLanguageGuard(SfxViewShell* pNewShell);
    ~SfxLokLanguageGuard();
};

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/include/sfx2/sidebar/AsynchronousCall.hxx b/include/sfx2/sidebar/AsynchronousCall.hxx
index c7e5224..9c9b2ee 100644
--- a/include/sfx2/sidebar/AsynchronousCall.hxx
+++ b/include/sfx2/sidebar/AsynchronousCall.hxx
@@ -23,6 +23,7 @@
#include <functional>

struct ImplSVEvent;
class SfxViewFrame;

namespace sfx2::sidebar
{
@@ -33,7 +34,7 @@ class AsynchronousCall
public:
    typedef ::std::function<void()> Action;

    AsynchronousCall(Action aAction);
    AsynchronousCall(const SfxViewFrame* pViewFrame, Action aAction);
    ~AsynchronousCall();

    void RequestCall();
@@ -43,6 +44,7 @@ public:
private:
    Action maAction;
    ImplSVEvent* mnCallId;
    const SfxViewFrame* mpViewFrame;

    DECL_LINK(HandleUserCall, void*, void);
};
diff --git a/include/test/lokcallback.hxx b/include/test/lokcallback.hxx
index bba7a39..7edf743 100644
--- a/include/test/lokcallback.hxx
+++ b/include/test/lokcallback.hxx
@@ -17,6 +17,8 @@

#include <vector>

class SfxChildWindow;

/**
A helper to convert SfxLokCallbackInterface to a LIbreOfficeKitCallback for tests.

@@ -44,6 +46,8 @@ public:

    virtual void Invoke() override;

    static SfxChildWindow* InitializeSidebar();

private:
    void callCallback(int nType, const char* pPayload, int nViewId);
    void startTimer();
diff --git a/sc/qa/unit/tiledrendering/data/chart.ods b/sc/qa/unit/tiledrendering/data/chart.ods
new file mode 100644
index 0000000..dcd1194
--- /dev/null
+++ b/sc/qa/unit/tiledrendering/data/chart.ods
Binary files differ
diff --git a/sc/qa/unit/tiledrendering/tiledrendering.cxx b/sc/qa/unit/tiledrendering/tiledrendering.cxx
index aa142a0..0475a03 100644
--- a/sc/qa/unit/tiledrendering/tiledrendering.cxx
+++ b/sc/qa/unit/tiledrendering/tiledrendering.cxx
@@ -3245,6 +3245,32 @@ CPPUNIT_TEST_FIXTURE(ScTiledRenderingTest, testStatusBarLocale)
    CPPUNIT_ASSERT_EQUAL(std::string("de-DE"), aLocale);
}

CPPUNIT_TEST_FIXTURE(ScTiledRenderingTest, testSidebarLocale)
{
    ScModelObj* pModelObj = createDoc("chart.ods");
    int nView1 = SfxLokHelper::getView();
    ViewCallback aView1;
    SfxViewShell* pView1 = SfxViewShell::Current();
    pView1->SetLOKLocale("en-US");
    SfxLokHelper::createView();
    ViewCallback aView2;
    SfxViewShell* pView2 = SfxViewShell::Current();
    pView2->SetLOKLocale("de-DE");
    TestLokCallbackWrapper::InitializeSidebar();
    Scheduler::ProcessEventsToIdle();
    aView2.m_aStateChanges.clear();

    pModelObj->postMouseEvent(LOK_MOUSEEVENT_MOUSEBUTTONDOWN, /*x=*/1,/*y=*/1,/*count=*/2, /*buttons=*/1, /*modifier=*/0);
    pModelObj->postMouseEvent(LOK_MOUSEEVENT_MOUSEBUTTONUP, /*x=*/1, /*y=*/1, /*count=*/2, /*buttons=*/1, /*modifier=*/0);
    SfxLokHelper::setView(nView1);
    Scheduler::ProcessEventsToIdle();

    auto it = aView2.m_aStateChanges.find(".uno:Sidebar");
    CPPUNIT_ASSERT(it != aView2.m_aStateChanges.end());
    std::string aLocale = it->second.get<std::string>("locale");
    CPPUNIT_ASSERT_EQUAL(std::string("de-DE"), aLocale);
}

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sfx2/source/control/bindings.cxx b/sfx2/source/control/bindings.cxx
index 8eb8f2f..5a318d1 100644
--- a/sfx2/source/control/bindings.cxx
+++ b/sfx2/source/control/bindings.cxx
@@ -1214,30 +1214,10 @@ void SfxBindings::UpdateControllers_Impl

IMPL_LINK( SfxBindings, NextJob, Timer *, pTimer, void )
{
    bool bSetView = false;
    SfxViewShell* pOldShell = nullptr;
    if (comphelper::LibreOfficeKit::isActive() && pDispatcher)
    {
        SfxViewFrame* pFrame = pDispatcher->GetFrame();
        SfxViewShell* pNewShell = pFrame ? pFrame->GetViewShell() : nullptr;
        pOldShell = SfxViewShell::Current();
        if (pNewShell && pNewShell != pOldShell)
        {
            // The current view ID is not the one that belongs to this frame, update
            // language/locale.
            comphelper::LibreOfficeKit::setLanguageTag(pNewShell->GetLOKLanguageTag());
            comphelper::LibreOfficeKit::setLocale(pNewShell->GetLOKLocale());
            bSetView = true;
        }
    }
    SfxViewFrame* pFrame = pDispatcher ? pDispatcher->GetFrame() : nullptr;
    SfxLokLanguageGuard aGuard(pFrame ? pFrame->GetViewShell() : nullptr);

    NextJob_Impl(pTimer);

    if (bSetView && pOldShell)
    {
        comphelper::LibreOfficeKit::setLanguageTag(pOldShell->GetLOKLanguageTag());
        comphelper::LibreOfficeKit::setLocale(pOldShell->GetLOKLocale());
    }
}

bool SfxBindings::NextJob_Impl(Timer const * pTimer)
diff --git a/sfx2/source/sidebar/AsynchronousCall.cxx b/sfx2/source/sidebar/AsynchronousCall.cxx
index fdb76d63..3377c1b 100644
--- a/sfx2/source/sidebar/AsynchronousCall.cxx
+++ b/sfx2/source/sidebar/AsynchronousCall.cxx
@@ -20,12 +20,15 @@
#include <sfx2/sidebar/AsynchronousCall.hxx>
#include <utility>
#include <vcl/svapp.hxx>
#include <sfx2/viewfrm.hxx>
#include <sfx2/lokhelper.hxx>

namespace sfx2::sidebar {

AsynchronousCall::AsynchronousCall (Action aAction)
AsynchronousCall::AsynchronousCall (const SfxViewFrame* pViewFrame, Action aAction)
    : maAction(std::move(aAction)),
      mnCallId(nullptr)
      mnCallId(nullptr),
      mpViewFrame(pViewFrame)
{
}

@@ -55,6 +58,7 @@ void AsynchronousCall::CancelRequest()
void AsynchronousCall::Sync()
{
    if (mnCallId != nullptr) {
        SfxLokLanguageGuard aGuard(mpViewFrame ? mpViewFrame->GetViewShell() : nullptr);
        maAction();
        CancelRequest();
    }
@@ -64,7 +68,10 @@ IMPL_LINK_NOARG(AsynchronousCall, HandleUserCall, void*, void )
{
    mnCallId = nullptr;
    if (maAction)
    {
        SfxLokLanguageGuard aGuard(mpViewFrame ? mpViewFrame->GetViewShell() : nullptr);
        maAction();
    }
}

} // end of namespace sfx2::sidebar
diff --git a/sfx2/source/sidebar/SidebarController.cxx b/sfx2/source/sidebar/SidebarController.cxx
index 3e76cb2..ba627e4 100644
--- a/sfx2/source/sidebar/SidebarController.cxx
+++ b/sfx2/source/sidebar/SidebarController.cxx
@@ -17,6 +17,9 @@
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
#include <sfx2/sidebar/SidebarController.hxx>

#include <boost/property_tree/json_parser.hpp>

#include <sfx2/sidebar/Deck.hxx>
#include <sidebar/DeckDescriptor.hxx>
#include <sidebar/DeckTitleBar.hxx>
@@ -125,8 +128,8 @@ SidebarController::SidebarController (
      mnRequestedForceFlags(SwitchFlag_NoForce),
      mbMinimumSidebarWidth(officecfg::Office::UI::Sidebar::General::MinimumWidth::get()),
      msCurrentDeckId(gsDefaultDeckId),
      maPropertyChangeForwarder([this](){ return this->BroadcastPropertyChange(); }),
      maContextChangeUpdate([this](){ return this->UpdateConfigurations(); }),
      maPropertyChangeForwarder(mpViewFrame, [this](){ return this->BroadcastPropertyChange(); }),
      maContextChangeUpdate(mpViewFrame, [this](){ return this->UpdateConfigurations(); }),
      mbFloatingDeckClosed(!pParentWindow->IsFloatingMode()),
      mnSavedSidebarWidth(pParentWindow->GetSizePixel().Width()),
      maFocusManager([this](const Panel& rPanel){ return this->ShowPanel(rPanel); }),
@@ -805,18 +808,35 @@ void SidebarController::SwitchToDeck (
    {
        if (const SfxViewShell* pViewShell = mpViewFrame->GetViewShell())
        {
            boost::property_tree::ptree aTree;
            aTree.put("locale", comphelper::LibreOfficeKit::getLocale().getBcp47());
            bool bStateChanged = false;
            if (msCurrentDeckId != rDeckDescriptor.msId)
            {
                const std::string hide = UnoNameFromDeckId(msCurrentDeckId, GetCurrentContext());
                if (!hide.empty())
                    pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_STATE_CHANGED,
                                                           OString(hide + "=false"));
                {
                    aTree.put("commandName", hide);
                    aTree.put("state", "false");
                    bStateChanged = true;
                }
            }

            const std::string show = UnoNameFromDeckId(rDeckDescriptor.msId, GetCurrentContext());
            if (!show.empty())
            {
                aTree.put("commandName", show);
                aTree.put("state", "true");
                bStateChanged = true;
            }

            if (bStateChanged)
            {
                std::stringstream aStream;
                boost::property_tree::write_json(aStream, aTree);
                pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_STATE_CHANGED,
                                                       OString(show + "=true"));
                                                       OString(aStream.str()));
            }
        }
    }

diff --git a/sfx2/source/view/lokhelper.cxx b/sfx2/source/view/lokhelper.cxx
index 7cfe514..647c580 100644
--- a/sfx2/source/view/lokhelper.cxx
+++ b/sfx2/source/view/lokhelper.cxx
@@ -1094,4 +1094,33 @@ void SfxLokHelper::sendNetworkAccessError()
    }
}

SfxLokLanguageGuard::SfxLokLanguageGuard(SfxViewShell* pNewShell)
    : m_bSetLanguage(false)
    , m_pOldShell(nullptr)
    , m_pNewShell(pNewShell)
{
    m_pOldShell = SfxViewShell::Current();
    if (!comphelper::LibreOfficeKit::isActive() || !m_pNewShell || m_pNewShell == m_pOldShell)
    {
        return;
    }

    // The current view ID is not the one that belongs to this frame, update
    // language/locale.
    comphelper::LibreOfficeKit::setLanguageTag(m_pNewShell->GetLOKLanguageTag());
    comphelper::LibreOfficeKit::setLocale(m_pNewShell->GetLOKLocale());
    m_bSetLanguage = true;
}

SfxLokLanguageGuard::~SfxLokLanguageGuard()
{
    if (!m_bSetLanguage || !m_pOldShell)
    {
        return;
    }

    comphelper::LibreOfficeKit::setLanguageTag(m_pOldShell->GetLOKLanguageTag());
    comphelper::LibreOfficeKit::setLocale(m_pOldShell->GetLOKLocale());
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/test/source/lokcallback.cxx b/test/source/lokcallback.cxx
index 767448c..a979b8b 100644
--- a/test/source/lokcallback.cxx
+++ b/test/source/lokcallback.cxx
@@ -14,6 +14,10 @@
#include <tools/gen.hxx>
#include <comphelper/lok.hxx>
#include <sfx2/viewsh.hxx>
#include <sfx2/childwin.hxx>
#include <sfx2/viewfrm.hxx>
#include <sfx2/sfxsids.hrc>
#include <sfx2/sidebar/SidebarDockingWindow.hxx>

TestLokCallbackWrapper::TestLokCallbackWrapper(LibreOfficeKitCallback callback, void* data)
    : Idle("TestLokCallbackWrapper flush timer")
@@ -185,4 +189,23 @@ void TestLokCallbackWrapper::Invoke()
    flushLOKData();
}

SfxChildWindow* TestLokCallbackWrapper::InitializeSidebar()
{
    // in init.cxx we do setupSidebar which creates the controller, do it here

    SfxViewShell* pViewShell = SfxViewShell::Current();
    assert(pViewShell);

    SfxViewFrame& rViewFrame = pViewShell->GetViewFrame();
    SfxChildWindow* pSideBar = rViewFrame.GetChildWindow(SID_SIDEBAR);
    assert(pSideBar);

    auto pDockingWin = dynamic_cast<sfx2::sidebar::SidebarDockingWindow*>(pSideBar->GetWindow());
    assert(pDockingWin);

    pDockingWin->GetOrCreateSidebarController(); // just to create the controller

    return pSideBar;
}

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