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

Generated by: LCOV version 1.10