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 .
21 #include <macrosecurity.hxx>
22 #include <certificatechooser.hxx>
23 #include <certificateviewer.hxx>
24 #include <biginteger.hxx>
26 #include <osl/file.hxx>
27 #include <vcl/help.hxx>
28 #include <vcl/layout.hxx>
31 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
32 #include <comphelper/sequence.hxx>
33 #include <sfx2/filedlghelper.hxx>
34 #include <comphelper/processfactory.hxx>
35 #include <com/sun/star/uno/Exception.hpp>
36 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
38 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
39 #include <tools/urlobj.hxx>
40 #include <svtools/treelistentry.hxx>
42 #include <vcl/msgbox.hxx>
44 #include "dialogs.hrc"
45 #include "resourcemanager.hxx"
47 using namespace ::com::sun::star
;
50 IMPL_LINK_NOARG(MacroSecurity
, OkBtnHdl
, Button
*, void)
52 mpLevelTP
->ClosePage();
53 mpTrustSrcTP
->ClosePage();
58 MacroSecurity::MacroSecurity( vcl::Window
* _pParent
,
59 const css::uno::Reference
< css::xml::crypto::XSecurityEnvironment
>& _rxSecurityEnvironment
)
60 : TabDialog(_pParent
, "MacroSecurityDialog", "xmlsec/ui/macrosecuritydialog.ui")
61 , mxSecurityEnvironment(_rxSecurityEnvironment
)
63 get(m_pTabCtrl
, "tabcontrol");
64 get(m_pResetBtn
, "reset");
67 mpLevelTP
.reset(VclPtr
<MacroSecurityLevelTP
>::Create(m_pTabCtrl
, this));
68 mpTrustSrcTP
.reset(VclPtr
<MacroSecurityTrustedSourcesTP
>::Create(m_pTabCtrl
, this));
70 m_nSecLevelId
= m_pTabCtrl
->GetPageId("SecurityLevelPage");
71 m_nSecTrustId
= m_pTabCtrl
->GetPageId("SecurityTrustPage");
73 m_pTabCtrl
->SetTabPage(m_nSecLevelId
, mpLevelTP
);
74 m_pTabCtrl
->SetTabPage(m_nSecTrustId
, mpTrustSrcTP
);
75 m_pTabCtrl
->SetCurPageId(m_nSecLevelId
);
77 m_pOkBtn
->SetClickHdl( LINK( this, MacroSecurity
, OkBtnHdl
) );
80 MacroSecurity::~MacroSecurity()
85 void MacroSecurity::dispose()
87 m_pTabCtrl
->GetTabPage(m_nSecTrustId
)->disposeOnce();
88 m_pTabCtrl
->GetTabPage(m_nSecLevelId
)->disposeOnce();
92 mpLevelTP
.disposeAndClear();
93 mpTrustSrcTP
.disposeAndClear();
97 MacroSecurityTP::MacroSecurityTP(vcl::Window
* _pParent
, const OString
& rID
,
98 const OUString
& rUIXMLDescription
, MacroSecurity
* _pDlg
)
99 : TabPage(_pParent
, rID
, rUIXMLDescription
)
104 MacroSecurityTP::~MacroSecurityTP()
109 void MacroSecurityTP::dispose()
115 MacroSecurityLevelTP::MacroSecurityLevelTP(vcl::Window
* _pParent
, MacroSecurity
* _pDlg
)
116 : MacroSecurityTP(_pParent
, "SecurityLevelPage", "xmlsec/ui/securitylevelpage.ui", _pDlg
)
118 get(m_pVeryHighRB
, "vhigh");
119 get(m_pHighRB
, "high");
120 get(m_pMediumRB
, "med");
121 get(m_pLowRB
, "low");
123 m_pLowRB
->SetClickHdl( LINK( this, MacroSecurityLevelTP
, RadioButtonHdl
) );
124 m_pMediumRB
->SetClickHdl( LINK( this, MacroSecurityLevelTP
, RadioButtonHdl
) );
125 m_pHighRB
->SetClickHdl( LINK( this, MacroSecurityLevelTP
, RadioButtonHdl
) );
126 m_pVeryHighRB
->SetClickHdl( LINK( this, MacroSecurityLevelTP
, RadioButtonHdl
) );
128 mnCurLevel
= (sal_uInt16
) mpDlg
->maSecOptions
.GetMacroSecurityLevel();
129 bool bReadonly
= mpDlg
->maSecOptions
.IsReadOnly( SvtSecurityOptions::EOption::MacroSecLevel
);
131 RadioButton
* pCheck
= nullptr;
132 FixedImage
* pImage
= nullptr;
136 pCheck
= m_pVeryHighRB
;
137 pImage
= get
<FixedImage
>("vhighimg");
141 pImage
= get
<FixedImage
>("highimg");
144 pCheck
= m_pMediumRB
;
145 pImage
= get
<FixedImage
>("medimg");
149 pImage
= get
<FixedImage
>("lowimg");
156 OSL_FAIL("illegal macro security level");
158 if (bReadonly
&& pImage
)
161 m_pVeryHighRB
->Enable(false);
162 m_pHighRB
->Enable(false);
163 m_pMediumRB
->Enable(false);
164 m_pLowRB
->Enable(false);
168 MacroSecurityLevelTP::~MacroSecurityLevelTP()
173 void MacroSecurityLevelTP::dispose()
175 m_pVeryHighRB
.clear();
179 MacroSecurityTP::dispose();
182 IMPL_LINK_NOARG(MacroSecurityLevelTP
, RadioButtonHdl
, Button
*, void)
184 sal_uInt16 nNewLevel
= 0;
185 if( m_pVeryHighRB
->IsChecked() )
187 else if( m_pHighRB
->IsChecked() )
189 else if( m_pMediumRB
->IsChecked() )
192 if ( nNewLevel
!= mnCurLevel
)
194 mnCurLevel
= nNewLevel
;
195 mpDlg
->EnableReset();
199 void MacroSecurityLevelTP::ClosePage()
201 mpDlg
->maSecOptions
.SetMacroSecurityLevel( mnCurLevel
);
204 void MacroSecurityTrustedSourcesTP::ImplCheckButtons()
206 bool bCertSelected
= m_pTrustCertLB
->FirstSelected() != nullptr;
207 m_pViewCertPB
->Enable( bCertSelected
);
208 m_pRemoveCertPB
->Enable( bCertSelected
&& !mbAuthorsReadonly
);
210 bool bLocationSelected
= m_pTrustFileLocLB
->GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND
;
211 m_pRemoveLocPB
->Enable( bLocationSelected
&& !mbURLsReadonly
);
215 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, ViewCertPBHdl
, Button
*, void)
217 if( m_pTrustCertLB
->FirstSelected() )
219 sal_uInt16 nSelected
= sal_uInt16( sal_uIntPtr( m_pTrustCertLB
->FirstSelected()->GetUserData() ) );
221 uno::Reference
< css::security::XCertificate
> xCert
= mpDlg
->mxSecurityEnvironment
->getCertificate( maTrustedAuthors
[nSelected
][0], xmlsecurity::numericStringToBigInteger( maTrustedAuthors
[nSelected
][1] ) );
223 // If we don't get it, create it from signature data:
225 xCert
= mpDlg
->mxSecurityEnvironment
->createCertificateFromAscii( maTrustedAuthors
[nSelected
][2] ) ;
227 SAL_WARN_IF( !xCert
.is(), "xmlsecurity.dialogs", "*MacroSecurityTrustedSourcesTP::ViewCertPBHdl(): Certificate not found and can't be created!" );
231 ScopedVclPtrInstance
< CertificateViewer
> aViewer( this, mpDlg
->mxSecurityEnvironment
, xCert
, false );
237 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, RemoveCertPBHdl
, Button
*, void)
239 if( m_pTrustCertLB
->FirstSelected() )
241 sal_uInt16 nAuthor
= sal_uInt16( sal_uIntPtr( m_pTrustCertLB
->FirstSelected()->GetUserData() ) );
242 ::comphelper::removeElementAt( maTrustedAuthors
, nAuthor
);
249 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, AddLocPBHdl
, Button
*, void)
253 uno::Reference
< uno::XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
254 uno::Reference
< ui::dialogs::XFolderPicker2
> xFolderPicker
= ui::dialogs::FolderPicker::create(xContext
);
256 short nRet
= xFolderPicker
->execute();
258 if( ui::dialogs::ExecutableDialogResults::OK
!= nRet
)
261 OUString aPathStr
= xFolderPicker
->getDirectory();
262 INetURLObject
aNewObj( aPathStr
);
263 aNewObj
.removeFinalSlash();
265 // then the new path also an URL else system path
266 OUString aSystemFileURL
= ( aNewObj
.GetProtocol() != INetProtocol::NotValid
) ?
267 aPathStr
: aNewObj
.getFSysPath( FSysStyle::Detect
);
269 OUString
aNewPathStr(aSystemFileURL
);
271 if ( osl::FileBase::getSystemPathFromFileURL( aSystemFileURL
, aSystemFileURL
) == osl::FileBase::E_None
)
272 aNewPathStr
= aSystemFileURL
;
274 if( m_pTrustFileLocLB
->GetEntryPos( aNewPathStr
) == LISTBOX_ENTRY_NOTFOUND
)
276 m_pTrustFileLocLB
->InsertEntry( aNewPathStr
);
281 catch( uno::Exception
& )
283 SAL_WARN( "xmlsecurity.dialogs", "MacroSecurityTrustedSourcesTP::AddLocPBHdl(): exception from folder picker" );
287 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, RemoveLocPBHdl
, Button
*, void)
289 sal_Int32 nSel
= m_pTrustFileLocLB
->GetSelectEntryPos();
290 if( nSel
!= LISTBOX_ENTRY_NOTFOUND
)
292 m_pTrustFileLocLB
->RemoveEntry( nSel
);
293 // Trusted Path could not be removed (#i33584#)
294 // after remove an entry, select another one if exists
295 sal_Int32 nNewCount
= m_pTrustFileLocLB
->GetEntryCount();
298 if ( nSel
>= nNewCount
)
299 nSel
= nNewCount
- 1;
300 m_pTrustFileLocLB
->SelectEntryPos( nSel
);
306 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, TrustCertLBSelectHdl
, SvTreeListBox
*, void)
311 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, TrustFileLocLBSelectHdl
, ListBox
&, void)
316 void MacroSecurityTrustedSourcesTP::FillCertLB()
318 m_pTrustCertLB
->Clear();
320 sal_uInt32 nEntries
= maTrustedAuthors
.getLength();
322 if ( nEntries
&& mpDlg
->mxSecurityEnvironment
.is() )
324 for( sal_uInt32 nEntry
= 0 ; nEntry
< nEntries
; ++nEntry
)
326 css::uno::Sequence
< OUString
>& rEntry
= maTrustedAuthors
[ nEntry
];
327 uno::Reference
< css::security::XCertificate
> xCert
;
329 // create from RawData
330 xCert
= mpDlg
->mxSecurityEnvironment
->createCertificateFromAscii( rEntry
[ 2 ] );
332 SvTreeListEntry
* pLBEntry
= m_pTrustCertLB
->InsertEntry( XmlSec::GetContentPart( xCert
->getSubjectName() ) );
333 m_pTrustCertLB
->SetEntryText( XmlSec::GetContentPart( xCert
->getIssuerName() ), pLBEntry
, 1 );
334 m_pTrustCertLB
->SetEntryText( XmlSec::GetDateTimeString( xCert
->getNotValidAfter() ), pLBEntry
, 2 );
335 pLBEntry
->SetUserData( reinterpret_cast<void*>(nEntry
) ); // misuse user data as index
340 class TrustCertLB
: public SvSimpleTable
343 explicit TrustCertLB(SvSimpleTableContainer
&rContainer
)
344 : SvSimpleTable(rContainer
, 0)
347 virtual void Resize() override
349 SvSimpleTable::Resize();
350 if (isInitialLayout(this))
352 const long nControlWidth
= GetSizePixel().Width();
353 long aTabLocs
[] = { 3, 0, 35*nControlWidth
/100, 70*nControlWidth
/100 };
354 SvSimpleTable::SetTabs(aTabLocs
, MapUnit::MapPixel
);
359 MacroSecurityTrustedSourcesTP::MacroSecurityTrustedSourcesTP(vcl::Window
* _pParent
, MacroSecurity
* _pDlg
)
360 : MacroSecurityTP(_pParent
, "SecurityTrustPage", "xmlsec/ui/securitytrustpage.ui", _pDlg
)
362 get(m_pTrustCertROFI
, "lockcertimg");
363 get(m_pViewCertPB
, "viewcert");
364 get(m_pRemoveCertPB
, "removecert");
365 get(m_pTrustFileROFI
, "lockfileimg");
366 get(m_pTrustFileLocLB
, "locations");
367 m_pTrustFileLocLB
->SetDropDownLineCount(6);
368 get(m_pAddLocPB
, "addfile");
369 get(m_pRemoveLocPB
, "removefile");
371 SvSimpleTableContainer
*pCertificates
= get
<SvSimpleTableContainer
>("certificates");
372 m_pTrustCertLB
.reset(VclPtr
<TrustCertLB
>::Create(*pCertificates
));
373 static long aTabs
[] = { 3, 0, 0, 0 };
374 m_pTrustCertLB
->SetTabs( aTabs
);
376 m_pTrustCertLB
->InsertHeaderEntry(get
<FixedText
>("to")->GetText() + "\t"
377 + get
<FixedText
>("by")->GetText() + "\t" + get
<FixedText
>("date")->GetText());
379 m_pTrustCertLB
->SetSelectHdl( LINK( this, MacroSecurityTrustedSourcesTP
, TrustCertLBSelectHdl
) );
380 m_pViewCertPB
->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP
, ViewCertPBHdl
) );
381 m_pViewCertPB
->Disable();
382 m_pRemoveCertPB
->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP
, RemoveCertPBHdl
) );
383 m_pRemoveCertPB
->Disable();
385 m_pTrustFileLocLB
->SetSelectHdl( LINK( this, MacroSecurityTrustedSourcesTP
, TrustFileLocLBSelectHdl
) );
386 m_pAddLocPB
->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP
, AddLocPBHdl
) );
387 m_pRemoveLocPB
->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP
, RemoveLocPBHdl
) );
388 m_pRemoveLocPB
->Disable();
390 maTrustedAuthors
= mpDlg
->maSecOptions
.GetTrustedAuthors();
391 mbAuthorsReadonly
= mpDlg
->maSecOptions
.IsReadOnly( SvtSecurityOptions::EOption::MacroTrustedAuthors
);
392 m_pTrustCertROFI
->Show( mbAuthorsReadonly
);
393 mbAuthorsReadonly
? m_pTrustCertLB
->DisableTable() : m_pTrustCertLB
->EnableTable();
397 css::uno::Sequence
< OUString
> aSecureURLs
= mpDlg
->maSecOptions
.GetSecureURLs();
398 mbURLsReadonly
= mpDlg
->maSecOptions
.IsReadOnly( SvtSecurityOptions::EOption::SecureUrls
);
399 m_pTrustFileROFI
->Show( mbURLsReadonly
);
400 m_pTrustFileLocLB
->Enable( !mbURLsReadonly
);
401 m_pAddLocPB
->Enable( !mbURLsReadonly
);
403 sal_Int32 nEntryCnt
= aSecureURLs
.getLength();
404 for( sal_Int32 i
= 0 ; i
< nEntryCnt
; ++i
)
406 OUString
aSystemFileURL( aSecureURLs
[ i
] );
407 osl::FileBase::getSystemPathFromFileURL( aSystemFileURL
, aSystemFileURL
);
408 m_pTrustFileLocLB
->InsertEntry( aSystemFileURL
);
412 MacroSecurityTrustedSourcesTP::~MacroSecurityTrustedSourcesTP()
417 void MacroSecurityTrustedSourcesTP::dispose()
419 m_pTrustCertLB
.disposeAndClear();
420 m_pTrustCertROFI
.clear();
421 m_pViewCertPB
.clear();
422 m_pRemoveCertPB
.clear();
423 m_pTrustFileROFI
.clear();
424 m_pTrustFileLocLB
.clear();
426 m_pRemoveLocPB
.clear();
427 MacroSecurityTP::dispose();
430 void MacroSecurityTrustedSourcesTP::ActivatePage()
432 mpDlg
->EnableReset( false );
436 void MacroSecurityTrustedSourcesTP::ClosePage()
438 sal_Int32 nEntryCnt
= m_pTrustFileLocLB
->GetEntryCount();
441 css::uno::Sequence
< OUString
> aSecureURLs( nEntryCnt
);
442 for( sal_Int32 i
= 0 ; i
< nEntryCnt
; ++i
)
444 OUString
aURL( m_pTrustFileLocLB
->GetEntry( i
) );
445 osl::FileBase::getFileURLFromSystemPath( aURL
, aURL
);
446 aSecureURLs
[ i
] = aURL
;
449 mpDlg
->maSecOptions
.SetSecureURLs( aSecureURLs
);
451 // Trusted Path could not be removed (#i33584#)
452 // don't forget to remove the old saved SecureURLs
454 mpDlg
->maSecOptions
.SetSecureURLs( css::uno::Sequence
< OUString
>() );
456 mpDlg
->maSecOptions
.SetTrustedAuthors( maTrustedAuthors
);
459 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */