Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
27 :
28 : #include <vcl/msgbox.hxx>
29 : #include <com/sun/star/security/NoPasswordException.hpp>
30 :
31 : using namespace ::com::sun::star::security;
32 :
33 : #include "ids.hrc"
34 : #include "secmacrowarnings.hxx"
35 : #include "secmacrowarnings.hrc"
36 :
37 : #ifdef _MSC_VER
38 : #pragma warning (disable : 4355) // 4355: this used in initializer-list
39 : #endif
40 :
41 : using namespace ::com::sun::star;
42 : using namespace ::com::sun::star;
43 :
44 :
45 : // HACK!!! copied from xmlsecurity/source/dialog/resourcemanager.cxx
46 :
47 : namespace
48 : {
49 0 : String GetContentPart( const String& _rRawString, const String& _rPartId )
50 : {
51 0 : String s;
52 :
53 0 : xub_StrLen nContStart = _rRawString.Search( _rPartId );
54 0 : if( nContStart != STRING_NOTFOUND )
55 : {
56 0 : nContStart = nContStart + _rPartId.Len();
57 0 : ++nContStart; // now it's start of content, directly after Id
58 :
59 0 : xub_StrLen nContEnd = _rRawString.Search( sal_Unicode( ',' ), nContStart );
60 :
61 0 : s = String( _rRawString, nContStart, nContEnd - nContStart );
62 : }
63 :
64 0 : return s;
65 : }
66 : }
67 :
68 :
69 0 : 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 0 : ,mnActSecLevel ( 0 )
87 : {
88 0 : FreeResource();
89 :
90 0 : InitControls();
91 :
92 0 : maDisableBtn.SetClickHdl( LINK( this, MacroWarning, DisableBtnHdl ) );
93 0 : maEnableBtn.SetClickHdl( LINK( this, MacroWarning, EnableBtnHdl ) );
94 0 : maDisableBtn.GrabFocus(); // Default button, but focus is on view button
95 0 : }
96 :
97 0 : MacroWarning::~MacroWarning()
98 : {
99 0 : }
100 :
101 0 : short MacroWarning::Execute()
102 : {
103 0 : FitControls();
104 0 : return ModalDialog::Execute();
105 : }
106 :
107 0 : void MacroWarning::SetDocumentURL( const String& rDocURL )
108 : {
109 0 : maDocNameFI.SetText( rDocURL );
110 0 : }
111 :
112 0 : IMPL_LINK_NOARG(MacroWarning, ViewSignsBtnHdl)
113 : {
114 : DBG_ASSERT( mxCert.is(), "*MacroWarning::ViewSignsBtnHdl(): no certificate set!" );
115 :
116 : uno::Reference< security::XDocumentDigitalSignatures > xD(
117 0 : security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion));
118 0 : if( xD.is() )
119 : {
120 0 : if( mxCert.is() )
121 0 : xD->showCertificate( mxCert );
122 0 : else if( mxStore.is() )
123 0 : xD->showScriptingContentSignatures( mxStore, uno::Reference< io::XInputStream >() );
124 : }
125 :
126 0 : return 0;
127 : }
128 :
129 0 : IMPL_LINK_NOARG(MacroWarning, EnableBtnHdl)
130 : {
131 0 : if( mbSignedMode && maAlwaysTrustCB.IsChecked() )
132 : { // insert path into trusted path list
133 : uno::Reference< security::XDocumentDigitalSignatures > xD(
134 0 : security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion));
135 0 : if( mxCert.is() )
136 0 : xD->addAuthorToTrustedSources( mxCert );
137 0 : else if( mxStore.is() )
138 : {
139 : DBG_ASSERT( mpInfos, "-MacroWarning::EnableBtnHdl(): no infos, search in nirvana..." );
140 :
141 0 : sal_Int32 nCnt = mpInfos->getLength();
142 0 : for( sal_Int32 i = 0 ; i < nCnt ; ++i )
143 0 : xD->addAuthorToTrustedSources( (*mpInfos)[ i ].Signer );
144 0 : }
145 : }
146 :
147 0 : EndDialog( RET_OK );
148 0 : return 0;
149 : }
150 :
151 0 : IMPL_LINK_NOARG(MacroWarning, DisableBtnHdl)
152 : {
153 0 : EndDialog( RET_CANCEL );
154 0 : return 0;
155 : }
156 :
157 0 : IMPL_LINK_NOARG(MacroWarning, AlwaysTrustCheckHdl)
158 : {
159 0 : bool bEnable = ( mnActSecLevel < 2 || maAlwaysTrustCB.IsChecked() );
160 0 : maEnableBtn.Enable( bEnable );
161 0 : maDisableBtn.Enable( !maAlwaysTrustCB.IsChecked() );
162 :
163 0 : return 0;
164 : }
165 :
166 0 : void MacroWarning::InitControls()
167 : {
168 : // set warning image
169 0 : Image aImg( WarningBox::GetStandardImage() );
170 0 : maSymbolImg.SetImage( aImg );
171 0 : maSymbolImg.SetSizePixel( aImg.GetSizePixel() );
172 : // set bold font and path ellipsis for docname fixedtext
173 0 : Font aTmpFont = maDocNameFI.GetControlFont();
174 0 : aTmpFont.SetWeight( WEIGHT_BOLD );
175 0 : maDocNameFI.SetControlFont( aTmpFont );
176 0 : WinBits nStyle = maDocNameFI.GetStyle();
177 0 : nStyle |= WB_PATHELLIPSIS;
178 0 : maDocNameFI.SetStyle( nStyle );
179 : // show signature controls?
180 0 : if( mbShowSignatures )
181 : {
182 0 : maDescr1bFI.Hide();
183 0 : maViewSignsBtn.SetClickHdl( LINK( this, MacroWarning, ViewSignsBtnHdl ) );
184 0 : maViewSignsBtn.Disable(); // default
185 0 : maAlwaysTrustCB.SetClickHdl( LINK( this, MacroWarning, AlwaysTrustCheckHdl ) );
186 :
187 0 : mnActSecLevel = SvtSecurityOptions().GetMacroSecurityLevel();
188 0 : if ( mnActSecLevel >= 2 )
189 0 : maEnableBtn.Disable();
190 : }
191 : else
192 : {
193 0 : maDescr1aFI.Hide();
194 0 : maSignsFI.Hide();
195 0 : maViewSignsBtn.Hide();
196 0 : maAlwaysTrustCB.Hide();
197 :
198 : // move hint up to position of signer list
199 0 : maDescr2FI.SetPosPixel( maSignsFI.GetPosPixel() );
200 : }
201 : // without signature controls could be smaller
202 0 : if ( !mbShowSignatures )
203 : {
204 0 : Point aPos = maDescr2FI.GetPosPixel();
205 0 : aPos.Y() += maDescr2FI.GetSizePixel().Height();
206 0 : aPos.Y() += LogicToPixel( Size( 3, 3 ) ).Height();
207 0 : long nDelta = maBottomSepFL.GetPosPixel().Y() - aPos.Y();
208 : Window* pWins[] =
209 : {
210 : &maBottomSepFL, &maEnableBtn, &maDisableBtn, &maHelpBtn
211 0 : };
212 0 : Window** pCurrent = pWins;
213 0 : for ( sal_uInt32 i = 0; i < sizeof( pWins ) / sizeof( pWins[ 0 ] ); ++i, ++pCurrent )
214 : {
215 0 : Point aNewPos = (*pCurrent)->GetPosPixel();
216 0 : aNewPos.Y() -= nDelta;
217 0 : (*pCurrent)->SetPosPixel( aNewPos );
218 : }
219 :
220 0 : Size aDlgSz = GetSizePixel();
221 0 : aDlgSz.Height() -= nDelta;
222 0 : SetSizePixel( aDlgSz );
223 : }
224 :
225 : // check if some buttontexts are to wide
226 0 : String sText = maViewSignsBtn.GetText();
227 0 : long nTxtW = maViewSignsBtn.GetTextWidth( sText );
228 0 : const long nOffset = 12;
229 0 : if ( sText.Search( '~' ) == STRING_NOTFOUND )
230 0 : nTxtW += nOffset;
231 0 : long nBtnW = maViewSignsBtn.GetSizePixel().Width();
232 0 : if ( nTxtW >= nBtnW )
233 : {
234 : // broaden the button
235 0 : long nDelta = Max( nTxtW - nBtnW, nOffset/3 );
236 0 : Size aNewSize = maViewSignsBtn.GetSizePixel();
237 0 : aNewSize.Width() += nDelta;
238 0 : maViewSignsBtn.SetSizePixel( aNewSize );
239 : // and give it a new position
240 0 : Point aNewPos = maViewSignsBtn.GetPosPixel();
241 0 : aNewPos.X() -= nDelta;
242 0 : maViewSignsBtn.SetPosPixel( aNewPos );
243 : // the the left fixedtext must be smaller
244 0 : aNewSize = maSignsFI.GetSizePixel();
245 0 : aNewSize.Width() -= nDelta;
246 0 : maSignsFI.SetSizePixel( aNewSize );
247 : }
248 : // if the button text (we compare with the longest of both) is too wide, then broaden the buttons
249 0 : String sText1 = maEnableBtn.GetText();
250 0 : long nTxtW1 = maEnableBtn.GetTextWidth( sText1 );
251 0 : if ( sText1.Search( '~' ) == STRING_NOTFOUND )
252 0 : nTxtW1 += nOffset;
253 0 : String sText2 = maDisableBtn.GetText();
254 0 : long nTxtW2 = maDisableBtn.GetTextWidth( sText2 );
255 0 : if ( sText2.Search( '~' ) == STRING_NOTFOUND )
256 0 : nTxtW2 += nOffset;
257 0 : nTxtW = Max( nTxtW1, nTxtW2 );
258 0 : nBtnW = maEnableBtn.GetSizePixel().Width();
259 0 : if ( nTxtW > nBtnW )
260 : {
261 : // broaden both buttons
262 0 : long nDelta = nTxtW - nBtnW;
263 0 : Size aNewSize = maEnableBtn.GetSizePixel();
264 0 : aNewSize.Width() += nDelta;
265 0 : maEnableBtn.SetSizePixel( aNewSize );
266 0 : maDisableBtn.SetSizePixel( aNewSize );
267 : // and give them a new position
268 0 : Point aNewPos = maEnableBtn.GetPosPixel();
269 0 : aNewPos.X() -= (2*nDelta);
270 0 : maEnableBtn.SetPosPixel( aNewPos );
271 0 : aNewPos = maDisableBtn.GetPosPixel();
272 0 : aNewPos.X() -= nDelta;
273 0 : maDisableBtn.SetPosPixel( aNewPos );
274 0 : }
275 0 : }
276 :
277 0 : void MacroWarning::FitControls()
278 : {
279 0 : Size a3Size = LogicToPixel( Size( 3, 3 ), MAP_APPFONT );
280 0 : Size aNewSize, aMinSize;
281 0 : long nTxtH = 0;
282 0 : long nCtrlH = 0;
283 0 : long nDelta = 0;
284 :
285 0 : if ( mbShowSignatures )
286 : {
287 0 : aMinSize = maSignsFI.CalcMinimumSize( maSignsFI.GetSizePixel().Width() );
288 0 : nTxtH = Max( aMinSize.Height(), maViewSignsBtn.GetSizePixel().Height() );
289 0 : nTxtH += a3Size.Height() / 2;
290 0 : nCtrlH = maSignsFI.GetSizePixel().Height();
291 0 : nDelta = Max( nCtrlH - nTxtH, static_cast< long >( -100 ) ); // not too large
292 0 : aNewSize = maSignsFI.GetSizePixel();
293 0 : aNewSize.Height() -= nDelta;
294 0 : maSignsFI.SetSizePixel( aNewSize );
295 : }
296 :
297 0 : aMinSize = maDescr2FI.CalcMinimumSize( maDescr2FI.GetSizePixel().Width() );
298 0 : nTxtH = aMinSize.Height();
299 0 : nCtrlH = maDescr2FI.GetSizePixel().Height();
300 0 : long nDelta2 = ( nCtrlH - nTxtH );
301 0 : aNewSize = maDescr2FI.GetSizePixel();
302 0 : aNewSize.Height() -= nDelta2;
303 0 : maDescr2FI.SetSizePixel( aNewSize );
304 :
305 : // new position for the succeeding windows
306 : Window* pWins[] =
307 : {
308 : &maDescr2FI, &maAlwaysTrustCB, &maBottomSepFL, &maEnableBtn, &maDisableBtn, &maHelpBtn
309 0 : };
310 0 : Window** pCurrent = pWins;
311 0 : for ( sal_uInt32 i = 0; i < sizeof( pWins ) / sizeof( pWins[ 0 ] ); ++i, ++pCurrent )
312 : {
313 0 : Point aNewPos = (*pCurrent)->GetPosPixel();
314 0 : aNewPos.Y() -= nDelta;
315 0 : (*pCurrent)->SetPosPixel( aNewPos );
316 :
317 0 : if ( *pCurrent == &maDescr2FI )
318 0 : nDelta += nDelta2;
319 : }
320 :
321 : // new size of the dialog
322 0 : aNewSize = GetSizePixel();
323 0 : aNewSize.Height() -= nDelta;
324 0 : SetSizePixel( aNewSize );
325 0 : }
326 :
327 0 : void MacroWarning::SetStorage( const cssu::Reference < css::embed::XStorage >& rxStore,
328 : const ::rtl::OUString& aODFVersion,
329 : const cssu::Sequence< security::DocumentSignatureInformation >& rInfos )
330 : {
331 0 : mxStore = rxStore;
332 0 : maODFVersion = aODFVersion;
333 0 : sal_Int32 nCnt = rInfos.getLength();
334 0 : if( mxStore.is() && nCnt > 0 )
335 : {
336 0 : mpInfos = &rInfos;
337 0 : rtl::OUString aCN_Id("CN");
338 0 : String s;
339 0 : s = GetContentPart( rInfos[ 0 ].Signer->getSubjectName(), aCN_Id );
340 :
341 0 : for( sal_Int32 i = 1 ; i < nCnt ; ++i )
342 : {
343 0 : s.AppendAscii( "\n" );
344 0 : s += GetContentPart( rInfos[ i ].Signer->getSubjectName(), aCN_Id );
345 : }
346 :
347 0 : maSignsFI.SetText( s );
348 0 : maViewSignsBtn.Enable();
349 : }
350 0 : }
351 :
352 0 : void MacroWarning::SetCertificate( const cssu::Reference< css::security::XCertificate >& _rxCert )
353 : {
354 0 : mxCert = _rxCert;
355 0 : if( mxCert.is() )
356 : {
357 0 : rtl::OUString aCN_Id("CN");
358 0 : String s;
359 0 : s = GetContentPart( mxCert->getSubjectName(), aCN_Id );
360 0 : maSignsFI.SetText( s );
361 0 : maViewSignsBtn.Enable();
362 : }
363 0 : }
364 :
365 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|