1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sfx2/filedlghelper.hxx>
21 #include <sfx2/new.hxx>
22 #include <sfx2/docfilt.hxx>
23 #include <sfx2/fcontnr.hxx>
24 #include <sfx2/docfac.hxx>
27 #include "mmdocselectpage.hxx"
28 #include <mailmergewizard.hxx>
29 #include <swabstdlg.hxx>
30 #include <mmconfigitem.hxx>
31 #include <swuiexp.hxx>
32 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
33 #include <com/sun/star/ui/dialogs/XFilePicker3.hpp>
35 using namespace ::com::sun::star::ui::dialogs
;
36 using namespace ::com::sun::star
;
37 using namespace ::com::sun::star::uno
;
40 SwMailMergeDocSelectPage::SwMailMergeDocSelectPage(weld::Container
* pPage
, SwMailMergeWizard
* pWizard
)
41 : vcl::OWizardPage(pPage
, pWizard
, "modules/swriter/ui/mmselectpage.ui", "MMSelectPage")
43 , m_xCurrentDocRB(m_xBuilder
->weld_radio_button("currentdoc"))
44 , m_xNewDocRB(m_xBuilder
->weld_radio_button("newdoc"))
45 , m_xLoadDocRB(m_xBuilder
->weld_radio_button("loaddoc"))
46 , m_xLoadTemplateRB(m_xBuilder
->weld_radio_button("template"))
47 , m_xRecentDocRB(m_xBuilder
->weld_radio_button("recentdoc"))
48 , m_xBrowseDocPB(m_xBuilder
->weld_button("browsedoc"))
49 , m_xBrowseTemplatePB(m_xBuilder
->weld_button("browsetemplate"))
50 , m_xRecentDocLB(m_xBuilder
->weld_combo_box("recentdoclb"))
51 , m_xDataSourceWarningFT(m_xBuilder
->weld_label("datasourcewarning"))
52 , m_xExchangeDatabasePB(m_xBuilder
->weld_button("exchangedatabase"))
54 m_xDataSourceWarningFT
->set_label_type(weld::LabelType::Warning
);
55 m_xCurrentDocRB
->set_active(true);
56 DocSelectHdl(*m_xNewDocRB
);
58 Link
<weld::Toggleable
&,void> aDocSelectLink
= LINK(this, SwMailMergeDocSelectPage
, DocSelectHdl
);
59 m_xCurrentDocRB
->connect_toggled(aDocSelectLink
);
60 m_xNewDocRB
->connect_toggled(aDocSelectLink
);
61 m_xLoadDocRB
->connect_toggled(aDocSelectLink
);
62 m_xLoadTemplateRB
->connect_toggled(aDocSelectLink
);
63 m_xRecentDocRB
->connect_toggled(aDocSelectLink
);
65 Link
<weld::Button
&,void> aFileSelectHdl
= LINK(this, SwMailMergeDocSelectPage
, FileSelectHdl
);
66 m_xBrowseDocPB
->connect_clicked(aFileSelectHdl
);
67 m_xBrowseTemplatePB
->connect_clicked(aFileSelectHdl
);
69 Link
<weld::Button
&,void> aExchangeDatabaseHdl
= LINK(this, SwMailMergeDocSelectPage
, ExchangeDatabaseHdl
);
70 m_xExchangeDatabasePB
->connect_clicked(aExchangeDatabaseHdl
);
72 const uno::Sequence
< OUString
>& rDocs
=
73 m_pWizard
->GetConfigItem().GetSavedDocuments();
74 for(const auto& rDoc
: rDocs
)
76 //insert in reverse order
77 m_xRecentDocLB
->insert_text(0, rDoc
);
79 if (!rDocs
.hasElements())
80 m_xRecentDocRB
->set_sensitive(false);
82 m_xRecentDocLB
->set_active(0);
85 SwMailMergeDocSelectPage::~SwMailMergeDocSelectPage()
89 IMPL_LINK_NOARG(SwMailMergeDocSelectPage
, DocSelectHdl
, weld::Toggleable
&, void)
91 m_xRecentDocLB
->set_sensitive(m_xRecentDocRB
->get_active());
92 m_pWizard
->UpdateRoadmap();
93 OUString sDataSourceName
= m_pWizard
->GetSwView()->GetDataSourceName();
95 if(m_xCurrentDocRB
->get_active() &&
96 !sDataSourceName
.isEmpty() &&
97 !SwView::IsDataSourceAvailable(sDataSourceName
))
99 m_xDataSourceWarningFT
->show();
100 m_pWizard
->enableButtons(WizardButtonFlags::NEXT
, false);
104 m_xDataSourceWarningFT
->hide();
105 m_pWizard
->enableButtons(WizardButtonFlags::NEXT
, m_pWizard
->isStateEnabled(MM_OUTPUTTYPETPAGE
));
108 if(m_xCurrentDocRB
->get_active())
109 m_xExchangeDatabasePB
->set_sensitive(true);
111 m_xExchangeDatabasePB
->set_sensitive(false);
114 IMPL_LINK(SwMailMergeDocSelectPage
, FileSelectHdl
, weld::Button
&, rButton
, void)
116 bool bTemplate
= m_xBrowseTemplatePB
.get() == &rButton
;
120 m_xLoadTemplateRB
->set_active(true);
121 SfxNewFileDialog
aNewFileDlg(m_pWizard
->getDialog(), SfxNewFileDialogMode::NONE
);
122 sal_uInt16 nRet
= aNewFileDlg
.run();
123 if(RET_TEMPLATE_LOAD
== nRet
)
125 else if(RET_CANCEL
!= nRet
)
126 m_sLoadTemplateName
= aNewFileDlg
.GetTemplateFileName();
129 m_xLoadDocRB
->set_active(true);
133 sfx2::FileDialogHelper
aDlgHelper(TemplateDescription::FILEOPEN_SIMPLE
,
134 FileDialogFlags::NONE
, m_pWizard
->getDialog());
135 aDlgHelper
.SetContext(sfx2::FileDialogHelper::WriterMailMerge
);
136 Reference
< XFilePicker3
> xFP
= aDlgHelper
.GetFilePicker();
138 SfxObjectFactory
&rFact
= m_pWizard
->GetSwView()->GetDocShell()->GetFactory();
139 SfxFilterMatcher
aMatcher( rFact
.GetFactoryName() );
140 SfxFilterMatcherIter
aIter( aMatcher
);
141 std::shared_ptr
<const SfxFilter
> pFlt
= aIter
.First();
144 if( pFlt
&& pFlt
->IsAllowedAsTemplate() )
146 const OUString sWild
= pFlt
->GetWildcard().getGlob();
147 xFP
->appendFilter( pFlt
->GetUIName(), sWild
);
150 if(pFlt
->GetFilterFlags() & SfxFilterFlags::DEFAULT
)
151 xFP
->setCurrentFilter( pFlt
->GetUIName() ) ;
157 if( ERRCODE_NONE
== aDlgHelper
.Execute() )
159 m_sLoadFileName
= xFP
->getSelectedFiles().getConstArray()[0];
162 m_pWizard
->UpdateRoadmap();
163 m_pWizard
->enableButtons(WizardButtonFlags::NEXT
, m_pWizard
->isStateEnabled(MM_OUTPUTTYPETPAGE
));
166 IMPL_LINK_NOARG(SwMailMergeDocSelectPage
, ExchangeDatabaseHdl
, weld::Button
&, void)
169 SwAbstractDialogFactory
& rFact
= ::swui::GetFactory();
170 ScopedVclPtr
<VclAbstractDialog
> pDlg(rFact
.CreateSwChangeDBDlg(*m_pWizard
->GetSwView()));
173 OUString sDataSourceName
= m_pWizard
->GetSwView()->GetDataSourceName();
175 if(m_xCurrentDocRB
->get_active() &&
176 !sDataSourceName
.isEmpty() &&
177 SwView::IsDataSourceAvailable(sDataSourceName
))
179 m_xDataSourceWarningFT
->hide();
180 m_pWizard
->enableButtons(WizardButtonFlags::NEXT
, true);
184 bool SwMailMergeDocSelectPage::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason
)
186 bool bReturn
= false;
187 bool bNext
= _eReason
== ::vcl::WizardTypes::eTravelForward
;
188 if(bNext
|| _eReason
== ::vcl::WizardTypes::eValidate
)
190 OUString sReloadDocument
;
191 bReturn
= m_xCurrentDocRB
->get_active() ||
192 m_xNewDocRB
->get_active();
195 sReloadDocument
= m_sLoadFileName
;
196 bReturn
= !sReloadDocument
.isEmpty() && m_xLoadDocRB
->get_active();
200 sReloadDocument
= m_sLoadTemplateName
;
201 bReturn
= !sReloadDocument
.isEmpty() && m_xLoadTemplateRB
->get_active();
205 bReturn
= m_xRecentDocRB
->get_active();
208 sReloadDocument
= m_xRecentDocLB
->get_active_text();
209 bReturn
= !sReloadDocument
.isEmpty();
212 if( _eReason
== ::vcl::WizardTypes::eValidate
)
213 m_pWizard
->SetDocumentLoad(!m_xCurrentDocRB
->get_active());
215 if(bNext
&& !m_xCurrentDocRB
->get_active())
217 if(!sReloadDocument
.isEmpty())
218 m_pWizard
->SetReloadDocument( sReloadDocument
);
219 m_pWizard
->SetRestartPage(MM_OUTPUTTYPETPAGE
);
220 m_pWizard
->response(RET_LOAD_DOC
);
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */