include/sfx2/digitalsignatures.hxx                         |    3 ++
 include/sfx2/docfile.hxx                                   |    2 +
 sfx2/source/doc/docfile.cxx                                |    7 +++---
 sfx2/source/doc/objserv.cxx                                |   14 +++++++++----
 xmlsecurity/inc/digitalsignaturesdialog.hxx                |    4 ++-
 xmlsecurity/source/component/documentdigitalsignatures.cxx |   13 +++++++-----
 xmlsecurity/source/dialogs/digitalsignaturesdialog.cxx     |   10 +++++++--
 7 files changed, 38 insertions(+), 15 deletions(-)

New commits:
commit a9d412b7d4fca05bba2004ac68712e24986fa205
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Thu Sep 26 09:07:41 2024 +0200
Commit:     Caolán McNamara <caolan.mcnam...@collabora.com>
CommitDate: Fri Sep 27 13:30:15 2024 +0200

    cool#9992 lok doc sign: conditionally show the add button in the sign dialog
    
    Open the sign dialog, the Add button is hidden, even if the LOK clients
    initializes the view with a working signing certificate.
    
    SfxMedium::SignContents_Impl() knows the correct view, but by the time
    the execution arrives to the DigitalSignaturesDialog ctor, this info is
    lost, so we don't know what is the current view.
    
    Fix the problem by looking up the view of the object shell at UNO
    command dispatch time, and passing that around, so the signature dialog
    can also access it. If the view has a signing certificate configured,
    then allow signing.
    
    The certificate chooser triggered by this button still needs fixing.
    
    (cherry picked from commit c57434559cf5ffd82c3c72e8a0884d4874885dca)
    
    Change-Id: I1fae63cea27ea1e68e938879f4507f53ade484f1
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/174025
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Tested-by: Caolán McNamara <caolan.mcnam...@collabora.com>
    Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com>

diff --git a/include/sfx2/digitalsignatures.hxx 
b/include/sfx2/digitalsignatures.hxx
index 856e1c5bf61a..84b77fd759dd 100644
--- a/include/sfx2/digitalsignatures.hxx
+++ b/include/sfx2/digitalsignatures.hxx
@@ -18,6 +18,8 @@
 
 #include <sal/types.h>
 
+class SfxViewShell;
+
 namespace sfx2
 {
 /// Extension of css::security::XDocumentDigitalSignatures for internal 
purposes.
@@ -35,6 +37,7 @@ public:
     /// Async replacement for signDocumentContent().
     virtual void SignDocumentContentAsync(const 
css::uno::Reference<css::embed::XStorage>& xStorage,
                                           const 
css::uno::Reference<css::io::XStream>& xSignStream,
+                                          SfxViewShell* pViewShell,
                                           const std::function<void(bool)>& 
rCallback)
         = 0;
 
diff --git a/include/sfx2/docfile.hxx b/include/sfx2/docfile.hxx
index 01046aa66789..2eb59b654147 100644
--- a/include/sfx2/docfile.hxx
+++ b/include/sfx2/docfile.hxx
@@ -53,6 +53,7 @@ class SfxFilter;
 class SfxMedium_Impl;
 class INetURLObject;
 class SfxFrame;
+class SfxViewShell;
 class DateTime;
 struct ImplSVEvent;
 
@@ -273,6 +274,7 @@ public:
     SAL_DLLPRIVATE void
     SignContents_Impl(weld::Window* pDialogParent,
                       bool bSignScriptingContent, bool 
bHasValidDocumentSignature,
+                      SfxViewShell* pViewShell,
                       const std::function<void(bool)>& rCallback,
                       const OUString& aSignatureLineId = OUString(),
                       const css::uno::Reference<css::security::XCertificate>& 
xCert
diff --git a/sfx2/source/doc/docfile.cxx b/sfx2/source/doc/docfile.cxx
index 8002645a015a..32a7721f7d6e 100644
--- a/sfx2/source/doc/docfile.cxx
+++ b/sfx2/source/doc/docfile.cxx
@@ -4301,6 +4301,7 @@ bool SfxMedium::SignDocumentContentUsingCertificate(
 void SfxMedium::SignContents_Impl(weld::Window* pDialogParent,
                                   bool bSignScriptingContent,
                                   bool bHasValidDocumentSignature,
+                                  SfxViewShell* pViewShell,
                                   const std::function<void(bool)>& rCallback,
                                   const OUString& aSignatureLineId,
                                   const Reference<XCertificate>& xCert,
@@ -4490,7 +4491,7 @@ void SfxMedium::SignContents_Impl(weld::Window* 
pDialogParent,
 
                     // Async, all code before return has to go into the 
callback.
                     
xModelSigner->SignDocumentContentAsync(GetZipStorageToSign_Impl(),
-                                                            xStream, 
[onODFSignDocumentContentFinished, onSignDocumentContentFinished](bool bRet) {
+                                                            xStream, 
pViewShell, [onODFSignDocumentContentFinished, 
onSignDocumentContentFinished](bool bRet) {
                         if (bRet)
                         {
                             onODFSignDocumentContentFinished();
@@ -4530,7 +4531,7 @@ void SfxMedium::SignContents_Impl(weld::Window* 
pDialogParent,
                 {
                     // We need read-write to be able to add the signature 
relation.
                     xModelSigner->SignDocumentContentAsync(
-                        GetZipStorageToSign_Impl(/*bReadOnly=*/false), 
xStream, [onOOXMLSignDocumentContentFinished, 
onSignDocumentContentFinished](bool bRet) {
+                        GetZipStorageToSign_Impl(/*bReadOnly=*/false), 
xStream, pViewShell, [onOOXMLSignDocumentContentFinished, 
onSignDocumentContentFinished](bool bRet) {
                         if (bRet)
                         {
                             onOOXMLSignDocumentContentFinished();
@@ -4552,7 +4553,7 @@ void SfxMedium::SignContents_Impl(weld::Window* 
pDialogParent,
                 // Something not ZIP based: e.g. PDF.
                 std::unique_ptr<SvStream> 
pStream(utl::UcbStreamHelper::CreateStream(GetName(), StreamMode::READ | 
StreamMode::WRITE));
                 uno::Reference<io::XStream> xStream(new 
utl::OStreamWrapper(std::move(pStream)));
-                
xModelSigner->SignDocumentContentAsync(uno::Reference<embed::XStorage>(), 
xStream, [onSignDocumentContentFinished](bool bRet) {
+                
xModelSigner->SignDocumentContentAsync(uno::Reference<embed::XStorage>(), 
xStream, pViewShell, [onSignDocumentContentFinished](bool bRet) {
                     onSignDocumentContentFinished(bRet);
                 });
                 return;
diff --git a/sfx2/source/doc/objserv.cxx b/sfx2/source/doc/objserv.cxx
index 870642f5b64f..5f791ea2e541 100644
--- a/sfx2/source/doc/objserv.cxx
+++ b/sfx2/source/doc/objserv.cxx
@@ -2145,8 +2145,10 @@ void SfxObjectShell::SignDocumentContent(weld::Window* 
pDialogParent, const std:
         return;
     }
 
+    SfxViewFrame* pFrame = GetFrame();
+    SfxViewShell* pViewShell = pFrame ? pFrame->GetViewShell() : nullptr;
     // Async, all code before the end has to go into the callback.
-    GetMedium()->SignContents_Impl(pDialogParent, false, HasValidSignatures(), 
[this, rCallback](bool bSignSuccess) {
+    GetMedium()->SignContents_Impl(pDialogParent, false, HasValidSignatures(), 
pViewShell, [this, rCallback](bool bSignSuccess) {
             AfterSigning(bSignSuccess, false);
 
             rCallback(bSignSuccess);
@@ -2261,13 +2263,15 @@ void SfxObjectShell::SignSignatureLine(weld::Window* 
pDialogParent,
     if (CheckIsReadonly(false, pDialogParent))
         return;
 
+    SfxViewFrame* pFrame = GetFrame();
+    SfxViewShell* pViewShell = pFrame ? pFrame->GetViewShell() : nullptr;
     GetMedium()->SignContents_Impl(pDialogParent,
-        false, HasValidSignatures(), [this](bool bSignSuccess) {
+        false, HasValidSignatures(), pViewShell, [this, pFrame](bool 
bSignSuccess) {
         AfterSigning(bSignSuccess, false);
 
         // Reload the document to get the updated graphic
         // FIXME: Update just the signature line graphic instead of reloading 
the document
-        if (SfxViewFrame* pFrame = GetFrame())
+        if (pFrame)
             pFrame->GetDispatcher()->Execute(SID_RELOAD);
     }, aSignatureLineId, xCert, xValidGraphic, xInvalidGraphic, aComment);
 }
@@ -2291,7 +2295,9 @@ void SfxObjectShell::SignScriptingContent(weld::Window* 
pDialogParent, const std
         return;
     }
 
-    GetMedium()->SignContents_Impl(pDialogParent, true, HasValidSignatures(), 
[this, rCallback](bool bSignSuccess) {
+    SfxViewFrame* pFrame = GetFrame();
+    SfxViewShell* pViewShell = pFrame ? pFrame->GetViewShell() : nullptr;
+    GetMedium()->SignContents_Impl(pDialogParent, true, HasValidSignatures(), 
pViewShell, [this, rCallback](bool bSignSuccess) {
         AfterSigning(bSignSuccess, true);
 
         rCallback(bSignSuccess);
diff --git a/xmlsecurity/inc/digitalsignaturesdialog.hxx 
b/xmlsecurity/inc/digitalsignaturesdialog.hxx
index 0b6c544c8e3c..73c3b80dd1a3 100644
--- a/xmlsecurity/inc/digitalsignaturesdialog.hxx
+++ b/xmlsecurity/inc/digitalsignaturesdialog.hxx
@@ -38,6 +38,7 @@ namespace com::sun::star {
 
 class HeaderBar;
 class CertificateViewer;
+class SfxViewShell;
 
 class DigitalSignaturesDialog final : public weld::GenericDialogController
 {
@@ -104,7 +105,8 @@ private:
 public:
     DigitalSignaturesDialog(weld::Window* pParent, const css::uno::Reference<
         css::uno::XComponentContext >& rxCtx, DocumentSignatureMode eMode,
-        bool bReadOnly, OUString sODFVersion, bool bHasDocumentSignature);
+        bool bReadOnly, OUString sODFVersion, bool bHasDocumentSignature,
+        SfxViewShell* pViewShell);
     virtual ~DigitalSignaturesDialog() override;
 
     // Initialize the dialog and the security environment, returns TRUE on 
success
diff --git a/xmlsecurity/source/component/documentdigitalsignatures.cxx 
b/xmlsecurity/source/component/documentdigitalsignatures.cxx
index d7f2121442d8..05feb96e3776 100644
--- a/xmlsecurity/source/component/documentdigitalsignatures.cxx
+++ b/xmlsecurity/source/component/documentdigitalsignatures.cxx
@@ -93,6 +93,7 @@ private:
     void ImplViewSignatures(const css::uno::Reference<css::embed::XStorage>& 
rxStorage,
                             const css::uno::Reference<css::io::XStream>& 
xSignStream,
                             DocumentSignatureMode eMode, bool bReadOnly,
+                            SfxViewShell* pViewShell,
                             const std::function<void(bool)>& rCallback);
     /// @throws css::uno::RuntimeException
     void ImplViewSignatures(const css::uno::Reference<css::embed::XStorage>& 
rxStorage,
@@ -222,6 +223,7 @@ public:
     /// See sfx2::DigitalSignatures::SignDocumentContentAsync().
     void SignDocumentContentAsync(const 
css::uno::Reference<css::embed::XStorage>& xStorage,
                                   const css::uno::Reference<css::io::XStream>& 
xSignStream,
+                                  SfxViewShell* pViewShell,
                                   const std::function<void(bool)>& rCallback) 
override;
     /// See sfx2::DigitalSignatures::SignScriptingContentAsync().
     void SignScriptingContentAsync(const 
css::uno::Reference<css::embed::XStorage>& xStorage,
@@ -436,17 +438,17 @@ void DocumentDigitalSignatures::ImplViewSignatures(
     Reference< io::XStream > xStream;
     if ( xSignStream.is() )
         xStream.set( xSignStream, UNO_QUERY );
-    ImplViewSignatures( rxStorage, xStream, eMode, bReadOnly, [](bool 
/*bSuccess*/){} );
+    ImplViewSignatures( rxStorage, xStream, eMode, bReadOnly, nullptr, [](bool 
/*bSuccess*/){} );
 }
 
 void DocumentDigitalSignatures::ImplViewSignatures(
     const Reference< css::embed::XStorage >& rxStorage, const Reference< 
css::io::XStream >& xSignStream,
-    DocumentSignatureMode eMode, bool bReadOnly, const 
std::function<void(bool)>& rCallback )
+    DocumentSignatureMode eMode, bool bReadOnly, SfxViewShell* pViewShell, 
const std::function<void(bool)>& rCallback )
 {
     bool bChanges = false;
     auto xSignaturesDialog = std::make_shared<DigitalSignaturesDialog>(
         Application::GetFrameWeld(mxParentWindow), mxCtx, eMode, bReadOnly, 
m_sODFVersion,
-        m_bHasDocumentSignature);
+        m_bHasDocumentSignature, pViewShell);
     bool bInit = xSignaturesDialog->Init();
     SAL_WARN_IF( !bInit, "xmlsecurity.comp", "Error initializing security 
context!" );
     if ( bInit )
@@ -843,10 +845,11 @@ bool DocumentDigitalSignatures::SignModelWithCertificate(
 
 void DocumentDigitalSignatures::SignDocumentContentAsync(const 
css::uno::Reference<css::embed::XStorage>& rxStorage,
                               const css::uno::Reference<css::io::XStream>& 
xSignStream,
+                              SfxViewShell* pViewShell,
                               const std::function<void(bool)>& rCallback)
 {
     OSL_ENSURE(!m_sODFVersion.isEmpty(), "DocumentDigitalSignatures: ODF 
Version not set, assuming minimum 1.2");
-    ImplViewSignatures( rxStorage, xSignStream, 
DocumentSignatureMode::Content, false, rCallback );
+    ImplViewSignatures( rxStorage, xSignStream, 
DocumentSignatureMode::Content, false, pViewShell, rCallback );
 }
 
 void DocumentDigitalSignatures::SignScriptingContentAsync(
@@ -855,7 +858,7 @@ void DocumentDigitalSignatures::SignScriptingContentAsync(
 {
     OSL_ENSURE(!m_sODFVersion.isEmpty(),"DocumentDigitalSignatures: ODF 
Version not set, assuming minimum 1.2");
     OSL_ENSURE(m_nArgumentsCount == 2, "DocumentDigitalSignatures: Service was 
not initialized properly");
-    ImplViewSignatures( rxStorage, xSignStream, DocumentSignatureMode::Macros, 
false, rCallback );
+    ImplViewSignatures( rxStorage, xSignStream, DocumentSignatureMode::Macros, 
false, nullptr, rCallback );
 }
 
 void DocumentDigitalSignatures::SetSignScriptingContent(
diff --git a/xmlsecurity/source/dialogs/digitalsignaturesdialog.cxx 
b/xmlsecurity/source/dialogs/digitalsignaturesdialog.cxx
index 82d50891b674..61f5d315b02e 100644
--- a/xmlsecurity/source/dialogs/digitalsignaturesdialog.cxx
+++ b/xmlsecurity/source/dialogs/digitalsignaturesdialog.cxx
@@ -67,6 +67,7 @@
 #include <vcl/weld.hxx>
 #include <vcl/svapp.hxx>
 #include <unotools/configitem.hxx>
+#include <sfx2/viewsh.hxx>
 
 #ifdef _WIN32
 #include <o3tl/char16_t2wchar_t.hxx>
@@ -235,7 +236,8 @@ bool IsThereCertificateMgr()
 DigitalSignaturesDialog::DigitalSignaturesDialog(
     weld::Window* pParent,
     const uno::Reference< uno::XComponentContext >& rxCtx, 
DocumentSignatureMode eMode,
-    bool bReadOnly, OUString sODFVersion, bool bHasDocumentSignature)
+    bool bReadOnly, OUString sODFVersion, bool bHasDocumentSignature,
+    SfxViewShell* pViewShell)
     : GenericDialogController(pParent, "xmlsec/ui/digitalsignaturesdialog.ui", 
"DigitalSignaturesDialog")
     , maSignatureManager(rxCtx, eMode)
     , m_sODFVersion (std::move(sODFVersion))
@@ -306,7 +308,11 @@ DigitalSignaturesDialog::DigitalSignaturesDialog(
 
     if (comphelper::LibreOfficeKit::isActive())
     {
-        m_xAddBtn->hide();
+        // If the view has a signing certificate, then allow adding a 
signature.
+        if (!pViewShell || !pViewShell->GetSigningCertificate().is())
+        {
+            m_xAddBtn->hide();
+        }
         m_xStartCertMgrBtn->hide();
     }
 

Reply via email to