merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / source / dialogs / macrosecurity.cxx
blob0bee4e2011e7cd226f689bc5c7b9229d15cfd832
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
31 #include <xmlsecurity/macrosecurity.hxx>
32 #include <xmlsecurity/certificatechooser.hxx>
33 #include <xmlsecurity/certificateviewer.hxx>
34 #include <xmlsecurity/biginteger.hxx>
36 #include <osl/file.hxx>
37 #include <vcl/help.hxx>
40 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
41 #include <com/sun/star/security/SerialNumberAdapter.hpp>
42 #include <comphelper/sequence.hxx>
43 #include <sfx2/filedlghelper.hxx>
44 #include <svl/pickerhelper.hxx>
45 #include <comphelper/processfactory.hxx>
46 #include <com/sun/star/uno/Exception.hpp>
47 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
48 #include <com/sun/star/ui/dialogs/XFolderPicker.hpp>
49 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
50 #include <tools/urlobj.hxx>
52 #include <vcl/msgbox.hxx>
54 #include "dialogs.hrc"
55 #include "resourcemanager.hxx"
57 /* HACK: disable some warnings for MS-C */
58 #ifdef _MSC_VER
59 #pragma warning (disable : 4355) // 4355: this used in initializer-list
60 #endif
62 using namespace ::com::sun::star;
65 IMPL_LINK( MacroSecurity, OkBtnHdl, void*, EMPTYARG )
67 mpLevelTP->ClosePage();
68 mpTrustSrcTP->ClosePage();
70 EndDialog( RET_OK );
72 return 0;
75 MacroSecurity::MacroSecurity( Window* _pParent, const cssu::Reference< cssu::XComponentContext> &_rxCtx, const cssu::Reference< dcss::xml::crypto::XSecurityEnvironment >& _rxSecurityEnvironment )
76 :TabDialog ( _pParent, XMLSEC_RES( RID_XMLSECTP_MACROSEC ) )
77 ,maTabCtrl ( this, XMLSEC_RES( 1 ) )
78 ,maOkBtn ( this, XMLSEC_RES( BTN_OK ) )
79 ,maCancelBtn ( this, XMLSEC_RES( BTN_CANCEL ) )
80 ,maHelpBtn ( this, XMLSEC_RES( BTN_HELP ) )
81 ,maResetBtn ( this, XMLSEC_RES( BTN_RESET ) )
83 FreeResource();
85 mxCtx = _rxCtx;
86 mxSecurityEnvironment = _rxSecurityEnvironment;
88 mpLevelTP = new MacroSecurityLevelTP( &maTabCtrl, this );
89 mpTrustSrcTP = new MacroSecurityTrustedSourcesTP( &maTabCtrl, this );
91 maTabCtrl.SetTabPage( RID_XMLSECTP_SECLEVEL, mpLevelTP );
92 maTabCtrl.SetTabPage( RID_XMLSECTP_TRUSTSOURCES, mpTrustSrcTP );
93 maTabCtrl.SetCurPageId( RID_XMLSECTP_SECLEVEL );
95 maOkBtn.SetClickHdl( LINK( this, MacroSecurity, OkBtnHdl ) );
98 MacroSecurity::~MacroSecurity()
100 delete maTabCtrl.GetTabPage( RID_XMLSECTP_TRUSTSOURCES );
101 delete maTabCtrl.GetTabPage( RID_XMLSECTP_SECLEVEL );
105 MacroSecurityTP::MacroSecurityTP( Window* _pParent, const ResId& _rResId, MacroSecurity* _pDlg )
106 :TabPage ( _pParent, _rResId )
107 ,mpDlg ( _pDlg )
111 MacroSecurityLevelTP::MacroSecurityLevelTP( Window* _pParent, MacroSecurity* _pDlg )
112 :MacroSecurityTP ( _pParent, XMLSEC_RES( RID_XMLSECTP_SECLEVEL ), _pDlg )
113 ,maSecLevelFL ( this, XMLSEC_RES( FL_SECLEVEL ) )
114 ,maSecReadonlyFI ( this, XMLSEC_RES( FI_SEC_READONLY ))
115 ,maVeryHighRB ( this, XMLSEC_RES( RB_VERYHIGH ) )
116 ,maHighRB ( this, XMLSEC_RES( RB_HIGH ) )
117 ,maMediumRB ( this, XMLSEC_RES( RB_MEDIUM ) )
118 ,maLowRB ( this, XMLSEC_RES( RB_LOW ) )
120 FreeResource();
122 maLowRB.SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
123 maMediumRB.SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
124 maHighRB.SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
125 maVeryHighRB.SetClickHdl( LINK( this, MacroSecurityLevelTP, RadioButtonHdl ) );
127 mnCurLevel = (USHORT) mpDlg->maSecOptions.GetMacroSecurityLevel();
128 sal_Bool bReadonly = mpDlg->maSecOptions.IsReadOnly( SvtSecurityOptions::E_MACRO_SECLEVEL );
130 RadioButton* pCheck = 0;
131 switch( mnCurLevel )
133 case 3: pCheck = &maVeryHighRB; break;
134 case 2: pCheck = &maHighRB; break;
135 case 1: pCheck = &maMediumRB; break;
136 case 0: pCheck = &maLowRB; break;
138 if(pCheck)
139 pCheck->Check();
140 else
142 DBG_ERROR("illegal macro security level");
144 maSecReadonlyFI.Show(bReadonly);
145 if(bReadonly)
147 //move to the selected button
148 if( pCheck && pCheck != &maVeryHighRB)
150 long nDiff = pCheck->GetPosPixel().Y() - maVeryHighRB.GetPosPixel().Y();
151 Point aPos(maSecReadonlyFI.GetPosPixel());
152 aPos.Y() += nDiff;
153 maSecReadonlyFI.SetPosPixel(aPos);
155 maVeryHighRB.Enable(sal_False);
156 maHighRB.Enable(sal_False);
157 maMediumRB.Enable(sal_False);
158 maLowRB.Enable(sal_False);
163 IMPL_LINK( MacroSecurityLevelTP, RadioButtonHdl, RadioButton*, EMPTYARG )
165 USHORT nNewLevel = 0;
166 if( maVeryHighRB.IsChecked() )
167 nNewLevel = 3;
168 else if( maHighRB.IsChecked() )
169 nNewLevel = 2;
170 else if( maMediumRB.IsChecked() )
171 nNewLevel = 1;
173 if ( nNewLevel != mnCurLevel )
175 mnCurLevel = nNewLevel;
176 mpDlg->EnableReset();
179 return 0;
182 void MacroSecurityLevelTP::ClosePage( void )
184 mpDlg->maSecOptions.SetMacroSecurityLevel( mnCurLevel );
187 void MacroSecurityTrustedSourcesTP::ImplCheckButtons()
189 bool bCertSelected = maTrustCertLB.FirstSelected() != NULL;
190 maViewCertPB.Enable( bCertSelected );
191 maRemoveCertPB.Enable( bCertSelected && !mbAuthorsReadonly);
193 bool bLocationSelected = maTrustFileLocLB.GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND;
194 maRemoveLocPB.Enable( bLocationSelected && !mbURLsReadonly);
198 IMPL_LINK( MacroSecurityTrustedSourcesTP, ViewCertPBHdl, void*, EMPTYARG )
200 if( maTrustCertLB.FirstSelected() )
202 USHORT nSelected = USHORT( sal_uIntPtr( maTrustCertLB.FirstSelected()->GetUserData() ) );
204 uno::Reference< dcss::security::XSerialNumberAdapter > xSerialNumberAdapter =
205 ::com::sun::star::security::SerialNumberAdapter::create(mpDlg->mxCtx);
207 uno::Reference< dcss::security::XCertificate > xCert = mpDlg->mxSecurityEnvironment->getCertificate( maTrustedAuthors[nSelected][0], xSerialNumberAdapter->toSequence( maTrustedAuthors[nSelected][1] ) );
209 // If we don't get it, create it from signature data:
210 if ( !xCert.is() )
211 xCert = mpDlg->mxSecurityEnvironment->createCertificateFromAscii( maTrustedAuthors[nSelected][2] ) ;
213 DBG_ASSERT( xCert.is(), "*MacroSecurityTrustedSourcesTP::ViewCertPBHdl(): Certificate not found and can't be created!" );
215 if ( xCert.is() )
217 CertificateViewer aViewer( this, mpDlg->mxSecurityEnvironment, xCert, FALSE );
218 aViewer.Execute();
221 return 0;
224 IMPL_LINK( MacroSecurityTrustedSourcesTP, RemoveCertPBHdl, void*, EMPTYARG )
226 if( maTrustCertLB.FirstSelected() )
228 USHORT nAuthor = USHORT( sal_uIntPtr( maTrustCertLB.FirstSelected()->GetUserData() ) );
229 ::comphelper::removeElementAt( maTrustedAuthors, nAuthor );
231 FillCertLB();
232 ImplCheckButtons();
235 return 0;
238 IMPL_LINK( MacroSecurityTrustedSourcesTP, AddLocPBHdl, void*, EMPTYARG )
242 rtl::OUString aService( RTL_CONSTASCII_USTRINGPARAM( FOLDER_PICKER_SERVICE_NAME ) );
243 uno::Reference < lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
244 uno::Reference < ui::dialogs::XFolderPicker > xFolderPicker( xFactory->createInstance( aService ), uno::UNO_QUERY );
246 short nRet = xFolderPicker->execute();
248 if( ui::dialogs::ExecutableDialogResults::OK != nRet )
249 return 0;
251 rtl::OUString aPathStr = xFolderPicker->getDirectory();
252 INetURLObject aNewObj( aPathStr );
253 aNewObj.removeFinalSlash();
255 // then the new path also an URL else system path
256 ::rtl::OUString aSystemFileURL = ( aNewObj.GetProtocol() != INET_PROT_NOT_VALID ) ?
257 aPathStr : aNewObj.getFSysPath( INetURLObject::FSYS_DETECT );
259 String aNewPathStr(aSystemFileURL);
261 if ( osl::FileBase::getSystemPathFromFileURL( aSystemFileURL, aSystemFileURL ) == osl::FileBase::E_None )
262 aNewPathStr = aSystemFileURL;
264 if( maTrustFileLocLB.GetEntryPos( aNewPathStr ) == LISTBOX_ENTRY_NOTFOUND )
266 maTrustFileLocLB.InsertEntry( aNewPathStr );
269 ImplCheckButtons();
271 catch( uno::Exception& )
273 DBG_ERRORFILE( "MacroSecurityTrustedSourcesTP::AddLocPBHdl(): exception from folder picker" );
276 return 0;
279 IMPL_LINK( MacroSecurityTrustedSourcesTP, RemoveLocPBHdl, void*, EMPTYARG )
281 USHORT nSel = maTrustFileLocLB.GetSelectEntryPos();
282 if( nSel != LISTBOX_ENTRY_NOTFOUND )
284 maTrustFileLocLB.RemoveEntry( nSel );
285 // --> PB 2004-09-21 #i33584#
286 // after remove an entry, select another one if exists
287 USHORT nNewCount = maTrustFileLocLB.GetEntryCount();
288 if ( nNewCount > 0 )
290 if ( nSel >= nNewCount )
291 nSel = nNewCount - 1;
292 maTrustFileLocLB.SelectEntryPos( nSel );
294 // <--
295 ImplCheckButtons();
298 return 0;
301 IMPL_LINK( MacroSecurityTrustedSourcesTP, TrustCertLBSelectHdl, void*, EMPTYARG )
303 ImplCheckButtons();
304 return 0;
307 IMPL_LINK( MacroSecurityTrustedSourcesTP, TrustFileLocLBSelectHdl, void*, EMPTYARG )
309 ImplCheckButtons();
310 return 0;
313 void MacroSecurityTrustedSourcesTP::FillCertLB( void )
315 maTrustCertLB.Clear();
317 sal_uInt32 nEntries = maTrustedAuthors.getLength();
319 if ( nEntries && mpDlg->mxSecurityEnvironment.is() )
321 for( sal_uInt32 nEntry = 0 ; nEntry < nEntries ; ++nEntry )
323 cssu::Sequence< ::rtl::OUString >& rEntry = maTrustedAuthors[ nEntry ];
324 uno::Reference< css::security::XCertificate > xCert;
326 // create from RawData
327 xCert = mpDlg->mxSecurityEnvironment->createCertificateFromAscii( rEntry[ 2 ] );
329 SvLBoxEntry* pLBEntry = maTrustCertLB.InsertEntry( XmlSec::GetContentPart( xCert->getSubjectName() ) );
330 maTrustCertLB.SetEntryText( XmlSec::GetContentPart( xCert->getIssuerName() ), pLBEntry, 1 );
331 maTrustCertLB.SetEntryText( XmlSec::GetDateTimeString( xCert->getNotValidAfter() ), pLBEntry, 2 );
332 pLBEntry->SetUserData( ( void* ) sal_Int32( nEntry ) ); // missuse user data as index
337 MacroSecurityTrustedSourcesTP::MacroSecurityTrustedSourcesTP( Window* _pParent, MacroSecurity* _pDlg )
338 :MacroSecurityTP ( _pParent, XMLSEC_RES( RID_XMLSECTP_TRUSTSOURCES ), _pDlg )
339 ,maTrustCertFL ( this, XMLSEC_RES( FL_TRUSTCERT ) )
340 ,maTrustCertROFI ( this, XMLSEC_RES( FI_TRUSTCERT_RO ) )
341 ,maTrustCertLB ( this, XMLSEC_RES( LB_TRUSTCERT ) )
342 ,maAddCertPB ( this, XMLSEC_RES( PB_ADD_TRUSTCERT ) )
343 ,maViewCertPB ( this, XMLSEC_RES( PB_VIEW_TRUSTCERT ) )
344 ,maRemoveCertPB ( this, XMLSEC_RES( PB_REMOVE_TRUSTCERT ) )
345 ,maTrustFileLocFL ( this, XMLSEC_RES( FL_TRUSTFILELOC ) )
346 ,maTrustFileROFI ( this, XMLSEC_RES( FI_TRUSTFILE_RO ) )
347 ,maTrustFileLocFI ( this, XMLSEC_RES( FI_TRUSTFILELOC ) )
348 ,maTrustFileLocLB ( this, XMLSEC_RES( LB_TRUSTFILELOC ) )
349 ,maAddLocPB ( this, XMLSEC_RES( FL_ADD_TRUSTFILELOC ) )
350 ,maRemoveLocPB ( this, XMLSEC_RES( FL_REMOVE_TRUSTFILELOC ) )
352 static long nTabs[] = { 3, 0, 35*CS_LB_WIDTH/100, 70*CS_LB_WIDTH/100 };
353 maTrustCertLB.SetTabs( &nTabs[ 0 ] );
354 maTrustCertLB.InsertHeaderEntry( String( XMLSEC_RES( STR_HEADERBAR ) ) );
356 FreeResource();
358 maTrustCertLB.SetSelectHdl( LINK( this, MacroSecurityTrustedSourcesTP, TrustCertLBSelectHdl ) );
359 maAddCertPB.Hide(); // not used in the moment...
360 maViewCertPB.SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, ViewCertPBHdl ) );
361 maViewCertPB.Disable();
362 maRemoveCertPB.SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, RemoveCertPBHdl ) );
363 maRemoveCertPB.Disable();
365 maTrustFileLocLB.SetSelectHdl( LINK( this, MacroSecurityTrustedSourcesTP, TrustFileLocLBSelectHdl ) );
366 maAddLocPB.SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, AddLocPBHdl ) );
367 maRemoveLocPB.SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP, RemoveLocPBHdl ) );
368 maRemoveLocPB.Disable();
370 maTrustedAuthors = mpDlg->maSecOptions.GetTrustedAuthors();
371 mbAuthorsReadonly = mpDlg->maSecOptions.IsReadOnly( SvtSecurityOptions::E_MACRO_TRUSTEDAUTHORS );
372 maTrustCertROFI.Show( mbAuthorsReadonly );
373 mbAuthorsReadonly ? maTrustCertLB.DisableTable() : maTrustCertLB.EnableTable();
374 // unused button
375 // maAddCertPB.Enable( !mbAuthorsReadonly );
377 FillCertLB();
379 cssu::Sequence< rtl::OUString > aSecureURLs = mpDlg->maSecOptions.GetSecureURLs();
380 mbURLsReadonly = mpDlg->maSecOptions.IsReadOnly( SvtSecurityOptions::E_SECUREURLS );
381 maTrustFileROFI.Show( mbURLsReadonly );
382 maTrustFileLocLB.Enable( !mbURLsReadonly );
383 maAddLocPB .Enable( !mbURLsReadonly );
385 sal_Int32 nEntryCnt = aSecureURLs.getLength();
386 for( sal_Int32 i = 0 ; i < nEntryCnt ; ++i )
388 ::rtl::OUString aSystemFileURL( aSecureURLs[ i ] );
389 osl::FileBase::getSystemPathFromFileURL( aSystemFileURL, aSystemFileURL );
390 maTrustFileLocLB.InsertEntry( aSystemFileURL );
394 void MacroSecurityTrustedSourcesTP::ActivatePage()
396 mpDlg->EnableReset( false );
397 FillCertLB();
400 void MacroSecurityTrustedSourcesTP::ClosePage( void )
402 USHORT nEntryCnt = maTrustFileLocLB.GetEntryCount();
403 if( nEntryCnt )
405 cssu::Sequence< rtl::OUString > aSecureURLs( nEntryCnt );
406 for( USHORT i = 0 ; i < nEntryCnt ; ++i )
408 ::rtl::OUString aURL( maTrustFileLocLB.GetEntry( i ) );
409 osl::FileBase::getFileURLFromSystemPath( aURL, aURL );
410 aSecureURLs[ i ] = aURL;
413 mpDlg->maSecOptions.SetSecureURLs( aSecureURLs );
415 // --> PB 2004-09-21 #i33584#
416 // don't forget to remove the old saved SecureURLs
417 else
418 mpDlg->maSecOptions.SetSecureURLs( cssu::Sequence< rtl::OUString >() );
419 // <--
421 mpDlg->maSecOptions.SetTrustedAuthors( maTrustedAuthors );
423 /*-- 26.02.2004 13:31:04---------------------------------------------------
425 -----------------------------------------------------------------------*/
426 ReadOnlyImage::ReadOnlyImage(Window* pParent, const ResId rResId) :
427 FixedImage(pParent, rResId)
429 sal_Bool bHighContrast = pParent->GetSettings().GetStyleSettings().GetHighContrastMode();
430 SetImage( Image(XMLSEC_RES( bHighContrast ? RID_XMLSECTP_LOCK_HC : RID_XMLSECTP_LOCK )));
433 /*-- 26.02.2004 13:31:04---------------------------------------------------
435 -----------------------------------------------------------------------*/
436 ReadOnlyImage::~ReadOnlyImage()
439 /*-- 26.02.2004 13:31:04---------------------------------------------------
441 -----------------------------------------------------------------------*/
442 void ReadOnlyImage::RequestHelp( const HelpEvent& rHEvt )
444 if( Help::IsBalloonHelpEnabled() || Help::IsQuickHelpEnabled() )
446 Rectangle aLogicPix( LogicToPixel( Rectangle( Point(), GetOutputSize() ) ) );
447 Rectangle aScreenRect( OutputToScreenPixel( aLogicPix.TopLeft() ),
448 OutputToScreenPixel( aLogicPix.BottomRight() ) );
450 String aStr(ReadOnlyImage::GetHelpTip());
451 if ( Help::IsBalloonHelpEnabled() )
452 Help::ShowBalloon( this, rHEvt.GetMousePosPixel(), aScreenRect,
453 aStr );
454 else if ( Help::IsQuickHelpEnabled() )
455 Help::ShowQuickHelp( this, aScreenRect, aStr );
457 else
458 Window::RequestHelp( rHEvt );
461 /*-- 26.02.2004 14:20:21---------------------------------------------------
463 -----------------------------------------------------------------------*/
464 const String& ReadOnlyImage::GetHelpTip()
466 static String aStr(XMLSEC_RES( RID_XMLSECTP_READONLY_CONFIG_TIP));
467 return aStr;