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 <comphelper/xmlsechelper.hxx>
24 #include <vcl/svapp.hxx>
25 #include <osl/file.hxx>
26 #include <rtl/ustrbuf.hxx>
27 #include <tools/datetime.hxx>
28 #include <tools/debug.hxx>
29 #include <unotools/datetime.hxx>
30 #include <unotools/resmgr.hxx>
31 #include <unotools/securityoptions.hxx>
32 #include <tools/urlobj.hxx>
34 #include "secmacrowarnings.hxx"
36 #include <strings.hrc>
38 using namespace ::com::sun::star::security
;
39 using namespace ::com::sun::star
;
40 using namespace comphelper
;
42 MacroWarning::MacroWarning(weld::Window
* pParent
, bool _bWithSignatures
)
43 : MessageDialogController(pParent
, u
"uui/ui/macrowarnmedium.ui"_ustr
, u
"MacroWarnMedium"_ustr
, u
"grid"_ustr
)
44 , mxGrid(m_xBuilder
->weld_widget(u
"grid"_ustr
))
45 , mxSignsFI(m_xBuilder
->weld_label(u
"signsLabel"_ustr
))
46 , mxNotYetValid(m_xBuilder
->weld_label(u
"certNotYetValidLabel"_ustr
))
47 , mxNoLongerValid(m_xBuilder
->weld_label(u
"certNoLongerValidLabel"_ustr
))
48 , mxViewSignsBtn(m_xBuilder
->weld_button(u
"viewSignsButton"_ustr
))
49 , mxViewCertBtn(m_xBuilder
->weld_button(u
"viewCertButton"_ustr
))
50 , mxAlwaysTrustCB(m_xBuilder
->weld_check_button(u
"alwaysTrustCheckbutton"_ustr
))
51 , mxEnableBtn(m_xBuilder
->weld_button(u
"ok"_ustr
))
52 , mxDisableBtn(m_xBuilder
->weld_button(u
"cancel"_ustr
))
54 , mbShowSignatures ( _bWithSignatures
)
59 mxEnableBtn
->connect_clicked(LINK(this, MacroWarning
, EnableBtnHdl
));
60 mxDisableBtn
->connect_clicked(LINK(this, MacroWarning
, DisableBtnHdl
));
61 mxDisableBtn
->grab_focus(); // Default button, but focus is on view button
62 m_xDialog
->SetInstallLOKNotifierHdl(LINK(this, MacroWarning
, InstallLOKNotifierHdl
));
65 IMPL_STATIC_LINK_NOARG(MacroWarning
, InstallLOKNotifierHdl
, void*, vcl::ILibreOfficeKitNotifier
*)
70 void MacroWarning::SetDocumentURL( const OUString
& rDocURL
)
74 osl::FileBase::getFileURLFromSystemPath(rDocURL
, aPath
);
75 aPath
= INetURLObject(aPath
).GetLastName(INetURLObject::DecodeMechanism::Unambiguous
);
76 m_xDialog
->set_primary_text(aPath
);
79 IMPL_LINK_NOARG(MacroWarning
, ViewSignsBtnHdl
, weld::Button
&, void)
81 DBG_ASSERT( mxCert
.is(), "*MacroWarning::ViewSignsBtnHdl(): no certificate set!" );
83 uno::Reference
< security::XDocumentDigitalSignatures
> xD(
84 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion
));
88 xD
->setParentWindow(m_xDialog
->GetXWindow());
90 xD
->showCertificate( mxCert
);
91 else if( mxStore
.is() )
92 xD
->showScriptingContentSignatures( mxStore
, uno::Reference
< io::XInputStream
>() );
96 mxAlwaysTrustCB
->set_sensitive(true);
100 IMPL_LINK_NOARG(MacroWarning
, EnableBtnHdl
, weld::Button
&, void)
102 if (mxAlwaysTrustCB
->get_active())
104 uno::Reference
< security::XDocumentDigitalSignatures
> xD(
105 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion
));
106 xD
->setParentWindow(m_xDialog
->GetXWindow());
108 xD
->addAuthorToTrustedSources( mxCert
);
109 else if( mxStore
.is() )
111 assert(mpInfos
&& "-MacroWarning::EnableBtnHdl(): no infos, search in nirvana...");
113 sal_Int32 nCnt
= mpInfos
->getLength();
114 for( sal_Int32 i
= 0 ; i
< nCnt
; ++i
)
115 xD
->addAuthorToTrustedSources( (*mpInfos
)[ i
].Signer
);
118 m_xDialog
->response(RET_OK
);
121 IMPL_LINK_NOARG(MacroWarning
, DisableBtnHdl
, weld::Button
&, void)
123 m_xDialog
->response(RET_CANCEL
);
126 IMPL_LINK_NOARG(MacroWarning
, AlwaysTrustCheckHdl
, weld::Toggleable
&, void)
128 const bool bEnable
= (mnActSecLevel
< 3 || mxAlwaysTrustCB
->get_active());
129 EnableOkBtn(bEnable
);
130 mxDisableBtn
->set_sensitive(!mxAlwaysTrustCB
->get_active());
133 void MacroWarning::InitControls()
135 // show signature controls?
136 if (mbShowSignatures
)
138 mxAlwaysTrustCB
->connect_toggled(LINK(this, MacroWarning
, AlwaysTrustCheckHdl
));
139 mxAlwaysTrustCB
->set_sensitive(false);
140 mxViewSignsBtn
->connect_clicked(LINK(this, MacroWarning
, ViewSignsBtnHdl
));
141 mxViewSignsBtn
->set_visible(false);
142 mxViewCertBtn
->connect_clicked(LINK(this, MacroWarning
, ViewSignsBtnHdl
));
143 mxViewCertBtn
->set_visible(false);
145 mnActSecLevel
= SvtSecurityOptions::GetMacroSecurityLevel();
146 if ( mnActSecLevel
>= 2 )
155 void MacroWarning::EnableOkBtn(bool bEnable
)
157 mxEnableBtn
->set_sensitive(bEnable
);
158 std::locale
aResLocale(Translate::Create("uui"));
159 mxEnableBtn
->set_tooltip_text(bEnable
? u
""_ustr
: Translate::get(STR_VERIFY_CERT
, aResLocale
));
162 void MacroWarning::SetStorage( const css::uno::Reference
< css::embed::XStorage
>& rxStore
,
163 const OUString
& aODFVersion
,
164 const css::uno::Sequence
< security::DocumentSignatureInformation
>& rInfos
)
167 maODFVersion
= aODFVersion
;
168 sal_Int32 nCnt
= rInfos
.getLength();
169 if( !(mxStore
.is() && nCnt
> 0) )
173 OUStringBuffer
s(xmlsec::GetContentPart(rInfos
[0].Signer
->getSubjectName(),
174 rInfos
[0].Signer
->getCertificateKind()));
176 for( sal_Int32 i
= 1 ; i
< nCnt
; ++i
)
178 s
.append(OUString::Concat("\n")
179 + xmlsec::GetContentPart(rInfos
[i
].Signer
->getSubjectName(),
180 rInfos
[0].Signer
->getCertificateKind()));
183 mxSignsFI
->set_label(s
.makeStringAndClear());
184 mxViewSignsBtn
->set_visible(true);
185 mxViewCertBtn
->set_visible(false);
188 void MacroWarning::SetCertificate( const css::uno::Reference
< css::security::XCertificate
>& _rxCert
)
193 OUString
s(xmlsec::GetContentPart(mxCert
->getSubjectName(), mxCert
->getCertificateKind()));
194 mxSignsFI
->set_label(s
);
196 ::DateTime
now( ::DateTime::SYSTEM
);
197 DateTime
aDateTimeStart( DateTime::EMPTY
);
198 DateTime
aDateTimeEnd( DateTime::EMPTY
);
199 utl::typeConvert( mxCert
->getNotValidBefore(), aDateTimeStart
);
200 utl::typeConvert( mxCert
->getNotValidAfter(), aDateTimeEnd
);
201 mxNotYetValid
->set_visible(now
< aDateTimeStart
);
202 mxNoLongerValid
->set_visible(now
> aDateTimeEnd
);
203 mxViewSignsBtn
->set_visible(false);
204 mxViewCertBtn
->set_visible(true);
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */