LCOV - code coverage report
Current view: top level - libreoffice/svx/source/form - databaselocationinput.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 93 0.0 %
Date: 2012-12-27 Functions: 0 16 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             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : 
      21             : #include "svx/databaselocationinput.hxx"
      22             : #include "svx/dialmgr.hxx"
      23             : 
      24             : #include "svx/fmresids.hrc"
      25             : 
      26             : #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
      27             : 
      28             : #include <comphelper/componentcontext.hxx>
      29             : #include <comphelper/namedvaluecollection.hxx>
      30             : #include <rtl/ustrbuf.hxx>
      31             : #include <sfx2/filedlghelper.hxx>
      32             : #include <svtools/urlcontrol.hxx>
      33             : #include <svl/filenotation.hxx>
      34             : #include <tools/diagnose_ex.h>
      35             : #include <unotools/confignode.hxx>
      36             : #include <unotools/ucbhelper.hxx>
      37             : #include <vcl/button.hxx>
      38             : #include <vcl/msgbox.hxx>
      39             : 
      40             : //........................................................................
      41             : namespace svx
      42             : {
      43             : //........................................................................
      44             : 
      45             :     /** === begin UNO using === **/
      46             :     using ::com::sun::star::uno::Sequence;
      47             :     using ::com::sun::star::uno::Reference;
      48             :     using ::com::sun::star::container::XNameAccess;
      49             :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      50             :     using ::com::sun::star::uno::Exception;
      51             :     /** === end UNO using === **/
      52             :     namespace TemplateDescription = ::com::sun::star::ui::dialogs::TemplateDescription;
      53             : 
      54             :     //====================================================================
      55             :     //= DatabaseLocationInputController_Impl
      56             :     //====================================================================
      57             :     class DatabaseLocationInputController_Impl
      58             :     {
      59             :     public:
      60             :         DatabaseLocationInputController_Impl(
      61             :             const ::comphelper::ComponentContext&   _rContext,
      62             :             ::svt::OFileURLControl&                 _rLocationInput,
      63             :             PushButton&                             _rBrowseButton
      64             :         );
      65             :         ~DatabaseLocationInputController_Impl();
      66             : 
      67             :         bool    prepareCommit();
      68             :         void    setURL( const String& _rURL );
      69             :         String  getURL() const;
      70             : 
      71             :     private:
      72             :         void    impl_initFilterProperties_nothrow();
      73             :         void    impl_onBrowseButtonClicked();
      74             :         void    impl_onLocationModified();
      75             :         String  impl_getCurrentURL() const;
      76             : 
      77             :         DECL_LINK( OnControlAction, VclWindowEvent* );
      78             : 
      79             :     private:
      80             :         const ::comphelper::ComponentContext    m_aContext;
      81             :         ::svt::OFileURLControl&                 m_rLocationInput;
      82             :         PushButton&                             m_rBrowseButton;
      83             :         Sequence< ::rtl::OUString >             m_aFilterExtensions;
      84             :         ::rtl::OUString                         m_sFilterUIName;
      85             :         bool                                    m_bNeedExistenceCheck;
      86             :     };
      87             : 
      88             :     //--------------------------------------------------------------------
      89           0 :     DatabaseLocationInputController_Impl::DatabaseLocationInputController_Impl( const ::comphelper::ComponentContext& _rContext,
      90             :             ::svt::OFileURLControl& _rLocationInput, PushButton& _rBrowseButton )
      91             :         :m_aContext( _rContext )
      92             :         ,m_rLocationInput( _rLocationInput )
      93             :         ,m_rBrowseButton( _rBrowseButton )
      94             :         ,m_aFilterExtensions()
      95             :         ,m_sFilterUIName()
      96           0 :         ,m_bNeedExistenceCheck( true )
      97             :     {
      98           0 :         impl_initFilterProperties_nothrow();
      99             : 
     100             :         // forward the allowed extensions to the input control
     101           0 :         ::rtl::OUStringBuffer aExtensionList;
     102           0 :         for (   const ::rtl::OUString* pExtension = m_aFilterExtensions.getConstArray();
     103           0 :                 pExtension != m_aFilterExtensions.getConstArray() + m_aFilterExtensions.getLength();
     104             :                 ++pExtension
     105             :             )
     106             :         {
     107           0 :             aExtensionList.append( *pExtension );
     108           0 :             aExtensionList.append( (sal_Unicode)';' );
     109             :         }
     110           0 :         m_rLocationInput.SetFilter( aExtensionList.makeStringAndClear() );
     111             : 
     112           0 :         m_rBrowseButton.AddEventListener( LINK( this, DatabaseLocationInputController_Impl, OnControlAction ) );
     113           0 :         m_rLocationInput.AddEventListener( LINK( this, DatabaseLocationInputController_Impl, OnControlAction ) );
     114           0 :     }
     115             : 
     116             :     //--------------------------------------------------------------------
     117           0 :     DatabaseLocationInputController_Impl::~DatabaseLocationInputController_Impl()
     118             :     {
     119           0 :         m_rBrowseButton.RemoveEventListener( LINK( this, DatabaseLocationInputController_Impl, OnControlAction ) );
     120           0 :         m_rLocationInput.RemoveEventListener( LINK( this, DatabaseLocationInputController_Impl, OnControlAction ) );
     121           0 :     }
     122             : 
     123             :     //--------------------------------------------------------------------
     124           0 :     bool DatabaseLocationInputController_Impl::prepareCommit()
     125             :     {
     126           0 :         ::rtl::OUString sURL( impl_getCurrentURL() );
     127           0 :         if ( sURL.isEmpty() )
     128           0 :             return false;
     129             : 
     130             :         // check if the name exists
     131           0 :         if ( m_bNeedExistenceCheck )
     132             :         {
     133           0 :             if ( ::utl::UCBContentHelper::Exists( sURL ) )
     134             :             {
     135           0 :                 QueryBox aBox( m_rLocationInput.GetSystemWindow(), WB_YES_NO, SVX_RES( RID_STR_ALREADYEXISTOVERWRITE ) );
     136           0 :                 if ( aBox.Execute() != RET_YES )
     137           0 :                     return false;
     138             :             }
     139             :         }
     140             : 
     141           0 :         return true;
     142             :     }
     143             : 
     144             :     //--------------------------------------------------------------------
     145           0 :     void DatabaseLocationInputController_Impl::setURL( const String& _rURL )
     146             :     {
     147           0 :         ::svt::OFileNotation aTransformer( _rURL );
     148           0 :         m_rLocationInput.SetText( aTransformer.get( ::svt::OFileNotation::N_SYSTEM ) );
     149           0 :     }
     150             : 
     151             :     //--------------------------------------------------------------------
     152           0 :     String DatabaseLocationInputController_Impl::getURL() const
     153             :     {
     154           0 :         return impl_getCurrentURL();
     155             :     }
     156             : 
     157             :     //--------------------------------------------------------------------
     158           0 :     void DatabaseLocationInputController_Impl::impl_initFilterProperties_nothrow()
     159             :     {
     160             :         try
     161             :         {
     162             :             // get the name of the default filter for database documents
     163             :             ::utl::OConfigurationTreeRoot aConfig(
     164             :                 ::utl::OConfigurationTreeRoot::createWithServiceFactory(
     165             :                     m_aContext.getLegacyServiceFactory(),
     166             :                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Setup/Office/Factories/com.sun.star.sdb.OfficeDatabaseDocument" ) )
     167           0 :             ) );
     168           0 :             ::rtl::OUString sDatabaseFilter;
     169           0 :             OSL_VERIFY( aConfig.getNodeValue( "ooSetupFactoryActualFilter" ) >>= sDatabaseFilter );
     170             : 
     171             :             // get the type this filter is responsible for
     172             :             Reference< XNameAccess > xFilterFactory(
     173             :                 m_aContext.createComponent( "com.sun.star.document.FilterFactory" ),
     174           0 :                 UNO_QUERY_THROW );
     175           0 :             ::comphelper::NamedValueCollection aFilterProperties( xFilterFactory->getByName( sDatabaseFilter ) );
     176           0 :             ::rtl::OUString sDocumentType = aFilterProperties.getOrDefault( "Type", ::rtl::OUString() );
     177             : 
     178             :             // get the extension(s) for this type
     179             :             Reference< XNameAccess > xTypeDetection(
     180             :                 m_aContext.createComponent( "com.sun.star.document.TypeDetection" ),
     181           0 :                 UNO_QUERY_THROW );
     182             : 
     183           0 :             ::comphelper::NamedValueCollection aTypeProperties( xTypeDetection->getByName( sDocumentType ) );
     184           0 :             m_aFilterExtensions = aTypeProperties.getOrDefault( "Extensions", m_aFilterExtensions );
     185           0 :             m_sFilterUIName = aTypeProperties.getOrDefault( "UIName", m_sFilterUIName );
     186             :         }
     187           0 :         catch( const Exception& )
     188             :         {
     189             :             DBG_UNHANDLED_EXCEPTION();
     190             :         }
     191             : 
     192             :         // ensure we have at least one extension
     193             :         OSL_ENSURE( m_aFilterExtensions.getLength(),
     194             :             "DatabaseLocationInputController_Impl::impl_initFilterProperties_nothrow: unable to determine the file extension(s)!" );
     195           0 :         if ( m_aFilterExtensions.getLength() == 0 )
     196             :         {
     197           0 :             m_aFilterExtensions.realloc(1);
     198           0 :             m_aFilterExtensions[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "*.odb" ) );
     199             :         }
     200           0 :     }
     201             : 
     202             :     // -----------------------------------------------------------------------------
     203           0 :     IMPL_LINK( DatabaseLocationInputController_Impl, OnControlAction, VclWindowEvent*, _pEvent )
     204             :     {
     205           0 :         if  (   ( _pEvent->GetWindow() == &m_rBrowseButton )
     206           0 :             &&  ( _pEvent->GetId() == VCLEVENT_BUTTON_CLICK )
     207             :             )
     208             :         {
     209           0 :             impl_onBrowseButtonClicked();
     210             :         }
     211             : 
     212           0 :         if  (   ( _pEvent->GetWindow() == &m_rLocationInput )
     213           0 :             &&  ( _pEvent->GetId() == VCLEVENT_EDIT_MODIFY )
     214             :             )
     215             :         {
     216           0 :             impl_onLocationModified();
     217             :         }
     218             : 
     219           0 :         return 0L;
     220             :     }
     221             : 
     222             :     // -----------------------------------------------------------------------------
     223           0 :     String DatabaseLocationInputController_Impl::impl_getCurrentURL() const
     224             :     {
     225           0 :         String sCurrentFile( m_rLocationInput.GetText() );
     226           0 :         if ( sCurrentFile.Len() )
     227             :         {
     228           0 :             ::svt::OFileNotation aCurrentFile( sCurrentFile );
     229           0 :             sCurrentFile = aCurrentFile.get( ::svt::OFileNotation::N_URL );
     230             :         }
     231           0 :         return sCurrentFile;
     232             :     }
     233             : 
     234             :     // -----------------------------------------------------------------------------
     235           0 :     void DatabaseLocationInputController_Impl::impl_onBrowseButtonClicked()
     236             :     {
     237             :         ::sfx2::FileDialogHelper aFileDlg(
     238             :             TemplateDescription::FILESAVE_AUTOEXTENSION,
     239             :             0,
     240           0 :             m_rLocationInput.GetSystemWindow()
     241           0 :         );
     242           0 :         aFileDlg.SetDisplayDirectory( impl_getCurrentURL() );
     243             : 
     244           0 :         aFileDlg.AddFilter( m_sFilterUIName, ::rtl::OUStringBuffer().appendAscii( "*." ).append( m_aFilterExtensions[0] ).makeStringAndClear() );
     245           0 :         aFileDlg.SetCurrentFilter( m_sFilterUIName );
     246             : 
     247           0 :         if ( aFileDlg.Execute() == ERRCODE_NONE )
     248             :         {
     249           0 :             INetURLObject aURL( aFileDlg.GetPath() );
     250           0 :             if( aURL.GetProtocol() != INET_PROT_NOT_VALID )
     251             :             {
     252           0 :                 ::svt::OFileNotation aFileNotation( aURL.GetMainURL( INetURLObject::NO_DECODE ) );
     253           0 :                 m_rLocationInput.SetText( aFileNotation.get( ::svt::OFileNotation::N_SYSTEM ) );
     254           0 :                 m_rLocationInput.GetModifyHdl().Call( &m_rLocationInput );
     255             :                 // the dialog already checked for the file's existence, so we don't need to, again
     256           0 :                 m_bNeedExistenceCheck = false;
     257           0 :             }
     258           0 :         }
     259           0 :     }
     260             : 
     261             :     // -----------------------------------------------------------------------------
     262           0 :     void DatabaseLocationInputController_Impl::impl_onLocationModified()
     263             :     {
     264           0 :         m_bNeedExistenceCheck = true;
     265           0 :     }
     266             : 
     267             :     //====================================================================
     268             :     //= DatabaseLocationInputController
     269             :     //====================================================================
     270             :     //--------------------------------------------------------------------
     271           0 :     DatabaseLocationInputController::DatabaseLocationInputController( const ::comphelper::ComponentContext& _rContext,
     272             :             ::svt::OFileURLControl& _rLocationInput, PushButton& _rBrowseButton )
     273           0 :         :m_pImpl( new DatabaseLocationInputController_Impl( _rContext, _rLocationInput, _rBrowseButton ) )
     274             :     {
     275           0 :     }
     276             : 
     277             :     //--------------------------------------------------------------------
     278           0 :     DatabaseLocationInputController::~DatabaseLocationInputController()
     279             :     {
     280           0 :     }
     281             : 
     282             :     //--------------------------------------------------------------------
     283           0 :     bool DatabaseLocationInputController::prepareCommit()
     284             :     {
     285           0 :         return m_pImpl->prepareCommit();
     286             :     }
     287             : 
     288             :     //--------------------------------------------------------------------
     289           0 :     void DatabaseLocationInputController::setURL( const String& _rURL )
     290             :     {
     291           0 :         m_pImpl->setURL( _rURL );
     292           0 :     }
     293             : 
     294             :     //--------------------------------------------------------------------
     295           0 :     String DatabaseLocationInputController::getURL() const
     296             :     {
     297           0 :         return m_pImpl->getURL();
     298             :     }
     299             : 
     300             : //........................................................................
     301             : } // namespace svx
     302             : //........................................................................
     303             : 
     304             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10