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 
21 #include <sfx2/filedlghelper.hxx>
22 #include <sfx2/objsh.hxx>
23 #include <svx/xoutbmp.hxx>
24 #include <utility>
25 #include <vcl/graph.hxx>
26 #include <vcl/weld.hxx>
28 #include <tools/urlobj.hxx>
29 
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <com/sun/star/graphic/GraphicProvider.hpp>
32 #include <com/sun/star/graphic/XGraphic.hpp>
33 #include <com/sun/star/graphic/XGraphicProvider.hpp>
34 #include <com/sun/star/security/CertificateKind.hpp>
35 #include <com/sun/star/security/XCertificate.hpp>
36 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
37 #include <com/sun/star/ui/dialogs/XFilePicker3.hpp>
38 
39 using namespace comphelper;
40 using namespace css;
41 using namespace css::uno;
42 using namespace css::beans;
43 using namespace css::frame;
44 using namespace css::io;
45 using namespace css::lang;
46 using namespace css::frame;
47 using namespace css::text;
48 using namespace css::graphic;
49 using namespace css::security;
50 using namespace css::ui::dialogs;
51 
53  : SignatureLineDialogBase(pParent, std::move(xModel), "cui/ui/signsignatureline.ui",
54  "SignSignatureLineDialog")
55  , m_xEditName(m_xBuilder->weld_entry("edit_name"))
56  , m_xEditComment(m_xBuilder->weld_text_view("edit_comment"))
57  , m_xBtnLoadImage(m_xBuilder->weld_button("btn_load_image"))
58  , m_xBtnClearImage(m_xBuilder->weld_button("btn_clear_image"))
59  , m_xBtnChooseCertificate(m_xBuilder->weld_button("btn_select_certificate"))
60  , m_xBtnSign(m_xBuilder->weld_button("ok"))
61  , m_xLabelHint(m_xBuilder->weld_label("label_hint"))
62  , m_xLabelHintText(m_xBuilder->weld_label("label_hint_text"))
63  , m_xLabelAddComment(m_xBuilder->weld_label("label_add_comment"))
64  , m_bShowSignDate(false)
65 {
66  Reference<container::XIndexAccess> xIndexAccess(m_xModel->getCurrentSelection(),
67  UNO_QUERY_THROW);
68  m_xShapeProperties.set(xIndexAccess->getByIndex(0), UNO_QUERY_THROW);
69 
70  bool bIsSignatureLine(false);
71  m_xShapeProperties->getPropertyValue("IsSignatureLine") >>= bIsSignatureLine;
72  if (!bIsSignatureLine)
73  {
74  SAL_WARN("cui.dialogs", "No signature line selected!");
75  return;
76  }
77 
78  m_xBtnLoadImage->connect_clicked(LINK(this, SignSignatureLineDialog, loadImage));
79  m_xBtnClearImage->connect_clicked(LINK(this, SignSignatureLineDialog, clearImage));
80  m_xBtnChooseCertificate->connect_clicked(
81  LINK(this, SignSignatureLineDialog, chooseCertificate));
82  m_xEditName->connect_changed(LINK(this, SignSignatureLineDialog, entryChanged));
83 
84  // Read properties from selected signature line
85  m_xShapeProperties->getPropertyValue("SignatureLineId") >>= m_aSignatureLineId;
86  m_xShapeProperties->getPropertyValue("SignatureLineSuggestedSignerName")
88  m_xShapeProperties->getPropertyValue("SignatureLineSuggestedSignerTitle")
90  OUString aSigningInstructions;
91  m_xShapeProperties->getPropertyValue("SignatureLineSigningInstructions")
92  >>= aSigningInstructions;
93  m_xShapeProperties->getPropertyValue("SignatureLineShowSignDate") >>= m_bShowSignDate;
94  bool bCanAddComment(false);
95  m_xShapeProperties->getPropertyValue("SignatureLineCanAddComment") >>= bCanAddComment;
96 
97  if (aSigningInstructions.isEmpty())
98  {
99  m_xLabelHint->hide();
100  m_xLabelHintText->hide();
101  }
102  else
103  {
104  m_xLabelHintText->set_label(aSigningInstructions);
105  }
106 
107  if (bCanAddComment)
108  {
109  m_xEditComment->set_size_request(m_xEditComment->get_approximate_digit_width() * 48,
110  m_xEditComment->get_text_height() * 5);
111  }
112  else
113  {
114  m_xLabelAddComment->hide();
115  m_xEditComment->hide();
116  m_xEditComment->set_size_request(0, 0);
117  }
118 
119  ValidateFields();
120 }
121 
123 {
125  sfx2::FileDialogHelper aHelper(TemplateDescription::FILEOPEN_PREVIEW, FileDialogFlags::NONE,
126  m_xDialog.get());
128  Reference<XFilePicker3> xFilePicker = aHelper.GetFilePicker();
129  if (!xFilePicker->execute())
130  return;
131 
132  Sequence<OUString> aSelectedFiles = xFilePicker->getSelectedFiles();
133  if (!aSelectedFiles.hasElements())
134  return;
135 
136  Reference<XGraphicProvider> xProvider = GraphicProvider::create(xContext);
137  Sequence<PropertyValue> aMediaProperties{ comphelper::makePropertyValue("URL",
138  aSelectedFiles[0]) };
139  m_xSignatureImage = xProvider->queryGraphic(aMediaProperties);
140  m_sOriginalImageBtnLabel = m_xBtnLoadImage->get_label();
141 
142  INetURLObject aObj(aSelectedFiles[0]);
143  m_xBtnLoadImage->set_label(aObj.GetLastName());
144 
145  ValidateFields();
146 }
147 
149 {
150  m_xSignatureImage.set(nullptr);
151  m_xBtnLoadImage->set_label(m_sOriginalImageBtnLabel);
152  ValidateFields();
153 }
154 
156 {
157  // Document needs to be saved before selecting a certificate
159  if (!pShell->PrepareForSigning(m_xDialog.get()))
160  return;
161 
162  Reference<XCertificate> xSignCertificate
164 
165  if (xSignCertificate.is())
166  {
167  m_xSelectedCertifate = xSignCertificate;
168  m_xBtnChooseCertificate->set_label(
169  svx::SignatureLineHelper::getSignerName(xSignCertificate));
170  }
171  ValidateFields();
172 }
173 
174 IMPL_LINK_NOARG(SignSignatureLineDialog, entryChanged, weld::Entry&, void) { ValidateFields(); }
175 
177 {
178  bool bEnableSignBtn = m_xSelectedCertifate.is()
179  && (!m_xEditName->get_text().isEmpty() || m_xSignatureImage.is());
180  m_xBtnSign->set_sensitive(bEnableSignBtn);
181 
182  m_xEditName->set_sensitive(!m_xSignatureImage.is());
183  m_xBtnLoadImage->set_sensitive(m_xEditName->get_text().isEmpty());
184  m_xBtnClearImage->set_sensitive(m_xSignatureImage.is());
185 }
186 
188 {
189  if (!m_xSelectedCertifate.is())
190  {
191  SAL_WARN("cui.dialogs", "No certificate selected!");
192  return;
193  }
194 
196  Reference<XGraphic> xValidGraphic = getSignedGraphic(true);
197  Reference<XGraphic> xInvalidGraphic = getSignedGraphic(false);
199  xValidGraphic, xInvalidGraphic, m_xEditComment->get_text());
200 }
201 
202 css::uno::Reference<css::graphic::XGraphic> SignSignatureLineDialog::getSignedGraphic(bool bValid)
203 {
204  // Read svg and replace placeholder texts
205  OUString aSvgImage(svx::SignatureLineHelper::getSignatureImage());
206  aSvgImage = aSvgImage.replaceAll("[SIGNER_NAME]", getCDataString(m_aSuggestedSignerName));
207  aSvgImage = aSvgImage.replaceAll("[SIGNER_TITLE]", getCDataString(m_aSuggestedSignerTitle));
208 
209  OUString aIssuerLine
210  = CuiResId(RID_CUISTR_SIGNATURELINE_SIGNED_BY)
212  aSvgImage = aSvgImage.replaceAll("[SIGNED_BY]", getCDataString(aIssuerLine));
213  if (bValid)
214  aSvgImage = aSvgImage.replaceAll("[INVALID_SIGNATURE]", "");
215 
216  OUString aDate;
217  if (m_bShowSignDate && bValid)
218  {
220  }
221  aSvgImage = aSvgImage.replaceAll("[DATE]", aDate);
222 
223  // Custom signature image
224  if (m_xSignatureImage.is())
225  {
226  OUString aGraphicInBase64;
227  Graphic aGraphic(m_xSignatureImage);
228  if (!XOutBitmap::GraphicToBase64(aGraphic, aGraphicInBase64, false))
229  SAL_WARN("cui.dialogs", "Could not convert graphic to base64");
230 
231  OUString aImagePart = "<image y=\"825\" x=\"1300\" "
232  "xlink:href=\"data:[MIMETYPE];base64,[BASE64_IMG]>\" "
233  "preserveAspectRatio=\"xMidYMid\" height=\"1520\" "
234  "width=\"7600\" />";
235  aImagePart = aImagePart.replaceAll(
236  "[MIMETYPE]", GraphicMimeTypeHelper::GetMimeTypeForXGraphic(m_xSignatureImage));
237  aImagePart = aImagePart.replaceAll("[BASE64_IMG]", aGraphicInBase64);
238  aSvgImage = aSvgImage.replaceAll("[SIGNATURE_IMAGE]", aImagePart);
239 
240  aSvgImage = aSvgImage.replaceAll("[SIGNATURE]", "");
241  }
242  else
243  {
244  aSvgImage = aSvgImage.replaceAll("[SIGNATURE_IMAGE]", "");
245  aSvgImage = aSvgImage.replaceAll("[SIGNATURE]", getCDataString(m_xEditName->get_text()));
246  }
247 
248  // Create graphic
249  return svx::SignatureLineHelper::importSVG(aSvgImage);
250 }
251 
252 /* 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::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
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)
std::unique_ptr< weld::Button > m_xBtnChooseCertificate
std::unique_ptr< weld::Button > m_xBtnClearImage
std::unique_ptr< weld::Button > m_xBtnLoadImage
OUString CuiResId(TranslateId aKey)
Definition: cuiresmgr.cxx:23
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)
Any aHelper
std::unique_ptr< weld::Button > m_xBtnSign
uno::Reference< graphic::XGraphic > importSVG(std::u16string_view rSVG)
std::unique_ptr< weld::Label > m_xLabelAddComment
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
static OUString getCDataString(std::u16string_view rString)
bool m_bDetectedRangeSegmentation false