merge the formfield patch from ooo-build
[ooovba.git] / xmlsecurity / source / dialogs / macrosecurity.cxx
blob2eec8d9b24d9f6b56363caff82b054a5b109605a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: macrosecurity.cxx,v $
10 * $Revision: 1.31 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
34 #include <xmlsecurity/macrosecurity.hxx>
35 #include <xmlsecurity/certificatechooser.hxx>
36 #include <xmlsecurity/certificateviewer.hxx>
37 #include <xmlsecurity/biginteger.hxx>
39 #include <osl/file.hxx>
40 #include <vcl/help.hxx>
43 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
44 #include <com/sun/star/security/SerialNumberAdapter.hpp>
45 #include <comphelper/sequence.hxx>
46 #include <sfx2/filedlghelper.hxx>
47 #include <svtools/pickerhelper.hxx>
48 #include <comphelper/processfactory.hxx>
49 #include <com/sun/star/uno/Exception.hpp>
50 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
51 #include <com/sun/star/ui/dialogs/XFolderPicker.hpp>
52 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
53 #include <tools/urlobj.hxx>
55 #include <vcl/msgbox.hxx>
57 #include "dialogs.hrc"
58 #include "resourcemanager.hxx"
60 /* HACK: disable some warnings for MS-C */
61 #ifdef _MSC_VER
62 #pragma warning (disable : 4355) // 4355: this used in initializer-list
63 #endif
65 using namespace ::com::sun::star;
68 IMPL_LINK( MacroSecurity, OkBtnHdl, void*, EMPTYARG )
70 mpLevelTP->ClosePage();
71 mpTrustSrcTP->ClosePage();
73 EndDialog( RET_OK );
75 return 0;
78 MacroSecurity::MacroSecurity( Window* _pParent, const cssu::Reference< cssu::XComponentContext> &_rxCtx, const cssu::Reference< dcss::xml::crypto::XSecurityEnvironment >& _rxSecurityEnvironment )
79 :TabDialog ( _pParent, XMLSEC_RES( RID_XMLSECTP_MACROSEC ) )
80 ,maTabCtrl ( this, XMLSEC_RES( 1 ) )
81 ,maOkBtn ( this, XMLSEC_RES( BTN_OK ) )
82 ,maCancelBtn ( this, XMLSEC_RES( BTN_CANCEL ) )
83 ,maHelpBtn ( this, XMLSEC_RES( BTN_HELP ) )
84 ,maResetBtn ( this, XMLSEC_RES( BTN_RESET ) )
86 FreeResource();
88 mxCtx = _rxCtx;
89 mxSecurityEnvironment = _rxSecurityEnvironment;
91 mpLevelTP = new MacroSecurityLevelTP( &maTabCtrl, this );
92 mpTrustSrcTP = new MacroSecurityTrustedSourcesTP( &maTabCtrl, this );
94 maTabCtrl.SetTabPage( RID_XMLSECTP_SECLEVEL, mpLevelTP );
95 maTabCtrl.SetTabPage( RID_XMLSECTP_TRUSTSOURCES, mpTrustSrcTP );
96 maTabCtrl.SetCurPageId( RID_XMLSECTP_SECLEVEL );
98 maOkBtn.SetClickHdl( LINK( this, MacroSecurity, OkBtnHdl ) );
101 MacroSecurity::~MacroSecurity()
103 delete maTabCtrl.GetTabPage( RID_XMLSECTP_TRUSTSOURCES );
104 delete maTabCtrl.GetTabPage( RID_XMLSECTP_SECLEVEL );
108 MacroSecurityTP::MacroSecurityTP( Window* _pParent, const ResId& _rResId, MacroSecurity* _pDlg )
109 :TabPage ( _pParent, _rResId )
110 ,mpDlg ( _pDlg )
114 MacroSecurityLevelTP::MacroSecurityLevelTP( Window* _pParent, MacroSecurity* _pDlg )
115 :MacroSecurityTP ( _pParent, XMLSEC_RES( RID_XMLSECTP_SECLEVEL ), _pDlg )
116 ,maSecLevelFL ( this, XMLSEC_RES( FL_SECLEVEL ) )
117 ,maSecReadonlyFI ( this, XMLSEC_RES( FI_SEC_READONLY ))
118 ,maVeryHighRB ( this, XMLSEC_RES( RB_VERYHIGH ) )
119 ,maHighRB ( this, XMLSEC_RES( RB_HIGH ) )
120 ,maMediumRB ( this, XMLSEC_RES( RB_MEDIUM ) )
121 ,maLowRB ( this, XMLSEC_RES( RB_LOW ) )
123 FreeResource();
125 maLowRB.SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
126 maMediumRB.SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
127 maHighRB.SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
128 maVeryHighRB.SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
130 mnCurLevel = (USHORT) mpDlg->maSecOptions.GetMacroSecurityLevel();
131 sal_Bool bReadonly = mpDlg->maSecOptions.IsReadOnly( SvtSecurityOptions::E_MACRO_SECLEVEL );
133 RadioButton* pCheck = 0;
134 switch( mnCurLevel )
136 case 3: pCheck = &maVeryHighRB; break;
137 case 2: pCheck = &maHighRB; break;
138 case 1: pCheck = &maMediumRB; break;
139 case 0: pCheck = &maLowRB; break;
141 if(pCheck)
142 pCheck->Check();
143 else
145 DBG_ERROR("illegal macro security level");
147 maSecReadonlyFI.Show(bReadonly);
148 if(bReadonly)
150 //move to the selected button
151 if( pCheck && pCheck != &maVeryHighRB)
153 long nDiff = pCheck->GetPosPixel().Y() - maVeryHighRB.GetPosPixel().Y();
154 Point aPos(maSecReadonlyFI.GetPosPixel());
155 aPos.Y() += nDiff;
156 maSecReadonlyFI.SetPosPixel(aPos);
158 maVeryHighRB.Enable(sal_False);
159 maHighRB.Enable(sal_False);
160 maMediumRB.Enable(sal_False);
161 maLowRB.Enable(sal_False);
166 IMPL_LINK( MacroSecurityLevelTP, RadioButtonHdl, RadioButton*, EMPTYARG )
168 USHORT nNewLevel = 0;
169 if( maVeryHighRB.IsChecked() )
170 nNewLevel = 3;
171 else if( maHighRB.IsChecked() )
172 nNewLevel = 2;
173 else if( maMediumRB.IsChecked() )
174 nNewLevel = 1;
176 if ( nNewLevel != mnCurLevel )
178 mnCurLevel = nNewLevel;
179 mpDlg->EnableReset();
182 return 0;
185 void MacroSecurityLevelTP::ClosePage( void )
187 mpDlg->maSecOptions.SetMacroSecurityLevel( mnCurLevel );
190 void MacroSecurityTrustedSourcesTP::ImplCheckButtons()
192 bool bCertSelected = maTrustCertLB.FirstSelected() != NULL;
193 maViewCertPB.Enable( bCertSelected );
194 maRemoveCertPB.Enable( bCertSelected && !mbAuthorsReadonly);
196 bool bLocationSelected = maTrustFileLocLB.GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND;
197 maRemoveLocPB.Enable( bLocationSelected && !mbURLsReadonly);
201 IMPL_LINK( MacroSecurityTrustedSourcesTP, ViewCertPBHdl, void*, EMPTYARG )
203 if( maTrustCertLB.FirstSelected() )
205 USHORT nSelected = USHORT( sal_uIntPtr( maTrustCertLB.FirstSelected()->GetUserData() ) );
207 uno::Reference< dcss::security::XSerialNumberAdapter > xSerialNumberAdapter =
208 ::com::sun::star::security::SerialNumberAdapter::create(mpDlg->mxCtx);
210 uno::Reference< dcss::security::XCertificate > xCert = mpDlg->mxSecurityEnvironment->getCertificate( maTrustedAuthors[nSelected][0], xSerialNumberAdapter->toSequence( maTrustedAuthors[nSelected][1] ) );
212 // If we don't get it, create it from signature data:
213 if ( !xCert.is() )
214 xCert = mpDlg->mxSecurityEnvironment->createCertificateFromAscii( maTrustedAuthors[nSelected][2] ) ;
216 DBG_ASSERT( xCert.is(), "*MacroSecurityTrustedSourcesTP::ViewCertPBHdl(): Certificate not found and can't be created!" );
218 if ( xCert.is() )
220 CertificateViewer aViewer( this, mpDlg->mxSecurityEnvironment, xCert, FALSE );
221 aViewer.Execute();
224 return 0;
227 IMPL_LINK( MacroSecurityTrustedSourcesTP, RemoveCertPBHdl, void*, EMPTYARG )
229 if( maTrustCertLB.FirstSelected() )
231 USHORT nAuthor = USHORT( sal_uIntPtr( maTrustCertLB.FirstSelected()->GetUserData() ) );
232 ::comphelper::removeElementAt( maTrustedAuthors, nAuthor );
234 FillCertLB();
235 ImplCheckButtons();
238 return 0;
241 IMPL_LINK( MacroSecurityTrustedSourcesTP, AddLocPBHdl, void*, EMPTYARG )
245 rtl::OUString aService( RTL_CONSTASCII_USTRINGPARAM( FOLDER_PICKER_SERVICE_NAME ) );
246 uno::Reference < lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
247 uno::Reference < ui::dialogs::XFolderPicker > xFolderPicker( xFactory->createInstance( aService ), uno::UNO_QUERY );
249 short nRet = xFolderPicker->execute();
251 if( ui::dialogs::ExecutableDialogResults::OK != nRet )
252 return 0;
254 rtl::OUString aPathStr = xFolderPicker->getDirectory();
255 INetURLObject aNewObj( aPathStr );
256 aNewObj.removeFinalSlash();
258 // then the new path also an URL else system path
259 ::rtl::OUString aSystemFileURL = ( aNewObj.GetProtocol() != INET_PROT_NOT_VALID ) ?
260 aPathStr : aNewObj.getFSysPath( INetURLObject::FSYS_DETECT );
262 String aNewPathStr(aSystemFileURL);
264 if ( osl::FileBase::getSystemPathFromFileURL( aSystemFileURL, aSystemFileURL ) == osl::FileBase::E_None )
265 aNewPathStr = aSystemFileURL;
267 if( maTrustFileLocLB.GetEntryPos( aNewPathStr ) == LISTBOX_ENTRY_NOTFOUND )
269 maTrustFileLocLB.InsertEntry( aNewPathStr );
272 ImplCheckButtons();
274 catch( uno::Exception& )
276 DBG_ERRORFILE( "MacroSecurityTrustedSourcesTP::AddLocPBHdl(): exception from folder picker" );
279 return 0;
282 IMPL_LINK( MacroSecurityTrustedSourcesTP, RemoveLocPBHdl, void*, EMPTYARG )
284 USHORT nSel = maTrustFileLocLB.GetSelectEntryPos();
285 if( nSel != LISTBOX_ENTRY_NOTFOUND )
287 maTrustFileLocLB.RemoveEntry( nSel );
288 // --> PB 2004-09-21 #i33584#
289 // after remove an entry, select another one if exists
290 USHORT nNewCount = maTrustFileLocLB.GetEntryCount();
291 if ( nNewCount > 0 )
293 if ( nSel >= nNewCount )
294 nSel = nNewCount - 1;
295 maTrustFileLocLB.SelectEntryPos( nSel );
297 // <--
298 ImplCheckButtons();
301 return 0;
304 IMPL_LINK( MacroSecurityTrustedSourcesTP, TrustCertLBSelectHdl, void*, EMPTYARG )
306 ImplCheckButtons();
307 return 0;
310 IMPL_LINK( MacroSecurityTrustedSourcesTP, TrustFileLocLBSelectHdl, void*, EMPTYARG )
312 ImplCheckButtons();
313 return 0;
316 void MacroSecurityTrustedSourcesTP::FillCertLB( void )
318 maTrustCertLB.Clear();
320 sal_uInt32 nEntries = maTrustedAuthors.getLength();
322 if ( nEntries && mpDlg->mxSecurityEnvironment.is() )
324 for( sal_uInt32 nEntry = 0 ; nEntry < nEntries ; ++nEntry )
326 cssu::Sequence< ::rtl::OUString >& rEntry = maTrustedAuthors[ nEntry ];
327 uno::Reference< css::security::XCertificate > xCert;
329 // create from RawData
330 xCert = mpDlg->mxSecurityEnvironment->createCertificateFromAscii( rEntry[ 2 ] );
332 SvLBoxEntry* pLBEntry = maTrustCertLB.InsertEntry( XmlSec::GetContentPart( xCert->getSubjectName() ) );
333 maTrustCertLB.SetEntryText( XmlSec::GetContentPart( xCert->getIssuerName() ), pLBEntry, 1 );
334 maTrustCertLB.SetEntryText( XmlSec::GetDateTimeString( xCert->getNotValidAfter() ), pLBEntry, 2 );
335 pLBEntry->SetUserData( ( void* ) sal_Int32( nEntry ) ); // missuse user data as index
340 MacroSecurityTrustedSourcesTP::MacroSecurityTrustedSourcesTP( Window* _pParent, MacroSecurity* _pDlg )
341 :MacroSecurityTP ( _pParent, XMLSEC_RES( RID_XMLSECTP_TRUSTSOURCES ), _pDlg )
342 ,maTrustCertFL ( this, XMLSEC_RES( FL_TRUSTCERT ) )
343 ,maTrustCertROFI ( this, XMLSEC_RES( FI_TRUSTCERT_RO ) )
344 ,maTrustCertLB ( this, XMLSEC_RES( LB_TRUSTCERT ) )
345 ,maAddCertPB ( this, XMLSEC_RES( PB_ADD_TRUSTCERT ) )
346 ,maViewCertPB ( this, XMLSEC_RES( PB_VIEW_TRUSTCERT ) )
347 ,maRemoveCertPB ( this, XMLSEC_RES( PB_REMOVE_TRUSTCERT ) )
348 ,maTrustFileLocFL ( this, XMLSEC_RES( FL_TRUSTFILELOC ) )
349 ,maTrustFileROFI ( this, XMLSEC_RES( FI_TRUSTFILE_RO ) )
350 ,maTrustFileLocFI ( this, XMLSEC_RES( FI_TRUSTFILELOC ) )
351 ,maTrustFileLocLB ( this, XMLSEC_RES( LB_TRUSTFILELOC ) )
352 ,maAddLocPB ( this, XMLSEC_RES( FL_ADD_TRUSTFILELOC ) )
353 ,maRemoveLocPB ( this, XMLSEC_RES( FL_REMOVE_TRUSTFILELOC ) )
355 static long nTabs[] = { 3, 0, 35*CS_LB_WIDTH/100, 70*CS_LB_WIDTH/100 };
356 maTrustCertLB.SetTabs( &nTabs[ 0 ] );
357 maTrustCertLB.InsertHeaderEntry( String( XMLSEC_RES( STR_HEADERBAR ) ) );
359 FreeResource();
361 maTrustCertLB.SetSelectHdl( LINK( this, MacroSecurityTrustedSourcesTP, TrustCertLBSelectHdl ) );
362 maAddCertPB.Hide(); // not used in the moment...
363 maViewCertPB.SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, ViewCertPBHdl ) );
364 maViewCertPB.Disable();
365 maRemoveCertPB.SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, RemoveCertPBHdl ) );
366 maRemoveCertPB.Disable();
368 maTrustFileLocLB.SetSelectHdl( LINK( this, MacroSecurityTrustedSourcesTP, TrustFileLocLBSelectHdl ) );
369 maAddLocPB.SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, AddLocPBHdl ) );
370 maRemoveLocPB.SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, RemoveLocPBHdl ) );
371 maRemoveLocPB.Disable();
373 maTrustedAuthors = mpDlg->maSecOptions.GetTrustedAuthors();
374 mbAuthorsReadonly = mpDlg->maSecOptions.IsReadOnly( SvtSecurityOptions::E_MACRO_TRUSTEDAUTHORS );
375 maTrustCertROFI.Show( mbAuthorsReadonly );
376 mbAuthorsReadonly ? maTrustCertLB.DisableTable() : maTrustCertLB.EnableTable();
377 // unused button
378 // maAddCertPB.Enable( !mbAuthorsReadonly );
380 FillCertLB();
382 cssu::Sequence< rtl::OUString > aSecureURLs = mpDlg->maSecOptions.GetSecureURLs();
383 mbURLsReadonly = mpDlg->maSecOptions.IsReadOnly( SvtSecurityOptions::E_SECUREURLS );
384 maTrustFileROFI.Show( mbURLsReadonly );
385 maTrustFileLocLB.Enable( !mbURLsReadonly );
386 maAddLocPB .Enable( !mbURLsReadonly );
388 sal_Int32 nEntryCnt = aSecureURLs.getLength();
389 for( sal_Int32 i = 0 ; i < nEntryCnt ; ++i )
391 ::rtl::OUString aSystemFileURL( aSecureURLs[ i ] );
392 osl::FileBase::getSystemPathFromFileURL( aSystemFileURL, aSystemFileURL );
393 maTrustFileLocLB.InsertEntry( aSystemFileURL );
397 void MacroSecurityTrustedSourcesTP::ActivatePage()
399 mpDlg->EnableReset( false );
400 FillCertLB();
403 void MacroSecurityTrustedSourcesTP::ClosePage( void )
405 USHORT nEntryCnt = maTrustFileLocLB.GetEntryCount();
406 if( nEntryCnt )
408 cssu::Sequence< rtl::OUString > aSecureURLs( nEntryCnt );
409 for( USHORT i = 0 ; i < nEntryCnt ; ++i )
411 ::rtl::OUString aURL( maTrustFileLocLB.GetEntry( i ) );
412 osl::FileBase::getFileURLFromSystemPath( aURL, aURL );
413 aSecureURLs[ i ] = aURL;
416 mpDlg->maSecOptions.SetSecureURLs( aSecureURLs );
418 // --> PB 2004-09-21 #i33584#
419 // don't forget to remove the old saved SecureURLs
420 else
421 mpDlg->maSecOptions.SetSecureURLs( cssu::Sequence< rtl::OUString >() );
422 // <--
424 mpDlg->maSecOptions.SetTrustedAuthors( maTrustedAuthors );
426 /*-- 26.02.2004 13:31:04---------------------------------------------------
428 -----------------------------------------------------------------------*/
429 ReadOnlyImage::ReadOnlyImage(Window* pParent, const ResId rResId) :
430 FixedImage(pParent, rResId)
432 sal_Bool bHighContrast = pParent->GetDisplayBackground().GetColor().IsDark();
433 SetImage( Image(XMLSEC_RES( bHighContrast ? RID_XMLSECTP_LOCK_HC : RID_XMLSECTP_LOCK )));
436 /*-- 26.02.2004 13:31:04---------------------------------------------------
438 -----------------------------------------------------------------------*/
439 ReadOnlyImage::~ReadOnlyImage()
442 /*-- 26.02.2004 13:31:04---------------------------------------------------
444 -----------------------------------------------------------------------*/
445 void ReadOnlyImage::RequestHelp( const HelpEvent& rHEvt )
447 if( Help::IsBalloonHelpEnabled() || Help::IsQuickHelpEnabled() )
449 Rectangle aLogicPix( LogicToPixel( Rectangle( Point(), GetOutputSize() ) ) );
450 Rectangle aScreenRect( OutputToScreenPixel( aLogicPix.TopLeft() ),
451 OutputToScreenPixel( aLogicPix.BottomRight() ) );
453 String aStr(ReadOnlyImage::GetHelpTip());
454 if ( Help::IsBalloonHelpEnabled() )
455 Help::ShowBalloon( this, rHEvt.GetMousePosPixel(), aScreenRect,
456 aStr );
457 else if ( Help::IsQuickHelpEnabled() )
458 Help::ShowQuickHelp( this, aScreenRect, aStr );
460 else
461 Window::RequestHelp( rHEvt );
464 /*-- 26.02.2004 14:20:21---------------------------------------------------
466 -----------------------------------------------------------------------*/
467 const String& ReadOnlyImage::GetHelpTip()
469 static String aStr(XMLSEC_RES( RID_XMLSECTP_READONLY_CONFIG_TIP));
470 return aStr;