1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: secmacrowarnings.cxx,v $
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
;
43 #include "secmacrowarnings.hxx"
44 #include "secmacrowarnings.hrc"
47 #pragma warning (disable : 4355) // 4355: this used in initializer-list
50 using namespace ::com::sun::star
;
51 using namespace ::com::sun::star
;
54 // HACK!!! copied from xmlsecurity/source/dialog/resourcemanager.cxx
58 String
GetContentPart( const String
& _rRawString
, const String
& _rPartId
)
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
);
78 MacroWarning::MacroWarning( Window
* _pParent
, bool _bWithSignatures
, ResMgr
& rResMgr
)
79 :ModalDialog ( _pParent
, ResId( RID_XMLSECDLG_MACROWARN
, rResMgr
) )
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
)
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()
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
);
130 xD
->showCertificate( mxCert
);
131 else if( mxStore
.is() )
132 xD
->showScriptingContentSignatures( mxStore
, uno::Reference
< io::XInputStream
>() );
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
);
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
);
163 IMPL_LINK( MacroWarning
, DisableBtnHdl
, void*, EMPTYARG
)
165 EndDialog( RET_CANCEL
);
169 IMPL_LINK( MacroWarning
, AlwaysTrustCheckHdl
, void*, EMPTYARG
)
171 bool bEnable
= ( mnActSecLevel
< 2 || maAlwaysTrustCB
.IsChecked() );
172 maEnableBtn
.Enable( bEnable
);
173 maDisableBtn
.Enable( !maAlwaysTrustCB
.IsChecked() );
178 void MacroWarning::InitControls()
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
)
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();
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();
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
)
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
)
265 String sText2
= maDisableBtn
.GetText();
266 long nTxtW2
= maDisableBtn
.GetTextWidth( sText2
);
267 if ( sText2
.Search( '~' ) == STRING_NOTFOUND
)
269 nTxtW
= Max( nTxtW1
, nTxtW2
);
270 nBtnW
= maEnableBtn
.GetSizePixel().Width();
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
;
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
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
)
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
)
343 sal_Int32 nCnt
= _rInfos
.getLength();
344 if( mxStore
.is() && nCnt
> 0 )
347 String
aCN_Id( String::CreateFromAscii( "CN" ) );
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
)
367 String
aCN_Id( String::CreateFromAscii( "CN" ) );
369 s
= GetContentPart( mxCert
->getSubjectName(), aCN_Id
);
370 maSignsFI
.SetText( s
);
371 maViewSignsBtn
.Enable();