LibreOffice Module svx (master)  1
signaturelinehelper.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  */
9 
11 
12 #include <com/sun/star/drawing/XShape.hpp>
13 #include <com/sun/star/graphic/GraphicProvider.hpp>
14 #include <com/sun/star/security/DocumentDigitalSignatures.hpp>
15 
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>
33 #include <unotools/streamwrap.hxx>
34 #include <unotools/syslocale.hxx>
35 #include <vcl/weld.hxx>
36 
37 using namespace com::sun::star;
38 
40 {
41 OUString getSignatureImage(const OUString& rType)
42 {
43  OUString aType = rType;
44  if (aType.isEmpty())
45  {
46  aType = "signature-line.svg";
47  }
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)
52  {
53  SAL_WARN("cui.dialogs", "failed to open " << aType);
54  }
55 
56  OString const svg = read_uInt8s_ToOString(aStream, aStream.remainingSize());
57  return OUString::fromUtf8(svg);
58 }
59 
60 uno::Reference<security::XCertificate> getSignatureCertificate(SfxObjectShell* pShell,
61  weld::Window* pParent)
62 {
63  if (!pShell)
64  {
65  return {};
66  }
67 
68  if (!pParent)
69  {
70  return {};
71  }
72 
73  uno::Reference<security::XDocumentDigitalSignatures> xSigner;
74  if (pShell->GetMedium()->GetFilter()->IsAlienFormat())
75  {
76  xSigner = security::DocumentDigitalSignatures::createDefault(
78  }
79  else
80  {
81  OUString const aODFVersion(
83  xSigner = security::DocumentDigitalSignatures::createWithVersion(
85  }
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())
91  {
92  certificateKind = security::CertificateKind_X509;
93  }
94  uno::Reference<security::XCertificate> xSignCertificate
95  = xSigner->selectSigningCertificateWithType(certificateKind, aDescription);
96  return xSignCertificate;
97 }
98 
99 OUString getSignerName(const css::uno::Reference<css::security::XCertificate>& xCertificate)
100 {
101  return comphelper::xmlsec::GetContentPart(xCertificate->getSubjectName(),
102  xCertificate->getCertificateKind());
103 }
104 
106 {
107  const SvtSysLocale aSysLocale;
108  const LocaleDataWrapper& rLocaleData = aSysLocale.GetLocaleData();
109  Date aDateTime(Date::SYSTEM);
110  return rLocaleData.getDate(aDateTime);
111 }
112 
113 uno::Reference<graphic::XGraphic> importSVG(std::u16string_view rSVG)
114 {
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);
121 
122  uno::Sequence<beans::PropertyValue> aMediaProperties{ comphelper::makePropertyValue(
123  "InputStream", xInputStream) };
124  uno::Reference<graphic::XGraphic> xGraphic(xProvider->queryGraphic(aMediaProperties));
125  return xGraphic;
126 }
127 
128 void setShapeCertificate(const SdrView* pView,
129  const css::uno::Reference<css::security::XCertificate>& xCertificate)
130 {
131  const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
132  if (rMarkList.GetMarkCount() < 1)
133  {
134  return;
135  }
136 
137  const SdrMark* pMark = rMarkList.GetMark(0);
138  SdrObject* pSignatureLine = pMark->GetMarkedSdrObj();
139  if (!pSignatureLine)
140  {
141  return;
142  }
143 
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()));
150 
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);
157  aDate = SvxResId(RID_SVXSTR_SIGNATURELINE_DATE).replaceFirst("%1", aDate);
158  aSvgImage = aSvgImage.replaceAll("[DATE]", aDate);
159 
160  uno::Reference<graphic::XGraphic> xGraphic = svx::SignatureLineHelper::importSVG(aSvgImage);
161  xShapeProps->setPropertyValue("Graphic", uno::Any(xGraphic));
162 }
163 }
164 
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Reference< css::awt::XWindow > GetXWindow()=0
size_t GetMarkCount() const
Definition: svdmark.hxx:178
OUString getSignatureImage(const OUString &rType)
Returns an SVG template.
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
OUString getSignerName(const css::uno::Reference< css::security::XCertificate > &xCertificate)
Get a signer name out of a certificate.
SdrMark * GetMark(size_t nNum) const
Definition: svdmark.cxx:230
OUString getLocalizedDate()
Gets a localized date string.
css::uno::Reference< css::embed::XStorage > const & GetStorage()
HashMap_OWString_Interface aMap
const LocaleDataWrapper & GetLocaleData() const
ErrCode GetError() const
OUString SvxResId(TranslateId aId)
Definition: dialmgr.cxx:24
virtual css::uno::Reference< css::drawing::XShape > getUnoShape()
Definition: svdobj.cxx:2884
uno::Reference< security::XCertificate > getSignatureCertificate(SfxObjectShell *pShell, weld::Window *pParent)
Choose a signature for signature line purposes.
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
sal_uInt64 remainingSize()
SdrObject * GetMarkedSdrObj() const
Definition: svdmark.hxx:68
Everything a View needs to know about a selected object.
Definition: svdmark.hxx:44
OUString GetContentPart(const OUString &_rRawString, const css::security::CertificateKind &rKind)
const SdrMarkList & GetMarkedObjectList() const
Definition: svdmrkv.hxx:258
uno::Reference< graphic::XGraphic > importSVG(std::u16string_view rSVG)
Interprets rSVG as a graphic and gives back the resulting UNO wrapper.
Abstract DrawObject.
Definition: svdobj.hxx:260
const std::shared_ptr< const SfxFilter > & GetFilter() const
OUString getDate(const Date &rDate) const
SvStream & WriteOString(std::string_view rStr)
#define ERRCODE_NONE
void setShapeCertificate(const SdrView *pView, const css::uno::Reference< css::security::XCertificate > &xCertificate)
Sets xCertificate as the signing certificate of the selected shape on pView.
Reference< XComponentContext > getProcessComponentContext()
static OUString GetODFVersionFromStorage(const css::uno::Reference< css::embed::XStorage > &xStorage)
#define SAL_WARN(area, stream)
OString read_uInt8s_ToOString(SvStream &rStrm, std::size_t nLen)
exports com.sun.star. svg
SfxMedium * GetMedium() const