desktop/qa/desktop_lib/test_desktop_lib.cxx  |   27 ++----------------------
 include/sfx2/lokhelper.hxx                   |   12 ++++++++++
 include/sfx2/sidebar/AsynchronousCall.hxx    |    4 ++-
 include/test/lokcallback.hxx                 |    4 +++
 sc/qa/unit/tiledrendering/data/chart.ods     |binary
 sc/qa/unit/tiledrendering/tiledrendering.cxx |   26 +++++++++++++++++++++++
 sfx2/source/control/bindings.cxx             |   24 +--------------------
 sfx2/source/sidebar/AsynchronousCall.cxx     |   11 ++++++++-
 sfx2/source/sidebar/SidebarController.cxx    |   30 ++++++++++++++++++++++-----
 sfx2/source/view/lokhelper.cxx               |   29 ++++++++++++++++++++++++++
 test/source/lokcallback.cxx                  |   23 ++++++++++++++++++++
 11 files changed, 136 insertions(+), 54 deletions(-)

New commits:
commit aaf6ce108e91b1504befe19afcee471e3316ae7a
Author:     Miklos Vajna <[email protected]>
AuthorDate: Thu Jan 11 10:59:05 2024 +0100
Commit:     Miklos Vajna <[email protected]>
CommitDate: Thu Jan 11 12:06:52 2024 +0100

    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 cc4360807717..d9809cf56ce3 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 338dda192b8c..dc9558a31f0f 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 c7e522401d5a..9c9b2ee32909 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 bba7a392ae2d..7edf74311fcf 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 000000000000..dcd1194811d7
Binary files /dev/null and b/sc/qa/unit/tiledrendering/data/chart.ods differ
diff --git a/sc/qa/unit/tiledrendering/tiledrendering.cxx 
b/sc/qa/unit/tiledrendering/tiledrendering.cxx
index aa142a0989c0..0475a034cd8b 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 8eb8f2f78689..5a318d11b77a 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 fdb76d63d1ef..3377c1bb31c2 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 3e76cb2b14af..ba627e4d586d 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 7cfe51426bce..647c580610f8 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 767448c771ca..a979b8b50582 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: */

Reply via email to