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>
20 #include <sal/log.hxx>
21 #include <comphelper/processfactory.hxx>
22 #include <comphelper/storagehelper.hxx>
23 #include <xmloff/xmltoken.hxx>
24 #include <xmloff/xmlimp.hxx>
25 #include <xmloff/xmlnamespace.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_Int32
/*nElement*/,
39 const Reference
<XFastAttributeList
>& xAttrList
,
40 const Reference
<XShape
>& rxShape
)
41 : SvXMLImportContext(rImport
)
43 Reference
<beans::XPropertySet
> xPropSet(rxShape
, UNO_QUERY_THROW
);
45 xPropSet
->setPropertyValue(u
"IsSignatureLine"_ustr
, Any(true));
47 xPropSet
->setPropertyValue(u
"SignatureLineId"_ustr
,
48 Any(xAttrList
->getOptionalValue(XML_ELEMENT(LO_EXT
, XML_ID
))));
49 xPropSet
->setPropertyValue(
50 u
"SignatureLineSuggestedSignerName"_ustr
,
51 Any(xAttrList
->getOptionalValue(XML_ELEMENT(LO_EXT
, XML_SUGGESTED_SIGNER_NAME
))));
52 xPropSet
->setPropertyValue(
53 u
"SignatureLineSuggestedSignerTitle"_ustr
,
54 Any(xAttrList
->getOptionalValue(XML_ELEMENT(LO_EXT
, XML_SUGGESTED_SIGNER_TITLE
))));
55 xPropSet
->setPropertyValue(
56 u
"SignatureLineSuggestedSignerEmail"_ustr
,
57 Any(xAttrList
->getOptionalValue(XML_ELEMENT(LO_EXT
, XML_SUGGESTED_SIGNER_EMAIL
))));
58 xPropSet
->setPropertyValue(
59 u
"SignatureLineSigningInstructions"_ustr
,
60 Any(xAttrList
->getOptionalValue(XML_ELEMENT(LO_EXT
, XML_SIGNING_INSTRUCTIONS
))));
62 bool bShowSignDate
= xAttrList
->getOptionalValue(XML_ELEMENT(LO_EXT
, XML_SHOW_SIGN_DATE
))
63 == GetXMLToken(XML_TRUE
);
64 bool bCanAddComment
= xAttrList
->getOptionalValue(XML_ELEMENT(LO_EXT
, XML_CAN_ADD_COMMENT
))
65 == GetXMLToken(XML_TRUE
);
66 xPropSet
->setPropertyValue(u
"SignatureLineShowSignDate"_ustr
, Any(bShowSignDate
));
67 xPropSet
->setPropertyValue(u
"SignatureLineCanAddComment"_ustr
, Any(bCanAddComment
));
69 // Save unsigned graphic (need it when exporting)
70 Reference
<XGraphic
> xUnsignedGraphic
;
71 xPropSet
->getPropertyValue(u
"Graphic"_ustr
) >>= xUnsignedGraphic
;
72 if (xUnsignedGraphic
.is())
73 xPropSet
->setPropertyValue(u
"SignatureLineUnsignedImage"_ustr
, Any(xUnsignedGraphic
));
75 Reference
<XGraphic
> xGraphic
;
78 // Get the document signatures
79 css::uno::Reference
<XStorable
> xStorable(GetImport().GetModel(), UNO_QUERY_THROW
);
80 Reference
<XStorage
> xStorage
= comphelper::OStorageHelper::GetStorageOfFormatFromURL(
81 ZIP_STORAGE_FORMAT_STRING
, xStorable
->getLocation(), ElementModes::READ
);
85 SAL_WARN("xmloff", "No xStorage!");
89 OUString
const aODFVersion(comphelper::OStorageHelper::GetODFVersionFromStorage(xStorage
));
90 Reference
<XDocumentDigitalSignatures
> xSignatures(
91 security::DocumentDigitalSignatures::createWithVersion(
92 comphelper::getProcessComponentContext(), aODFVersion
));
94 const Sequence
<DocumentSignatureInformation
> xSignatureInfo
95 = xSignatures
->verifyDocumentContentSignatures(xStorage
, Reference
<XInputStream
>());
97 // Try to find matching signature line image - if none exists that is fine,
98 // then the signature line is not digitally signed.
100 = std::find_if(xSignatureInfo
.begin(), xSignatureInfo
.end(),
101 [&xAttrList
](const DocumentSignatureInformation
& rSignatureInfo
) {
102 return rSignatureInfo
.SignatureLineId
103 == xAttrList
->getOptionalValue(XML_ELEMENT(LO_EXT
, XML_ID
));
105 bool bIsSigned(false);
106 if (pSignatureInfo
!= xSignatureInfo
.end())
109 if (pSignatureInfo
->SignatureIsValid
)
111 // Signature is valid, use the 'valid' image
112 SAL_WARN_IF(!pSignatureInfo
->ValidSignatureLineImage
.is(), "xmloff",
113 "No ValidSignatureLineImage!");
114 xGraphic
= pSignatureInfo
->ValidSignatureLineImage
;
118 // Signature is invalid, use the 'invalid' image
119 SAL_WARN_IF(!pSignatureInfo
->InvalidSignatureLineImage
.is(), "xmloff",
120 "No InvalidSignatureLineImage!");
121 xGraphic
= pSignatureInfo
->InvalidSignatureLineImage
;
124 xPropSet
->setPropertyValue(u
"Graphic"_ustr
, Any(xGraphic
));
126 xPropSet
->setPropertyValue(u
"SignatureLineIsSigned"_ustr
, Any(bIsSigned
));
128 catch (css::uno::Exception
&)
130 // DocumentDigitalSignatures service not available.
131 // We render the "unsigned" shape instead.
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */