update dev300-m58
[ooovba.git] / svx / source / form / databaselocationinput.cxx
blobb7d117b3def90d0cf8d80639b81207dfa8578de4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: databaselocationinput.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #include "svx/databaselocationinput.hxx"
35 #include "svx/dialmgr.hxx"
37 #include "fmresids.hrc"
39 /** === begin UNO includes === **/
40 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
41 /** === end UNO includes === **/
43 #include <comphelper/componentcontext.hxx>
44 #include <comphelper/namedvaluecollection.hxx>
45 #include <rtl/ustrbuf.hxx>
46 #include <sfx2/filedlghelper.hxx>
47 #include <svtools/urlcontrol.hxx>
48 #include <svtools/filenotation.hxx>
49 #include <tools/diagnose_ex.h>
50 #include <unotools/confignode.hxx>
51 #include <unotools/ucbhelper.hxx>
52 #include <vcl/button.hxx>
53 #include <vcl/msgbox.hxx>
55 //........................................................................
56 namespace svx
58 //........................................................................
60 /** === begin UNO using === **/
61 using ::com::sun::star::uno::Sequence;
62 using ::com::sun::star::uno::Reference;
63 using ::com::sun::star::container::XNameAccess;
64 using ::com::sun::star::uno::UNO_QUERY_THROW;
65 using ::com::sun::star::uno::Exception;
66 /** === end UNO using === **/
67 namespace TemplateDescription = ::com::sun::star::ui::dialogs::TemplateDescription;
69 //====================================================================
70 //= DatabaseLocationInputController_Impl
71 //====================================================================
72 class DatabaseLocationInputController_Impl
74 public:
75 DatabaseLocationInputController_Impl(
76 const ::comphelper::ComponentContext& _rContext,
77 ::svt::OFileURLControl& _rLocationInput,
78 PushButton& _rBrowseButton
80 ~DatabaseLocationInputController_Impl();
82 bool prepareCommit();
83 void setURL( const String& _rURL );
84 String getURL() const;
86 private:
87 void impl_initFilterProperties_nothrow();
88 void impl_onBrowseButtonClicked();
89 void impl_onLocationModified();
90 String impl_getCurrentURL() const;
92 DECL_LINK( OnControlAction, VclWindowEvent* );
94 private:
95 const ::comphelper::ComponentContext m_aContext;
96 ::svt::OFileURLControl& m_rLocationInput;
97 PushButton& m_rBrowseButton;
98 Sequence< ::rtl::OUString > m_aFilterExtensions;
99 ::rtl::OUString m_sFilterUIName;
100 bool m_bNeedExistenceCheck;
103 //--------------------------------------------------------------------
104 DatabaseLocationInputController_Impl::DatabaseLocationInputController_Impl( const ::comphelper::ComponentContext& _rContext,
105 ::svt::OFileURLControl& _rLocationInput, PushButton& _rBrowseButton )
106 :m_aContext( _rContext )
107 ,m_rLocationInput( _rLocationInput )
108 ,m_rBrowseButton( _rBrowseButton )
109 ,m_aFilterExtensions()
110 ,m_sFilterUIName()
111 ,m_bNeedExistenceCheck( true )
113 impl_initFilterProperties_nothrow();
115 // forward the allowed extensions to the input control
116 ::rtl::OUStringBuffer aExtensionList;
117 for ( const ::rtl::OUString* pExtension = m_aFilterExtensions.getConstArray();
118 pExtension != m_aFilterExtensions.getConstArray() + m_aFilterExtensions.getLength();
119 ++pExtension
122 aExtensionList.append( *pExtension );
123 aExtensionList.append( (sal_Unicode)';' );
125 m_rLocationInput.SetFilter( aExtensionList.makeStringAndClear() );
127 m_rBrowseButton.AddEventListener( LINK( this, DatabaseLocationInputController_Impl, OnControlAction ) );
128 m_rLocationInput.AddEventListener( LINK( this, DatabaseLocationInputController_Impl, OnControlAction ) );
131 //--------------------------------------------------------------------
132 DatabaseLocationInputController_Impl::~DatabaseLocationInputController_Impl()
134 m_rBrowseButton.RemoveEventListener( LINK( this, DatabaseLocationInputController_Impl, OnControlAction ) );
135 m_rLocationInput.RemoveEventListener( LINK( this, DatabaseLocationInputController_Impl, OnControlAction ) );
138 //--------------------------------------------------------------------
139 bool DatabaseLocationInputController_Impl::prepareCommit()
141 ::rtl::OUString sURL( impl_getCurrentURL() );
142 if ( !sURL.getLength() )
143 return false;
145 // check if the name exists
146 if ( m_bNeedExistenceCheck )
148 if ( ::utl::UCBContentHelper::Exists( sURL ) )
150 QueryBox aBox( m_rLocationInput.GetSystemWindow(), WB_YES_NO, SVX_RES( RID_STR_ALREADYEXISTOVERWRITE ) );
151 if ( aBox.Execute() != RET_YES )
152 return false;
156 return true;
159 //--------------------------------------------------------------------
160 void DatabaseLocationInputController_Impl::setURL( const String& _rURL )
162 ::svt::OFileNotation aTransformer( _rURL );
163 m_rLocationInput.SetText( aTransformer.get( ::svt::OFileNotation::N_SYSTEM ) );
166 //--------------------------------------------------------------------
167 String DatabaseLocationInputController_Impl::getURL() const
169 return impl_getCurrentURL();
172 //--------------------------------------------------------------------
173 void DatabaseLocationInputController_Impl::impl_initFilterProperties_nothrow()
177 // get the name of the default filter for database documents
178 ::utl::OConfigurationTreeRoot aConfig(
179 ::utl::OConfigurationTreeRoot::createWithServiceFactory(
180 m_aContext.getLegacyServiceFactory(),
181 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Setup/Office/Factories/com.sun.star.sdb.OfficeDatabaseDocument" ) )
182 ) );
183 ::rtl::OUString sDatabaseFilter;
184 OSL_VERIFY( aConfig.getNodeValue( "ooSetupFactoryActualFilter" ) >>= sDatabaseFilter );
186 // get the type this filter is responsible for
187 Reference< XNameAccess > xFilterFactory(
188 m_aContext.createComponent( "com.sun.star.document.FilterFactory" ),
189 UNO_QUERY_THROW );
190 ::comphelper::NamedValueCollection aFilterProperties( xFilterFactory->getByName( sDatabaseFilter ) );
191 ::rtl::OUString sDocumentType = aFilterProperties.getOrDefault( "Type", ::rtl::OUString() );
193 // get the extension(s) for this type
194 Reference< XNameAccess > xTypeDetection(
195 m_aContext.createComponent( "com.sun.star.document.TypeDetection" ),
196 UNO_QUERY_THROW );
198 ::comphelper::NamedValueCollection aTypeProperties( xTypeDetection->getByName( sDocumentType ) );
199 m_aFilterExtensions = aTypeProperties.getOrDefault( "Extensions", m_aFilterExtensions );
200 m_sFilterUIName = aTypeProperties.getOrDefault( "UIName", m_sFilterUIName );
202 catch( const Exception& )
204 DBG_UNHANDLED_EXCEPTION();
207 // ensure we have at least one extension
208 OSL_ENSURE( m_aFilterExtensions.getLength(),
209 "DatabaseLocationInputController_Impl::impl_initFilterProperties_nothrow: unable to determine the file extension(s)!" );
210 if ( m_aFilterExtensions.getLength() == 0 )
212 m_aFilterExtensions.realloc(1);
213 m_aFilterExtensions[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "*.odb" ) );
217 // -----------------------------------------------------------------------------
218 IMPL_LINK( DatabaseLocationInputController_Impl, OnControlAction, VclWindowEvent*, _pEvent )
220 if ( ( _pEvent->GetWindow() == &m_rBrowseButton )
221 && ( _pEvent->GetId() == VCLEVENT_BUTTON_CLICK )
224 impl_onBrowseButtonClicked();
227 if ( ( _pEvent->GetWindow() == &m_rLocationInput )
228 && ( _pEvent->GetId() == VCLEVENT_EDIT_MODIFY )
231 impl_onLocationModified();
234 return 0L;
237 // -----------------------------------------------------------------------------
238 String DatabaseLocationInputController_Impl::impl_getCurrentURL() const
240 String sCurrentFile( m_rLocationInput.GetText() );
241 if ( sCurrentFile.Len() )
243 ::svt::OFileNotation aCurrentFile( sCurrentFile );
244 sCurrentFile = aCurrentFile.get( ::svt::OFileNotation::N_URL );
246 return sCurrentFile;
249 // -----------------------------------------------------------------------------
250 void DatabaseLocationInputController_Impl::impl_onBrowseButtonClicked()
252 ::sfx2::FileDialogHelper aFileDlg(
253 TemplateDescription::FILESAVE_AUTOEXTENSION,
254 WB_STDMODAL | WB_SAVEAS,
255 m_rLocationInput.GetSystemWindow()
257 aFileDlg.SetDisplayDirectory( impl_getCurrentURL() );
259 aFileDlg.AddFilter( m_sFilterUIName, ::rtl::OUStringBuffer().appendAscii( "*." ).append( m_aFilterExtensions[0] ).makeStringAndClear() );
260 aFileDlg.SetCurrentFilter( m_sFilterUIName );
262 if ( aFileDlg.Execute() == ERRCODE_NONE )
264 INetURLObject aURL( aFileDlg.GetPath() );
265 if( aURL.GetProtocol() != INET_PROT_NOT_VALID )
267 ::svt::OFileNotation aFileNotation( aURL.GetMainURL( INetURLObject::NO_DECODE ) );
268 m_rLocationInput.SetText( aFileNotation.get( ::svt::OFileNotation::N_SYSTEM ) );
269 m_rLocationInput.GetModifyHdl().Call( &m_rLocationInput );
270 // the dialog already checked for the file's existence, so we don't need to, again
271 m_bNeedExistenceCheck = false;
276 // -----------------------------------------------------------------------------
277 void DatabaseLocationInputController_Impl::impl_onLocationModified()
279 m_bNeedExistenceCheck = true;
282 //====================================================================
283 //= DatabaseLocationInputController
284 //====================================================================
285 //--------------------------------------------------------------------
286 DatabaseLocationInputController::DatabaseLocationInputController( const ::comphelper::ComponentContext& _rContext,
287 ::svt::OFileURLControl& _rLocationInput, PushButton& _rBrowseButton )
288 :m_pImpl( new DatabaseLocationInputController_Impl( _rContext, _rLocationInput, _rBrowseButton ) )
292 //--------------------------------------------------------------------
293 DatabaseLocationInputController::~DatabaseLocationInputController()
297 //--------------------------------------------------------------------
298 bool DatabaseLocationInputController::prepareCommit()
300 return m_pImpl->prepareCommit();
303 //--------------------------------------------------------------------
304 void DatabaseLocationInputController::setURL( const String& _rURL )
306 m_pImpl->setURL( _rURL );
309 //--------------------------------------------------------------------
310 String DatabaseLocationInputController::getURL() const
312 return m_pImpl->getURL();
315 //........................................................................
316 } // namespace svx
317 //........................................................................