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>
37 using namespace com::sun::star
;
39 namespace svx::SignatureLineHelper
41 OUString
getSignatureImage(const OUString
& rType
)
43 OUString aType
= rType
;
46 aType
= "signature-line.svg";
48 OUString
aPath("$BRAND_BASE_DIR/" LIBO_SHARE_FOLDER
"/filter/" + aType
);
49 rtl::Bootstrap::expandMacros(aPath
);
50 SvFileStream
aStream(aPath
, StreamMode::READ
);
51 if (aStream
.GetError() != ERRCODE_NONE
)
53 SAL_WARN("cui.dialogs", "failed to open " << aType
);
56 OString
const svg
= read_uInt8s_ToOString(aStream
, aStream
.remainingSize());
57 return OUString::fromUtf8(svg
);
60 uno::Reference
<security::XCertificate
> getSignatureCertificate(SfxObjectShell
* pShell
,
61 weld::Window
* pParent
)
73 uno::Reference
<security::XDocumentDigitalSignatures
> xSigner
;
74 if (pShell
->GetMedium()->GetFilter()->IsAlienFormat())
76 xSigner
= security::DocumentDigitalSignatures::createDefault(
77 comphelper::getProcessComponentContext());
81 OUString
const aODFVersion(
82 comphelper::OStorageHelper::GetODFVersionFromStorage(pShell
->GetStorage()));
83 xSigner
= security::DocumentDigitalSignatures::createWithVersion(
84 comphelper::getProcessComponentContext(), aODFVersion
);
86 xSigner
->setParentWindow(pParent
->GetXWindow());
87 OUString aDescription
;
88 security::CertificateKind certificateKind
= security::CertificateKind_NONE
;
89 // When signing ooxml, we only want X.509 certificates
90 if (pShell
->GetMedium()->GetFilter()->IsAlienFormat())
92 certificateKind
= security::CertificateKind_X509
;
94 uno::Reference
<security::XCertificate
> xSignCertificate
95 = xSigner
->selectSigningCertificateWithType(certificateKind
, aDescription
);
96 return xSignCertificate
;
99 OUString
getSignerName(const css::uno::Reference
<css::security::XCertificate
>& xCertificate
)
101 return comphelper::xmlsec::GetContentPart(xCertificate
->getSubjectName(),
102 xCertificate
->getCertificateKind());
105 OUString
getLocalizedDate()
107 const SvtSysLocale aSysLocale
;
108 const LocaleDataWrapper
& rLocaleData
= aSysLocale
.GetLocaleData();
109 Date
aDateTime(Date::SYSTEM
);
110 return rLocaleData
.getDate(aDateTime
);
113 uno::Reference
<graphic::XGraphic
> importSVG(std::u16string_view rSVG
)
115 SvMemoryStream
aSvgStream(4096, 4096);
116 aSvgStream
.WriteOString(OUStringToOString(rSVG
, RTL_TEXTENCODING_UTF8
));
117 uno::Reference
<io::XInputStream
> xInputStream(new utl::OSeekableInputStreamWrapper(aSvgStream
));
118 uno::Reference
<uno::XComponentContext
> xContext(comphelper::getProcessComponentContext());
119 uno::Reference
<graphic::XGraphicProvider
> xProvider
120 = graphic::GraphicProvider::create(xContext
);
122 uno::Sequence
<beans::PropertyValue
> aMediaProperties
{ comphelper::makePropertyValue(
123 "InputStream", xInputStream
) };
124 uno::Reference
<graphic::XGraphic
> xGraphic(xProvider
->queryGraphic(aMediaProperties
));
128 void setShapeCertificate(const SdrView
* pView
,
129 const css::uno::Reference
<css::security::XCertificate
>& xCertificate
)
131 const SdrMarkList
& rMarkList
= pView
->GetMarkedObjectList();
132 if (rMarkList
.GetMarkCount() < 1)
137 const SdrMark
* pMark
= rMarkList
.GetMark(0);
138 SdrObject
* pSignatureLine
= pMark
->GetMarkedSdrObj();
144 // Remember the selected certificate.
145 uno::Reference
<drawing::XShape
> xShape
= pSignatureLine
->getUnoShape();
146 uno::Reference
<beans::XPropertySet
> xShapeProps(xShape
, uno::UNO_QUERY
);
147 comphelper::SequenceAsHashMap
aMap(xShapeProps
->getPropertyValue("InteropGrabBag"));
148 aMap
["SignatureCertificate"] <<= xCertificate
;
149 xShapeProps
->setPropertyValue("InteropGrabBag", uno::Any(aMap
.getAsConstPropertyValueList()));
151 // Read svg and replace placeholder texts.
152 OUString
aSvgImage(svx::SignatureLineHelper::getSignatureImage("signature-line-draw.svg"));
153 aSvgImage
= aSvgImage
.replaceAll("[SIGNED_BY]", SvxResId(RID_SVXSTR_SIGNATURELINE_DSIGNED_BY
));
154 OUString aSignerName
= svx::SignatureLineHelper::getSignerName(xCertificate
);
155 aSvgImage
= aSvgImage
.replaceAll("[SIGNER_NAME]", aSignerName
);
156 OUString aDate
= svx::SignatureLineHelper::getLocalizedDate();
157 aDate
= SvxResId(RID_SVXSTR_SIGNATURELINE_DATE
).replaceFirst("%1", aDate
);
158 aSvgImage
= aSvgImage
.replaceAll("[DATE]", aDate
);
160 uno::Reference
<graphic::XGraphic
> xGraphic
= svx::SignatureLineHelper::importSVG(aSvgImage
);
161 xShapeProps
->setPropertyValue("Graphic", uno::Any(xGraphic
));
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */