include/sfx2/dispatch.hxx        |    1 
 include/sfx2/viewsh.hxx          |    2 
 sfx2/source/control/dispatch.cxx |   94 ++++++++++++++++++++-------------------
 3 files changed, 50 insertions(+), 47 deletions(-)

New commits:
commit e71b6718693c35a15ca512db72366f126b97f512
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Wed Jun 18 13:20:10 2025 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Wed Jun 18 15:13:38 2025 +0200

    Restructure SfxDispatcher::FindServer_ more
    
    A follow-up to commit 51c6879137325c66dd447ff82c35292953d15ae7
    (Restructure SfxDispatcher::FindServer_, 2025-06-17).
    
    Split the code that checks exceptions for global read-only mode
    (defined by xImp->bReadOnly), and LOK view read-only mode (defined
    by GetViewShell()->IsLokReadOnlyView()).
    
    IsCommandAllowedInLokReadOnlyViewMode is removed from SfxDispatcher
    (there is no need to access this static function from outside).
    
    Change-Id: Ib5e3cfec116112ade23b351367ccece289c1f2d8
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/186650
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/include/sfx2/dispatch.hxx b/include/sfx2/dispatch.hxx
index 5bf9a313a904..3af732228a27 100644
--- a/include/sfx2/dispatch.hxx
+++ b/include/sfx2/dispatch.hxx
@@ -89,7 +89,6 @@ friend class SfxHintPoster;
 
 
     bool                FindServer_( sal_uInt16 nId, SfxSlotServer &rServer );
-    static bool         IsCommandAllowedInLokReadOnlyViewMode(const OUString & 
commandName);
     bool                FillState_( const SfxSlotServer &rServer,
                                     SfxItemSet &rState, const SfxSlot 
*pRealSlot );
     void                Execute_( SfxShell &rShell, const SfxSlot &rSlot,
diff --git a/include/sfx2/viewsh.hxx b/include/sfx2/viewsh.hxx
index c49fa11c8902..5f1349fa1b29 100644
--- a/include/sfx2/viewsh.hxx
+++ b/include/sfx2/viewsh.hxx
@@ -248,7 +248,7 @@ public:
     void                        SetLokReadOnlyView(bool readOnlyView) { 
lokReadOnlyView = readOnlyView; };
     bool                        IsLokReadOnlyView() const { return 
lokReadOnlyView; };
     void                        SetAllowChangeComments(bool allow) { 
allowChangeComments = allow; }
-    bool                        IsAllowChangeComments() { return 
allowChangeComments; }
+    bool                        IsAllowChangeComments() const { return 
allowChangeComments; }
 
     // Misc
 
diff --git a/sfx2/source/control/dispatch.cxx b/sfx2/source/control/dispatch.cxx
index cadb3206e6c2..f427dee9d209 100644
--- a/sfx2/source/control/dispatch.cxx
+++ b/sfx2/source/control/dispatch.cxx
@@ -1535,23 +1535,27 @@ SfxSlotFilterState 
SfxDispatcher::IsSlotEnabledByFilter_Impl( sal_uInt16 nSID )
         return bFound ? SfxSlotFilterState::DISABLED : 
SfxSlotFilterState::ENABLED;
 }
 
-bool SfxDispatcher::IsCommandAllowedInLokReadOnlyViewMode (const OUString & 
commandName) {
-    static constexpr OUString allowedList[] = {
-        u".uno:InsertAnnotation"_ustr,
-        u".uno:ReplyComment"_ustr,
-        u".uno:ResolveComment"_ustr,
-        u".uno:ResolveCommentThread"_ustr,
-        u".uno:DeleteComment"_ustr,
-        u".uno:DeleteAnnotation"_ustr,
-        u".uno:EditAnnotation"_ustr,
-        u".uno:PromoteComment"_ustr,
-        u".uno:Save"_ustr,
-    };
-
-    if (std::find(std::begin(allowedList), std::end(allowedList), commandName) 
!= std::end(allowedList))
-        return true;
-    else
-        return false;
+static bool IsCommandAllowedInLokReadOnlyViewMode(std::u16string_view 
commandName,
+                                                  const SfxViewShell& 
viewShell)
+{
+    if (viewShell.IsAllowChangeComments())
+    {
+        static constexpr std::u16string_view allowed[] = {
+            u".uno:InsertAnnotation",
+            u".uno:ReplyComment",
+            u".uno:ResolveComment",
+            u".uno:ResolveCommentThread",
+            u".uno:DeleteComment",
+            u".uno:DeleteAnnotation",
+            u".uno:EditAnnotation",
+            u".uno:PromoteComment",
+            u".uno:Save",
+        };
+
+        if (std::find(std::begin(allowed), std::end(allowed), commandName) != 
std::end(allowed))
+            return true;
+    }
+    return false;
 }
 
 /** This helper method searches for the <Slot-Server> which currently serves
@@ -1623,17 +1627,10 @@ bool SfxDispatcher::FindServer_(sal_uInt16 nSlot, 
SfxSlotServer& rServer)
     }
 
     const bool isViewerAppMode = 
officecfg::Office::Common::Misc::ViewerAppMode::get();
-    bool bReadOnly = ( SfxSlotFilterState::ENABLED_READONLY != nSlotEnableMode 
&& xImp->bReadOnly );
-    bool bCheckForCommentCommands = false;
-
-    if (!bReadOnly && comphelper::LibreOfficeKit::isActive() && xImp->pFrame 
&& xImp->pFrame->GetViewShell())
-    {
-        SfxViewShell *pViewSh = xImp->pFrame->GetViewShell();
-        bReadOnly = pViewSh->IsLokReadOnlyView();
-
-        if (bReadOnly && pViewSh->IsAllowChangeComments())
-            bCheckForCommentCommands = true;
-    }
+    const bool bReadOnlyGlobal = SfxSlotFilterState::ENABLED_READONLY != 
nSlotEnableMode && xImp->bReadOnly;
+    const bool bReadOnlyLokView = !bReadOnlyGlobal && 
comphelper::LibreOfficeKit::isActive()
+                                  && xImp->pFrame && 
xImp->pFrame->GetViewShell()
+                                  && 
xImp->pFrame->GetViewShell()->IsLokReadOnlyView();
 
     const bool bIsInPlace = xImp->pFrame && 
xImp->pFrame->GetObjectShell()->IsInPlaceActive();
     // Shell belongs to Server?
@@ -1679,29 +1676,36 @@ bool SfxDispatcher::FindServer_(sal_uInt16 nSlot, 
SfxSlotServer& rServer)
         if (!(pSlot->nFlags & SfxSlotMode::VIEWERAPP) && isViewerAppMode)
             return false;
 
-        if (!(pSlot->nFlags & SfxSlotMode::READONLYDOC) && bReadOnly)
+        // The slot is not read-only
+        if (!(pSlot->nFlags & SfxSlotMode::READONLYDOC))
         {
-            bool bAllowThis = false;
-
-            // This check can be true only if Lokit is active and view is 
readonly.
-            if (bCheckForCommentCommands)
-                bAllowThis = 
IsCommandAllowedInLokReadOnlyViewMode(pSlot->GetCommand());
-
-            // Enable insert new annotation in Writer in read-only mode
-            if (!bAllowThis && getenv("EDIT_COMMENT_IN_READONLY_MODE") != 
nullptr)
+            // 1. The global context is read-only
+            if (bReadOnlyGlobal)
             {
-                OUString sCommand = pSlot->GetCommand();
-                if (sCommand == u".uno:InsertAnnotation"_ustr
-                    || ((sCommand == u".uno:FontDialog"_ustr
-                         || sCommand == u".uno:ParagraphDialog"_ustr)
-                        && pIFace->GetClassName() == "SwAnnotationShell"_ostr))
+                bool bAllowThis = false;
+                // Enable insert new annotation in Writer in read-only mode
+                if (getenv("EDIT_COMMENT_IN_READONLY_MODE") != nullptr)
                 {
-                    bAllowThis = true;
+                    OUString sCommand = pSlot->GetCommand();
+                    if (sCommand == u".uno:InsertAnnotation"_ustr
+                        || ((sCommand == u".uno:FontDialog"_ustr
+                             || sCommand == u".uno:ParagraphDialog"_ustr)
+                            && pIFace->GetClassName() == 
"SwAnnotationShell"_ostr))
+                    {
+                        bAllowThis = true;
+                    }
                 }
+                if (!bAllowThis)
+                    return false;
             }
 
-            if (!bAllowThis)
-                return false;
+            // 2. LOK view context is read-only
+            if (bReadOnlyLokView)
+            {
+                if (!IsCommandAllowedInLokReadOnlyViewMode(pSlot->GetCommand(),
+                                                           
*xImp->pFrame->GetViewShell()))
+                    return false;
+            }
         }
 
         rServer.SetSlot(pSlot);

Reply via email to