1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <unotools/moduleoptions.hxx>
21 #include <unotools/fltrcfg.hxx>
22 #include <officecfg/Office/Common.hxx>
23 #include "optfltr.hxx"
24 #include <strings.hrc>
25 #include <dialmgr.hxx>
26 
27 enum class MSFltrPg2_CheckBoxEntries {
28     Math,
29     Writer,
30     Calc,
31     Impress,
32     SmartArt,
33     Visio,
34     PDF,
35     InvalidCBEntry
36 };
37 
38 
OfaMSFilterTabPage(weld::Container * pPage,weld::DialogController * pController,const SfxItemSet & rSet)39 OfaMSFilterTabPage::OfaMSFilterTabPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rSet)
40     : SfxTabPage(pPage, pController, "cui/ui/optfltrpage.ui", "OptFltrPage", &rSet)
41     , m_xWBasicCodeCB(m_xBuilder->weld_check_button("wo_basic"))
42     , m_xWBasicWbctblCB(m_xBuilder->weld_check_button("wo_exec"))
43     , m_xWBasicStgCB(m_xBuilder->weld_check_button("wo_saveorig"))
44     , m_xEBasicCodeCB(m_xBuilder->weld_check_button("ex_basic"))
45     , m_xEBasicExectblCB(m_xBuilder->weld_check_button("ex_exec"))
46     , m_xEBasicStgCB(m_xBuilder->weld_check_button("ex_saveorig"))
47     , m_xPBasicCodeCB(m_xBuilder->weld_check_button("pp_basic"))
48     , m_xPBasicStgCB(m_xBuilder->weld_check_button("pp_saveorig"))
49 {
50     m_xWBasicCodeCB->connect_clicked( LINK( this, OfaMSFilterTabPage, LoadWordBasicCheckHdl_Impl ) );
51     m_xEBasicCodeCB->connect_clicked( LINK( this, OfaMSFilterTabPage, LoadExcelBasicCheckHdl_Impl ) );
52 }
53 
~OfaMSFilterTabPage()54 OfaMSFilterTabPage::~OfaMSFilterTabPage()
55 {
56 }
57 
IMPL_LINK_NOARG(OfaMSFilterTabPage,LoadWordBasicCheckHdl_Impl,weld::Button &,void)58 IMPL_LINK_NOARG(OfaMSFilterTabPage, LoadWordBasicCheckHdl_Impl, weld::Button&, void)
59 {
60     m_xWBasicWbctblCB->set_sensitive(m_xWBasicCodeCB->get_active());
61 }
62 
IMPL_LINK_NOARG(OfaMSFilterTabPage,LoadExcelBasicCheckHdl_Impl,weld::Button &,void)63 IMPL_LINK_NOARG(OfaMSFilterTabPage, LoadExcelBasicCheckHdl_Impl, weld::Button&, void)
64 {
65     m_xEBasicExectblCB->set_sensitive(m_xEBasicCodeCB->get_active());
66 }
67 
Create(weld::Container * pPage,weld::DialogController * pController,const SfxItemSet * rAttrSet)68 std::unique_ptr<SfxTabPage> OfaMSFilterTabPage::Create( weld::Container* pPage, weld::DialogController* pController,
69                                                const SfxItemSet* rAttrSet )
70 {
71     return std::make_unique<OfaMSFilterTabPage>(pPage, pController, *rAttrSet);
72 }
73 
FillItemSet(SfxItemSet *)74 bool OfaMSFilterTabPage::FillItemSet( SfxItemSet* )
75 {
76     SvtFilterOptions& rOpt = SvtFilterOptions::Get();
77 
78     if( m_xWBasicCodeCB->get_state_changed_from_saved() )
79         rOpt.SetLoadWordBasicCode( m_xWBasicCodeCB->get_active() );
80     if( m_xWBasicWbctblCB->get_state_changed_from_saved() )
81         rOpt.SetLoadWordBasicExecutable( m_xWBasicWbctblCB->get_active() );
82     if( m_xWBasicStgCB->get_state_changed_from_saved() )
83         rOpt.SetLoadWordBasicStorage( m_xWBasicStgCB->get_active() );
84 
85     if( m_xEBasicCodeCB->get_state_changed_from_saved())
86         rOpt.SetLoadExcelBasicCode( m_xEBasicCodeCB->get_active() );
87     if( m_xEBasicExectblCB->get_state_changed_from_saved())
88         rOpt.SetLoadExcelBasicExecutable( m_xEBasicExectblCB->get_active() );
89     if( m_xEBasicStgCB->get_state_changed_from_saved())
90         rOpt.SetLoadExcelBasicStorage( m_xEBasicStgCB->get_active() );
91 
92     if( m_xPBasicCodeCB->get_state_changed_from_saved())
93         rOpt.SetLoadPPointBasicCode( m_xPBasicCodeCB->get_active() );
94     if( m_xPBasicStgCB->get_state_changed_from_saved())
95         rOpt.SetLoadPPointBasicStorage( m_xPBasicStgCB->get_active() );
96 
97     return false;
98 }
99 
Reset(const SfxItemSet *)100 void OfaMSFilterTabPage::Reset( const SfxItemSet* )
101 {
102     const SvtFilterOptions& rOpt = SvtFilterOptions::Get();
103 
104     m_xWBasicCodeCB->set_active( rOpt.IsLoadWordBasicCode() );
105     m_xWBasicCodeCB->save_state();
106     m_xWBasicWbctblCB->set_active( rOpt.IsLoadWordBasicExecutable() );
107     m_xWBasicWbctblCB->save_state();
108     m_xWBasicStgCB->set_active( rOpt.IsLoadWordBasicStorage() );
109     m_xWBasicStgCB->save_state();
110     LoadWordBasicCheckHdl_Impl( *m_xWBasicCodeCB );
111 
112     m_xEBasicCodeCB->set_active( rOpt.IsLoadExcelBasicCode() );
113     m_xEBasicCodeCB->save_state();
114     m_xEBasicExectblCB->set_active( rOpt.IsLoadExcelBasicExecutable() );
115     m_xEBasicExectblCB->save_state();
116     m_xEBasicStgCB->set_active( rOpt.IsLoadExcelBasicStorage() );
117     m_xEBasicStgCB->save_state();
118     LoadExcelBasicCheckHdl_Impl( *m_xEBasicCodeCB );
119 
120     m_xPBasicCodeCB->set_active( rOpt.IsLoadPPointBasicCode() );
121     m_xPBasicCodeCB->save_state();
122     m_xPBasicStgCB->set_active( rOpt.IsLoadPPointBasicStorage() );
123     m_xPBasicStgCB->save_state();
124 }
125 
OfaMSFilterTabPage2(weld::Container * pPage,weld::DialogController * pController,const SfxItemSet & rSet)126 OfaMSFilterTabPage2::OfaMSFilterTabPage2(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rSet)
127     : SfxTabPage(pPage, pController, "cui/ui/optfltrembedpage.ui", "OptFilterPage", &rSet)
128     , sChgToFromMath(CuiResId(RID_SVXSTR_CHG_MATH))
129     , sChgToFromWriter(CuiResId(RID_SVXSTR_CHG_WRITER))
130     , sChgToFromCalc(CuiResId(RID_SVXSTR_CHG_CALC))
131     , sChgToFromImpress(CuiResId(RID_SVXSTR_CHG_IMPRESS))
132     , sChgToFromSmartArt(CuiResId(RID_SVXSTR_CHG_SMARTART))
133     , sChgToFromVisio(CuiResId(RID_SVXSTR_CHG_VISIO))
134     , sChgToFromPDF(CuiResId(RID_SVXSTR_CHG_PDF))
135     , m_xCheckLB(m_xBuilder->weld_tree_view("checklbcontainer"))
136     , m_xHighlightingRB(m_xBuilder->weld_radio_button("highlighting"))
137     , m_xShadingRB(m_xBuilder->weld_radio_button("shading"))
138     , m_xMSOLockFileCB(m_xBuilder->weld_check_button("mso_lockfile"))
139 {
140     std::vector<int> aWidths;
141     aWidths.push_back(m_xCheckLB->get_checkbox_column_width());
142     aWidths.push_back(m_xCheckLB->get_checkbox_column_width());
143     m_xCheckLB->set_column_fixed_widths(aWidths);
144 }
145 
~OfaMSFilterTabPage2()146 OfaMSFilterTabPage2::~OfaMSFilterTabPage2()
147 {
148 }
149 
Create(weld::Container * pPage,weld::DialogController * pController,const SfxItemSet * rAttrSet)150 std::unique_ptr<SfxTabPage> OfaMSFilterTabPage2::Create( weld::Container* pPage, weld::DialogController* pController,
151                                                 const SfxItemSet* rAttrSet )
152 {
153     return std::make_unique<OfaMSFilterTabPage2>( pPage, pController, *rAttrSet );
154 }
155 
FillItemSet(SfxItemSet *)156 bool OfaMSFilterTabPage2::FillItemSet( SfxItemSet* )
157 {
158     SvtFilterOptions& rOpt = SvtFilterOptions::Get();
159 
160     static struct ChkCBoxEntries{
161         MSFltrPg2_CheckBoxEntries eType;
162         bool (SvtFilterOptions:: *FnIs)() const;
163         void (SvtFilterOptions:: *FnSet)( bool bFlag );
164     } const aChkArr[] = {
165         { MSFltrPg2_CheckBoxEntries::Math,     &SvtFilterOptions::IsMathType2Math,
166                         &SvtFilterOptions::SetMathType2Math },
167         { MSFltrPg2_CheckBoxEntries::Math,     &SvtFilterOptions::IsMath2MathType,
168                         &SvtFilterOptions::SetMath2MathType },
169         { MSFltrPg2_CheckBoxEntries::Writer,   &SvtFilterOptions::IsWinWord2Writer,
170                         &SvtFilterOptions::SetWinWord2Writer },
171         { MSFltrPg2_CheckBoxEntries::Writer,   &SvtFilterOptions::IsWriter2WinWord,
172                         &SvtFilterOptions::SetWriter2WinWord },
173         { MSFltrPg2_CheckBoxEntries::Calc,     &SvtFilterOptions::IsExcel2Calc,
174                         &SvtFilterOptions::SetExcel2Calc },
175         { MSFltrPg2_CheckBoxEntries::Calc,     &SvtFilterOptions::IsCalc2Excel,
176                         &SvtFilterOptions::SetCalc2Excel },
177         { MSFltrPg2_CheckBoxEntries::Impress,  &SvtFilterOptions::IsPowerPoint2Impress,
178                         &SvtFilterOptions::SetPowerPoint2Impress },
179         { MSFltrPg2_CheckBoxEntries::Impress,  &SvtFilterOptions::IsImpress2PowerPoint,
180                         &SvtFilterOptions::SetImpress2PowerPoint },
181         { MSFltrPg2_CheckBoxEntries::SmartArt,  &SvtFilterOptions::IsSmartArt2Shape,
182                         &SvtFilterOptions::SetSmartArt2Shape },
183         { MSFltrPg2_CheckBoxEntries::Visio, &SvtFilterOptions::IsVisio2Draw,
184                         &SvtFilterOptions::SetVisio2Draw },
185     };
186 
187     bool bFirstCol = true;
188     for( const ChkCBoxEntries & rEntry : aChkArr )
189     {
190         // we loop through the list, alternating reading the first/second column,
191         // each row appears twice in the list (except for smartart and later entries, which are
192         // import only)
193         sal_uInt16 nCol = bFirstCol ? 0 : 1;
194         bFirstCol = !bFirstCol;
195         int nEntry = GetEntry4Type(rEntry.eType);
196         if (nEntry != -1)
197         {
198             bool bCheck = m_xCheckLB->get_toggle(nEntry, nCol);
199             if( bCheck != (rOpt.*rEntry.FnIs)() )
200                 (rOpt.*rEntry.FnSet)( bCheck );
201         }
202         if (rEntry.eType == MSFltrPg2_CheckBoxEntries::SmartArt)
203         {
204             bFirstCol = !bFirstCol;
205         }
206     }
207     int nPDFEntry = GetEntry4Type(MSFltrPg2_CheckBoxEntries::PDF);
208     bool bPDFCheck = m_xCheckLB->get_toggle(nPDFEntry, 0);
209     if (bPDFCheck != officecfg::Office::Common::Filter::Adobe::Import::PDFToDraw::get())
210     {
211         std::shared_ptr<comphelper::ConfigurationChanges> pBatch(
212             comphelper::ConfigurationChanges::create());
213         officecfg::Office::Common::Filter::Adobe::Import::PDFToDraw::set(bPDFCheck, pBatch);
214         pBatch->commit();
215     }
216 
217     if( m_xHighlightingRB->get_state_changed_from_saved() )
218     {
219         if (m_xHighlightingRB->get_active())
220             rOpt.SetCharBackground2Highlighting();
221         else
222             rOpt.SetCharBackground2Shading();
223     }
224 
225     if (m_xMSOLockFileCB->get_state_changed_from_saved())
226     {
227         rOpt.EnableMSOLockFileCreation(m_xMSOLockFileCB->get_active());
228     }
229 
230     return true;
231 }
232 
Reset(const SfxItemSet *)233 void OfaMSFilterTabPage2::Reset( const SfxItemSet* )
234 {
235     SvtFilterOptions& rOpt = SvtFilterOptions::Get();
236 
237     m_xCheckLB->freeze();
238     m_xCheckLB->clear();
239 
240     SvtModuleOptions aModuleOpt;
241 
242     // int the same sequence as the enums of MSFltrPg2_CheckBoxEntries
243     if ( aModuleOpt.IsModuleInstalled( SvtModuleOptions::EModule::MATH ) )
244         InsertEntry( sChgToFromMath, MSFltrPg2_CheckBoxEntries::Math );
245     if ( aModuleOpt.IsModuleInstalled( SvtModuleOptions::EModule::WRITER ) )
246         InsertEntry( sChgToFromWriter, MSFltrPg2_CheckBoxEntries::Writer );
247     if ( aModuleOpt.IsModuleInstalled( SvtModuleOptions::EModule::CALC ) )
248         InsertEntry( sChgToFromCalc, MSFltrPg2_CheckBoxEntries::Calc );
249     if ( aModuleOpt.IsModuleInstalled( SvtModuleOptions::EModule::IMPRESS ) )
250         InsertEntry( sChgToFromImpress, MSFltrPg2_CheckBoxEntries::Impress );
251     InsertEntry( sChgToFromSmartArt, MSFltrPg2_CheckBoxEntries::SmartArt, false );
252     if (aModuleOpt.IsModuleInstalled(SvtModuleOptions::EModule::DRAW))
253     {
254         InsertEntry(sChgToFromVisio, MSFltrPg2_CheckBoxEntries::Visio, false);
255         InsertEntry(sChgToFromPDF, MSFltrPg2_CheckBoxEntries::PDF, false);
256     }
257 
258     static struct ChkCBoxEntries{
259         MSFltrPg2_CheckBoxEntries eType;
260         bool (SvtFilterOptions:: *FnIs)() const;
261     } const aChkArr[] = {
262         { MSFltrPg2_CheckBoxEntries::Math,     &SvtFilterOptions::IsMathType2Math },
263         { MSFltrPg2_CheckBoxEntries::Math,     &SvtFilterOptions::IsMath2MathType },
264         { MSFltrPg2_CheckBoxEntries::Writer,   &SvtFilterOptions::IsWinWord2Writer },
265         { MSFltrPg2_CheckBoxEntries::Writer,   &SvtFilterOptions::IsWriter2WinWord },
266         { MSFltrPg2_CheckBoxEntries::Calc,     &SvtFilterOptions::IsExcel2Calc },
267         { MSFltrPg2_CheckBoxEntries::Calc,     &SvtFilterOptions::IsCalc2Excel },
268         { MSFltrPg2_CheckBoxEntries::Impress,  &SvtFilterOptions::IsPowerPoint2Impress },
269         { MSFltrPg2_CheckBoxEntries::Impress,  &SvtFilterOptions::IsImpress2PowerPoint },
270         { MSFltrPg2_CheckBoxEntries::SmartArt, &SvtFilterOptions::IsSmartArt2Shape },
271         { MSFltrPg2_CheckBoxEntries::Visio,    &SvtFilterOptions::IsVisio2Draw },
272         { MSFltrPg2_CheckBoxEntries::PDF,      nullptr },
273     };
274 
275     bool bFirstCol = true;
276     for( const ChkCBoxEntries & rArr : aChkArr )
277     {
278         // we loop through the list, alternating reading the first/second column,
279         // each row appears twice in the list (except for smartart and later entries, which are
280         // import only)
281         sal_uInt16 nCol = bFirstCol ? 0 : 1;
282         bFirstCol = !bFirstCol;
283         int nEntry = GetEntry4Type( rArr.eType );
284         if (nEntry != -1)
285         {
286             bool bCheck = false;
287             if (rArr.eType != MSFltrPg2_CheckBoxEntries::PDF)
288             {
289                 bCheck = (rOpt.*rArr.FnIs)();
290             }
291             else
292             {
293                 bCheck = officecfg::Office::Common::Filter::Adobe::Import::PDFToDraw::get();
294                 nCol = 0;
295             }
296             m_xCheckLB->set_toggle(nEntry, bCheck ? TRISTATE_TRUE : TRISTATE_FALSE, nCol);
297         }
298         if (rArr.eType == MSFltrPg2_CheckBoxEntries::SmartArt)
299         {
300             bFirstCol = !bFirstCol;
301         }
302     }
303     m_xCheckLB->thaw();
304 
305     if (rOpt.IsCharBackground2Highlighting())
306         m_xHighlightingRB->set_active(true);
307     else
308         m_xShadingRB->set_active(true);
309 
310     m_xHighlightingRB->save_state();
311 
312     m_xMSOLockFileCB->set_active(rOpt.IsMSOLockFileCreationIsEnabled());
313     m_xMSOLockFileCB->save_state();
314     m_xMSOLockFileCB->set_sensitive(!officecfg::Office::Common::Filter::Microsoft::Import::CreateMSOLockFiles::isReadOnly());
315 }
316 
InsertEntry(const OUString & _rTxt,MSFltrPg2_CheckBoxEntries _nType)317 void OfaMSFilterTabPage2::InsertEntry( const OUString& _rTxt, MSFltrPg2_CheckBoxEntries _nType )
318 {
319     InsertEntry( _rTxt, _nType, true );
320 }
321 
InsertEntry(const OUString & _rTxt,MSFltrPg2_CheckBoxEntries _nType,bool saveEnabled)322 void OfaMSFilterTabPage2::InsertEntry( const OUString& _rTxt, MSFltrPg2_CheckBoxEntries _nType,
323                                        bool saveEnabled )
324 {
325     int nPos = m_xCheckLB->n_children();
326     m_xCheckLB->append();
327     m_xCheckLB->set_toggle(nPos, TRISTATE_FALSE, 0);
328     if (saveEnabled)
329         m_xCheckLB->set_toggle(nPos, TRISTATE_FALSE, 1);
330     m_xCheckLB->set_text(nPos, _rTxt, 2);
331     m_xCheckLB->set_id(nPos, OUString::number(static_cast<sal_Int32>(_nType)));
332 }
333 
GetEntry4Type(MSFltrPg2_CheckBoxEntries _nType) const334 int OfaMSFilterTabPage2::GetEntry4Type( MSFltrPg2_CheckBoxEntries _nType ) const
335 {
336     for (int i = 0, nEntryCount = m_xCheckLB->n_children(); i < nEntryCount; ++i)
337     {
338         if (_nType == static_cast<MSFltrPg2_CheckBoxEntries>(m_xCheckLB->get_id(i).toInt32()))
339             return i;
340     }
341     return -1;
342 }
343 
344 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
345