1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <svx/signaturelinehelper.hxx>
12 #include <com/sun/star/drawing/XShape.hpp>
13 #include <com/sun/star/graphic/GraphicProvider.hpp>
14 #include <com/sun/star/security/DocumentDigitalSignatures.hpp>
16 #include <comphelper/processfactory.hxx>
17 #include <comphelper/propertyvalue.hxx>
18 #include <comphelper/sequenceashashmap.hxx>
19 #include <comphelper/storagehelper.hxx>
20 #include <comphelper/xmlsechelper.hxx>
21 #include <config_folders.h>
22 #include <rtl/bootstrap.hxx>
23 #include <sal/log.hxx>
24 #include <sfx2/docfile.hxx>
25 #include <sfx2/docfilt.hxx>
26 #include <sfx2/objsh.hxx>
27 #include <svx/dialmgr.hxx>
28 #include <svx/strings.hrc>
29 #include <svx/svdmark.hxx>
30 #include <svx/svdview.hxx>
31 #include <tools/stream.hxx>
32 #include <unotools/localedatawrapper.hxx>
33 #include <unotools/streamwrap.hxx>
34 #include <unotools/syslocale.hxx>
35 #include <vcl/weld.hxx>
36 #include <sfx2/digitalsignatures.hxx>
38 using namespace com::sun::star
;
40 namespace svx::SignatureLineHelper
42 OUString
getSignatureImage(const OUString
& rType
)
44 OUString aType
= rType
;
47 aType
= "signature-line.svg";
49 OUString
aPath("$BRAND_BASE_DIR/" LIBO_SHARE_FOLDER
"/filter/" + aType
);
50 rtl::Bootstrap::expandMacros(aPath
);
51 SvFileStream
aStream(aPath
, StreamMode::READ
);
52 if (aStream
.GetError() != ERRCODE_NONE
)
54 SAL_WARN("cui.dialogs", "failed to open " << aType
);
57 OString
const svg
= read_uInt8s_ToOString(aStream
, aStream
.remainingSize());
58 return OUString::fromUtf8(svg
);
61 uno::Reference
<security::XCertificate
>
62 getSignatureCertificate(SfxObjectShell
* pShell
, SfxViewShell
* pViewShell
, weld::Window
* pParent
)
74 uno::Reference
<security::XDocumentDigitalSignatures
> xSigner
;
75 if (pShell
->GetMedium()->GetFilter()->IsAlienFormat())
77 xSigner
= security::DocumentDigitalSignatures::createDefault(
78 comphelper::getProcessComponentContext());
82 OUString
const aODFVersion(
83 comphelper::OStorageHelper::GetODFVersionFromStorage(pShell
->GetStorage()));
84 xSigner
= security::DocumentDigitalSignatures::createWithVersion(
85 comphelper::getProcessComponentContext(), aODFVersion
);
87 xSigner
->setParentWindow(pParent
->GetXWindow());
88 OUString aDescription
;
89 security::CertificateKind certificateKind
= security::CertificateKind_NONE
;
90 // When signing ooxml, we only want X.509 certificates
91 if (pShell
->GetMedium()->GetFilter()->IsAlienFormat())
93 certificateKind
= security::CertificateKind_X509
;
95 auto xModelSigner
= dynamic_cast<sfx2::DigitalSignatures
*>(xSigner
.get());
97 uno::Reference
<security::XCertificate
> xSignCertificate
98 = xModelSigner
->SelectSigningCertificateWithType(pViewShell
, certificateKind
, aDescription
);
99 return xSignCertificate
;
102 OUString
getSignerName(const css::uno::Reference
<css::security::XCertificate
>& xCertificate
)
104 return comphelper::xmlsec::GetContentPart(xCertificate
->getSubjectName(),
105 xCertificate
->getCertificateKind());
108 OUString
getLocalizedDate()
110 const SvtSysLocale aSysLocale
;
111 const LocaleDataWrapper
& rLocaleData
= aSysLocale
.GetLocaleData();
112 Date
aDateTime(Date::SYSTEM
);
113 return rLocaleData
.getDate(aDateTime
);
116 uno::Reference
<graphic::XGraphic
> importSVG(std::u16string_view rSVG
)
118 SvMemoryStream
aSvgStream(4096, 4096);
119 aSvgStream
.WriteOString(OUStringToOString(rSVG
, RTL_TEXTENCODING_UTF8
));
120 uno::Reference
<io::XInputStream
> xInputStream(new utl::OSeekableInputStreamWrapper(aSvgStream
));
121 const uno::Reference
<uno::XComponentContext
>& xContext(
122 comphelper::getProcessComponentContext());
123 uno::Reference
<graphic::XGraphicProvider
> xProvider
124 = graphic::GraphicProvider::create(xContext
);
126 uno::Sequence
<beans::PropertyValue
> aMediaProperties
{ comphelper::makePropertyValue(
127 u
"InputStream"_ustr
, xInputStream
) };
128 uno::Reference
<graphic::XGraphic
> xGraphic(xProvider
->queryGraphic(aMediaProperties
));
132 void setShapeCertificate(const SdrView
* pView
,
133 const css::uno::Reference
<css::security::XCertificate
>& xCertificate
)
135 const SdrMarkList
& rMarkList
= pView
->GetMarkedObjectList();
136 if (rMarkList
.GetMarkCount() < 1)
141 const SdrMark
* pMark
= rMarkList
.GetMark(0);
142 SdrObject
* pSignatureLine
= pMark
->GetMarkedSdrObj();
148 // Remember the selected certificate.
149 uno::Reference
<drawing::XShape
> xShape
= pSignatureLine
->getUnoShape();
150 uno::Reference
<beans::XPropertySet
> xShapeProps(xShape
, uno::UNO_QUERY
);
151 comphelper::SequenceAsHashMap
aMap(xShapeProps
->getPropertyValue(u
"InteropGrabBag"_ustr
));
152 aMap
[u
"SignatureCertificate"_ustr
] <<= xCertificate
;
153 xShapeProps
->setPropertyValue(u
"InteropGrabBag"_ustr
,
154 uno::Any(aMap
.getAsConstPropertyValueList()));
156 // Read svg and replace placeholder texts.
158 svx::SignatureLineHelper::getSignatureImage(u
"signature-line-draw.svg"_ustr
));
159 aSvgImage
= aSvgImage
.replaceAll("[SIGNED_BY]", SvxResId(RID_SVXSTR_SIGNATURELINE_DSIGNED_BY
));
160 OUString aSignerName
= svx::SignatureLineHelper::getSignerName(xCertificate
);
161 aSvgImage
= aSvgImage
.replaceAll("[SIGNER_NAME]", aSignerName
);
162 OUString aDate
= svx::SignatureLineHelper::getLocalizedDate();
163 aDate
= SvxResId(RID_SVXSTR_SIGNATURELINE_DATE
).replaceFirst("%1", aDate
);
164 aSvgImage
= aSvgImage
.replaceAll("[DATE]", aDate
);
166 uno::Reference
<graphic::XGraphic
> xGraphic
= svx::SignatureLineHelper::importSVG(aSvgImage
);
167 xShapeProps
->setPropertyValue(u
"Graphic"_ustr
, uno::Any(xGraphic
));
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */