1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
34 #include "secmacrowarnings.hxx"
36 using namespace ::com::sun::star
;
39 // HACK!!! copied from xmlsecurity/source/dialog/resourcemanager.cxx
43 OUString
GetContentPart( const OUString
& _rRawString
, const OUString
& _rPartId
)
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
);
56 s
= _rRawString
.copy( nContStart
, nContEnd
- nContStart
);
58 s
= _rRawString
.copy( nContStart
);
66 MacroWarning::MacroWarning( vcl::Window
* _pParent
, bool _bWithSignatures
, ResMgr
& )
67 :ModalDialog ( _pParent
, "MacroWarnMedium", "uui/ui/macrowarnmedium.ui" )
69 ,mbSignedMode ( true )
70 ,mbShowSignatures ( _bWithSignatures
)
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");
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()
96 void MacroWarning::dispose()
103 mpViewSignsBtn
.clear();
105 mpAlwaysTrustCB
.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
));
125 xD
->showCertificate( mxCert
);
126 else if( mxStore
.is() )
127 xD
->showScriptingContentSignatures( mxStore
, uno::Reference
< io::XInputStream
>() );
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
));
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
);
155 IMPL_LINK_NOARG(MacroWarning
, DisableBtnHdl
)
157 EndDialog( RET_CANCEL
);
161 IMPL_LINK_NOARG(MacroWarning
, AlwaysTrustCheckHdl
)
163 bool bEnable
= ( mnActSecLevel
< 2 || mpAlwaysTrustCB
->IsChecked() );
164 mpEnableBtn
->Enable( bEnable
);
165 mpDisableBtn
->Enable( !mpAlwaysTrustCB
->IsChecked() );
170 void MacroWarning::InitControls()
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();
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
)
212 maODFVersion
= aODFVersion
;
213 sal_Int32 nCnt
= rInfos
.getLength();
214 if( mxStore
.is() && nCnt
> 0 )
217 OUString
aCN_Id("CN");
219 s
= GetContentPart( rInfos
[ 0 ].Signer
->getSubjectName(), aCN_Id
);
221 for( sal_Int32 i
= 1 ; i
< nCnt
; ++i
)
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
)
237 OUString
aCN_Id("CN");
239 s
= GetContentPart( mxCert
->getSubjectName(), aCN_Id
);
240 mpSignsFI
->SetText( s
);
241 mpViewSignsBtn
->Enable();
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */