tdf#164627 docx export: consolidate getWordCompatibilityMode()
[LibreOffice.git] / filter / source / xsltdialog / xmlfiltertabdialog.cxx
blob47c018636d3a915a14e8240ab4e49e2ce3c6453c
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 <com/sun/star/container/XNameAccess.hpp>
21 #include <com/sun/star/beans/PropertyValue.hpp>
22 #include <com/sun/star/uno/XComponentContext.hpp>
23 #include <comphelper/fileurl.hxx>
24 #include <comphelper/diagnose_ex.hxx>
25 #include <vcl/svapp.hxx>
26 #include <vcl/weld.hxx>
27 #include <osl/file.hxx>
29 #include <strings.hrc>
30 #include "xmlfiltertabdialog.hxx"
31 #include "xmlfiltertabpagebasic.hxx"
32 #include "xmlfiltertabpagexslt.hxx"
33 #include "xmlfiltercommon.hxx"
35 using namespace com::sun::star::uno;
36 using namespace com::sun::star::container;
37 using namespace com::sun::star::beans;
38 using namespace com::sun::star::lang;
40 XMLFilterTabDialog::XMLFilterTabDialog(weld::Window *pParent,
41 const Reference< XComponentContext >& rxContext, const filter_info_impl* pInfo)
42 : GenericDialogController(pParent, u"filter/ui/xsltfilterdialog.ui"_ustr, u"XSLTFilterDialog"_ustr)
43 , mxContext(rxContext)
44 , m_xTabCtrl(m_xBuilder->weld_notebook(u"tabcontrol"_ustr))
45 , m_xOKBtn(m_xBuilder->weld_button(u"ok"_ustr))
46 , mpBasicPage(new XMLFilterTabPageBasic(m_xTabCtrl->get_page(u"general"_ustr)))
47 , mpXSLTPage(new XMLFilterTabPageXSLT(m_xTabCtrl->get_page(u"transformation"_ustr), m_xDialog.get()))
49 mpOldInfo = pInfo;
50 mpNewInfo.reset( new filter_info_impl( *mpOldInfo ) );
52 OUString aTitle(m_xDialog->get_title());
53 aTitle = aTitle.replaceAll("%s", mpNewInfo->maFilterName);
54 m_xDialog->set_title(aTitle);
56 m_xOKBtn->connect_clicked( LINK( this, XMLFilterTabDialog, OkHdl ) );
58 mpBasicPage->SetInfo( mpNewInfo.get() );
59 mpXSLTPage->SetInfo( mpNewInfo.get() );
62 XMLFilterTabDialog::~XMLFilterTabDialog()
66 bool XMLFilterTabDialog::onOk()
68 mpXSLTPage->FillInfo( mpNewInfo.get() );
69 mpBasicPage->FillInfo( mpNewInfo.get() );
71 OUString sErrorPage;
72 TranslateId pErrorId;
73 weld::Widget* pFocusWindow = nullptr;
74 OUString aReplace1;
75 OUString aReplace2;
77 // 1. see if the filter name is ok
78 if( (mpNewInfo->maFilterName.isEmpty()) || (mpNewInfo->maFilterName != mpOldInfo->maFilterName) )
80 // if the user deleted the filter name, we reset the original filter name
81 if( mpNewInfo->maFilterName.isEmpty() )
83 mpNewInfo->maFilterName = mpOldInfo->maFilterName;
85 else
87 try
89 Reference< XNameAccess > xFilterContainer( mxContext->getServiceManager()->createInstanceWithContext( u"com.sun.star.document.FilterFactory"_ustr, mxContext ), UNO_QUERY );
90 if( xFilterContainer.is() )
92 if( xFilterContainer->hasByName( mpNewInfo->maFilterName ) )
94 sErrorPage = "general";
95 pErrorId = STR_ERROR_FILTER_NAME_EXISTS;
96 pFocusWindow = mpBasicPage->m_xEDFilterName.get();
97 aReplace1 = mpNewInfo->maFilterName;
102 catch( const Exception& )
104 TOOLS_WARN_EXCEPTION("filter.xslt", "");
109 // 2. see if the interface name is ok
110 if( (mpNewInfo->maInterfaceName.isEmpty()) || (mpNewInfo->maInterfaceName != mpOldInfo->maInterfaceName) )
112 // if the user deleted the interface name, we reset the original filter name
113 if( mpNewInfo->maInterfaceName.isEmpty() )
115 mpNewInfo->maInterfaceName = mpOldInfo->maInterfaceName;
117 else
121 Reference< XNameAccess > xFilterContainer( mxContext->getServiceManager()->createInstanceWithContext( u"com.sun.star.document.FilterFactory"_ustr, mxContext ), UNO_QUERY );
122 if( xFilterContainer.is() )
124 Sequence< OUString > aFilterNames( xFilterContainer->getElementNames() );
125 OUString* pFilterName = aFilterNames.getArray();
127 const sal_Int32 nCount = aFilterNames.getLength();
128 sal_Int32 nFilter;
130 Sequence< PropertyValue > aValues;
131 for( nFilter = 0; (nFilter < nCount) && !pErrorId; nFilter++, pFilterName++ )
133 Any aAny( xFilterContainer->getByName( *pFilterName ) );
134 if( !(aAny >>= aValues) )
135 continue;
137 const sal_Int32 nValueCount( aValues.getLength() );
138 PropertyValue* pValues = aValues.getArray();
139 sal_Int32 nValue;
141 for( nValue = 0; (nValue < nValueCount) && !pErrorId; nValue++, pValues++ )
143 if ( pValues->Name == "UIName" )
145 OUString aInterfaceName;
146 pValues->Value >>= aInterfaceName;
147 if( aInterfaceName == mpNewInfo->maInterfaceName )
149 sErrorPage = "general";
150 pErrorId = STR_ERROR_TYPE_NAME_EXISTS;
151 pFocusWindow = mpBasicPage->m_xEDInterfaceName.get();
152 aReplace1 = mpNewInfo->maInterfaceName;
153 aReplace2 = *pFilterName;
160 catch( const Exception& )
162 TOOLS_WARN_EXCEPTION("filter.xslt", "");
167 if (!pErrorId)
169 // 4. see if the export xslt is valid
170 if( (mpNewInfo->maExportXSLT != mpOldInfo->maExportXSLT) && comphelper::isFileUrl( mpNewInfo->maExportXSLT ) )
172 osl::File aFile( mpNewInfo->maExportXSLT );
173 osl::File::RC aRC = aFile.open( osl_File_OpenFlag_Read );
174 if( aRC != osl::File::E_None )
176 pErrorId = STR_ERROR_EXPORT_XSLT_NOT_FOUND;
177 sErrorPage = "transformation";
178 pFocusWindow = mpXSLTPage->m_xEDExportXSLT->getWidget();
183 if (!pErrorId)
185 // 5. see if the import xslt is valid
186 if( (mpNewInfo->maImportXSLT != mpOldInfo->maImportXSLT) && comphelper::isFileUrl( mpNewInfo->maImportXSLT ) )
188 osl::File aFile( mpNewInfo->maImportXSLT );
189 osl::File::RC aRC = aFile.open( osl_File_OpenFlag_Read );
190 if( aRC != osl::File::E_None )
192 pErrorId = STR_ERROR_IMPORT_XSLT_NOT_FOUND;
193 sErrorPage = "transformation";
194 pFocusWindow = mpXSLTPage->m_xEDImportTemplate->getWidget();
199 // see if we have at least an import or an export xslt
200 if((mpNewInfo->maImportXSLT.isEmpty()) && (mpNewInfo->maExportXSLT.isEmpty()) )
202 pErrorId = STR_ERROR_EXPORT_XSLT_NOT_FOUND;
203 sErrorPage = "transformation";
204 pFocusWindow = mpXSLTPage->m_xEDExportXSLT->getWidget();
207 if (!pErrorId)
209 // 6. see if the import template is valid
210 if( (mpNewInfo->maImportTemplate != mpOldInfo->maImportTemplate) && comphelper::isFileUrl( mpNewInfo->maImportTemplate ) )
212 osl::File aFile( mpNewInfo->maImportTemplate );
213 osl::File::RC aRC = aFile.open( osl_File_OpenFlag_Read );
214 if( aRC != osl::File::E_None )
216 pErrorId = STR_ERROR_IMPORT_TEMPLATE_NOT_FOUND;
217 sErrorPage = "transformation";
218 pFocusWindow = mpXSLTPage->m_xEDImportTemplate->getWidget();
223 if (pErrorId)
225 m_xTabCtrl->set_current_page(sErrorPage);
227 OUString aMessage(FilterResId(pErrorId));
229 if( aReplace2.getLength() )
231 aMessage = aMessage.replaceAll( "%s1", aReplace1 );
232 aMessage = aMessage.replaceAll( "%s2", aReplace2 );
234 else if( aReplace1.getLength() )
236 aMessage = aMessage.replaceAll( "%s", aReplace1 );
239 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
240 VclMessageType::Warning, VclButtonsType::Ok,
241 aMessage));
242 xBox->run();
244 if( pFocusWindow )
245 pFocusWindow->grab_focus();
247 return false;
249 else
251 return true;
255 IMPL_LINK_NOARG(XMLFilterTabDialog, OkHdl, weld::Button&, void)
257 if( onOk() )
258 m_xDialog->response(RET_OK);
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */