LCOV - code coverage report
Current view: top level - libreoffice/cui/source/options - certpath.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 117 0.0 %
Date: 2012-12-27 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             :  * Version: MPL 1.1 / GPLv3+ / LGPLv3+
       4             :  *
       5             :  * The contents of this file are subject to the Mozilla Public License Version
       6             :  * 1.1 (the "License"); you may not use this file except in compliance with
       7             :  * the License or as specified alternatively below. You may obtain a copy of
       8             :  * the License at http://www.mozilla.org/MPL/
       9             :  *
      10             :  * Software distributed under the License is distributed on an "AS IS" basis,
      11             :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12             :  * for the specific language governing rights and limitations under the
      13             :  * License.
      14             :  *
      15             :  * Major Contributor(s):
      16             :  * Copyright (C) 2012 Red Hat, Inc., Caolán McNamara <caolanm@redhat.com>
      17             :  *  (initial developer)
      18             :  *
      19             :  * All Rights Reserved.
      20             :  *
      21             :  * For minor contributions see the git repository.
      22             :  *
      23             :  * Alternatively, the contents of this file may be used under the terms of
      24             :  * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
      25             :  * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
      26             :  * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
      27             :  * instead of those above.
      28             :  */
      29             : 
      30             : #include <officecfg/Office/Common.hxx>
      31             : #include <osl/file.hxx>
      32             : #include <osl/security.hxx>
      33             : #include <svtools/stdctrl.hxx>
      34             : #include "svtools/treelistentry.hxx"
      35             : #include <unotools/securityoptions.hxx>
      36             : #include <cuires.hrc>
      37             : #include "certpath.hxx"
      38             : #include "certpath.hrc"
      39             : #include "dialmgr.hxx"
      40             : 
      41             : #include <com/sun/star/mozilla/XMozillaBootstrap.hpp>
      42             : #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
      43             : #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
      44             : #include <comphelper/processfactory.hxx>
      45             : 
      46             : using namespace ::com::sun::star;
      47             : 
      48           0 : CertPathDialog::CertPathDialog( Window* pParent ) :
      49           0 :      ModalDialog( pParent, CUI_RES( RID_SVXDLG_CERTPATH ) )
      50           0 :     , m_aCertPathFL       ( this, CUI_RES( FL_CERTPATH ) )
      51           0 :     , m_aCertPathFT       ( this, CUI_RES( FT_CERTPATH ) )
      52           0 :     , m_aCertPathListContainer( this, CUI_RES( LB_CERTPATH ) )
      53             :     , m_aCertPathList( m_aCertPathListContainer )
      54           0 :     , m_aAddBtn           ( this, CUI_RES( PB_ADD ) )
      55           0 :     , m_aButtonsFL       ( this, CUI_RES( FL_BUTTONS ) )
      56           0 :     , m_aOKBtn           ( this, CUI_RES( PB_OK ) )
      57           0 :     , m_aCancelBtn       ( this, CUI_RES( PB_CANCEL ) )
      58           0 :     , m_aHelpBtn         ( this, CUI_RES( PB_HELP ) )
      59           0 :     , m_sAddDialogText(CUI_RESSTR(STR_ADDDLGTEXT))
      60           0 :     , m_sManual(CUI_RESSTR(STR_MANUAL))
      61             : {
      62             :     static long aStaticTabs[]=
      63             :     {
      64             :         3, 0, 15, 75
      65             :     };
      66             : 
      67           0 :     m_aCertPathList.SvxSimpleTable::SetTabs( aStaticTabs );
      68             : 
      69           0 :     rtl::OUString sProfile(CUI_RESSTR(STR_PROFILE));
      70           0 :     rtl::OUString sDirectory(CUI_RESSTR(STR_DIRECTORY));
      71             : 
      72           0 :     rtl::OUStringBuffer sHeader;
      73           0 :     sHeader.append('\t').append(sProfile).append('\t').append(sDirectory);
      74           0 :     m_aCertPathList.InsertHeaderEntry( sHeader.makeStringAndClear(), HEADERBAR_APPEND, HIB_LEFT );
      75           0 :     m_aCertPathList.SetCheckButtonHdl( LINK( this, CertPathDialog, CheckHdl_Impl ) );
      76             : 
      77           0 :     m_aAddBtn.SetClickHdl( LINK( this, CertPathDialog, AddHdl_Impl ) );
      78           0 :     m_aOKBtn.SetClickHdl( LINK( this, CertPathDialog, OKHdl_Impl ) );
      79             : 
      80           0 :     FreeResource();
      81             : 
      82             :     try
      83             :     {
      84             :         mozilla::MozillaProductType productTypes[3] = {
      85             :             mozilla::MozillaProductType_Thunderbird,
      86             :             mozilla::MozillaProductType_Firefox,
      87           0 :             mozilla::MozillaProductType_Mozilla };
      88             :         const char* productNames[3] = {
      89             :             "thunderbird",
      90             :             "firefox",
      91           0 :             "mozilla" };
      92           0 :         sal_Int32 nProduct = SAL_N_ELEMENTS(productTypes);
      93             : 
      94           0 :         uno::Reference<uno::XInterface> xInstance = comphelper::getProcessServiceFactory()->createInstance(
      95           0 :             "com.sun.star.mozilla.MozillaBootstrap");
      96             : 
      97           0 :         uno::Reference<mozilla::XMozillaBootstrap> xMozillaBootstrap(xInstance, uno::UNO_QUERY_THROW);
      98             : 
      99           0 :         for (sal_Int32 i = 0; i < nProduct; ++i)
     100             :         {
     101           0 :             ::rtl::OUString profile = xMozillaBootstrap->getDefaultProfile(productTypes[i]);
     102             : 
     103           0 :             if (!profile.isEmpty())
     104             :             {
     105           0 :                 ::rtl::OUString sProfilePath = xMozillaBootstrap->getProfilePath( productTypes[i], profile );
     106           0 :                 rtl::OUStringBuffer sEntry;
     107           0 :                 sEntry.append('\t').appendAscii(productNames[i]).append(':').append(profile).append('\t').append(sProfilePath);
     108           0 :                 SvTreeListEntry *pEntry = m_aCertPathList.InsertEntry(sEntry.makeStringAndClear());
     109           0 :                 rtl::OUString* pCertPath = new rtl::OUString(sProfilePath);
     110           0 :                 pEntry->SetUserData(pCertPath);
     111             :             }
     112           0 :         }
     113             :     }
     114           0 :     catch (const uno::Exception&)
     115             :     {
     116             :     }
     117             : 
     118           0 :     SvTreeListEntry *pEntry = m_aCertPathList.GetEntry(0);
     119           0 :     if (pEntry)
     120             :     {
     121           0 :         m_aCertPathList.SetCheckButtonState(pEntry, SV_BUTTON_CHECKED);
     122           0 :         HandleCheckEntry(pEntry);
     123             :     }
     124             : 
     125             :     try
     126             :     {
     127             :         rtl::OUString sUserSetCertPath =
     128           0 :             officecfg::Office::Common::Security::Scripting::CertDir::get().get_value_or(rtl::OUString());
     129             : 
     130           0 :         if (!sUserSetCertPath.isEmpty())
     131           0 :             AddCertPath(m_sManual, sUserSetCertPath);
     132             :     }
     133           0 :     catch (const uno::Exception &e)
     134             :     {
     135             :         SAL_WARN("cui.options", "CertPathDialog::CertPathDialog(): caught exception" << e.Message);
     136             :     }
     137             : 
     138           0 :     const char* pEnv = getenv("MOZILLA_CERTIFICATE_FOLDER");
     139           0 :     if (pEnv)
     140           0 :         AddCertPath("$MOZILLA_CERTIFICATE_FOLDER", rtl::OUString(pEnv, strlen(pEnv), osl_getThreadTextEncoding()));
     141           0 : }
     142             : 
     143           0 : IMPL_LINK_NOARG(CertPathDialog, OKHdl_Impl)
     144             : {
     145           0 :     fprintf(stderr, "dir is %s\n", rtl::OUStringToOString(getDirectory(), RTL_TEXTENCODING_UTF8).getStr());
     146             : 
     147             :     try
     148             :     {
     149             :         boost::shared_ptr< comphelper::ConfigurationChanges > batch(
     150           0 :             comphelper::ConfigurationChanges::create());
     151             :         officecfg::Office::Common::Security::Scripting::CertDir::set(
     152           0 :             getDirectory(), batch);
     153           0 :         batch->commit();
     154             :     }
     155           0 :     catch (const uno::Exception &e)
     156             :     {
     157             :         SAL_WARN("cui.options", "CertPathDialog::OKHdl_Impl(): caught exception" << e.Message);
     158             :     }
     159             : 
     160           0 :     EndDialog(true);
     161             : 
     162           0 :     return 0;
     163             : }
     164             : 
     165           0 : rtl::OUString CertPathDialog::getDirectory() const
     166             : {
     167           0 :     SvTreeListEntry* pEntry = m_aCertPathList.FirstSelected();
     168           0 :     void* pCertPath = pEntry ? pEntry->GetUserData() : NULL;
     169           0 :     return pCertPath ? *static_cast<rtl::OUString*>(pCertPath) : rtl::OUString();
     170             : }
     171             : 
     172           0 : CertPathDialog::~CertPathDialog()
     173             : {
     174           0 :     SvTreeListEntry* pEntry = m_aCertPathList.First();
     175           0 :     while (pEntry)
     176             :     {
     177           0 :         rtl::OUString* pCertPath = static_cast<rtl::OUString*>(pEntry->GetUserData());
     178           0 :         delete pCertPath;
     179           0 :         pEntry = m_aCertPathList.Next( pEntry );
     180             :     }
     181           0 : }
     182             : 
     183           0 : IMPL_LINK( CertPathDialog, CheckHdl_Impl, SvxSimpleTable *, pList )
     184             : {
     185           0 :     SvTreeListEntry* pEntry = pList ? m_aCertPathList.GetEntry(m_aCertPathList.GetCurMousePoint())
     186           0 :                                 : m_aCertPathList.FirstSelected();
     187           0 :     if (pEntry)
     188           0 :         m_aCertPathList.HandleEntryChecked(pEntry);
     189           0 :     return 0;
     190             : }
     191             : 
     192           0 : void CertPathDialog::HandleCheckEntry( SvTreeListEntry* _pEntry )
     193             : {
     194           0 :     m_aCertPathList.Select( _pEntry, true );
     195           0 :     SvButtonState eState = m_aCertPathList.GetCheckButtonState( _pEntry );
     196             : 
     197           0 :     if (SV_BUTTON_CHECKED == eState)
     198             :     {
     199             :         // uncheck the other entries
     200           0 :         SvTreeListEntry* pEntry = m_aCertPathList.First();
     201           0 :         while (pEntry)
     202             :         {
     203           0 :             if (pEntry != _pEntry)
     204           0 :                 m_aCertPathList.SetCheckButtonState(pEntry, SV_BUTTON_UNCHECKED);
     205           0 :             pEntry = m_aCertPathList.Next(pEntry);
     206             :         }
     207             :     }
     208             :     else
     209           0 :         m_aCertPathList.SetCheckButtonState(_pEntry, SV_BUTTON_CHECKED);
     210           0 : }
     211             : 
     212           0 : void CertPathDialog::AddCertPath(const rtl::OUString &rProfile, const rtl::OUString &rPath)
     213             : {
     214           0 :     SvTreeListEntry* pEntry = m_aCertPathList.First();
     215           0 :     while (pEntry)
     216             :     {
     217           0 :         rtl::OUString* pCertPath = static_cast<rtl::OUString*>(pEntry->GetUserData());
     218             :         //already exists, just select the original one
     219           0 :         if (pCertPath->equals(rPath))
     220             :         {
     221           0 :             m_aCertPathList.SetCheckButtonState(pEntry, SV_BUTTON_CHECKED);
     222           0 :             HandleCheckEntry(pEntry);
     223           0 :             return;
     224             :         }
     225           0 :         pEntry = m_aCertPathList.Next(pEntry);
     226             :     }
     227             : 
     228           0 :     rtl::OUStringBuffer sEntry;
     229           0 :     sEntry.append('\t').append(rProfile).append('\t').append(rPath);
     230           0 :     pEntry = m_aCertPathList.InsertEntry(sEntry.makeStringAndClear());
     231           0 :     rtl::OUString* pCertPath = new rtl::OUString(rPath);
     232           0 :     pEntry->SetUserData(pCertPath);
     233           0 :     m_aCertPathList.SetCheckButtonState(pEntry, SV_BUTTON_CHECKED);
     234           0 :     HandleCheckEntry(pEntry);
     235             : }
     236             : 
     237           0 : IMPL_LINK_NOARG(CertPathDialog, AddHdl_Impl)
     238             : {
     239             :     try
     240             :     {
     241           0 :         uno::Reference<ui::dialogs::XFolderPicker2> xFolderPicker = ui::dialogs::FolderPicker::create(comphelper::getProcessComponentContext());
     242             : 
     243           0 :         rtl::OUString sURL;
     244           0 :         osl::Security().getHomeDir(sURL);
     245           0 :         xFolderPicker->setDisplayDirectory(sURL);
     246           0 :         xFolderPicker->setDescription(m_sAddDialogText);
     247             : 
     248           0 :         if (xFolderPicker->execute() == ui::dialogs::ExecutableDialogResults::OK)
     249             :         {
     250           0 :             sURL = xFolderPicker->getDirectory();
     251           0 :             rtl::OUString aPath;
     252           0 :             if (osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(sURL, aPath))
     253           0 :                 AddCertPath(m_sManual, aPath);
     254           0 :         }
     255             :     }
     256           0 :     catch (uno::Exception & e)
     257             :     {
     258             :         SAL_WARN("cui.options", "caught UNO exception: " << e.Message);
     259             :     }
     260             : 
     261           0 :     return 0;
     262             : }
     263             : 
     264             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10