1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "svx/databaselocationinput.hxx"
31 #include "svx/dialmgr.hxx"
33 #include "svx/fmresids.hrc"
35 /** === begin UNO includes === **/
36 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
37 /** === end UNO includes === **/
39 #include <comphelper/componentcontext.hxx>
40 #include <comphelper/namedvaluecollection.hxx>
41 #include <rtl/ustrbuf.hxx>
42 #include <sfx2/filedlghelper.hxx>
43 #include <svtools/urlcontrol.hxx>
44 #include <svl/filenotation.hxx>
45 #include <tools/diagnose_ex.h>
46 #include <unotools/confignode.hxx>
47 #include <unotools/ucbhelper.hxx>
48 #include <vcl/button.hxx>
49 #include <vcl/msgbox.hxx>
51 //........................................................................
54 //........................................................................
56 /** === begin UNO using === **/
57 using ::com::sun::star::uno::Sequence
;
58 using ::com::sun::star::uno::Reference
;
59 using ::com::sun::star::container::XNameAccess
;
60 using ::com::sun::star::uno::UNO_QUERY_THROW
;
61 using ::com::sun::star::uno::Exception
;
62 /** === end UNO using === **/
63 namespace TemplateDescription
= ::com::sun::star::ui::dialogs::TemplateDescription
;
65 //====================================================================
66 //= DatabaseLocationInputController_Impl
67 //====================================================================
68 class DatabaseLocationInputController_Impl
71 DatabaseLocationInputController_Impl(
72 const ::comphelper::ComponentContext
& _rContext
,
73 ::svt::OFileURLControl
& _rLocationInput
,
74 PushButton
& _rBrowseButton
76 ~DatabaseLocationInputController_Impl();
79 void setURL( const String
& _rURL
);
80 String
getURL() const;
83 void impl_initFilterProperties_nothrow();
84 void impl_onBrowseButtonClicked();
85 void impl_onLocationModified();
86 String
impl_getCurrentURL() const;
88 DECL_LINK( OnControlAction
, VclWindowEvent
* );
91 const ::comphelper::ComponentContext m_aContext
;
92 ::svt::OFileURLControl
& m_rLocationInput
;
93 PushButton
& m_rBrowseButton
;
94 Sequence
< ::rtl::OUString
> m_aFilterExtensions
;
95 ::rtl::OUString m_sFilterUIName
;
96 bool m_bNeedExistenceCheck
;
99 //--------------------------------------------------------------------
100 DatabaseLocationInputController_Impl::DatabaseLocationInputController_Impl( const ::comphelper::ComponentContext
& _rContext
,
101 ::svt::OFileURLControl
& _rLocationInput
, PushButton
& _rBrowseButton
)
102 :m_aContext( _rContext
)
103 ,m_rLocationInput( _rLocationInput
)
104 ,m_rBrowseButton( _rBrowseButton
)
105 ,m_aFilterExtensions()
107 ,m_bNeedExistenceCheck( true )
109 impl_initFilterProperties_nothrow();
111 // forward the allowed extensions to the input control
112 ::rtl::OUStringBuffer aExtensionList
;
113 for ( const ::rtl::OUString
* pExtension
= m_aFilterExtensions
.getConstArray();
114 pExtension
!= m_aFilterExtensions
.getConstArray() + m_aFilterExtensions
.getLength();
118 aExtensionList
.append( *pExtension
);
119 aExtensionList
.append( (sal_Unicode
)';' );
121 m_rLocationInput
.SetFilter( aExtensionList
.makeStringAndClear() );
123 m_rBrowseButton
.AddEventListener( LINK( this, DatabaseLocationInputController_Impl
, OnControlAction
) );
124 m_rLocationInput
.AddEventListener( LINK( this, DatabaseLocationInputController_Impl
, OnControlAction
) );
127 //--------------------------------------------------------------------
128 DatabaseLocationInputController_Impl::~DatabaseLocationInputController_Impl()
130 m_rBrowseButton
.RemoveEventListener( LINK( this, DatabaseLocationInputController_Impl
, OnControlAction
) );
131 m_rLocationInput
.RemoveEventListener( LINK( this, DatabaseLocationInputController_Impl
, OnControlAction
) );
134 //--------------------------------------------------------------------
135 bool DatabaseLocationInputController_Impl::prepareCommit()
137 ::rtl::OUString
sURL( impl_getCurrentURL() );
138 if ( sURL
.isEmpty() )
141 // check if the name exists
142 if ( m_bNeedExistenceCheck
)
144 if ( ::utl::UCBContentHelper::Exists( sURL
) )
146 QueryBox
aBox( m_rLocationInput
.GetSystemWindow(), WB_YES_NO
, SVX_RES( RID_STR_ALREADYEXISTOVERWRITE
) );
147 if ( aBox
.Execute() != RET_YES
)
155 //--------------------------------------------------------------------
156 void DatabaseLocationInputController_Impl::setURL( const String
& _rURL
)
158 ::svt::OFileNotation
aTransformer( _rURL
);
159 m_rLocationInput
.SetText( aTransformer
.get( ::svt::OFileNotation::N_SYSTEM
) );
162 //--------------------------------------------------------------------
163 String
DatabaseLocationInputController_Impl::getURL() const
165 return impl_getCurrentURL();
168 //--------------------------------------------------------------------
169 void DatabaseLocationInputController_Impl::impl_initFilterProperties_nothrow()
173 // get the name of the default filter for database documents
174 ::utl::OConfigurationTreeRoot
aConfig(
175 ::utl::OConfigurationTreeRoot::createWithServiceFactory(
176 m_aContext
.getLegacyServiceFactory(),
177 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Setup/Office/Factories/com.sun.star.sdb.OfficeDatabaseDocument" ) )
179 ::rtl::OUString sDatabaseFilter
;
180 OSL_VERIFY( aConfig
.getNodeValue( "ooSetupFactoryActualFilter" ) >>= sDatabaseFilter
);
182 // get the type this filter is responsible for
183 Reference
< XNameAccess
> xFilterFactory(
184 m_aContext
.createComponent( "com.sun.star.document.FilterFactory" ),
186 ::comphelper::NamedValueCollection
aFilterProperties( xFilterFactory
->getByName( sDatabaseFilter
) );
187 ::rtl::OUString sDocumentType
= aFilterProperties
.getOrDefault( "Type", ::rtl::OUString() );
189 // get the extension(s) for this type
190 Reference
< XNameAccess
> xTypeDetection(
191 m_aContext
.createComponent( "com.sun.star.document.TypeDetection" ),
194 ::comphelper::NamedValueCollection
aTypeProperties( xTypeDetection
->getByName( sDocumentType
) );
195 m_aFilterExtensions
= aTypeProperties
.getOrDefault( "Extensions", m_aFilterExtensions
);
196 m_sFilterUIName
= aTypeProperties
.getOrDefault( "UIName", m_sFilterUIName
);
198 catch( const Exception
& )
200 DBG_UNHANDLED_EXCEPTION();
203 // ensure we have at least one extension
204 OSL_ENSURE( m_aFilterExtensions
.getLength(),
205 "DatabaseLocationInputController_Impl::impl_initFilterProperties_nothrow: unable to determine the file extension(s)!" );
206 if ( m_aFilterExtensions
.getLength() == 0 )
208 m_aFilterExtensions
.realloc(1);
209 m_aFilterExtensions
[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "*.odb" ) );
213 // -----------------------------------------------------------------------------
214 IMPL_LINK( DatabaseLocationInputController_Impl
, OnControlAction
, VclWindowEvent
*, _pEvent
)
216 if ( ( _pEvent
->GetWindow() == &m_rBrowseButton
)
217 && ( _pEvent
->GetId() == VCLEVENT_BUTTON_CLICK
)
220 impl_onBrowseButtonClicked();
223 if ( ( _pEvent
->GetWindow() == &m_rLocationInput
)
224 && ( _pEvent
->GetId() == VCLEVENT_EDIT_MODIFY
)
227 impl_onLocationModified();
233 // -----------------------------------------------------------------------------
234 String
DatabaseLocationInputController_Impl::impl_getCurrentURL() const
236 String
sCurrentFile( m_rLocationInput
.GetText() );
237 if ( sCurrentFile
.Len() )
239 ::svt::OFileNotation
aCurrentFile( sCurrentFile
);
240 sCurrentFile
= aCurrentFile
.get( ::svt::OFileNotation::N_URL
);
245 // -----------------------------------------------------------------------------
246 void DatabaseLocationInputController_Impl::impl_onBrowseButtonClicked()
248 ::sfx2::FileDialogHelper
aFileDlg(
249 TemplateDescription::FILESAVE_AUTOEXTENSION
,
251 m_rLocationInput
.GetSystemWindow()
253 aFileDlg
.SetDisplayDirectory( impl_getCurrentURL() );
255 aFileDlg
.AddFilter( m_sFilterUIName
, ::rtl::OUStringBuffer().appendAscii( "*." ).append( m_aFilterExtensions
[0] ).makeStringAndClear() );
256 aFileDlg
.SetCurrentFilter( m_sFilterUIName
);
258 if ( aFileDlg
.Execute() == ERRCODE_NONE
)
260 INetURLObject
aURL( aFileDlg
.GetPath() );
261 if( aURL
.GetProtocol() != INET_PROT_NOT_VALID
)
263 ::svt::OFileNotation
aFileNotation( aURL
.GetMainURL( INetURLObject::NO_DECODE
) );
264 m_rLocationInput
.SetText( aFileNotation
.get( ::svt::OFileNotation::N_SYSTEM
) );
265 m_rLocationInput
.GetModifyHdl().Call( &m_rLocationInput
);
266 // the dialog already checked for the file's existence, so we don't need to, again
267 m_bNeedExistenceCheck
= false;
272 // -----------------------------------------------------------------------------
273 void DatabaseLocationInputController_Impl::impl_onLocationModified()
275 m_bNeedExistenceCheck
= true;
278 //====================================================================
279 //= DatabaseLocationInputController
280 //====================================================================
281 //--------------------------------------------------------------------
282 DatabaseLocationInputController::DatabaseLocationInputController( const ::comphelper::ComponentContext
& _rContext
,
283 ::svt::OFileURLControl
& _rLocationInput
, PushButton
& _rBrowseButton
)
284 :m_pImpl( new DatabaseLocationInputController_Impl( _rContext
, _rLocationInput
, _rBrowseButton
) )
288 //--------------------------------------------------------------------
289 DatabaseLocationInputController::~DatabaseLocationInputController()
293 //--------------------------------------------------------------------
294 bool DatabaseLocationInputController::prepareCommit()
296 return m_pImpl
->prepareCommit();
299 //--------------------------------------------------------------------
300 void DatabaseLocationInputController::setURL( const String
& _rURL
)
302 m_pImpl
->setURL( _rURL
);
305 //--------------------------------------------------------------------
306 String
DatabaseLocationInputController::getURL() const
308 return m_pImpl
->getURL();
311 //........................................................................
313 //........................................................................
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */