LCOV - code coverage report
Current view: top level - cui/source/options - certpath.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 0 122 0.0 %
Date: 2015-06-13 12:38:46 Functions: 0 15 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  */
       9             : 
      10             : #include <officecfg/Office/Common.hxx>
      11             : #include <osl/file.hxx>
      12             : #include <osl/security.hxx>
      13             : #include <osl/thread.h>
      14             : #include <svtools/stdctrl.hxx>
      15             : #include "svtools/treelistentry.hxx"
      16             : #include <unotools/securityoptions.hxx>
      17             : #include <cuires.hrc>
      18             : #include "certpath.hxx"
      19             : #include "dialmgr.hxx"
      20             : 
      21             : #include <com/sun/star/mozilla/MozillaBootstrap.hpp>
      22             : #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
      23             : #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
      24             : #include <comphelper/processfactory.hxx>
      25             : 
      26             : using namespace ::com::sun::star;
      27             : 
      28           0 : CertPathDialog::CertPathDialog(vcl::Window* pParent)
      29           0 :     : ModalDialog(pParent, "CertDialog", "cui/ui/certdialog.ui")
      30             : {
      31           0 :     get(m_pOKBtn, "ok");
      32           0 :     get(m_pAddBtn, "add");
      33           0 :     get(m_pCertPathListContainer, "paths");
      34           0 :     Size aSize(LogicToPixel(Size(210, 60), MAP_APPFONT));
      35           0 :     m_pCertPathListContainer->set_width_request(aSize.Width());
      36           0 :     m_pCertPathListContainer->set_height_request(aSize.Height());
      37           0 :     m_pCertPathList =
      38           0 :         VclPtr<svx::SvxRadioButtonListBox>::Create(*m_pCertPathListContainer, 0);
      39           0 :     m_sAddDialogText = get<FixedText>("certdir")->GetText();
      40           0 :     m_sManual = get<FixedText>("manual")->GetText();
      41             : 
      42             :     static long aStaticTabs[]=
      43             :     {
      44             :         3, 0, 15, 75
      45             :     };
      46             : 
      47           0 :     m_pCertPathList->SvSimpleTable::SetTabs( aStaticTabs );
      48             : 
      49           0 :     OUString sProfile(get<FixedText>("profile")->GetText());
      50           0 :     OUString sDirectory(get<FixedText>("dir")->GetText());
      51             : 
      52           0 :     OUStringBuffer sHeader;
      53           0 :     sHeader.append('\t').append(sProfile).append('\t').append(sDirectory);
      54           0 :     m_pCertPathList->InsertHeaderEntry( sHeader.makeStringAndClear(), HEADERBAR_APPEND, HeaderBarItemBits::LEFT );
      55           0 :     m_pCertPathList->SetCheckButtonHdl( LINK( this, CertPathDialog, CheckHdl_Impl ) );
      56             : 
      57           0 :     m_pAddBtn->SetClickHdl( LINK( this, CertPathDialog, AddHdl_Impl ) );
      58           0 :     m_pOKBtn->SetClickHdl( LINK( this, CertPathDialog, OKHdl_Impl ) );
      59             : 
      60             :     try
      61             :     {
      62             :         mozilla::MozillaProductType productTypes[3] = {
      63             :             mozilla::MozillaProductType_Thunderbird,
      64             :             mozilla::MozillaProductType_Firefox,
      65           0 :             mozilla::MozillaProductType_Mozilla };
      66             :         const char* productNames[3] = {
      67             :             "thunderbird",
      68             :             "firefox",
      69           0 :             "mozilla" };
      70           0 :         sal_Int32 nProduct = SAL_N_ELEMENTS(productTypes);
      71             : 
      72           0 :         uno::Reference<mozilla::XMozillaBootstrap> xMozillaBootstrap = mozilla::MozillaBootstrap::create( comphelper::getProcessComponentContext() );
      73             : 
      74           0 :         for (sal_Int32 i = 0; i < nProduct; ++i)
      75             :         {
      76           0 :             OUString profile = xMozillaBootstrap->getDefaultProfile(productTypes[i]);
      77             : 
      78           0 :             if (!profile.isEmpty())
      79             :             {
      80           0 :                 OUString sProfilePath = xMozillaBootstrap->getProfilePath( productTypes[i], profile );
      81           0 :                 OUStringBuffer sEntry;
      82           0 :                 sEntry.append('\t').appendAscii(productNames[i]).append(':').append(profile).append('\t').append(sProfilePath);
      83           0 :                 SvTreeListEntry *pEntry = m_pCertPathList->InsertEntry(sEntry.makeStringAndClear());
      84           0 :                 OUString* pCertPath = new OUString(sProfilePath);
      85           0 :                 pEntry->SetUserData(pCertPath);
      86             :             }
      87           0 :         }
      88             :     }
      89           0 :     catch (const uno::Exception&)
      90             :     {
      91             :     }
      92             : 
      93           0 :     SvTreeListEntry *pEntry = m_pCertPathList->GetEntry(0);
      94           0 :     if (pEntry)
      95             :     {
      96           0 :         m_pCertPathList->SetCheckButtonState(pEntry, SV_BUTTON_CHECKED);
      97           0 :         HandleCheckEntry(pEntry);
      98             :     }
      99             : 
     100             :     try
     101             :     {
     102             :         OUString sUserSetCertPath =
     103           0 :             officecfg::Office::Common::Security::Scripting::CertDir::get().get_value_or(OUString());
     104             : 
     105           0 :         if (!sUserSetCertPath.isEmpty())
     106           0 :             AddCertPath(m_sManual, sUserSetCertPath);
     107             :     }
     108           0 :     catch (const uno::Exception &e)
     109             :     {
     110             :         SAL_WARN("cui.options", "CertPathDialog::CertPathDialog(): caught exception" << e.Message);
     111             :     }
     112             : 
     113           0 :     const char* pEnv = getenv("MOZILLA_CERTIFICATE_FOLDER");
     114           0 :     if (pEnv)
     115           0 :         AddCertPath("$MOZILLA_CERTIFICATE_FOLDER", OUString(pEnv, strlen(pEnv), osl_getThreadTextEncoding()));
     116           0 : }
     117             : 
     118           0 : IMPL_LINK_NOARG(CertPathDialog, OKHdl_Impl)
     119             : {
     120             :     try
     121             :     {
     122             :         std::shared_ptr< comphelper::ConfigurationChanges > batch(
     123           0 :             comphelper::ConfigurationChanges::create());
     124             :         officecfg::Office::Common::Security::Scripting::CertDir::set(
     125           0 :             getDirectory(), batch);
     126           0 :         batch->commit();
     127             :     }
     128           0 :     catch (const uno::Exception &e)
     129             :     {
     130             :         SAL_WARN("cui.options", "CertPathDialog::OKHdl_Impl(): caught exception" << e.Message);
     131             :     }
     132             : 
     133           0 :     EndDialog(RET_OK);
     134             : 
     135           0 :     return 0;
     136             : }
     137             : 
     138           0 : OUString CertPathDialog::getDirectory() const
     139             : {
     140           0 :     SvTreeListEntry* pEntry = m_pCertPathList->FirstSelected();
     141           0 :     void* pCertPath = pEntry ? pEntry->GetUserData() : NULL;
     142           0 :     return pCertPath ? *static_cast<OUString*>(pCertPath) : OUString();
     143             : }
     144             : 
     145           0 : CertPathDialog::~CertPathDialog()
     146             : {
     147           0 :     disposeOnce();
     148           0 : }
     149             : 
     150           0 : void CertPathDialog::dispose()
     151             : {
     152           0 :     SvTreeListEntry* pEntry = m_pCertPathList->First();
     153           0 :     while (pEntry)
     154             :     {
     155           0 :         OUString* pCertPath = static_cast<OUString*>(pEntry->GetUserData());
     156           0 :         delete pCertPath;
     157           0 :         pEntry = m_pCertPathList->Next( pEntry );
     158             :     }
     159           0 :     m_pCertPathList.disposeAndClear();
     160           0 :     m_pCertPathListContainer.clear();
     161           0 :     m_pAddBtn.clear();
     162           0 :     m_pOKBtn.clear();
     163           0 :     ModalDialog::dispose();
     164           0 : }
     165             : 
     166           0 : IMPL_LINK( CertPathDialog, CheckHdl_Impl, SvSimpleTable *, pList )
     167             : {
     168           0 :     SvTreeListEntry* pEntry = pList ? m_pCertPathList->GetEntry(m_pCertPathList->GetCurMousePoint())
     169           0 :                                 : m_pCertPathList->FirstSelected();
     170           0 :     if (pEntry)
     171           0 :         m_pCertPathList->HandleEntryChecked(pEntry);
     172           0 :     return 0;
     173             : }
     174             : 
     175           0 : void CertPathDialog::HandleCheckEntry( SvTreeListEntry* _pEntry )
     176             : {
     177           0 :     m_pCertPathList->Select( _pEntry, true );
     178           0 :     SvButtonState eState = m_pCertPathList->GetCheckButtonState( _pEntry );
     179             : 
     180           0 :     if (SV_BUTTON_CHECKED == eState)
     181             :     {
     182             :         // uncheck the other entries
     183           0 :         SvTreeListEntry* pEntry = m_pCertPathList->First();
     184           0 :         while (pEntry)
     185             :         {
     186           0 :             if (pEntry != _pEntry)
     187           0 :                 m_pCertPathList->SetCheckButtonState(pEntry, SV_BUTTON_UNCHECKED);
     188           0 :             pEntry = m_pCertPathList->Next(pEntry);
     189             :         }
     190             :     }
     191             :     else
     192           0 :         m_pCertPathList->SetCheckButtonState(_pEntry, SV_BUTTON_CHECKED);
     193           0 : }
     194             : 
     195           0 : void CertPathDialog::AddCertPath(const OUString &rProfile, const OUString &rPath)
     196             : {
     197           0 :     SvTreeListEntry* pEntry = m_pCertPathList->First();
     198           0 :     while (pEntry)
     199             :     {
     200           0 :         OUString* pCertPath = static_cast<OUString*>(pEntry->GetUserData());
     201             :         //already exists, just select the original one
     202           0 :         if (pCertPath->equals(rPath))
     203             :         {
     204           0 :             m_pCertPathList->SetCheckButtonState(pEntry, SV_BUTTON_CHECKED);
     205           0 :             HandleCheckEntry(pEntry);
     206           0 :             return;
     207             :         }
     208           0 :         pEntry = m_pCertPathList->Next(pEntry);
     209             :     }
     210             : 
     211           0 :     OUStringBuffer sEntry;
     212           0 :     sEntry.append('\t').append(rProfile).append('\t').append(rPath);
     213           0 :     pEntry = m_pCertPathList->InsertEntry(sEntry.makeStringAndClear());
     214           0 :     OUString* pCertPath = new OUString(rPath);
     215           0 :     pEntry->SetUserData(pCertPath);
     216           0 :     m_pCertPathList->SetCheckButtonState(pEntry, SV_BUTTON_CHECKED);
     217           0 :     HandleCheckEntry(pEntry);
     218             : }
     219             : 
     220           0 : IMPL_LINK_NOARG(CertPathDialog, AddHdl_Impl)
     221             : {
     222             :     try
     223             :     {
     224           0 :         uno::Reference<ui::dialogs::XFolderPicker2> xFolderPicker = ui::dialogs::FolderPicker::create(comphelper::getProcessComponentContext());
     225             : 
     226           0 :         OUString sURL;
     227           0 :         osl::Security().getHomeDir(sURL);
     228           0 :         xFolderPicker->setDisplayDirectory(sURL);
     229           0 :         xFolderPicker->setDescription(m_sAddDialogText);
     230             : 
     231           0 :         if (xFolderPicker->execute() == ui::dialogs::ExecutableDialogResults::OK)
     232             :         {
     233           0 :             sURL = xFolderPicker->getDirectory();
     234           0 :             OUString aPath;
     235           0 :             if (osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(sURL, aPath))
     236           0 :                 AddCertPath(m_sManual, aPath);
     237           0 :         }
     238             :     }
     239           0 :     catch (uno::Exception & e)
     240             :     {
     241             :         SAL_WARN("cui.options", "caught UNO exception: " << e.Message);
     242             :     }
     243             : 
     244           0 :     return 0;
     245           0 : }
     246             : 
     247             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11