From baabbe09869182e2f5998906ca043cddcc01533c Mon Sep 17 00:00:00 2001 From: Anshu Date: Thu, 31 Dec 2020 23:13:07 +0530 Subject: 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 --- sc/source/ui/docshell/docfunc.cxx | 66 +++++++++++++++++++++------------------ sc/source/ui/inc/docfunc.hxx | 4 +++ 2 files changed, 40 insertions(+), 30 deletions(-) (limited to 'sc') diff --git a/sc/source/ui/docshell/docfunc.cxx b/sc/source/ui/docshell/docfunc.cxx index 3fbab69b9229..2a174b88d1fb 100644 --- a/sc/source/ui/docshell/docfunc.cxx +++ b/sc/source/ui/docshell/docfunc.cxx @@ -3965,38 +3965,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 p; + if (!rProtect.isProtected() && rDoc.IsUndoEnabled()) + { + // In case of unprotecting, use a copy of passed ScTableProtection object for undo + p = std::make_unique(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(*pProtect); + } + rDocShell.GetUndoManager()->AddUndoAction( + std::make_unique(&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 p(new ScDocProtection(*pProtect)); - p->setProtected(true); // just in case ... - rDocShell.GetUndoManager()->AddUndoAction( - std::make_unique(&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 pNewProtection(pOldProtection ? new ScTableProtection(*pOldProtection) : new ScTableProtection()); pNewProtection->setProtected(true); pNewProtection->setPassword(rPassword); @@ -4018,9 +4031,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 pProtectCopy(new ScDocProtection(*pDocProtect)); - if (!pDocProtect->verifyPassword(rPassword)) { if (!bApi) @@ -4033,14 +4043,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(&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 -- cgit