[Libreoffice-commits] core.git: comphelper/source cui/source include/comphelper include/sfx2 sfx2/source

Samuel Mehrbrodt Samuel.Mehrbrodt at cib.de
Fri May 18 14:24:19 UTC 2018


 comphelper/source/misc/storagehelper.cxx       |   13 ++
 cui/source/dialogs/SignSignatureLineDialog.cxx |    2 
 include/comphelper/storagehelper.hxx           |    3 
 include/sfx2/objsh.hxx                         |   14 +--
 sfx2/source/doc/objserv.cxx                    |  116 +++++++++++++------------
 sfx2/source/inc/objshimp.hxx                   |    1 
 6 files changed, 87 insertions(+), 62 deletions(-)

New commits:
commit 0ca1659353cc9847f436280873e1047d2524395e
Author: Samuel Mehrbrodt <Samuel.Mehrbrodt at cib.de>
Date:   Thu May 17 11:10:34 2018 +0200

    Refactor SfxObjectShell sign methods
    
    for signature line signing
    
    Change-Id: Iac4f3a248af53bdbf159e6e8d656e1dcbc3aacbf
    Reviewed-on: https://gerrit.libreoffice.org/54473
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Samuel Mehrbrodt <Samuel.Mehrbrodt at cib.de>

diff --git a/comphelper/source/misc/storagehelper.cxx b/comphelper/source/misc/storagehelper.cxx
index dad5b83511e2..abd06ed048a9 100644
--- a/comphelper/source/misc/storagehelper.cxx
+++ b/comphelper/source/misc/storagehelper.cxx
@@ -692,6 +692,19 @@ uno::Reference< io::XStream > OStorageHelper::GetStreamAtPackageURL(
     return nullptr;
 }
 
+OUString OStorageHelper::GetODFVersionFromStorage(uno::Reference<embed::XStorage> xStorage)
+{
+    OUString aODFVersion;
+    try
+    {
+        uno::Reference<beans::XPropertySet> xPropSet(xStorage, uno::UNO_QUERY_THROW);
+        xPropSet->getPropertyValue("Version") >>= aODFVersion;
+    }
+    catch (uno::Exception&)
+    {
+    }
+    return aODFVersion;
+}
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/cui/source/dialogs/SignSignatureLineDialog.cxx b/cui/source/dialogs/SignSignatureLineDialog.cxx
index f6ae8b10ae82..a9fd2a506110 100644
--- a/cui/source/dialogs/SignSignatureLineDialog.cxx
+++ b/cui/source/dialogs/SignSignatureLineDialog.cxx
@@ -145,7 +145,7 @@ void SignSignatureLineDialog::Apply()
     }
 
     SfxObjectShell* pShell = SfxObjectShell::Current();
-    pShell->SignDocumentContent(m_xSelectedCertifate, m_aSignatureLineId);
+    pShell->SignSignatureLine(m_aSignatureLineId, m_xSelectedCertifate);
 
     bool bSuccess = false; // TODO
 
diff --git a/include/comphelper/storagehelper.hxx b/include/comphelper/storagehelper.hxx
index 778fb1c8ea25..e8fcc0d11cb7 100644
--- a/include/comphelper/storagehelper.hxx
+++ b/include/comphelper/storagehelper.hxx
@@ -193,6 +193,9 @@ public:
         const css::uno::Reference< css::embed::XStorage > &xStorage,
         const OUString& rURL, sal_uInt32 const nOpenMode,
         LifecycleProxy const & rNastiness );
+
+    static OUString
+    GetODFVersionFromStorage(css::uno::Reference<css::embed::XStorage> xStorage);
 };
 
 }
diff --git a/include/sfx2/objsh.hxx b/include/sfx2/objsh.hxx
index 2f99fa010497..97d570c7876c 100644
--- a/include/sfx2/objsh.hxx
+++ b/include/sfx2/objsh.hxx
@@ -207,6 +207,7 @@ private:
                                                   // sal_False := new object
     bool                        bIsInGenerateThumbnail; //optimize thumbnail generate and store procedure to improve odt saving performance, i120030
     bool                        mbAvoidRecentDocs; ///< Avoid adding to the recent documents list, if not necessary.
+    bool m_bAllowModifiedBackAfterSigning;
 
     enum TriState               {undefined, yes, no};
     TriState                    mbContinueImportOnFilterExceptions = undefined; // try to import as much as possible
@@ -353,11 +354,14 @@ public:
             css::uno::Reference<css::text::XTextRange> const& xInsertPosition);
     bool                        ExportTo( SfxMedium &rMedium );
 
-    // xmlsec05, check with SFX team
+    /** Returns to if preparing was succesful, else false. */
+    bool PrepareForSigning();
+    void AfterSigning(bool bSignSuccess, bool bSignScriptingContent);
+    bool HasValidSignatures();
     SignatureState              GetDocumentSignatureState();
     void                        SignDocumentContent();
-    void                        SignDocumentContent(css::uno::Reference<css::security::XCertificate> xCert,
-                                                    const OUString& aSignatureLineId);
+    void SignSignatureLine(const OUString& aSignatureLineId,
+                           const css::uno::Reference<css::security::XCertificate> xCert);
     SignatureState              GetScriptingSignatureState();
     void                        SignScriptingContent();
     DECL_LINK(SignDocumentHandler, Button*, void);
@@ -740,10 +744,6 @@ public:
             const css::uno::Reference< css::security::XDocumentDigitalSignatures >& xSigner
                 = css::uno::Reference< css::security::XDocumentDigitalSignatures >() );
 
-    SAL_DLLPRIVATE void ImplSign(const css::uno::Reference<css::security::XCertificate> xCert
-                                 = css::uno::Reference<css::security::XCertificate>(),
-                                 const OUString& aSignatureLineId = OUString(), bool bScriptingContent = false);
-
     SAL_DLLPRIVATE bool QuerySaveSizeExceededModules_Impl( const css::uno::Reference< css::task::XInteractionHandler >& xHandler );
     SAL_DLLPRIVATE bool QueryAllowExoticFormat_Impl( const css::uno::Reference< css::task::XInteractionHandler >& xHandler,
                                                      const OUString& rURL,
diff --git a/sfx2/source/doc/objserv.cxx b/sfx2/source/doc/objserv.cxx
index 46865cf1107a..009e2a6a1a77 100644
--- a/sfx2/source/doc/objserv.cxx
+++ b/sfx2/source/doc/objserv.cxx
@@ -63,6 +63,7 @@
 #include <svtools/asynclink.hxx>
 #include <comphelper/fileformat.h>
 #include <comphelper/documentconstants.hxx>
+#include <comphelper/storagehelper.hxx>
 #include <tools/link.hxx>
 
 #include <sfx2/app.hxx>
@@ -1361,8 +1362,7 @@ SignatureState SfxObjectShell::ImplGetSignatureState( bool bScriptingContent )
     return *pState;
 }
 
-void SfxObjectShell::ImplSign(Reference<XCertificate> xCert, const OUString& aSignatureLineId,
-                              bool bScriptingContent)
+bool SfxObjectShell::PrepareForSigning()
 {
     // Check if it is stored in OASIS format...
     if  (   GetMedium()
@@ -1378,7 +1378,7 @@ void SfxObjectShell::ImplSign(Reference<XCertificate> xCert, const OUString& aSi
                                                   VclMessageType::Info, VclButtonsType::Ok, SfxResId(STR_INFO_WRONGDOCFORMAT)));
 
         xBox->run();
-        return;
+        return false;
     }
 
     // check whether the document is signed
@@ -1400,19 +1400,7 @@ void SfxObjectShell::ImplSign(Reference<XCertificate> xCert, const OUString& aSi
     SvtSaveOptions::ODFDefaultVersion nVersion = aSaveOpt.GetODFDefaultVersion();
 
     // the document is not new and is not modified
-    OUString aODFVersion;
-    try
-    {
-        // check the ODF version of the document
-        // No idea what relevance this has if the document has not been loaded from ODF (or is not
-        // being saved to ODF)
-        uno::Reference < beans::XPropertySet > xPropSet( GetStorage(), uno::UNO_QUERY_THROW );
-        xPropSet->getPropertyValue("Version") >>= aODFVersion;
-    }
-    catch( uno::Exception& )
-    {}
-
-    bool bNoSig = false;
+    OUString aODFVersion(comphelper::OStorageHelper::GetODFVersionFromStorage(GetStorage()));
 
     if ( IsModified() || !GetMedium() || GetMedium()->GetName().isEmpty()
       || (aODFVersion != ODFVER_012_TEXT && !bHasSign) )
@@ -1445,7 +1433,7 @@ void SfxObjectShell::ImplSign(Reference<XCertificate> xCert, const OUString& aSi
                     std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(nullptr,
                                                               VclMessageType::Info, VclButtonsType::Ok, SfxResId(STR_INFO_WRONGDOCFORMAT)));
                     xBox->run();
-                    return;
+                    return false;
                 }
             }
             else
@@ -1454,7 +1442,7 @@ void SfxObjectShell::ImplSign(Reference<XCertificate> xCert, const OUString& aSi
                 // digital signatures dialog
                 // If we have come here then the user denied to save.
                 if (!bHasSign)
-                    bNoSig = true;
+                    return false;
             }
         }
         else
@@ -1462,60 +1450,59 @@ void SfxObjectShell::ImplSign(Reference<XCertificate> xCert, const OUString& aSi
             std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(nullptr,
                                                       VclMessageType::Warning, VclButtonsType::Ok, SfxResId(STR_XMLSEC_ODF12_EXPECTED)));
             xBox->run();
-            return;
+            return false;
         }
 
         if ( IsModified() || !GetMedium() || GetMedium()->GetName().isEmpty() )
-            return;
+            return false;
     }
 
     // the document is not modified currently, so it can not become modified after signing
-    bool bAllowModifiedBack = false;
+    pImpl->m_bAllowModifiedBackAfterSigning = false;
     if ( IsEnableSetModified() )
     {
         EnableSetModified( false );
-        bAllowModifiedBack = true;
+        pImpl->m_bAllowModifiedBackAfterSigning = true;
     }
 
     // we have to store to the original document, the original medium should be closed for this time
-    if ( !bNoSig
-      && ConnectTmpStorage_Impl( pMedium->GetStorage(), pMedium ) )
+    if ( ConnectTmpStorage_Impl( pMedium->GetStorage(), pMedium ) )
     {
         GetMedium()->CloseAndRelease();
+        return true;
+    }
+    return false;
+}
 
-        bool bHasValidSignatures = pImpl->nDocumentSignatureState == SignatureState::OK
-            || pImpl->nDocumentSignatureState == SignatureState::NOTVALIDATED
-            || pImpl->nDocumentSignatureState == SignatureState::PARTIAL_OK;
-
-        bool bSignSuccess = GetMedium()->SignContents_Impl(
-            xCert, aSignatureLineId, bScriptingContent, aODFVersion, bHasValidSignatures);
+void SfxObjectShell::AfterSigning(bool bSignSuccess, bool bSignScriptingContent)
+{
+    pImpl->m_bSavingForSigning = true;
+    DoSaveCompleted( GetMedium() );
+    pImpl->m_bSavingForSigning = false;
 
-        pImpl->m_bSavingForSigning = true;
-        DoSaveCompleted( GetMedium() );
-        pImpl->m_bSavingForSigning = false;
+    if ( bSignSuccess )
+    {
+        if ( bSignScriptingContent )
+            pImpl->nScriptingSignatureState = SignatureState::UNKNOWN; // Re-Check
 
-        if ( bSignSuccess )
-        {
-            if ( bScriptingContent )
-            {
-                pImpl->nScriptingSignatureState = SignatureState::UNKNOWN;// Re-Check
+        pImpl->nDocumentSignatureState = SignatureState::UNKNOWN; // Re-Check
 
-                // adding of scripting signature removes existing document signature
-                pImpl->nDocumentSignatureState = SignatureState::UNKNOWN;// Re-Check
-            }
-            else
-                pImpl->nDocumentSignatureState = SignatureState::UNKNOWN;// Re-Check
-
-            Invalidate( SID_SIGNATURE );
-            Invalidate( SID_MACRO_SIGNATURE );
-            Broadcast( SfxHint(SfxHintId::TitleChanged) );
-        }
+        Invalidate( SID_SIGNATURE );
+        Invalidate( SID_MACRO_SIGNATURE );
+        Broadcast( SfxHint(SfxHintId::TitleChanged) );
     }
 
-    if ( bAllowModifiedBack )
+    if ( pImpl->m_bAllowModifiedBackAfterSigning )
         EnableSetModified();
 }
 
+bool SfxObjectShell::HasValidSignatures()
+{
+    return pImpl->nDocumentSignatureState == SignatureState::OK
+           || pImpl->nDocumentSignatureState == SignatureState::NOTVALIDATED
+           || pImpl->nDocumentSignatureState == SignatureState::PARTIAL_OK;
+}
+
 SignatureState SfxObjectShell::GetDocumentSignatureState()
 {
     return ImplGetSignatureState();
@@ -1523,13 +1510,27 @@ SignatureState SfxObjectShell::GetDocumentSignatureState()
 
 void SfxObjectShell::SignDocumentContent()
 {
-    ImplSign();
+    if (!PrepareForSigning())
+        return;
+
+    OUString aODFVersion(comphelper::OStorageHelper::GetODFVersionFromStorage(GetStorage()));
+    bool bSignSuccess = GetMedium()->SignContents_Impl(
+        Reference<XCertificate>(), "", false, aODFVersion, HasValidSignatures());
+
+    AfterSigning(bSignSuccess, false);
 }
 
-void SfxObjectShell::SignDocumentContent(const Reference<XCertificate> xCert,
-                                         const OUString& aSignatureLineId)
+void SfxObjectShell::SignSignatureLine(const OUString& aSignatureLineId,
+                                       const Reference<XCertificate> xCert)
 {
-    ImplSign(xCert, aSignatureLineId);
+    if (!PrepareForSigning())
+        return;
+
+    OUString aODFVersion(comphelper::OStorageHelper::GetODFVersionFromStorage(GetStorage()));
+    bool bSignSuccess = GetMedium()->SignContents_Impl(
+        xCert, aSignatureLineId, false, aODFVersion, HasValidSignatures());
+
+    AfterSigning(bSignSuccess, false);
 }
 
 SignatureState SfxObjectShell::GetScriptingSignatureState()
@@ -1539,7 +1540,14 @@ SignatureState SfxObjectShell::GetScriptingSignatureState()
 
 void SfxObjectShell::SignScriptingContent()
 {
-    ImplSign( Reference<XCertificate>(), OUString(), true );
+    if (!PrepareForSigning())
+        return;
+
+    OUString aODFVersion(comphelper::OStorageHelper::GetODFVersionFromStorage(GetStorage()));
+    bool bSignSuccess = GetMedium()->SignContents_Impl(
+        Reference<XCertificate>(), OUString(), true, aODFVersion, HasValidSignatures());
+
+    AfterSigning(bSignSuccess, true);
 }
 
 namespace
diff --git a/sfx2/source/inc/objshimp.hxx b/sfx2/source/inc/objshimp.hxx
index b47a49260d46..67628d5dbdb0 100644
--- a/sfx2/source/inc/objshimp.hxx
+++ b/sfx2/source/inc/objshimp.hxx
@@ -129,6 +129,7 @@ struct SfxObjectShell_Impl : public ::sfx2::IMacroDocumentAccess
     bool                m_bModifyPasswordEntered;
     /// If true, then this is not a real save, just the signatures change.
     bool m_bSavingForSigning;
+    bool m_bAllowModifiedBackAfterSigning;
 
     SfxObjectShell_Impl( SfxObjectShell& _rDocShell );
     virtual ~SfxObjectShell_Impl();


More information about the Libreoffice-commits mailing list