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/graphic/GraphicProvider.hpp>
13 #include <com/sun/star/security/DocumentDigitalSignatures.hpp>
14 
19 #include <config_folders.h>
20 #include <rtl/bootstrap.hxx>
21 #include <sal/log.hxx>
22 #include <sfx2/docfile.hxx>
23 #include <sfx2/docfilt.hxx>
24 #include <sfx2/objsh.hxx>
25 #include <svx/dialmgr.hxx>
26 #include <svx/strings.hrc>
27 #include <svx/svdmark.hxx>
28 #include <svx/svdview.hxx>
29 #include <tools/stream.hxx>
31 #include <unotools/streamwrap.hxx>
32 #include <unotools/syslocale.hxx>
33 #include <vcl/weld.hxx>
34 
35 using namespace com::sun::star;
36 
38 {
39 OUString getSignatureImage(const OUString& rType)
40 {
41  OUString aType = rType;
42  if (aType.isEmpty())
43  {
44  aType = "signature-line.svg";
45  }
46  OUString aPath("$BRAND_BASE_DIR/" LIBO_SHARE_FOLDER "/filter/" + aType);
47  rtl::Bootstrap::expandMacros(aPath);
48  SvFileStream aStream(aPath, StreamMode::READ);
49  if (aStream.GetError() != ERRCODE_NONE)
50  {
51  SAL_WARN("cui.dialogs", "failed to open " << aType);
52  }
53 
54  OString const svg = read_uInt8s_ToOString(aStream, aStream.remainingSize());
55  return OUString::fromUtf8(svg);
56 }
57 
58 uno::Reference<security::XCertificate> getSignatureCertificate(SfxObjectShell* pShell,
59  weld::Window* pParent)
60 {
61  if (!pShell)
62  {
63  return uno::Reference<security::XCertificate>();
64  }
65 
66  if (!pParent)
67  {
68  return uno::Reference<security::XCertificate>();
69  }
70 
71  uno::Reference<security::XDocumentDigitalSignatures> xSigner;
72  if (pShell->GetMedium()->GetFilter()->IsAlienFormat())
73  {
74  xSigner = security::DocumentDigitalSignatures::createDefault(
76  }
77  else
78  {
79  OUString const aODFVersion(
81  xSigner = security::DocumentDigitalSignatures::createWithVersion(
83  }
84  xSigner->setParentWindow(pParent->GetXWindow());
85  OUString aDescription;
86  security::CertificateKind certificateKind = security::CertificateKind_NONE;
87  // When signing ooxml, we only want X.509 certificates
88  if (pShell->GetMedium()->GetFilter()->IsAlienFormat())
89  {
90  certificateKind = security::CertificateKind_X509;
91  }
92  uno::Reference<security::XCertificate> xSignCertificate
93  = xSigner->selectSigningCertificateWithType(certificateKind, aDescription);
94  return xSignCertificate;
95 }
96 
97 OUString getSignerName(const css::uno::Reference<css::security::XCertificate>& xCertificate)
98 {
99  return comphelper::xmlsec::GetContentPart(xCertificate->getSubjectName(),
100  xCertificate->getCertificateKind());
101 }
102 
104 {
105  const SvtSysLocale aSysLocale;
106  const LocaleDataWrapper& rLocaleData = aSysLocale.GetLocaleData();
107  Date aDateTime(Date::SYSTEM);
108  return rLocaleData.getDate(aDateTime);
109 }
110 
111 uno::Reference<graphic::XGraphic> importSVG(const OUString& rSVG)
112 {
113  SvMemoryStream aSvgStream(4096, 4096);
114  aSvgStream.WriteOString(OUStringToOString(rSVG, RTL_TEXTENCODING_UTF8));
115  uno::Reference<io::XInputStream> xInputStream(new utl::OSeekableInputStreamWrapper(aSvgStream));
116  uno::Reference<uno::XComponentContext> xContext(comphelper::getProcessComponentContext());
117  uno::Reference<graphic::XGraphicProvider> xProvider
118  = graphic::GraphicProvider::create(xContext);
119 
120  uno::Sequence<beans::PropertyValue> aMediaProperties(1);
121  aMediaProperties[0].Name = "InputStream";
122  aMediaProperties[0].Value <<= xInputStream;
123  uno::Reference<graphic::XGraphic> xGraphic(xProvider->queryGraphic(aMediaProperties));
124  return xGraphic;
125 }
126 
127 void setShapeCertificate(const SdrView* pView,
128  const css::uno::Reference<css::security::XCertificate>& xCertificate)
129 {
130  const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
131  if (rMarkList.GetMarkCount() < 1)
132  {
133  return;
134  }
135 
136  const SdrMark* pMark = rMarkList.GetMark(0);
137  SdrObject* pSignatureLine = pMark->GetMarkedSdrObj();
138  if (!pSignatureLine)
139  {
140  return;
141  }
142 
143  // Remember the selected certificate.
144  uno::Reference<beans::XPropertySet> xShapeProps(pSignatureLine->getUnoShape(), uno::UNO_QUERY);
145  comphelper::SequenceAsHashMap aMap(xShapeProps->getPropertyValue("InteropGrabBag"));
146  aMap["SignatureCertificate"] <<= xCertificate;
147  xShapeProps->setPropertyValue("InteropGrabBag",
148  uno::makeAny(aMap.getAsConstPropertyValueList()));
149 
150  // Read svg and replace placeholder texts.
151  OUString aSvgImage(svx::SignatureLineHelper::getSignatureImage("signature-line-draw.svg"));
152  aSvgImage = aSvgImage.replaceAll("[SIGNED_BY]", SvxResId(RID_SVXSTR_SIGNATURELINE_DSIGNED_BY));
153  OUString aSignerName = svx::SignatureLineHelper::getSignerName(xCertificate);
154  aSvgImage = aSvgImage.replaceAll("[SIGNER_NAME]", aSignerName);
156  aDate = SvxResId(RID_SVXSTR_SIGNATURELINE_DATE).replaceFirst("%1", aDate);
157  aSvgImage = aSvgImage.replaceAll("[DATE]", aDate);
158 
159  uno::Reference<graphic::XGraphic> xGraphic = svx::SignatureLineHelper::importSVG(aSvgImage);
160  xShapeProps->setPropertyValue("Graphic", uno::Any(xGraphic));
161 }
162 }
163 
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Reference< css::awt::XWindow > GetXWindow()=0
size_t GetMarkCount() const
Definition: svdmark.hxx:180
OUString getSignatureImage(const OUString &rType)
Returns an SVG template.
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:229
SvStream & WriteOString(const OString &rStr)
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
virtual css::uno::Reference< css::uno::XInterface > getUnoShape()
Definition: svdobj.cxx:2772
OUString SvxResId(const char *pId)
Definition: dialmgr.cxx:25
uno::Reference< security::XCertificate > getSignatureCertificate(SfxObjectShell *pShell, weld::Window *pParent)
Choose a signature for signature line purposes.
sal_uInt64 remainingSize()
uno::Reference< graphic::XGraphic > importSVG(const OUString &rSVG)
Interprets rSVG as a graphic and gives back the resulting UNO wrapper.
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:251
Abstract DrawObject.
Definition: svdobj.hxx:260
const std::shared_ptr< const SfxFilter > & GetFilter() const
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
OUString getDate(const Date &rDate) const
Any makeAny(Color const &value)
#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)
Reference< XGraphic > xGraphic
exports com.sun.star. svg
SfxMedium * GetMedium() const