1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include "SignatureLineContext.hxx"
12 #include <com/sun/star/beans/XPropertySet.hpp>
13 #include <com/sun/star/embed/XStorage.hpp>
14 #include <com/sun/star/frame/XModel.hpp>
15 #include <com/sun/star/frame/XStorable.hpp>
16 #include <com/sun/star/graphic/XGraphic.hpp>
17 #include <com/sun/star/security/DocumentDigitalSignatures.hpp>
18 #include <com/sun/star/security/XDocumentDigitalSignatures.hpp>
19 #include <com/sun/star/xml/sax/XAttributeList.hpp>
21 #include <sal/log.hxx>
22 #include <comphelper/processfactory.hxx>
23 #include <comphelper/storagehelper.hxx>
24 #include <xmloff/xmltoken.hxx>
25 #include <xmloff/xmlimp.hxx>
28 using namespace css::xml::sax
;
29 using namespace css::uno
;
30 using namespace css::drawing
;
31 using namespace css::embed
;
32 using namespace css::frame
;
33 using namespace css::io
;
34 using namespace css::graphic
;
35 using namespace css::security
;
36 using namespace xmloff::token
;
38 SignatureLineContext::SignatureLineContext(SvXMLImport
& rImport
, sal_uInt16 nPrfx
,
39 const OUString
& rLocalName
,
40 const Reference
<XAttributeList
>& xAttrList
,
41 const Reference
<XShape
>& rxShape
)
42 : SvXMLImportContext(rImport
, nPrfx
, rLocalName
)
44 Reference
<beans::XPropertySet
> xPropSet(rxShape
, UNO_QUERY_THROW
);
46 xPropSet
->setPropertyValue("IsSignatureLine", Any(true));
48 xPropSet
->setPropertyValue("SignatureLineId", Any(xAttrList
->getValueByName("loext:id")));
49 xPropSet
->setPropertyValue("SignatureLineSuggestedSignerName",
50 Any(xAttrList
->getValueByName("loext:suggested-signer-name")));
51 xPropSet
->setPropertyValue("SignatureLineSuggestedSignerTitle",
52 Any(xAttrList
->getValueByName("loext:suggested-signer-title")));
53 xPropSet
->setPropertyValue("SignatureLineSuggestedSignerEmail",
54 Any(xAttrList
->getValueByName("loext:suggested-signer-email")));
55 xPropSet
->setPropertyValue("SignatureLineSigningInstructions",
56 Any(xAttrList
->getValueByName("loext:signing-instructions")));
58 bool bShowSignDate
= xAttrList
->getValueByName("loext:show-sign-date") == GetXMLToken(XML_TRUE
);
60 = xAttrList
->getValueByName("loext:can-add-comment") == GetXMLToken(XML_TRUE
);
61 xPropSet
->setPropertyValue("SignatureLineShowSignDate", Any(bShowSignDate
));
62 xPropSet
->setPropertyValue("SignatureLineCanAddComment", Any(bCanAddComment
));
64 // Save unsigned graphic (need it when exporting)
65 Reference
<XGraphic
> xUnsignedGraphic
;
66 xPropSet
->getPropertyValue("Graphic") >>= xUnsignedGraphic
;
67 if (xUnsignedGraphic
.is())
68 xPropSet
->setPropertyValue("SignatureLineUnsignedImage", Any(xUnsignedGraphic
));
70 Reference
<XGraphic
> xGraphic
;
73 // Get the document signatures
74 css::uno::Reference
<XStorable
> xStorable(GetImport().GetModel(), UNO_QUERY_THROW
);
75 Reference
<XStorage
> xStorage
= comphelper::OStorageHelper::GetStorageOfFormatFromURL(
76 ZIP_STORAGE_FORMAT_STRING
, xStorable
->getLocation(), ElementModes::READ
);
80 SAL_WARN("xmloff", "No xStorage!");
84 OUString
const aODFVersion(comphelper::OStorageHelper::GetODFVersionFromStorage(xStorage
));
85 Reference
<XDocumentDigitalSignatures
> xSignatures(
86 security::DocumentDigitalSignatures::createWithVersion(
87 comphelper::getProcessComponentContext(), aODFVersion
));
89 Sequence
<DocumentSignatureInformation
> xSignatureInfo
90 = xSignatures
->verifyDocumentContentSignatures(xStorage
, Reference
<XInputStream
>());
92 // Try to find matching signature line image - if none exists that is fine,
93 // then the signature line is not digitally signed.
94 auto pSignatureInfo
= std::find_if(
95 xSignatureInfo
.begin(), xSignatureInfo
.end(),
96 [&xAttrList
](const DocumentSignatureInformation
& rSignatureInfo
) {
97 return rSignatureInfo
.SignatureLineId
== xAttrList
->getValueByName("loext:id");
99 bool bIsSigned(false);
100 if (pSignatureInfo
!= xSignatureInfo
.end())
103 if (pSignatureInfo
->SignatureIsValid
)
105 // Signature is valid, use the 'valid' image
106 SAL_WARN_IF(!pSignatureInfo
->ValidSignatureLineImage
.is(), "xmloff",
107 "No ValidSignatureLineImage!");
108 xGraphic
= pSignatureInfo
->ValidSignatureLineImage
;
112 // Signature is invalid, use the 'invalid' image
113 SAL_WARN_IF(!pSignatureInfo
->InvalidSignatureLineImage
.is(), "xmloff",
114 "No InvalidSignatureLineImage!");
115 xGraphic
= pSignatureInfo
->InvalidSignatureLineImage
;
118 xPropSet
->setPropertyValue("Graphic", Any(xGraphic
));
120 xPropSet
->setPropertyValue("SignatureLineIsSigned", Any(bIsSigned
));
122 catch (css::uno::Exception
&)
124 // DocumentDigitalSignatures service not available.
125 // We render the "unsigned" shape instead.
129 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */