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