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 <xmlsecurity/macrosecurity.hxx>
22 #include <xmlsecurity/certificatechooser.hxx>
23 #include <xmlsecurity/certificateviewer.hxx>
24 #include <xmlsecurity/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 <com/sun/star/security/SerialNumberAdapter.hpp>
33 #include <comphelper/sequence.hxx>
34 #include <sfx2/filedlghelper.hxx>
35 #include <comphelper/processfactory.hxx>
36 #include <com/sun/star/uno/Exception.hpp>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
39 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
40 #include <tools/urlobj.hxx>
41 #include <svtools/treelistentry.hxx>
43 #include <vcl/msgbox.hxx>
45 #include "dialogs.hrc"
46 #include "resourcemanager.hxx"
48 using namespace ::com::sun::star
;
51 IMPL_LINK_NOARG(MacroSecurity
, OkBtnHdl
)
53 mpLevelTP
->ClosePage();
54 mpTrustSrcTP
->ClosePage();
61 MacroSecurity::MacroSecurity( vcl::Window
* _pParent
,
62 const css::uno::Reference
< css::uno::XComponentContext
> &_rxCtx
,
63 const css::uno::Reference
< css::xml::crypto::XSecurityEnvironment
>& _rxSecurityEnvironment
)
64 : TabDialog(_pParent
, "MacroSecurityDialog", "xmlsec/ui/macrosecuritydialog.ui")
66 , mxSecurityEnvironment(_rxSecurityEnvironment
)
68 get(m_pTabCtrl
, "tabcontrol");
69 get(m_pResetBtn
, "reset");
72 mpLevelTP
.reset(VclPtr
<MacroSecurityLevelTP
>::Create(m_pTabCtrl
, this));
73 mpTrustSrcTP
.reset(VclPtr
<MacroSecurityTrustedSourcesTP
>::Create(m_pTabCtrl
, this));
75 m_nSecLevelId
= m_pTabCtrl
->GetPageId("SecurityLevelPage");
76 m_nSecTrustId
= m_pTabCtrl
->GetPageId("SecurityTrustPage");
78 m_pTabCtrl
->SetTabPage(m_nSecLevelId
, mpLevelTP
);
79 m_pTabCtrl
->SetTabPage(m_nSecTrustId
, mpTrustSrcTP
);
80 m_pTabCtrl
->SetCurPageId(m_nSecLevelId
);
82 m_pOkBtn
->SetClickHdl( LINK( this, MacroSecurity
, OkBtnHdl
) );
85 MacroSecurity::~MacroSecurity()
90 void MacroSecurity::dispose()
92 m_pTabCtrl
->GetTabPage(m_nSecTrustId
)->disposeOnce();
93 m_pTabCtrl
->GetTabPage(m_nSecLevelId
)->disposeOnce();
97 mpLevelTP
.disposeAndClear();
98 mpTrustSrcTP
.disposeAndClear();
102 MacroSecurityTP::MacroSecurityTP(vcl::Window
* _pParent
, const OString
& rID
,
103 const OUString
& rUIXMLDescription
, MacroSecurity
* _pDlg
)
104 : TabPage(_pParent
, rID
, rUIXMLDescription
)
109 MacroSecurityTP::~MacroSecurityTP()
114 void MacroSecurityTP::dispose()
120 MacroSecurityLevelTP::MacroSecurityLevelTP(vcl::Window
* _pParent
, MacroSecurity
* _pDlg
)
121 : MacroSecurityTP(_pParent
, "SecurityLevelPage", "xmlsec/ui/securitylevelpage.ui", _pDlg
)
123 get(m_pVeryHighRB
, "vhigh");
124 get(m_pHighRB
, "high");
125 get(m_pMediumRB
, "med");
126 get(m_pLowRB
, "low");
128 m_pLowRB
->SetClickHdl( LINK( this, MacroSecurityLevelTP
, RadioButtonHdl
) );
129 m_pMediumRB
->SetClickHdl( LINK( this, MacroSecurityLevelTP
, RadioButtonHdl
) );
130 m_pHighRB
->SetClickHdl( LINK( this, MacroSecurityLevelTP
, RadioButtonHdl
) );
131 m_pVeryHighRB
->SetClickHdl( LINK( this, MacroSecurityLevelTP
, RadioButtonHdl
) );
133 mnCurLevel
= (sal_uInt16
) mpDlg
->maSecOptions
.GetMacroSecurityLevel();
134 bool bReadonly
= mpDlg
->maSecOptions
.IsReadOnly( SvtSecurityOptions::E_MACRO_SECLEVEL
);
136 RadioButton
* pCheck
= 0;
137 FixedImage
* pImage
= 0;
141 pCheck
= m_pVeryHighRB
;
142 pImage
= get
<FixedImage
>("vhighimg");
146 pImage
= get
<FixedImage
>("highimg");
149 pCheck
= m_pMediumRB
;
150 pImage
= get
<FixedImage
>("medimg");
154 pImage
= get
<FixedImage
>("lowimg");
161 OSL_FAIL("illegal macro security level");
163 if (bReadonly
&& pImage
)
166 m_pVeryHighRB
->Enable(false);
167 m_pHighRB
->Enable(false);
168 m_pMediumRB
->Enable(false);
169 m_pLowRB
->Enable(false);
173 MacroSecurityLevelTP::~MacroSecurityLevelTP()
178 void MacroSecurityLevelTP::dispose()
180 m_pVeryHighRB
.clear();
184 MacroSecurityTP::dispose();
187 IMPL_LINK_NOARG(MacroSecurityLevelTP
, RadioButtonHdl
)
189 sal_uInt16 nNewLevel
= 0;
190 if( m_pVeryHighRB
->IsChecked() )
192 else if( m_pHighRB
->IsChecked() )
194 else if( m_pMediumRB
->IsChecked() )
197 if ( nNewLevel
!= mnCurLevel
)
199 mnCurLevel
= nNewLevel
;
200 mpDlg
->EnableReset();
206 void MacroSecurityLevelTP::ClosePage()
208 mpDlg
->maSecOptions
.SetMacroSecurityLevel( mnCurLevel
);
211 void MacroSecurityTrustedSourcesTP::ImplCheckButtons()
213 bool bCertSelected
= m_pTrustCertLB
->FirstSelected() != NULL
;
214 m_pViewCertPB
->Enable( bCertSelected
);
215 m_pRemoveCertPB
->Enable( bCertSelected
&& !mbAuthorsReadonly
);
217 bool bLocationSelected
= m_pTrustFileLocLB
->GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND
;
218 m_pRemoveLocPB
->Enable( bLocationSelected
&& !mbURLsReadonly
);
222 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, ViewCertPBHdl
)
224 if( m_pTrustCertLB
->FirstSelected() )
226 sal_uInt16 nSelected
= sal_uInt16( sal_uIntPtr( m_pTrustCertLB
->FirstSelected()->GetUserData() ) );
228 uno::Reference
< css::security::XSerialNumberAdapter
> xSerialNumberAdapter
=
229 ::com::sun::star::security::SerialNumberAdapter::create(mpDlg
->mxCtx
);
231 uno::Reference
< css::security::XCertificate
> xCert
= mpDlg
->mxSecurityEnvironment
->getCertificate( maTrustedAuthors
[nSelected
][0], xSerialNumberAdapter
->toSequence( maTrustedAuthors
[nSelected
][1] ) );
233 // If we don't get it, create it from signature data:
235 xCert
= mpDlg
->mxSecurityEnvironment
->createCertificateFromAscii( maTrustedAuthors
[nSelected
][2] ) ;
237 DBG_ASSERT( xCert
.is(), "*MacroSecurityTrustedSourcesTP::ViewCertPBHdl(): Certificate not found and can't be created!" );
241 ScopedVclPtrInstance
< CertificateViewer
> aViewer( this, mpDlg
->mxSecurityEnvironment
, xCert
, false );
248 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, RemoveCertPBHdl
)
250 if( m_pTrustCertLB
->FirstSelected() )
252 sal_uInt16 nAuthor
= sal_uInt16( sal_uIntPtr( m_pTrustCertLB
->FirstSelected()->GetUserData() ) );
253 ::comphelper::removeElementAt( maTrustedAuthors
, nAuthor
);
262 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, AddLocPBHdl
)
266 uno::Reference
< uno::XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
267 uno::Reference
< ui::dialogs::XFolderPicker2
> xFolderPicker
= ui::dialogs::FolderPicker::create(xContext
);
269 short nRet
= xFolderPicker
->execute();
271 if( ui::dialogs::ExecutableDialogResults::OK
!= nRet
)
274 OUString aPathStr
= xFolderPicker
->getDirectory();
275 INetURLObject
aNewObj( aPathStr
);
276 aNewObj
.removeFinalSlash();
278 // then the new path also an URL else system path
279 OUString aSystemFileURL
= ( aNewObj
.GetProtocol() != INetProtocol::NotValid
) ?
280 aPathStr
: aNewObj
.getFSysPath( INetURLObject::FSYS_DETECT
);
282 OUString
aNewPathStr(aSystemFileURL
);
284 if ( osl::FileBase::getSystemPathFromFileURL( aSystemFileURL
, aSystemFileURL
) == osl::FileBase::E_None
)
285 aNewPathStr
= aSystemFileURL
;
287 if( m_pTrustFileLocLB
->GetEntryPos( aNewPathStr
) == LISTBOX_ENTRY_NOTFOUND
)
289 m_pTrustFileLocLB
->InsertEntry( aNewPathStr
);
294 catch( uno::Exception
& )
296 SAL_WARN( "xmlsecurity.dialogs", "MacroSecurityTrustedSourcesTP::AddLocPBHdl(): exception from folder picker" );
302 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, RemoveLocPBHdl
)
304 sal_Int32 nSel
= m_pTrustFileLocLB
->GetSelectEntryPos();
305 if( nSel
!= LISTBOX_ENTRY_NOTFOUND
)
307 m_pTrustFileLocLB
->RemoveEntry( nSel
);
308 // Trusted Path could not be removed (#i33584#)
309 // after remove an entry, select another one if exists
310 sal_Int32 nNewCount
= m_pTrustFileLocLB
->GetEntryCount();
313 if ( nSel
>= nNewCount
)
314 nSel
= nNewCount
- 1;
315 m_pTrustFileLocLB
->SelectEntryPos( nSel
);
323 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, TrustCertLBSelectHdl
)
329 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, TrustFileLocLBSelectHdl
)
335 void MacroSecurityTrustedSourcesTP::FillCertLB()
337 m_pTrustCertLB
->Clear();
339 sal_uInt32 nEntries
= maTrustedAuthors
.getLength();
341 if ( nEntries
&& mpDlg
->mxSecurityEnvironment
.is() )
343 for( sal_uInt32 nEntry
= 0 ; nEntry
< nEntries
; ++nEntry
)
345 css::uno::Sequence
< OUString
>& rEntry
= maTrustedAuthors
[ nEntry
];
346 uno::Reference
< css::security::XCertificate
> xCert
;
348 // create from RawData
349 xCert
= mpDlg
->mxSecurityEnvironment
->createCertificateFromAscii( rEntry
[ 2 ] );
351 SvTreeListEntry
* pLBEntry
= m_pTrustCertLB
->InsertEntry( XmlSec::GetContentPart( xCert
->getSubjectName() ) );
352 m_pTrustCertLB
->SetEntryText( XmlSec::GetContentPart( xCert
->getIssuerName() ), pLBEntry
, 1 );
353 m_pTrustCertLB
->SetEntryText( XmlSec::GetDateTimeString( xCert
->getNotValidAfter() ), pLBEntry
, 2 );
354 pLBEntry
->SetUserData( reinterpret_cast<void*>(nEntry
) ); // missuse user data as index
359 class TrustCertLB
: public SvSimpleTable
362 TrustCertLB(SvSimpleTableContainer
&rContainer
)
363 : SvSimpleTable(rContainer
, 0)
366 virtual void Resize() SAL_OVERRIDE
368 SvSimpleTable::Resize();
369 if (isInitialLayout(this))
371 const long nControlWidth
= GetSizePixel().Width();
372 long aTabLocs
[] = { 3, 0, 35*nControlWidth
/100, 70*nControlWidth
/100 };
373 SvSimpleTable::SetTabs(aTabLocs
, MAP_PIXEL
);
378 MacroSecurityTrustedSourcesTP::MacroSecurityTrustedSourcesTP(vcl::Window
* _pParent
, MacroSecurity
* _pDlg
)
379 : MacroSecurityTP(_pParent
, "SecurityTrustPage", "xmlsec/ui/securitytrustpage.ui", _pDlg
)
381 get(m_pTrustCertROFI
, "lockcertimg");
382 get(m_pViewCertPB
, "viewcert");
383 get(m_pRemoveCertPB
, "removecert");
384 get(m_pTrustFileROFI
, "lockfileimg");
385 get(m_pTrustFileLocLB
, "locations");
386 m_pTrustFileLocLB
->SetDropDownLineCount(6);
387 get(m_pAddLocPB
, "addfile");
388 get(m_pRemoveLocPB
, "removefile");
390 SvSimpleTableContainer
*pCertificates
= get
<SvSimpleTableContainer
>("certificates");
391 m_pTrustCertLB
.reset(VclPtr
<TrustCertLB
>::Create(*pCertificates
));
392 static long aTabs
[] = { 3, 0, 0, 0 };
393 m_pTrustCertLB
->SetTabs( aTabs
);
395 m_pTrustCertLB
->InsertHeaderEntry(get
<FixedText
>("to")->GetText() + "\t"
396 + get
<FixedText
>("by")->GetText() + "\t" + get
<FixedText
>("date")->GetText());
398 m_pTrustCertLB
->SetSelectHdl( LINK( this, MacroSecurityTrustedSourcesTP
, TrustCertLBSelectHdl
) );
399 m_pViewCertPB
->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP
, ViewCertPBHdl
) );
400 m_pViewCertPB
->Disable();
401 m_pRemoveCertPB
->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP
, RemoveCertPBHdl
) );
402 m_pRemoveCertPB
->Disable();
404 m_pTrustFileLocLB
->SetSelectHdl( LINK( this, MacroSecurityTrustedSourcesTP
, TrustFileLocLBSelectHdl
) );
405 m_pAddLocPB
->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP
, AddLocPBHdl
) );
406 m_pRemoveLocPB
->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP
, RemoveLocPBHdl
) );
407 m_pRemoveLocPB
->Disable();
409 maTrustedAuthors
= mpDlg
->maSecOptions
.GetTrustedAuthors();
410 mbAuthorsReadonly
= mpDlg
->maSecOptions
.IsReadOnly( SvtSecurityOptions::E_MACRO_TRUSTEDAUTHORS
);
411 m_pTrustCertROFI
->Show( mbAuthorsReadonly
);
412 mbAuthorsReadonly
? m_pTrustCertLB
->DisableTable() : m_pTrustCertLB
->EnableTable();
416 css::uno::Sequence
< OUString
> aSecureURLs
= mpDlg
->maSecOptions
.GetSecureURLs();
417 mbURLsReadonly
= mpDlg
->maSecOptions
.IsReadOnly( SvtSecurityOptions::E_SECUREURLS
);
418 m_pTrustFileROFI
->Show( mbURLsReadonly
);
419 m_pTrustFileLocLB
->Enable( !mbURLsReadonly
);
420 m_pAddLocPB
->Enable( !mbURLsReadonly
);
422 sal_Int32 nEntryCnt
= aSecureURLs
.getLength();
423 for( sal_Int32 i
= 0 ; i
< nEntryCnt
; ++i
)
425 OUString
aSystemFileURL( aSecureURLs
[ i
] );
426 osl::FileBase::getSystemPathFromFileURL( aSystemFileURL
, aSystemFileURL
);
427 m_pTrustFileLocLB
->InsertEntry( aSystemFileURL
);
431 MacroSecurityTrustedSourcesTP::~MacroSecurityTrustedSourcesTP()
436 void MacroSecurityTrustedSourcesTP::dispose()
438 m_pTrustCertLB
.disposeAndClear();
439 m_pTrustCertROFI
.clear();
440 m_pViewCertPB
.clear();
441 m_pRemoveCertPB
.clear();
442 m_pTrustFileROFI
.clear();
443 m_pTrustFileLocLB
.clear();
445 m_pRemoveLocPB
.clear();
446 MacroSecurityTP::dispose();
449 void MacroSecurityTrustedSourcesTP::ActivatePage()
451 mpDlg
->EnableReset( false );
455 void MacroSecurityTrustedSourcesTP::ClosePage()
457 sal_Int32 nEntryCnt
= m_pTrustFileLocLB
->GetEntryCount();
460 css::uno::Sequence
< OUString
> aSecureURLs( nEntryCnt
);
461 for( sal_Int32 i
= 0 ; i
< nEntryCnt
; ++i
)
463 OUString
aURL( m_pTrustFileLocLB
->GetEntry( i
) );
464 osl::FileBase::getFileURLFromSystemPath( aURL
, aURL
);
465 aSecureURLs
[ i
] = aURL
;
468 mpDlg
->maSecOptions
.SetSecureURLs( aSecureURLs
);
470 // Trusted Path could not be removed (#i33584#)
471 // don't forget to remove the old saved SecureURLs
473 mpDlg
->maSecOptions
.SetSecureURLs( css::uno::Sequence
< OUString
>() );
475 mpDlg
->maSecOptions
.SetTrustedAuthors( maTrustedAuthors
);
478 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */