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/security/DocumentDigitalSignatures.hpp>
22 #include <comphelper/processfactory.hxx>
24 #include <rtl/ustrbuf.hxx>
25 #include <tools/debug.hxx>
26 #include <unotools/securityoptions.hxx>
28 using namespace ::com::sun::star::security
;
30 #include "secmacrowarnings.hxx"
32 using namespace ::com::sun::star
;
35 // HACK!!! copied from xmlsecurity/source/dialog/resourcemanager.cxx
39 OUString
GetContentPart( const OUString
& _rRawString
, const OUString
& _rPartId
)
43 sal_Int32 nContStart
= _rRawString
.indexOf( _rPartId
);
44 if( nContStart
!= -1 )
46 nContStart
= nContStart
+ _rPartId
.getLength();
47 ++nContStart
; // now its start of content, directly after Id
49 sal_Int32 nContEnd
= _rRawString
.indexOf( ',', nContStart
);
52 s
= _rRawString
.copy( nContStart
, nContEnd
- nContStart
);
54 s
= _rRawString
.copy( nContStart
);
61 MacroWarning::MacroWarning(weld::Window
* pParent
, bool _bWithSignatures
)
62 : MessageDialogController(pParent
, "uui/ui/macrowarnmedium.ui", "MacroWarnMedium", "grid")
63 , mxGrid(m_xBuilder
->weld_widget("grid"))
64 , mxSignsFI(m_xBuilder
->weld_label("signsLabel"))
65 , mxViewSignsBtn(m_xBuilder
->weld_button("viewSignsButton"))
66 , mxAlwaysTrustCB(m_xBuilder
->weld_check_button("alwaysTrustCheckbutton"))
67 , mxEnableBtn(m_xBuilder
->weld_button("ok"))
68 , mxDisableBtn(m_xBuilder
->weld_button("cancel"))
70 , mbShowSignatures ( _bWithSignatures
)
75 mxEnableBtn
->connect_clicked(LINK(this, MacroWarning
, EnableBtnHdl
));
76 mxDisableBtn
->grab_focus(); // Default button, but focus is on view button
79 void MacroWarning::SetDocumentURL( const OUString
& rDocURL
)
81 OUString aAbbreviatedPath
;
82 osl_abbreviateSystemPath(rDocURL
.pData
, &aAbbreviatedPath
.pData
, 50, nullptr);
83 m_xDialog
->set_primary_text(aAbbreviatedPath
);
86 IMPL_LINK_NOARG(MacroWarning
, ViewSignsBtnHdl
, weld::Button
&, void)
88 DBG_ASSERT( mxCert
.is(), "*MacroWarning::ViewSignsBtnHdl(): no certificate set!" );
90 uno::Reference
< security::XDocumentDigitalSignatures
> xD(
91 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion
));
94 xD
->setParentWindow(m_xDialog
->GetXWindow());
96 xD
->showCertificate( mxCert
);
97 else if( mxStore
.is() )
98 xD
->showScriptingContentSignatures( mxStore
, uno::Reference
< io::XInputStream
>() );
102 IMPL_LINK_NOARG(MacroWarning
, EnableBtnHdl
, weld::Button
&, void)
104 if (mxAlwaysTrustCB
->get_active())
106 uno::Reference
< security::XDocumentDigitalSignatures
> xD(
107 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion
));
108 xD
->setParentWindow(m_xDialog
->GetXWindow());
110 xD
->addAuthorToTrustedSources( mxCert
);
111 else if( mxStore
.is() )
113 DBG_ASSERT( mpInfos
, "-MacroWarning::EnableBtnHdl(): no infos, search in nirvana..." );
115 sal_Int32 nCnt
= mpInfos
->getLength();
116 for( sal_Int32 i
= 0 ; i
< nCnt
; ++i
)
117 xD
->addAuthorToTrustedSources( (*mpInfos
)[ i
].Signer
);
120 m_xDialog
->response(RET_OK
);
123 IMPL_LINK_NOARG(MacroWarning
, AlwaysTrustCheckHdl
, weld::Button
&, void)
125 const bool bEnable
= (mnActSecLevel
< 2 || mxAlwaysTrustCB
->get_active());
126 mxEnableBtn
->set_sensitive(bEnable
);
127 mxDisableBtn
->set_sensitive(!mxAlwaysTrustCB
->get_active());
130 void MacroWarning::InitControls()
132 // show signature controls?
133 if (mbShowSignatures
)
135 mxViewSignsBtn
->connect_clicked(LINK(this, MacroWarning
, ViewSignsBtnHdl
));
136 mxViewSignsBtn
->set_sensitive(false);
138 const SvtSecurityOptions aSecOption
;
139 if (!aSecOption
.IsReadOnly(SvtSecurityOptions::EOption::MacroTrustedAuthors
))
140 mxAlwaysTrustCB
->connect_clicked(LINK(this, MacroWarning
, AlwaysTrustCheckHdl
));
142 mxAlwaysTrustCB
->set_visible(false);
144 mnActSecLevel
= aSecOption
.GetMacroSecurityLevel();
145 if ( mnActSecLevel
>= 2 )
146 mxEnableBtn
->set_sensitive(false);
154 void MacroWarning::SetStorage( const css::uno::Reference
< css::embed::XStorage
>& rxStore
,
155 const OUString
& aODFVersion
,
156 const css::uno::Sequence
< security::DocumentSignatureInformation
>& rInfos
)
159 maODFVersion
= aODFVersion
;
160 sal_Int32 nCnt
= rInfos
.getLength();
161 if( !(mxStore
.is() && nCnt
> 0) )
165 OUString
aCN_Id("CN");
166 OUStringBuffer s
= GetContentPart( rInfos
[ 0 ].Signer
->getSubjectName(), aCN_Id
);
168 for( sal_Int32 i
= 1 ; i
< nCnt
; ++i
)
171 s
.append(GetContentPart( rInfos
[ i
].Signer
->getSubjectName(), aCN_Id
));
174 mxSignsFI
->set_label(s
.makeStringAndClear());
175 mxViewSignsBtn
->set_sensitive(true);
178 void MacroWarning::SetCertificate( const css::uno::Reference
< css::security::XCertificate
>& _rxCert
)
183 OUString s
= GetContentPart( mxCert
->getSubjectName(), "CN" );
184 mxSignsFI
->set_label(s
);
185 mxViewSignsBtn
->set_sensitive(true);
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */