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 :
36 : using namespace ::com::sun::star;
37 :
38 :
39 : // HACK!!! copied from xmlsecurity/source/dialog/resourcemanager.cxx
40 :
41 : namespace
42 : {
43 0 : OUString GetContentPart( const OUString& _rRawString, const OUString& _rPartId )
44 : {
45 0 : OUString s;
46 :
47 0 : sal_Int32 nContStart = _rRawString.indexOf( _rPartId );
48 0 : if( nContStart != -1 )
49 : {
50 0 : nContStart = nContStart + _rPartId.getLength();
51 0 : ++nContStart; // now its start of content, directly after Id
52 :
53 0 : sal_Int32 nContEnd = _rRawString.indexOf( ',', nContStart );
54 :
55 0 : if ( nContEnd != -1 )
56 0 : s = _rRawString.copy( nContStart, nContEnd - nContStart );
57 : else
58 0 : s = _rRawString.copy( nContStart );
59 : }
60 :
61 0 : return s;
62 : }
63 : }
64 :
65 :
66 0 : 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 0 : ,mnActSecLevel ( 0 )
72 : {
73 0 : get(mpSymbolImg, "symbolImage");
74 0 : get(mpDocNameFI, "docNameLabel");
75 0 : get(mpDescr1FI, "descr1Label");
76 0 : get(mpSignsFI, "signsLabel");
77 0 : get(mpViewSignsBtn, "viewSignsButton");
78 0 : get(mpDescr2FI, "descr2Label");
79 0 : get(mpAlwaysTrustCB, "alwaysTrustCheckbutton");
80 0 : get(mpEnableBtn, "ok");
81 0 : get(mpDisableBtn, "cancel");
82 :
83 0 : InitControls();
84 :
85 0 : mpDisableBtn->SetClickHdl( LINK( this, MacroWarning, DisableBtnHdl ) );
86 0 : mpEnableBtn->SetClickHdl( LINK( this, MacroWarning, EnableBtnHdl ) );
87 0 : mpDisableBtn->GrabFocus(); // Default button, but focus is on view button
88 0 : }
89 :
90 0 : MacroWarning::~MacroWarning()
91 : {
92 0 : }
93 :
94 0 : void MacroWarning::SetDocumentURL( const OUString& rDocURL )
95 : {
96 0 : mpDocNameFI->SetText( rDocURL );
97 0 : }
98 :
99 0 : IMPL_LINK_NOARG(MacroWarning, ViewSignsBtnHdl)
100 : {
101 : DBG_ASSERT( mxCert.is(), "*MacroWarning::ViewSignsBtnHdl(): no certificate set!" );
102 :
103 : uno::Reference< security::XDocumentDigitalSignatures > xD(
104 0 : security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion));
105 0 : if( xD.is() )
106 : {
107 0 : if( mxCert.is() )
108 0 : xD->showCertificate( mxCert );
109 0 : else if( mxStore.is() )
110 0 : xD->showScriptingContentSignatures( mxStore, uno::Reference< io::XInputStream >() );
111 : }
112 :
113 0 : return 0;
114 : }
115 :
116 0 : IMPL_LINK_NOARG(MacroWarning, EnableBtnHdl)
117 : {
118 0 : if( mbSignedMode && mpAlwaysTrustCB->IsChecked() )
119 : { // insert path into trusted path list
120 : uno::Reference< security::XDocumentDigitalSignatures > xD(
121 0 : security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion));
122 0 : if( mxCert.is() )
123 0 : xD->addAuthorToTrustedSources( mxCert );
124 0 : else if( mxStore.is() )
125 : {
126 : DBG_ASSERT( mpInfos, "-MacroWarning::EnableBtnHdl(): no infos, search in nirvana..." );
127 :
128 0 : sal_Int32 nCnt = mpInfos->getLength();
129 0 : for( sal_Int32 i = 0 ; i < nCnt ; ++i )
130 0 : xD->addAuthorToTrustedSources( (*mpInfos)[ i ].Signer );
131 0 : }
132 : }
133 :
134 0 : EndDialog( RET_OK );
135 0 : return 0;
136 : }
137 :
138 0 : IMPL_LINK_NOARG(MacroWarning, DisableBtnHdl)
139 : {
140 0 : EndDialog( RET_CANCEL );
141 0 : return 0;
142 : }
143 :
144 0 : IMPL_LINK_NOARG(MacroWarning, AlwaysTrustCheckHdl)
145 : {
146 0 : bool bEnable = ( mnActSecLevel < 2 || mpAlwaysTrustCB->IsChecked() );
147 0 : mpEnableBtn->Enable( bEnable );
148 0 : mpDisableBtn->Enable( !mpAlwaysTrustCB->IsChecked() );
149 :
150 0 : return 0;
151 : }
152 :
153 0 : void MacroWarning::InitControls()
154 : {
155 : // set warning image
156 0 : Image aImg( WarningBox::GetStandardImage() );
157 0 : mpSymbolImg->SetImage( aImg );
158 0 : mpSymbolImg->SetSizePixel( aImg.GetSizePixel() );
159 : // set bold font and path ellipsis for docname fixedtext
160 0 : vcl::Font aTmpFont = mpDocNameFI->GetControlFont();
161 0 : aTmpFont.SetWeight( WEIGHT_BOLD );
162 0 : mpDocNameFI->SetControlFont( aTmpFont );
163 0 : WinBits nStyle = mpDocNameFI->GetStyle();
164 0 : nStyle |= WB_PATHELLIPSIS;
165 0 : mpDocNameFI->SetStyle( nStyle );
166 : // show signature controls?
167 0 : if( mbShowSignatures )
168 : {
169 0 : mpViewSignsBtn->SetClickHdl( LINK( this, MacroWarning, ViewSignsBtnHdl ) );
170 0 : mpViewSignsBtn->Disable(); // default
171 0 : mpAlwaysTrustCB->SetClickHdl( LINK( this, MacroWarning, AlwaysTrustCheckHdl ) );
172 :
173 0 : mnActSecLevel = SvtSecurityOptions().GetMacroSecurityLevel();
174 0 : if ( mnActSecLevel >= 2 )
175 0 : mpEnableBtn->Disable();
176 : }
177 : else
178 : {
179 0 : mpDescr1FI->SetText("The document contains document macros.");
180 0 : mpSignsFI->Hide();
181 0 : mpViewSignsBtn->Hide();
182 0 : mpAlwaysTrustCB->Hide();
183 :
184 : // move hint up to position of signer list
185 0 : mpDescr2FI->SetPosPixel( mpSignsFI->GetPosPixel() );
186 0 : }
187 0 : }
188 :
189 0 : void MacroWarning::SetStorage( const css::uno::Reference < css::embed::XStorage >& rxStore,
190 : const OUString& aODFVersion,
191 : const css::uno::Sequence< security::DocumentSignatureInformation >& rInfos )
192 : {
193 0 : mxStore = rxStore;
194 0 : maODFVersion = aODFVersion;
195 0 : sal_Int32 nCnt = rInfos.getLength();
196 0 : if( mxStore.is() && nCnt > 0 )
197 : {
198 0 : mpInfos = &rInfos;
199 0 : OUString aCN_Id("CN");
200 0 : OUString s;
201 0 : s = GetContentPart( rInfos[ 0 ].Signer->getSubjectName(), aCN_Id );
202 :
203 0 : for( sal_Int32 i = 1 ; i < nCnt ; ++i )
204 : {
205 0 : s += "\n";
206 0 : s += GetContentPart( rInfos[ i ].Signer->getSubjectName(), aCN_Id );
207 : }
208 :
209 0 : mpSignsFI->SetText( s );
210 0 : mpViewSignsBtn->Enable();
211 : }
212 0 : }
213 :
214 0 : void MacroWarning::SetCertificate( const css::uno::Reference< css::security::XCertificate >& _rxCert )
215 : {
216 0 : mxCert = _rxCert;
217 0 : if( mxCert.is() )
218 : {
219 0 : OUString aCN_Id("CN");
220 0 : OUString s;
221 0 : s = GetContentPart( mxCert->getSubjectName(), aCN_Id );
222 0 : mpSignsFI->SetText( s );
223 0 : mpViewSignsBtn->Enable();
224 : }
225 180 : }
226 :
227 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|