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/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
39using namespace comphelper;
40using namespace css;
41using namespace css::uno;
42using namespace css::beans;
43using namespace css::frame;
44using namespace css::io;
45using namespace css::lang;
46using namespace css::frame;
47using namespace css::text;
48using namespace css::graphic;
49using namespace css::security;
50using 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
120}
121
123{
124 Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
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 || !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(
170 }
171 ValidateFields();
172}
173
174IMPL_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 if (!pShell)
197 {
198 SAL_WARN("cui.dialogs", "No SfxObjectShell!");
199 return;
200 }
201
202 Reference<XGraphic> xValidGraphic = getSignedGraphic(true);
203 Reference<XGraphic> xInvalidGraphic = getSignedGraphic(false);
205 xValidGraphic, xInvalidGraphic, m_xEditComment->get_text());
206}
207
208css::uno::Reference<css::graphic::XGraphic> SignSignatureLineDialog::getSignedGraphic(bool bValid)
209{
210 // Read svg and replace placeholder texts
212 aSvgImage = aSvgImage.replaceAll("[SIGNER_NAME]", getCDataString(m_aSuggestedSignerName));
213 aSvgImage = aSvgImage.replaceAll("[SIGNER_TITLE]", getCDataString(m_aSuggestedSignerTitle));
214
215 OUString aIssuerLine
216 = CuiResId(RID_CUISTR_SIGNATURELINE_SIGNED_BY)
218 aSvgImage = aSvgImage.replaceAll("[SIGNED_BY]", getCDataString(aIssuerLine));
219 if (bValid)
220 aSvgImage = aSvgImage.replaceAll("[INVALID_SIGNATURE]", "");
221
222 OUString aDate;
223 if (m_bShowSignDate && bValid)
224 {
226 }
227 aSvgImage = aSvgImage.replaceAll("[DATE]", aDate);
228
229 // Custom signature image
230 if (m_xSignatureImage.is())
231 {
232 OUString aGraphicInBase64;
233 Graphic aGraphic(m_xSignatureImage);
234 if (!XOutBitmap::GraphicToBase64(aGraphic, aGraphicInBase64, false))
235 SAL_WARN("cui.dialogs", "Could not convert graphic to base64");
236
237 OUString aImagePart = "<image y=\"825\" x=\"1300\" "
238 "xlink:href=\"data:[MIMETYPE];base64,[BASE64_IMG]>\" "
239 "preserveAspectRatio=\"xMidYMid\" height=\"1520\" "
240 "width=\"7600\" />";
241 aImagePart = aImagePart.replaceAll(
242 "[MIMETYPE]", GraphicMimeTypeHelper::GetMimeTypeForXGraphic(m_xSignatureImage));
243 aImagePart = aImagePart.replaceAll("[BASE64_IMG]", aGraphicInBase64);
244 aSvgImage = aSvgImage.replaceAll("[SIGNATURE_IMAGE]", aImagePart);
245
246 aSvgImage = aSvgImage.replaceAll("[SIGNATURE]", "");
247 }
248 else
249 {
250 aSvgImage = aSvgImage.replaceAll("[SIGNATURE_IMAGE]", "");
251 aSvgImage = aSvgImage.replaceAll("[SIGNATURE]", getCDataString(m_xEditName->get_text()));
252 }
253
254 // Create graphic
255 return svx::SignatureLineHelper::importSVG(aSvgImage);
256}
257
258/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
IMPL_LINK_NOARG(SignSignatureLineDialog, loadImage, weld::Button &, void)
Reference< XExecutableDialog > m_xDialog
OUString GetLastName(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
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)
bool PrepareForSigning(weld::Window *pDialogParent)
static SAL_WARN_UNUSED_RESULT SfxObjectShell * Current()
css::uno::Reference< css::beans::XPropertySet > m_xShapeProperties
std::unique_ptr< weld::Button > m_xBtnSign
css::uno::Reference< css::graphic::XGraphic > getSignedGraphic(bool bValid)
SignSignatureLineDialog(weld::Widget *pParent, css::uno::Reference< css::frame::XModel > xModel)
std::unique_ptr< weld::Button > m_xBtnChooseCertificate
std::unique_ptr< weld::Button > m_xBtnClearImage
virtual void Apply() override
std::unique_ptr< weld::Label > m_xLabelAddComment
std::unique_ptr< weld::Entry > m_xEditName
std::unique_ptr< weld::Label > m_xLabelHint
css::uno::Reference< css::graphic::XGraphic > m_xSignatureImage
css::uno::Reference< css::security::XCertificate > m_xSelectedCertifate
std::unique_ptr< weld::Label > m_xLabelHintText
std::unique_ptr< weld::TextView > m_xEditComment
std::unique_ptr< weld::Button > m_xBtnLoadImage
static OUString getCDataString(std::u16string_view rString)
css::uno::Reference< css::frame::XModel > m_xModel
static bool GraphicToBase64(const Graphic &rGraphic, OUString &rOUString, bool bAddPrefix=true, ConvertDataFormat aTargetFormat=ConvertDataFormat::Unknown)
std::shared_ptr< weld::Dialog > m_xDialog
OUString CuiResId(TranslateId aKey)
Definition: cuiresmgr.cxx:23
Any aHelper
#define SAL_WARN(area, stream)
Reference< XComponentContext > getProcessComponentContext()
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
OUString getSignerName(const css::uno::Reference< css::security::XCertificate > &xCertificate)
uno::Reference< graphic::XGraphic > importSVG(std::u16string_view rSVG)
uno::Reference< security::XCertificate > getSignatureCertificate(SfxObjectShell *pShell, weld::Window *pParent)
OUString getSignatureImage(const OUString &rType=OUString())
Reference< XModel > xModel