1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "mmgreetingspage.hxx"
21 #include <mailmergewizard.hxx>
22 #include <mmconfigitem.hxx>
23 #include "mmaddressblockpage.hxx"
25 #include <com/sun/star/sdb/XColumn.hpp>
26 #include <com/sun/star/sdbc/SQLException.hpp>
27 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
28 #include <com/sun/star/container/XNameAccess.hpp>
29 #include <swmodule.hxx>
33 using namespace ::com::sun::star
;
34 using namespace ::com::sun::star::uno
;
36 static void lcl_FillGreetingsBox(weld::ComboBox
& rBox
,
37 SwMailMergeConfigItem
const & rConfig
,
38 SwMailMergeConfigItem::Gender eType
)
40 const Sequence
< OUString
> rEntries
= rConfig
.GetGreetings(eType
);
41 for(const auto& rEntry
: rEntries
)
42 rBox
.append_text(rEntry
);
43 rBox
.set_active(rConfig
.GetCurrentGreeting(eType
));
46 static void lcl_StoreGreetingsBox(const weld::ComboBox
& rBox
,
47 SwMailMergeConfigItem
& rConfig
,
48 SwMailMergeConfigItem::Gender eType
)
50 Sequence
< OUString
> aEntries(rBox
.get_count());
51 OUString
* pEntries
= aEntries
.getArray();
52 for(sal_Int32 nEntry
= 0; nEntry
< rBox
.get_count(); ++nEntry
)
53 pEntries
[nEntry
] = rBox
.get_text(nEntry
);
54 rConfig
.SetGreetings(eType
, aEntries
);
55 rConfig
.SetCurrentGreeting(eType
, rBox
.get_active());
58 IMPL_LINK_NOARG(SwGreetingsHandler
, IndividualHdl_Impl
, weld::ToggleButton
&, void)
60 bool bIndividual
= m_xPersonalizedCB
->get_sensitive() && m_xPersonalizedCB
->get_active();
61 m_xFemaleFT
->set_sensitive(bIndividual
);
62 m_xFemaleLB
->set_sensitive(bIndividual
);
63 m_xFemalePB
->set_sensitive(bIndividual
);
64 m_xMaleFT
->set_sensitive(bIndividual
);
65 m_xMaleLB
->set_sensitive(bIndividual
);
66 m_xMalePB
->set_sensitive(bIndividual
);
67 m_xFemaleFI
->set_sensitive(bIndividual
);
68 m_xFemaleColumnFT
->set_sensitive(bIndividual
);
69 m_xFemaleColumnLB
->set_sensitive(bIndividual
);
70 m_xFemaleFieldFT
->set_sensitive(bIndividual
);
71 m_xFemaleFieldCB
->set_sensitive(bIndividual
);
75 m_rConfigItem
.SetIndividualGreeting(bIndividual
, false);
76 m_pWizard
->UpdateRoadmap();
77 m_pWizard
->enableButtons(WizardButtonFlags::NEXT
, m_pWizard
->isStateEnabled(MM_LAYOUTPAGE
));
82 IMPL_LINK(SwGreetingsHandler
, GreetingHdl_Impl
, weld::Button
&, rButton
, void)
84 std::unique_ptr
<SwCustomizeAddressBlockDialog
> xDlg(new SwCustomizeAddressBlockDialog(&rButton
, m_rConfigItem
,
85 &rButton
== m_xMalePB
.get() ?
86 SwCustomizeAddressBlockDialog::GREETING_MALE
:
87 SwCustomizeAddressBlockDialog::GREETING_FEMALE
));
88 if (RET_OK
== xDlg
->run())
90 weld::ComboBox
* pToInsert
= &rButton
== m_xMalePB
.get() ? m_xMaleLB
.get() : m_xFemaleLB
.get();
91 pToInsert
->append_text(xDlg
->GetAddress());
92 pToInsert
->set_active(pToInsert
->get_count() - 1);
95 m_pWizard
->UpdateRoadmap();
96 m_pWizard
->enableButtons(WizardButtonFlags::NEXT
, m_pWizard
->isStateEnabled(MM_LAYOUTPAGE
));
102 void SwGreetingsHandler::UpdatePreview()
104 //the base class does nothing
107 IMPL_LINK_NOARG(SwMailMergeGreetingsPage
, AssignHdl_Impl
, weld::Button
&, void)
109 const OUString
sPreview(m_xFemaleLB
->get_active_text() + "\n" + m_xMaleLB
->get_active_text());
110 SwAssignFieldsDialog
aDlg(m_pWizard
->getDialog(), m_rConfigItem
, sPreview
, false);
111 if (RET_OK
== aDlg
.run())
114 m_pWizard
->UpdateRoadmap();
115 m_pWizard
->enableButtons(WizardButtonFlags::NEXT
, m_pWizard
->isStateEnabled(MM_LAYOUTPAGE
));
119 IMPL_LINK_NOARG(SwMailMergeGreetingsPage
, GreetingSelectListBoxHdl_Impl
, weld::ComboBox
&, void)
124 IMPL_LINK_NOARG(SwMailMergeGreetingsPage
, GreetingSelectComboBoxHdl_Impl
, weld::ComboBox
&, void)
129 void SwMailMergeGreetingsPage::UpdatePreview()
131 //find out which type of greeting should be selected:
132 bool bFemale
= false;
133 bool bNoValue
= !m_xFemaleColumnLB
->get_sensitive();
136 const OUString sFemaleValue
= m_xFemaleFieldCB
->get_active_text();
137 const OUString sFemaleColumn
= m_xFemaleColumnLB
->get_active_text();
138 Reference
< sdbcx::XColumnsSupplier
> xColsSupp( m_rConfigItem
.GetResultSet(), UNO_QUERY
);
139 Reference
< container::XNameAccess
> xColAccess
= xColsSupp
.is() ? xColsSupp
->getColumns() : nullptr;
140 if(!sFemaleValue
.isEmpty() && !sFemaleColumn
.isEmpty() &&
142 xColAccess
->hasByName(sFemaleColumn
))
144 //get the content and exchange it in the address string
145 Any aCol
= xColAccess
->getByName(sFemaleColumn
);
146 Reference
< sdb::XColumn
> xColumn
;
152 bFemale
= xColumn
->getString() == sFemaleValue
;
154 //no last name value marks the greeting also as neutral
155 const OUString sLastNameColumn
=
156 m_rConfigItem
.GetAssignedColumn(MM_PART_LASTNAME
);
157 if ( xColAccess
->hasByName(sLastNameColumn
) )
159 aCol
= xColAccess
->getByName(sLastNameColumn
);
161 bNoValue
= xColumn
->getString().isEmpty();
164 catch (const sdbc::SQLException
&)
166 OSL_FAIL("SQLException caught");
172 OUString sPreview
= bFemale
? m_xFemaleLB
->get_active_text() :
173 bNoValue
? m_xNeutralCB
->get_active_text() : m_xMaleLB
->get_active_text();
175 sPreview
= SwAddressPreview::FillData(sPreview
, m_rConfigItem
);
176 m_xPreview
->SetAddress(sPreview
);
179 void SwGreetingsHandler::Contains(bool bContainsGreeting
)
181 m_xPersonalizedCB
->set_sensitive(bContainsGreeting
);
182 bool bEnablePersonal
= bContainsGreeting
&& m_xPersonalizedCB
->get_active();
183 m_xFemaleFT
->set_sensitive(bEnablePersonal
);
184 m_xFemaleLB
->set_sensitive(bEnablePersonal
);
185 m_xFemalePB
->set_sensitive(bEnablePersonal
);
186 m_xMaleFT
->set_sensitive(bEnablePersonal
);
187 m_xMaleLB
->set_sensitive(bEnablePersonal
);
188 m_xMalePB
->set_sensitive(bEnablePersonal
);
189 m_xFemaleFI
->set_sensitive(bEnablePersonal
);
190 m_xFemaleColumnFT
->set_sensitive(bEnablePersonal
);
191 m_xFemaleColumnLB
->set_sensitive(bEnablePersonal
);
192 m_xFemaleFieldFT
->set_sensitive(bEnablePersonal
);
193 m_xFemaleFieldCB
->set_sensitive(bEnablePersonal
);
194 m_xNeutralFT
->set_sensitive(bContainsGreeting
);
195 m_xNeutralCB
->set_sensitive(bContainsGreeting
);
198 SwMailMergeGreetingsPage::SwMailMergeGreetingsPage(weld::Container
* pPage
, SwMailMergeWizard
* pWizard
)
199 : vcl::OWizardPage(pPage
, pWizard
, "modules/swriter/ui/mmsalutationpage.ui", "MMSalutationPage")
200 , SwGreetingsHandler(pWizard
->GetConfigItem(), *m_xBuilder
)
201 , m_xPreview(new SwAddressPreview(m_xBuilder
->weld_scrolled_window("previewwin", true)))
202 , m_xPreviewFI(m_xBuilder
->weld_label("previewft"))
203 , m_xAssignPB(m_xBuilder
->weld_button("assign"))
204 , m_xDocumentIndexFI(m_xBuilder
->weld_label("documentindex"))
205 , m_xPrevSetIB(m_xBuilder
->weld_button("prev"))
206 , m_xNextSetIB(m_xBuilder
->weld_button("next"))
207 , m_xPreviewWIN(new weld::CustomWeld(*m_xBuilder
, "preview", *m_xPreview
))
211 Size
aSize(m_xPreview
->GetDrawingArea()->get_ref_device().LogicToPixel(Size(186, 21), MapMode(MapUnit::MapAppFont
)));
212 m_xPreviewWIN
->set_size_request(aSize
.Width(), aSize
.Height());
213 m_sDocument
= m_xDocumentIndexFI
->get_label();
217 m_xGreetingLineCB
->connect_toggled(LINK(this, SwMailMergeGreetingsPage
, ContainsHdl_Impl
));
218 Link
<weld::ToggleButton
&,void> aIndividualLink
= LINK(this, SwGreetingsHandler
, IndividualHdl_Impl
);
219 m_xPersonalizedCB
->connect_toggled(aIndividualLink
);
220 Link
<weld::Button
&,void> aGreetingLink
= LINK(this, SwGreetingsHandler
, GreetingHdl_Impl
);
221 m_xFemalePB
->connect_clicked(aGreetingLink
);
222 m_xMalePB
->connect_clicked(aGreetingLink
);
223 m_xAssignPB
->connect_clicked(LINK(this, SwMailMergeGreetingsPage
, AssignHdl_Impl
));
224 Link
<weld::ComboBox
&,void> aLBoxLink2
= LINK(this, SwMailMergeGreetingsPage
, GreetingSelectListBoxHdl_Impl
);
225 m_xFemaleLB
->connect_changed(aLBoxLink2
);
226 m_xMaleLB
->connect_changed(aLBoxLink2
);
227 m_xFemaleColumnLB
->connect_changed(aLBoxLink2
);
228 m_xFemaleFieldCB
->connect_changed(LINK(this, SwMailMergeGreetingsPage
, GreetingSelectComboBoxHdl_Impl
));
229 m_xNeutralCB
->connect_changed(LINK(this, SwMailMergeGreetingsPage
, GreetingSelectComboBoxHdl_Impl
));
231 Link
<weld::Button
&,void> aDataLink
= LINK(this, SwMailMergeGreetingsPage
, InsertDataHdl_Impl
);
232 m_xPrevSetIB
->connect_clicked(aDataLink
);
233 m_xNextSetIB
->connect_clicked(aDataLink
);
235 m_xGreetingLineCB
->set_active(m_rConfigItem
.IsGreetingLine(false));
236 m_xPersonalizedCB
->set_active(m_rConfigItem
.IsIndividualGreeting(false));
237 ContainsHdl_Impl(*m_xGreetingLineCB
);
238 aIndividualLink
.Call(*m_xPersonalizedCB
);
240 lcl_FillGreetingsBox(*m_xFemaleLB
, m_rConfigItem
, SwMailMergeConfigItem::FEMALE
);
241 lcl_FillGreetingsBox(*m_xMaleLB
, m_rConfigItem
, SwMailMergeConfigItem::MALE
);
242 lcl_FillGreetingsBox(*m_xNeutralCB
, m_rConfigItem
, SwMailMergeConfigItem::NEUTRAL
);
244 m_xDocumentIndexFI
->set_label(m_sDocument
.replaceFirst("%1", "1"));
247 SwMailMergeGreetingsPage::~SwMailMergeGreetingsPage()
249 m_xPreviewWIN
.reset();
253 void SwMailMergeGreetingsPage::Activate()
255 //try to find the gender setting
256 m_xFemaleColumnLB
->clear();
257 Reference
< sdbcx::XColumnsSupplier
> xColsSupp
= m_rConfigItem
.GetColumnsSupplier();
260 Reference
< container::XNameAccess
> xColAccess
= xColsSupp
->getColumns();
261 const Sequence
< OUString
> aColumns
= xColAccess
->getElementNames();
262 for(const auto& rColumn
: aColumns
)
263 m_xFemaleColumnLB
->append_text(rColumn
);
266 m_xFemaleColumnLB
->set_active_text(m_rConfigItem
.GetAssignedColumn(MM_PART_GENDER
));
267 m_xFemaleColumnLB
->save_value();
269 m_xFemaleFieldCB
->set_entry_text(m_rConfigItem
.GetFemaleGenderValue());
270 m_xFemaleFieldCB
->save_value();
273 m_pWizard
->enableButtons(WizardButtonFlags::NEXT
, m_pWizard
->isStateEnabled(MM_LAYOUTPAGE
));
276 bool SwMailMergeGreetingsPage::commitPage( ::vcl::WizardTypes::CommitPageReason
)
278 if (m_xFemaleColumnLB
->get_value_changed_from_saved())
280 const SwDBData
& rDBData
= m_rConfigItem
.GetCurrentDBData();
281 Sequence
< OUString
> aAssignment
= m_rConfigItem
.GetColumnAssignment( rDBData
);
282 if(aAssignment
.getLength() <= MM_PART_GENDER
)
283 aAssignment
.realloc(MM_PART_GENDER
+ 1);
284 aAssignment
[MM_PART_GENDER
] = m_xFemaleColumnLB
->get_active_text();
285 m_rConfigItem
.SetColumnAssignment( rDBData
, aAssignment
);
287 if (m_xFemaleFieldCB
->get_value_changed_from_saved())
288 m_rConfigItem
.SetFemaleGenderValue(m_xFemaleFieldCB
->get_active_text());
290 lcl_StoreGreetingsBox(*m_xFemaleLB
, m_rConfigItem
, SwMailMergeConfigItem::FEMALE
);
291 lcl_StoreGreetingsBox(*m_xMaleLB
, m_rConfigItem
, SwMailMergeConfigItem::MALE
);
293 sal_Int32 nCurrentTextPos
= m_xNeutralCB
->find_text(m_xNeutralCB
->get_active_text());
294 if (nCurrentTextPos
== -1)
296 m_xNeutralCB
->append_text(m_xNeutralCB
->get_active_text());
297 m_xNeutralCB
->set_active(m_xNeutralCB
->get_count() - 1);
299 lcl_StoreGreetingsBox(*m_xNeutralCB
, m_rConfigItem
, SwMailMergeConfigItem::NEUTRAL
);
300 m_rConfigItem
.SetGreetingLine(m_xGreetingLineCB
->get_active(), false);
301 m_rConfigItem
.SetIndividualGreeting(m_xPersonalizedCB
->get_active(), false);
305 IMPL_LINK(SwMailMergeGreetingsPage
, ContainsHdl_Impl
, weld::ToggleButton
&, rBox
, void)
307 bool bContainsGreeting
= rBox
.get_active();
308 SwGreetingsHandler::Contains(bContainsGreeting
);
309 m_xPreviewFI
->set_sensitive(bContainsGreeting
);
310 m_xPreviewWIN
->set_sensitive(bContainsGreeting
);
311 m_xAssignPB
->set_sensitive(bContainsGreeting
);
312 m_xDocumentIndexFI
->set_sensitive(bContainsGreeting
);
313 m_xPrevSetIB
->set_sensitive(bContainsGreeting
);
314 m_xNextSetIB
->set_sensitive(bContainsGreeting
);
315 m_rConfigItem
.SetGreetingLine(m_xGreetingLineCB
->get_active(), false);
316 m_pWizard
->UpdateRoadmap();
317 m_pWizard
->enableButtons(WizardButtonFlags::NEXT
, m_pWizard
->isStateEnabled(MM_LAYOUTPAGE
));
320 IMPL_LINK(SwMailMergeGreetingsPage
, InsertDataHdl_Impl
, weld::Button
&, rButton
, void)
322 bool bNext
= &rButton
== m_xNextSetIB
.get();
323 sal_Int32 nPos
= m_rConfigItem
.GetResultSetPosition();
324 m_rConfigItem
.MoveResultSet( bNext
? ++nPos
: --nPos
);
325 nPos
= m_rConfigItem
.GetResultSetPosition();
334 m_xPrevSetIB
->set_sensitive(bEnable
);
335 m_xNextSetIB
->set_sensitive(bEnable
);
336 m_xDocumentIndexFI
->set_sensitive(bEnable
);
337 m_xDocumentIndexFI
->set_label(m_sDocument
.replaceFirst("%1", OUString::number(nPos
)));
340 SwMailBodyDialog::SwMailBodyDialog(weld::Window
* pParent
)
341 : SfxDialogController(pParent
, "modules/swriter/ui/mmmailbody.ui", "MailBodyDialog")
342 , SwGreetingsHandler(*GetActiveView()->GetMailMergeConfigItem(), *m_xBuilder
)
343 , m_xBodyFT(m_xBuilder
->weld_label("bodyft"))
344 , m_xBodyMLE(m_xBuilder
->weld_text_view("bodymle"))
345 , m_xOK(m_xBuilder
->weld_button("ok"))
347 m_bIsTabPage
= false;
348 m_xBodyMLE
->set_size_request(m_xBodyMLE
->get_approximate_digit_width() * 45,
349 m_xBodyMLE
->get_height_rows(6));
350 m_xGreetingLineCB
->connect_toggled(LINK(this, SwMailBodyDialog
, ContainsHdl_Impl
));
351 Link
<weld::ToggleButton
&,void> aIndividualLink
= LINK(this, SwGreetingsHandler
, IndividualHdl_Impl
);
352 m_xPersonalizedCB
->connect_toggled(aIndividualLink
);
353 Link
<weld::Button
&,void> aGreetingLink
= LINK(this, SwGreetingsHandler
, GreetingHdl_Impl
);
354 m_xFemalePB
->connect_clicked(aGreetingLink
);
355 m_xMalePB
->connect_clicked(aGreetingLink
);
356 m_xOK
->connect_clicked(LINK(this, SwMailBodyDialog
, OKHdl
));
358 m_xGreetingLineCB
->set_active(m_rConfigItem
.IsGreetingLine(true));
359 m_xPersonalizedCB
->set_active(m_rConfigItem
.IsIndividualGreeting(true));
360 ContainsHdl_Impl(*m_xGreetingLineCB
);
361 aIndividualLink
.Call(*m_xPersonalizedCB
);
363 lcl_FillGreetingsBox(*m_xFemaleLB
, m_rConfigItem
, SwMailMergeConfigItem::FEMALE
);
364 lcl_FillGreetingsBox(*m_xMaleLB
, m_rConfigItem
, SwMailMergeConfigItem::MALE
);
365 lcl_FillGreetingsBox(*m_xNeutralCB
, m_rConfigItem
, SwMailMergeConfigItem::NEUTRAL
);
367 //try to find the gender setting
368 m_xFemaleColumnLB
->clear();
369 Reference
< sdbcx::XColumnsSupplier
> xColsSupp
= m_rConfigItem
.GetColumnsSupplier();
372 Reference
< container::XNameAccess
> xColAccess
= xColsSupp
->getColumns();
373 const Sequence
< OUString
> aColumns
= xColAccess
->getElementNames();
374 for(const auto& rColumn
: aColumns
)
375 m_xFemaleColumnLB
->append_text(rColumn
);
378 m_xFemaleColumnLB
->set_active_text(m_rConfigItem
.GetAssignedColumn(MM_PART_GENDER
));
379 m_xFemaleColumnLB
->save_value();
381 m_xFemaleFieldCB
->set_entry_text(m_rConfigItem
.GetFemaleGenderValue());
382 m_xFemaleFieldCB
->save_value();
385 SwMailBodyDialog::~SwMailBodyDialog()
389 IMPL_LINK(SwMailBodyDialog
, ContainsHdl_Impl
, weld::ToggleButton
&, rBox
, void)
391 SwGreetingsHandler::Contains(rBox
.get_active());
392 m_rConfigItem
.SetGreetingLine(rBox
.get_active(), true);
395 IMPL_LINK_NOARG(SwMailBodyDialog
, OKHdl
, weld::Button
&, void)
397 m_rConfigItem
.SetGreetingLine(
398 m_xGreetingLineCB
->get_active(), false);
399 m_rConfigItem
.SetIndividualGreeting(
400 m_xPersonalizedCB
->get_active(), false);
402 if (m_xFemaleColumnLB
->get_value_changed_from_saved())
404 const SwDBData
& rDBData
= m_rConfigItem
.GetCurrentDBData();
405 Sequence
< OUString
> aAssignment
= m_rConfigItem
.GetColumnAssignment( rDBData
);
406 sal_Int32 nPos
= m_xFemaleColumnLB
->get_active();
407 if(aAssignment
.getLength() < MM_PART_GENDER
)
408 aAssignment
.realloc(MM_PART_GENDER
);
410 aAssignment
[MM_PART_GENDER
] = m_xFemaleColumnLB
->get_active_text();
412 aAssignment
[MM_PART_GENDER
].clear();
413 m_rConfigItem
.SetColumnAssignment( rDBData
, aAssignment
);
415 if (m_xFemaleFieldCB
->get_value_changed_from_saved())
416 m_rConfigItem
.SetFemaleGenderValue(m_xFemaleFieldCB
->get_active_text());
418 m_xDialog
->response(RET_OK
);
421 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */