bump product version to 5.0.4.1
[LibreOffice.git] / uui / source / secmacrowarnings.cxx
blob8af1a4ea3ff9b054494e73268af18e01abf00900
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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;
33 #include "ids.hrc"
34 #include "secmacrowarnings.hxx"
36 using namespace ::com::sun::star;
39 // HACK!!! copied from xmlsecurity/source/dialog/resourcemanager.cxx
41 namespace
43 OUString GetContentPart( const OUString& _rRawString, const OUString& _rPartId )
45 OUString s;
47 sal_Int32 nContStart = _rRawString.indexOf( _rPartId );
48 if( nContStart != -1 )
50 nContStart = nContStart + _rPartId.getLength();
51 ++nContStart; // now its start of content, directly after Id
53 sal_Int32 nContEnd = _rRawString.indexOf( ',', nContStart );
55 if ( nContEnd != -1 )
56 s = _rRawString.copy( nContStart, nContEnd - nContStart );
57 else
58 s = _rRawString.copy( nContStart );
61 return s;
66 MacroWarning::MacroWarning( vcl::Window* _pParent, bool _bWithSignatures, ResMgr& )
67 :ModalDialog ( _pParent, "MacroWarnMedium", "uui/ui/macrowarnmedium.ui" )
68 ,mpInfos ( NULL )
69 ,mbSignedMode ( true )
70 ,mbShowSignatures ( _bWithSignatures )
71 ,mnActSecLevel ( 0 )
73 get(mpSymbolImg, "symbolImage");
74 get(mpDocNameFI, "docNameLabel");
75 get(mpDescr1FI, "descr1Label");
76 get(mpDescr1aFI, "descr1aLabel");
77 get(mpSignsFI, "signsLabel");
78 get(mpViewSignsBtn, "viewSignsButton");
79 get(mpDescr2FI, "descr2Label");
80 get(mpAlwaysTrustCB, "alwaysTrustCheckbutton");
81 get(mpEnableBtn, "ok");
82 get(mpDisableBtn, "cancel");
84 InitControls();
86 mpDisableBtn->SetClickHdl( LINK( this, MacroWarning, DisableBtnHdl ) );
87 mpEnableBtn->SetClickHdl( LINK( this, MacroWarning, EnableBtnHdl ) );
88 mpDisableBtn->GrabFocus(); // Default button, but focus is on view button
91 MacroWarning::~MacroWarning()
93 disposeOnce();
96 void MacroWarning::dispose()
98 mpSymbolImg.clear();
99 mpDocNameFI.clear();
100 mpDescr1aFI.clear();
101 mpDescr1FI.clear();
102 mpSignsFI.clear();
103 mpViewSignsBtn.clear();
104 mpDescr2FI.clear();
105 mpAlwaysTrustCB.clear();
106 mpEnableBtn.clear();
107 mpDisableBtn.clear();
108 ModalDialog::dispose();
111 void MacroWarning::SetDocumentURL( const OUString& rDocURL )
113 mpDocNameFI->SetText( rDocURL );
116 IMPL_LINK_NOARG(MacroWarning, ViewSignsBtnHdl)
118 DBG_ASSERT( mxCert.is(), "*MacroWarning::ViewSignsBtnHdl(): no certificate set!" );
120 uno::Reference< security::XDocumentDigitalSignatures > xD(
121 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion));
122 if( xD.is() )
124 if( mxCert.is() )
125 xD->showCertificate( mxCert );
126 else if( mxStore.is() )
127 xD->showScriptingContentSignatures( mxStore, uno::Reference< io::XInputStream >() );
130 return 0;
133 IMPL_LINK_NOARG(MacroWarning, EnableBtnHdl)
135 if( mbSignedMode && mpAlwaysTrustCB->IsChecked() )
136 { // insert path into trusted path list
137 uno::Reference< security::XDocumentDigitalSignatures > xD(
138 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion));
139 if( mxCert.is() )
140 xD->addAuthorToTrustedSources( mxCert );
141 else if( mxStore.is() )
143 DBG_ASSERT( mpInfos, "-MacroWarning::EnableBtnHdl(): no infos, search in nirvana..." );
145 sal_Int32 nCnt = mpInfos->getLength();
146 for( sal_Int32 i = 0 ; i < nCnt ; ++i )
147 xD->addAuthorToTrustedSources( (*mpInfos)[ i ].Signer );
151 EndDialog( RET_OK );
152 return 0;
155 IMPL_LINK_NOARG(MacroWarning, DisableBtnHdl)
157 EndDialog( RET_CANCEL );
158 return 0;
161 IMPL_LINK_NOARG(MacroWarning, AlwaysTrustCheckHdl)
163 bool bEnable = ( mnActSecLevel < 2 || mpAlwaysTrustCB->IsChecked() );
164 mpEnableBtn->Enable( bEnable );
165 mpDisableBtn->Enable( !mpAlwaysTrustCB->IsChecked() );
167 return 0;
170 void MacroWarning::InitControls()
172 // set warning image
173 Image aImg( WarningBox::GetStandardImage() );
174 mpSymbolImg->SetImage( aImg );
175 mpSymbolImg->SetSizePixel( aImg.GetSizePixel() );
176 // set bold font and path ellipsis for docname fixedtext
177 vcl::Font aTmpFont = mpDocNameFI->GetControlFont();
178 aTmpFont.SetWeight( WEIGHT_BOLD );
179 mpDocNameFI->SetControlFont( aTmpFont );
180 WinBits nStyle = mpDocNameFI->GetStyle();
181 nStyle |= WB_PATHELLIPSIS;
182 mpDocNameFI->SetStyle( nStyle );
183 // show signature controls?
184 if( mbShowSignatures )
186 mpViewSignsBtn->SetClickHdl( LINK( this, MacroWarning, ViewSignsBtnHdl ) );
187 mpViewSignsBtn->Disable(); // default
188 mpAlwaysTrustCB->SetClickHdl( LINK( this, MacroWarning, AlwaysTrustCheckHdl ) );
190 mnActSecLevel = SvtSecurityOptions().GetMacroSecurityLevel();
191 if ( mnActSecLevel >= 2 )
192 mpEnableBtn->Disable();
194 else
196 mpDescr1FI->Hide();
197 mpDescr1aFI->Show();
198 mpSignsFI->Hide();
199 mpViewSignsBtn->Hide();
200 mpAlwaysTrustCB->Hide();
202 // move hint up to position of signer list
203 mpDescr2FI->SetPosPixel( mpSignsFI->GetPosPixel() );
207 void MacroWarning::SetStorage( const css::uno::Reference < css::embed::XStorage >& rxStore,
208 const OUString& aODFVersion,
209 const css::uno::Sequence< security::DocumentSignatureInformation >& rInfos )
211 mxStore = rxStore;
212 maODFVersion = aODFVersion;
213 sal_Int32 nCnt = rInfos.getLength();
214 if( mxStore.is() && nCnt > 0 )
216 mpInfos = &rInfos;
217 OUString aCN_Id("CN");
218 OUString s;
219 s = GetContentPart( rInfos[ 0 ].Signer->getSubjectName(), aCN_Id );
221 for( sal_Int32 i = 1 ; i < nCnt ; ++i )
223 s += "\n";
224 s += GetContentPart( rInfos[ i ].Signer->getSubjectName(), aCN_Id );
227 mpSignsFI->SetText( s );
228 mpViewSignsBtn->Enable();
232 void MacroWarning::SetCertificate( const css::uno::Reference< css::security::XCertificate >& _rxCert )
234 mxCert = _rxCert;
235 if( mxCert.is() )
237 OUString aCN_Id("CN");
238 OUString s;
239 s = GetContentPart( mxCert->getSubjectName(), aCN_Id );
240 mpSignsFI->SetText( s );
241 mpViewSignsBtn->Enable();
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */