1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 #ifdef SW_DLLIMPLEMENTATION
27 #undef SW_DLLIMPLEMENTATION
28 #endif
29 
30 
31 #include <mailmergewizard.hxx>
32 #include <mmdocselectpage.hxx>
33 #include <mmoutputtypepage.hxx>
34 #include <mmaddressblockpage.hxx>
35 #include <mmpreparemergepage.hxx>
36 #include <mmmergepage.hxx>
37 #include <mmgreetingspage.hxx>
38 #include <mmoutputpage.hxx>
39 #include <mmlayoutpage.hxx>
40 #include <mmconfigitem.hxx>
41 #include <swabstdlg.hxx>
42 #ifndef _DBUI_HRC
43 #include <dbui.hrc>
44 #endif
45 #include <view.hxx>
46 #ifndef _DBMGR_HXX
47 #include <dbmgr.hxx>
48 #endif
49 #include <sfx2/viewfrm.hxx>
50 #include <wrtsh.hxx>
51 #include <sfx2/viewfrm.hxx>
52 #include "vcl/msgbox.hxx" // RET_CANCEL
53 
54 #include <helpid.h>
55 #include <dbui.hrc>
56 #include <mailmergewizard.hrc>
57 
58 using namespace svt;
59 using namespace ::com::sun::star;
60 /*-- 01.04.2004 16:52:49---------------------------------------------------
61 
62   -----------------------------------------------------------------------*/
SwMailMergeWizard(SwView & rView,SwMailMergeConfigItem & rItem)63 SwMailMergeWizard::SwMailMergeWizard(SwView& rView, SwMailMergeConfigItem& rItem) :
64         RoadmapWizard(&rView.GetViewFrame()->GetWindow(),
65                         SW_RES(DLG_MAILMERGEWIZARD),
66                         WZB_NEXT|WZB_PREVIOUS|WZB_FINISH|WZB_CANCEL|WZB_HELP),
67         m_pSwView(&rView),
68         m_bDocumentLoad( false ),
69         m_rConfigItem(rItem),
70         m_sStarting(        SW_RES( ST_STARTING      )),
71         m_sDocumentType(    SW_RES( ST_DOCUMETNTYPE   )),
72         m_sAddressBlock(    SW_RES( ST_ADDRESSBLOCK   )),
73         m_sAddressList(     SW_RES( ST_ADDRESSLIST )),
74         m_sGreetingsLine(   SW_RES( ST_GREETINGSLINE   )),
75         m_sLayout(          SW_RES( ST_LAYOUT        )),
76         m_sPrepareMerge(    SW_RES( ST_PREPAREMERGE )),
77         m_sMerge(           SW_RES( ST_MERGE        )),
78         m_sOutput(          SW_RES( ST_OUTPUT       )),
79         m_sFinish(          SW_RES( ST_FINISH       )),
80         m_nRestartPage( MM_DOCUMENTSELECTPAGE )
81 {
82     FreeResource();
83     ShowButtonFixedLine(sal_True);
84     defaultButton(WZB_NEXT);
85     enableButtons(WZB_FINISH, sal_False);
86 
87     m_pFinish->SetText(m_sFinish);
88     m_pNextPage->SetHelpId(HID_MM_NEXT_PAGE);
89     m_pPrevPage->SetHelpId(HID_MM_PREV_PAGE);
90 
91     //#i51949# no output type page visible if e-Mail is not supported
92     if(rItem.IsMailAvailable())
93         declarePath(
94             0,
95             MM_DOCUMENTSELECTPAGE,
96             MM_OUTPUTTYPETPAGE,
97             MM_ADDRESSBLOCKPAGE,
98             MM_GREETINGSPAGE,
99             MM_LAYOUTPAGE,
100             MM_PREPAREMERGEPAGE,
101             MM_MERGEPAGE,
102             MM_OUTPUTPAGE,
103             WZS_INVALID_STATE
104         );
105     else
106         declarePath(
107             0,
108             MM_DOCUMENTSELECTPAGE,
109             MM_ADDRESSBLOCKPAGE,
110             MM_GREETINGSPAGE,
111             MM_LAYOUTPAGE,
112             MM_PREPAREMERGEPAGE,
113             MM_MERGEPAGE,
114             MM_OUTPUTPAGE,
115             WZS_INVALID_STATE
116         );
117 
118     ActivatePage();
119     UpdateRoadmap();
120 }
121 /*-- 01.04.2004 16:52:53---------------------------------------------------
122 
123   -----------------------------------------------------------------------*/
~SwMailMergeWizard()124 SwMailMergeWizard::~SwMailMergeWizard()
125 {
126 }
127 /*-- 01.04.2004 16:52:53---------------------------------------------------
128 
129   -----------------------------------------------------------------------*/
createPage(WizardState _nState)130 OWizardPage*    SwMailMergeWizard::createPage(WizardState _nState)
131 {
132     OWizardPage* pRet = 0;
133     switch(_nState)
134     {
135         case MM_DOCUMENTSELECTPAGE : pRet = new SwMailMergeDocSelectPage(this);     break;
136         case MM_OUTPUTTYPETPAGE : pRet = new SwMailMergeOutputTypePage(this);       break;
137         case MM_ADDRESSBLOCKPAGE  : pRet = new SwMailMergeAddressBlockPage(this);     break;
138         case MM_GREETINGSPAGE     : pRet = new SwMailMergeGreetingsPage(this);      break;
139         case MM_LAYOUTPAGE        : pRet = new SwMailMergeLayoutPage(this);     break;
140         case MM_PREPAREMERGEPAGE  : pRet = new SwMailMergePrepareMergePage(this);   break;
141         case MM_MERGEPAGE         : pRet = new SwMailMergeMergePage(this);          break;
142         case MM_OUTPUTPAGE       :  pRet = new SwMailMergeOutputPage(this);         break;
143     }
144     DBG_ASSERT(pRet, "no page created in ::createPage");
145     return pRet;
146 }
147 /*-- 04.06.2004 13:18:47---------------------------------------------------
148 
149   -----------------------------------------------------------------------*/
enterState(WizardState _nState)150 void SwMailMergeWizard::enterState( WizardState _nState )
151 {
152     ::svt::RoadmapWizard::enterState( _nState );
153 /*
154     entering a page after the layoutpage requires the insertion
155     of greeting and address block - if not yet done
156     entering the merge or output page requires to create the output document
157 */
158     if(_nState > MM_LAYOUTPAGE && m_rConfigItem.GetSourceView() &&
159             ((m_rConfigItem.IsAddressBlock() && !m_rConfigItem.IsAddressInserted()) ||
160              (m_rConfigItem.IsGreetingLine(sal_False) && !m_rConfigItem.IsGreetingInserted() )))
161     {
162         SwMailMergeLayoutPage::InsertAddressAndGreeting(m_rConfigItem.GetSourceView(),
163                                 m_rConfigItem, Point(-1, -1), true);
164     }
165     if(_nState >= MM_MERGEPAGE && !m_rConfigItem.GetTargetView())
166     {
167         CreateTargetDocument();
168         m_nRestartPage = _nState;
169         EndDialog(RET_TARGET_CREATED);
170         return;
171     }
172     else if(_nState < MM_MERGEPAGE && m_rConfigItem.GetTargetView())
173     {
174         //close the dialog, remove the target view, show the source view
175         m_nRestartPage = _nState;
176         //set ResultSet back to start
177         m_rConfigItem.MoveResultSet(1);
178         EndDialog(RET_REMOVE_TARGET);
179         return;
180     }
181     bool bEnablePrev = true;
182     bool bEnableNext = true;
183     switch(_nState)
184     {
185         case MM_DOCUMENTSELECTPAGE :
186             bEnablePrev = false;
187         break;
188         case MM_ADDRESSBLOCKPAGE  :
189             bEnableNext = m_rConfigItem.GetResultSet().is();
190         break;
191         case MM_OUTPUTPAGE       :
192             bEnableNext = false;
193         break;
194     }
195     enableButtons( WZB_PREVIOUS, bEnablePrev);
196     enableButtons( WZB_NEXT, bEnableNext);
197 
198     UpdateRoadmap();
199 }
200 /*-- 04.06.2004 12:54:13---------------------------------------------------
201 
202   -----------------------------------------------------------------------*/
getStateDisplayName(WizardState _nState) const203 String  SwMailMergeWizard::getStateDisplayName( WizardState _nState ) const
204 {
205     String sRet;
206     switch(_nState)
207     {
208         case MM_DOCUMENTSELECTPAGE :sRet = m_sStarting;      break;
209         case MM_OUTPUTTYPETPAGE :   sRet = m_sDocumentType;  break;
210         case MM_ADDRESSBLOCKPAGE  :
211             sRet =  m_rConfigItem.IsOutputToLetter() ?
212                     m_sAddressBlock : m_sAddressList;
213 
214         break;
215         case MM_GREETINGSPAGE     : sRet = m_sGreetingsLine; break;
216         case MM_LAYOUTPAGE        : sRet = m_sLayout;        break;
217         case MM_PREPAREMERGEPAGE  : sRet = m_sPrepareMerge;  break;
218         case MM_MERGEPAGE         : sRet = m_sMerge;         break;
219         case MM_OUTPUTPAGE       : sRet = m_sOutput;        break;
220     }
221     return sRet;
222 }
223 /*-- 24.06.2004 09:24:45---------------------------------------------------
224     enables/disables pages in the roadmap depending on the current
225     page and state
226   -----------------------------------------------------------------------*/
UpdateRoadmap()227 void SwMailMergeWizard::UpdateRoadmap()
228 {
229 /*
230     MM_DOCUMENTSELECTPAGE       > inactive after the layoutpage
231     MM_OUTPUTTYPETPAGE :        > inactive after the layoutpage
232     MM_ADDRESSBLOCKPAGE         > inactive after the layoutpage
233     MM_GREETINGSPAGE            > inactive after the layoutpage
234     MM_LAYOUTPAGE               >   inactive after the layoutpage
235                                     inactive if address block and greeting are switched off
236                                     or are already inserted into the source document
237     MM_PREPAREMERGEPAGE         > only active if address data has been selected
238                                     inactive after preparemerge page
239     MM_MERGEPAGE                > only active if address data has been selected
240 
241     MM_OUTPUTPAGE               > only active if address data has been selected
242 */
243 
244     // enableState( <page id>, false );
245     const sal_uInt16 nCurPage = GetCurLevel();
246     TabPage* pCurPage = GetPage( nCurPage );
247     if(!pCurPage)
248         return;
249     bool bEnable = false;
250     bool bAddressFieldsConfigured = !m_rConfigItem.IsOutputToLetter() ||
251                 !m_rConfigItem.IsAddressBlock() ||
252                 m_rConfigItem.IsAddressFieldsAssigned();
253     bool bGreetingFieldsConfigured = !m_rConfigItem.IsGreetingLine(sal_False) ||
254             !m_rConfigItem.IsIndividualGreeting(sal_False)||
255                     m_rConfigItem.IsGreetingFieldsAssigned();
256     //#i97436# if a document has to be loaded then enable output type page only
257     m_bDocumentLoad = false;
258     bool bEnableOutputTypePage = (nCurPage != MM_DOCUMENTSELECTPAGE) ||
259         static_cast<svt::OWizardPage*>(pCurPage)->commitPage( ::svt::WizardTypes::eValidate );
260 
261     for(sal_uInt16 nPage = MM_DOCUMENTSELECTPAGE; nPage <= MM_OUTPUTPAGE; ++nPage)
262     {
263         switch(nPage)
264         {
265             case MM_DOCUMENTSELECTPAGE :
266                 bEnable = sal_True;
267             break;
268             case MM_OUTPUTTYPETPAGE :
269                 bEnable = bEnableOutputTypePage;
270             break;
271             case MM_ADDRESSBLOCKPAGE  :
272                 bEnable = !m_bDocumentLoad && bEnableOutputTypePage;
273             break;
274             case MM_GREETINGSPAGE     :
275                 bEnable = !m_bDocumentLoad && bEnableOutputTypePage &&
276                     m_rConfigItem.GetResultSet().is() &&
277                             bAddressFieldsConfigured;
278             break;
279             case MM_PREPAREMERGEPAGE  :
280             case MM_MERGEPAGE         :
281             case MM_OUTPUTPAGE       :
282             case MM_LAYOUTPAGE        :
283                 bEnable = !m_bDocumentLoad && bEnableOutputTypePage &&
284                             m_rConfigItem.GetResultSet().is() &&
285                             bAddressFieldsConfigured &&
286                             bGreetingFieldsConfigured;
287                 if(MM_LAYOUTPAGE == nPage)
288                     bEnable &=
289                         ((m_rConfigItem.IsAddressBlock() && !m_rConfigItem.IsAddressInserted()) ||
290                             (m_rConfigItem.IsGreetingLine(sal_False) && !m_rConfigItem.IsGreetingInserted() ));
291             break;
292         }
293         enableState( nPage, bEnable );
294     }
295 }
296 /*-- 24.06.2004 09:24:45---------------------------------------------------
297     enables/disables pages in the roadmap depending on the current
298     page and state
299   -----------------------------------------------------------------------*/
CreateTargetDocument()300 void SwMailMergeWizard::CreateTargetDocument()
301 {
302     GetSwView()->GetWrtShell().GetNewDBMgr()->
303                 MergeDocuments( m_rConfigItem, *GetSwView() );
304     m_rConfigItem.SetMergeDone();
305     if( m_rConfigItem.GetTargetView() )
306         m_rConfigItem.GetTargetView()->GetViewFrame()->GetFrame().Appear();
307 }
308 /*-- 23.08.2004 08:51:36---------------------------------------------------
309 
310   -----------------------------------------------------------------------*/
updateRoadmapItemLabel(WizardState _nState)311 void SwMailMergeWizard::updateRoadmapItemLabel( WizardState _nState )
312 {
313     svt::RoadmapWizard::updateRoadmapItemLabel( _nState );
314 }
315 
316 // ------------------------------------------------------------------------
317 
Execute()318 short SwMailMergeWizard::Execute()
319 {
320     DBG_ERROR( "SwMailMergeWizard cannot be executed via Dialog::Execute!\n"
321                "It creates a thread (MailDispatcher instance) that will call"
322                "back to VCL apartment => deadlock!\n"
323                "Use Dialog::StartExecuteModal to execute the dialog!" );
324     return RET_CANCEL;
325 }
326 
327 // ------------------------------------------------------------------------
328 
StartExecuteModal(const Link & rEndDialogHdl)329 void SwMailMergeWizard::StartExecuteModal( const Link& rEndDialogHdl )
330 {
331     ::svt::RoadmapWizard::StartExecuteModal( rEndDialogHdl );
332 }
333