LibreOffice Module uui (master) 1
secmacrowarnings.cxx
Go to the documentation of this file.
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/security/DocumentDigitalSignatures.hpp>
23#include <vcl/svapp.hxx>
24#include <osl/file.hxx>
25#include <rtl/ustrbuf.hxx>
26#include <tools/debug.hxx>
28#include <tools/urlobj.hxx>
29
30using namespace ::com::sun::star::security;
31
32#include "secmacrowarnings.hxx"
33
34using namespace ::com::sun::star;
35
36
37// HACK!!! copied from xmlsecurity/source/dialog/resourcemanager.cxx
38
39namespace
40{
41 std::u16string_view GetContentPart( std::u16string_view _rRawString, std::u16string_view _rPartId )
42 {
43 std::u16string_view s;
44
45 size_t nContStart = _rRawString.find( _rPartId );
46 if( nContStart != std::u16string_view::npos )
47 {
48 nContStart = nContStart + _rPartId.size();
49 ++nContStart; // now its start of content, directly after Id
50
51 size_t nContEnd = _rRawString.find( ',', nContStart );
52
53 if ( nContEnd != std::u16string_view::npos )
54 s = _rRawString.substr( nContStart, nContEnd - nContStart );
55 else
56 s = _rRawString.substr( nContStart );
57 }
58
59 return s;
60 }
61}
62
63MacroWarning::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"))
71 , mpInfos ( nullptr )
72 , mbShowSignatures ( _bWithSignatures )
73 , mnActSecLevel ( 0 )
74{
76
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));
81}
82
84{
85 return GetpApp();
86}
87
88void MacroWarning::SetDocumentURL( const OUString& rDocURL )
89{
90 OUString aPath;
91
92 osl::FileBase::getFileURLFromSystemPath(rDocURL, aPath);
94 m_xDialog->set_primary_text(aPath);
95}
96
97IMPL_LINK_NOARG(MacroWarning, ViewSignsBtnHdl, weld::Button&, void)
98{
99 DBG_ASSERT( mxCert.is(), "*MacroWarning::ViewSignsBtnHdl(): no certificate set!" );
100
101 uno::Reference< security::XDocumentDigitalSignatures > xD(
102 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion));
103 if( xD.is() )
104 {
105 xD->setParentWindow(m_xDialog->GetXWindow());
106 if( mxCert.is() )
107 xD->showCertificate( mxCert );
108 else if( mxStore.is() )
109 xD->showScriptingContentSignatures( mxStore, uno::Reference< io::XInputStream >() );
110 }
111}
112
114{
115 if (mxAlwaysTrustCB->get_active())
116 {
117 uno::Reference< security::XDocumentDigitalSignatures > xD(
118 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion));
119 xD->setParentWindow(m_xDialog->GetXWindow());
120 if( mxCert.is() )
121 xD->addAuthorToTrustedSources( mxCert );
122 else if( mxStore.is() )
123 {
124 DBG_ASSERT( mpInfos, "-MacroWarning::EnableBtnHdl(): no infos, search in nirvana..." );
125
126 sal_Int32 nCnt = mpInfos->getLength();
127 for( sal_Int32 i = 0 ; i < nCnt ; ++i )
128 xD->addAuthorToTrustedSources( (*mpInfos)[ i ].Signer );
129 }
130 }
131 m_xDialog->response(RET_OK);
132}
133
135{
136 m_xDialog->response(RET_CANCEL);
137}
138
139IMPL_LINK_NOARG(MacroWarning, AlwaysTrustCheckHdl, weld::Toggleable&, void)
140{
141 const bool bEnable = (mnActSecLevel < 2 || mxAlwaysTrustCB->get_active());
142 mxEnableBtn->set_sensitive(bEnable);
143 mxDisableBtn->set_sensitive(!mxAlwaysTrustCB->get_active());
144}
145
147{
148 // show signature controls?
150 {
151 mxViewSignsBtn->connect_clicked(LINK(this, MacroWarning, ViewSignsBtnHdl));
152 mxViewSignsBtn->set_sensitive(false);
153
155 mxAlwaysTrustCB->connect_toggled(LINK(this, MacroWarning, AlwaysTrustCheckHdl));
156 else
157 mxAlwaysTrustCB->set_visible(false);
158
160 if ( mnActSecLevel >= 2 )
161 mxEnableBtn->set_sensitive(false);
162 }
163 else
164 {
165 mxGrid->hide();
166 }
167}
168
169void MacroWarning::SetStorage( const css::uno::Reference < css::embed::XStorage >& rxStore,
170 const OUString& aODFVersion,
171 const css::uno::Sequence< security::DocumentSignatureInformation >& rInfos )
172{
173 mxStore = rxStore;
174 maODFVersion = aODFVersion;
175 sal_Int32 nCnt = rInfos.getLength();
176 if( !(mxStore.is() && nCnt > 0) )
177 return;
178
179 mpInfos = &rInfos;
180 OUString aCN_Id("CN");
181 OUStringBuffer s(GetContentPart( rInfos[ 0 ].Signer->getSubjectName(), aCN_Id ));
182
183 for( sal_Int32 i = 1 ; i < nCnt ; ++i )
184 {
185 s.append(OUString::Concat("\n")
186 + GetContentPart( rInfos[ i ].Signer->getSubjectName(), aCN_Id ));
187 }
188
189 mxSignsFI->set_label(s.makeStringAndClear());
190 mxViewSignsBtn->set_sensitive(true);
191}
192
193void MacroWarning::SetCertificate( const css::uno::Reference< css::security::XCertificate >& _rxCert )
194{
195 mxCert = _rxCert;
196 if( mxCert.is() )
197 {
198 OUString s( GetContentPart( mxCert->getSubjectName(), u"CN" ) );
199 mxSignsFI->set_label(s);
200 mxViewSignsBtn->set_sensitive(true);
201 }
202}
203
204/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XExecutableDialog > m_xDialog
OUString GetLastName(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
std::unique_ptr< weld::Widget > mxGrid
MacroWarning(weld::Window *pParent, bool _bShowSignatures)
std::unique_ptr< weld::Button > mxEnableBtn
void SetStorage(const css::uno::Reference< css::embed::XStorage > &rxStore, const OUString &aODFVersion, const css::uno::Sequence< css::security::DocumentSignatureInformation > &_rInfos)
void SetDocumentURL(const OUString &rDocURL)
css::uno::Reference< css::embed::XStorage > mxStore
std::unique_ptr< weld::CheckButton > mxAlwaysTrustCB
std::unique_ptr< weld::Button > mxDisableBtn
void SetCertificate(const css::uno::Reference< css::security::XCertificate > &_rxCert)
std::unique_ptr< weld::Label > mxSignsFI
css::uno::Reference< css::security::XCertificate > mxCert
OUString maODFVersion
std::unique_ptr< weld::Button > mxViewSignsBtn
const bool mbShowSignatures
sal_Int32 mnActSecLevel
const css::uno::Sequence< css::security::DocumentSignatureInformation > * mpInfos
std::unique_ptr< weld::MessageDialog > m_xDialog
#define DBG_ASSERT(sCon, aError)
float u
bool IsReadOnly(EOption eOption)
sal_Int32 GetMacroSecurityLevel()
OUString GetContentPart(const OUString &_rRawString, const css::security::CertificateKind &rKind)
Reference< XComponentContext > getProcessComponentContext()
int i
IMPL_LINK_NOARG(MacroWarning, ViewSignsBtnHdl, weld::Button &, void)
IMPL_STATIC_LINK_NOARG(MacroWarning, InstallLOKNotifierHdl, void *, vcl::ILibreOfficeKitNotifier *)
VCL_DLLPUBLIC Application * GetpApp()
RET_OK
RET_CANCEL