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