desktop/qa/desktop_lib/test_desktop_lib.cxx  |   29 ++------------------------
 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 |   28 +++++++++++++++++++++++++
 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                  |   25 ++++++++++++++++++++++
 11 files changed, 140 insertions(+), 56 deletions(-)

New commits:
commit 59785bd8605f59860b24c40f04da04344c8df2c3
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Fri Jan 12 08:35:23 2024 +0100
Commit:     Caolán McNamara <caolan.mcnam...@collabora.com>
CommitDate: Fri Jan 12 12:12:50 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.
    
    (cherry picked from commit aaf6ce108e91b1504befe19afcee471e3316ae7a)
    
    Conflicts:
            desktop/qa/desktop_lib/test_desktop_lib.cxx
            sc/qa/unit/tiledrendering/tiledrendering.cxx
            sfx2/source/sidebar/SidebarController.cxx
    
    Change-Id: I52724a24d93fb753175a3b9b99bc33178519d981
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/161952
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com>

diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx 
b/desktop/qa/desktop_lib/test_desktop_lib.cxx
index ea572c8e8a41..1d035a1c4b8c 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>
@@ -3296,35 +3297,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* pViewFrame = pViewShell->GetViewFrame();
-        CPPUNIT_ASSERT(pViewFrame);
-
-        SfxChildWindow* pSideBar = pViewFrame->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;
@@ -3338,7 +3315,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 e593002d0498..288cfc0e075c 100644
--- a/include/sfx2/lokhelper.hxx
+++ b/include/sfx2/lokhelper.hxx
@@ -252,6 +252,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 1908838a6cdf..0f27bba769d8 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 d49fa95c62c5..fbd5beba3b71 100644
--- a/sc/qa/unit/tiledrendering/tiledrendering.cxx
+++ b/sc/qa/unit/tiledrendering/tiledrendering.cxx
@@ -163,6 +163,7 @@ public:
     void testInvalidateForSplitPanes();
     void testStatusBarLocale();
     void testLongFirstColumnMouseClick();
+    void testSidebarLocale();
 
     CPPUNIT_TEST_SUITE(ScTiledRenderingTest);
     CPPUNIT_TEST(testRowColumnHeaders);
@@ -236,6 +237,7 @@ public:
     CPPUNIT_TEST(testInvalidateForSplitPanes);
     CPPUNIT_TEST(testStatusBarLocale);
     CPPUNIT_TEST(testLongFirstColumnMouseClick);
+    CPPUNIT_TEST(testSidebarLocale);
     CPPUNIT_TEST_SUITE_END();
 
 private:
@@ -3628,6 +3630,32 @@ void 
ScTiledRenderingTest::testLongFirstColumnMouseClick()
     CPPUNIT_ASSERT_EQUAL(SCROW(0), ScDocShell::GetViewData()->GetCurY());
 }
 
+void 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_TEST_SUITE_REGISTRATION(ScTiledRenderingTest);
diff --git a/sfx2/source/control/bindings.cxx b/sfx2/source/control/bindings.cxx
index 81750cb1fe4b..0935f1d810c9 100644
--- a/sfx2/source/control/bindings.cxx
+++ b/sfx2/source/control/bindings.cxx
@@ -1219,30 +1219,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 459bf9fb2ff8..611a84b92ab9 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); }),
@@ -800,18 +803,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,
-                                                           (hide + 
"=false").c_str());
+                {
+                    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,
-                                                       (show + 
"=true").c_str());
+                                                       aStream.str().c_str());
+            }
         }
     }
 
diff --git a/sfx2/source/view/lokhelper.cxx b/sfx2/source/view/lokhelper.cxx
index 0deb4d6a65a2..0b83d1b16dc8 100644
--- a/sfx2/source/view/lokhelper.cxx
+++ b/sfx2/source/view/lokhelper.cxx
@@ -1109,4 +1109,33 @@ void 
SfxLokHelper::sendNetworkAccessError(std::string_view rAction)
     }
 }
 
+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 5af844f089ad..ef6ad4f37fcb 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")
@@ -186,4 +190,25 @@ 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* pViewFrame = pViewShell->GetViewFrame();
+    assert(pViewFrame);
+
+    SfxChildWindow* pSideBar = pViewFrame->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