LCOV - code coverage report
Current view: top level - cui/source/options - certpath.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 113 0.0 %
Date: 2014-04-14 Functions: 0 12 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(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             :     m_pCertPathList =
      38           0 :         new svx::SvxRadioButtonListBox(*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, HIB_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             :         boost::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 :     SvTreeListEntry* pEntry = m_pCertPathList->First();
     148           0 :     while (pEntry)
     149             :     {
     150           0 :         OUString* pCertPath = static_cast<OUString*>(pEntry->GetUserData());
     151           0 :         delete pCertPath;
     152           0 :         pEntry = m_pCertPathList->Next( pEntry );
     153             :     }
     154           0 :     delete m_pCertPathList;
     155           0 : }
     156             : 
     157           0 : IMPL_LINK( CertPathDialog, CheckHdl_Impl, SvSimpleTable *, pList )
     158             : {
     159           0 :     SvTreeListEntry* pEntry = pList ? m_pCertPathList->GetEntry(m_pCertPathList->GetCurMousePoint())
     160           0 :                                 : m_pCertPathList->FirstSelected();
     161           0 :     if (pEntry)
     162           0 :         m_pCertPathList->HandleEntryChecked(pEntry);
     163           0 :     return 0;
     164             : }
     165             : 
     166           0 : void CertPathDialog::HandleCheckEntry( SvTreeListEntry* _pEntry )
     167             : {
     168           0 :     m_pCertPathList->Select( _pEntry, true );
     169           0 :     SvButtonState eState = m_pCertPathList->GetCheckButtonState( _pEntry );
     170             : 
     171           0 :     if (SV_BUTTON_CHECKED == eState)
     172             :     {
     173             :         // uncheck the other entries
     174           0 :         SvTreeListEntry* pEntry = m_pCertPathList->First();
     175           0 :         while (pEntry)
     176             :         {
     177           0 :             if (pEntry != _pEntry)
     178           0 :                 m_pCertPathList->SetCheckButtonState(pEntry, SV_BUTTON_UNCHECKED);
     179           0 :             pEntry = m_pCertPathList->Next(pEntry);
     180             :         }
     181             :     }
     182             :     else
     183           0 :         m_pCertPathList->SetCheckButtonState(_pEntry, SV_BUTTON_CHECKED);
     184           0 : }
     185             : 
     186           0 : void CertPathDialog::AddCertPath(const OUString &rProfile, const OUString &rPath)
     187             : {
     188           0 :     SvTreeListEntry* pEntry = m_pCertPathList->First();
     189           0 :     while (pEntry)
     190             :     {
     191           0 :         OUString* pCertPath = static_cast<OUString*>(pEntry->GetUserData());
     192             :         //already exists, just select the original one
     193           0 :         if (pCertPath->equals(rPath))
     194             :         {
     195           0 :             m_pCertPathList->SetCheckButtonState(pEntry, SV_BUTTON_CHECKED);
     196           0 :             HandleCheckEntry(pEntry);
     197           0 :             return;
     198             :         }
     199           0 :         pEntry = m_pCertPathList->Next(pEntry);
     200             :     }
     201             : 
     202           0 :     OUStringBuffer sEntry;
     203           0 :     sEntry.append('\t').append(rProfile).append('\t').append(rPath);
     204           0 :     pEntry = m_pCertPathList->InsertEntry(sEntry.makeStringAndClear());
     205           0 :     OUString* pCertPath = new OUString(rPath);
     206           0 :     pEntry->SetUserData(pCertPath);
     207           0 :     m_pCertPathList->SetCheckButtonState(pEntry, SV_BUTTON_CHECKED);
     208           0 :     HandleCheckEntry(pEntry);
     209             : }
     210             : 
     211           0 : IMPL_LINK_NOARG(CertPathDialog, AddHdl_Impl)
     212             : {
     213             :     try
     214             :     {
     215           0 :         uno::Reference<ui::dialogs::XFolderPicker2> xFolderPicker = ui::dialogs::FolderPicker::create(comphelper::getProcessComponentContext());
     216             : 
     217           0 :         OUString sURL;
     218           0 :         osl::Security().getHomeDir(sURL);
     219           0 :         xFolderPicker->setDisplayDirectory(sURL);
     220           0 :         xFolderPicker->setDescription(m_sAddDialogText);
     221             : 
     222           0 :         if (xFolderPicker->execute() == ui::dialogs::ExecutableDialogResults::OK)
     223             :         {
     224           0 :             sURL = xFolderPicker->getDirectory();
     225           0 :             OUString aPath;
     226           0 :             if (osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(sURL, aPath))
     227           0 :                 AddCertPath(m_sManual, aPath);
     228           0 :         }
     229             :     }
     230           0 :     catch (uno::Exception & e)
     231             :     {
     232             :         SAL_WARN("cui.options", "caught UNO exception: " << e.Message);
     233             :     }
     234             : 
     235           0 :     return 0;
     236             : }
     237             : 
     238             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10