diff options
author | Anshu <anshukhare50@gmail.com> | 2020-12-31 23:13:07 +0530 |
---|---|---|
committer | Andras Timar <andras.timar@collabora.com> | 2021-04-14 12:01:21 +0200 |
commit | baabbe09869182e2f5998906ca043cddcc01533c (patch) | |
tree | e7a15070199b6c4f5a22d582e8b6efbccf629fef /sc | |
parent | eac9eff48292df7a172c57e5c4e7bd9224d201a4 (diff) |
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@collabora.com>
Diffstat (limited to 'sc')
-rw-r--r-- | sc/source/ui/docshell/docfunc.cxx | 66 | ||||
-rw-r--r-- | sc/source/ui/inc/docfunc.hxx | 4 |
2 files changed, 40 insertions, 30 deletions
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<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); @@ -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<ScDocProtection> 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<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 |