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

Generated by: LCOV version 1.10