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 <unotools/pathoptions.hxx>
36 #include <sfx2/filedlghelper.hxx>
37 #include <sfx2/new.hxx>
38 #include <sfx2/docfilt.hxx>
39 #include <sfx2/fcontnr.hxx>
40 #include <sfx2/docfac.hxx>
41 #include <vcl/msgbox.hxx>
42 #include <view.hxx>
43 #ifndef _DOCSH_HXX
44 #include <docsh.hxx>
45 #endif
46 #include <mmdocselectpage.hxx>
47 #include <mailmergewizard.hxx>
48 #include <shellio.hxx>
49 #include <swabstdlg.hxx>
50 #include <mmconfigitem.hxx>
51 
52 #include <dbui.hrc>
53 #include <mmdocselectpage.hrc>
54 
55 #include "com/sun/star/ui/dialogs/TemplateDescription.hpp"
56 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
57 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
58 
59 using namespace ::com::sun::star::ui::dialogs;
60 using namespace ::com::sun::star;
61 using namespace ::com::sun::star::uno;
62 using namespace svt;
63 
64 /*-- 02.04.2004 09:40:14---------------------------------------------------
65 
66   -----------------------------------------------------------------------*/
67 SwMailMergeDocSelectPage::SwMailMergeDocSelectPage( SwMailMergeWizard* _pParent ) :
68     svt::OWizardPage(_pParent, SW_RES(DLG_MM_DOCSELECT_PAGE)),
69 #ifdef MSC
70 #pragma warning (disable : 4355)
71 #endif
72     m_aHeaderFI(this,          SW_RES(  FI_HEADER           ) ),
73     m_aHowToFT          (this, SW_RES( FT_HOWTO )),
74     m_aCurrentDocRB     (this, SW_RES( RB_CURRENTDOC )),
75     m_aNewDocRB         (this, SW_RES( RB_NEWDOC )),
76     m_aLoadDocRB        (this, SW_RES( RB_LOADDOC )),
77     m_aLoadTemplateRB   (this, SW_RES( RB_LOADTEMPLATE )),
78     m_aRecentDocRB      (this, SW_RES( RB_RECENTDOC )),
79     m_aBrowseDocPB      (this, SW_RES( PB_LOADDOC )),
80     m_aBrowseTemplatePB (this, SW_RES( PB_BROWSETEMPLATE )),
81     m_aRecentDocLB      (this, SW_RES( LB_RECENTDOC )),
82 #ifdef MSC
83 #pragma warning (default : 4355)
84 #endif
85     m_pWizard(_pParent)
86 {
87     FreeResource();
88 
89     m_aCurrentDocRB.Check();
90     DocSelectHdl(&m_aNewDocRB);
91 
92     Link aDocSelectLink = LINK(this, SwMailMergeDocSelectPage, DocSelectHdl);
93     m_aCurrentDocRB.SetClickHdl(aDocSelectLink);
94     m_aNewDocRB.SetClickHdl(aDocSelectLink);
95     m_aLoadDocRB.SetClickHdl(aDocSelectLink);
96     m_aLoadTemplateRB.SetClickHdl(aDocSelectLink);
97     m_aRecentDocRB.SetClickHdl(aDocSelectLink);
98 
99     Link aFileSelectHdl = LINK(this, SwMailMergeDocSelectPage, FileSelectHdl);
100     m_aBrowseDocPB.SetClickHdl(aFileSelectHdl);
101     m_aBrowseTemplatePB.SetClickHdl(aFileSelectHdl);
102 
103     const uno::Sequence< ::rtl::OUString >& rDocs =
104                             m_pWizard->GetConfigItem().GetSavedDocuments();
105     for(sal_Int32 nDoc = 0; nDoc < rDocs.getLength(); ++nDoc)
106     {
107         //insert in reverse order
108         m_aRecentDocLB.InsertEntry(rDocs[nDoc], 0);
109     }
110     m_aRecentDocLB.SelectEntryPos(0);
111     if(!rDocs.getLength())
112     {
113         m_aRecentDocRB.Enable(sal_False);
114     }
115 }
116 
117 /*-- 02.04.2004 09:40:14---------------------------------------------------
118 
119   -----------------------------------------------------------------------*/
120 SwMailMergeDocSelectPage::~SwMailMergeDocSelectPage()
121 {
122 }
123 
124 /*-- 05.04.2004 14:21:48---------------------------------------------------
125 
126   -----------------------------------------------------------------------*/
127 IMPL_LINK(SwMailMergeDocSelectPage, DocSelectHdl, RadioButton*, pButton)
128 {
129     m_aRecentDocLB.Enable(&m_aRecentDocRB == pButton);
130 
131     m_pWizard->UpdateRoadmap();
132     m_pWizard->enableButtons(WZB_NEXT, m_pWizard->isStateEnabled(MM_OUTPUTTYPETPAGE));
133 
134     return 0;
135 }
136 /*-- 05.04.2004 14:25:12---------------------------------------------------
137 
138   -----------------------------------------------------------------------*/
139 IMPL_LINK(SwMailMergeDocSelectPage, FileSelectHdl, PushButton*, pButton)
140 {
141     bool bTemplate = &m_aBrowseTemplatePB == pButton;
142 
143     if(bTemplate)
144     {
145         m_aLoadTemplateRB.Check();
146         SfxNewFileDialog* pNewFileDlg = new SfxNewFileDialog(this, 0);
147         //pNewFileDlg->SetTemplateFlags(nFlags);
148         sal_uInt16 nRet = pNewFileDlg->Execute();
149         if(RET_TEMPLATE_LOAD == nRet)
150             bTemplate = false;
151         else if(RET_CANCEL != nRet)
152             m_sLoadTemplateName = pNewFileDlg->GetTemplateFileName();
153         delete pNewFileDlg;
154     }
155     else
156         m_aLoadDocRB.Check();
157 
158     if(!bTemplate)
159     {
160         sfx2::FileDialogHelper aDlgHelper( TemplateDescription::FILEOPEN_SIMPLE, 0 );
161         Reference < XFilePicker > xFP = aDlgHelper.GetFilePicker();
162 
163         xFP->setDisplayDirectory( SvtPathOptions().GetWorkPath() );
164 
165         SfxObjectFactory &rFact = m_pWizard->GetSwView()->GetDocShell()->GetFactory();
166         SfxFilterMatcher aMatcher( String::CreateFromAscii(rFact.GetShortName()) );
167         SfxFilterMatcherIter aIter( &aMatcher );
168         Reference<XFilterManager> xFltMgr(xFP, UNO_QUERY);
169         const SfxFilter* pFlt = aIter.First();
170         while( pFlt )
171         {
172             if( pFlt && pFlt->IsAllowedAsTemplate() )
173             {
174                 const String sWild = ((WildCard&)pFlt->GetWildcard()).GetWildCard();
175                 xFltMgr->appendFilter( pFlt->GetUIName(), sWild );
176 
177 				// #i40125
178 				if(pFlt->GetFilterFlags() & SFX_FILTER_DEFAULT)
179 					xFltMgr->setCurrentFilter( pFlt->GetUIName() ) ;
180             }
181 
182 
183             pFlt = aIter.Next();
184         }
185 
186         if( ERRCODE_NONE == aDlgHelper.Execute() )
187         {
188             m_sLoadFileName = xFP->getFiles().getConstArray()[0];
189         }
190     }
191     m_pWizard->UpdateRoadmap();
192     m_pWizard->enableButtons(WZB_NEXT, m_pWizard->isStateEnabled(MM_OUTPUTTYPETPAGE));
193 
194     return 0;
195 }
196 
197 /*-- 06.04.2004 12:52:24---------------------------------------------------
198 
199   -----------------------------------------------------------------------*/
200 sal_Bool SwMailMergeDocSelectPage::commitPage( ::svt::WizardTypes::CommitPageReason _eReason )
201 {
202     sal_Bool bReturn = sal_False;
203     bool bNext = _eReason == ::svt::WizardTypes::eTravelForward;
204     if(bNext || _eReason == ::svt::WizardTypes::eValidate )
205     {
206         ::rtl::OUString sReloadDocument;
207         bReturn = m_aCurrentDocRB.IsChecked() ||
208                 m_aNewDocRB.IsChecked() ||
209                 ((sReloadDocument = m_sLoadFileName).getLength() && m_aLoadDocRB.IsChecked() )||
210                 ((sReloadDocument = m_sLoadTemplateName).getLength() && m_aLoadTemplateRB.IsChecked())||
211                 (m_aRecentDocRB.IsChecked() && (sReloadDocument = m_aRecentDocLB.GetSelectEntry()).getLength());
212         if( _eReason == ::svt::WizardTypes::eValidate )
213             m_pWizard->SetDocumentLoad(!m_aCurrentDocRB.IsChecked());
214 
215         if(bNext && !m_aCurrentDocRB.IsChecked())
216         {
217             if(sReloadDocument.getLength())
218                 m_pWizard->SetReloadDocument( sReloadDocument );
219             m_pWizard->SetRestartPage(MM_OUTPUTTYPETPAGE);
220             m_pWizard->EndDialog(RET_LOAD_DOC);
221         }
222     }
223     return bReturn;
224 }
225