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/lang/XMultiServiceFactory.hpp>
22 #include <com/sun/star/security/DocumentDigitalSignatures.hpp>
23 #include <comphelper/sequence.hxx>
24 #include <comphelper/documentconstants.hxx>
25 #include <comphelper/processfactory.hxx>
27 #include <sal/macros.h>
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
42 OUString
GetContentPart( const OUString
& _rRawString
, const OUString
& _rPartId
)
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
);
55 s
= _rRawString
.copy( nContStart
, nContEnd
- nContStart
);
57 s
= _rRawString
.copy( nContStart
);
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"))
73 , mbSignedMode ( true )
74 , mbShowSignatures ( _bWithSignatures
)
79 mxEnableBtn
->connect_clicked(LINK(this, MacroWarning
, EnableBtnHdl
));
80 mxDisableBtn
->grab_focus(); // Default button, but focus is on view button
83 void MacroWarning::SetDocumentURL( const OUString
& rDocURL
)
85 OUString aAbbreviatedPath
;
86 osl_abbreviateSystemPath(rDocURL
.pData
, &aAbbreviatedPath
.pData
, 50, nullptr);
87 fprintf(stderr
, "in %s, out %s\n", OUStringToOString(rDocURL
, RTL_TEXTENCODING_UTF8
).getStr(),
88 OUStringToOString(aAbbreviatedPath
, RTL_TEXTENCODING_UTF8
).getStr());
89 m_xDialog
->set_primary_text(aAbbreviatedPath
);
92 IMPL_LINK_NOARG(MacroWarning
, ViewSignsBtnHdl
, weld::Button
&, void)
94 DBG_ASSERT( mxCert
.is(), "*MacroWarning::ViewSignsBtnHdl(): no certificate set!" );
96 uno::Reference
< security::XDocumentDigitalSignatures
> xD(
97 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion
));
101 xD
->showCertificate( mxCert
);
102 else if( mxStore
.is() )
103 xD
->showScriptingContentSignatures( mxStore
, uno::Reference
< io::XInputStream
>() );
107 IMPL_LINK_NOARG(MacroWarning
, EnableBtnHdl
, weld::Button
&, void)
109 if (mbSignedMode
&& mxAlwaysTrustCB
->get_active())
110 { // insert path into trusted path list
111 uno::Reference
< security::XDocumentDigitalSignatures
> xD(
112 security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), maODFVersion
));
114 xD
->addAuthorToTrustedSources( mxCert
);
115 else if( mxStore
.is() )
117 DBG_ASSERT( mpInfos
, "-MacroWarning::EnableBtnHdl(): no infos, search in nirvana..." );
119 sal_Int32 nCnt
= mpInfos
->getLength();
120 for( sal_Int32 i
= 0 ; i
< nCnt
; ++i
)
121 xD
->addAuthorToTrustedSources( (*mpInfos
)[ i
].Signer
);
124 m_xDialog
->response(RET_OK
);
127 IMPL_LINK_NOARG(MacroWarning
, AlwaysTrustCheckHdl
, weld::Button
&, void)
129 const bool bEnable
= (mnActSecLevel
< 2 || mxAlwaysTrustCB
->get_active());
130 mxEnableBtn
->set_sensitive(bEnable
);
131 mxDisableBtn
->set_sensitive(!mxAlwaysTrustCB
->get_active());
134 void MacroWarning::InitControls()
136 // show signature controls?
137 if (mbShowSignatures
)
139 mxViewSignsBtn
->connect_clicked(LINK(this, MacroWarning
, ViewSignsBtnHdl
));
140 mxViewSignsBtn
->set_sensitive(false); // default
141 mxAlwaysTrustCB
->connect_clicked(LINK(this, MacroWarning
, AlwaysTrustCheckHdl
));
143 mnActSecLevel
= SvtSecurityOptions().GetMacroSecurityLevel();
144 if ( mnActSecLevel
>= 2 )
145 mxEnableBtn
->set_sensitive(false);
153 void MacroWarning::SetStorage( const css::uno::Reference
< css::embed::XStorage
>& rxStore
,
154 const OUString
& aODFVersion
,
155 const css::uno::Sequence
< security::DocumentSignatureInformation
>& rInfos
)
158 maODFVersion
= aODFVersion
;
159 sal_Int32 nCnt
= rInfos
.getLength();
160 if( mxStore
.is() && nCnt
> 0 )
163 OUString
aCN_Id("CN");
165 s
= GetContentPart( rInfos
[ 0 ].Signer
->getSubjectName(), aCN_Id
);
167 for( sal_Int32 i
= 1 ; i
< nCnt
; ++i
)
170 s
+= GetContentPart( rInfos
[ i
].Signer
->getSubjectName(), aCN_Id
);
173 mxSignsFI
->set_label(s
);
174 mxViewSignsBtn
->set_sensitive(true);
178 void MacroWarning::SetCertificate( const css::uno::Reference
< css::security::XCertificate
>& _rxCert
)
183 OUString s
= GetContentPart( mxCert
->getSubjectName(), "CN" );
184 mxSignsFI
->set_label(s
);
185 mxViewSignsBtn
->set_sensitive(true);
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */