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 /* HACK: disable some warnings for MS-C */
50 #pragma warning (disable : 4355) // 4355: this used in initializer-list
53 using namespace ::com::sun::star
;
56 IMPL_LINK_NOARG(MacroSecurity
, OkBtnHdl
)
58 mpLevelTP
->ClosePage();
59 mpTrustSrcTP
->ClosePage();
66 MacroSecurity::MacroSecurity( Window
* _pParent
,
67 const css::uno::Reference
< css::uno::XComponentContext
> &_rxCtx
,
68 const css::uno::Reference
< css::xml::crypto::XSecurityEnvironment
>& _rxSecurityEnvironment
)
69 : TabDialog(_pParent
, "MacroSecurityDialog", "xmlsec/ui/macrosecuritydialog.ui")
71 , mxSecurityEnvironment(_rxSecurityEnvironment
)
73 get(m_pTabCtrl
, "tabcontrol");
74 get(m_pResetBtn
, "reset");
77 mpLevelTP
= new MacroSecurityLevelTP(m_pTabCtrl
, this);
78 mpTrustSrcTP
= new MacroSecurityTrustedSourcesTP(m_pTabCtrl
, this);
80 m_nSecLevelId
= m_pTabCtrl
->GetPageId("SecurityLevelPage");
81 m_nSecTrustId
= m_pTabCtrl
->GetPageId("SecurityTrustPage");
83 m_pTabCtrl
->SetTabPage(m_nSecLevelId
, mpLevelTP
);
84 m_pTabCtrl
->SetTabPage(m_nSecTrustId
, mpTrustSrcTP
);
85 m_pTabCtrl
->SetCurPageId(m_nSecLevelId
);
87 m_pOkBtn
->SetClickHdl( LINK( this, MacroSecurity
, OkBtnHdl
) );
90 MacroSecurity::~MacroSecurity()
92 delete m_pTabCtrl
->GetTabPage(m_nSecTrustId
);
93 delete m_pTabCtrl
->GetTabPage(m_nSecLevelId
);
96 MacroSecurityTP::MacroSecurityTP(Window
* _pParent
, const OString
& rID
,
97 const OUString
& rUIXMLDescription
, MacroSecurity
* _pDlg
)
98 : TabPage(_pParent
, rID
, rUIXMLDescription
)
103 MacroSecurityLevelTP::MacroSecurityLevelTP(Window
* _pParent
, MacroSecurity
* _pDlg
)
104 : MacroSecurityTP(_pParent
, "SecurityLevelPage", "xmlsec/ui/securitylevelpage.ui", _pDlg
)
106 get(m_pVeryHighRB
, "vhigh");
107 get(m_pHighRB
, "high");
108 get(m_pMediumRB
, "med");
109 get(m_pLowRB
, "low");
111 m_pLowRB
->SetClickHdl( LINK( this, MacroSecurityLevelTP
, RadioButtonHdl
) );
112 m_pMediumRB
->SetClickHdl( LINK( this, MacroSecurityLevelTP
, RadioButtonHdl
) );
113 m_pHighRB
->SetClickHdl( LINK( this, MacroSecurityLevelTP
, RadioButtonHdl
) );
114 m_pVeryHighRB
->SetClickHdl( LINK( this, MacroSecurityLevelTP
, RadioButtonHdl
) );
116 mnCurLevel
= (sal_uInt16
) mpDlg
->maSecOptions
.GetMacroSecurityLevel();
117 sal_Bool bReadonly
= mpDlg
->maSecOptions
.IsReadOnly( SvtSecurityOptions::E_MACRO_SECLEVEL
);
119 RadioButton
* pCheck
= 0;
120 FixedImage
* pImage
= 0;
124 pCheck
= m_pVeryHighRB
;
125 pImage
= get
<FixedImage
>("vhighimg");
129 pImage
= get
<FixedImage
>("highimg");
132 pCheck
= m_pMediumRB
;
133 pImage
= get
<FixedImage
>("medimg");
137 pImage
= get
<FixedImage
>("lowimg");
144 OSL_FAIL("illegal macro security level");
149 m_pVeryHighRB
->Enable(false);
150 m_pHighRB
->Enable(false);
151 m_pMediumRB
->Enable(false);
152 m_pLowRB
->Enable(false);
156 IMPL_LINK_NOARG(MacroSecurityLevelTP
, RadioButtonHdl
)
158 sal_uInt16 nNewLevel
= 0;
159 if( m_pVeryHighRB
->IsChecked() )
161 else if( m_pHighRB
->IsChecked() )
163 else if( m_pMediumRB
->IsChecked() )
166 if ( nNewLevel
!= mnCurLevel
)
168 mnCurLevel
= nNewLevel
;
169 mpDlg
->EnableReset();
175 void MacroSecurityLevelTP::ClosePage( void )
177 mpDlg
->maSecOptions
.SetMacroSecurityLevel( mnCurLevel
);
180 void MacroSecurityTrustedSourcesTP::ImplCheckButtons()
182 bool bCertSelected
= m_pTrustCertLB
->FirstSelected() != NULL
;
183 m_pViewCertPB
->Enable( bCertSelected
);
184 m_pRemoveCertPB
->Enable( bCertSelected
&& !mbAuthorsReadonly
);
186 bool bLocationSelected
= m_pTrustFileLocLB
->GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND
;
187 m_pRemoveLocPB
->Enable( bLocationSelected
&& !mbURLsReadonly
);
191 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, ViewCertPBHdl
)
193 if( m_pTrustCertLB
->FirstSelected() )
195 sal_uInt16 nSelected
= sal_uInt16( sal_uIntPtr( m_pTrustCertLB
->FirstSelected()->GetUserData() ) );
197 uno::Reference
< css::security::XSerialNumberAdapter
> xSerialNumberAdapter
=
198 ::com::sun::star::security::SerialNumberAdapter::create(mpDlg
->mxCtx
);
200 uno::Reference
< css::security::XCertificate
> xCert
= mpDlg
->mxSecurityEnvironment
->getCertificate( maTrustedAuthors
[nSelected
][0], xSerialNumberAdapter
->toSequence( maTrustedAuthors
[nSelected
][1] ) );
202 // If we don't get it, create it from signature data:
204 xCert
= mpDlg
->mxSecurityEnvironment
->createCertificateFromAscii( maTrustedAuthors
[nSelected
][2] ) ;
206 DBG_ASSERT( xCert
.is(), "*MacroSecurityTrustedSourcesTP::ViewCertPBHdl(): Certificate not found and can't be created!" );
210 CertificateViewer
aViewer( this, mpDlg
->mxSecurityEnvironment
, xCert
, false );
217 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, RemoveCertPBHdl
)
219 if( m_pTrustCertLB
->FirstSelected() )
221 sal_uInt16 nAuthor
= sal_uInt16( sal_uIntPtr( m_pTrustCertLB
->FirstSelected()->GetUserData() ) );
222 ::comphelper::removeElementAt( maTrustedAuthors
, nAuthor
);
231 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, AddLocPBHdl
)
235 uno::Reference
< uno::XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
236 uno::Reference
< ui::dialogs::XFolderPicker2
> xFolderPicker
= ui::dialogs::FolderPicker::create(xContext
);
238 short nRet
= xFolderPicker
->execute();
240 if( ui::dialogs::ExecutableDialogResults::OK
!= nRet
)
243 OUString aPathStr
= xFolderPicker
->getDirectory();
244 INetURLObject
aNewObj( aPathStr
);
245 aNewObj
.removeFinalSlash();
247 // then the new path also an URL else system path
248 OUString aSystemFileURL
= ( aNewObj
.GetProtocol() != INET_PROT_NOT_VALID
) ?
249 aPathStr
: aNewObj
.getFSysPath( INetURLObject::FSYS_DETECT
);
251 OUString
aNewPathStr(aSystemFileURL
);
253 if ( osl::FileBase::getSystemPathFromFileURL( aSystemFileURL
, aSystemFileURL
) == osl::FileBase::E_None
)
254 aNewPathStr
= aSystemFileURL
;
256 if( m_pTrustFileLocLB
->GetEntryPos( aNewPathStr
) == LISTBOX_ENTRY_NOTFOUND
)
258 m_pTrustFileLocLB
->InsertEntry( aNewPathStr
);
263 catch( uno::Exception
& )
265 SAL_WARN( "xmlsecurity.dialogs", "MacroSecurityTrustedSourcesTP::AddLocPBHdl(): exception from folder picker" );
271 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, RemoveLocPBHdl
)
273 sal_uInt16 nSel
= m_pTrustFileLocLB
->GetSelectEntryPos();
274 if( nSel
!= LISTBOX_ENTRY_NOTFOUND
)
276 m_pTrustFileLocLB
->RemoveEntry( nSel
);
277 // Trusted Path could not be removed (#i33584#)
278 // after remove an entry, select another one if exists
279 sal_uInt16 nNewCount
= m_pTrustFileLocLB
->GetEntryCount();
282 if ( nSel
>= nNewCount
)
283 nSel
= nNewCount
- 1;
284 m_pTrustFileLocLB
->SelectEntryPos( nSel
);
292 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, TrustCertLBSelectHdl
)
298 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP
, TrustFileLocLBSelectHdl
)
304 void MacroSecurityTrustedSourcesTP::FillCertLB( void )
306 m_pTrustCertLB
->Clear();
308 sal_uInt32 nEntries
= maTrustedAuthors
.getLength();
310 if ( nEntries
&& mpDlg
->mxSecurityEnvironment
.is() )
312 for( sal_uInt32 nEntry
= 0 ; nEntry
< nEntries
; ++nEntry
)
314 css::uno::Sequence
< OUString
>& rEntry
= maTrustedAuthors
[ nEntry
];
315 uno::Reference
< css::security::XCertificate
> xCert
;
317 // create from RawData
318 xCert
= mpDlg
->mxSecurityEnvironment
->createCertificateFromAscii( rEntry
[ 2 ] );
320 SvTreeListEntry
* pLBEntry
= m_pTrustCertLB
->InsertEntry( XmlSec::GetContentPart( xCert
->getSubjectName() ) );
321 m_pTrustCertLB
->SetEntryText( XmlSec::GetContentPart( xCert
->getIssuerName() ), pLBEntry
, 1 );
322 m_pTrustCertLB
->SetEntryText( XmlSec::GetDateTimeString( xCert
->getNotValidAfter() ), pLBEntry
, 2 );
323 pLBEntry
->SetUserData( ( void* ) (sal_IntPtr
)nEntry
); // missuse user data as index
328 class TrustCertLB
: public SvxSimpleTable
331 TrustCertLB(SvxSimpleTableContainer
&rContainer
)
332 : SvxSimpleTable(rContainer
, 0)
335 virtual void Resize()
337 SvxSimpleTable::Resize();
338 if (isInitialLayout(this))
340 const long nControlWidth
= GetSizePixel().Width();
341 long aTabLocs
[] = { 3, 0, 35*nControlWidth
/100, 70*nControlWidth
/100 };
342 SvxSimpleTable::SetTabs(aTabLocs
, MAP_PIXEL
);
347 MacroSecurityTrustedSourcesTP::MacroSecurityTrustedSourcesTP(Window
* _pParent
, MacroSecurity
* _pDlg
)
348 : MacroSecurityTP(_pParent
, "SecurityTrustPage", "xmlsec/ui/securitytrustpage.ui", _pDlg
)
350 get(m_pTrustCertROFI
, "lockcertimg");
351 get(m_pViewCertPB
, "viewcert");
352 get(m_pRemoveCertPB
, "removecert");
353 get(m_pTrustFileROFI
, "lockfileimg");
354 get(m_pTrustFileLocLB
, "locations");
355 m_pTrustFileLocLB
->SetDropDownLineCount(6);
356 get(m_pAddLocPB
, "addfile");
357 get(m_pRemoveLocPB
, "removefile");
359 SvxSimpleTableContainer
*pCertificates
= get
<SvxSimpleTableContainer
>("certificates");
360 m_pTrustCertLB
= new TrustCertLB(*pCertificates
);
361 static long aTabs
[] = { 3, 0, 0, 0 };
362 m_pTrustCertLB
->SetTabs( aTabs
);
364 OUStringBuffer
aBuf(get
<FixedText
>("to")->GetText());
365 aBuf
.append("\t").append(get
<FixedText
>("by")->GetText())
366 .append("\t").append(get
<FixedText
>("date")->GetText());
367 m_pTrustCertLB
->InsertHeaderEntry(aBuf
.makeStringAndClear());
369 m_pTrustCertLB
->SetSelectHdl( LINK( this, MacroSecurityTrustedSourcesTP
, TrustCertLBSelectHdl
) );
370 m_pViewCertPB
->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP
, ViewCertPBHdl
) );
371 m_pViewCertPB
->Disable();
372 m_pRemoveCertPB
->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP
, RemoveCertPBHdl
) );
373 m_pRemoveCertPB
->Disable();
375 m_pTrustFileLocLB
->SetSelectHdl( LINK( this, MacroSecurityTrustedSourcesTP
, TrustFileLocLBSelectHdl
) );
376 m_pAddLocPB
->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP
, AddLocPBHdl
) );
377 m_pRemoveLocPB
->SetClickHdl( LINK( this, MacroSecurityTrustedSourcesTP
, RemoveLocPBHdl
) );
378 m_pRemoveLocPB
->Disable();
380 maTrustedAuthors
= mpDlg
->maSecOptions
.GetTrustedAuthors();
381 mbAuthorsReadonly
= mpDlg
->maSecOptions
.IsReadOnly( SvtSecurityOptions::E_MACRO_TRUSTEDAUTHORS
);
382 m_pTrustCertROFI
->Show( mbAuthorsReadonly
);
383 mbAuthorsReadonly
? m_pTrustCertLB
->DisableTable() : m_pTrustCertLB
->EnableTable();
387 css::uno::Sequence
< OUString
> aSecureURLs
= mpDlg
->maSecOptions
.GetSecureURLs();
388 mbURLsReadonly
= mpDlg
->maSecOptions
.IsReadOnly( SvtSecurityOptions::E_SECUREURLS
);
389 m_pTrustFileROFI
->Show( mbURLsReadonly
);
390 m_pTrustFileLocLB
->Enable( !mbURLsReadonly
);
391 m_pAddLocPB
->Enable( !mbURLsReadonly
);
393 sal_Int32 nEntryCnt
= aSecureURLs
.getLength();
394 for( sal_Int32 i
= 0 ; i
< nEntryCnt
; ++i
)
396 OUString
aSystemFileURL( aSecureURLs
[ i
] );
397 osl::FileBase::getSystemPathFromFileURL( aSystemFileURL
, aSystemFileURL
);
398 m_pTrustFileLocLB
->InsertEntry( aSystemFileURL
);
402 MacroSecurityTrustedSourcesTP::~MacroSecurityTrustedSourcesTP()
404 delete m_pTrustCertLB
;
407 void MacroSecurityTrustedSourcesTP::ActivatePage()
409 mpDlg
->EnableReset( false );
413 void MacroSecurityTrustedSourcesTP::ClosePage( void )
415 sal_uInt16 nEntryCnt
= m_pTrustFileLocLB
->GetEntryCount();
418 css::uno::Sequence
< OUString
> aSecureURLs( nEntryCnt
);
419 for( sal_uInt16 i
= 0 ; i
< nEntryCnt
; ++i
)
421 OUString
aURL( m_pTrustFileLocLB
->GetEntry( i
) );
422 osl::FileBase::getFileURLFromSystemPath( aURL
, aURL
);
423 aSecureURLs
[ i
] = aURL
;
426 mpDlg
->maSecOptions
.SetSecureURLs( aSecureURLs
);
428 // Trusted Path could not be removed (#i33584#)
429 // don't forget to remove the old saved SecureURLs
431 mpDlg
->maSecOptions
.SetSecureURLs( css::uno::Sequence
< OUString
>() );
433 mpDlg
->maSecOptions
.SetTrustedAuthors( maTrustedAuthors
);
436 XmlReadOnlyImage::XmlReadOnlyImage(Window
* pParent
, const ResId rResId
) :
437 FixedImage(pParent
, rResId
)
439 SetImage( Image(XMLSEC_RES( RID_XMLSECTP_LOCK
)));
442 XmlReadOnlyImage::~XmlReadOnlyImage()
446 void XmlReadOnlyImage::RequestHelp( const HelpEvent
& rHEvt
)
448 if( Help::IsBalloonHelpEnabled() || Help::IsQuickHelpEnabled() )
450 Rectangle
aLogicPix( LogicToPixel( Rectangle( Point(), GetOutputSize() ) ) );
451 Rectangle
aScreenRect( OutputToScreenPixel( aLogicPix
.TopLeft() ),
452 OutputToScreenPixel( aLogicPix
.BottomRight() ) );
454 OUString
aStr(XmlReadOnlyImage::GetHelpTip());
455 if ( Help::IsBalloonHelpEnabled() )
456 Help::ShowBalloon( this, rHEvt
.GetMousePosPixel(), aScreenRect
,
458 else if ( Help::IsQuickHelpEnabled() )
459 Help::ShowQuickHelp( this, aScreenRect
, aStr
);
462 Window::RequestHelp( rHEvt
);
465 const OUString
& XmlReadOnlyImage::GetHelpTip()
467 static OUString
aStr(XMLSEC_RES( RID_XMLSECTP_READONLY_CONFIG_TIP
));
471 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */