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