1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
34 #include "secmacrowarnings.hxx"
35 #include "secmacrowarnings.hrc"
37 using namespace ::com::sun::star
;
40 // HACK!!! copied from xmlsecurity/source/dialog/resourcemanager.cxx
44 OUString
GetContentPart( const OUString
& _rRawString
, const OUString
& _rPartId
)
48 sal_Int32 nContStart
= _rRawString
.indexOf( _rPartId
);
49 if( nContStart
!= -1 )
51 nContStart
= nContStart
+ _rPartId
.getLength();
52 ++nContStart
; // now it's start of content, directly after Id
54 sal_Int32 nContEnd
= _rRawString
.indexOf( ',', nContStart
);
57 s
= _rRawString
.copy( nContStart
, nContEnd
- nContStart
);
59 s
= _rRawString
.copy( nContStart
);
67 MacroWarning::MacroWarning( Window
* _pParent
, bool _bWithSignatures
, ResMgr
& rResMgr
)
68 :ModalDialog ( _pParent
, ResId( RID_XMLSECDLG_MACROWARN
, rResMgr
) )
70 ,maSymbolImg ( this, ResId( IMG_SYMBOL
, rResMgr
) )
71 ,maDocNameFI ( this, ResId( FI_DOCNAME
, rResMgr
) )
72 ,maDescr1aFI ( this, ResId( FI_DESCR1A
, rResMgr
) )
73 ,maDescr1bFI ( this, ResId( FI_DESCR1B
, rResMgr
) )
74 ,maSignsFI ( this, ResId( FI_SIGNS
, rResMgr
) )
75 ,maViewSignsBtn ( this, ResId( PB_VIEWSIGNS
, rResMgr
) )
76 ,maDescr2FI ( this, ResId( FI_DESCR2
, rResMgr
) )
77 ,maAlwaysTrustCB ( this, ResId( CB_ALWAYSTRUST
, rResMgr
) )
78 ,maBottomSepFL ( this, ResId( FL_BOTTOM_SEP
, rResMgr
) )
79 ,maEnableBtn ( this, ResId( PB_ENABLE
, rResMgr
) )
80 ,maDisableBtn ( this, ResId( PB_DISABLE
, rResMgr
) )
81 ,maHelpBtn ( this, ResId( BTN_HELP
, rResMgr
) )
82 ,mbSignedMode ( true )
83 ,mbShowSignatures ( _bWithSignatures
)
90 maDisableBtn
.SetClickHdl( LINK( this, MacroWarning
, DisableBtnHdl
) );
91 maEnableBtn
.SetClickHdl( LINK( this, MacroWarning
, EnableBtnHdl
) );
92 maDisableBtn
.GrabFocus(); // Default button, but focus is on view button
95 MacroWarning::~MacroWarning()
99 short MacroWarning::Execute()
102 return ModalDialog::Execute();
105 void MacroWarning::SetDocumentURL( const OUString
& rDocURL
)
107 maDocNameFI
.SetText( rDocURL
);
110 IMPL_LINK_NOARG(MacroWarning
, ViewSignsBtnHdl
)
112 DBG_ASSERT( mxCert
.is(), "*MacroWarning::ViewSignsBtnHdl(): no certificate set!" );
114 uno::Reference
< security::XDocumentDigitalSignatures
> xD(
115 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion
));
119 xD
->showCertificate( mxCert
);
120 else if( mxStore
.is() )
121 xD
->showScriptingContentSignatures( mxStore
, uno::Reference
< io::XInputStream
>() );
127 IMPL_LINK_NOARG(MacroWarning
, EnableBtnHdl
)
129 if( mbSignedMode
&& maAlwaysTrustCB
.IsChecked() )
130 { // insert path into trusted path list
131 uno::Reference
< security::XDocumentDigitalSignatures
> xD(
132 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion
));
134 xD
->addAuthorToTrustedSources( mxCert
);
135 else if( mxStore
.is() )
137 DBG_ASSERT( mpInfos
, "-MacroWarning::EnableBtnHdl(): no infos, search in nirvana..." );
139 sal_Int32 nCnt
= mpInfos
->getLength();
140 for( sal_Int32 i
= 0 ; i
< nCnt
; ++i
)
141 xD
->addAuthorToTrustedSources( (*mpInfos
)[ i
].Signer
);
149 IMPL_LINK_NOARG(MacroWarning
, DisableBtnHdl
)
151 EndDialog( RET_CANCEL
);
155 IMPL_LINK_NOARG(MacroWarning
, AlwaysTrustCheckHdl
)
157 bool bEnable
= ( mnActSecLevel
< 2 || maAlwaysTrustCB
.IsChecked() );
158 maEnableBtn
.Enable( bEnable
);
159 maDisableBtn
.Enable( !maAlwaysTrustCB
.IsChecked() );
164 void MacroWarning::InitControls()
167 Image
aImg( WarningBox::GetStandardImage() );
168 maSymbolImg
.SetImage( aImg
);
169 maSymbolImg
.SetSizePixel( aImg
.GetSizePixel() );
170 // set bold font and path ellipsis for docname fixedtext
171 Font aTmpFont
= maDocNameFI
.GetControlFont();
172 aTmpFont
.SetWeight( WEIGHT_BOLD
);
173 maDocNameFI
.SetControlFont( aTmpFont
);
174 WinBits nStyle
= maDocNameFI
.GetStyle();
175 nStyle
|= WB_PATHELLIPSIS
;
176 maDocNameFI
.SetStyle( nStyle
);
177 // show signature controls?
178 if( mbShowSignatures
)
181 maViewSignsBtn
.SetClickHdl( LINK( this, MacroWarning
, ViewSignsBtnHdl
) );
182 maViewSignsBtn
.Disable(); // default
183 maAlwaysTrustCB
.SetClickHdl( LINK( this, MacroWarning
, AlwaysTrustCheckHdl
) );
185 mnActSecLevel
= SvtSecurityOptions().GetMacroSecurityLevel();
186 if ( mnActSecLevel
>= 2 )
187 maEnableBtn
.Disable();
193 maViewSignsBtn
.Hide();
194 maAlwaysTrustCB
.Hide();
196 // move hint up to position of signer list
197 maDescr2FI
.SetPosPixel( maSignsFI
.GetPosPixel() );
199 // without signature controls could be smaller
200 if ( !mbShowSignatures
)
202 Point aPos
= maDescr2FI
.GetPosPixel();
203 aPos
.Y() += maDescr2FI
.GetSizePixel().Height();
204 aPos
.Y() += LogicToPixel( Size( 3, 3 ) ).Height();
205 long nDelta
= maBottomSepFL
.GetPosPixel().Y() - aPos
.Y();
208 &maBottomSepFL
, &maEnableBtn
, &maDisableBtn
, &maHelpBtn
210 Window
** pCurrent
= pWins
;
211 for ( sal_uInt32 i
= 0; i
< sizeof( pWins
) / sizeof( pWins
[ 0 ] ); ++i
, ++pCurrent
)
213 Point aNewPos
= (*pCurrent
)->GetPosPixel();
214 aNewPos
.Y() -= nDelta
;
215 (*pCurrent
)->SetPosPixel( aNewPos
);
218 Size aDlgSz
= GetSizePixel();
219 aDlgSz
.Height() -= nDelta
;
220 SetSizePixel( aDlgSz
);
223 // check if some buttontexts are to wide
224 OUString sText
= maViewSignsBtn
.GetText();
225 long nTxtW
= maViewSignsBtn
.GetTextWidth( sText
);
226 const long nOffset
= 12;
227 if ( sText
.indexOf( '~' ) == -1 )
229 long nBtnW
= maViewSignsBtn
.GetSizePixel().Width();
230 if ( nTxtW
>= nBtnW
)
232 // broaden the button
233 long nDelta
= std::max( nTxtW
- nBtnW
, nOffset
/3 );
234 Size aNewSize
= maViewSignsBtn
.GetSizePixel();
235 aNewSize
.Width() += nDelta
;
236 maViewSignsBtn
.SetSizePixel( aNewSize
);
237 // and give it a new position
238 Point aNewPos
= maViewSignsBtn
.GetPosPixel();
239 aNewPos
.X() -= nDelta
;
240 maViewSignsBtn
.SetPosPixel( aNewPos
);
241 // the left fixedtext must be smaller
242 aNewSize
= maSignsFI
.GetSizePixel();
243 aNewSize
.Width() -= nDelta
;
244 maSignsFI
.SetSizePixel( aNewSize
);
246 // if the button text (we compare with the longest of both) is too wide, then broaden the buttons
247 OUString sText1
= maEnableBtn
.GetText();
248 long nTxtW1
= maEnableBtn
.GetTextWidth( sText1
);
249 if ( sText1
.indexOf( '~' ) == -1 )
251 OUString sText2
= maDisableBtn
.GetText();
252 long nTxtW2
= maDisableBtn
.GetTextWidth( sText2
);
253 if ( sText2
.indexOf( '~' ) == -1 )
255 nTxtW
= std::max( nTxtW1
, nTxtW2
);
256 nBtnW
= maEnableBtn
.GetSizePixel().Width();
259 // broaden both buttons
260 long nDelta
= nTxtW
- nBtnW
;
261 Size aNewSize
= maEnableBtn
.GetSizePixel();
262 aNewSize
.Width() += nDelta
;
263 maEnableBtn
.SetSizePixel( aNewSize
);
264 maDisableBtn
.SetSizePixel( aNewSize
);
265 // and give them a new position
266 Point aNewPos
= maEnableBtn
.GetPosPixel();
267 aNewPos
.X() -= (2*nDelta
);
268 maEnableBtn
.SetPosPixel( aNewPos
);
269 aNewPos
= maDisableBtn
.GetPosPixel();
270 aNewPos
.X() -= nDelta
;
271 maDisableBtn
.SetPosPixel( aNewPos
);
275 void MacroWarning::FitControls()
277 Size a3Size
= LogicToPixel( Size( 3, 3 ), MAP_APPFONT
);
278 Size aNewSize
, aMinSize
;
283 if ( mbShowSignatures
)
285 aMinSize
= maSignsFI
.CalcMinimumSize( maSignsFI
.GetSizePixel().Width() );
286 nTxtH
= std::max( aMinSize
.Height(), maViewSignsBtn
.GetSizePixel().Height() );
287 nTxtH
+= a3Size
.Height() / 2;
288 nCtrlH
= maSignsFI
.GetSizePixel().Height();
289 nDelta
= std::max( nCtrlH
- nTxtH
, static_cast< long >( -100 ) ); // not too large
290 aNewSize
= maSignsFI
.GetSizePixel();
291 aNewSize
.Height() -= nDelta
;
292 maSignsFI
.SetSizePixel( aNewSize
);
295 aMinSize
= maDescr2FI
.CalcMinimumSize( maDescr2FI
.GetSizePixel().Width() );
296 nTxtH
= aMinSize
.Height();
297 nCtrlH
= maDescr2FI
.GetSizePixel().Height();
298 long nDelta2
= ( nCtrlH
- nTxtH
);
299 aNewSize
= maDescr2FI
.GetSizePixel();
300 aNewSize
.Height() -= nDelta2
;
301 maDescr2FI
.SetSizePixel( aNewSize
);
303 // new position for the succeeding windows
306 &maDescr2FI
, &maAlwaysTrustCB
, &maBottomSepFL
, &maEnableBtn
, &maDisableBtn
, &maHelpBtn
308 Window
** pCurrent
= pWins
;
309 for ( sal_uInt32 i
= 0; i
< sizeof( pWins
) / sizeof( pWins
[ 0 ] ); ++i
, ++pCurrent
)
311 Point aNewPos
= (*pCurrent
)->GetPosPixel();
312 aNewPos
.Y() -= nDelta
;
313 (*pCurrent
)->SetPosPixel( aNewPos
);
315 if ( *pCurrent
== &maDescr2FI
)
319 // new size of the dialog
320 aNewSize
= GetSizePixel();
321 aNewSize
.Height() -= nDelta
;
322 SetSizePixel( aNewSize
);
325 void MacroWarning::SetStorage( const css::uno::Reference
< css::embed::XStorage
>& rxStore
,
326 const OUString
& aODFVersion
,
327 const css::uno::Sequence
< security::DocumentSignatureInformation
>& rInfos
)
330 maODFVersion
= aODFVersion
;
331 sal_Int32 nCnt
= rInfos
.getLength();
332 if( mxStore
.is() && nCnt
> 0 )
335 OUString
aCN_Id("CN");
337 s
= GetContentPart( rInfos
[ 0 ].Signer
->getSubjectName(), aCN_Id
);
339 for( sal_Int32 i
= 1 ; i
< nCnt
; ++i
)
342 s
+= GetContentPart( rInfos
[ i
].Signer
->getSubjectName(), aCN_Id
);
345 maSignsFI
.SetText( s
);
346 maViewSignsBtn
.Enable();
350 void MacroWarning::SetCertificate( const css::uno::Reference
< css::security::XCertificate
>& _rxCert
)
355 OUString
aCN_Id("CN");
357 s
= GetContentPart( mxCert
->getSubjectName(), aCN_Id
);
358 maSignsFI
.SetText( s
);
359 maViewSignsBtn
.Enable();
363 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */