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