update dev300-m58
[ooovba.git] / uui / source / secmacrowarnings.cxx
blob8545b5de5d255b3d92fbdc4d39d70907d3c97e7f
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: secmacrowarnings.cxx,v $
10 * $Revision: 1.3 $
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 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
32 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 #include <com/sun/star/security/XDocumentDigitalSignatures.hpp>
34 #include <comphelper/sequence.hxx>
35 #include <comphelper/processfactory.hxx>
37 #include <vcl/msgbox.hxx>
38 #include <com/sun/star/security/NoPasswordException.hpp>
40 using namespace ::com::sun::star::security;
42 #include "ids.hrc"
43 #include "secmacrowarnings.hxx"
44 #include "secmacrowarnings.hrc"
46 #ifdef _MSC_VER
47 #pragma warning (disable : 4355) // 4355: this used in initializer-list
48 #endif
50 using namespace ::com::sun::star;
51 using namespace ::com::sun::star;
54 // HACK!!! copied from xmlsecurity/source/dialog/resourcemanager.cxx
56 namespace
58 String GetContentPart( const String& _rRawString, const String& _rPartId )
60 String s;
62 xub_StrLen nContStart = _rRawString.Search( _rPartId );
63 if( nContStart != STRING_NOTFOUND )
65 nContStart = nContStart + _rPartId.Len();
66 ++nContStart; // now it's start of content, directly after Id
68 xub_StrLen nContEnd = _rRawString.Search( sal_Unicode( ',' ), nContStart );
70 s = String( _rRawString, nContStart, nContEnd - nContStart );
73 return s;
78 MacroWarning::MacroWarning( Window* _pParent, bool _bWithSignatures, ResMgr& rResMgr )
79 :ModalDialog ( _pParent, ResId( RID_XMLSECDLG_MACROWARN, rResMgr ) )
80 ,mpInfos ( NULL )
81 ,maSymbolImg ( this, ResId( IMG_SYMBOL, rResMgr ) )
82 ,maDocNameFI ( this, ResId( FI_DOCNAME, rResMgr ) )
83 ,maDescr1aFI ( this, ResId( FI_DESCR1A, rResMgr ) )
84 ,maDescr1bFI ( this, ResId( FI_DESCR1B, rResMgr ) )
85 ,maSignsFI ( this, ResId( FI_SIGNS, rResMgr ) )
86 ,maViewSignsBtn ( this, ResId( PB_VIEWSIGNS, rResMgr ) )
87 ,maDescr2FI ( this, ResId( FI_DESCR2, rResMgr ) )
88 ,maAlwaysTrustCB ( this, ResId( CB_ALWAYSTRUST, rResMgr ) )
89 ,maBottomSepFL ( this, ResId( FL_BOTTOM_SEP, rResMgr ) )
90 ,maEnableBtn ( this, ResId( PB_ENABLE, rResMgr ) )
91 ,maDisableBtn ( this, ResId( PB_DISABLE, rResMgr ) )
92 ,maHelpBtn ( this, ResId( BTN_HELP, rResMgr ) )
93 ,mbSignedMode ( true )
94 ,mbShowSignatures ( _bWithSignatures )
95 ,mnActSecLevel ( 0 )
97 FreeResource();
99 InitControls();
101 maDisableBtn.SetClickHdl( LINK( this, MacroWarning, DisableBtnHdl ) );
102 maEnableBtn.SetClickHdl( LINK( this, MacroWarning, EnableBtnHdl ) );
103 maDisableBtn.GrabFocus(); // Default button, but focus is on view button
106 MacroWarning::~MacroWarning()
110 short MacroWarning::Execute()
112 FitControls();
113 return ModalDialog::Execute();
116 void MacroWarning::SetDocumentURL( const String& rDocURL )
118 maDocNameFI.SetText( rDocURL );
121 IMPL_LINK( MacroWarning, ViewSignsBtnHdl, void*, EMPTYARG )
123 DBG_ASSERT( mxCert.is(), "*MacroWarning::ViewSignsBtnHdl(): no certificate set!" );
125 uno::Reference< security::XDocumentDigitalSignatures > xD(
126 comphelper::getProcessServiceFactory()->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.security.DocumentDigitalSignatures" ) ) ), uno::UNO_QUERY );
127 if( xD.is() )
129 if( mxCert.is() )
130 xD->showCertificate( mxCert );
131 else if( mxStore.is() )
132 xD->showScriptingContentSignatures( mxStore, uno::Reference< io::XInputStream >() );
135 return 0;
138 IMPL_LINK( MacroWarning, EnableBtnHdl, void*, EMPTYARG )
140 if( mbSignedMode && maAlwaysTrustCB.IsChecked() )
141 { // insert path into trusted path list
142 uno::Reference< security::XDocumentDigitalSignatures > xD(
143 comphelper::getProcessServiceFactory()->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.security.DocumentDigitalSignatures" ) ) ), uno::UNO_QUERY );
144 if( xD.is() )
146 if( mxCert.is() )
147 xD->addAuthorToTrustedSources( mxCert );
148 else if( mxStore.is() )
150 DBG_ASSERT( mpInfos, "-MacroWarning::EnableBtnHdl(): no infos, search in nirvana..." );
152 sal_Int32 nCnt = mpInfos->getLength();
153 for( sal_Int32 i = 0 ; i < nCnt ; ++i )
154 xD->addAuthorToTrustedSources( (*mpInfos)[ i ].Signer );
159 EndDialog( RET_OK );
160 return 0;
163 IMPL_LINK( MacroWarning, DisableBtnHdl, void*, EMPTYARG )
165 EndDialog( RET_CANCEL );
166 return 0;
169 IMPL_LINK( MacroWarning, AlwaysTrustCheckHdl, void*, EMPTYARG )
171 bool bEnable = ( mnActSecLevel < 2 || maAlwaysTrustCB.IsChecked() );
172 maEnableBtn.Enable( bEnable );
173 maDisableBtn.Enable( !maAlwaysTrustCB.IsChecked() );
175 return 0;
178 void MacroWarning::InitControls()
180 // set warning image
181 Image aImg( WarningBox::GetStandardImage() );
182 maSymbolImg.SetImage( aImg );
183 maSymbolImg.SetSizePixel( aImg.GetSizePixel() );
184 // set bold font and path ellipsis for docname fixedtext
185 Font aTmpFont = maDocNameFI.GetControlFont();
186 aTmpFont.SetWeight( WEIGHT_BOLD );
187 maDocNameFI.SetControlFont( aTmpFont );
188 WinBits nStyle = maDocNameFI.GetStyle();
189 nStyle |= WB_PATHELLIPSIS;
190 maDocNameFI.SetStyle( nStyle );
191 // show signature controls?
192 if( mbShowSignatures )
194 maDescr1bFI.Hide();
195 maViewSignsBtn.SetClickHdl( LINK( this, MacroWarning, ViewSignsBtnHdl ) );
196 maViewSignsBtn.Disable(); // default
197 maAlwaysTrustCB.SetClickHdl( LINK( this, MacroWarning, AlwaysTrustCheckHdl ) );
199 mnActSecLevel = SvtSecurityOptions().GetMacroSecurityLevel();
200 if ( mnActSecLevel >= 2 )
201 maEnableBtn.Disable();
203 else
205 maDescr1aFI.Hide();
206 maSignsFI.Hide();
207 maViewSignsBtn.Hide();
208 maAlwaysTrustCB.Hide();
210 // move hint up to position of signer list
211 maDescr2FI.SetPosPixel( maSignsFI.GetPosPixel() );
213 // without signature controls could be smaller
214 if ( !mbShowSignatures )
216 Point aPos = maDescr2FI.GetPosPixel();
217 aPos.Y() += maDescr2FI.GetSizePixel().Height();
218 aPos.Y() += LogicToPixel( Size( 3, 3 ) ).Height();
219 long nDelta = maBottomSepFL.GetPosPixel().Y() - aPos.Y();
220 Window* pWins[] =
222 &maBottomSepFL, &maEnableBtn, &maDisableBtn, &maHelpBtn
224 Window** pCurrent = pWins;
225 for ( sal_uInt32 i = 0; i < sizeof( pWins ) / sizeof( pWins[ 0 ] ); ++i, ++pCurrent )
227 Point aNewPos = (*pCurrent)->GetPosPixel();
228 aNewPos.Y() -= nDelta;
229 (*pCurrent)->SetPosPixel( aNewPos );
232 Size aDlgSz = GetSizePixel();
233 aDlgSz.Height() -= nDelta;
234 SetSizePixel( aDlgSz );
237 // check if some buttontexts are to wide
238 String sText = maViewSignsBtn.GetText();
239 long nTxtW = maViewSignsBtn.GetTextWidth( sText );
240 const long nOffset = 12;
241 if ( sText.Search( '~' ) == STRING_NOTFOUND )
242 nTxtW += nOffset;
243 long nBtnW = maViewSignsBtn.GetSizePixel().Width();
244 if ( nTxtW >= nBtnW )
246 // broaden the button
247 long nDelta = Max( nTxtW - nBtnW, nOffset/3 );
248 Size aNewSize = maViewSignsBtn.GetSizePixel();
249 aNewSize.Width() += nDelta;
250 maViewSignsBtn.SetSizePixel( aNewSize );
251 // and give it a new position
252 Point aNewPos = maViewSignsBtn.GetPosPixel();
253 aNewPos.X() -= nDelta;
254 maViewSignsBtn.SetPosPixel( aNewPos );
255 // the the left fixedtext must be smaller
256 aNewSize = maSignsFI.GetSizePixel();
257 aNewSize.Width() -= nDelta;
258 maSignsFI.SetSizePixel( aNewSize );
260 // if the button text (we compare with the longest of both) is too wide, then broaden the buttons
261 String sText1 = maEnableBtn.GetText();
262 long nTxtW1 = maEnableBtn.GetTextWidth( sText1 );
263 if ( sText1.Search( '~' ) == STRING_NOTFOUND )
264 nTxtW1 += nOffset;
265 String sText2 = maDisableBtn.GetText();
266 long nTxtW2 = maDisableBtn.GetTextWidth( sText2 );
267 if ( sText2.Search( '~' ) == STRING_NOTFOUND )
268 nTxtW2 += nOffset;
269 nTxtW = Max( nTxtW1, nTxtW2 );
270 nBtnW = maEnableBtn.GetSizePixel().Width();
271 if ( nTxtW > nBtnW )
273 // broaden both buttons
274 long nDelta = nTxtW - nBtnW;
275 Size aNewSize = maEnableBtn.GetSizePixel();
276 aNewSize.Width() += nDelta;
277 maEnableBtn.SetSizePixel( aNewSize );
278 maDisableBtn.SetSizePixel( aNewSize );
279 // and give them a new position
280 Point aNewPos = maEnableBtn.GetPosPixel();
281 aNewPos.X() -= (2*nDelta);
282 maEnableBtn.SetPosPixel( aNewPos );
283 aNewPos = maDisableBtn.GetPosPixel();
284 aNewPos.X() -= nDelta;
285 maDisableBtn.SetPosPixel( aNewPos );
289 void MacroWarning::FitControls()
291 Size a3Size = LogicToPixel( Size( 3, 3 ), MAP_APPFONT );
292 Size aNewSize, aMinSize;
293 long nTxtH = 0;
294 long nCtrlH = 0;
295 long nDelta = 0;
297 if ( mbShowSignatures )
299 aMinSize = maSignsFI.CalcMinimumSize( maSignsFI.GetSizePixel().Width() );
300 nTxtH = Max( aMinSize.Height(), maViewSignsBtn.GetSizePixel().Height() );
301 nTxtH += a3Size.Height() / 2;
302 nCtrlH = maSignsFI.GetSizePixel().Height();
303 nDelta = Max( nCtrlH - nTxtH, static_cast< long >( -100 ) ); // not too large
304 aNewSize = maSignsFI.GetSizePixel();
305 aNewSize.Height() -= nDelta;
306 maSignsFI.SetSizePixel( aNewSize );
309 aMinSize = maDescr2FI.CalcMinimumSize( maDescr2FI.GetSizePixel().Width() );
310 nTxtH = aMinSize.Height();
311 nCtrlH = maDescr2FI.GetSizePixel().Height();
312 long nDelta2 = ( nCtrlH - nTxtH );
313 aNewSize = maDescr2FI.GetSizePixel();
314 aNewSize.Height() -= nDelta2;
315 maDescr2FI.SetSizePixel( aNewSize );
317 // new position for the succeeding windows
318 Window* pWins[] =
320 &maDescr2FI, &maAlwaysTrustCB, &maBottomSepFL, &maEnableBtn, &maDisableBtn, &maHelpBtn
322 Window** pCurrent = pWins;
323 for ( sal_uInt32 i = 0; i < sizeof( pWins ) / sizeof( pWins[ 0 ] ); ++i, ++pCurrent )
325 Point aNewPos = (*pCurrent)->GetPosPixel();
326 aNewPos.Y() -= nDelta;
327 (*pCurrent)->SetPosPixel( aNewPos );
329 if ( *pCurrent == &maDescr2FI )
330 nDelta += nDelta2;
333 // new size of the dialog
334 aNewSize = GetSizePixel();
335 aNewSize.Height() -= nDelta;
336 SetSizePixel( aNewSize );
339 void MacroWarning::SetStorage( const cssu::Reference < css::embed::XStorage >& rxStore,
340 const cssu::Sequence< security::DocumentSignatureInformation >& _rInfos )
342 mxStore = rxStore;
343 sal_Int32 nCnt = _rInfos.getLength();
344 if( mxStore.is() && nCnt > 0 )
346 mpInfos = &_rInfos;
347 String aCN_Id( String::CreateFromAscii( "CN" ) );
348 String s;
349 s = GetContentPart( _rInfos[ 0 ].Signer->getSubjectName(), aCN_Id );
351 for( sal_Int32 i = 1 ; i < nCnt ; ++i )
353 s.AppendAscii( "\n" );
354 s += GetContentPart( _rInfos[ i ].Signer->getSubjectName(), aCN_Id );
357 maSignsFI.SetText( s );
358 maViewSignsBtn.Enable();
362 void MacroWarning::SetCertificate( const cssu::Reference< css::security::XCertificate >& _rxCert )
364 mxCert = _rxCert;
365 if( mxCert.is() )
367 String aCN_Id( String::CreateFromAscii( "CN" ) );
368 String s;
369 s = GetContentPart( mxCert->getSubjectName(), aCN_Id );
370 maSignsFI.SetText( s );
371 maViewSignsBtn.Enable();