LibreOffice Module sw (master)  1
mailmergewizard.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 <mailmergewizard.hxx>
21 #include "mmdocselectpage.hxx"
22 #include "mmoutputtypepage.hxx"
23 #include "mmaddressblockpage.hxx"
24 #include "mmgreetingspage.hxx"
25 #include "mmlayoutpage.hxx"
26 #include <mmconfigitem.hxx>
27 #include <swabstdlg.hxx>
28 #include <strings.hrc>
29 #include <view.hxx>
30 
31 #include <helpids.h>
32 
33 using namespace svt;
34 using namespace ::com::sun::star;
35 
36 SwMailMergeWizard::SwMailMergeWizard(SwView& rView, std::shared_ptr<SwMailMergeConfigItem> const & rItem)
37  : RoadmapWizardMachine(rView.GetFrameWeld())
38  , m_pSwView(&rView)
39  , m_bDocumentLoad(false)
40  , m_xConfigItem(rItem)
41  , m_sStarting(SwResId(ST_STARTING))
42  , m_sDocumentType(SwResId(ST_DOCUMENTTYPE))
43  , m_sAddressBlock(SwResId(ST_ADDRESSBLOCK))
44  , m_sAddressList(SwResId(ST_ADDRESSLIST))
45  , m_sGreetingsLine(SwResId(ST_GREETINGSLINE))
46  , m_sLayout(SwResId(ST_LAYOUT))
47  , m_nRestartPage(MM_DOCUMENTSELECTPAGE)
48 {
49  defaultButton(WizardButtonFlags::NEXT);
50  enableButtons(WizardButtonFlags::FINISH, false);
51 
52  setTitleBase(SwResId(ST_MMWTITLE));
53 
54  m_xFinish->set_label(SwResId( ST_FINISH ));
55  m_xNextPage->set_help_id(HID_MM_NEXT_PAGE);
56  m_xPrevPage->set_help_id(HID_MM_PREV_PAGE);
57 
58  //#i51949# no output type page visible if e-Mail is not supported
59  if (m_xConfigItem->IsMailAvailable())
61  0,
67  );
68  else
70  0,
75  );
76 
77  ActivatePage();
78  m_xAssistant->set_current_page(0);
79  UpdateRoadmap();
80 }
81 
83 {
84 }
85 
86 std::unique_ptr<BuilderPage> SwMailMergeWizard::createPage(WizardState _nState)
87 {
88  OString sIdent(OString::number(_nState));
89  weld::Container* pPageContainer = m_xAssistant->append_page(sIdent);
90 
91  std::unique_ptr<vcl::OWizardPage> xRet;
92  switch(_nState)
93  {
95  xRet = std::make_unique<SwMailMergeDocSelectPage>(pPageContainer, this);
96 
97  /* tdf#52986 Set help ID using SetRoadmapHelpId for all pages
98  so that when by default the focus is on the left side pane of
99  the wizard the relevant help page is displayed when hitting
100  the Help / F1 button */
101  SetRoadmapHelpId("modules/swriter/ui/mmselectpage/MMSelectPage");
102  break;
103  case MM_OUTPUTTYPETPAGE :
104  xRet = std::make_unique<SwMailMergeOutputTypePage>(pPageContainer, this);
105  SetRoadmapHelpId("modules/swriter/ui/mmoutputtypepage/MMOutputTypePage");
106  break;
107  case MM_ADDRESSBLOCKPAGE :
108  xRet = std::make_unique<SwMailMergeAddressBlockPage>(pPageContainer, this);
109  SetRoadmapHelpId("modules/swriter/ui/mmaddressblockpage/MMAddressBlockPage");
110  break;
111  case MM_GREETINGSPAGE :
112  xRet = std::make_unique<SwMailMergeGreetingsPage>(pPageContainer, this);
113  SetRoadmapHelpId("modules/swriter/ui/mmsalutationpage/MMSalutationPage");
114  break;
115  case MM_LAYOUTPAGE :
116  xRet = std::make_unique<SwMailMergeLayoutPage>(pPageContainer, this);
117  SetRoadmapHelpId("modules/swriter/ui/mmlayoutpage/MMLayoutPage");
118  break;
119  }
120 
121  m_xAssistant->set_page_title(sIdent, getStateDisplayName(_nState));
122 
123  OSL_ENSURE(xRet, "no page created in ::createPage");
124  return xRet;
125 }
126 
128 {
130 
131  if (m_xConfigItem->GetTargetView())
132  {
133  //close the dialog, remove the target view, show the source view
134  m_nRestartPage = _nState;
135  //set ResultSet back to start
136  m_xConfigItem->MoveResultSet(1);
137  m_xAssistant->response(RET_REMOVE_TARGET);
138  return;
139  }
140  bool bEnablePrev = true;
141  bool bEnableNext = true;
142  switch(_nState)
143  {
145  {
146  bEnablePrev = false; // the first page
147 
148  OUString sDataSourceName = GetSwView()->GetDataSourceName();
149  if(!sDataSourceName.isEmpty() &&
150  !SwView::IsDataSourceAvailable(sDataSourceName))
151  {
152  bEnableNext = false;
153  }
154  }
155  break;
156  case MM_ADDRESSBLOCKPAGE :
157  bEnableNext = m_xConfigItem->GetResultSet().is();
158  break;
159  case MM_LAYOUTPAGE:
160  bEnableNext = false; // the last page
161  break;
162  }
163  enableButtons( WizardButtonFlags::PREVIOUS, bEnablePrev);
164  enableButtons( WizardButtonFlags::NEXT, bEnableNext);
165 
166  UpdateRoadmap();
167 }
168 
170 {
171  switch(_nState)
172  {
174  return m_sStarting;
175  case MM_OUTPUTTYPETPAGE:
176  return m_sDocumentType;
177  case MM_ADDRESSBLOCKPAGE:
178  return m_xConfigItem->IsOutputToLetter() ?
180  case MM_GREETINGSPAGE:
181  return m_sGreetingsLine;
182  case MM_LAYOUTPAGE:
183  return m_sLayout;
184  }
185  return OUString();
186 }
187 
188 // enables/disables pages in the roadmap depending on the current page and state
190 {
191 /*
192  MM_DOCUMENTSELECTPAGE > inactive after the layoutpage
193  MM_OUTPUTTYPETPAGE : > inactive after the layoutpage
194  MM_ADDRESSBLOCKPAGE > inactive after the layoutpage
195  MM_GREETINGSPAGE > inactive after the layoutpage
196  MM_LAYOUTPAGE > inactive after the layoutpage
197  inactive if address block and greeting are switched off
198  or are already inserted into the source document
199 */
200 
201  // enableState( <page id>, false );
202  const sal_uInt16 nCurPage = m_xAssistant->get_current_page();
203  BuilderPage* pCurPage = GetPage( nCurPage );
204  if(!pCurPage)
205  return;
206  bool bAddressFieldsConfigured = !m_xConfigItem->IsOutputToLetter() ||
207  !m_xConfigItem->IsAddressBlock() ||
208  m_xConfigItem->IsAddressFieldsAssigned();
209  bool bGreetingFieldsConfigured = !m_xConfigItem->IsGreetingLine(false) ||
210  !m_xConfigItem->IsIndividualGreeting(false) ||
211  m_xConfigItem->IsGreetingFieldsAssigned();
212 
213  //#i97436# if a document has to be loaded then enable output type page only
214  m_bDocumentLoad = false;
215  bool bEnableOutputTypePage = (nCurPage != MM_DOCUMENTSELECTPAGE) ||
216  static_cast<vcl::OWizardPage*>(pCurPage)->commitPage( ::vcl::WizardTypes::eValidate );
217 
218  // handle the Finish button
219  bool bCanFinish = !m_bDocumentLoad && bEnableOutputTypePage &&
220  m_xConfigItem->GetResultSet().is() &&
221  bAddressFieldsConfigured &&
222  bGreetingFieldsConfigured;
223  enableButtons(WizardButtonFlags::FINISH, (nCurPage != MM_DOCUMENTSELECTPAGE) && bCanFinish);
224 
225  for(sal_uInt16 nPage = MM_DOCUMENTSELECTPAGE; nPage <= MM_LAYOUTPAGE; ++nPage)
226  {
227  bool bEnable = true;
228  switch(nPage)
229  {
231  bEnable = true;
232  break;
233  case MM_OUTPUTTYPETPAGE:
234  bEnable = bEnableOutputTypePage;
235  break;
236  case MM_ADDRESSBLOCKPAGE:
237  bEnable = !m_bDocumentLoad && bEnableOutputTypePage;
238  // update page title for email vs letter
239  m_xAssistant->set_page_title(OString::number(MM_ADDRESSBLOCKPAGE), getStateDisplayName(MM_ADDRESSBLOCKPAGE));
240  break;
241  case MM_GREETINGSPAGE:
242  bEnable = !m_bDocumentLoad && bEnableOutputTypePage &&
243  m_xConfigItem->GetResultSet().is() &&
244  bAddressFieldsConfigured;
245  break;
246  case MM_LAYOUTPAGE:
247  bEnable = bCanFinish &&
248  ((m_xConfigItem->IsAddressBlock() && !m_xConfigItem->IsAddressInserted()) ||
249  (m_xConfigItem->IsGreetingLine(false) && !m_xConfigItem->IsGreetingInserted() ));
250  break;
251  }
252  enableState( nPage, bEnable );
253  }
254 }
255 
257 {
258  OSL_FAIL("SwMailMergeWizard cannot be executed via Dialog::Execute!\n"
259  "It creates a thread (MailDispatcher instance) that will call"
260  "back to VCL apartment => deadlock!\n"
261  "Use Dialog::StartExecuteAsync to execute the dialog!" );
262  return RET_CANCEL;
263 }
264 
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< weld::Button > m_xPrevPage
void declarePath(RoadmapWizardTypes::PathId _nPathId, const RoadmapWizardTypes::WizardPath &_lWizardStates)
virtual std::unique_ptr< BuilderPage > createPage(WizardState _nState) override
#define MM_OUTPUTTYPETPAGE
virtual void ActivatePage()
virtual void enterState(WizardTypes::WizardState nState) override
RET_CANCEL
#define MM_LAYOUTPAGE
void defaultButton(WizardButtonFlags _nWizardButtonFlags)
#define MM_ADDRESSBLOCKPAGE
#define MM_DOCUMENTSELECTPAGE
#define MM_GREETINGSPAGE
weld::Window * GetFrameWeld(const SfxFrame *pFrame)
Definition: dialoghelp.cxx:19
SwMailMergeWizard(SwView &rView, std::shared_ptr< SwMailMergeConfigItem > const &rConfigItem)
std::unique_ptr< weld::Button > m_xNextPage
sal_Int16 WizardState
void SetRoadmapHelpId(const OString &_rId)
virtual OUString getStateDisplayName(WizardState _nState) const override
sal_uInt16 m_nRestartPage
virtual ~SwMailMergeWizard() override
void enableState(WizardTypes::WizardState nState, bool _bEnable=true)
OUString GetDataSourceName() const
Definition: view.cxx:1903
#define RET_REMOVE_TARGET
Definition: swabstdlg.hxx:351
std::unique_ptr< weld::Button > m_xFinish
#define HID_MM_PREV_PAGE
Definition: helpids.h:146
virtual void enterState(WizardState _nState) override
void setTitleBase(const OUString &_rTitleBase)
#define HID_MM_NEXT_PAGE
Definition: helpids.h:145
void enableButtons(WizardButtonFlags _nWizardButtonFlags, bool _bEnable)
BuilderPage * GetPage(WizardTypes::WizardState eState) const
OUString SwResId(std::string_view aId)
Definition: swmodule.cxx:165
std::shared_ptr< SwMailMergeConfigItem > m_xConfigItem
std::unique_ptr< weld::Assistant > m_xAssistant
virtual short run() override
Definition: view.hxx:144
static bool IsDataSourceAvailable(const OUString sDataSourceName)
Definition: view.cxx:1914