bump product version to 6.3.0.0.beta1
[LibreOffice.git] / cui / source / dialogs / SignatureLineDialog.cxx
blob16f3b22ddd4ae8c457b1055c44a60b71b5b63137
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.
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 */
10 #include <SignatureLineDialog.hxx>
12 #include <comphelper/processfactory.hxx>
13 #include <comphelper/xmltools.hxx>
14 #include <tools/stream.hxx>
15 #include <unotools/streamwrap.hxx>
16 #include <utility>
17 #include <vcl/weld.hxx>
19 #include <com/sun/star/beans/XPropertySet.hpp>
20 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
21 #include <com/sun/star/drawing/XShape.hpp>
22 #include <com/sun/star/graphic/GraphicProvider.hpp>
23 #include <com/sun/star/graphic/XGraphic.hpp>
24 #include <com/sun/star/graphic/XGraphicProvider.hpp>
25 #include <com/sun/star/io/XInputStream.hpp>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <com/sun/star/sheet/XSpreadsheet.hpp>
29 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
30 #include <com/sun/star/sheet/XSpreadsheetView.hpp>
31 #include <com/sun/star/text/TextContentAnchorType.hpp>
32 #include <com/sun/star/text/XTextContent.hpp>
33 #include <com/sun/star/text/XTextDocument.hpp>
34 #include <com/sun/star/text/XTextViewCursor.hpp>
35 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
37 using namespace css;
38 using namespace css::uno;
39 using namespace css::beans;
40 using namespace css::container;
41 using namespace css::frame;
42 using namespace css::io;
43 using namespace css::lang;
44 using namespace css::frame;
45 using namespace css::sheet;
46 using namespace css::text;
47 using namespace css::drawing;
48 using namespace css::graphic;
50 SignatureLineDialog::SignatureLineDialog(weld::Widget* pParent, Reference<XModel> xModel,
51 bool bEditExisting)
52 : SignatureLineDialogBase(pParent, std::move(xModel), "cui/ui/signatureline.ui",
53 "SignatureLineDialog")
54 , m_xEditName(m_xBuilder->weld_entry("edit_name"))
55 , m_xEditTitle(m_xBuilder->weld_entry("edit_title"))
56 , m_xEditEmail(m_xBuilder->weld_entry("edit_email"))
57 , m_xEditInstructions(m_xBuilder->weld_text_view("edit_instructions"))
58 , m_xCheckboxCanAddComments(m_xBuilder->weld_check_button("checkbox_can_add_comments"))
59 , m_xCheckboxShowSignDate(m_xBuilder->weld_check_button("checkbox_show_sign_date"))
61 m_xEditInstructions->set_size_request(m_xEditInstructions->get_approximate_digit_width() * 48,
62 m_xEditInstructions->get_text_height() * 5);
64 // No signature line selected - start with empty dialog and set some default values
65 if (!bEditExisting)
67 m_xCheckboxCanAddComments->set_active(true);
68 m_xCheckboxShowSignDate->set_active(true);
69 return;
72 Reference<container::XIndexAccess> xIndexAccess(m_xModel->getCurrentSelection(),
73 UNO_QUERY_THROW);
74 Reference<XPropertySet> xProps(xIndexAccess->getByIndex(0), UNO_QUERY_THROW);
76 // Read properties from selected signature line
77 xProps->getPropertyValue("SignatureLineId") >>= m_aSignatureLineId;
78 OUString aSuggestedSignerName;
79 xProps->getPropertyValue("SignatureLineSuggestedSignerName") >>= aSuggestedSignerName;
80 m_xEditName->set_text(aSuggestedSignerName);
81 OUString aSuggestedSignerTitle;
82 xProps->getPropertyValue("SignatureLineSuggestedSignerTitle") >>= aSuggestedSignerTitle;
83 m_xEditTitle->set_text(aSuggestedSignerTitle);
84 OUString aSuggestedSignerEmail;
85 xProps->getPropertyValue("SignatureLineSuggestedSignerEmail") >>= aSuggestedSignerEmail;
86 m_xEditEmail->set_text(aSuggestedSignerEmail);
87 OUString aSigningInstructions;
88 xProps->getPropertyValue("SignatureLineSigningInstructions") >>= aSigningInstructions;
89 m_xEditInstructions->set_text(aSigningInstructions);
90 bool bCanAddComments = false;
91 xProps->getPropertyValue("SignatureLineCanAddComment") >>= bCanAddComments;
92 m_xCheckboxCanAddComments->set_active(bCanAddComments);
93 bool bShowSignDate = false;
94 xProps->getPropertyValue("SignatureLineShowSignDate") >>= bShowSignDate;
95 m_xCheckboxShowSignDate->set_active(bShowSignDate);
97 // Mark this as existing shape
98 m_xExistingShapeProperties = xProps;
101 void SignatureLineDialog::Apply()
103 if (m_aSignatureLineId.isEmpty())
104 m_aSignatureLineId
105 = OStringToOUString(comphelper::xml::generateGUIDString(), RTL_TEXTENCODING_ASCII_US);
106 OUString aSignerName(m_xEditName->get_text());
107 OUString aSignerTitle(m_xEditTitle->get_text());
108 OUString aSignerEmail(m_xEditEmail->get_text());
109 OUString aSigningInstructions(m_xEditInstructions->get_text());
110 bool bCanAddComments(m_xCheckboxCanAddComments->get_active());
111 bool bShowSignDate(m_xCheckboxShowSignDate->get_active());
113 // Read svg and replace placeholder texts
114 OUString aSvgImage(getSignatureImage());
115 aSvgImage = aSvgImage.replaceAll("[SIGNER_NAME]", getCDataString(aSignerName));
116 aSvgImage = aSvgImage.replaceAll("[SIGNER_TITLE]", getCDataString(aSignerTitle));
118 // These are only filled if the signature line is signed.
119 aSvgImage = aSvgImage.replaceAll("[SIGNATURE]", "");
120 aSvgImage = aSvgImage.replaceAll("[SIGNED_BY]", "");
121 aSvgImage = aSvgImage.replaceAll("[INVALID_SIGNATURE]", "");
122 aSvgImage = aSvgImage.replaceAll("[DATE]", "");
124 // Insert/Update graphic
125 SvMemoryStream aSvgStream(4096, 4096);
126 aSvgStream.WriteOString(OUStringToOString(aSvgImage, RTL_TEXTENCODING_UTF8));
127 Reference<XInputStream> xInputStream(new utl::OSeekableInputStreamWrapper(aSvgStream));
128 Reference<XComponentContext> xContext(comphelper::getProcessComponentContext());
129 Reference<XGraphicProvider> xProvider = css::graphic::GraphicProvider::create(xContext);
131 Sequence<PropertyValue> aMediaProperties(1);
132 aMediaProperties[0].Name = "InputStream";
133 aMediaProperties[0].Value <<= xInputStream;
134 Reference<XGraphic> xGraphic(xProvider->queryGraphic(aMediaProperties));
136 bool bIsExistingSignatureLine = m_xExistingShapeProperties.is();
137 Reference<XPropertySet> xShapeProps;
138 if (bIsExistingSignatureLine)
139 xShapeProps = m_xExistingShapeProperties;
140 else
141 xShapeProps.set(Reference<lang::XMultiServiceFactory>(m_xModel, UNO_QUERY_THROW)
142 ->createInstance("com.sun.star.drawing.GraphicObjectShape"),
143 UNO_QUERY);
145 xShapeProps->setPropertyValue("Graphic", Any(xGraphic));
146 xShapeProps->setPropertyValue("SignatureLineUnsignedImage", Any(xGraphic));
148 // Set signature line properties
149 xShapeProps->setPropertyValue("IsSignatureLine", Any(true));
150 xShapeProps->setPropertyValue("SignatureLineId", Any(m_aSignatureLineId));
151 if (!aSignerName.isEmpty())
152 xShapeProps->setPropertyValue("SignatureLineSuggestedSignerName", Any(aSignerName));
153 if (!aSignerTitle.isEmpty())
154 xShapeProps->setPropertyValue("SignatureLineSuggestedSignerTitle", Any(aSignerTitle));
155 if (!aSignerEmail.isEmpty())
156 xShapeProps->setPropertyValue("SignatureLineSuggestedSignerEmail", Any(aSignerEmail));
157 if (!aSigningInstructions.isEmpty())
158 xShapeProps->setPropertyValue("SignatureLineSigningInstructions",
159 Any(aSigningInstructions));
160 xShapeProps->setPropertyValue("SignatureLineShowSignDate", Any(bShowSignDate));
161 xShapeProps->setPropertyValue("SignatureLineCanAddComment", Any(bCanAddComments));
163 if (!bIsExistingSignatureLine)
165 // Default size
166 Reference<XShape> xShape(xShapeProps, UNO_QUERY);
167 awt::Size aShapeSize;
168 aShapeSize.Height = 3000;
169 aShapeSize.Width = 6000;
170 xShape->setSize(aShapeSize);
172 // Default anchoring
173 xShapeProps->setPropertyValue("AnchorType", Any(TextContentAnchorType_AT_PARAGRAPH));
175 const Reference<XServiceInfo> xServiceInfo(m_xModel, UNO_QUERY);
177 // Writer
178 const Reference<XTextDocument> xTextDocument(m_xModel, UNO_QUERY);
179 if (xTextDocument.is())
181 Reference<XTextContent> xTextContent(xShape, UNO_QUERY_THROW);
182 Reference<XTextViewCursorSupplier> xViewCursorSupplier(m_xModel->getCurrentController(),
183 UNO_QUERY_THROW);
184 Reference<XTextViewCursor> xCursor = xViewCursorSupplier->getViewCursor();
185 // use cursor's XText - it might be in table cell, frame, ...
186 Reference<XText> const xText(xCursor->getText());
187 assert(xText.is());
188 xText->insertTextContent(xCursor, xTextContent, true);
189 return;
192 // Calc
193 const Reference<XSpreadsheetDocument> xSpreadsheetDocument(m_xModel, UNO_QUERY);
194 if (xSpreadsheetDocument.is())
196 Reference<XPropertySet> xSheetCell(m_xModel->getCurrentSelection(), UNO_QUERY_THROW);
197 awt::Point aCellPosition;
198 xSheetCell->getPropertyValue("Position") >>= aCellPosition;
199 xShape->setPosition(aCellPosition);
201 Reference<XSpreadsheetView> xView(m_xModel->getCurrentController(), UNO_QUERY_THROW);
202 Reference<XSpreadsheet> xSheet(xView->getActiveSheet(), UNO_SET_THROW);
203 Reference<XDrawPageSupplier> xDrawPageSupplier(xSheet, UNO_QUERY_THROW);
204 Reference<XDrawPage> xDrawPage(xDrawPageSupplier->getDrawPage(), UNO_SET_THROW);
205 Reference<XShapes> xShapes(xDrawPage, UNO_QUERY_THROW);
207 xShapes->add(xShape);
208 return;
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */