sc: factor out some more code
[LibreOffice.git] / sw / source / ui / dbui / mmdocselectpage.cxx
blobd877eb538b1a4888fa8e2a2a7c219dff6c771cf4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
25 #include <view.hxx>
26 #include <docsh.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;
39 SwMailMergeDocSelectPage::SwMailMergeDocSelectPage(weld::Container* pPage, SwMailMergeWizard* pWizard)
40 : vcl::OWizardPage(pPage, pWizard, u"modules/swriter/ui/mmselectpage.ui"_ustr, u"MMSelectPage"_ustr)
41 , m_pWizard(pWizard)
42 , m_xCurrentDocRB(m_xBuilder->weld_radio_button(u"currentdoc"_ustr))
43 , m_xNewDocRB(m_xBuilder->weld_radio_button(u"newdoc"_ustr))
44 , m_xLoadDocRB(m_xBuilder->weld_radio_button(u"loaddoc"_ustr))
45 , m_xLoadTemplateRB(m_xBuilder->weld_radio_button(u"template"_ustr))
46 , m_xRecentDocRB(m_xBuilder->weld_radio_button(u"recentdoc"_ustr))
47 , m_xBrowseDocPB(m_xBuilder->weld_button(u"browsedoc"_ustr))
48 , m_xBrowseTemplatePB(m_xBuilder->weld_button(u"browsetemplate"_ustr))
49 , m_xRecentDocLB(m_xBuilder->weld_combo_box(u"recentdoclb"_ustr))
50 , m_xDataSourceWarningFT(m_xBuilder->weld_label(u"datasourcewarning"_ustr))
51 , m_xExchangeDatabasePB(m_xBuilder->weld_button(u"exchangedatabase"_ustr))
53 m_xDataSourceWarningFT->set_label_type(weld::LabelType::Warning);
54 m_xCurrentDocRB->set_active(true);
55 DocSelectHdl(*m_xNewDocRB);
57 Link<weld::Toggleable&,void> aDocSelectLink = LINK(this, SwMailMergeDocSelectPage, DocSelectHdl);
58 m_xCurrentDocRB->connect_toggled(aDocSelectLink);
59 m_xNewDocRB->connect_toggled(aDocSelectLink);
60 m_xLoadDocRB->connect_toggled(aDocSelectLink);
61 m_xLoadTemplateRB->connect_toggled(aDocSelectLink);
62 m_xRecentDocRB->connect_toggled(aDocSelectLink);
64 Link<weld::Button&,void> aFileSelectHdl = LINK(this, SwMailMergeDocSelectPage, FileSelectHdl);
65 m_xBrowseDocPB->connect_clicked(aFileSelectHdl);
66 m_xBrowseTemplatePB->connect_clicked(aFileSelectHdl);
68 Link<weld::Button&,void> aExchangeDatabaseHdl = LINK(this, SwMailMergeDocSelectPage, ExchangeDatabaseHdl);
69 m_xExchangeDatabasePB->connect_clicked(aExchangeDatabaseHdl);
71 const uno::Sequence< OUString >& rDocs =
72 m_pWizard->GetConfigItem().GetSavedDocuments();
73 for(const auto& rDoc : rDocs)
75 //insert in reverse order
76 m_xRecentDocLB->insert_text(0, rDoc);
78 if (!rDocs.hasElements())
79 m_xRecentDocRB->set_sensitive(false);
80 else
81 m_xRecentDocLB->set_active(0);
84 SwMailMergeDocSelectPage::~SwMailMergeDocSelectPage()
88 IMPL_LINK_NOARG(SwMailMergeDocSelectPage, DocSelectHdl, weld::Toggleable&, void)
90 m_xRecentDocLB->set_sensitive(m_xRecentDocRB->get_active());
91 m_pWizard->UpdateRoadmap();
92 OUString sDataSourceName = m_pWizard->GetSwView()->GetDataSourceName();
94 if(m_xCurrentDocRB->get_active() &&
95 !sDataSourceName.isEmpty() &&
96 !SwView::IsDataSourceAvailable(sDataSourceName))
98 m_xDataSourceWarningFT->show();
99 m_pWizard->enableButtons(WizardButtonFlags::NEXT, false);
101 else
103 m_xDataSourceWarningFT->hide();
104 m_pWizard->enableButtons(WizardButtonFlags::NEXT, m_pWizard->isStateEnabled(MM_OUTPUTTYPETPAGE));
107 if(m_xCurrentDocRB->get_active())
108 m_xExchangeDatabasePB->set_sensitive(true);
109 else
110 m_xExchangeDatabasePB->set_sensitive(false);
113 IMPL_LINK(SwMailMergeDocSelectPage, FileSelectHdl, weld::Button&, rButton, void)
115 bool bTemplate = m_xBrowseTemplatePB.get() == &rButton;
117 if(bTemplate)
119 m_xLoadTemplateRB->set_active(true);
120 SfxNewFileDialog aNewFileDlg(m_pWizard->getDialog(), SfxNewFileDialogMode::NONE);
121 sal_uInt16 nRet = aNewFileDlg.run();
122 if(RET_TEMPLATE_LOAD == nRet)
123 bTemplate = false;
124 else if(RET_CANCEL != nRet)
125 m_sLoadTemplateName = aNewFileDlg.GetTemplateFileName();
127 else
128 m_xLoadDocRB->set_active(true);
130 if(!bTemplate)
132 sfx2::FileDialogHelper aDlgHelper(TemplateDescription::FILEOPEN_SIMPLE,
133 FileDialogFlags::NONE, m_pWizard->getDialog());
134 aDlgHelper.SetContext(sfx2::FileDialogHelper::WriterMailMerge);
135 Reference < XFilePicker3 > xFP = aDlgHelper.GetFilePicker();
137 SfxObjectFactory &rFact = m_pWizard->GetSwView()->GetDocShell()->GetFactory();
138 SfxFilterMatcher aMatcher( rFact.GetFactoryName() );
139 SfxFilterMatcherIter aIter( aMatcher );
140 std::shared_ptr<const SfxFilter> pFlt = aIter.First();
141 while( pFlt )
143 if( pFlt->IsAllowedAsTemplate() )
145 const OUString sWild = pFlt->GetWildcard().getGlob();
146 xFP->appendFilter( pFlt->GetUIName(), sWild );
148 // #i40125
149 if(pFlt->GetFilterFlags() & SfxFilterFlags::DEFAULT)
150 xFP->setCurrentFilter( pFlt->GetUIName() ) ;
153 pFlt = aIter.Next();
156 if( ERRCODE_NONE == aDlgHelper.Execute() )
158 m_sLoadFileName = xFP->getSelectedFiles().getConstArray()[0];
161 m_pWizard->UpdateRoadmap();
162 m_pWizard->enableButtons(WizardButtonFlags::NEXT, m_pWizard->isStateEnabled(MM_OUTPUTTYPETPAGE));
165 IMPL_LINK_NOARG(SwMailMergeDocSelectPage, ExchangeDatabaseHdl, weld::Button&, void)
168 SwAbstractDialogFactory& rFact = ::swui::GetFactory();
169 VclPtr<AbstractChangeDbDialog> pDlg(rFact.CreateSwChangeDBDlg(*m_pWizard->GetSwView()));
170 pDlg->StartExecuteAsync(
171 [this, pDlg] (sal_Int32 nResult)->void
173 if (nResult == RET_OK)
174 pDlg->UpdateFields();
175 pDlg->disposeOnce();
177 OUString sDataSourceName = m_pWizard->GetSwView()->GetDataSourceName();
179 if(m_xCurrentDocRB->get_active() &&
180 !sDataSourceName.isEmpty() &&
181 SwView::IsDataSourceAvailable(sDataSourceName))
183 m_xDataSourceWarningFT->hide();
184 m_pWizard->enableButtons(WizardButtonFlags::NEXT, true);
191 bool SwMailMergeDocSelectPage::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason )
193 bool bReturn = false;
194 bool bNext = _eReason == ::vcl::WizardTypes::eTravelForward;
195 if(bNext || _eReason == ::vcl::WizardTypes::eValidate )
197 OUString sReloadDocument;
198 bReturn = m_xCurrentDocRB->get_active() ||
199 m_xNewDocRB->get_active();
200 if (!bReturn)
202 sReloadDocument = m_sLoadFileName;
203 bReturn = !sReloadDocument.isEmpty() && m_xLoadDocRB->get_active();
205 if (!bReturn)
207 sReloadDocument = m_sLoadTemplateName;
208 bReturn = !sReloadDocument.isEmpty() && m_xLoadTemplateRB->get_active();
210 if (!bReturn)
212 bReturn = m_xRecentDocRB->get_active();
213 if (bReturn)
215 sReloadDocument = m_xRecentDocLB->get_active_text();
216 bReturn = !sReloadDocument.isEmpty();
219 if( _eReason == ::vcl::WizardTypes::eValidate )
220 m_pWizard->SetDocumentLoad(!m_xCurrentDocRB->get_active());
222 if(bNext && !m_xCurrentDocRB->get_active())
224 if(!sReloadDocument.isEmpty())
225 m_pWizard->SetReloadDocument( sReloadDocument );
226 m_pWizard->SetRestartPage(MM_OUTPUTTYPETPAGE);
227 m_pWizard->response(RET_LOAD_DOC);
230 return bReturn;
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */