Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / uui / source / secmacrowarnings.cxx
blob0bc2e1d04d72ade4a3064f52c785fde9482c36ff
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/documentconstants.hxx>
24 #include <comphelper/processfactory.hxx>
25 #include <osl/file.h>
26 #include <sal/macros.h>
27 #include <tools/debug.hxx>
28 #include <unotools/resmgr.hxx>
29 #include <com/sun/star/security/NoPasswordException.hpp>
31 using namespace ::com::sun::star::security;
33 #include "secmacrowarnings.hxx"
35 using namespace ::com::sun::star;
38 // HACK!!! copied from xmlsecurity/source/dialog/resourcemanager.cxx
40 namespace
42 OUString GetContentPart( const OUString& _rRawString, const OUString& _rPartId )
44 OUString s;
46 sal_Int32 nContStart = _rRawString.indexOf( _rPartId );
47 if( nContStart != -1 )
49 nContStart = nContStart + _rPartId.getLength();
50 ++nContStart; // now its start of content, directly after Id
52 sal_Int32 nContEnd = _rRawString.indexOf( ',', nContStart );
54 if ( nContEnd != -1 )
55 s = _rRawString.copy( nContStart, nContEnd - nContStart );
56 else
57 s = _rRawString.copy( nContStart );
60 return s;
64 MacroWarning::MacroWarning(weld::Window* pParent, bool _bWithSignatures)
65 : MessageDialogController(pParent, "uui/ui/macrowarnmedium.ui", "MacroWarnMedium", "grid")
66 , mxGrid(m_xBuilder->weld_widget("grid"))
67 , mxSignsFI(m_xBuilder->weld_label("signsLabel"))
68 , mxViewSignsBtn(m_xBuilder->weld_button("viewSignsButton"))
69 , mxAlwaysTrustCB(m_xBuilder->weld_check_button("alwaysTrustCheckbutton"))
70 , mxEnableBtn(m_xBuilder->weld_button("ok"))
71 , mxDisableBtn(m_xBuilder->weld_button("cancel"))
72 , mpInfos ( nullptr )
73 , mbShowSignatures ( _bWithSignatures )
74 , mnActSecLevel ( 0 )
76 InitControls();
78 mxEnableBtn->connect_clicked(LINK(this, MacroWarning, EnableBtnHdl));
79 mxDisableBtn->grab_focus(); // Default button, but focus is on view button
82 void MacroWarning::SetDocumentURL( const OUString& rDocURL )
84 OUString aAbbreviatedPath;
85 osl_abbreviateSystemPath(rDocURL.pData, &aAbbreviatedPath.pData, 50, nullptr);
86 m_xDialog->set_primary_text(aAbbreviatedPath);
89 IMPL_LINK_NOARG(MacroWarning, ViewSignsBtnHdl, weld::Button&, void)
91 DBG_ASSERT( mxCert.is(), "*MacroWarning::ViewSignsBtnHdl(): no certificate set!" );
93 uno::Reference< security::XDocumentDigitalSignatures > xD(
94 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion));
95 if( xD.is() )
97 xD->setParentWindow(m_xDialog->GetXWindow());
98 if( mxCert.is() )
99 xD->showCertificate( mxCert );
100 else if( mxStore.is() )
101 xD->showScriptingContentSignatures( mxStore, uno::Reference< io::XInputStream >() );
105 IMPL_LINK_NOARG(MacroWarning, EnableBtnHdl, weld::Button&, void)
107 if (mxAlwaysTrustCB->get_active())
109 uno::Reference< security::XDocumentDigitalSignatures > xD(
110 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion));
111 xD->setParentWindow(m_xDialog->GetXWindow());
112 if( mxCert.is() )
113 xD->addAuthorToTrustedSources( mxCert );
114 else if( mxStore.is() )
116 DBG_ASSERT( mpInfos, "-MacroWarning::EnableBtnHdl(): no infos, search in nirvana..." );
118 sal_Int32 nCnt = mpInfos->getLength();
119 for( sal_Int32 i = 0 ; i < nCnt ; ++i )
120 xD->addAuthorToTrustedSources( (*mpInfos)[ i ].Signer );
123 m_xDialog->response(RET_OK);
126 IMPL_LINK_NOARG(MacroWarning, AlwaysTrustCheckHdl, weld::Button&, void)
128 const bool bEnable = (mnActSecLevel < 2 || mxAlwaysTrustCB->get_active());
129 mxEnableBtn->set_sensitive(bEnable);
130 mxDisableBtn->set_sensitive(!mxAlwaysTrustCB->get_active());
133 void MacroWarning::InitControls()
135 // show signature controls?
136 if (mbShowSignatures)
138 mxViewSignsBtn->connect_clicked(LINK(this, MacroWarning, ViewSignsBtnHdl));
139 mxViewSignsBtn->set_sensitive(false);
141 const SvtSecurityOptions aSecOption;
142 if (!aSecOption.IsReadOnly(SvtSecurityOptions::EOption::MacroTrustedAuthors))
143 mxAlwaysTrustCB->connect_clicked(LINK(this, MacroWarning, AlwaysTrustCheckHdl));
144 else
145 mxAlwaysTrustCB->set_visible(false);
147 mnActSecLevel = aSecOption.GetMacroSecurityLevel();
148 if ( mnActSecLevel >= 2 )
149 mxEnableBtn->set_sensitive(false);
151 else
153 mxGrid->hide();
157 void MacroWarning::SetStorage( const css::uno::Reference < css::embed::XStorage >& rxStore,
158 const OUString& aODFVersion,
159 const css::uno::Sequence< security::DocumentSignatureInformation >& rInfos )
161 mxStore = rxStore;
162 maODFVersion = aODFVersion;
163 sal_Int32 nCnt = rInfos.getLength();
164 if( mxStore.is() && nCnt > 0 )
166 mpInfos = &rInfos;
167 OUString aCN_Id("CN");
168 OUStringBuffer s = GetContentPart( rInfos[ 0 ].Signer->getSubjectName(), aCN_Id );
170 for( sal_Int32 i = 1 ; i < nCnt ; ++i )
172 s.append("\n");
173 s.append(GetContentPart( rInfos[ i ].Signer->getSubjectName(), aCN_Id ));
176 mxSignsFI->set_label(s.makeStringAndClear());
177 mxViewSignsBtn->set_sensitive(true);
181 void MacroWarning::SetCertificate( const css::uno::Reference< css::security::XCertificate >& _rxCert )
183 mxCert = _rxCert;
184 if( mxCert.is() )
186 OUString s = GetContentPart( mxCert->getSubjectName(), "CN" );
187 mxSignsFI->set_label(s);
188 mxViewSignsBtn->set_sensitive(true);
192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */