bump product version to 4.1.6.2
[LibreOffice.git] / uui / source / secmacrowarnings.cxx
blob8e98e3cef63c388558eed2b202bcb97f8d091f14
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
21 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
22 #include <com/sun/star/security/DocumentDigitalSignatures.hpp>
23 #include <comphelper/sequence.hxx>
24 #include "comphelper/documentconstants.hxx"
25 #include <comphelper/processfactory.hxx>
26 #include <sal/macros.h>
28 #include <vcl/msgbox.hxx>
29 #include <com/sun/star/security/NoPasswordException.hpp>
31 using namespace ::com::sun::star::security;
33 #include "ids.hrc"
34 #include "secmacrowarnings.hxx"
35 #include "secmacrowarnings.hrc"
37 #ifdef _MSC_VER
38 #pragma warning (disable : 4355) // 4355: this used in initializer-list
39 #endif
41 using namespace ::com::sun::star;
44 // HACK!!! copied from xmlsecurity/source/dialog/resourcemanager.cxx
46 namespace
48 String GetContentPart( const String& _rRawString, const String& _rPartId )
50 String s;
52 xub_StrLen nContStart = _rRawString.Search( _rPartId );
53 if( nContStart != STRING_NOTFOUND )
55 nContStart = nContStart + _rPartId.Len();
56 ++nContStart; // now it's start of content, directly after Id
58 xub_StrLen nContEnd = _rRawString.Search( sal_Unicode( ',' ), nContStart );
60 s = String( _rRawString, nContStart, nContEnd - nContStart );
63 return s;
68 MacroWarning::MacroWarning( Window* _pParent, bool _bWithSignatures, ResMgr& rResMgr )
69 :ModalDialog ( _pParent, ResId( RID_XMLSECDLG_MACROWARN, rResMgr ) )
70 ,mpInfos ( NULL )
71 ,maSymbolImg ( this, ResId( IMG_SYMBOL, rResMgr ) )
72 ,maDocNameFI ( this, ResId( FI_DOCNAME, rResMgr ) )
73 ,maDescr1aFI ( this, ResId( FI_DESCR1A, rResMgr ) )
74 ,maDescr1bFI ( this, ResId( FI_DESCR1B, rResMgr ) )
75 ,maSignsFI ( this, ResId( FI_SIGNS, rResMgr ) )
76 ,maViewSignsBtn ( this, ResId( PB_VIEWSIGNS, rResMgr ) )
77 ,maDescr2FI ( this, ResId( FI_DESCR2, rResMgr ) )
78 ,maAlwaysTrustCB ( this, ResId( CB_ALWAYSTRUST, rResMgr ) )
79 ,maBottomSepFL ( this, ResId( FL_BOTTOM_SEP, rResMgr ) )
80 ,maEnableBtn ( this, ResId( PB_ENABLE, rResMgr ) )
81 ,maDisableBtn ( this, ResId( PB_DISABLE, rResMgr ) )
82 ,maHelpBtn ( this, ResId( BTN_HELP, rResMgr ) )
83 ,mbSignedMode ( true )
84 ,mbShowSignatures ( _bWithSignatures )
85 ,mnActSecLevel ( 0 )
87 FreeResource();
89 InitControls();
91 maDisableBtn.SetClickHdl( LINK( this, MacroWarning, DisableBtnHdl ) );
92 maEnableBtn.SetClickHdl( LINK( this, MacroWarning, EnableBtnHdl ) );
93 maDisableBtn.GrabFocus(); // Default button, but focus is on view button
96 MacroWarning::~MacroWarning()
100 short MacroWarning::Execute()
102 FitControls();
103 return ModalDialog::Execute();
106 void MacroWarning::SetDocumentURL( const String& rDocURL )
108 maDocNameFI.SetText( rDocURL );
111 IMPL_LINK_NOARG(MacroWarning, ViewSignsBtnHdl)
113 DBG_ASSERT( mxCert.is(), "*MacroWarning::ViewSignsBtnHdl(): no certificate set!" );
115 uno::Reference< security::XDocumentDigitalSignatures > xD(
116 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion));
117 if( xD.is() )
119 if( mxCert.is() )
120 xD->showCertificate( mxCert );
121 else if( mxStore.is() )
122 xD->showScriptingContentSignatures( mxStore, uno::Reference< io::XInputStream >() );
125 return 0;
128 IMPL_LINK_NOARG(MacroWarning, EnableBtnHdl)
130 if( mbSignedMode && maAlwaysTrustCB.IsChecked() )
131 { // insert path into trusted path list
132 uno::Reference< security::XDocumentDigitalSignatures > xD(
133 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion));
134 if( mxCert.is() )
135 xD->addAuthorToTrustedSources( mxCert );
136 else if( mxStore.is() )
138 DBG_ASSERT( mpInfos, "-MacroWarning::EnableBtnHdl(): no infos, search in nirvana..." );
140 sal_Int32 nCnt = mpInfos->getLength();
141 for( sal_Int32 i = 0 ; i < nCnt ; ++i )
142 xD->addAuthorToTrustedSources( (*mpInfos)[ i ].Signer );
146 EndDialog( RET_OK );
147 return 0;
150 IMPL_LINK_NOARG(MacroWarning, DisableBtnHdl)
152 EndDialog( RET_CANCEL );
153 return 0;
156 IMPL_LINK_NOARG(MacroWarning, AlwaysTrustCheckHdl)
158 bool bEnable = ( mnActSecLevel < 2 || maAlwaysTrustCB.IsChecked() );
159 maEnableBtn.Enable( bEnable );
160 maDisableBtn.Enable( !maAlwaysTrustCB.IsChecked() );
162 return 0;
165 void MacroWarning::InitControls()
167 // set warning image
168 Image aImg( WarningBox::GetStandardImage() );
169 maSymbolImg.SetImage( aImg );
170 maSymbolImg.SetSizePixel( aImg.GetSizePixel() );
171 // set bold font and path ellipsis for docname fixedtext
172 Font aTmpFont = maDocNameFI.GetControlFont();
173 aTmpFont.SetWeight( WEIGHT_BOLD );
174 maDocNameFI.SetControlFont( aTmpFont );
175 WinBits nStyle = maDocNameFI.GetStyle();
176 nStyle |= WB_PATHELLIPSIS;
177 maDocNameFI.SetStyle( nStyle );
178 // show signature controls?
179 if( mbShowSignatures )
181 maDescr1bFI.Hide();
182 maViewSignsBtn.SetClickHdl( LINK( this, MacroWarning, ViewSignsBtnHdl ) );
183 maViewSignsBtn.Disable(); // default
184 maAlwaysTrustCB.SetClickHdl( LINK( this, MacroWarning, AlwaysTrustCheckHdl ) );
186 mnActSecLevel = SvtSecurityOptions().GetMacroSecurityLevel();
187 if ( mnActSecLevel >= 2 )
188 maEnableBtn.Disable();
190 else
192 maDescr1aFI.Hide();
193 maSignsFI.Hide();
194 maViewSignsBtn.Hide();
195 maAlwaysTrustCB.Hide();
197 // move hint up to position of signer list
198 maDescr2FI.SetPosPixel( maSignsFI.GetPosPixel() );
200 // without signature controls could be smaller
201 if ( !mbShowSignatures )
203 Point aPos = maDescr2FI.GetPosPixel();
204 aPos.Y() += maDescr2FI.GetSizePixel().Height();
205 aPos.Y() += LogicToPixel( Size( 3, 3 ) ).Height();
206 long nDelta = maBottomSepFL.GetPosPixel().Y() - aPos.Y();
207 Window* pWins[] =
209 &maBottomSepFL, &maEnableBtn, &maDisableBtn, &maHelpBtn
211 Window** pCurrent = pWins;
212 for ( sal_uInt32 i = 0; i < sizeof( pWins ) / sizeof( pWins[ 0 ] ); ++i, ++pCurrent )
214 Point aNewPos = (*pCurrent)->GetPosPixel();
215 aNewPos.Y() -= nDelta;
216 (*pCurrent)->SetPosPixel( aNewPos );
219 Size aDlgSz = GetSizePixel();
220 aDlgSz.Height() -= nDelta;
221 SetSizePixel( aDlgSz );
224 // check if some buttontexts are to wide
225 String sText = maViewSignsBtn.GetText();
226 long nTxtW = maViewSignsBtn.GetTextWidth( sText );
227 const long nOffset = 12;
228 if ( sText.Search( '~' ) == STRING_NOTFOUND )
229 nTxtW += nOffset;
230 long nBtnW = maViewSignsBtn.GetSizePixel().Width();
231 if ( nTxtW >= nBtnW )
233 // broaden the button
234 long nDelta = std::max( nTxtW - nBtnW, nOffset/3 );
235 Size aNewSize = maViewSignsBtn.GetSizePixel();
236 aNewSize.Width() += nDelta;
237 maViewSignsBtn.SetSizePixel( aNewSize );
238 // and give it a new position
239 Point aNewPos = maViewSignsBtn.GetPosPixel();
240 aNewPos.X() -= nDelta;
241 maViewSignsBtn.SetPosPixel( aNewPos );
242 // the left fixedtext must be smaller
243 aNewSize = maSignsFI.GetSizePixel();
244 aNewSize.Width() -= nDelta;
245 maSignsFI.SetSizePixel( aNewSize );
247 // if the button text (we compare with the longest of both) is too wide, then broaden the buttons
248 String sText1 = maEnableBtn.GetText();
249 long nTxtW1 = maEnableBtn.GetTextWidth( sText1 );
250 if ( sText1.Search( '~' ) == STRING_NOTFOUND )
251 nTxtW1 += nOffset;
252 String sText2 = maDisableBtn.GetText();
253 long nTxtW2 = maDisableBtn.GetTextWidth( sText2 );
254 if ( sText2.Search( '~' ) == STRING_NOTFOUND )
255 nTxtW2 += nOffset;
256 nTxtW = std::max( nTxtW1, nTxtW2 );
257 nBtnW = maEnableBtn.GetSizePixel().Width();
258 if ( nTxtW > nBtnW )
260 // broaden both buttons
261 long nDelta = nTxtW - nBtnW;
262 Size aNewSize = maEnableBtn.GetSizePixel();
263 aNewSize.Width() += nDelta;
264 maEnableBtn.SetSizePixel( aNewSize );
265 maDisableBtn.SetSizePixel( aNewSize );
266 // and give them a new position
267 Point aNewPos = maEnableBtn.GetPosPixel();
268 aNewPos.X() -= (2*nDelta);
269 maEnableBtn.SetPosPixel( aNewPos );
270 aNewPos = maDisableBtn.GetPosPixel();
271 aNewPos.X() -= nDelta;
272 maDisableBtn.SetPosPixel( aNewPos );
276 void MacroWarning::FitControls()
278 Size a3Size = LogicToPixel( Size( 3, 3 ), MAP_APPFONT );
279 Size aNewSize, aMinSize;
280 long nTxtH = 0;
281 long nCtrlH = 0;
282 long nDelta = 0;
284 if ( mbShowSignatures )
286 aMinSize = maSignsFI.CalcMinimumSize( maSignsFI.GetSizePixel().Width() );
287 nTxtH = std::max( aMinSize.Height(), maViewSignsBtn.GetSizePixel().Height() );
288 nTxtH += a3Size.Height() / 2;
289 nCtrlH = maSignsFI.GetSizePixel().Height();
290 nDelta = std::max( nCtrlH - nTxtH, static_cast< long >( -100 ) ); // not too large
291 aNewSize = maSignsFI.GetSizePixel();
292 aNewSize.Height() -= nDelta;
293 maSignsFI.SetSizePixel( aNewSize );
296 aMinSize = maDescr2FI.CalcMinimumSize( maDescr2FI.GetSizePixel().Width() );
297 nTxtH = aMinSize.Height();
298 nCtrlH = maDescr2FI.GetSizePixel().Height();
299 long nDelta2 = ( nCtrlH - nTxtH );
300 aNewSize = maDescr2FI.GetSizePixel();
301 aNewSize.Height() -= nDelta2;
302 maDescr2FI.SetSizePixel( aNewSize );
304 // new position for the succeeding windows
305 Window* pWins[] =
307 &maDescr2FI, &maAlwaysTrustCB, &maBottomSepFL, &maEnableBtn, &maDisableBtn, &maHelpBtn
309 Window** pCurrent = pWins;
310 for ( sal_uInt32 i = 0; i < sizeof( pWins ) / sizeof( pWins[ 0 ] ); ++i, ++pCurrent )
312 Point aNewPos = (*pCurrent)->GetPosPixel();
313 aNewPos.Y() -= nDelta;
314 (*pCurrent)->SetPosPixel( aNewPos );
316 if ( *pCurrent == &maDescr2FI )
317 nDelta += nDelta2;
320 // new size of the dialog
321 aNewSize = GetSizePixel();
322 aNewSize.Height() -= nDelta;
323 SetSizePixel( aNewSize );
326 void MacroWarning::SetStorage( const css::uno::Reference < css::embed::XStorage >& rxStore,
327 const OUString& aODFVersion,
328 const css::uno::Sequence< security::DocumentSignatureInformation >& rInfos )
330 mxStore = rxStore;
331 maODFVersion = aODFVersion;
332 sal_Int32 nCnt = rInfos.getLength();
333 if( mxStore.is() && nCnt > 0 )
335 mpInfos = &rInfos;
336 OUString aCN_Id("CN");
337 String s;
338 s = GetContentPart( rInfos[ 0 ].Signer->getSubjectName(), aCN_Id );
340 for( sal_Int32 i = 1 ; i < nCnt ; ++i )
342 s.AppendAscii( "\n" );
343 s += GetContentPart( rInfos[ i ].Signer->getSubjectName(), aCN_Id );
346 maSignsFI.SetText( s );
347 maViewSignsBtn.Enable();
351 void MacroWarning::SetCertificate( const css::uno::Reference< css::security::XCertificate >& _rxCert )
353 mxCert = _rxCert;
354 if( mxCert.is() )
356 OUString aCN_Id("CN");
357 String s;
358 s = GetContentPart( mxCert->getSubjectName(), aCN_Id );
359 maSignsFI.SetText( s );
360 maViewSignsBtn.Enable();
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */