[Libreoffice-commits] core.git: sc/source

Anshu (via logerrit) logerrit at kemper.freedesktop.org
Tue Jan 5 16:12:52 UTC 2021


 sc/source/ui/docshell/docfunc.cxx |   66 ++++++++++++++++++++------------------
 sc/source/ui/inc/docfunc.hxx      |    4 ++
 2 files changed, 40 insertions(+), 30 deletions(-)

New commits:
commit 5c97d7f490f82152ff42bf61f5a4b270f0141ba2
Author:     Anshu <anshukhare50 at gmail.com>
AuthorDate: Thu Dec 31 23:13:07 2020 +0530
Commit:     Mike Kaganski <mike.kaganski at collabora.com>
CommitDate: Tue Jan 5 17:12:09 2021 +0100

    tdf#139339 : Unify protect/unprotect cases for document protection
    
    Change-Id: I4bd3851d50a6215fef1f3ab722102adf6f44f10b
    
    Restored accidently missed lines for document protection in
    
    https: //gerrit.libreoffice.org/c/core/+/107041
    Change-Id: I4bd3851d50a6215fef1f3ab722102adf6f44f10b
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/108541
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kaganski at collabora.com>

diff --git a/sc/source/ui/docshell/docfunc.cxx b/sc/source/ui/docshell/docfunc.cxx
index 18171bd5c44b..e3f914620b35 100644
--- a/sc/source/ui/docshell/docfunc.cxx
+++ b/sc/source/ui/docshell/docfunc.cxx
@@ -3964,38 +3964,51 @@ void ScDocFunc::ProtectSheet( SCTAB nTab, const ScTableProtection& rProtect )
     aModificator.SetDocumentModified();
 }
 
-bool ScDocFunc::Protect( SCTAB nTab, const OUString& rPassword )
+void ScDocFunc::ProtectDocument(const ScDocProtection& rProtect)
 {
     ScDocument& rDoc = rDocShell.GetDocument();
+
+    std::unique_ptr<ScDocProtection> p;
+    if (!rProtect.isProtected() && rDoc.IsUndoEnabled())
+    {
+        // In case of unprotecting, use a copy of passed ScTableProtection object for undo
+        p = std::make_unique<ScDocProtection>(rProtect);
+    }
+    rDoc.SetDocProtection(&rProtect);
+    if (rDoc.IsUndoEnabled())
+    {
+        if (!p)
+        {
+            // For protection case, use a copy of resulting ScTableProtection for undo
+            ScDocProtection* pProtect = rDoc.GetDocProtection();
+            p = std::make_unique<ScDocProtection>(*pProtect);
+        }
+        rDocShell.GetUndoManager()->AddUndoAction(
+            std::make_unique<ScUndoDocProtect>(&rDocShell, std::move(p)));
+        // ownership of unique_ptr now transferred to ScUndoTabProtect.
+    }
+
+    rDocShell.PostPaintGridAll();
+    ScDocShellModificator aModificator(rDocShell);
+    aModificator.SetDocumentModified();
+}
+
+bool ScDocFunc::Protect( SCTAB nTab, const OUString& rPassword )
+{
     if (nTab == TABLEID_DOC)
     {
         // document protection
         ScDocProtection aProtection;
         aProtection.setProtected(true);
         aProtection.setPassword(rPassword);
-        rDoc.SetDocProtection(&aProtection);
-        if (rDoc.IsUndoEnabled())
-        {
-            ScDocProtection* pProtect = rDoc.GetDocProtection();
-            OSL_ENSURE(pProtect, "ScDocFunc::Unprotect: ScDocProtection pointer is NULL!");
-            if (pProtect)
-            {
-                ::std::unique_ptr<ScDocProtection> p(new ScDocProtection(*pProtect));
-                p->setProtected(true); // just in case ...
-                rDocShell.GetUndoManager()->AddUndoAction(
-                    std::make_unique<ScUndoDocProtect>(&rDocShell, std::move(p)) );
-                // ownership of unique_ptr is transferred to ScUndoDocProtect.
-            }
-        }
-        rDocShell.PostPaintGridAll();
-        ScDocShellModificator aModificator(rDocShell);
-        aModificator.SetDocumentModified();
+        ProtectDocument(aProtection);
+
     }
     else
     {
         // sheet protection
 
-        const ScTableProtection* pOldProtection = rDoc.GetTabProtection(nTab);
+        const ScTableProtection* pOldProtection = rDocShell.GetDocument().GetTabProtection(nTab);
         ::std::unique_ptr<ScTableProtection> pNewProtection(pOldProtection ? new ScTableProtection(*pOldProtection) : new ScTableProtection());
         pNewProtection->setProtected(true);
         pNewProtection->setPassword(rPassword);
@@ -4017,9 +4030,6 @@ bool ScDocFunc::Unprotect( SCTAB nTab, const OUString& rPassword, bool bApi )
             // already unprotected (should not happen)!
             return true;
 
-        // save the protection state before unprotect (for undo).
-        ::std::unique_ptr<ScDocProtection> pProtectCopy(new ScDocProtection(*pDocProtect));
-
         if (!pDocProtect->verifyPassword(rPassword))
         {
             if (!bApi)
@@ -4032,14 +4042,10 @@ bool ScDocFunc::Unprotect( SCTAB nTab, const OUString& rPassword, bool bApi )
             return false;
         }
 
-        rDoc.SetDocProtection(nullptr);
-        if (rDoc.IsUndoEnabled())
-        {
-            pProtectCopy->setProtected(false);
-            rDocShell.GetUndoManager()->AddUndoAction(
-                std::make_unique<ScUndoDocProtect>(&rDocShell, std::move(pProtectCopy)) );
-            // ownership of unique_ptr now transferred to ScUndoDocProtect.
-        }
+        ScDocProtection aNewProtection(*pDocProtect);
+        aNewProtection.setProtected(false);
+        ProtectDocument(aNewProtection);
+
     }
     else
     {
diff --git a/sc/source/ui/inc/docfunc.hxx b/sc/source/ui/inc/docfunc.hxx
index f7b518ef7f35..411abc10496e 100644
--- a/sc/source/ui/inc/docfunc.hxx
+++ b/sc/source/ui/inc/docfunc.hxx
@@ -41,6 +41,7 @@ class ScFormulaCell;
 class ScTokenArray;
 struct ScTabOpParam;
 class ScTableProtection;
+class ScDocProtection;
 struct ScCellMergeOption;
 class ScConditionalFormat;
 class ScConditionalFormatList;
@@ -232,6 +233,9 @@ public:
     void SetConditionalFormatList( ScConditionalFormatList* pList, SCTAB nTab );
 
     void ConvertFormulaToValue( const ScRange& rRange, bool bInteraction );
+
+private:
+    void ProtectDocument(const ScDocProtection& rProtect);
 };
 
 class ScDocFuncDirect : public ScDocFunc


More information about the Libreoffice-commits mailing list