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>
23 #include <vcl/svapp.hxx>
24 #include <osl/file.hxx>
25 #include <rtl/ustrbuf.hxx>
26 #include <tools/debug.hxx>
27 #include <unotools/securityoptions.hxx>
28 #include <tools/urlobj.hxx>
30 using namespace ::com::sun::star::security
;
32 #include "secmacrowarnings.hxx"
34 using namespace ::com::sun::star
;
37 // HACK!!! copied from xmlsecurity/source/dialog/resourcemanager.cxx
41 std::u16string_view
GetContentPart( std::u16string_view _rRawString
, std::u16string_view _rPartId
)
43 std::u16string_view s
;
45 size_t nContStart
= _rRawString
.find( _rPartId
);
46 if( nContStart
!= std::u16string_view::npos
)
48 nContStart
= nContStart
+ _rPartId
.size();
49 ++nContStart
; // now its start of content, directly after Id
51 size_t nContEnd
= _rRawString
.find( ',', nContStart
);
53 if ( nContEnd
!= std::u16string_view::npos
)
54 s
= _rRawString
.substr( nContStart
, nContEnd
- nContStart
);
56 s
= _rRawString
.substr( nContStart
);
63 MacroWarning::MacroWarning(weld::Window
* pParent
, bool _bWithSignatures
)
64 : MessageDialogController(pParent
, "uui/ui/macrowarnmedium.ui", "MacroWarnMedium", "grid")
65 , mxGrid(m_xBuilder
->weld_widget("grid"))
66 , mxSignsFI(m_xBuilder
->weld_label("signsLabel"))
67 , mxViewSignsBtn(m_xBuilder
->weld_button("viewSignsButton"))
68 , mxAlwaysTrustCB(m_xBuilder
->weld_check_button("alwaysTrustCheckbutton"))
69 , mxEnableBtn(m_xBuilder
->weld_button("ok"))
70 , mxDisableBtn(m_xBuilder
->weld_button("cancel"))
72 , mbShowSignatures ( _bWithSignatures
)
77 mxEnableBtn
->connect_clicked(LINK(this, MacroWarning
, EnableBtnHdl
));
78 mxDisableBtn
->connect_clicked(LINK(this, MacroWarning
, DisableBtnHdl
));
79 mxDisableBtn
->grab_focus(); // Default button, but focus is on view button
80 m_xDialog
->SetInstallLOKNotifierHdl(LINK(this, MacroWarning
, InstallLOKNotifierHdl
));
83 IMPL_STATIC_LINK_NOARG(MacroWarning
, InstallLOKNotifierHdl
, void*, vcl::ILibreOfficeKitNotifier
*)
88 void MacroWarning::SetDocumentURL( const OUString
& rDocURL
)
92 osl::FileBase::getFileURLFromSystemPath(rDocURL
, aPath
);
93 aPath
= INetURLObject(aPath
).GetLastName(INetURLObject::DecodeMechanism::Unambiguous
);
94 m_xDialog
->set_primary_text(aPath
);
97 IMPL_LINK_NOARG(MacroWarning
, ViewSignsBtnHdl
, weld::Button
&, void)
99 DBG_ASSERT( mxCert
.is(), "*MacroWarning::ViewSignsBtnHdl(): no certificate set!" );
101 uno::Reference
< security::XDocumentDigitalSignatures
> xD(
102 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion
));
105 xD
->setParentWindow(m_xDialog
->GetXWindow());
107 xD
->showCertificate( mxCert
);
108 else if( mxStore
.is() )
109 xD
->showScriptingContentSignatures( mxStore
, uno::Reference
< io::XInputStream
>() );
113 IMPL_LINK_NOARG(MacroWarning
, EnableBtnHdl
, weld::Button
&, void)
115 if (mxAlwaysTrustCB
->get_active())
117 uno::Reference
< security::XDocumentDigitalSignatures
> xD(
118 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion
));
119 xD
->setParentWindow(m_xDialog
->GetXWindow());
121 xD
->addAuthorToTrustedSources( mxCert
);
122 else if( mxStore
.is() )
124 DBG_ASSERT( mpInfos
, "-MacroWarning::EnableBtnHdl(): no infos, search in nirvana..." );
126 sal_Int32 nCnt
= mpInfos
->getLength();
127 for( sal_Int32 i
= 0 ; i
< nCnt
; ++i
)
128 xD
->addAuthorToTrustedSources( (*mpInfos
)[ i
].Signer
);
131 m_xDialog
->response(RET_OK
);
134 IMPL_LINK_NOARG(MacroWarning
, DisableBtnHdl
, weld::Button
&, void)
136 m_xDialog
->response(RET_CANCEL
);
139 IMPL_LINK_NOARG(MacroWarning
, AlwaysTrustCheckHdl
, weld::Toggleable
&, void)
141 const bool bEnable
= (mnActSecLevel
< 2 || mxAlwaysTrustCB
->get_active());
142 mxEnableBtn
->set_sensitive(bEnable
);
143 mxDisableBtn
->set_sensitive(!mxAlwaysTrustCB
->get_active());
146 void MacroWarning::InitControls()
148 // show signature controls?
149 if (mbShowSignatures
)
151 mxViewSignsBtn
->connect_clicked(LINK(this, MacroWarning
, ViewSignsBtnHdl
));
152 mxViewSignsBtn
->set_sensitive(false);
154 if (!SvtSecurityOptions::IsReadOnly(SvtSecurityOptions::EOption::MacroTrustedAuthors
))
155 mxAlwaysTrustCB
->connect_toggled(LINK(this, MacroWarning
, AlwaysTrustCheckHdl
));
157 mxAlwaysTrustCB
->set_visible(false);
159 mnActSecLevel
= SvtSecurityOptions::GetMacroSecurityLevel();
160 if ( mnActSecLevel
>= 2 )
161 mxEnableBtn
->set_sensitive(false);
169 void MacroWarning::SetStorage( const css::uno::Reference
< css::embed::XStorage
>& rxStore
,
170 const OUString
& aODFVersion
,
171 const css::uno::Sequence
< security::DocumentSignatureInformation
>& rInfos
)
174 maODFVersion
= aODFVersion
;
175 sal_Int32 nCnt
= rInfos
.getLength();
176 if( !(mxStore
.is() && nCnt
> 0) )
180 OUString
aCN_Id("CN");
181 OUStringBuffer
s(GetContentPart( rInfos
[ 0 ].Signer
->getSubjectName(), aCN_Id
));
183 for( sal_Int32 i
= 1 ; i
< nCnt
; ++i
)
186 s
.append(GetContentPart( rInfos
[ i
].Signer
->getSubjectName(), aCN_Id
));
189 mxSignsFI
->set_label(s
.makeStringAndClear());
190 mxViewSignsBtn
->set_sensitive(true);
193 void MacroWarning::SetCertificate( const css::uno::Reference
< css::security::XCertificate
>& _rxCert
)
198 OUString
s( GetContentPart( mxCert
->getSubjectName(), u
"CN" ) );
199 mxSignsFI
->set_label(s
);
200 mxViewSignsBtn
->set_sensitive(true);
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */