Bump version to 5.0-14
[LibreOffice.git] / xmlsecurity / source / dialogs / macrosecurity.cxx
blob1142b1dfd173b63e11400252657779fb7922d75d
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 .
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();
56 EndDialog( RET_OK );
58 return 0;
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")
65 , mxCtx(_rxCtx)
66 , mxSecurityEnvironment(_rxSecurityEnvironment)
68 get(m_pTabCtrl, "tabcontrol");
69 get(m_pResetBtn, "reset");
70 get(m_pOkBtn, "ok");
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()
87 disposeOnce();
90 void MacroSecurity::dispose()
92 m_pTabCtrl->GetTabPage(m_nSecTrustId)->disposeOnce();
93 m_pTabCtrl->GetTabPage(m_nSecLevelId)->disposeOnce();
94 m_pTabCtrl.clear();
95 m_pOkBtn.clear();
96 m_pResetBtn.clear();
97 mpLevelTP.disposeAndClear();
98 mpTrustSrcTP.disposeAndClear();
99 TabDialog::dispose();
102 MacroSecurityTP::MacroSecurityTP(vcl::Window* _pParent, const OString& rID,
103 const OUString& rUIXMLDescription, MacroSecurity* _pDlg)
104 : TabPage(_pParent, rID, rUIXMLDescription)
105 , mpDlg(_pDlg)
109 MacroSecurityTP::~MacroSecurityTP()
111 disposeOnce();
114 void MacroSecurityTP::dispose()
116 mpDlg.clear();
117 TabPage::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;
138 switch (mnCurLevel)
140 case 3:
141 pCheck = m_pVeryHighRB;
142 pImage = get<FixedImage>("vhighimg");
143 break;
144 case 2:
145 pCheck = m_pHighRB;
146 pImage = get<FixedImage>("highimg");
147 break;
148 case 1:
149 pCheck = m_pMediumRB;
150 pImage = get<FixedImage>("medimg");
151 break;
152 case 0:
153 pCheck = m_pLowRB;
154 pImage = get<FixedImage>("lowimg");
155 break;
157 if (pCheck)
158 pCheck->Check();
159 else
161 OSL_FAIL("illegal macro security level");
163 if (bReadonly && pImage)
165 pImage->Show(true);
166 m_pVeryHighRB->Enable(false);
167 m_pHighRB->Enable(false);
168 m_pMediumRB->Enable(false);
169 m_pLowRB->Enable(false);
173 MacroSecurityLevelTP::~MacroSecurityLevelTP()
175 disposeOnce();
178 void MacroSecurityLevelTP::dispose()
180 m_pVeryHighRB.clear();
181 m_pHighRB.clear();
182 m_pMediumRB.clear();
183 m_pLowRB.clear();
184 MacroSecurityTP::dispose();
187 IMPL_LINK_NOARG(MacroSecurityLevelTP, RadioButtonHdl)
189 sal_uInt16 nNewLevel = 0;
190 if( m_pVeryHighRB->IsChecked() )
191 nNewLevel = 3;
192 else if( m_pHighRB->IsChecked() )
193 nNewLevel = 2;
194 else if( m_pMediumRB->IsChecked() )
195 nNewLevel = 1;
197 if ( nNewLevel != mnCurLevel )
199 mnCurLevel = nNewLevel;
200 mpDlg->EnableReset();
203 return 0;
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:
234 if ( !xCert.is() )
235 xCert = mpDlg->mxSecurityEnvironment->createCertificateFromAscii( maTrustedAuthors[nSelected][2] ) ;
237 DBG_ASSERT( xCert.is(), "*MacroSecurityTrustedSourcesTP::ViewCertPBHdl(): Certificate not found and can't be created!" );
239 if ( xCert.is() )
241 ScopedVclPtrInstance< CertificateViewer > aViewer( this, mpDlg->mxSecurityEnvironment, xCert, false );
242 aViewer->Execute();
245 return 0;
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 );
255 FillCertLB();
256 ImplCheckButtons();
259 return 0;
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 )
272 return 0;
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 );
292 ImplCheckButtons();
294 catch( uno::Exception& )
296 SAL_WARN( "xmlsecurity.dialogs", "MacroSecurityTrustedSourcesTP::AddLocPBHdl(): exception from folder picker" );
299 return 0;
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();
311 if ( nNewCount > 0 )
313 if ( nSel >= nNewCount )
314 nSel = nNewCount - 1;
315 m_pTrustFileLocLB->SelectEntryPos( nSel );
317 ImplCheckButtons();
320 return 0;
323 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, TrustCertLBSelectHdl)
325 ImplCheckButtons();
326 return 0;
329 IMPL_LINK_NOARG(MacroSecurityTrustedSourcesTP, TrustFileLocLBSelectHdl)
331 ImplCheckButtons();
332 return 0;
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
361 public:
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();
414 FillCertLB();
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()
433 disposeOnce();
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();
444 m_pAddLocPB.clear();
445 m_pRemoveLocPB.clear();
446 MacroSecurityTP::dispose();
449 void MacroSecurityTrustedSourcesTP::ActivatePage()
451 mpDlg->EnableReset( false );
452 FillCertLB();
455 void MacroSecurityTrustedSourcesTP::ClosePage()
457 sal_Int32 nEntryCnt = m_pTrustFileLocLB->GetEntryCount();
458 if( nEntryCnt )
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
472 else
473 mpDlg->maSecOptions.SetSecureURLs( css::uno::Sequence< OUString >() );
475 mpDlg->maSecOptions.SetTrustedAuthors( maTrustedAuthors );
478 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */