Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / uui / source / secmacrowarnings.cxx
blob44605e278f1ad9e572dce3367b72d616196e0e10
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;
42 using namespace ::com::sun::star;
45 // HACK!!! copied from xmlsecurity/source/dialog/resourcemanager.cxx
47 namespace
49 String GetContentPart( const String& _rRawString, const String& _rPartId )
51 String s;
53 xub_StrLen nContStart = _rRawString.Search( _rPartId );
54 if( nContStart != STRING_NOTFOUND )
56 nContStart = nContStart + _rPartId.Len();
57 ++nContStart; // now it's start of content, directly after Id
59 xub_StrLen nContEnd = _rRawString.Search( sal_Unicode( ',' ), nContStart );
61 s = String( _rRawString, nContStart, nContEnd - nContStart );
64 return s;
69 MacroWarning::MacroWarning( Window* _pParent, bool _bWithSignatures, ResMgr& rResMgr )
70 :ModalDialog ( _pParent, ResId( RID_XMLSECDLG_MACROWARN, rResMgr ) )
71 ,mpInfos ( NULL )
72 ,maSymbolImg ( this, ResId( IMG_SYMBOL, rResMgr ) )
73 ,maDocNameFI ( this, ResId( FI_DOCNAME, rResMgr ) )
74 ,maDescr1aFI ( this, ResId( FI_DESCR1A, rResMgr ) )
75 ,maDescr1bFI ( this, ResId( FI_DESCR1B, rResMgr ) )
76 ,maSignsFI ( this, ResId( FI_SIGNS, rResMgr ) )
77 ,maViewSignsBtn ( this, ResId( PB_VIEWSIGNS, rResMgr ) )
78 ,maDescr2FI ( this, ResId( FI_DESCR2, rResMgr ) )
79 ,maAlwaysTrustCB ( this, ResId( CB_ALWAYSTRUST, rResMgr ) )
80 ,maBottomSepFL ( this, ResId( FL_BOTTOM_SEP, rResMgr ) )
81 ,maEnableBtn ( this, ResId( PB_ENABLE, rResMgr ) )
82 ,maDisableBtn ( this, ResId( PB_DISABLE, rResMgr ) )
83 ,maHelpBtn ( this, ResId( BTN_HELP, rResMgr ) )
84 ,mbSignedMode ( true )
85 ,mbShowSignatures ( _bWithSignatures )
86 ,mnActSecLevel ( 0 )
88 FreeResource();
90 InitControls();
92 maDisableBtn.SetClickHdl( LINK( this, MacroWarning, DisableBtnHdl ) );
93 maEnableBtn.SetClickHdl( LINK( this, MacroWarning, EnableBtnHdl ) );
94 maDisableBtn.GrabFocus(); // Default button, but focus is on view button
97 MacroWarning::~MacroWarning()
101 short MacroWarning::Execute()
103 FitControls();
104 return ModalDialog::Execute();
107 void MacroWarning::SetDocumentURL( const String& rDocURL )
109 maDocNameFI.SetText( rDocURL );
112 IMPL_LINK_NOARG(MacroWarning, ViewSignsBtnHdl)
114 DBG_ASSERT( mxCert.is(), "*MacroWarning::ViewSignsBtnHdl(): no certificate set!" );
116 uno::Reference< security::XDocumentDigitalSignatures > xD(
117 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion));
118 if( xD.is() )
120 if( mxCert.is() )
121 xD->showCertificate( mxCert );
122 else if( mxStore.is() )
123 xD->showScriptingContentSignatures( mxStore, uno::Reference< io::XInputStream >() );
126 return 0;
129 IMPL_LINK_NOARG(MacroWarning, EnableBtnHdl)
131 if( mbSignedMode && maAlwaysTrustCB.IsChecked() )
132 { // insert path into trusted path list
133 uno::Reference< security::XDocumentDigitalSignatures > xD(
134 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion));
135 if( mxCert.is() )
136 xD->addAuthorToTrustedSources( mxCert );
137 else if( mxStore.is() )
139 DBG_ASSERT( mpInfos, "-MacroWarning::EnableBtnHdl(): no infos, search in nirvana..." );
141 sal_Int32 nCnt = mpInfos->getLength();
142 for( sal_Int32 i = 0 ; i < nCnt ; ++i )
143 xD->addAuthorToTrustedSources( (*mpInfos)[ i ].Signer );
147 EndDialog( RET_OK );
148 return 0;
151 IMPL_LINK_NOARG(MacroWarning, DisableBtnHdl)
153 EndDialog( RET_CANCEL );
154 return 0;
157 IMPL_LINK_NOARG(MacroWarning, AlwaysTrustCheckHdl)
159 bool bEnable = ( mnActSecLevel < 2 || maAlwaysTrustCB.IsChecked() );
160 maEnableBtn.Enable( bEnable );
161 maDisableBtn.Enable( !maAlwaysTrustCB.IsChecked() );
163 return 0;
166 void MacroWarning::InitControls()
168 // set warning image
169 Image aImg( WarningBox::GetStandardImage() );
170 maSymbolImg.SetImage( aImg );
171 maSymbolImg.SetSizePixel( aImg.GetSizePixel() );
172 // set bold font and path ellipsis for docname fixedtext
173 Font aTmpFont = maDocNameFI.GetControlFont();
174 aTmpFont.SetWeight( WEIGHT_BOLD );
175 maDocNameFI.SetControlFont( aTmpFont );
176 WinBits nStyle = maDocNameFI.GetStyle();
177 nStyle |= WB_PATHELLIPSIS;
178 maDocNameFI.SetStyle( nStyle );
179 // show signature controls?
180 if( mbShowSignatures )
182 maDescr1bFI.Hide();
183 maViewSignsBtn.SetClickHdl( LINK( this, MacroWarning, ViewSignsBtnHdl ) );
184 maViewSignsBtn.Disable(); // default
185 maAlwaysTrustCB.SetClickHdl( LINK( this, MacroWarning, AlwaysTrustCheckHdl ) );
187 mnActSecLevel = SvtSecurityOptions().GetMacroSecurityLevel();
188 if ( mnActSecLevel >= 2 )
189 maEnableBtn.Disable();
191 else
193 maDescr1aFI.Hide();
194 maSignsFI.Hide();
195 maViewSignsBtn.Hide();
196 maAlwaysTrustCB.Hide();
198 // move hint up to position of signer list
199 maDescr2FI.SetPosPixel( maSignsFI.GetPosPixel() );
201 // without signature controls could be smaller
202 if ( !mbShowSignatures )
204 Point aPos = maDescr2FI.GetPosPixel();
205 aPos.Y() += maDescr2FI.GetSizePixel().Height();
206 aPos.Y() += LogicToPixel( Size( 3, 3 ) ).Height();
207 long nDelta = maBottomSepFL.GetPosPixel().Y() - aPos.Y();
208 Window* pWins[] =
210 &maBottomSepFL, &maEnableBtn, &maDisableBtn, &maHelpBtn
212 Window** pCurrent = pWins;
213 for ( sal_uInt32 i = 0; i < sizeof( pWins ) / sizeof( pWins[ 0 ] ); ++i, ++pCurrent )
215 Point aNewPos = (*pCurrent)->GetPosPixel();
216 aNewPos.Y() -= nDelta;
217 (*pCurrent)->SetPosPixel( aNewPos );
220 Size aDlgSz = GetSizePixel();
221 aDlgSz.Height() -= nDelta;
222 SetSizePixel( aDlgSz );
225 // check if some buttontexts are to wide
226 String sText = maViewSignsBtn.GetText();
227 long nTxtW = maViewSignsBtn.GetTextWidth( sText );
228 const long nOffset = 12;
229 if ( sText.Search( '~' ) == STRING_NOTFOUND )
230 nTxtW += nOffset;
231 long nBtnW = maViewSignsBtn.GetSizePixel().Width();
232 if ( nTxtW >= nBtnW )
234 // broaden the button
235 long nDelta = Max( nTxtW - nBtnW, nOffset/3 );
236 Size aNewSize = maViewSignsBtn.GetSizePixel();
237 aNewSize.Width() += nDelta;
238 maViewSignsBtn.SetSizePixel( aNewSize );
239 // and give it a new position
240 Point aNewPos = maViewSignsBtn.GetPosPixel();
241 aNewPos.X() -= nDelta;
242 maViewSignsBtn.SetPosPixel( aNewPos );
243 // the the left fixedtext must be smaller
244 aNewSize = maSignsFI.GetSizePixel();
245 aNewSize.Width() -= nDelta;
246 maSignsFI.SetSizePixel( aNewSize );
248 // if the button text (we compare with the longest of both) is too wide, then broaden the buttons
249 String sText1 = maEnableBtn.GetText();
250 long nTxtW1 = maEnableBtn.GetTextWidth( sText1 );
251 if ( sText1.Search( '~' ) == STRING_NOTFOUND )
252 nTxtW1 += nOffset;
253 String sText2 = maDisableBtn.GetText();
254 long nTxtW2 = maDisableBtn.GetTextWidth( sText2 );
255 if ( sText2.Search( '~' ) == STRING_NOTFOUND )
256 nTxtW2 += nOffset;
257 nTxtW = Max( nTxtW1, nTxtW2 );
258 nBtnW = maEnableBtn.GetSizePixel().Width();
259 if ( nTxtW > nBtnW )
261 // broaden both buttons
262 long nDelta = nTxtW - nBtnW;
263 Size aNewSize = maEnableBtn.GetSizePixel();
264 aNewSize.Width() += nDelta;
265 maEnableBtn.SetSizePixel( aNewSize );
266 maDisableBtn.SetSizePixel( aNewSize );
267 // and give them a new position
268 Point aNewPos = maEnableBtn.GetPosPixel();
269 aNewPos.X() -= (2*nDelta);
270 maEnableBtn.SetPosPixel( aNewPos );
271 aNewPos = maDisableBtn.GetPosPixel();
272 aNewPos.X() -= nDelta;
273 maDisableBtn.SetPosPixel( aNewPos );
277 void MacroWarning::FitControls()
279 Size a3Size = LogicToPixel( Size( 3, 3 ), MAP_APPFONT );
280 Size aNewSize, aMinSize;
281 long nTxtH = 0;
282 long nCtrlH = 0;
283 long nDelta = 0;
285 if ( mbShowSignatures )
287 aMinSize = maSignsFI.CalcMinimumSize( maSignsFI.GetSizePixel().Width() );
288 nTxtH = Max( aMinSize.Height(), maViewSignsBtn.GetSizePixel().Height() );
289 nTxtH += a3Size.Height() / 2;
290 nCtrlH = maSignsFI.GetSizePixel().Height();
291 nDelta = Max( nCtrlH - nTxtH, static_cast< long >( -100 ) ); // not too large
292 aNewSize = maSignsFI.GetSizePixel();
293 aNewSize.Height() -= nDelta;
294 maSignsFI.SetSizePixel( aNewSize );
297 aMinSize = maDescr2FI.CalcMinimumSize( maDescr2FI.GetSizePixel().Width() );
298 nTxtH = aMinSize.Height();
299 nCtrlH = maDescr2FI.GetSizePixel().Height();
300 long nDelta2 = ( nCtrlH - nTxtH );
301 aNewSize = maDescr2FI.GetSizePixel();
302 aNewSize.Height() -= nDelta2;
303 maDescr2FI.SetSizePixel( aNewSize );
305 // new position for the succeeding windows
306 Window* pWins[] =
308 &maDescr2FI, &maAlwaysTrustCB, &maBottomSepFL, &maEnableBtn, &maDisableBtn, &maHelpBtn
310 Window** pCurrent = pWins;
311 for ( sal_uInt32 i = 0; i < sizeof( pWins ) / sizeof( pWins[ 0 ] ); ++i, ++pCurrent )
313 Point aNewPos = (*pCurrent)->GetPosPixel();
314 aNewPos.Y() -= nDelta;
315 (*pCurrent)->SetPosPixel( aNewPos );
317 if ( *pCurrent == &maDescr2FI )
318 nDelta += nDelta2;
321 // new size of the dialog
322 aNewSize = GetSizePixel();
323 aNewSize.Height() -= nDelta;
324 SetSizePixel( aNewSize );
327 void MacroWarning::SetStorage( const cssu::Reference < css::embed::XStorage >& rxStore,
328 const ::rtl::OUString& aODFVersion,
329 const cssu::Sequence< security::DocumentSignatureInformation >& rInfos )
331 mxStore = rxStore;
332 maODFVersion = aODFVersion;
333 sal_Int32 nCnt = rInfos.getLength();
334 if( mxStore.is() && nCnt > 0 )
336 mpInfos = &rInfos;
337 rtl::OUString aCN_Id("CN");
338 String s;
339 s = GetContentPart( rInfos[ 0 ].Signer->getSubjectName(), aCN_Id );
341 for( sal_Int32 i = 1 ; i < nCnt ; ++i )
343 s.AppendAscii( "\n" );
344 s += GetContentPart( rInfos[ i ].Signer->getSubjectName(), aCN_Id );
347 maSignsFI.SetText( s );
348 maViewSignsBtn.Enable();
352 void MacroWarning::SetCertificate( const cssu::Reference< css::security::XCertificate >& _rxCert )
354 mxCert = _rxCert;
355 if( mxCert.is() )
357 rtl::OUString aCN_Id("CN");
358 String s;
359 s = GetContentPart( mxCert->getSubjectName(), aCN_Id );
360 maSignsFI.SetText( s );
361 maViewSignsBtn.Enable();
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */