LibreOffice Module cui (master)  1
SignSignatureLineDialog.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 <sal/log.hxx>
13 #include <sal/types.h>
14 
15 #include <dialmgr.hxx>
16 #include <strings.hrc>
17 
22 #include <sfx2/docfile.hxx>
23 #include <sfx2/docfilt.hxx>
24 #include <sfx2/objsh.hxx>
25 #include <svx/xoutbmp.hxx>
26 #include <tools/date.hxx>
27 #include <tools/stream.hxx>
29 #include <unotools/streamwrap.hxx>
30 #include <unotools/syslocale.hxx>
31 #include <utility>
32 #include <vcl/graph.hxx>
33 #include <vcl/weld.hxx>
35 
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <com/sun/star/graphic/GraphicProvider.hpp>
38 #include <com/sun/star/graphic/XGraphic.hpp>
39 #include <com/sun/star/graphic/XGraphicProvider.hpp>
40 #include <com/sun/star/io/XInputStream.hpp>
41 #include <com/sun/star/security/CertificateKind.hpp>
42 #include <com/sun/star/security/DocumentDigitalSignatures.hpp>
43 #include <com/sun/star/security/XCertificate.hpp>
44 #include <com/sun/star/security/XDocumentDigitalSignatures.hpp>
45 #include <com/sun/star/ui/dialogs/FilePicker.hpp>
46 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
47 #include <com/sun/star/ui/dialogs/XFilePicker3.hpp>
48 
49 using namespace comphelper;
50 using namespace css;
51 using namespace css::uno;
52 using namespace css::beans;
53 using namespace css::frame;
54 using namespace css::io;
55 using namespace css::lang;
56 using namespace css::frame;
57 using namespace css::text;
58 using namespace css::graphic;
59 using namespace css::security;
60 using namespace css::ui::dialogs;
61 
63  : SignatureLineDialogBase(pParent, std::move(xModel), "cui/ui/signsignatureline.ui",
64  "SignSignatureLineDialog")
65  , m_xEditName(m_xBuilder->weld_entry("edit_name"))
66  , m_xEditComment(m_xBuilder->weld_text_view("edit_comment"))
67  , m_xBtnLoadImage(m_xBuilder->weld_button("btn_load_image"))
68  , m_xBtnClearImage(m_xBuilder->weld_button("btn_clear_image"))
69  , m_xBtnChooseCertificate(m_xBuilder->weld_button("btn_select_certificate"))
70  , m_xBtnSign(m_xBuilder->weld_button("ok"))
71  , m_xLabelHint(m_xBuilder->weld_label("label_hint"))
72  , m_xLabelHintText(m_xBuilder->weld_label("label_hint_text"))
73  , m_xLabelAddComment(m_xBuilder->weld_label("label_add_comment"))
74  , m_bShowSignDate(false)
75 {
76  Reference<container::XIndexAccess> xIndexAccess(m_xModel->getCurrentSelection(),
77  UNO_QUERY_THROW);
78  m_xShapeProperties.set(xIndexAccess->getByIndex(0), UNO_QUERY_THROW);
79 
80  bool bIsSignatureLine(false);
81  m_xShapeProperties->getPropertyValue("IsSignatureLine") >>= bIsSignatureLine;
82  if (!bIsSignatureLine)
83  {
84  SAL_WARN("cui.dialogs", "No signature line selected!");
85  return;
86  }
87 
88  m_xBtnLoadImage->connect_clicked(LINK(this, SignSignatureLineDialog, loadImage));
89  m_xBtnClearImage->connect_clicked(LINK(this, SignSignatureLineDialog, clearImage));
90  m_xBtnChooseCertificate->connect_clicked(
91  LINK(this, SignSignatureLineDialog, chooseCertificate));
92  m_xEditName->connect_changed(LINK(this, SignSignatureLineDialog, entryChanged));
93 
94  // Read properties from selected signature line
95  m_xShapeProperties->getPropertyValue("SignatureLineId") >>= m_aSignatureLineId;
96  m_xShapeProperties->getPropertyValue("SignatureLineSuggestedSignerName")
98  m_xShapeProperties->getPropertyValue("SignatureLineSuggestedSignerTitle")
100  OUString aSigningInstructions;
101  m_xShapeProperties->getPropertyValue("SignatureLineSigningInstructions")
102  >>= aSigningInstructions;
103  m_xShapeProperties->getPropertyValue("SignatureLineShowSignDate") >>= m_bShowSignDate;
104  bool bCanAddComment(false);
105  m_xShapeProperties->getPropertyValue("SignatureLineCanAddComment") >>= bCanAddComment;
106 
107  if (aSigningInstructions.isEmpty())
108  {
109  m_xLabelHint->hide();
110  m_xLabelHintText->hide();
111  }
112  else
113  {
114  m_xLabelHintText->set_label(aSigningInstructions);
115  }
116 
117  if (bCanAddComment)
118  {
119  m_xEditComment->set_size_request(m_xEditComment->get_approximate_digit_width() * 48,
120  m_xEditComment->get_text_height() * 5);
121  }
122  else
123  {
124  m_xLabelAddComment->hide();
125  m_xEditComment->hide();
126  m_xEditComment->set_size_request(0, 0);
127  }
128 
129  ValidateFields();
130 }
131 
133 {
135  Reference<XFilePicker3> xFilePicker
136  = FilePicker::createWithMode(xContext, TemplateDescription::FILEOPEN_PREVIEW);
137  if (!xFilePicker->execute())
138  return;
139 
140  Sequence<OUString> aSelectedFiles = xFilePicker->getSelectedFiles();
141  if (!aSelectedFiles.hasElements())
142  return;
143 
144  Reference<XGraphicProvider> xProvider = GraphicProvider::create(xContext);
145  Sequence<PropertyValue> aMediaProperties(1);
146  aMediaProperties[0].Name = "URL";
147  aMediaProperties[0].Value <<= aSelectedFiles[0];
148  m_xSignatureImage = xProvider->queryGraphic(aMediaProperties);
149  m_sOriginalImageBtnLabel = m_xBtnLoadImage->get_label();
150 
151  INetURLObject aObj(aSelectedFiles[0]);
152  m_xBtnLoadImage->set_label(aObj.GetLastName());
153 
154  ValidateFields();
155 }
156 
158 {
159  m_xSignatureImage.set(nullptr);
160  m_xBtnLoadImage->set_label(m_sOriginalImageBtnLabel);
161  ValidateFields();
162 }
163 
165 {
166  // Document needs to be saved before selecting a certificate
168  if (!pShell->PrepareForSigning(m_xDialog.get()))
169  return;
170 
171  Reference<XCertificate> xSignCertificate
173 
174  if (xSignCertificate.is())
175  {
176  m_xSelectedCertifate = xSignCertificate;
177  m_xBtnChooseCertificate->set_label(
178  svx::SignatureLineHelper::getSignerName(xSignCertificate));
179  }
180  ValidateFields();
181 }
182 
183 IMPL_LINK_NOARG(SignSignatureLineDialog, entryChanged, weld::Entry&, void) { ValidateFields(); }
184 
186 {
187  bool bEnableSignBtn = m_xSelectedCertifate.is()
188  && (!m_xEditName->get_text().isEmpty() || m_xSignatureImage.is());
189  m_xBtnSign->set_sensitive(bEnableSignBtn);
190 
191  m_xEditName->set_sensitive(!m_xSignatureImage.is());
192  m_xBtnLoadImage->set_sensitive(m_xEditName->get_text().isEmpty());
193  m_xBtnClearImage->set_sensitive(m_xSignatureImage.is());
194 }
195 
197 {
198  if (!m_xSelectedCertifate.is())
199  {
200  SAL_WARN("cui.dialogs", "No certificate selected!");
201  return;
202  }
203 
205  Reference<XGraphic> xValidGraphic = getSignedGraphic(true);
206  Reference<XGraphic> xInvalidGraphic = getSignedGraphic(false);
208  xValidGraphic, xInvalidGraphic, m_xEditComment->get_text());
209 }
210 
211 css::uno::Reference<css::graphic::XGraphic> SignSignatureLineDialog::getSignedGraphic(bool bValid)
212 {
213  // Read svg and replace placeholder texts
214  OUString aSvgImage(svx::SignatureLineHelper::getSignatureImage());
215  aSvgImage = aSvgImage.replaceAll("[SIGNER_NAME]", getCDataString(m_aSuggestedSignerName));
216  aSvgImage = aSvgImage.replaceAll("[SIGNER_TITLE]", getCDataString(m_aSuggestedSignerTitle));
217 
218  OUString aIssuerLine
219  = CuiResId(RID_SVXSTR_SIGNATURELINE_SIGNED_BY)
221  aSvgImage = aSvgImage.replaceAll("[SIGNED_BY]", getCDataString(aIssuerLine));
222  if (bValid)
223  aSvgImage = aSvgImage.replaceAll("[INVALID_SIGNATURE]", "");
224 
225  OUString aDate;
226  if (m_bShowSignDate && bValid)
227  {
229  }
230  aSvgImage = aSvgImage.replaceAll("[DATE]", aDate);
231 
232  // Custom signature image
233  if (m_xSignatureImage.is())
234  {
235  OUString aGraphicInBase64;
236  Graphic aGraphic(m_xSignatureImage);
237  if (!XOutBitmap::GraphicToBase64(aGraphic, aGraphicInBase64, false))
238  SAL_WARN("cui.dialogs", "Could not convert graphic to base64");
239 
240  OUString aImagePart = "<image y=\"825\" x=\"1300\" "
241  "xlink:href=\"data:[MIMETYPE];base64,[BASE64_IMG]>\" "
242  "preserveAspectRatio=\"xMidYMid\" height=\"1520\" "
243  "width=\"7600\" />";
244  aImagePart = aImagePart.replaceAll(
245  "[MIMETYPE]", GraphicMimeTypeHelper::GetMimeTypeForXGraphic(m_xSignatureImage));
246  aImagePart = aImagePart.replaceAll("[BASE64_IMG]", aGraphicInBase64);
247  aSvgImage = aSvgImage.replaceAll("[SIGNATURE_IMAGE]", aImagePart);
248 
249  aSvgImage = aSvgImage.replaceAll("[SIGNATURE]", "");
250  }
251  else
252  {
253  aSvgImage = aSvgImage.replaceAll("[SIGNATURE_IMAGE]", "");
254  aSvgImage = aSvgImage.replaceAll("[SIGNATURE]", getCDataString(m_xEditName->get_text()));
255  }
256 
257  // Create graphic
258  return svx::SignatureLineHelper::importSVG(aSvgImage);
259 }
260 
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
virtual void Apply() override
std::shared_ptr< weld::Dialog > m_xDialog
bool PrepareForSigning(weld::Window *pDialogParent)
static bool GraphicToBase64(const Graphic &rGraphic, OUString &rOUString, bool bAddPrefix=true, ConvertDataFormat aTargetFormat=ConvertDataFormat::Unknown)
OUString getSignatureImage(const OUString &rType=OUString())
css::uno::Reference< css::beans::XPropertySet > m_xShapeProperties
css::uno::Reference< css::graphic::XGraphic > m_xSignatureImage
OUString getSignerName(const css::uno::Reference< css::security::XCertificate > &xCertificate)
std::unique_ptr< weld::TextView > m_xEditComment
OUString GetLastName(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
static SfxObjectShell * Current()
std::unique_ptr< weld::Label > m_xLabelHint
css::uno::Reference< css::frame::XModel > m_xModel
uno::Reference< security::XCertificate > getSignatureCertificate(SfxObjectShell *pShell, weld::Window *pParent)
uno::Reference< graphic::XGraphic > importSVG(const OUString &rSVG)
std::unique_ptr< weld::Button > m_xBtnChooseCertificate
std::unique_ptr< weld::Button > m_xBtnClearImage
std::unique_ptr< weld::Button > m_xBtnLoadImage
css::uno::Reference< css::security::XCertificate > m_xSelectedCertifate
void SignSignatureLine(weld::Window *pDialogParent, const OUString &aSignatureLineId, const css::uno::Reference< css::security::XCertificate > &xCert, const css::uno::Reference< css::graphic::XGraphic > &xValidGraphic, const css::uno::Reference< css::graphic::XGraphic > &xInvalidGraphic, const OUString &aComment)
std::unique_ptr< weld::Button > m_xBtnSign
std::unique_ptr< weld::Label > m_xLabelAddComment
static OUString getCDataString(const OUString &rString)
OUString CuiResId(const char *pKey)
Definition: cuiresmgr.cxx:23
css::uno::Reference< css::graphic::XGraphic > getSignedGraphic(bool bValid)
Reference< XExecutableDialog > m_xDialog
IMPL_LINK_NOARG(SignSignatureLineDialog, loadImage, weld::Button &, void)
Reference< XComponentContext > getProcessComponentContext()
SignSignatureLineDialog(weld::Widget *pParent, css::uno::Reference< css::frame::XModel > xModel)
#define SAL_WARN(area, stream)
std::unique_ptr< weld::Entry > m_xEditName
std::unique_ptr< weld::Label > m_xLabelHintText