LibreOffice Module sw (master)  1
mmgreetingspage.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include "mmgreetingspage.hxx"
21 #include <mailmergewizard.hxx>
22 #include <mmconfigitem.hxx>
23 #include "mmaddressblockpage.hxx"
24 #include <swtypes.hxx>
25 #include <dbui.hrc>
26 #include <com/sun/star/sdb/XColumn.hpp>
27 #include <com/sun/star/sdbc/SQLException.hpp>
28 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
29 #include <com/sun/star/container/XNameAccess.hpp>
30 #include <swmodule.hxx>
31 #include <view.hxx>
32 
33 using namespace svt;
34 using namespace ::com::sun::star;
35 using namespace ::com::sun::star::uno;
36 
38  SwMailMergeConfigItem const & rConfig,
40 {
41  const Sequence< OUString> rEntries = rConfig.GetGreetings(eType);
42  for(sal_Int32 nEntry = 0; nEntry < rEntries.getLength(); ++nEntry)
43  rBox.append_text(rEntries[nEntry]);
44  rBox.set_active(rConfig.GetCurrentGreeting(eType));
45 }
46 
47 static void lcl_StoreGreetingsBox(const weld::ComboBox& rBox,
48  SwMailMergeConfigItem& rConfig,
50 {
52  OUString* pEntries = aEntries.getArray();
53  for(sal_Int32 nEntry = 0; nEntry < rBox.get_count(); ++nEntry)
54  pEntries[nEntry] = rBox.get_text(nEntry);
55  rConfig.SetGreetings(eType, aEntries);
56  rConfig.SetCurrentGreeting(eType, rBox.get_active());
57 }
58 
60 {
61  bool bIndividual = m_xPersonalizedCB->get_sensitive() && m_xPersonalizedCB->get_active();
62  m_xFemaleFT->set_sensitive(bIndividual);
63  m_xFemaleLB->set_sensitive(bIndividual);
64  m_xFemalePB->set_sensitive(bIndividual);
65  m_xMaleFT->set_sensitive(bIndividual);
66  m_xMaleLB->set_sensitive(bIndividual);
67  m_xMalePB->set_sensitive(bIndividual);
68  m_xFemaleFI->set_sensitive(bIndividual);
69  m_xFemaleColumnFT->set_sensitive(bIndividual);
70  m_xFemaleColumnLB->set_sensitive(bIndividual);
71  m_xFemaleFieldFT->set_sensitive(bIndividual);
72  m_xFemaleFieldCB->set_sensitive(bIndividual);
73 
74  if( m_bIsTabPage )
75  {
76  m_rConfigItem.SetIndividualGreeting(bIndividual, false);
77  m_pWizard->UpdateRoadmap();
78  m_pWizard->enableButtons(WizardButtonFlags::NEXT, m_pWizard->isStateEnabled(MM_LAYOUTPAGE));
79  }
80  UpdatePreview();
81 }
82 
83 IMPL_LINK(SwGreetingsHandler, GreetingHdl_Impl, weld::Button&, rButton, void)
84 {
86  VclPtr<SwCustomizeAddressBlockDialog>::Create(nullptr /*TODO*/, m_rConfigItem,
87  &rButton == m_xMalePB.get() ?
90  if (RET_OK == pDlg->Execute())
91  {
92  weld::ComboBox* pToInsert = &rButton == m_xMalePB.get() ? m_xMaleLB.get() : m_xFemaleLB.get();
93  pToInsert->append_text(pDlg->GetAddress());
94  pToInsert->set_active(pToInsert->get_count() - 1);
95  if(m_bIsTabPage)
96  {
97  m_pWizard->UpdateRoadmap();
98  m_pWizard->enableButtons(WizardButtonFlags::NEXT, m_pWizard->isStateEnabled(MM_LAYOUTPAGE));
99  }
100  UpdatePreview();
101  }
102 }
103 
105 {
106  //the base class does nothing
107 }
108 
110 {
111  const OUString sPreview(m_xFemaleLB->get_active_text() + "\n" + m_xMaleLB->get_active_text());
112  SwAssignFieldsDialog aDlg(GetFrameWeld(), m_rConfigItem, sPreview, false);
113  if (RET_OK == aDlg.run())
114  {
115  UpdatePreview();
116  m_pWizard->UpdateRoadmap();
117  m_pWizard->enableButtons(WizardButtonFlags::NEXT, m_pWizard->isStateEnabled(MM_LAYOUTPAGE));
118  }
119 }
120 
121 IMPL_LINK_NOARG(SwMailMergeGreetingsPage, GreetingSelectListBoxHdl_Impl, weld::ComboBox&, void)
122 {
123  UpdatePreview();
124 }
125 
126 IMPL_LINK_NOARG(SwMailMergeGreetingsPage, GreetingSelectComboBoxHdl_Impl, weld::ComboBox&, void)
127 {
128  UpdatePreview();
129 }
130 
132 {
133  //find out which type of greeting should be selected:
134  bool bFemale = false;
135  bool bNoValue = !m_xFemaleColumnLB->get_sensitive();
136  if( !bNoValue )
137  {
138  const OUString sFemaleValue = m_xFemaleFieldCB->get_active_text();
139  const OUString sFemaleColumn = m_xFemaleColumnLB->get_active_text();
140  Reference< sdbcx::XColumnsSupplier > xColsSupp( m_rConfigItem.GetResultSet(), UNO_QUERY);
141  Reference < container::XNameAccess> xColAccess = xColsSupp.is() ? xColsSupp->getColumns() : nullptr;
142  if(!sFemaleValue.isEmpty() && !sFemaleColumn.isEmpty() &&
143  xColAccess.is() &&
144  xColAccess->hasByName(sFemaleColumn))
145  {
146  //get the content and exchange it in the address string
147  Any aCol = xColAccess->getByName(sFemaleColumn);
148  Reference< sdb::XColumn > xColumn;
149  aCol >>= xColumn;
150  if(xColumn.is())
151  {
152  try
153  {
154  bFemale = xColumn->getString() == sFemaleValue;
155 
156  //no last name value marks the greeting also as neutral
157  const OUString sLastNameColumn =
158  m_rConfigItem.GetAssignedColumn(MM_PART_LASTNAME);
159  if ( xColAccess->hasByName(sLastNameColumn) )
160  {
161  aCol = xColAccess->getByName(sLastNameColumn);
162  aCol >>= xColumn;
163  bNoValue = xColumn->getString().isEmpty();
164  }
165  }
166  catch (const sdbc::SQLException&)
167  {
168  OSL_FAIL("SQLException caught");
169  }
170  }
171  }
172  }
173 
174  OUString sPreview = bFemale ? m_xFemaleLB->get_active_text() :
175  bNoValue ? m_xNeutralCB->get_active_text() : m_xMaleLB->get_active_text();
176 
177  sPreview = SwAddressPreview::FillData(sPreview, m_rConfigItem);
178  m_xPreview->SetAddress(sPreview);
179 }
180 
181 void SwGreetingsHandler::Contains(bool bContainsGreeting)
182 {
183  m_xPersonalizedCB->set_sensitive(bContainsGreeting);
184  bool bEnablePersonal = bContainsGreeting && m_xPersonalizedCB->get_active();
185  m_xFemaleFT->set_sensitive(bEnablePersonal);
186  m_xFemaleLB->set_sensitive(bEnablePersonal);
187  m_xFemalePB->set_sensitive(bEnablePersonal);
188  m_xMaleFT->set_sensitive(bEnablePersonal);
189  m_xMaleLB->set_sensitive(bEnablePersonal);
190  m_xMalePB->set_sensitive(bEnablePersonal);
191  m_xFemaleFI->set_sensitive(bEnablePersonal);
192  m_xFemaleColumnFT->set_sensitive(bEnablePersonal);
193  m_xFemaleColumnLB->set_sensitive(bEnablePersonal);
194  m_xFemaleFieldFT->set_sensitive(bEnablePersonal);
195  m_xFemaleFieldCB->set_sensitive(bEnablePersonal);
196  m_xNeutralFT->set_sensitive(bContainsGreeting);
197  m_xNeutralCB->set_sensitive(bContainsGreeting);
198 }
199 
201  : svt::OWizardPage(pParent, "modules/swriter/ui/mmsalutationpage.ui", "MMSalutationPage")
202  , SwGreetingsHandler(pWizard->GetConfigItem(), *m_xBuilder)
203  , m_xPreview(new AddressPreview(m_xBuilder->weld_scrolled_window("previewwin")))
204  , m_xPreviewFI(m_xBuilder->weld_label("previewft"))
205  , m_xAssignPB(m_xBuilder->weld_button("assign"))
206  , m_xDocumentIndexFI(m_xBuilder->weld_label("documentindex"))
207  , m_xPrevSetIB(m_xBuilder->weld_button("prev"))
208  , m_xNextSetIB(m_xBuilder->weld_button("next"))
209  , m_xPreviewWIN(new weld::CustomWeld(*m_xBuilder, "preview", *m_xPreview))
210 {
211  m_pWizard = pWizard;
212 
213  Size aSize(m_xPreview->GetDrawingArea()->get_ref_device().LogicToPixel(Size(186, 21), MapMode(MapUnit::MapAppFont)));
214  m_xPreviewWIN->set_size_request(aSize.Width(), aSize.Height());
215  m_sDocument = m_xDocumentIndexFI->get_label();
216 
217  m_bIsTabPage = true;
218 
219  m_xGreetingLineCB->connect_toggled(LINK(this, SwMailMergeGreetingsPage, ContainsHdl_Impl));
220  Link<weld::ToggleButton&,void> aIndividualLink = LINK(this, SwGreetingsHandler, IndividualHdl_Impl);
221  m_xPersonalizedCB->connect_toggled(aIndividualLink);
222  Link<weld::Button&,void> aGreetingLink = LINK(this, SwGreetingsHandler, GreetingHdl_Impl);
223  m_xFemalePB->connect_clicked(aGreetingLink);
224  m_xMalePB->connect_clicked(aGreetingLink);
225  m_xAssignPB->connect_clicked(LINK(this, SwMailMergeGreetingsPage, AssignHdl_Impl));
226  Link<weld::ComboBox&,void> aLBoxLink2 = LINK(this, SwMailMergeGreetingsPage, GreetingSelectListBoxHdl_Impl);
227  m_xFemaleLB->connect_changed(aLBoxLink2);
228  m_xMaleLB->connect_changed(aLBoxLink2);
229  m_xFemaleColumnLB->connect_changed(aLBoxLink2);
230  m_xFemaleFieldCB->connect_changed(LINK(this, SwMailMergeGreetingsPage, GreetingSelectComboBoxHdl_Impl));
231  m_xNeutralCB->connect_changed(LINK(this, SwMailMergeGreetingsPage, GreetingSelectComboBoxHdl_Impl));
232 
233  Link<weld::Button&,void> aDataLink = LINK(this, SwMailMergeGreetingsPage, InsertDataHdl_Impl);
234  m_xPrevSetIB->connect_clicked(aDataLink);
235  m_xNextSetIB->connect_clicked(aDataLink);
236 
237  m_xGreetingLineCB->set_active(m_rConfigItem.IsGreetingLine(false));
239  ContainsHdl_Impl(*m_xGreetingLineCB);
240  aIndividualLink.Call(*m_xPersonalizedCB);
241 
245 
246  m_xDocumentIndexFI->set_label(m_sDocument.replaceFirst("%1", "1"));
247 }
248 
250 {
251  disposeOnce();
252 }
253 
255 {
256  m_xPreviewWIN.reset();
257  m_xPreview.reset();
259 }
260 
262 {
263  //try to find the gender setting
264  m_xFemaleColumnLB->clear();
265  Reference< sdbcx::XColumnsSupplier > xColsSupp = m_rConfigItem.GetColumnsSupplier();
266  if(xColsSupp.is())
267  {
268  Reference < container::XNameAccess> xColAccess = xColsSupp->getColumns();
269  Sequence< OUString > aColumns = xColAccess->getElementNames();
270  for(sal_Int32 nName = 0; nName < aColumns.getLength(); ++nName)
271  m_xFemaleColumnLB->append_text(aColumns[nName]);
272  }
273 
274  m_xFemaleColumnLB->set_active_text(m_rConfigItem.GetAssignedColumn(MM_PART_GENDER));
275  m_xFemaleColumnLB->save_value();
276 
278  m_xFemaleFieldCB->save_value();
279 
280  UpdatePreview();
281  m_pWizard->enableButtons(WizardButtonFlags::NEXT, m_pWizard->isStateEnabled(MM_LAYOUTPAGE));
282 }
283 
285 {
286  if (m_xFemaleColumnLB->get_value_changed_from_saved())
287  {
288  const SwDBData& rDBData = m_rConfigItem.GetCurrentDBData();
289  Sequence< OUString> aAssignment = m_rConfigItem.GetColumnAssignment( rDBData );
290  if(aAssignment.getLength() <= MM_PART_GENDER)
291  aAssignment.realloc(MM_PART_GENDER + 1);
292  aAssignment[MM_PART_GENDER] = m_xFemaleColumnLB->get_active_text();
293  m_rConfigItem.SetColumnAssignment( rDBData, aAssignment );
294  }
295  if (m_xFemaleFieldCB->get_value_changed_from_saved())
297 
300 
301  sal_Int32 nCurrentTextPos = m_xNeutralCB->find_text(m_xNeutralCB->get_active_text());
302  if (nCurrentTextPos == -1)
303  {
304  m_xNeutralCB->append_text(m_xNeutralCB->get_active_text());
305  m_xNeutralCB->set_active(m_xNeutralCB->get_count() - 1);
306  }
308  m_rConfigItem.SetGreetingLine(m_xGreetingLineCB->get_active(), false);
310  return true;
311 }
312 
313 IMPL_LINK(SwMailMergeGreetingsPage, ContainsHdl_Impl, weld::ToggleButton&, rBox, void)
314 {
315  bool bContainsGreeting = rBox.get_active();
316  SwGreetingsHandler::Contains(bContainsGreeting);
317  m_xPreviewFI->set_sensitive(bContainsGreeting);
318  m_xPreviewWIN->set_sensitive(bContainsGreeting);
319  m_xAssignPB->set_sensitive(bContainsGreeting);
320  m_xDocumentIndexFI->set_sensitive(bContainsGreeting);
321  m_xPrevSetIB->set_sensitive(bContainsGreeting);
322  m_xNextSetIB->set_sensitive(bContainsGreeting);
323  m_rConfigItem.SetGreetingLine(m_xGreetingLineCB->get_active(), false);
324  m_pWizard->UpdateRoadmap();
325  m_pWizard->enableButtons(WizardButtonFlags::NEXT, m_pWizard->isStateEnabled(MM_LAYOUTPAGE));
326 }
327 
328 IMPL_LINK(SwMailMergeGreetingsPage, InsertDataHdl_Impl, weld::Button&, rButton, void)
329 {
330  bool bNext = &rButton == m_xNextSetIB.get();
331  sal_Int32 nPos = m_rConfigItem.GetResultSetPosition();
332  m_rConfigItem.MoveResultSet( bNext ? ++nPos : --nPos);
333  nPos = m_rConfigItem.GetResultSetPosition();
334  bool bEnable = true;
335  if(nPos < 1)
336  {
337  bEnable = false;
338  nPos = 1;
339  }
340  else
341  UpdatePreview();
342  m_xPrevSetIB->set_sensitive(bEnable);
343  m_xNextSetIB->set_sensitive(bEnable);
344  m_xDocumentIndexFI->set_sensitive(bEnable);
345  m_xDocumentIndexFI->set_label(m_sDocument.replaceFirst("%1", OUString::number(nPos)));
346 }
347 
349  : SfxDialogController(pParent, "modules/swriter/ui/mmmailbody.ui", "MailBodyDialog")
350  , SwGreetingsHandler(*GetActiveView()->GetMailMergeConfigItem(), *m_xBuilder)
351  , m_xBodyFT(m_xBuilder->weld_label("bodyft"))
352  , m_xBodyMLE(m_xBuilder->weld_text_view("bodymle"))
353  , m_xOK(m_xBuilder->weld_button("ok"))
354 {
355  m_bIsTabPage = false;
356  m_xBodyMLE->set_size_request(m_xBodyMLE->get_approximate_digit_width() * 45,
357  m_xBodyMLE->get_height_rows(6));
358  m_xGreetingLineCB->connect_toggled(LINK(this, SwMailBodyDialog, ContainsHdl_Impl));
359  Link<weld::ToggleButton&,void> aIndividualLink = LINK(this, SwGreetingsHandler, IndividualHdl_Impl);
360  m_xPersonalizedCB->connect_toggled(aIndividualLink);
361  Link<weld::Button&,void> aGreetingLink = LINK(this, SwGreetingsHandler, GreetingHdl_Impl);
362  m_xFemalePB->connect_clicked(aGreetingLink);
363  m_xMalePB->connect_clicked(aGreetingLink);
364  m_xOK->connect_clicked(LINK(this, SwMailBodyDialog, OKHdl));
365 
368  ContainsHdl_Impl(*m_xGreetingLineCB);
369  aIndividualLink.Call(*m_xPersonalizedCB);
370 
374 
375  //try to find the gender setting
376  m_xFemaleColumnLB->clear();
377  Reference< sdbcx::XColumnsSupplier > xColsSupp = m_rConfigItem.GetColumnsSupplier();
378  if(xColsSupp.is())
379  {
380  Reference < container::XNameAccess> xColAccess = xColsSupp->getColumns();
381  Sequence< OUString > aColumns = xColAccess->getElementNames();
382  for(sal_Int32 nName = 0; nName < aColumns.getLength(); ++nName)
383  m_xFemaleColumnLB->append_text(aColumns[nName]);
384  }
385 
386  m_xFemaleColumnLB->set_active_text(m_rConfigItem.GetAssignedColumn(MM_PART_GENDER));
387  m_xFemaleColumnLB->save_value();
388 
390  m_xFemaleFieldCB->save_value();
391 }
392 
394 {
395 }
396 
397 IMPL_LINK(SwMailBodyDialog, ContainsHdl_Impl, weld::ToggleButton&, rBox, void)
398 {
399  SwGreetingsHandler::Contains(rBox.get_active());
400  m_rConfigItem.SetGreetingLine(rBox.get_active(), true);
401 }
402 
404 {
405  m_rConfigItem.SetGreetingLine(
406  m_xGreetingLineCB->get_active(), false);
407  m_rConfigItem.SetIndividualGreeting(
408  m_xPersonalizedCB->get_active(), false);
409 
410  if (m_xFemaleColumnLB->get_value_changed_from_saved())
411  {
412  const SwDBData& rDBData = m_rConfigItem.GetCurrentDBData();
413  Sequence< OUString> aAssignment = m_rConfigItem.GetColumnAssignment( rDBData );
414  sal_Int32 nPos = m_xFemaleColumnLB->get_active();
415  if(aAssignment.getLength() < MM_PART_GENDER)
416  aAssignment.realloc(MM_PART_GENDER);
417  if( nPos > 0 )
418  aAssignment[MM_PART_GENDER] = m_xFemaleColumnLB->get_active_text();
419  else
420  aAssignment[MM_PART_GENDER].clear();
421  m_rConfigItem.SetColumnAssignment( rDBData, aAssignment );
422  }
423  if (m_xFemaleFieldCB->get_value_changed_from_saved())
424  m_rConfigItem.SetFemaleGenderValue(m_xFemaleFieldCB->get_active_text());
425 
426  m_xDialog->response(RET_OK);
427 }
428 
429 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< weld::CheckButton > m_xGreetingLineCB
virtual void dispose() override
virtual void dispose() override
virtual ~SwMailBodyDialog() override
void SetGreetings(Gender eType, const css::uno::Sequence< OUString > &rBlocks)
std::unique_ptr< weld::Button > m_xPrevSetIB
virtual bool commitPage(::svt::WizardTypes::CommitPageReason _eReason) override
void SetGreetingLine(bool bSet, bool bInEMail)
SwView * GetActiveView()
Definition: swmodul1.cxx:123
std::unique_ptr< weld::Button > m_xMalePB
IMPL_LINK_NOARG(OCommonPicker, OnCancelPicker, void *, void)
void SetCurrentGreeting(Gender eType, sal_Int32 nIndex)
virtual void ActivatePage() override
virtual OUString get_text(int pos) const =0
void SetIndividualGreeting(bool bSet, bool bInEMail)
std::unique_ptr< weld::ComboBox > m_xMaleLB
const SwDBData & GetCurrentDBData() const
virtual int get_active() const =0
IMPL_LINK(SwGreetingsHandler, GreetingHdl_Impl, weld::Button &, rButton, void)
#define MM_LAYOUTPAGE
VclPtr< SwMailMergeWizard > m_pWizard
virtual int get_count() const =0
virtual ~SwMailMergeGreetingsPage() override
css::uno::Reference< css::sdbcx::XColumnsSupplier > const & GetColumnsSupplier()
std::unique_ptr< weld::Button > m_xFemalePB
static void lcl_FillGreetingsBox(weld::ComboBox &rBox, SwMailMergeConfigItem const &rConfig, SwMailMergeConfigItem::Gender eType)
void Contains(bool bContainsGreeting)
IMPL_LINK_NOARG(SwGreetingsHandler, IndividualHdl_Impl, weld::ToggleButton &, void)
std::unique_ptr< weld::ComboBox > m_xNeutralCB
std::unique_ptr< weld::TextView > m_xBodyMLE
SwMailMergeGreetingsPage(SwMailMergeWizard *pWizard, TabPageParent pParent)
void SetFemaleGenderValue(const OUString &rValue)
std::unique_ptr< weld::ComboBox > m_xFemaleFieldCB
IMPL_LINK(AsyncPickerAction, OnActionDone, void *, pEmptyArg, void)
std::unique_ptr< weld::Button > m_xNextSetIB
weld::Window * GetFrameWeld(SfxFrame *pFrame)
Definition: dialoghelp.cxx:20
virtual void set_active(int pos)=0
static void lcl_StoreGreetingsBox(const weld::ComboBox &rBox, SwMailMergeConfigItem &rConfig, SwMailMergeConfigItem::Gender eType)
std::unique_ptr< weld::Label > m_xDocumentIndexFI
bool isStateEnabled(WizardState _nState) const
void enableButtons(WizardButtonFlags _nWizardButtonFlags, bool _bEnable)
css::uno::Sequence< OUString > GetColumnAssignment(const SwDBData &rDBData) const
sal_Int32 GetCurrentGreeting(Gender eType) const
bool IsIndividualGreeting(bool bInEMail) const
std::unique_ptr< AddressPreview > m_xPreview
std::unique_ptr< weld::ComboBox > m_xFemaleLB
RET_OK
std::unique_ptr< weld::ComboBox > m_xFemaleColumnLB
OUString GetAssignedColumn(sal_uInt32 nColumn) const
std::unique_ptr< weld::Button > m_xAssignPB
bool IsGreetingLine(bool bInEMail) const
void SetColumnAssignment(const SwDBData &rDBData, const css::uno::Sequence< OUString > &)
std::unique_ptr< weld::Button > m_xOK
const OUString & GetFemaleGenderValue() const
std::unique_ptr< weld::CheckButton > m_xPersonalizedCB
ScXMLEditAttributeMap::Entry const aEntries[]
virtual void UpdatePreview()
void append_text(const OUString &rStr)
SwMailBodyDialog(weld::Window *pParent)
sal_Int32 nPos
static OUString FillData(const OUString &rAddress, SwMailMergeConfigItem const &rConfigItem, const css::uno::Sequence< OUString > *pAssignments=nullptr)
virtual void UpdatePreview() override
std::unique_ptr< weld::CustomWeld > m_xPreviewWIN
SwMailMergeConfigItem & m_rConfigItem
The mail merge state, available even when m_pWizard is nullptr.
const css::uno::Sequence< OUString > GetGreetings(Gender eType) const