LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / uui / source / secmacrowarnings.cxx
blob601796e656a5d819026564a6984f0768ad352d3a
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/security/DocumentDigitalSignatures.hpp>
22 #include <comphelper/processfactory.hxx>
23 #include <vcl/svapp.hxx>
24 #include <osl/file.hxx>
25 #include <osl/file.h>
26 #include <rtl/ustrbuf.hxx>
27 #include <tools/debug.hxx>
28 #include <unotools/securityoptions.hxx>
29 #include <tools/urlobj.hxx>
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->connect_clicked(LINK(this, MacroWarning, DisableBtnHdl));
80 mxDisableBtn->grab_focus(); // Default button, but focus is on view button
81 m_xDialog->SetInstallLOKNotifierHdl(LINK(this, MacroWarning, InstallLOKNotifierHdl));
84 IMPL_STATIC_LINK_NOARG(MacroWarning, InstallLOKNotifierHdl, void*, vcl::ILibreOfficeKitNotifier*)
86 return GetpApp();
89 void MacroWarning::SetDocumentURL( const OUString& rDocURL )
91 OUString aPath;
93 osl::FileBase::getFileURLFromSystemPath(rDocURL, aPath);
94 aPath = INetURLObject(aPath).GetLastName(INetURLObject::DecodeMechanism::Unambiguous);
95 m_xDialog->set_primary_text(aPath);
98 IMPL_LINK_NOARG(MacroWarning, ViewSignsBtnHdl, weld::Button&, void)
100 DBG_ASSERT( mxCert.is(), "*MacroWarning::ViewSignsBtnHdl(): no certificate set!" );
102 uno::Reference< security::XDocumentDigitalSignatures > xD(
103 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion));
104 if( xD.is() )
106 xD->setParentWindow(m_xDialog->GetXWindow());
107 if( mxCert.is() )
108 xD->showCertificate( mxCert );
109 else if( mxStore.is() )
110 xD->showScriptingContentSignatures( mxStore, uno::Reference< io::XInputStream >() );
114 IMPL_LINK_NOARG(MacroWarning, EnableBtnHdl, weld::Button&, void)
116 if (mxAlwaysTrustCB->get_active())
118 uno::Reference< security::XDocumentDigitalSignatures > xD(
119 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion));
120 xD->setParentWindow(m_xDialog->GetXWindow());
121 if( mxCert.is() )
122 xD->addAuthorToTrustedSources( mxCert );
123 else if( mxStore.is() )
125 DBG_ASSERT( mpInfos, "-MacroWarning::EnableBtnHdl(): no infos, search in nirvana..." );
127 sal_Int32 nCnt = mpInfos->getLength();
128 for( sal_Int32 i = 0 ; i < nCnt ; ++i )
129 xD->addAuthorToTrustedSources( (*mpInfos)[ i ].Signer );
132 m_xDialog->response(RET_OK);
135 IMPL_LINK_NOARG(MacroWarning, DisableBtnHdl, weld::Button&, void)
137 m_xDialog->response(RET_CANCEL);
140 IMPL_LINK_NOARG(MacroWarning, AlwaysTrustCheckHdl, weld::Toggleable&, void)
142 const bool bEnable = (mnActSecLevel < 2 || mxAlwaysTrustCB->get_active());
143 mxEnableBtn->set_sensitive(bEnable);
144 mxDisableBtn->set_sensitive(!mxAlwaysTrustCB->get_active());
147 void MacroWarning::InitControls()
149 // show signature controls?
150 if (mbShowSignatures)
152 mxViewSignsBtn->connect_clicked(LINK(this, MacroWarning, ViewSignsBtnHdl));
153 mxViewSignsBtn->set_sensitive(false);
155 if (!SvtSecurityOptions::IsReadOnly(SvtSecurityOptions::EOption::MacroTrustedAuthors))
156 mxAlwaysTrustCB->connect_toggled(LINK(this, MacroWarning, AlwaysTrustCheckHdl));
157 else
158 mxAlwaysTrustCB->set_visible(false);
160 mnActSecLevel = SvtSecurityOptions::GetMacroSecurityLevel();
161 if ( mnActSecLevel >= 2 )
162 mxEnableBtn->set_sensitive(false);
164 else
166 mxGrid->hide();
170 void MacroWarning::SetStorage( const css::uno::Reference < css::embed::XStorage >& rxStore,
171 const OUString& aODFVersion,
172 const css::uno::Sequence< security::DocumentSignatureInformation >& rInfos )
174 mxStore = rxStore;
175 maODFVersion = aODFVersion;
176 sal_Int32 nCnt = rInfos.getLength();
177 if( !(mxStore.is() && nCnt > 0) )
178 return;
180 mpInfos = &rInfos;
181 OUString aCN_Id("CN");
182 OUStringBuffer s(GetContentPart( rInfos[ 0 ].Signer->getSubjectName(), aCN_Id ));
184 for( sal_Int32 i = 1 ; i < nCnt ; ++i )
186 s.append("\n");
187 s.append(GetContentPart( rInfos[ i ].Signer->getSubjectName(), aCN_Id ));
190 mxSignsFI->set_label(s.makeStringAndClear());
191 mxViewSignsBtn->set_sensitive(true);
194 void MacroWarning::SetCertificate( const css::uno::Reference< css::security::XCertificate >& _rxCert )
196 mxCert = _rxCert;
197 if( mxCert.is() )
199 OUString s = GetContentPart( mxCert->getSubjectName(), "CN" );
200 mxSignsFI->set_label(s);
201 mxViewSignsBtn->set_sensitive(true);
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */