merge the formfield patch from ooo-build
[ooovba.git] / uui / source / secmacrowarnings.cxx
blobdb272a88f96aa96bf0476ecfc06f97bdce4c8e39
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/documentconstants.hxx"
36 #include <comphelper/processfactory.hxx>
38 #include <vcl/msgbox.hxx>
39 #include <com/sun/star/security/NoPasswordException.hpp>
41 using namespace ::com::sun::star::security;
43 #include "ids.hrc"
44 #include "secmacrowarnings.hxx"
45 #include "secmacrowarnings.hrc"
47 #ifdef _MSC_VER
48 #pragma warning (disable : 4355) // 4355: this used in initializer-list
49 #endif
51 using namespace ::com::sun::star;
52 using namespace ::com::sun::star;
55 // HACK!!! copied from xmlsecurity/source/dialog/resourcemanager.cxx
57 namespace
59 String GetContentPart( const String& _rRawString, const String& _rPartId )
61 String s;
63 xub_StrLen nContStart = _rRawString.Search( _rPartId );
64 if( nContStart != STRING_NOTFOUND )
66 nContStart = nContStart + _rPartId.Len();
67 ++nContStart; // now it's start of content, directly after Id
69 xub_StrLen nContEnd = _rRawString.Search( sal_Unicode( ',' ), nContStart );
71 s = String( _rRawString, nContStart, nContEnd - nContStart );
74 return s;
79 MacroWarning::MacroWarning( Window* _pParent, bool _bWithSignatures, ResMgr& rResMgr )
80 :ModalDialog ( _pParent, ResId( RID_XMLSECDLG_MACROWARN, rResMgr ) )
81 ,mpInfos ( NULL )
82 ,maSymbolImg ( this, ResId( IMG_SYMBOL, rResMgr ) )
83 ,maDocNameFI ( this, ResId( FI_DOCNAME, rResMgr ) )
84 ,maDescr1aFI ( this, ResId( FI_DESCR1A, rResMgr ) )
85 ,maDescr1bFI ( this, ResId( FI_DESCR1B, rResMgr ) )
86 ,maSignsFI ( this, ResId( FI_SIGNS, rResMgr ) )
87 ,maViewSignsBtn ( this, ResId( PB_VIEWSIGNS, rResMgr ) )
88 ,maDescr2FI ( this, ResId( FI_DESCR2, rResMgr ) )
89 ,maAlwaysTrustCB ( this, ResId( CB_ALWAYSTRUST, rResMgr ) )
90 ,maBottomSepFL ( this, ResId( FL_BOTTOM_SEP, rResMgr ) )
91 ,maEnableBtn ( this, ResId( PB_ENABLE, rResMgr ) )
92 ,maDisableBtn ( this, ResId( PB_DISABLE, rResMgr ) )
93 ,maHelpBtn ( this, ResId( BTN_HELP, rResMgr ) )
94 ,mbSignedMode ( true )
95 ,mbShowSignatures ( _bWithSignatures )
96 ,mnActSecLevel ( 0 )
98 FreeResource();
100 InitControls();
102 maDisableBtn.SetClickHdl( LINK( this, MacroWarning, DisableBtnHdl ) );
103 maEnableBtn.SetClickHdl( LINK( this, MacroWarning, EnableBtnHdl ) );
104 maDisableBtn.GrabFocus(); // Default button, but focus is on view button
107 MacroWarning::~MacroWarning()
111 short MacroWarning::Execute()
113 FitControls();
114 return ModalDialog::Execute();
117 void MacroWarning::SetDocumentURL( const String& rDocURL )
119 maDocNameFI.SetText( rDocURL );
122 IMPL_LINK( MacroWarning, ViewSignsBtnHdl, void*, EMPTYARG )
124 DBG_ASSERT( mxCert.is(), "*MacroWarning::ViewSignsBtnHdl(): no certificate set!" );
126 uno::Sequence< uno::Any > aArgs( 1 );
127 aArgs[0] = uno::makeAny( maODFVersion );
128 uno::Reference< security::XDocumentDigitalSignatures > xD(
129 comphelper::getProcessServiceFactory()->createInstanceWithArguments( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.security.DocumentDigitalSignatures" ) ), aArgs ), uno::UNO_QUERY );
130 if( xD.is() )
132 if( mxCert.is() )
133 xD->showCertificate( mxCert );
134 else if( mxStore.is() )
135 xD->showScriptingContentSignatures( mxStore, uno::Reference< io::XInputStream >() );
138 return 0;
141 IMPL_LINK( MacroWarning, EnableBtnHdl, void*, EMPTYARG )
143 if( mbSignedMode && maAlwaysTrustCB.IsChecked() )
144 { // insert path into trusted path list
145 uno::Sequence< uno::Any > aArgs( 1 );
146 aArgs[0] = uno::makeAny( maODFVersion );
147 uno::Reference< security::XDocumentDigitalSignatures > xD(
148 comphelper::getProcessServiceFactory()->createInstanceWithArguments( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.security.DocumentDigitalSignatures" ) ), aArgs ), uno::UNO_QUERY );
149 if( xD.is() )
151 if( mxCert.is() )
152 xD->addAuthorToTrustedSources( mxCert );
153 else if( mxStore.is() )
155 DBG_ASSERT( mpInfos, "-MacroWarning::EnableBtnHdl(): no infos, search in nirvana..." );
157 sal_Int32 nCnt = mpInfos->getLength();
158 for( sal_Int32 i = 0 ; i < nCnt ; ++i )
159 xD->addAuthorToTrustedSources( (*mpInfos)[ i ].Signer );
164 EndDialog( RET_OK );
165 return 0;
168 IMPL_LINK( MacroWarning, DisableBtnHdl, void*, EMPTYARG )
170 EndDialog( RET_CANCEL );
171 return 0;
174 IMPL_LINK( MacroWarning, AlwaysTrustCheckHdl, void*, EMPTYARG )
176 bool bEnable = ( mnActSecLevel < 2 || maAlwaysTrustCB.IsChecked() );
177 maEnableBtn.Enable( bEnable );
178 maDisableBtn.Enable( !maAlwaysTrustCB.IsChecked() );
180 return 0;
183 void MacroWarning::InitControls()
185 // set warning image
186 Image aImg( WarningBox::GetStandardImage() );
187 maSymbolImg.SetImage( aImg );
188 maSymbolImg.SetSizePixel( aImg.GetSizePixel() );
189 // set bold font and path ellipsis for docname fixedtext
190 Font aTmpFont = maDocNameFI.GetControlFont();
191 aTmpFont.SetWeight( WEIGHT_BOLD );
192 maDocNameFI.SetControlFont( aTmpFont );
193 WinBits nStyle = maDocNameFI.GetStyle();
194 nStyle |= WB_PATHELLIPSIS;
195 maDocNameFI.SetStyle( nStyle );
196 // show signature controls?
197 if( mbShowSignatures )
199 maDescr1bFI.Hide();
200 maViewSignsBtn.SetClickHdl( LINK( this, MacroWarning, ViewSignsBtnHdl ) );
201 maViewSignsBtn.Disable(); // default
202 maAlwaysTrustCB.SetClickHdl( LINK( this, MacroWarning, AlwaysTrustCheckHdl ) );
204 mnActSecLevel = SvtSecurityOptions().GetMacroSecurityLevel();
205 if ( mnActSecLevel >= 2 )
206 maEnableBtn.Disable();
208 else
210 maDescr1aFI.Hide();
211 maSignsFI.Hide();
212 maViewSignsBtn.Hide();
213 maAlwaysTrustCB.Hide();
215 // move hint up to position of signer list
216 maDescr2FI.SetPosPixel( maSignsFI.GetPosPixel() );
218 // without signature controls could be smaller
219 if ( !mbShowSignatures )
221 Point aPos = maDescr2FI.GetPosPixel();
222 aPos.Y() += maDescr2FI.GetSizePixel().Height();
223 aPos.Y() += LogicToPixel( Size( 3, 3 ) ).Height();
224 long nDelta = maBottomSepFL.GetPosPixel().Y() - aPos.Y();
225 Window* pWins[] =
227 &maBottomSepFL, &maEnableBtn, &maDisableBtn, &maHelpBtn
229 Window** pCurrent = pWins;
230 for ( sal_uInt32 i = 0; i < sizeof( pWins ) / sizeof( pWins[ 0 ] ); ++i, ++pCurrent )
232 Point aNewPos = (*pCurrent)->GetPosPixel();
233 aNewPos.Y() -= nDelta;
234 (*pCurrent)->SetPosPixel( aNewPos );
237 Size aDlgSz = GetSizePixel();
238 aDlgSz.Height() -= nDelta;
239 SetSizePixel( aDlgSz );
242 // check if some buttontexts are to wide
243 String sText = maViewSignsBtn.GetText();
244 long nTxtW = maViewSignsBtn.GetTextWidth( sText );
245 const long nOffset = 12;
246 if ( sText.Search( '~' ) == STRING_NOTFOUND )
247 nTxtW += nOffset;
248 long nBtnW = maViewSignsBtn.GetSizePixel().Width();
249 if ( nTxtW >= nBtnW )
251 // broaden the button
252 long nDelta = Max( nTxtW - nBtnW, nOffset/3 );
253 Size aNewSize = maViewSignsBtn.GetSizePixel();
254 aNewSize.Width() += nDelta;
255 maViewSignsBtn.SetSizePixel( aNewSize );
256 // and give it a new position
257 Point aNewPos = maViewSignsBtn.GetPosPixel();
258 aNewPos.X() -= nDelta;
259 maViewSignsBtn.SetPosPixel( aNewPos );
260 // the the left fixedtext must be smaller
261 aNewSize = maSignsFI.GetSizePixel();
262 aNewSize.Width() -= nDelta;
263 maSignsFI.SetSizePixel( aNewSize );
265 // if the button text (we compare with the longest of both) is too wide, then broaden the buttons
266 String sText1 = maEnableBtn.GetText();
267 long nTxtW1 = maEnableBtn.GetTextWidth( sText1 );
268 if ( sText1.Search( '~' ) == STRING_NOTFOUND )
269 nTxtW1 += nOffset;
270 String sText2 = maDisableBtn.GetText();
271 long nTxtW2 = maDisableBtn.GetTextWidth( sText2 );
272 if ( sText2.Search( '~' ) == STRING_NOTFOUND )
273 nTxtW2 += nOffset;
274 nTxtW = Max( nTxtW1, nTxtW2 );
275 nBtnW = maEnableBtn.GetSizePixel().Width();
276 if ( nTxtW > nBtnW )
278 // broaden both buttons
279 long nDelta = nTxtW - nBtnW;
280 Size aNewSize = maEnableBtn.GetSizePixel();
281 aNewSize.Width() += nDelta;
282 maEnableBtn.SetSizePixel( aNewSize );
283 maDisableBtn.SetSizePixel( aNewSize );
284 // and give them a new position
285 Point aNewPos = maEnableBtn.GetPosPixel();
286 aNewPos.X() -= (2*nDelta);
287 maEnableBtn.SetPosPixel( aNewPos );
288 aNewPos = maDisableBtn.GetPosPixel();
289 aNewPos.X() -= nDelta;
290 maDisableBtn.SetPosPixel( aNewPos );
294 void MacroWarning::FitControls()
296 Size a3Size = LogicToPixel( Size( 3, 3 ), MAP_APPFONT );
297 Size aNewSize, aMinSize;
298 long nTxtH = 0;
299 long nCtrlH = 0;
300 long nDelta = 0;
302 if ( mbShowSignatures )
304 aMinSize = maSignsFI.CalcMinimumSize( maSignsFI.GetSizePixel().Width() );
305 nTxtH = Max( aMinSize.Height(), maViewSignsBtn.GetSizePixel().Height() );
306 nTxtH += a3Size.Height() / 2;
307 nCtrlH = maSignsFI.GetSizePixel().Height();
308 nDelta = Max( nCtrlH - nTxtH, static_cast< long >( -100 ) ); // not too large
309 aNewSize = maSignsFI.GetSizePixel();
310 aNewSize.Height() -= nDelta;
311 maSignsFI.SetSizePixel( aNewSize );
314 aMinSize = maDescr2FI.CalcMinimumSize( maDescr2FI.GetSizePixel().Width() );
315 nTxtH = aMinSize.Height();
316 nCtrlH = maDescr2FI.GetSizePixel().Height();
317 long nDelta2 = ( nCtrlH - nTxtH );
318 aNewSize = maDescr2FI.GetSizePixel();
319 aNewSize.Height() -= nDelta2;
320 maDescr2FI.SetSizePixel( aNewSize );
322 // new position for the succeeding windows
323 Window* pWins[] =
325 &maDescr2FI, &maAlwaysTrustCB, &maBottomSepFL, &maEnableBtn, &maDisableBtn, &maHelpBtn
327 Window** pCurrent = pWins;
328 for ( sal_uInt32 i = 0; i < sizeof( pWins ) / sizeof( pWins[ 0 ] ); ++i, ++pCurrent )
330 Point aNewPos = (*pCurrent)->GetPosPixel();
331 aNewPos.Y() -= nDelta;
332 (*pCurrent)->SetPosPixel( aNewPos );
334 if ( *pCurrent == &maDescr2FI )
335 nDelta += nDelta2;
338 // new size of the dialog
339 aNewSize = GetSizePixel();
340 aNewSize.Height() -= nDelta;
341 SetSizePixel( aNewSize );
344 void MacroWarning::SetStorage( const cssu::Reference < css::embed::XStorage >& rxStore,
345 const ::rtl::OUString& aODFVersion,
346 const cssu::Sequence< security::DocumentSignatureInformation >& rInfos )
348 mxStore = rxStore;
349 maODFVersion = aODFVersion;
350 sal_Int32 nCnt = rInfos.getLength();
351 if( mxStore.is() && nCnt > 0 )
353 mpInfos = &rInfos;
354 String aCN_Id( String::CreateFromAscii( "CN" ) );
355 String s;
356 s = GetContentPart( rInfos[ 0 ].Signer->getSubjectName(), aCN_Id );
358 for( sal_Int32 i = 1 ; i < nCnt ; ++i )
360 s.AppendAscii( "\n" );
361 s += GetContentPart( rInfos[ i ].Signer->getSubjectName(), aCN_Id );
364 maSignsFI.SetText( s );
365 maViewSignsBtn.Enable();
369 void MacroWarning::SetCertificate( const cssu::Reference< css::security::XCertificate >& _rxCert )
371 mxCert = _rxCert;
372 if( mxCert.is() )
374 String aCN_Id( String::CreateFromAscii( "CN" ) );
375 String s;
376 s = GetContentPart( mxCert->getSubjectName(), aCN_Id );
377 maSignsFI.SetText( s );
378 maViewSignsBtn.Enable();