xref: /AOO41X/main/filter/source/xsltdialog/xmlfiltertabdialog.cxx (revision 9e0fc027f109ec4ffcb6033aeec742a099701108)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_filter.hxx"
26 #include <com/sun/star/container/XNameAccess.hpp>
27 #ifndef _COM_SUN_STAR_BEANS_NAMEDVALUE_HPP_
28 #include <com/sun/star/beans/PropertyValue.hpp>
29 #endif
30 #include <tools/resid.hxx>
31 #include <vcl/msgbox.hxx>
32 #include <osl/file.hxx>
33 
34 #include "xmlfilterdialogstrings.hrc"
35 #include "xmlfiltertabdialog.hxx"
36 #include "xmlfiltertabdialog.hrc"
37 #include "xmlfiltertabpagebasic.hrc"
38 #include "xmlfiltertabpagexslt.hrc"
39 #include "xmlfiltertabpagebasic.hxx"
40 #include "xmlfiltertabpagexslt.hxx"
41 #include "xmlfiltersettingsdialog.hxx"
42 #include "xmlfiltersettingsdialog.hrc"
43 #include "xmlfilterhelpids.hrc"
44 
45 using namespace rtl;
46 using namespace com::sun::star::uno;
47 using namespace com::sun::star::container;
48 using namespace com::sun::star::beans;
49 using namespace com::sun::star::lang;
50 
XMLFilterTabDialog(Window * pParent,ResMgr & rResMgr,const Reference<XMultiServiceFactory> & rxMSF,const filter_info_impl * pInfo)51 XMLFilterTabDialog::XMLFilterTabDialog( Window *pParent, ResMgr& rResMgr, const Reference< XMultiServiceFactory >& rxMSF, const filter_info_impl* pInfo ) :
52     TabDialog( pParent, ResId( DLG_XML_FILTER_TABDIALOG, rResMgr ) ),
53     mxMSF( rxMSF ),
54     mrResMgr( rResMgr ),
55     maTabCtrl( this, ResId( 1, rResMgr ) ),
56     maOKBtn( this ),
57     maCancelBtn( this ),
58     maHelpBtn( this )
59 {
60     FreeResource();
61 
62     maTabCtrl.SetHelpId( HID_XML_FILTER_TABPAGE_CTRL );
63 
64     mpOldInfo = pInfo;
65     mpNewInfo = new filter_info_impl( *mpOldInfo );
66 
67     String aTitle( GetText() );
68     aTitle.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM("%s") ), mpNewInfo->maFilterName );
69     SetText( aTitle );
70 
71     maTabCtrl.Show();
72     maOKBtn.Show();
73     maCancelBtn.Show();
74     maHelpBtn.Show();
75 
76     maOKBtn.SetClickHdl( LINK( this, XMLFilterTabDialog, OkHdl ) );
77 
78     maTabCtrl.SetActivatePageHdl( LINK( this, XMLFilterTabDialog, ActivatePageHdl ) );
79     maTabCtrl.SetDeactivatePageHdl( LINK( this, XMLFilterTabDialog, DeactivatePageHdl ) );
80 
81     mpBasicPage = new XMLFilterTabPageBasic( &maTabCtrl, mrResMgr );
82     mpBasicPage->SetInfo( mpNewInfo );
83 
84     maTabCtrl.SetTabPage( RID_XML_FILTER_TABPAGE_BASIC, mpBasicPage );
85 
86     Size aSiz = mpBasicPage->GetSizePixel();
87     Size aCtrlSiz = maTabCtrl.GetTabPageSizePixel();
88     // set size on TabControl only if smaller than TabPage
89     if ( aCtrlSiz.Width() < aSiz.Width() || aCtrlSiz.Height() < aSiz.Height() )
90     {
91         maTabCtrl.SetTabPageSizePixel( aSiz );
92         aCtrlSiz = aSiz;
93     }
94 
95     mpXSLTPage = new XMLFilterTabPageXSLT( &maTabCtrl, mrResMgr, mxMSF );
96     mpXSLTPage->SetInfo( mpNewInfo );
97 
98     maTabCtrl.SetTabPage( RID_XML_FILTER_TABPAGE_XSLT, mpXSLTPage );
99 
100     aSiz = mpXSLTPage->GetSizePixel();
101     if ( aCtrlSiz.Width() < aSiz.Width() || aCtrlSiz.Height() < aSiz.Height() )
102     {
103         maTabCtrl.SetTabPageSizePixel( aSiz );
104         aCtrlSiz = aSiz;
105     }
106 
107     ActivatePageHdl( &maTabCtrl );
108 
109     AdjustLayout();
110 }
111 
112 // -----------------------------------------------------------------------
113 
~XMLFilterTabDialog()114 XMLFilterTabDialog::~XMLFilterTabDialog()
115 {
116     delete mpBasicPage;
117     delete mpXSLTPage;
118     delete mpNewInfo;
119 }
120 
121 // -----------------------------------------------------------------------
122 
onOk()123 bool XMLFilterTabDialog::onOk()
124 {
125     mpXSLTPage->FillInfo( mpNewInfo );
126     mpBasicPage->FillInfo( mpNewInfo );
127 
128     sal_uInt16 nErrorPage = 0;
129     sal_uInt16 nErrorId = 0;
130     Window* pFocusWindow = NULL;
131     String aReplace1;
132     String aReplace2;
133 
134     // 1. see if the filter name is ok
135     if( (mpNewInfo->maFilterName.getLength() == 0) || (mpNewInfo->maFilterName != mpOldInfo->maFilterName) )
136     {
137         // if the user deleted the filter name, we reset the original filter name
138         if( mpNewInfo->maFilterName.getLength() == 0 )
139         {
140             mpNewInfo->maFilterName = mpOldInfo->maFilterName;
141         }
142         else
143         {
144             try
145             {
146                 Reference< XNameAccess > xFilterContainer( mxMSF->createInstance( OUString::createFromAscii("com.sun.star.document.FilterFactory" ) ), UNO_QUERY );
147                 if( xFilterContainer.is() )
148                 {
149                     if( xFilterContainer->hasByName( mpNewInfo->maFilterName ) )
150                     {
151                         nErrorPage = RID_XML_FILTER_TABPAGE_BASIC;
152                         nErrorId = STR_ERROR_FILTER_NAME_EXISTS;
153                         pFocusWindow = &(mpBasicPage->maEDFilterName);
154                         aReplace1 = mpNewInfo->maFilterName;
155                     }
156 
157                 }
158             }
159             catch( Exception& )
160             {
161                 DBG_ERROR( "XMLFilterTabDialog::onOk exception catched!" );
162             }
163         }
164     }
165 
166     // 2. see if the interface name is ok
167     if( (mpNewInfo->maInterfaceName.getLength() == 0) || (mpNewInfo->maInterfaceName != mpOldInfo->maInterfaceName) )
168     {
169         // if the user deleted the interface name, we reset the original filter name
170         if( mpNewInfo->maInterfaceName.getLength() == 0 )
171         {
172             mpNewInfo->maInterfaceName = mpOldInfo->maInterfaceName;
173         }
174         else
175         {
176             try
177             {
178                 Reference< XNameAccess > xFilterContainer( mxMSF->createInstance( OUString::createFromAscii("com.sun.star.document.FilterFactory" ) ), UNO_QUERY );
179                 if( xFilterContainer.is() )
180                 {
181                     Sequence< OUString > aFilterNames( xFilterContainer->getElementNames() );
182                     OUString* pFilterName = aFilterNames.getArray();
183 
184                     const sal_Int32 nCount = aFilterNames.getLength();
185                     sal_Int32 nFilter;
186 
187                     Sequence< PropertyValue > aValues;
188                     for( nFilter = 0; (nFilter < nCount) && (nErrorId == 0); nFilter++, pFilterName++ )
189                     {
190                         Any aAny( xFilterContainer->getByName( *pFilterName ) );
191                         if( !(aAny >>= aValues) )
192                             continue;
193 
194                         const sal_Int32 nValueCount( aValues.getLength() );
195                         PropertyValue* pValues = aValues.getArray();
196                         sal_Int32 nValue;
197 
198                         for( nValue = 0; (nValue < nValueCount) && (nErrorId == 0); nValue++, pValues++ )
199                         {
200                             if( pValues->Name.equalsAscii( "UIName" ) )
201                             {
202                                 OUString aInterfaceName;
203                                 pValues->Value >>= aInterfaceName;
204                                 if( aInterfaceName == mpNewInfo->maInterfaceName )
205                                 {
206                                     nErrorPage = RID_XML_FILTER_TABPAGE_BASIC;
207                                     nErrorId = STR_ERROR_TYPE_NAME_EXISTS;
208                                     pFocusWindow = &(mpBasicPage->maEDInterfaceName);
209                                     aReplace1 = mpNewInfo->maInterfaceName;
210                                     aReplace2 = *pFilterName;
211                                 }
212                             }
213                         }
214                     }
215                 }
216             }
217             catch( Exception& )
218             {
219                 DBG_ERROR( "XMLFilterTabDialog::onOk exception catched!" );
220             }
221         }
222     }
223 
224     // 3. see if the dtd is valid
225     if( 0 == nErrorId )
226     {
227         if( (mpNewInfo->maDTD != mpOldInfo->maDTD) && isFileURL( mpNewInfo->maDTD ) )
228         {
229             osl::File aFile( mpNewInfo->maDTD );
230             osl::File::RC aRC = aFile.open( OpenFlag_Read );
231             if( aRC != osl::File::E_None )
232             {
233                 nErrorId = STR_ERROR_DTD_NOT_FOUND;
234                 nErrorPage = RID_XML_FILTER_TABPAGE_XSLT;
235                 pFocusWindow = &(mpXSLTPage->maEDDTDSchema);
236             }
237         }
238     }
239 
240     if( 0 == nErrorId )
241     {
242         // 4. see if the export xslt is valid
243         if( (mpNewInfo->maExportXSLT != mpOldInfo->maExportXSLT) && isFileURL( mpNewInfo->maExportXSLT ) )
244         {
245             osl::File aFile( mpNewInfo->maExportXSLT );
246             osl::File::RC aRC = aFile.open( OpenFlag_Read );
247             if( aRC != osl::File::E_None )
248             {
249                 nErrorId = STR_ERROR_EXPORT_XSLT_NOT_FOUND;
250                 nErrorPage = RID_XML_FILTER_TABPAGE_XSLT;
251                 pFocusWindow = &(mpXSLTPage->maEDExportXSLT);
252             }
253         }
254     }
255 
256     if( 0 == nErrorId )
257     {
258         // 5. see if the import xslt is valid
259         if( (mpNewInfo->maImportXSLT != mpOldInfo->maImportXSLT) && isFileURL( mpNewInfo->maImportXSLT ) )
260         {
261             osl::File aFile( mpNewInfo->maImportXSLT );
262             osl::File::RC aRC = aFile.open( OpenFlag_Read );
263             if( aRC != osl::File::E_None )
264             {
265                 nErrorId = STR_ERROR_IMPORT_XSLT_NOT_FOUND;
266                 nErrorPage = RID_XML_FILTER_TABPAGE_XSLT;
267                 pFocusWindow = &(mpXSLTPage->maEDImportTemplate);
268             }
269         }
270     }
271 
272     // see if we have at least an import or an export dtd
273     if((mpNewInfo->maImportXSLT.getLength() == 0) && (mpNewInfo->maExportXSLT.getLength() == 0) )
274     {
275         nErrorId = STR_ERROR_EXPORT_XSLT_NOT_FOUND;
276         nErrorPage = RID_XML_FILTER_TABPAGE_XSLT;
277         pFocusWindow = &(mpXSLTPage->maEDExportXSLT);
278     }
279 
280     if( 0 == nErrorId )
281     {
282         // 6. see if the import template is valid
283         if( (mpNewInfo->maImportTemplate != mpOldInfo->maImportTemplate) && isFileURL( mpNewInfo->maImportTemplate ) )
284         {
285             osl::File aFile( mpNewInfo->maImportTemplate );
286             osl::File::RC aRC = aFile.open( OpenFlag_Read );
287             if( aRC != osl::File::E_None )
288             {
289                 nErrorId = STR_ERROR_IMPORT_TEMPLATE_NOT_FOUND;
290                 nErrorPage = RID_XML_FILTER_TABPAGE_XSLT;
291                 pFocusWindow = &(mpXSLTPage->maEDImportTemplate);
292             }
293         }
294     }
295 
296     if( 0 != nErrorId )
297     {
298         maTabCtrl.SetCurPageId( (sal_uInt16)nErrorPage );
299         ActivatePageHdl( &maTabCtrl );
300 
301         ResId aResId( nErrorId, mrResMgr );
302         String aMessage( aResId );
303 
304         if( aReplace2.Len() )
305         {
306             aMessage.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM("%s1") ), aReplace1 );
307             aMessage.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM("%s2") ), aReplace2 );
308         }
309         else if( aReplace1.Len() )
310         {
311             aMessage.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM("%s") ), aReplace1 );
312         }
313 
314         ErrorBox aBox(this, (WinBits)(WB_OK), aMessage );
315         aBox.Execute();
316 
317         if( pFocusWindow )
318             pFocusWindow->GrabFocus();
319 
320         return false;
321     }
322     else
323     {
324         return true;
325     }
326 }
327 
328 // -----------------------------------------------------------------------
329 
getNewFilterInfo() const330 filter_info_impl* XMLFilterTabDialog::getNewFilterInfo() const
331 {
332     return mpNewInfo;
333 }
334 
335 // -----------------------------------------------------------------------
336 
337 IMPL_LINK( XMLFilterTabDialog, CancelHdl, Button*, /* pButton */ )
338 {
339     Close();
340     return 0;
341 }
342 
343 // -----------------------------------------------------------------------
344 
IMPL_LINK(XMLFilterTabDialog,OkHdl,Button *,EMPTYARG)345 IMPL_LINK( XMLFilterTabDialog, OkHdl, Button *, EMPTYARG )
346 {
347     if( onOk() )
348         EndDialog(1);
349 
350     return 0;
351 }
352 
353 // -----------------------------------------------------------------------
354 
IMPL_LINK(XMLFilterTabDialog,ActivatePageHdl,TabControl *,pTabCtrl)355 IMPL_LINK( XMLFilterTabDialog, ActivatePageHdl, TabControl *, pTabCtrl )
356 {
357     const sal_uInt16 nId = pTabCtrl->GetCurPageId();
358     TabPage* pTabPage = pTabCtrl->GetTabPage( nId );
359     pTabPage->Show();
360 
361     return 0;
362 }
363 
364 // -----------------------------------------------------------------------
365 
366 IMPL_LINK( XMLFilterTabDialog, DeactivatePageHdl, TabControl *, /* pTabCtrl */ )
367 {
368     return sal_True;
369 }
370