Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / uui / source / secmacrowarnings.cxx
blobbe18ad6e932f878f81b06cda30cd396f64a4c8ae
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/security/XDocumentDigitalSignatures.hpp>
32 #include <comphelper/sequence.hxx>
33 #include "comphelper/documentconstants.hxx"
34 #include <comphelper/processfactory.hxx>
35 #include <sal/macros.h>
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_NOARG(MacroWarning, ViewSignsBtnHdl)
123 DBG_ASSERT( mxCert.is(), "*MacroWarning::ViewSignsBtnHdl(): no certificate set!" );
125 uno::Sequence< uno::Any > aArgs( 1 );
126 aArgs[0] = uno::makeAny( maODFVersion );
127 uno::Reference< security::XDocumentDigitalSignatures > xD(
128 comphelper::getProcessServiceFactory()->createInstanceWithArguments( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.security.DocumentDigitalSignatures" ) ), aArgs ), uno::UNO_QUERY );
129 if( xD.is() )
131 if( mxCert.is() )
132 xD->showCertificate( mxCert );
133 else if( mxStore.is() )
134 xD->showScriptingContentSignatures( mxStore, uno::Reference< io::XInputStream >() );
137 return 0;
140 IMPL_LINK_NOARG(MacroWarning, EnableBtnHdl)
142 if( mbSignedMode && maAlwaysTrustCB.IsChecked() )
143 { // insert path into trusted path list
144 uno::Sequence< uno::Any > aArgs( 1 );
145 aArgs[0] = uno::makeAny( maODFVersion );
146 uno::Reference< security::XDocumentDigitalSignatures > xD(
147 comphelper::getProcessServiceFactory()->createInstanceWithArguments( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.security.DocumentDigitalSignatures" ) ), aArgs ), uno::UNO_QUERY );
148 if( xD.is() )
150 if( mxCert.is() )
151 xD->addAuthorToTrustedSources( mxCert );
152 else if( mxStore.is() )
154 DBG_ASSERT( mpInfos, "-MacroWarning::EnableBtnHdl(): no infos, search in nirvana..." );
156 sal_Int32 nCnt = mpInfos->getLength();
157 for( sal_Int32 i = 0 ; i < nCnt ; ++i )
158 xD->addAuthorToTrustedSources( (*mpInfos)[ i ].Signer );
163 EndDialog( RET_OK );
164 return 0;
167 IMPL_LINK_NOARG(MacroWarning, DisableBtnHdl)
169 EndDialog( RET_CANCEL );
170 return 0;
173 IMPL_LINK_NOARG(MacroWarning, AlwaysTrustCheckHdl)
175 bool bEnable = ( mnActSecLevel < 2 || maAlwaysTrustCB.IsChecked() );
176 maEnableBtn.Enable( bEnable );
177 maDisableBtn.Enable( !maAlwaysTrustCB.IsChecked() );
179 return 0;
182 void MacroWarning::InitControls()
184 // set warning image
185 Image aImg( WarningBox::GetStandardImage() );
186 maSymbolImg.SetImage( aImg );
187 maSymbolImg.SetSizePixel( aImg.GetSizePixel() );
188 // set bold font and path ellipsis for docname fixedtext
189 Font aTmpFont = maDocNameFI.GetControlFont();
190 aTmpFont.SetWeight( WEIGHT_BOLD );
191 maDocNameFI.SetControlFont( aTmpFont );
192 WinBits nStyle = maDocNameFI.GetStyle();
193 nStyle |= WB_PATHELLIPSIS;
194 maDocNameFI.SetStyle( nStyle );
195 // show signature controls?
196 if( mbShowSignatures )
198 maDescr1bFI.Hide();
199 maViewSignsBtn.SetClickHdl( LINK( this, MacroWarning, ViewSignsBtnHdl ) );
200 maViewSignsBtn.Disable(); // default
201 maAlwaysTrustCB.SetClickHdl( LINK( this, MacroWarning, AlwaysTrustCheckHdl ) );
203 mnActSecLevel = SvtSecurityOptions().GetMacroSecurityLevel();
204 if ( mnActSecLevel >= 2 )
205 maEnableBtn.Disable();
207 else
209 maDescr1aFI.Hide();
210 maSignsFI.Hide();
211 maViewSignsBtn.Hide();
212 maAlwaysTrustCB.Hide();
214 // move hint up to position of signer list
215 maDescr2FI.SetPosPixel( maSignsFI.GetPosPixel() );
217 // without signature controls could be smaller
218 if ( !mbShowSignatures )
220 Point aPos = maDescr2FI.GetPosPixel();
221 aPos.Y() += maDescr2FI.GetSizePixel().Height();
222 aPos.Y() += LogicToPixel( Size( 3, 3 ) ).Height();
223 long nDelta = maBottomSepFL.GetPosPixel().Y() - aPos.Y();
224 Window* pWins[] =
226 &maBottomSepFL, &maEnableBtn, &maDisableBtn, &maHelpBtn
228 Window** pCurrent = pWins;
229 for ( sal_uInt32 i = 0; i < sizeof( pWins ) / sizeof( pWins[ 0 ] ); ++i, ++pCurrent )
231 Point aNewPos = (*pCurrent)->GetPosPixel();
232 aNewPos.Y() -= nDelta;
233 (*pCurrent)->SetPosPixel( aNewPos );
236 Size aDlgSz = GetSizePixel();
237 aDlgSz.Height() -= nDelta;
238 SetSizePixel( aDlgSz );
241 // check if some buttontexts are to wide
242 String sText = maViewSignsBtn.GetText();
243 long nTxtW = maViewSignsBtn.GetTextWidth( sText );
244 const long nOffset = 12;
245 if ( sText.Search( '~' ) == STRING_NOTFOUND )
246 nTxtW += nOffset;
247 long nBtnW = maViewSignsBtn.GetSizePixel().Width();
248 if ( nTxtW >= nBtnW )
250 // broaden the button
251 long nDelta = Max( nTxtW - nBtnW, nOffset/3 );
252 Size aNewSize = maViewSignsBtn.GetSizePixel();
253 aNewSize.Width() += nDelta;
254 maViewSignsBtn.SetSizePixel( aNewSize );
255 // and give it a new position
256 Point aNewPos = maViewSignsBtn.GetPosPixel();
257 aNewPos.X() -= nDelta;
258 maViewSignsBtn.SetPosPixel( aNewPos );
259 // the the left fixedtext must be smaller
260 aNewSize = maSignsFI.GetSizePixel();
261 aNewSize.Width() -= nDelta;
262 maSignsFI.SetSizePixel( aNewSize );
264 // if the button text (we compare with the longest of both) is too wide, then broaden the buttons
265 String sText1 = maEnableBtn.GetText();
266 long nTxtW1 = maEnableBtn.GetTextWidth( sText1 );
267 if ( sText1.Search( '~' ) == STRING_NOTFOUND )
268 nTxtW1 += nOffset;
269 String sText2 = maDisableBtn.GetText();
270 long nTxtW2 = maDisableBtn.GetTextWidth( sText2 );
271 if ( sText2.Search( '~' ) == STRING_NOTFOUND )
272 nTxtW2 += nOffset;
273 nTxtW = Max( nTxtW1, nTxtW2 );
274 nBtnW = maEnableBtn.GetSizePixel().Width();
275 if ( nTxtW > nBtnW )
277 // broaden both buttons
278 long nDelta = nTxtW - nBtnW;
279 Size aNewSize = maEnableBtn.GetSizePixel();
280 aNewSize.Width() += nDelta;
281 maEnableBtn.SetSizePixel( aNewSize );
282 maDisableBtn.SetSizePixel( aNewSize );
283 // and give them a new position
284 Point aNewPos = maEnableBtn.GetPosPixel();
285 aNewPos.X() -= (2*nDelta);
286 maEnableBtn.SetPosPixel( aNewPos );
287 aNewPos = maDisableBtn.GetPosPixel();
288 aNewPos.X() -= nDelta;
289 maDisableBtn.SetPosPixel( aNewPos );
293 void MacroWarning::FitControls()
295 Size a3Size = LogicToPixel( Size( 3, 3 ), MAP_APPFONT );
296 Size aNewSize, aMinSize;
297 long nTxtH = 0;
298 long nCtrlH = 0;
299 long nDelta = 0;
301 if ( mbShowSignatures )
303 aMinSize = maSignsFI.CalcMinimumSize( maSignsFI.GetSizePixel().Width() );
304 nTxtH = Max( aMinSize.Height(), maViewSignsBtn.GetSizePixel().Height() );
305 nTxtH += a3Size.Height() / 2;
306 nCtrlH = maSignsFI.GetSizePixel().Height();
307 nDelta = Max( nCtrlH - nTxtH, static_cast< long >( -100 ) ); // not too large
308 aNewSize = maSignsFI.GetSizePixel();
309 aNewSize.Height() -= nDelta;
310 maSignsFI.SetSizePixel( aNewSize );
313 aMinSize = maDescr2FI.CalcMinimumSize( maDescr2FI.GetSizePixel().Width() );
314 nTxtH = aMinSize.Height();
315 nCtrlH = maDescr2FI.GetSizePixel().Height();
316 long nDelta2 = ( nCtrlH - nTxtH );
317 aNewSize = maDescr2FI.GetSizePixel();
318 aNewSize.Height() -= nDelta2;
319 maDescr2FI.SetSizePixel( aNewSize );
321 // new position for the succeeding windows
322 Window* pWins[] =
324 &maDescr2FI, &maAlwaysTrustCB, &maBottomSepFL, &maEnableBtn, &maDisableBtn, &maHelpBtn
326 Window** pCurrent = pWins;
327 for ( sal_uInt32 i = 0; i < sizeof( pWins ) / sizeof( pWins[ 0 ] ); ++i, ++pCurrent )
329 Point aNewPos = (*pCurrent)->GetPosPixel();
330 aNewPos.Y() -= nDelta;
331 (*pCurrent)->SetPosPixel( aNewPos );
333 if ( *pCurrent == &maDescr2FI )
334 nDelta += nDelta2;
337 // new size of the dialog
338 aNewSize = GetSizePixel();
339 aNewSize.Height() -= nDelta;
340 SetSizePixel( aNewSize );
343 void MacroWarning::SetStorage( const cssu::Reference < css::embed::XStorage >& rxStore,
344 const ::rtl::OUString& aODFVersion,
345 const cssu::Sequence< security::DocumentSignatureInformation >& rInfos )
347 mxStore = rxStore;
348 maODFVersion = aODFVersion;
349 sal_Int32 nCnt = rInfos.getLength();
350 if( mxStore.is() && nCnt > 0 )
352 mpInfos = &rInfos;
353 String aCN_Id( String::CreateFromAscii( "CN" ) );
354 String s;
355 s = GetContentPart( rInfos[ 0 ].Signer->getSubjectName(), aCN_Id );
357 for( sal_Int32 i = 1 ; i < nCnt ; ++i )
359 s.AppendAscii( "\n" );
360 s += GetContentPart( rInfos[ i ].Signer->getSubjectName(), aCN_Id );
363 maSignsFI.SetText( s );
364 maViewSignsBtn.Enable();
368 void MacroWarning::SetCertificate( const cssu::Reference< css::security::XCertificate >& _rxCert )
370 mxCert = _rxCert;
371 if( mxCert.is() )
373 String aCN_Id( String::CreateFromAscii( "CN" ) );
374 String s;
375 s = GetContentPart( mxCert->getSubjectName(), aCN_Id );
376 maSignsFI.SetText( s );
377 maViewSignsBtn.Enable();
381 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */