summaryrefslogtreecommitdiffstats
path: root/cui/source/dialogs/SignatureLineDialog.cxx
blob: 224dcdecd4bb730b9cd5bb9d60ac6ada0867467e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <SignatureLineDialog.hxx>

#include <comphelper/xmltools.hxx>
#include <utility>
#include <vcl/weld.hxx>
#include <svx/signaturelinehelper.hxx>

#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/drawing/XDrawPageSupplier.hpp>
#include <com/sun/star/drawing/XShape.hpp>
#include <com/sun/star/graphic/XGraphic.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/sheet/XSpreadsheet.hpp>
#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
#include <com/sun/star/sheet/XSpreadsheetView.hpp>
#include <com/sun/star/text/TextContentAnchorType.hpp>
#include <com/sun/star/text/XTextContent.hpp>
#include <com/sun/star/text/XTextDocument.hpp>
#include <com/sun/star/text/XTextViewCursor.hpp>
#include <com/sun/star/text/XTextViewCursorSupplier.hpp>

using namespace css;
using namespace css::uno;
using namespace css::beans;
using namespace css::container;
using namespace css::frame;
using namespace css::lang;
using namespace css::frame;
using namespace css::sheet;
using namespace css::text;
using namespace css::drawing;
using namespace css::graphic;

SignatureLineDialog::SignatureLineDialog(weld::Widget* pParent, Reference<XModel> xModel,
                                         bool bEditExisting)
    : SignatureLineDialogBase(pParent, std::move(xModel), "cui/ui/signatureline.ui",
                              "SignatureLineDialog")
    , m_xEditName(m_xBuilder->weld_entry("edit_name"))
    , m_xEditTitle(m_xBuilder->weld_entry("edit_title"))
    , m_xEditEmail(m_xBuilder->weld_entry("edit_email"))
    , m_xEditInstructions(m_xBuilder->weld_text_view("edit_instructions"))
    , m_xCheckboxCanAddComments(m_xBuilder->weld_check_button("checkbox_can_add_comments"))
    , m_xCheckboxShowSignDate(m_xBuilder->weld_check_button("checkbox_show_sign_date"))
{
    m_xEditInstructions->set_size_request(m_xEditInstructions->get_approximate_digit_width() * 48,
                                          m_xEditInstructions->get_text_height() * 5);

    // No signature line selected - start with empty dialog and set some default values
    if (!bEditExisting)
    {
        m_xCheckboxCanAddComments->set_active(true);
        m_xCheckboxShowSignDate->set_active(true);
        return;
    }

    Reference<container::XIndexAccess> xIndexAccess(m_xModel->getCurrentSelection(),
                                                    UNO_QUERY_THROW);
    Reference<XPropertySet> xProps(xIndexAccess->getByIndex(0), UNO_QUERY_THROW);

    // Read properties from selected signature line
    xProps->getPropertyValue("SignatureLineId") >>= m_aSignatureLineId;
    OUString aSuggestedSignerName;
    xProps->getPropertyValue("SignatureLineSuggestedSignerName") >>= aSuggestedSignerName;
    m_xEditName->set_text(aSuggestedSignerName);
    OUString aSuggestedSignerTitle;
    xProps->getPropertyValue("SignatureLineSuggestedSignerTitle") >>= aSuggestedSignerTitle;
    m_xEditTitle->set_text(aSuggestedSignerTitle);
    OUString aSuggestedSignerEmail;
    xProps->getPropertyValue("SignatureLineSuggestedSignerEmail") >>= aSuggestedSignerEmail;
    m_xEditEmail->set_text(aSuggestedSignerEmail);
    OUString aSigningInstructions;
    xProps->getPropertyValue("SignatureLineSigningInstructions") >>= aSigningInstructions;
    m_xEditInstructions->set_text(aSigningInstructions);
    bool bCanAddComments = false;
    xProps->getPropertyValue("SignatureLineCanAddComment") >>= bCanAddComments;
    m_xCheckboxCanAddComments->set_active(bCanAddComments);
    bool bShowSignDate = false;
    xProps->getPropertyValue("SignatureLineShowSignDate") >>= bShowSignDate;
    m_xCheckboxShowSignDate->set_active(bShowSignDate);

    // Mark this as existing shape
    m_xExistingShapeProperties = xProps;
}

void SignatureLineDialog::Apply()
{
    if (m_aSignatureLineId.isEmpty())
        m_aSignatureLineId
            = OStringToOUString(comphelper::xml::generateGUIDString(), RTL_TEXTENCODING_ASCII_US);
    OUString aSignerName(m_xEditName->get_text());
    OUString aSignerTitle(m_xEditTitle->get_text());
    OUString aSignerEmail(m_xEditEmail->get_text());
    OUString aSigningInstructions(m_xEditInstructions->get_text());
    bool bCanAddComments(m_xCheckboxCanAddComments->get_active());
    bool bShowSignDate(m_xCheckboxShowSignDate->get_active());

    // Read svg and replace placeholder texts
    OUString aSvgImage(svx::SignatureLineHelper::getSignatureImage());
    aSvgImage = aSvgImage.replaceAll("[SIGNER_NAME]", getCDataString(aSignerName));
    aSvgImage = aSvgImage.replaceAll("[SIGNER_TITLE]", getCDataString(aSignerTitle));

    // These are only filled if the signature line is signed.
    aSvgImage = aSvgImage.replaceAll("[SIGNATURE]", "");
    aSvgImage = aSvgImage.replaceAll("[SIGNED_BY]", "");
    aSvgImage = aSvgImage.replaceAll("[INVALID_SIGNATURE]", "");
    aSvgImage = aSvgImage.replaceAll("[DATE]", "");

    // Insert/Update graphic
    Reference<XGraphic> xGraphic = svx::SignatureLineHelper::importSVG(aSvgImage);

    bool bIsExistingSignatureLine = m_xExistingShapeProperties.is();
    Reference<XPropertySet> xShapeProps;
    if (bIsExistingSignatureLine)
        xShapeProps = m_xExistingShapeProperties;
    else
        xShapeProps.set(Reference<lang::XMultiServiceFactory>(m_xModel, UNO_QUERY_THROW)
                            ->createInstance("com.sun.star.drawing.GraphicObjectShape"),
                        UNO_QUERY);

    xShapeProps->setPropertyValue("Graphic", Any(xGraphic));
    xShapeProps->setPropertyValue("SignatureLineUnsignedImage", Any(xGraphic));

    // Set signature line properties
    xShapeProps->setPropertyValue("IsSignatureLine", Any(true));
    xShapeProps->setPropertyValue("SignatureLineId", Any(m_aSignatureLineId));
    if (!aSignerName.isEmpty())
        xShapeProps->setPropertyValue("SignatureLineSuggestedSignerName", Any(aSignerName));
    if (!aSignerTitle.isEmpty())
        xShapeProps->setPropertyValue("SignatureLineSuggestedSignerTitle", Any(aSignerTitle));
    if (!aSignerEmail.isEmpty())
        xShapeProps->setPropertyValue("SignatureLineSuggestedSignerEmail", Any(aSignerEmail));
    if (!aSigningInstructions.isEmpty())
        xShapeProps->setPropertyValue("SignatureLineSigningInstructions",
                                      Any(aSigningInstructions));
    xShapeProps->setPropertyValue("SignatureLineShowSignDate", Any(bShowSignDate));
    xShapeProps->setPropertyValue("SignatureLineCanAddComment", Any(bCanAddComments));

    if (bIsExistingSignatureLine)
        return;

    // Default size
    Reference<XShape> xShape(xShapeProps, UNO_QUERY);
    awt::Size aShapeSize;
    aShapeSize.Height = 3000;
    aShapeSize.Width = 6000;
    xShape->setSize(aShapeSize);

    // Default anchoring
    xShapeProps->setPropertyValue("AnchorType", Any(TextContentAnchorType_AT_PARAGRAPH));

    // Writer
    const Reference<XTextDocument> xTextDocument(m_xModel, UNO_QUERY);
    if (xTextDocument.is())
    {
        Reference<XTextContent> xTextContent(xShape, UNO_QUERY_THROW);
        Reference<XTextViewCursorSupplier> xViewCursorSupplier(m_xModel->getCurrentController(),
                                                               UNO_QUERY_THROW);
        Reference<XTextViewCursor> xCursor = xViewCursorSupplier->getViewCursor();
        // use cursor's XText - it might be in table cell, frame, ...
        Reference<XText> const xText(xCursor->getText());
        assert(xText.is());
        xText->insertTextContent(xCursor, xTextContent, true);
        return;
    }

    // Calc
    const Reference<XSpreadsheetDocument> xSpreadsheetDocument(m_xModel, UNO_QUERY);
    if (!xSpreadsheetDocument.is())
        return;

    Reference<XPropertySet> xSheetCell(m_xModel->getCurrentSelection(), UNO_QUERY_THROW);
    awt::Point aCellPosition;
    xSheetCell->getPropertyValue("Position") >>= aCellPosition;
    xShape->setPosition(aCellPosition);

    Reference<XSpreadsheetView> xView(m_xModel->getCurrentController(), UNO_QUERY_THROW);
    Reference<XSpreadsheet> xSheet(xView->getActiveSheet(), UNO_SET_THROW);
    Reference<XDrawPageSupplier> xDrawPageSupplier(xSheet, UNO_QUERY_THROW);
    Reference<XDrawPage> xDrawPage(xDrawPageSupplier->getDrawPage(), UNO_SET_THROW);
    Reference<XShapes> xShapes(xDrawPage, UNO_QUERY_THROW);

    xShapes->add(xShape);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */